Esempio n. 1
0
        public override async void SendMessage(string command)
        {
            var message = new jm.ClientMessage {
                Id      = Guid.NewGuid().ToString(),
                Room    = this.Name,
                Content = command
            };

            if (!command.TrimStart().StartsWith("/", StringComparison.Ordinal))
            {
                OnMessageReceived(new MessageEventArgs(new ChannelMessage(message.Id, DateTimeOffset.Now, Server.CurrentUser.Name, command)));
            }
            try
            {
                await Server.Client.Send(message);
            }
            catch (Exception ex)
            {
                Application.Instance.Invoke(() => {
                    MessageBox.Show(
                        Application.Instance.MainForm,
                        string.Format("Error sending message: {0}", ex)
                        );
                });
            }
        }
Esempio n. 2
0
        public override async void SendMessage(string command)
        {
            if (Client == null)
            {
                OnGlobalMessageReceived(new NotificationEventArgs(new NotificationMessage(DateTimeOffset.Now, "Cannot send command. You are not connected.")));
                return;
            }
            var message = new jm.ClientMessage
            {
                Id      = Guid.NewGuid().ToString(),
                Content = command
            };

            try
            {
                await Client.Send(message);
            }
            catch (Exception ex)
            {
                Application.Instance.Invoke(() => MessageBox.Show(Application.Instance.MainForm, string.Format("Error sending message: {0}", ex)));
            }
        }
Esempio n. 3
0
 public override async void SendMessage(string command)
 {
     var message = new jm.ClientMessage{
         Id = Guid.NewGuid ().ToString (),
         Room = this.Name,
         Content = command
     };
     if (!command.TrimStart().StartsWith("/"))
     {
         OnMessageReceived(new MessageEventArgs(new ChannelMessage(message.Id, DateTimeOffset.Now, Server.CurrentUser.Name, command)));
     }
     try
     {
         await Server.Client.Send(message);
     }
     catch (Exception ex)
     {
         Application.Instance.Invoke(() => {
             MessageBox.Show(
                 Application.Instance.MainForm,
                 string.Format("Error sending message: {0}", ex)
             );
         });
     }
 }
Esempio n. 4
0
        public bool Send(ClientMessage message)
        {
            bool outOfSync = OutOfSync;

            SetVersion();

            // Sanitize the content (strip and bad html out)
            message.Content = HttpUtility.HtmlEncode(message.Content);

            // See if this is a valid command (starts with /)
            if (TryHandleCommand(message.Content, message.Room))
            {
                return outOfSync;
            }

            string id = GetUserId();

            ChatUser user = _repository.VerifyUserId(id);
            ChatRoom room = _repository.VerifyUserRoom(_cache, user, message.Room);

            // REVIEW: Is it better to use _repository.VerifyRoom(message.Room, mustBeOpen: false)
            // here?
            if (room.Closed)
            {
                throw new InvalidOperationException(String.Format("You cannot post messages to '{0}'. The room is closed.", message.Room));
            }

            // Update activity *after* ensuring the user, this forces them to be active
            UpdateActivity(user, room);

            HashSet<string> links;
            var messageText = ParseChatMessageText(message.Content, out links);

            ChatMessage chatMessage = _service.AddMessage(user, room, message.Id, messageText);

            var messageViewModel = new MessageViewModel(chatMessage);
            Clients[room.Name].addMessage(messageViewModel, room.Name);

            _repository.CommitChanges();

            string clientMessageId = chatMessage.Id;

            // Update the id on the message
            chatMessage.Id = Guid.NewGuid().ToString("d");
            _repository.CommitChanges();

            if (!links.Any())
            {
                return outOfSync;
            }

            ProcessUrls(links, room.Name, clientMessageId, chatMessage.Id);

            return outOfSync;
        }
Esempio n. 5
0
        public bool Send(string content, string roomName)
        {
            var message = new ClientMessage
            {
                Content = content,
                Id = Guid.NewGuid().ToString("d"),
                Room = roomName
            };

            return Send(message);
        }
Esempio n. 6
0
        public bool Send(ClientMessage message)
        {
            bool outOfSync = OutOfSync;

            SetVersion();

            // See if this is a valid command (starts with /)
            if (TryHandleCommand(message.Content, message.Room))
            {
                return outOfSync;
            }

            var userId = Context.User.Identity.Name;

            ChatUser user = _repository.VerifyUserId(userId);
            ChatRoom room = _repository.VerifyUserRoom(_cache, user, message.Room);

            // REVIEW: Is it better to use _repository.VerifyRoom(message.Room, mustBeOpen: false)
            // here?
            if (room.Closed)
            {
                throw new InvalidOperationException(String.Format("You cannot post messages to '{0}'. The room is closed.", message.Room));
            }

            // Update activity *after* ensuring the user, this forces them to be active
            UpdateActivity(user, room);

            ChatMessage chatMessage = _service.AddMessage(user, room, message.Id, message.Content);

            var messageViewModel = new MessageViewModel(chatMessage);
            Clients.Group(room.Name).addMessage(messageViewModel, room.Name);

            _repository.CommitChanges();

            string clientMessageId = chatMessage.Id;

            // Update the id on the message
            chatMessage.Id = Guid.NewGuid().ToString("d");
            _repository.CommitChanges();

            var urls = UrlExtractor.ExtractUrls(chatMessage.Content);
            if (urls.Count > 0)
            {
                ProcessUrls(urls, room.Name, clientMessageId, message.Id);
            }

            return outOfSync;
        }
Esempio n. 7
0
        public bool Send(ClientMessage message)
        {
            bool outOfSync = OutOfSync;

            SetVersion();

            // Sanitize the content (strip and bad html out)
            message.Content = HttpUtility.HtmlEncode(message.Content);

            // See if this is a valid command (starts with /)
            if (TryHandleCommand(message.Content, message.Room))
            {
                return outOfSync;
            }

            string id = Caller.id;

            ChatUser user = _repository.VerifyUserId(id);
            ChatRoom room = _repository.VerifyUserRoom(user, message.Room);

            // Update activity *after* ensuring the user, this forces them to be active
            UpdateActivity(user, room);

            HashSet<string> links;
            var messageText = ParseChatMessageText(message.Content, out links);

            ChatMessage chatMessage = _service.AddMessage(user, room, message.Id, messageText);

            var messageViewModel = new MessageViewModel(chatMessage);
            Clients[room.Name].addMessage(messageViewModel, room.Name);

            _repository.CommitChanges();

            string clientMessageId = chatMessage.Id;

            // Update the id on the message
            chatMessage.Id = Guid.NewGuid().ToString("d");
            _repository.CommitChanges();

            if (!links.Any())
            {
                return outOfSync;
            }

            ProcessUrls(links, room.Name, clientMessageId, chatMessage.Id);

            return outOfSync;
        }
Esempio n. 8
0
 public override async void SendMessage(string command)
 {
     if (Client == null)
     {
         OnGlobalMessageReceived(new NotificationEventArgs(new NotificationMessage(DateTimeOffset.Now, "Cannot send command. You are not connected.")));
         return;
     }
     var message = new jm.ClientMessage{
         Id = Guid.NewGuid ().ToString (),
         Content = command
     };
     try
     {
         await Client.Send(message);
     }
     catch (Exception ex)
     {
         Application.Instance.Invoke(() => {
             MessageBox.Show(Application.Instance.MainForm, string.Format("Error sending message: {0}", ex));
         });
     }
     ;
 }
Esempio n. 9
0
        public bool Send(ClientMessage clientMessage)
        {
            CheckStatus();

            // See if this is a valid command (starts with /)
            if (TryHandleCommand(clientMessage.Content, clientMessage.Room))
            {
                return true;
            }

            var userId = Context.User.Identity.Name;

            ChatUser user = _repository.VerifyUserId(userId);
            ChatRoom room = _repository.VerifyUserRoom(_cache, user, clientMessage.Room);

            if (room == null || (room.Private && !user.AllowedRooms.Contains(room)))
            {
                return false;
            }

            // REVIEW: Is it better to use _repository.VerifyRoom(message.Room, mustBeOpen: false)
            // here?
            if (room.Closed)
            {
                throw new InvalidOperationException(String.Format("You cannot post messages to '{0}'. The room is closed.", clientMessage.Room));
            }

            // Update activity *after* ensuring the user, this forces them to be active
            UpdateActivity(user, room);

            // Create a true unique id and save the message to the db
            string id = Guid.NewGuid().ToString("d");
            ChatMessage chatMessage = _service.AddMessage(user, room, id, clientMessage.Content);
            _repository.CommitChanges();

            var messageViewModel = new MessageViewModel(chatMessage);

            if (clientMessage.Id == null)
            {
                // If the client didn't generate an id for the message then just
                // send it to everyone. The assumption is that the client has some ui
                // that it wanted to update immediately showing the message and
                // then when the actual message is roundtripped it would "solidify it".
                Clients.Group(room.Name).addMessage(messageViewModel, room.Name);
            }
            else
            {
                // If the client did set an id then we need to give everyone the real id first
                Clients.OthersInGroup(room.Name).addMessage(messageViewModel, room.Name);

                // Now tell the caller to replace the message
                Clients.Caller.replaceMessage(clientMessage.Id, messageViewModel, room.Name);
            }

            // Add mentions
            AddMentions(chatMessage);

            var urls = UrlExtractor.ExtractUrls(chatMessage.Content);
            if (urls.Count > 0)
            {
                _resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id);
            }

            return true;
        }
Esempio n. 10
0
        public bool Send(string content, string roomName)
        {
            var message = new ClientMessage
            {
                Content = content,
                Room = roomName
            };

            return Send(message);
        }
Esempio n. 11
0
        public bool Send(ClientMessage clientMessage)
        {
            CheckStatus();

            // reject it if it's too long
            if (_settings.MaxMessageLength > 0 && clientMessage.Content.Length > _settings.MaxMessageLength)
            {
                throw new HubException(String.Format(LanguageResources.SendMessageTooLong, _settings.MaxMessageLength));
            }

            // See if this is a valid command (starts with /)
            if (TryHandleCommand(clientMessage.Content, clientMessage.Room))
            {
                return true;
            }

            var userId = Context.User.GetUserId();

            var user = _repository.VerifyUserId(userId);
            var room = _repository.VerifyUserRoom(_cache, user, clientMessage.Room);
            var roomUserData = _repository.GetRoomUserData(user, room);

            if (room == null || (room.Private && !user.AllowedRooms.Contains(room)))
            {
                return false;
            }

            // REVIEW: Is it better to use the extension method room.EnsureOpen here?
            if (room.Closed)
            {
                throw new HubException(String.Format(LanguageResources.SendMessageRoomClosed, clientMessage.Room));
            }

            if (roomUserData.IsMuted)
            {
                throw new InvalidOperationException(String.Format("You cannot post messages to '{0}'. You have been muted.", clientMessage.Room));
            }

            // Update activity *after* ensuring the user, this forces them to be active
            UpdateActivity(user, room);

            string id = clientMessage.Id;
            ChatMessage chatMessage = _repository.GetMessageById(id);

            if (chatMessage == null)
            {
                // Create a true unique id and save the message to the db
                id = Guid.NewGuid().ToString("d");
                chatMessage = _service.AddMessage(user, room, id, clientMessage.Content);

                _repository.CommitChanges();
            }
            else if (chatMessage.User == user)
            {
                chatMessage.Content = clientMessage.Content;
                chatMessage.HtmlContent = null;
                chatMessage.Edited = DateTimeOffset.UtcNow;

                _repository.Update(chatMessage);
                _repository.CommitChanges();
            }
            else
            {
                throw new InvalidOperationException(String.Format("You cannot edit a message you do not own."));
            }

            var messageViewModel = new MessageViewModel(chatMessage);

            if (clientMessage.Id == null)
            {
                // If the client didn't generate an id for the message then just
                // send it to everyone. The assumption is that the client has some ui
                // that it wanted to update immediately showing the message and
                // then when the actual message is roundtripped it would "solidify it".
                Clients.Group(room.Name).addMessage(messageViewModel, room.Name);
            }
            else
            {
                // If the client did set an id then we need to give everyone the real id first
                Clients.OthersInGroup(room.Name).addMessage(messageViewModel, room.Name);

                // Now tell the caller to replace the message
                Clients.Caller.replaceMessage(clientMessage.Id, messageViewModel, room.Name);
            }

            // Add mentions
            AddMentions(chatMessage);

            var urls = UrlExtractor.ExtractUrls(chatMessage.Content);
            if (urls.Count > 0)
            {
                _resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id);
            }

            return true;
        }