Beispiel #1
0
        protected override void Handle(FetchChannelsRequestPacket packet, Socket sender)
        {
            var data = packet.Data;

            if (data == null)
            {
                return;
            }

            // check whether request is valid
            ActiveUser activeUser = ActiveUsers.GetUser(sender);

            if (activeUser == null)
            {
                SendError(HttpStatusCode.Unauthorized, sender);
                return;
            }

            User user = userCrud.ReadOneById(activeUser.userId);

            // fetch channels that match search
            List <BaseChannelData> channels = channelCrud.ReadManyBase(x =>
                                                                       x.members.Contains(user._id) &&
                                                                       x.name.ToLower().Contains(data.search.ToLower())
                                                                       );

            var response = new FetchChannelsResponsePacket(new FetchChannelsResponsePacketData(
                                                               (int)HttpStatusCode.OK,
                                                               channels
                                                               ));

            Zephy.serverSocket.SendPacket(response, sender);
        }
        protected override void Handle(ModifyMembersRequestPacket packet, Socket sender)
        {
            var data = packet.Data;

            if (data == null)
            {
                return;
            }

            ListedUser user    = userCrud.ReadOneListedById(data.user);
            Channel    channel = channelCrud.ReadOneById(data.channel);

            if (user == null || channel == null)
            {
                SendError(HttpStatusCode.BadRequest, sender);
                return;
            }

            if (data.action == MemberAction.ADD_MEMBER)
            {
                if (channel.members.Contains(user._id))
                {
                    SendError(HttpStatusCode.Conflict, sender);
                    return;
                }
                channel.members.Add(user._id);
                channelCrud.UpdateOne(channel._id, channel);
            }
            else if (data.action == MemberAction.REMOVE_MEMBER)
            {
                if (!channel.members.Contains(user._id))
                {
                    SendError(HttpStatusCode.NotFound, sender);
                    return;
                }
                channel.members.Remove(user._id);
                channelCrud.UpdateOne(channel._id, channel);
            }


            var response = new ModifyMembersResponsePacket(new ModifyMembersResponsePacketData(
                                                               (int)HttpStatusCode.OK, user, channel._id, data.action
                                                               ));

            if (ActiveUsers.IsLoggedIn(data.user))
            {
                Zephy.serverSocket.SendPacket(response, ActiveUsers.GetUser(data.user).clientSocket);
            }

            foreach (string memberId in channel.members)
            {
                if (ActiveUsers.IsLoggedIn(memberId))
                {
                    Zephy.serverSocket.SendPacket(response, ActiveUsers.GetUser(memberId).clientSocket);
                }
            }
        }
Beispiel #3
0
        protected override void Handle(SendMessageRequestPacket packet, Socket sender)
        {
            var data = packet.Data;

            if (data == null)
            {
                return;
            }

            // check if request is valid
            Channel channel = channelCrud.ReadOneById(data.forChannel);

            if (
                string.IsNullOrWhiteSpace(data.forChannel) ||
                string.IsNullOrWhiteSpace(data.content) ||
                channel == null)
            {
                SendError(HttpStatusCode.BadRequest, sender);
                return;
            }

            ActiveUser senderUser = ActiveUsers.GetUser(sender);

            if (senderUser == null || !channel.members.Contains(senderUser.userId))
            {
                SendError(HttpStatusCode.Unauthorized, sender);
                return;
            }


            // add new message to database
            var newMessage = new Message
            {
                content = data.content,
                sentAt  = Util.ToUnixTimestamp(DateTime.Now),
                author  = senderUser.userId,
                channel = channel._id,
            };

            messageCrud.CreateOne(newMessage);

            channelCrud.UpdateOne(channel._id, channel);

            // send response to members of channel (only if success!)
            var responsePacket = new SendMessageResponsePacket(new SendMessageResponsePacketData(
                                                                   (int)HttpStatusCode.OK, messageCrud.ReadOnePopulated(newMessage._id), channel._id
                                                                   ));

            foreach (string memberId in channel.members)
            {
                if (ActiveUsers.IsLoggedIn(memberId))
                {
                    Zephy.serverSocket.SendPacket(responsePacket, ActiveUsers.GetUser(memberId).clientSocket);
                }
            }
        }
        protected override void Handle(CreateChannelRequestPacket packet, Socket sender)
        {
            var data = packet.Data;

            if (data == null)
            {
                return;
            }

            // check whether request is valid
            if (data.withMembers.Count <= 1 || Util.HasDuplicates(data.withMembers))
            {
                SendError(HttpStatusCode.BadRequest, sender);
                return;
            }

            ActiveUser user = ActiveUsers.GetUser(sender);

            if (user == null)
            {
                SendError(HttpStatusCode.Unauthorized, sender);
                return;
            }

            if (channelCrud.Exists(data.withMembers))
            {
                SendError(HttpStatusCode.Conflict, sender);
                return;
            }

            // create actual channel
            var newChannel = new Channel()
            {
                name    = data.name,
                members = data.withMembers,
            };

            channelCrud.CreateOne(newChannel);

            var successResponse = new CreateChannelResponsePacket(new CreateChannelResponsePacketData(
                                                                      (int)HttpStatusCode.OK,
                                                                      newChannel
                                                                      ));

            Zephy.serverSocket.SendPacket(successResponse, sender);
        }
Beispiel #5
0
        protected override void Handle(FetchUserListRequestPacket packet, Socket sender)
        {
            var data = packet.Data;

            if (data == null)
            {
                return;
            }
            data.search = data.search.ToLower();

            ActiveUser activeUser = ActiveUsers.GetUser(sender);

            if (activeUser == null)
            {
                SendError(HttpStatusCode.Unauthorized, sender);
                return;
            }

            User user = userCrud.ReadOneById(activeUser.userId);

            // Fetch all users that match the search & aren't the requester
            List <ListedUser> users = userCrud.ReadManyListedPaginated(
                data.page, PAGE_SIZE,
                x =>
                (x.identifier.ToLower().Contains(data.search) ||
                 x.fullName.ToLower().Contains(data.search)) &&
                x._id != user._id
                );



            // remove excludes
            if (data.optionalExcludeIds != null)
            {
                users.RemoveAll(x => data.optionalExcludeIds.Contains(x._id));
            }

            var response = new FetchUserListResponsePacket(new FetchUserListResponsePacketData(
                                                               (int)HttpStatusCode.OK,
                                                               data.page,
                                                               users
                                                               ));

            Zephy.serverSocket.SendPacket(response, sender);
        }
        protected override void Handle(ModifyChannelRequestPacket packet, Socket sender)
        {
            var data = packet.Data;

            if (data == null)
            {
                return;
            }

            Channel channel = channelCrud.ReadOneById(data.channel);

            if (channel == null)
            {
                SendError(HttpStatusCode.NotFound, sender);
                return;
            }

            if (data.data.Length < 2 && data.data.Length > 32)
            {
                SendError(HttpStatusCode.BadRequest, sender);
                return;
            }

            channel.name = data.data;
            channelCrud.UpdateOne(channel._id, channel);

            var response = new ModifyChannelResponsePacket(new ModifyChannelResponsePacketData(
                                                               (int)HttpStatusCode.OK, channel._id, data.action, data.data
                                                               ));

            foreach (string memberId in channel.members)
            {
                if (ActiveUsers.IsLoggedIn(memberId))
                {
                    Zephy.serverSocket.SendPacket(response, ActiveUsers.GetUser(memberId).clientSocket);
                }
            }
        }