예제 #1
0
        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);
                }
            }
        }
예제 #2
0
        private void SendError(HttpStatusCode code, Socket sender)
        {
            var errResponse = new ModifyMembersResponsePacket(new ModifyMembersResponsePacketData(
                                                                  (int)code,
                                                                  null,
                                                                  "",
                                                                  -1
                                                                  ));

            Zephy.serverSocket.SendPacket(errResponse, sender);
        }