osu/osu.Game/Online/Chat/MessageNotifier.cs

178 lines
6.6 KiB
C#
Raw Normal View History

2019-12-17 06:04:55 +00: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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
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]
private NotificationOverlay notifications { get; set; }
[Resolved]
private ChatOverlay chatOverlay { get; set; }
[Resolved]
private ChannelManager channelManager { get; set; }
private Bindable<bool> notifyOnUsername;
private Bindable<bool> notifyOnPrivateMessage;
2021-05-27 19:58:54 +00:00
private readonly IBindable<User> localUser = new Bindable<User>();
2021-06-05 09:03:49 +00:00
private readonly IBindableList<Channel> joinedChannels = new BindableList<Channel>();
[BackgroundDependencyLoader]
2019-12-26 02:32:40 +00:00
private void load(OsuConfigManager config, IAPIProvider api)
{
notifyOnUsername = config.GetBindable<bool>(OsuSetting.NotifyOnUsernameMentioned);
notifyOnPrivateMessage = config.GetBindable<bool>(OsuSetting.NotifyOnPrivateMessage);
2021-06-05 09:03:49 +00:00
localUser.BindTo(api.LocalUser);
2020-01-25 15:43:51 +00:00
joinedChannels.BindCollectionChanged(channelsChanged);
2021-06-05 09:03:49 +00:00
joinedChannels.BindTo(channelManager.JoinedChannels);
}
2019-12-26 02:32:40 +00:00
private void channelsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
2019-12-26 02:32:40 +00:00
{
case NotifyCollectionChangedAction.Add:
foreach (var channel in e.NewItems.Cast<Channel>())
channel.NewMessagesArrived += checkNewMessages;
break;
case NotifyCollectionChangedAction.Remove:
foreach (var channel in e.OldItems.Cast<Channel>())
channel.NewMessagesArrived -= checkNewMessages;
break;
}
2019-12-26 02:32:40 +00:00
}
private void checkNewMessages(IEnumerable<Message> messages)
2019-12-26 02:32:40 +00:00
{
if (!messages.Any())
2019-12-26 02:32:40 +00:00
return;
var channel = channelManager.JoinedChannels.SingleOrDefault(c => c.Id == messages.First().ChannelId);
2019-12-26 02:32:40 +00:00
if (channel == null)
return;
2020-01-21 23:13:07 +00:00
// Only send notifications, if ChatOverlay and the target channel aren't visible.
if (chatOverlay.IsPresent && channelManager.CurrentChannel.Value == channel)
return;
foreach (var message in messages.OrderByDescending(m => m.Id))
{
// ignore messages that already have been read
2020-01-21 23:28:08 +00:00
if (message.Id <= channel.LastReadId)
return;
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-19 16:55:17 +00:00
if (checkForPMs(channel, message))
continue;
checkForMentions(channel, message);
2020-01-19 16:55:17 +00:00
}
}
2021-06-05 13:57:14 +00: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-19 16:55:17 +00:00
private bool checkForPMs(Channel channel, Message message)
{
if (!notifyOnPrivateMessage.Value || channel.Type != ChannelType.PM)
2020-01-19 16:55:17 +00:00
return false;
notifications.Post(new PrivateMessageNotification(message.Sender.Username, channel));
2020-01-19 16:55:17 +00:00
return true;
}
private void checkForMentions(Channel channel, Message message)
2020-01-19 16:55:17 +00:00
{
if (!notifyOnUsername.Value || !checkContainsUsername(message.Content, localUser.Value.Username)) return;
2020-01-19 16:55:17 +00:00
notifications.Post(new MentionNotification(message.Sender.Username, channel));
}
/// <summary>
/// Checks if <paramref name="message"/> contains <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>
private static bool checkContainsUsername(string message, string username) => message.Contains(username, StringComparison.OrdinalIgnoreCase) || message.Contains(username.Replace(' ', '_'), StringComparison.OrdinalIgnoreCase);
public class PrivateMessageNotification : OpenChannelNotification
{
public PrivateMessageNotification(string username, Channel channel)
: base(channel)
{
Icon = FontAwesome.Solid.Envelope;
Text = $"You received a private message from '{username}'. Click to read it!";
}
}
public class MentionNotification : OpenChannelNotification
{
public MentionNotification(string username, Channel channel)
: base(channel)
{
Icon = FontAwesome.Solid.At;
Text = $"Your name was mentioned in chat by '{username}'. Click to find out why!";
}
}
public abstract class OpenChannelNotification : SimpleNotification
{
protected OpenChannelNotification(Channel channel)
{
2020-02-03 22:03:27 +00:00
this.channel = channel;
}
2020-02-03 22:03:27 +00:00
private readonly Channel channel;
public override bool IsImportant => false;
[BackgroundDependencyLoader]
private void load(OsuColour colours, ChatOverlay chatOverlay, NotificationOverlay notificationOverlay, ChannelManager channelManager)
{
IconBackgound.Colour = colours.PurpleDark;
Activated = delegate
{
2020-01-19 16:55:17 +00:00
notificationOverlay.Hide();
chatOverlay.Show();
2020-02-03 22:03:27 +00:00
channelManager.CurrentChannel.Value = channel;
return true;
};
}
}
}
}