2019-12-17 14:04:55 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-01-20 00:36:38 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-01-20 01:23:12 +08:00
|
|
|
|
using Humanizer;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2019-12-26 10:32:40 +08:00
|
|
|
|
using osu.Framework.Logging;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
|
using osu.Game.Users;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.Chat
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Component that handles creating and posting notifications for incoming messages.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MessageNotifier : Component
|
|
|
|
|
{
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
|
private NotificationOverlay notificationOverlay { get; set; }
|
|
|
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
|
private ChatOverlay chatOverlay { get; set; }
|
|
|
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
|
private ChannelManager channelManager { get; set; }
|
|
|
|
|
|
|
|
|
|
private Bindable<bool> notifyOnMention;
|
|
|
|
|
private Bindable<bool> notifyOnChat;
|
|
|
|
|
private Bindable<User> localUser;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if the user is able to see incoming messages.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsActive => chatOverlay?.IsPresent == true;
|
|
|
|
|
|
2020-01-18 23:18:17 +08:00
|
|
|
|
private readonly List<PrivateMessageNotification> privateMessageNotifications = new List<PrivateMessageNotification>();
|
2020-01-17 07:00:10 +08:00
|
|
|
|
|
2019-12-17 13:59:27 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2019-12-26 10:32:40 +08:00
|
|
|
|
private void load(OsuConfigManager config, IAPIProvider api)
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
|
|
|
|
notifyOnMention = config.GetBindable<bool>(OsuSetting.ChatHighlightName);
|
|
|
|
|
notifyOnChat = config.GetBindable<bool>(OsuSetting.ChatMessageNotification);
|
|
|
|
|
localUser = api.LocalUser;
|
2019-12-26 10:32:40 +08:00
|
|
|
|
|
|
|
|
|
// Listen for new messages
|
2020-01-18 22:57:51 +08:00
|
|
|
|
channelManager.JoinedChannels.ItemsAdded += joinedChannels =>
|
2019-12-26 10:32:40 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var channel in joinedChannels)
|
|
|
|
|
channel.NewMessagesArrived += channel_NewMessagesArrived;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-18 22:57:51 +08:00
|
|
|
|
channelManager.JoinedChannels.ItemsRemoved += leftChannels =>
|
2019-12-26 10:32:40 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var channel in leftChannels)
|
|
|
|
|
channel.NewMessagesArrived -= channel_NewMessagesArrived;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 06:15:30 +08:00
|
|
|
|
private void channel_NewMessagesArrived(IEnumerable<Message> messages)
|
2019-12-26 10:32:40 +08:00
|
|
|
|
{
|
|
|
|
|
if (messages == null || !messages.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
HandleMessages(messages.First().ChannelId, messages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Resolves the channel id
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void HandleMessages(long channelId, IEnumerable<Message> messages)
|
|
|
|
|
{
|
2020-01-22 07:29:12 +08:00
|
|
|
|
var channel = channelManager.JoinedChannels.SingleOrDefault(c => c.Id == channelId);
|
2019-12-26 10:32:40 +08:00
|
|
|
|
|
|
|
|
|
if (channel == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Log($"Couldn't resolve channel id {channelId}", LoggingTarget.Information);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HandleMessages(channel, messages);
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleMessages(Channel channel, IEnumerable<Message> messages)
|
|
|
|
|
{
|
2020-01-22 07:13:07 +08:00
|
|
|
|
// Only send notifications, if ChatOverlay and the target channel aren't visible.
|
2019-12-17 13:59:27 +08:00
|
|
|
|
if (IsActive && channelManager.CurrentChannel.Value == channel)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-01-22 07:27:46 +08:00
|
|
|
|
foreach (var message in messages.OrderByDescending(m => m.Id))
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
2020-01-17 07:00:10 +08:00
|
|
|
|
// ignore messages that already have been read
|
2020-01-22 07:28:08 +08:00
|
|
|
|
if (message.Id <= channel.LastReadId)
|
2020-01-17 07:00:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2020-01-22 06:42:15 +08:00
|
|
|
|
if (message.Sender.Id == localUser.Value.Id)
|
2019-12-17 13:59:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
if (checkForPMs(channel, message))
|
|
|
|
|
continue;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
// change output to bool again if another "message processor" is added.
|
2020-01-22 06:42:15 +08:00
|
|
|
|
checkForMentions(channel, message, localUser.Value.Username);
|
2020-01-20 00:55:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-18 21:17:26 +08:00
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
private bool checkForPMs(Channel channel, Message message)
|
|
|
|
|
{
|
|
|
|
|
if (!notifyOnChat.Value || channel.Type != ChannelType.PM)
|
|
|
|
|
return false;
|
2020-01-17 06:15:30 +08:00
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
var existingNotification = privateMessageNotifications.FirstOrDefault(n => n.Username == message.Sender.Username);
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
if (existingNotification == null)
|
|
|
|
|
{
|
2020-01-22 07:28:59 +08:00
|
|
|
|
var notification = new PrivateMessageNotification(message.Sender.Username, channel, (n) => privateMessageNotifications.Remove(n));
|
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
notificationOverlay?.Post(notification);
|
|
|
|
|
privateMessageNotifications.Add(notification);
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
2020-01-20 00:55:17 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
existingNotification.MessageCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkForMentions(Channel channel, Message message, string username)
|
|
|
|
|
{
|
2020-01-22 06:43:21 +08:00
|
|
|
|
if (!notifyOnMention.Value || !anyCaseInsensitive(getWords(message.Content), username))
|
2020-01-20 00:55:17 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var notification = new MentionNotification(message.Sender.Username, channel);
|
|
|
|
|
notificationOverlay?.Post(notification);
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 00:36:38 +08:00
|
|
|
|
private static IEnumerable<string> getWords(string input) => Regex.Matches(input, @"\w+").Select(c => c.Value);
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the first matching string/word in both <paramref name="x"/> and <paramref name="y"/> (case-insensitive)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static string hasCaseInsensitive(IEnumerable<string> x, IEnumerable<string> y) => x.FirstOrDefault(x2 => anyCaseInsensitive(y, x2));
|
|
|
|
|
|
2020-01-17 07:00:10 +08:00
|
|
|
|
private static bool anyCaseInsensitive(IEnumerable<string> x, string y) => x.Any(x2 => x2.Equals(y, StringComparison.OrdinalIgnoreCase));
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
2020-01-17 07:00:10 +08:00
|
|
|
|
public class PrivateMessageNotification : SimpleNotification
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
2020-01-22 07:28:59 +08:00
|
|
|
|
public PrivateMessageNotification(string username, Channel channel, Action<PrivateMessageNotification> onRemove)
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
|
|
|
|
Icon = FontAwesome.Solid.Envelope;
|
|
|
|
|
Username = username;
|
|
|
|
|
MessageCount = 1;
|
2020-01-20 00:55:17 +08:00
|
|
|
|
Channel = channel;
|
2020-01-22 07:28:59 +08:00
|
|
|
|
OnRemove = onRemove;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 22:57:51 +08:00
|
|
|
|
private int messageCount;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
|
|
|
|
public int MessageCount
|
|
|
|
|
{
|
|
|
|
|
get => messageCount;
|
2020-01-20 01:23:12 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
messageCount = value;
|
|
|
|
|
Text = $"You received {"private message".ToQuantity(messageCount)} from '{Username}'. Click to read it!";
|
|
|
|
|
}
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Username { get; set; }
|
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
public Channel Channel { get; set; }
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
2020-01-22 07:28:59 +08:00
|
|
|
|
public Action<PrivateMessageNotification> OnRemove { get; set; }
|
|
|
|
|
|
2019-12-17 13:59:27 +08:00
|
|
|
|
public override bool IsImportant => false;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2020-01-22 07:28:59 +08:00
|
|
|
|
private void load(OsuColour colours, ChatOverlay chatOverlay, NotificationOverlay notificationOverlay, ChannelManager channelManager)
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
|
|
|
|
IconBackgound.Colour = colours.PurpleDark;
|
2020-01-22 07:28:59 +08:00
|
|
|
|
|
2019-12-17 13:59:27 +08:00
|
|
|
|
Activated = delegate
|
|
|
|
|
{
|
2020-01-20 00:55:17 +08:00
|
|
|
|
notificationOverlay.Hide();
|
|
|
|
|
chatOverlay.Show();
|
|
|
|
|
channelManager.CurrentChannel.Value = Channel;
|
2020-01-17 07:00:10 +08:00
|
|
|
|
|
2019-12-17 13:59:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
};
|
2020-01-22 07:28:59 +08:00
|
|
|
|
|
|
|
|
|
Closed += delegate
|
|
|
|
|
{
|
|
|
|
|
OnRemove.Invoke(this);
|
|
|
|
|
};
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 07:00:10 +08:00
|
|
|
|
public class MentionNotification : SimpleNotification
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
2020-01-20 00:55:17 +08:00
|
|
|
|
public MentionNotification(string username, Channel channel)
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
|
|
|
|
Icon = FontAwesome.Solid.At;
|
|
|
|
|
Text = $"Your name was mentioned in chat by '{username}'. Click to find out why!";
|
2020-01-20 00:55:17 +08:00
|
|
|
|
Channel = channel;
|
2019-12-17 13:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 00:55:17 +08:00
|
|
|
|
public Channel Channel { get; set; }
|
2019-12-17 13:59:27 +08:00
|
|
|
|
|
|
|
|
|
public override bool IsImportant => false;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2020-01-20 00:55:17 +08:00
|
|
|
|
private void load(OsuColour colours, ChatOverlay chatOverlay, NotificationOverlay notificationOverlay, ChannelManager channelManager)
|
2019-12-17 13:59:27 +08:00
|
|
|
|
{
|
|
|
|
|
IconBackgound.Colour = colours.PurpleDark;
|
2020-01-22 07:28:59 +08:00
|
|
|
|
|
2019-12-17 13:59:27 +08:00
|
|
|
|
Activated = delegate
|
|
|
|
|
{
|
2020-01-20 00:55:17 +08:00
|
|
|
|
notificationOverlay.Hide();
|
|
|
|
|
chatOverlay.Show();
|
|
|
|
|
channelManager.CurrentChannel.Value = Channel;
|
|
|
|
|
|
2019-12-17 13:59:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|