Implement missing API code for channel read state (#7425)

Implement missing API code for channel read state
This commit is contained in:
Dean Herbert 2020-01-13 11:42:10 +08:00 committed by GitHub
commit da47d4b521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 0 deletions

View 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;
}
}
}

View File

@ -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>

View File

@ -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)
{

View File

@ -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;