2018-04-13 17:19:50 +08:00
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-04-14 19:31:03 +08:00
|
|
|
using System.Collections.ObjectModel;
|
2018-04-13 17:19:50 +08:00
|
|
|
using System.Linq;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
using osu.Framework.Lists;
|
2018-04-14 19:31:03 +08:00
|
|
|
using osu.Game.Users;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Chat
|
|
|
|
{
|
|
|
|
public class Channel
|
|
|
|
{
|
2018-07-10 02:39:16 +08:00
|
|
|
public readonly int MaxHistory = 300;
|
2018-07-10 00:52:28 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2018-07-10 00:58:33 +08:00
|
|
|
/// Contains every joined user except the current logged in user.
|
2018-07-10 00:52:28 +08:00
|
|
|
/// </summary>
|
|
|
|
public readonly ObservableCollection<User> JoinedUsers = new ObservableCollection<User>();
|
|
|
|
public readonly SortedList<Message> Messages = new SortedList<Message>(Comparer<Message>.Default);
|
2018-07-10 02:39:16 +08:00
|
|
|
private readonly List<LocalEchoMessage> pendingMessages = new List<LocalEchoMessage>();
|
2018-07-10 00:52:28 +08:00
|
|
|
|
|
|
|
public event Action<IEnumerable<Message>> NewMessagesArrived;
|
|
|
|
public event Action<LocalEchoMessage, Message> PendingMessageResolved;
|
|
|
|
public event Action<Message> MessageRemoved;
|
|
|
|
|
|
|
|
public readonly Bindable<bool> Joined = new Bindable<bool>();
|
|
|
|
public TargetType Target { get; }
|
2018-07-10 03:00:39 +08:00
|
|
|
public bool ReadOnly => false; //todo not yet used.
|
2018-07-10 00:52:28 +08:00
|
|
|
public override string ToString() => Name;
|
2018-04-14 19:31:03 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
[JsonProperty(@"name")]
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
[JsonProperty(@"description")]
|
|
|
|
public string Topic;
|
|
|
|
|
|
|
|
[JsonProperty(@"type")]
|
|
|
|
public string Type;
|
|
|
|
|
|
|
|
[JsonProperty(@"channel_id")]
|
2018-04-14 19:31:03 +08:00
|
|
|
public long Id;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
[JsonConstructor]
|
|
|
|
public Channel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:31:03 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Contructs a private channel
|
2018-07-10 00:58:33 +08:00
|
|
|
/// TODO this class needs to be serialized from something like channels/private, instead of creating from the contructor
|
2018-04-14 19:31:03 +08:00
|
|
|
/// </summary>
|
2018-07-10 00:52:28 +08:00
|
|
|
/// <param name="user">The user</param>
|
2018-04-14 19:31:03 +08:00
|
|
|
public Channel(User user)
|
|
|
|
{
|
|
|
|
Target = TargetType.User;
|
|
|
|
Name = user.Username;
|
|
|
|
Id = user.Id;
|
|
|
|
JoinedUsers.Add(user);
|
|
|
|
}
|
2018-07-10 00:58:33 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
public void AddLocalEcho(LocalEchoMessage message)
|
|
|
|
{
|
|
|
|
pendingMessages.Add(message);
|
|
|
|
Messages.Add(message);
|
|
|
|
|
|
|
|
NewMessagesArrived?.Invoke(new[] { message });
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddNewMessages(params Message[] messages)
|
|
|
|
{
|
|
|
|
messages = messages.Except(Messages).ToArray();
|
|
|
|
|
|
|
|
Messages.AddRange(messages);
|
|
|
|
|
|
|
|
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-10 02:39:16 +08:00
|
|
|
throw new InvalidOperationException("Attempted to add the same message again");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
Messages.Add(final);
|
|
|
|
PendingMessageResolved?.Invoke(echo, final);
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:31:03 +08:00
|
|
|
private void purgeOldMessages()
|
|
|
|
{
|
|
|
|
// never purge local echos
|
|
|
|
int messageCount = Messages.Count - pendingMessages.Count;
|
2018-07-10 02:39:16 +08:00
|
|
|
if (messageCount > MaxHistory)
|
|
|
|
Messages.RemoveRange(0, messageCount - MaxHistory);
|
2018-04-14 19:31:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 00:52:28 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
}
|