Fetch existing private message channels on re-joining

This commit is contained in:
Dean Herbert 2020-06-08 17:49:45 +09:00
parent 5f6c35a577
commit 712fd6a944
4 changed files with 73 additions and 11 deletions

View File

@ -0,0 +1,34 @@
// 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.Linq;
using System.Net.Http;
using osu.Framework.IO.Network;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests
{
public class CreateChannelRequest : APIRequest<APIChatChannel>
{
private readonly Channel channel;
public CreateChannelRequest(Channel channel)
{
this.channel = channel;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.Method = HttpMethod.Post;
req.AddParameter("type", $"{ChannelType.PM}");
req.AddParameter("target_id", $"{channel.Users.First().Id}");
return req;
}
protected override string Target => @"chat/channels";
}
}

View File

@ -0,0 +1,18 @@
// 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.Collections.Generic;
using Newtonsoft.Json;
using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests.Responses
{
public class APIChatChannel
{
[JsonProperty(@"channel_id")]
public int? ChannelID { get; set; }
[JsonProperty(@"recent_messages")]
public List<Message> RecentMessages { get; set; }
}
}

View File

@ -84,7 +84,8 @@ private long[] userIds
public long? LastReadId;
/// <summary>
/// Signalles if the current user joined this channel or not. Defaults to false.
/// Signals if the current user joined this channel or not. Defaults to false.
/// Note that this does not guarantee a join has completed. Check Id > 0 for confirmation.
/// </summary>
public Bindable<bool> Joined = new Bindable<bool>();

View File

@ -86,7 +86,7 @@ public void OpenPrivateChannel(User user)
return;
CurrentChannel.Value = JoinedChannels.FirstOrDefault(c => c.Type == ChannelType.PM && c.Users.Count == 1 && c.Users.Any(u => u.Id == user.Id))
?? new Channel(user);
?? JoinChannel(new Channel(user));
}
private void currentChannelChanged(ValueChangedEvent<Channel> e)
@ -140,7 +140,7 @@ void dequeueAndRun()
target.AddLocalEcho(message);
// if this is a PM and the first message, we need to do a special request to create the PM channel
if (target.Type == ChannelType.PM && !target.Joined.Value)
if (target.Type == ChannelType.PM && target.Id == 0)
{
var createNewPrivateMessageRequest = new CreateNewPrivateMessageRequest(target.Users.First(), message);
@ -356,26 +356,35 @@ private Channel joinChannel(Channel channel, bool fetchInitialMessages = false)
// ensure we are joined to the channel
if (!channel.Joined.Value)
{
channel.Joined.Value = true;
switch (channel.Type)
{
case ChannelType.Multiplayer:
// join is implicit. happens when you join a multiplayer game.
// this will probably change in the future.
channel.Joined.Value = true;
joinChannel(channel, fetchInitialMessages);
return channel;
case ChannelType.Private:
// can't do this yet.
case ChannelType.PM:
var createRequest = new CreateChannelRequest(channel);
createRequest.Success += resChannel =>
{
if (resChannel.ChannelID.HasValue)
{
channel.Id = resChannel.ChannelID.Value;
handleChannelMessages(resChannel.RecentMessages);
channel.MessagesLoaded = true; // this will mark the channel as having received messages even if there were none.
}
};
api.Queue(createRequest);
break;
default:
var req = new JoinChannelRequest(channel, api.LocalUser.Value);
req.Success += () =>
{
channel.Joined.Value = true;
joinChannel(channel, fetchInitialMessages);
};
req.Success += () => joinChannel(channel, fetchInitialMessages);
req.Failure += ex => LeaveChannel(channel);
api.Queue(req);
return channel;