2019-01-24 08:43:03 +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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-04-14 11:31:03 +00:00
|
|
|
using System.Collections.ObjectModel;
|
2018-04-13 09:19:50 +00:00
|
|
|
using System.Linq;
|
|
|
|
using Newtonsoft.Json;
|
2019-02-21 10:04:31 +00:00
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 09:19:50 +00:00
|
|
|
using osu.Framework.Lists;
|
2018-04-14 11:31:03 +00:00
|
|
|
using osu.Game.Users;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Chat
|
|
|
|
{
|
|
|
|
public class Channel
|
|
|
|
{
|
2019-10-29 05:32:38 +00:00
|
|
|
public const int MAX_HISTORY = 300;
|
2018-07-09 16:52:28 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2018-11-13 06:20:40 +00:00
|
|
|
/// Contains every joined user except the current logged in user. Currently only returned for PM channels.
|
2018-07-09 16:52:28 +00:00
|
|
|
/// </summary>
|
2018-11-13 06:20:40 +00:00
|
|
|
public readonly ObservableCollection<User> Users = new ObservableCollection<User>();
|
|
|
|
|
|
|
|
[JsonProperty(@"users")]
|
2020-11-06 03:40:54 +00:00
|
|
|
private int[] userIds
|
2018-11-13 06:20:40 +00:00
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
foreach (var id in value)
|
|
|
|
Users.Add(new User { Id = id });
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 18:46:44 +00:00
|
|
|
|
2018-07-24 03:14:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Contains all the messages send in the channel.
|
|
|
|
/// </summary>
|
2018-07-09 16:52:28 +00:00
|
|
|
public readonly SortedList<Message> Messages = new SortedList<Message>(Comparer<Message>.Default);
|
2018-07-24 03:14:33 +00:00
|
|
|
|
2020-01-02 16:07:28 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Contains all the messages that weren't read by the user.
|
|
|
|
/// </summary>
|
|
|
|
public IEnumerable<Message> UnreadMessages => Messages.Where(m => LastReadId < m.Id);
|
|
|
|
|
2018-07-24 03:14:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Contains all the messages that are still pending for submission to the server.
|
|
|
|
/// </summary>
|
2018-07-09 18:39:16 +00:00
|
|
|
private readonly List<LocalEchoMessage> pendingMessages = new List<LocalEchoMessage>();
|
2018-07-09 16:52:28 +00:00
|
|
|
|
2018-07-24 03:14:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// An event that fires when new messages arrived.
|
|
|
|
/// </summary>
|
2018-07-09 16:52:28 +00:00
|
|
|
public event Action<IEnumerable<Message>> NewMessagesArrived;
|
2018-07-24 03:14:33 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An event that fires when a pending message gets resolved.
|
|
|
|
/// </summary>
|
2018-07-09 16:52:28 +00:00
|
|
|
public event Action<LocalEchoMessage, Message> PendingMessageResolved;
|
2018-07-24 03:14:33 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An event that fires when a pending message gets removed.
|
|
|
|
/// </summary>
|
2018-07-09 16:52:28 +00:00
|
|
|
public event Action<Message> MessageRemoved;
|
|
|
|
|
2020-05-05 01:31:11 +00:00
|
|
|
public bool ReadOnly => false; // todo: not yet used.
|
2018-07-24 03:14:33 +00:00
|
|
|
|
2018-07-09 16:52:28 +00:00
|
|
|
public override string ToString() => Name;
|
2018-04-14 11:31:03 +00:00
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
[JsonProperty(@"name")]
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
[JsonProperty(@"description")]
|
|
|
|
public string Topic;
|
|
|
|
|
|
|
|
[JsonProperty(@"type")]
|
2018-09-25 11:53:24 +00:00
|
|
|
public ChannelType Type;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
[JsonProperty(@"channel_id")]
|
2018-04-14 11:31:03 +00:00
|
|
|
public long Id;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-09-26 10:15:02 +00:00
|
|
|
[JsonProperty(@"last_message_id")]
|
|
|
|
public long? LastMessageId;
|
|
|
|
|
2020-01-02 16:07:28 +00:00
|
|
|
[JsonProperty(@"last_read_id")]
|
|
|
|
public long? LastReadId;
|
|
|
|
|
2018-11-12 11:41:10 +00:00
|
|
|
/// <summary>
|
2020-06-08 08:49:45 +00:00
|
|
|
/// Signals if the current user joined this channel or not. Defaults to false.
|
|
|
|
/// Note that this does not guarantee a join has completed. Check Id > 0 for confirmation.
|
2018-11-12 11:41:10 +00:00
|
|
|
/// </summary>
|
2018-04-13 09:19:50 +00:00
|
|
|
public Bindable<bool> Joined = new Bindable<bool>();
|
|
|
|
|
|
|
|
[JsonConstructor]
|
|
|
|
public Channel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-07 04:56:21 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Create a private messaging channel with the specified user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="user">The user to create the private conversation with.</param>
|
|
|
|
public Channel(User user)
|
|
|
|
{
|
|
|
|
Type = ChannelType.PM;
|
|
|
|
Users.Add(user);
|
|
|
|
Name = user.Username;
|
|
|
|
}
|
|
|
|
|
2018-07-24 03:14:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds the argument message as a local echo. When this local echo is resolved <see cref="PendingMessageResolved"/> will get called.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message"></param>
|
2018-04-13 09:19:50 +00:00
|
|
|
public void AddLocalEcho(LocalEchoMessage message)
|
|
|
|
{
|
|
|
|
pendingMessages.Add(message);
|
|
|
|
Messages.Add(message);
|
|
|
|
|
|
|
|
NewMessagesArrived?.Invoke(new[] { message });
|
|
|
|
}
|
|
|
|
|
2018-11-13 08:24:11 +00:00
|
|
|
public bool MessagesLoaded;
|
2018-09-26 10:15:02 +00:00
|
|
|
|
2018-07-24 03:14:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds new messages to the channel and purges old messages. Triggers the <see cref="NewMessagesArrived"/> event.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="messages"></param>
|
2018-04-13 09:19:50 +00:00
|
|
|
public void AddNewMessages(params Message[] messages)
|
|
|
|
{
|
|
|
|
messages = messages.Except(Messages).ToArray();
|
|
|
|
|
2018-09-28 01:00:40 +00:00
|
|
|
if (messages.Length == 0) return;
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
Messages.AddRange(messages);
|
2018-09-26 10:15:02 +00:00
|
|
|
|
|
|
|
var maxMessageId = messages.Max(m => m.Id);
|
|
|
|
if (maxMessageId > LastMessageId)
|
|
|
|
LastMessageId = maxMessageId;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
purgeOldMessages();
|
|
|
|
|
|
|
|
NewMessagesArrived?.Invoke(messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Replace or remove a message from the channel.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="echo">The local echo message (client-side).</param>
|
|
|
|
/// <param name="final">The response message, or null if the message became invalid.</param>
|
|
|
|
public void ReplaceMessage(LocalEchoMessage echo, Message final)
|
|
|
|
{
|
|
|
|
if (!pendingMessages.Remove(echo))
|
|
|
|
throw new InvalidOperationException("Attempted to remove echo that wasn't present");
|
|
|
|
|
|
|
|
Messages.Remove(echo);
|
|
|
|
|
|
|
|
if (final == null)
|
|
|
|
{
|
|
|
|
MessageRemoved?.Invoke(echo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Messages.Contains(final))
|
2018-07-09 18:39:16 +00:00
|
|
|
throw new InvalidOperationException("Attempted to add the same message again");
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
Messages.Add(final);
|
|
|
|
PendingMessageResolved?.Invoke(echo, final);
|
|
|
|
}
|
|
|
|
|
2018-04-14 11:31:03 +00:00
|
|
|
private void purgeOldMessages()
|
|
|
|
{
|
|
|
|
// never purge local echos
|
|
|
|
int messageCount = Messages.Count - pendingMessages.Count;
|
2019-10-29 05:32:38 +00:00
|
|
|
if (messageCount > MAX_HISTORY)
|
|
|
|
Messages.RemoveRange(0, messageCount - MAX_HISTORY);
|
2018-04-14 11:31:03 +00:00
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
}
|
|
|
|
}
|