Exemplo n.º 1
0
 public void LikeMessage(Model.Message message)
 {
     if (!string.IsNullOrEmpty(message.Id))
     {
         var repo = new Domain.MessageRepository();
         repo.UpdateMessageLikes(new ObjectId(message.Id), 1);
         message.Likes++;
     }
 }
Exemplo n.º 2
0
        public void Recieve(Message message)
        {
            var manager = new NewsfeedManager();

            string username;
            if (!manager.TryGetCurrentUsername(message, out username) || !manager.ValidateSameOrigin(message))
            {
                //If the sender is not logged user or the message comes from different domain do nothing
                return;
            }

            if (!message.IsEmpty) //New message is recieved
            {
                var content = manager.GetMessage(message);

                var action = (ServiceAction)Enum.Parse(typeof(ServiceAction), content.Action);

                switch (action)
                {
                    case ServiceAction.ShowMore:
                        var messagesRepo = new Domain.MessageRepository();
                        var messages = messagesRepo.GetLatestMessages(content.DisplayedMessages, 20, this.currentClient.User.BlockedUsers);
                        this.SendOlderMessagesToClient(messages, manager);
                        break;
                    case ServiceAction.NewMessage:
                        manager.SaveMessage(content, this.currentClient.User);
                        this.BroadcastMessage(content);
                        break;
                    case ServiceAction.LikeMessage:
                        manager.LikeMessage(content);
                        this.BroadcastMessage(content);
                        this.SendLikeNotification(content, username);
                        break;
                    case ServiceAction.BlockUser:
                        manager.BlockUser(this.currentClient.User, content);
                        break;
                    default:
                        break;
                }
            }
            else //New connection has been created and this is her opening message
            {
                ClientsManager.Instance.ClearFailed();

                //Associate the opened channel with the logged user
                this.currentClient = ClientsManager.Instance.RegisterClient(message);

                var messagesRepo = new Domain.MessageRepository();
                var messages = messagesRepo.GetLatestMessages(0, 20, this.currentClient.User.BlockedUsers);

                this.SendOlderMessagesToClient(messages, manager);

                var hello = new Model.Message()
                {
                    Action = ServiceAction.Notification.ToString(),
                    Text = "Welcome to the newsfeed!",
                    SentDate = DateTime.Now
                };
                this.currentClient.Callback.Send(manager.CreateMessage(hello));
            }
        }
Exemplo n.º 3
0
        public void SaveMessage(Model.Message message, Domain.User user)
        {
            var messagesRepo = new Domain.MessageRepository();
            var usersRepo = new Domain.UserRepository();

            //a new message has been sent to the server
            message.SentDate = DateTime.Now;

            message.Username = user.Username;
            message.SenderId = user.Id.ToString();
            message.AvatarId = user.Avatar.ToString();

            //Save the message to the db
            var messageDto = MapClientMessageToDomain(message);

            messagesRepo.InsertMessage(messageDto);

            message.Id = messageDto.Id.ToString();
        }