Example #1
0
        private void AddMentions(ChatMessage message)
        {
            bool anyMentions = false;

            foreach (var userName in MentionExtractor.ExtractMentions(message.Content))
            {
                ChatUser mentionedUser = _repository.GetUserByName(userName);

                // Don't create a mention if
                // 1. If the mentioned user doesn't exist.
                // 2. If you mention yourself
                // 3. If you're mentioned in a private room that you don't have access to
                if (mentionedUser == null ||
                    mentionedUser == message.User ||
                    (message.Room.Private && !mentionedUser.AllowedRooms.Contains(message.Room)))
                {
                    continue;
                }

                anyMentions = true;

                // Mark the notification as read if the user is online
                bool markAsRead = mentionedUser.Status == (int)UserStatus.Active;

                _service.AddNotification(mentionedUser, message, markAsRead);
            }

            if (anyMentions)
            {
                _repository.CommitChanges();
            }
        }
Example #2
0
        private void AddMentions(ChatMessage message)
        {
            var mentionedUsers = new List <ChatUser>();

            foreach (var userName in MentionExtractor.ExtractMentions(message.Content))
            {
                ChatUser mentionedUser = _repository.GetUserByName(userName);

                // Don't create a mention if
                // 1. If the mentioned user doesn't exist.
                // 2. If you mention yourself
                // 3. If you're mentioned in a private room that you don't have access to
                // 4. You've already been mentioned in this message
                if (mentionedUser == null ||
                    mentionedUser == message.User ||
                    (message.Room.Private && !mentionedUser.AllowedRooms.Contains(message.Room)) ||
                    mentionedUsers.Contains(mentionedUser))
                {
                    continue;
                }

                // mark as read if ALL of the following
                // 1. user is not offline
                // 2. user is not AFK
                // 3. user has been active within the last 10 minutes
                // 4. user is currently in the room
                bool markAsRead = mentionedUser.Status != (int)UserStatus.Offline &&
                                  !mentionedUser.IsAfk &&
                                  (DateTimeOffset.UtcNow - mentionedUser.LastActivity) < TimeSpan.FromMinutes(10) &&
                                  _repository.IsUserInRoom(_cache, mentionedUser, message.Room);

                _service.AddNotification(mentionedUser, message, message.Room, markAsRead);

                mentionedUsers.Add(mentionedUser);
            }

            if (mentionedUsers.Count > 0)
            {
                _repository.CommitChanges();
            }

            foreach (var user in mentionedUsers)
            {
                UpdateUnreadMentions(user);
            }
        }