Ejemplo n.º 1
0
        /// <summary>
        /// Gets the current callback channel and adds it to the collection of all channels.
        /// </summary>
        /// <returns></returns>
        public ChannelWrapper RegisterClient(Message message)
        {
            var manager = new NewsfeedManager();

            string username;
            if (manager.TryGetCurrentUsername(message, out username))
            {
                ChannelWrapper wrapper;

                //If the user is logged and resfresh the page, a new channel will be opened and the old should be removed
                if (this.clients.TryGetValue(username, out wrapper))
                {
                    this.clients.Remove(username);
                }

                var client = OperationContext.Current.GetCallbackChannel<INewsfeedServiceCallback>();

                wrapper = new ChannelWrapper(username, client);
                wrapper.Closed += Connection_Closed;
                wrapper.Faulted += Connection_Closed;

                var usersRepository = new Domain.UserRepository();
                wrapper.User = usersRepository.Get(username);

                this.clients.Add(username, wrapper);

                return wrapper;
            }

            return null;
        }
Ejemplo n.º 2
0
        private void BroadcastMessage(Model.Message message)
        {
            //Prevent modifying the collection when someone is sending message
            lock (NewsfeedService.clientsLock)
            {
                var manager = new NewsfeedManager();
                foreach (var client in ClientsManager.Instance.Clients)
                {
                    try
                    {
                        //get only usernames from the BsonDosument collection
                        var blockedUsernames = client.Value.User.BlockedUsers.Select(u => u.GetElement("Username").Value.AsString);
                        if (blockedUsernames.Contains(message.Username))
                        {
                            continue;
                        }

                        client.Value.Callback.Send(manager.CreateMessage(message));
                    }
                    catch
                    {
                    }
                }
                ClientsManager.Instance.ClearFailed();
            }
        }
Ejemplo n.º 3
0
 private void SendOlderMessagesToClient(IEnumerable<Domain.Message> messages, NewsfeedManager manager)
 {
     foreach (var m in messages)
     {
         var uiMessage = manager.MapDomainMessageToClient(m);
         uiMessage.Action = ServiceAction.ShowMore.ToString();
         this.currentClient.Callback.Send(manager.CreateMessage(uiMessage));
     }
 }
Ejemplo n.º 4
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));
            }
        }
Ejemplo n.º 5
0
        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);
            }
        }