1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 03:22:54 +08:00

Rename configuration variables and refactor lots more

This commit is contained in:
Dean Herbert 2021-06-11 16:37:31 +09:00
parent 3d645608eb
commit 20759657de
3 changed files with 46 additions and 62 deletions

View File

@ -61,8 +61,8 @@ namespace osu.Game.Configuration
SetDefault(OsuSetting.ShowOnlineExplicitContent, false); SetDefault(OsuSetting.ShowOnlineExplicitContent, false);
SetDefault(OsuSetting.ChatHighlightName, true); SetDefault(OsuSetting.NotifyOnUsernameMentioned, true);
SetDefault(OsuSetting.ChatMessageNotification, true); SetDefault(OsuSetting.NotifyOnPrivateMessage, true);
// Audio // Audio
SetDefault(OsuSetting.VolumeInactive, 0.25, 0, 1, 0.01); SetDefault(OsuSetting.VolumeInactive, 0.25, 0, 1, 0.01);
@ -262,8 +262,8 @@ namespace osu.Game.Configuration
ScalingSizeY, ScalingSizeY,
UIScale, UIScale,
IntroSequence, IntroSequence,
ChatHighlightName, NotifyOnUsernameMentioned,
ChatMessageNotification, NotifyOnPrivateMessage,
UIHoldActivationDelay, UIHoldActivationDelay,
HitLighting, HitLighting,
MenuBackgroundSource, MenuBackgroundSource,

View File

@ -9,7 +9,6 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Framework.Logging;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Online.API; using osu.Game.Online.API;
@ -33,16 +32,18 @@ namespace osu.Game.Online.Chat
[Resolved] [Resolved]
private ChannelManager channelManager { get; set; } private ChannelManager channelManager { get; set; }
private Bindable<bool> notifyOnMention; private Bindable<bool> notifyOnUsername;
private Bindable<bool> notifyOnPM; private Bindable<bool> notifyOnPrivateMessage;
private readonly IBindable<User> localUser = new Bindable<User>(); private readonly IBindable<User> localUser = new Bindable<User>();
private readonly IBindableList<Channel> joinedChannels = new BindableList<Channel>(); private readonly IBindableList<Channel> joinedChannels = new BindableList<Channel>();
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuConfigManager config, IAPIProvider api) private void load(OsuConfigManager config, IAPIProvider api)
{ {
notifyOnMention = config.GetBindable<bool>(OsuSetting.ChatHighlightName); notifyOnUsername = config.GetBindable<bool>(OsuSetting.NotifyOnUsernameMentioned);
notifyOnPM = config.GetBindable<bool>(OsuSetting.ChatMessageNotification); notifyOnPrivateMessage = config.GetBindable<bool>(OsuSetting.NotifyOnPrivateMessage);
localUser.BindTo(api.LocalUser); localUser.BindTo(api.LocalUser);
joinedChannels.BindCollectionChanged(channelsChanged); joinedChannels.BindCollectionChanged(channelsChanged);
@ -55,19 +56,19 @@ namespace osu.Game.Online.Chat
{ {
case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Add:
foreach (var channel in e.NewItems.Cast<Channel>()) foreach (var channel in e.NewItems.Cast<Channel>())
channel.NewMessagesArrived += newMessagesArrived; channel.NewMessagesArrived += checkNewMessages;
break; break;
case NotifyCollectionChangedAction.Remove: case NotifyCollectionChangedAction.Remove:
foreach (var channel in e.OldItems.Cast<Channel>()) foreach (var channel in e.OldItems.Cast<Channel>())
channel.NewMessagesArrived -= newMessagesArrived; channel.NewMessagesArrived -= checkNewMessages;
break; break;
} }
} }
private void newMessagesArrived(IEnumerable<Message> messages) private void checkNewMessages(IEnumerable<Message> messages)
{ {
if (!messages.Any()) if (!messages.Any())
return; return;
@ -75,10 +76,7 @@ namespace osu.Game.Online.Chat
var channel = channelManager.JoinedChannels.SingleOrDefault(c => c.Id == messages.First().ChannelId); var channel = channelManager.JoinedChannels.SingleOrDefault(c => c.Id == messages.First().ChannelId);
if (channel == null) if (channel == null)
{
Logger.Log($"Couldn't resolve channel id {messages.First().ChannelId}", LoggingTarget.Information);
return; return;
}
// Only send notifications, if ChatOverlay and the target channel aren't visible. // Only send notifications, if ChatOverlay and the target channel aren't visible.
if (chatOverlay.IsPresent && channelManager.CurrentChannel.Value == channel) if (chatOverlay.IsPresent && channelManager.CurrentChannel.Value == channel)
@ -93,12 +91,11 @@ namespace osu.Game.Online.Chat
if (message.Sender.Id == localUser.Value.Id) if (message.Sender.Id == localUser.Value.Id)
continue; continue;
// check for private messages first, // check for private messages first to avoid both posting two notifications about the same message
// to avoid both posting two notifications about the same message
if (checkForPMs(channel, message)) if (checkForPMs(channel, message))
continue; continue;
_ = checkForMentions(channel, message, localUser.Value.Username); checkForMentions(channel, message);
} }
} }
@ -107,45 +104,52 @@ namespace osu.Game.Online.Chat
/// </summary> /// </summary>
/// <param name="channel">The channel associated to the <paramref name="message"/></param> /// <param name="channel">The channel associated to the <paramref name="message"/></param>
/// <param name="message">The message to be checked</param> /// <param name="message">The message to be checked</param>
/// <returns>Whether a notification was fired.</returns>
private bool checkForPMs(Channel channel, Message message) private bool checkForPMs(Channel channel, Message message)
{ {
if (!notifyOnPM.Value || channel.Type != ChannelType.PM) if (!notifyOnPrivateMessage.Value || channel.Type != ChannelType.PM)
return false; return false;
if (channel.Id != message.ChannelId)
throw new ArgumentException("The provided channel doesn't match with the channel id provided by the message parameter.", nameof(channel));
notifications.Post(new PrivateMessageNotification(message.Sender.Username, channel)); notifications.Post(new PrivateMessageNotification(message.Sender.Username, channel));
return true; return true;
} }
/// <summary> private void checkForMentions(Channel channel, Message message)
/// Checks whether the user enabled mention notifications and whether specified <paramref name="message"/> mentions the provided <paramref name="username"/>.
/// </summary>
/// <param name="channel">The channel associated to the <paramref name="message"/></param>
/// <param name="message">The message to be checked</param>
/// <param name="username">The username that will be checked for</param>
private bool checkForMentions(Channel channel, Message message, string username)
{ {
if (!notifyOnMention.Value || !isMentioning(message.Content, username)) if (!notifyOnUsername.Value || !checkContainsUsername(message.Content, localUser.Value.Username)) return;
return false;
if (channel.Id != message.ChannelId)
throw new ArgumentException("The provided channel doesn't match with the channel id provided by the message parameter.", nameof(channel));
notifications.Post(new MentionNotification(message.Sender.Username, channel)); notifications.Post(new MentionNotification(message.Sender.Username, channel));
return true;
} }
/// <summary> /// <summary>
/// Checks if <paramref name="message"/> contains <paramref name="username"/>, if not, retries making spaces into underscores. /// 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> /// </summary>
/// <returns>If the <paramref name="message"/> mentions the <paramref name="username"/></returns> private static bool checkContainsUsername(string message, string username) => message.Contains(username, StringComparison.OrdinalIgnoreCase) || message.Contains(username.Replace(' ', '_'), StringComparison.OrdinalIgnoreCase);
private static bool isMentioning(string message, string username) => message.Contains(username, StringComparison.OrdinalIgnoreCase) || message.Contains(username.Replace(' ', '_'), StringComparison.OrdinalIgnoreCase);
public class OpenChannelNotification : SimpleNotification public class PrivateMessageNotification : OpenChannelNotification
{ {
public OpenChannelNotification(Channel channel) 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)
{ {
this.channel = channel; this.channel = channel;
} }
@ -169,25 +173,5 @@ namespace osu.Game.Online.Chat
}; };
} }
} }
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!";
}
}
} }
} }

View File

@ -19,12 +19,12 @@ namespace osu.Game.Overlays.Settings.Sections.Online
new SettingsCheckbox new SettingsCheckbox
{ {
LabelText = "Show a notification when someone mentions your name", LabelText = "Show a notification when someone mentions your name",
Current = config.GetBindable<bool>(OsuSetting.ChatHighlightName) Current = config.GetBindable<bool>(OsuSetting.NotifyOnUsernameMentioned)
}, },
new SettingsCheckbox new SettingsCheckbox
{ {
LabelText = "Show a notification when you receive a private message", LabelText = "Show a notification when you receive a private message",
Current = config.GetBindable<bool>(OsuSetting.ChatMessageNotification) Current = config.GetBindable<bool>(OsuSetting.NotifyOnPrivateMessage)
}, },
}; };
} }