mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 17:32:54 +08:00
Implement missing API code for channel read state (#7425)
Implement missing API code for channel read state
This commit is contained in:
commit
da47d4b521
30
osu.Game/Online/API/Requests/MarkChannelAsReadRequest.cs
Normal file
30
osu.Game/Online/API/Requests/MarkChannelAsReadRequest.cs
Normal file
@ -0,0 +1,30 @@
|
||||
// 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.Net.Http;
|
||||
using osu.Framework.IO.Network;
|
||||
using osu.Game.Online.Chat;
|
||||
|
||||
namespace osu.Game.Online.API.Requests
|
||||
{
|
||||
public class MarkChannelAsReadRequest : APIRequest
|
||||
{
|
||||
private readonly Channel channel;
|
||||
private readonly Message message;
|
||||
|
||||
public MarkChannelAsReadRequest(Channel channel, Message message)
|
||||
{
|
||||
this.channel = channel;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
protected override string Target => $"chat/channels/{channel.Id}/mark-as-read/{message.Id}";
|
||||
|
||||
protected override WebRequest CreateWebRequest()
|
||||
{
|
||||
var req = base.CreateWebRequest();
|
||||
req.Method = HttpMethod.Put;
|
||||
return req;
|
||||
}
|
||||
}
|
||||
}
|
@ -36,6 +36,11 @@ namespace osu.Game.Online.Chat
|
||||
/// </summary>
|
||||
public readonly SortedList<Message> Messages = new SortedList<Message>(Comparer<Message>.Default);
|
||||
|
||||
/// <summary>
|
||||
/// Contains all the messages that weren't read by the user.
|
||||
/// </summary>
|
||||
public IEnumerable<Message> UnreadMessages => Messages.Where(m => LastReadId < m.Id);
|
||||
|
||||
/// <summary>
|
||||
/// Contains all the messages that are still pending for submission to the server.
|
||||
/// </summary>
|
||||
@ -75,6 +80,9 @@ namespace osu.Game.Online.Chat
|
||||
[JsonProperty(@"last_message_id")]
|
||||
public long? LastMessageId;
|
||||
|
||||
[JsonProperty(@"last_read_id")]
|
||||
public long? LastReadId;
|
||||
|
||||
/// <summary>
|
||||
/// Signalles if the current user joined this channel or not. Defaults to false.
|
||||
/// </summary>
|
||||
|
@ -445,6 +445,28 @@ namespace osu.Game.Online.Chat
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks the <paramref name="channel"/> as read
|
||||
/// </summary>
|
||||
/// <param name="channel">The channel that will be marked as read</param>
|
||||
public void MarkChannelAsRead(Channel channel)
|
||||
{
|
||||
if (channel.LastMessageId == channel.LastReadId)
|
||||
return;
|
||||
|
||||
var message = channel.Messages.LastOrDefault();
|
||||
|
||||
if (message == null)
|
||||
return;
|
||||
|
||||
var req = new MarkChannelAsReadRequest(channel, message);
|
||||
|
||||
req.Success += () => channel.LastReadId = message.Id;
|
||||
req.Failure += e => Logger.Error(e, $"Failed to mark channel {channel} up to '{message}' as read");
|
||||
|
||||
api.Queue(req);
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(IAPIProvider api)
|
||||
{
|
||||
|
@ -279,6 +279,10 @@ namespace osu.Game.Overlays
|
||||
currentChannelContainer.Clear(false);
|
||||
currentChannelContainer.Add(loaded);
|
||||
}
|
||||
|
||||
// mark channel as read when channel switched
|
||||
if (e.NewValue.Messages.Any())
|
||||
channelManager.MarkChannelAsRead(e.NewValue);
|
||||
}
|
||||
|
||||
private float startDragChatHeight;
|
||||
|
Loading…
Reference in New Issue
Block a user