예제 #1
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);
                }
            }
        }
예제 #2
0
        public override void Seed(SeederEntriesAmount amount)
        {
            if ((amount.messageSeederAmount < 0 || amount.userSeederAmount > MAX_SEED) ||
                (amount.channelSeederAmount < 0 || amount.channelSeederAmount > MAX_SEED))
            {
                throw new ArgumentException("out of bounds", "amount");
            }

            if (messageCrud.DocumentCount > 0 || channelCrud.DocumentCount > 0)
            {
                return;
            }


            int messagesPerChannel = (int)((float)amount.messageSeederAmount / amount.channelSeederAmount);

            List <User.User> users = userCrud.ReadMany();

            for (int i = 0; i < amount.channelSeederAmount; i++)
            {
                Channel newChannel = new Channel
                {
                    _id     = ObjectId.GenerateNewId().ToString(),
                    name    = Faker.Internet.DomainWord(),
                    members = new List <string>(),
                };

                const int CHANNEL_MEMBER_COUNT = 2;

                for (int j = 0; j < CHANNEL_MEMBER_COUNT; j++)
                {
                    string rndUserId;
                    do
                    {
                        rndUserId = users[rnd.Next(users.Count)]._id;
                    } while (newChannel.members.Contains(rndUserId));

                    newChannel.members.Add(rndUserId);
                }


                for (int m = 0; m < messagesPerChannel; m++)
                {
                    if (newChannel.members.Count == 0)
                    {
                        break;
                    }

                    string          author       = newChannel.members[rnd.Next(newChannel.members.Count)];
                    int             rndTimestamp = Util.Util.RandTimestamp();
                    Message.Message msg          = new Message.Message
                    {
                        _id     = ObjectId.GenerateNewId().ToString(),
                        content = rnd.Next(1, 10) < 8 ? Faker.Lorem.Sentence(20) : "ok",
                        author  = author,
                        sentAt  = rndTimestamp,
                        channel = newChannel._id
                    };

                    messageCrud.CreateOne(msg);
                }

                channelCrud.CreateOne(newChannel);
            }
        }