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