1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 10:17:36 +08:00
osu-lazer/osu.Game/Online/Chat/MessageNotifier.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

200 lines
7.3 KiB
C#
Raw Normal View History

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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Platform;
using osu.Game.Configuration;
using osu.Game.Graphics;
2023-06-29 03:11:56 +08:00
using osu.Game.Localisation;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Online.Chat
{
/// <summary>
/// Component that handles creating and posting notifications for incoming messages.
/// </summary>
public partial class MessageNotifier : Component
{
[Resolved]
private INotificationOverlay notifications { get; set; }
[Resolved]
private ChatOverlay chatOverlay { get; set; }
[Resolved]
private ChannelManager channelManager { get; set; }
[Resolved]
private GameHost host { get; set; }
private Bindable<bool> notifyOnUsername;
private Bindable<bool> notifyOnPrivateMessage;
private readonly IBindable<APIUser> localUser = new Bindable<APIUser>();
2021-06-05 17:03:49 +08:00
private readonly IBindableList<Channel> joinedChannels = new BindableList<Channel>();
[BackgroundDependencyLoader]
2019-12-26 10:32:40 +08:00
private void load(OsuConfigManager config, IAPIProvider api)
{
notifyOnUsername = config.GetBindable<bool>(OsuSetting.NotifyOnUsernameMentioned);
notifyOnPrivateMessage = config.GetBindable<bool>(OsuSetting.NotifyOnPrivateMessage);
2021-06-05 17:03:49 +08:00
localUser.BindTo(api.LocalUser);
joinedChannels.BindTo(channelManager.JoinedChannels);
}
2019-12-26 10:32:40 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
joinedChannels.BindCollectionChanged(channelsChanged, true);
}
private void channelsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
2019-12-26 10:32:40 +08:00
{
case NotifyCollectionChangedAction.Add:
Debug.Assert(e.NewItems != null);
foreach (var channel in e.NewItems.Cast<Channel>())
channel.NewMessagesArrived += checkNewMessages;
break;
case NotifyCollectionChangedAction.Remove:
Debug.Assert(e.OldItems != null);
foreach (var channel in e.OldItems.Cast<Channel>())
channel.NewMessagesArrived -= checkNewMessages;
break;
}
2019-12-26 10:32:40 +08:00
}
private void checkNewMessages(IEnumerable<Message> messages)
2019-12-26 10:32:40 +08:00
{
if (!messages.Any())
2019-12-26 10:32:40 +08:00
return;
var channel = channelManager.JoinedChannels.SingleOrDefault(c => c.Id > 0 && c.Id == messages.First().ChannelId);
2019-12-26 10:32:40 +08:00
if (channel == null)
return;
2023-05-21 01:29:59 +08:00
// Only send notifications if ChatOverlay or the target channel aren't visible, or if the window is unfocused
if (chatOverlay.IsPresent && channelManager.CurrentChannel.Value == channel && host.IsActive.Value)
return;
foreach (var message in messages.OrderByDescending(m => m.Id))
{
// ignore messages that already have been read
2020-01-22 07:28:08 +08:00
if (message.Id <= channel.LastReadId)
return;
2023-05-21 01:29:59 +08:00
// ignore notifications triggered by local user's own chat messages
if (message.Sender.Id == localUser.Value.Id)
continue;
// check for private messages first to avoid both posting two notifications about the same message
2020-01-20 00:55:17 +08:00
if (checkForPMs(channel, message))
continue;
checkForMentions(channel, message);
2020-01-20 00:55:17 +08:00
}
}
2021-06-05 21:57:14 +08:00
/// <summary>
/// Checks whether the user enabled private message notifications and whether specified <paramref name="message"/> is a direct message.
/// </summary>
/// <param name="channel">The channel associated to the <paramref name="message"/></param>
/// <param name="message">The message to be checked</param>
/// <returns>Whether a notification was fired.</returns>
2020-01-20 00:55:17 +08:00
private bool checkForPMs(Channel channel, Message message)
{
if (!notifyOnPrivateMessage.Value || channel.Type != ChannelType.PM)
2020-01-20 00:55:17 +08:00
return false;
notifications.Post(new PrivateMessageNotification(message, channel));
2020-01-20 00:55:17 +08:00
return true;
}
private void checkForMentions(Channel channel, Message message)
2020-01-20 00:55:17 +08:00
{
2021-12-14 23:23:51 +08:00
if (!notifyOnUsername.Value || !CheckContainsUsername(message.Content, localUser.Value.Username)) return;
2020-01-20 00:55:17 +08:00
notifications.Post(new MentionNotification(message, channel));
}
/// <summary>
/// Checks if <paramref name="message"/> mentions <paramref name="username"/>.
/// This will match against the case where underscores are used instead of spaces (which is how osu-stable handles usernames with spaces).
/// </summary>
2021-12-14 23:23:51 +08:00
public static bool CheckContainsUsername(string message, string username)
2021-12-07 08:38:37 +08:00
{
string fullName = Regex.Escape(username);
string underscoreName = Regex.Escape(username.Replace(' ', '_'));
return Regex.IsMatch(message, $@"(^|\W)({fullName}|{underscoreName})($|\W)", RegexOptions.IgnoreCase);
}
public partial class PrivateMessageNotification : HighlightMessageNotification
{
public PrivateMessageNotification(Message message, Channel channel)
: base(message, channel)
{
Icon = FontAwesome.Solid.Envelope;
2023-06-29 03:11:56 +08:00
Text = NotificationsStrings.PrivateMessageReceived(message.Sender.Username);
}
}
public partial class MentionNotification : HighlightMessageNotification
{
public MentionNotification(Message message, Channel channel)
: base(message, channel)
{
Icon = FontAwesome.Solid.At;
2023-06-29 03:11:56 +08:00
Text = NotificationsStrings.YourNameWasMentioned(message.Sender.Username);
}
}
public abstract partial class HighlightMessageNotification : SimpleNotification
{
2023-07-05 16:25:16 +08:00
public override string PopInSampleName => "UI/notification-mention";
protected HighlightMessageNotification(Message message, Channel channel)
{
this.message = message;
this.channel = channel;
}
private readonly Message message;
private readonly Channel channel;
[BackgroundDependencyLoader]
private void load(OsuColour colours, ChatOverlay chatOverlay, INotificationOverlay notificationOverlay)
{
IconContent.Colour = colours.PurpleDark;
Activated = delegate
{
2020-01-20 00:55:17 +08:00
notificationOverlay.Hide();
chatOverlay.HighlightMessage(message, channel);
return true;
};
}
}
}
}