2022-10-28 15:19:15 +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.
|
|
|
|
|
|
|
|
using System;
|
2022-10-28 16:53:28 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-10-28 15:19:15 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2024-01-25 19:25:27 +08:00
|
|
|
using osu.Game.Online;
|
2022-10-28 15:19:15 +08:00
|
|
|
using osu.Game.Online.API;
|
2022-10-28 16:53:28 +08:00
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.Chat;
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
namespace osu.Game.Tests
|
2022-10-28 15:19:15 +08:00
|
|
|
{
|
2024-01-25 19:25:27 +08:00
|
|
|
public class PollingChatClient : PersistentEndpointClient
|
2022-10-28 15:19:15 +08:00
|
|
|
{
|
2024-01-25 19:25:27 +08:00
|
|
|
public event Action<Channel>? ChannelJoined;
|
|
|
|
public event Action<List<Message>>? NewMessages;
|
|
|
|
public event Action? PresenceReceived;
|
2022-10-28 16:53:28 +08:00
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
private readonly IAPIProvider api;
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
private long lastMessageId;
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
public PollingChatClient(IAPIProvider api)
|
2022-10-28 15:19:15 +08:00
|
|
|
{
|
2024-01-25 19:25:27 +08:00
|
|
|
this.api = api;
|
2022-10-28 15:19:15 +08:00
|
|
|
}
|
|
|
|
|
2022-11-04 17:48:34 +08:00
|
|
|
public override Task ConnectAsync(CancellationToken cancellationToken)
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
2024-01-25 19:25:27 +08:00
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
await api.PerformAsync(CreateInitialFetchRequest()).ConfigureAwait(true);
|
|
|
|
await Task.Delay(1000, cancellationToken).ConfigureAwait(true);
|
|
|
|
}
|
|
|
|
}, cancellationToken);
|
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2022-11-21 14:22:57 +08:00
|
|
|
protected APIRequest CreateInitialFetchRequest(long? lastMessageId = null)
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
|
|
|
var fetchReq = new GetUpdatesRequest(lastMessageId ?? this.lastMessageId);
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
fetchReq.Success += updates =>
|
|
|
|
{
|
|
|
|
if (updates?.Presence != null)
|
|
|
|
{
|
|
|
|
foreach (var channel in updates.Presence)
|
2024-01-25 19:25:27 +08:00
|
|
|
handleChannelJoined(channel);
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
//todo: handle left channels
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
handleMessages(updates.Messages);
|
2022-10-28 15:19:15 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
PresenceReceived?.Invoke();
|
|
|
|
};
|
|
|
|
|
|
|
|
return fetchReq;
|
2022-10-28 15:19:15 +08:00
|
|
|
}
|
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
private void handleChannelJoined(Channel channel)
|
2022-11-07 13:34:53 +08:00
|
|
|
{
|
|
|
|
channel.Joined.Value = true;
|
|
|
|
ChannelJoined?.Invoke(channel);
|
|
|
|
}
|
2022-10-28 15:19:15 +08:00
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
private void handleMessages(List<Message>? messages)
|
2022-10-28 15:19:15 +08:00
|
|
|
{
|
2022-11-21 14:17:54 +08:00
|
|
|
if (messages == null)
|
|
|
|
return;
|
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
NewMessages?.Invoke(messages);
|
|
|
|
lastMessageId = Math.Max(lastMessageId, messages.LastOrDefault()?.Id ?? 0);
|
2022-10-28 15:19:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|