public Model.Message MapDomainMessageToClient(Domain.Message message) { var model = new Model.Message() { Id = message.Id.ToString(), SentDate = message.SentDate, Text = message.Text, Likes = message.Likes, SenderId = message.Author.GetValue("Id").ToString(), Username = message.Author.GetValue("Username").ToString(), }; BsonValue avatar; if (message.Author.TryGetValue("Avatar", out avatar)) model.AvatarId = avatar.ToString(); return model; }
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)); } }
private void SendLikeNotification(Model.Message content, string username) { var notification = new Model.Message() { Action = ServiceAction.Notification.ToString(), Text = String.Format("{0} liked your message: {1}", username, content.Text), SentDate = DateTime.Now }; var manager = new NewsfeedManager(); var notificationMessage = manager.CreateMessage(notification); ChannelWrapper client; if (ClientsManager.Instance.Clients.TryGetValue(content.Username, out client)) { //TODO: save the notifcation in the database so the user can recieve it later when he is logged; client.Callback.Send(notificationMessage); } }