1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 13:32:54 +08:00

Update chat to work with new API version

This commit is contained in:
Dean Herbert 2018-09-25 20:53:24 +09:00
parent 5e51a50658
commit 7cd547a760
12 changed files with 168 additions and 46 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Diagnostics; using System.Diagnostics;
using System.Net.Http;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.IO.Network; using osu.Framework.IO.Network;
@ -40,7 +41,7 @@ namespace osu.Game.Online.API
using (var req = new AccessTokenRequestPassword(username, password) using (var req = new AccessTokenRequestPassword(username, password)
{ {
Url = $@"{endpoint}/oauth/token", Url = $@"{endpoint}/oauth/token",
Method = HttpMethod.POST, Method = HttpMethod.Post,
ClientId = clientId, ClientId = clientId,
ClientSecret = clientSecret ClientSecret = clientSecret
}) })
@ -66,7 +67,7 @@ namespace osu.Game.Online.API
using (var req = new AccessTokenRequestRefresh(refresh) using (var req = new AccessTokenRequestRefresh(refresh)
{ {
Url = $@"{endpoint}/oauth/token", Url = $@"{endpoint}/oauth/token",
Method = HttpMethod.POST, Method = HttpMethod.Post,
ClientId = clientId, ClientId = clientId,
ClientSecret = clientSecret ClientSecret = clientSecret
}) })

View File

@ -2,34 +2,19 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.IO.Network;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests namespace osu.Game.Online.API.Requests
{ {
public class GetMessagesRequest : APIRequest<List<Message>> public class GetMessagesRequest : APIRequest<List<Message>>
{ {
private readonly List<Channel> channels; private readonly Channel channel;
private readonly long? since;
public GetMessagesRequest(List<Channel> channels, long? sinceId) public GetMessagesRequest(Channel channel)
{ {
this.channels = channels; this.channel = channel;
since = sinceId;
} }
protected override WebRequest CreateWebRequest() protected override string Target => $@"chat/channels/{channel.Id}/messages";
{
string channelString = string.Join(",", channels.Select(x => x.Id));
var req = base.CreateWebRequest();
req.AddParameter(@"channels", channelString);
if (since.HasValue) req.AddParameter(@"since", since.Value.ToString());
return req;
}
protected override string Target => @"chat/messages";
} }
} }

View File

@ -0,0 +1,32 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using JetBrains.Annotations;
using osu.Framework.IO.Network;
using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests
{
public class GetUpdatesRequest : APIRequest<GetUpdatesResponse>
{
private readonly long since;
private readonly Channel channel;
public GetUpdatesRequest(long sinceId, [CanBeNull] Channel channel = null)
{
this.channel = channel;
since = sinceId;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
if (channel != null) req.AddParameter(@"channel", channel.Id.ToString());
req.AddParameter(@"since", since.ToString());
return req;
}
protected override string Target => @"chat/updates";
}
}

View File

@ -0,0 +1,14 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests
{
public class GetUpdatesResponse
{
public List<Channel> Presence;
public List<Message> Messages;
}
}

View File

@ -0,0 +1,31 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Net.Http;
using osu.Framework.IO.Network;
using osu.Game.Online.Chat;
using osu.Game.Users;
namespace osu.Game.Online.API.Requests
{
public class JoinChannelRequest : APIRequest<Channel>
{
private readonly Channel channel;
private readonly User user;
public JoinChannelRequest(Channel channel, User user)
{
this.channel = channel;
this.user = user;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.Method = HttpMethod.Put;
return req;
}
protected override string Target => $@"chat/channels/{channel.Id}/users/{user.Id}";
}
}

View File

@ -0,0 +1,31 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Net.Http;
using osu.Framework.IO.Network;
using osu.Game.Online.Chat;
using osu.Game.Users;
namespace osu.Game.Online.API.Requests
{
public class LeaveChannelRequest : APIRequest
{
private readonly Channel channel;
private readonly User user;
public LeaveChannelRequest(Channel channel, User user)
{
this.channel = channel;
this.user = user;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.Method = HttpMethod.Delete;
return req;
}
protected override string Target => $@"chat/channels/{channel.Id}/users/{user.Id}";
}
}

View File

@ -1,7 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Extensions; using System.Net.Http;
using osu.Framework.IO.Network; using osu.Framework.IO.Network;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
@ -20,15 +20,13 @@ namespace osu.Game.Online.API.Requests
{ {
var req = base.CreateWebRequest(); var req = base.CreateWebRequest();
req.Method = HttpMethod.POST; req.Method = HttpMethod.Post;
req.AddParameter(@"target_type", message.TargetType.GetDescription());
req.AddParameter(@"target_id", message.TargetId.ToString());
req.AddParameter(@"is_action", message.IsAction.ToString().ToLowerInvariant()); req.AddParameter(@"is_action", message.IsAction.ToString().ToLowerInvariant());
req.AddParameter(@"message", message.Content); req.AddParameter(@"message", message.Content);
return req; return req;
} }
protected override string Target => @"chat/messages"; protected override string Target => $@"chat/channels/{message.TargetId}/messages";
} }
} }

View File

@ -19,7 +19,7 @@ namespace osu.Game.Online.Chat
public string Topic; public string Topic;
[JsonProperty(@"type")] [JsonProperty(@"type")]
public string Type; public ChannelType Type;
[JsonProperty(@"channel_id")] [JsonProperty(@"channel_id")]
public int Id; public int Id;

View File

@ -0,0 +1,11 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Online.Chat
{
public enum ChannelType
{
PM,
Public
}
}

View File

@ -47,7 +47,7 @@ namespace osu.Game.Overlays
public const float TAB_AREA_HEIGHT = 50; public const float TAB_AREA_HEIGHT = 50;
private GetMessagesRequest fetchReq; private GetUpdatesRequest fetchReq;
private readonly ChatTabControl channelTabs; private readonly ChatTabControl channelTabs;
@ -285,7 +285,7 @@ namespace osu.Game.Overlays
chatBackground.Colour = colours.ChatBlue; chatBackground.Colour = colours.ChatBlue;
} }
private long? lastMessageId; private long lastMessageId;
private readonly List<Channel> careChannels = new List<Channel>(); private readonly List<Channel> careChannels = new List<Channel>();
@ -304,9 +304,9 @@ namespace osu.Game.Overlays
Scheduler.Add(delegate Scheduler.Add(delegate
{ {
addChannel(channels.Find(c => c.Name == @"#lazer")); //addChannel(channels.Find(c => c.Name == @"#lazer"));
addChannel(channels.Find(c => c.Name == @"#osu")); //addChannel(channels.Find(c => c.Name == @"#osu"));
addChannel(channels.Find(c => c.Name == @"#lobby")); //addChannel(channels.Find(c => c.Name == @"#lobby"));
channelSelection.OnRequestJoin = addChannel; channelSelection.OnRequestJoin = addChannel;
channelSelection.OnRequestLeave = removeChannel; channelSelection.OnRequestLeave = removeChannel;
@ -320,7 +320,7 @@ namespace osu.Game.Overlays
}; };
}); });
messageRequest = Scheduler.AddDelayed(fetchNewMessages, 1000, true); messageRequest = Scheduler.AddDelayed(fetchUpdates, 1000, true);
}; };
api.Queue(req); api.Queue(req);
@ -394,6 +394,15 @@ namespace osu.Game.Overlays
{ {
careChannels.Add(channel); careChannels.Add(channel);
channelTabs.AddItem(channel); channelTabs.AddItem(channel);
if (channel.Type == ChannelType.Public && !channel.Joined)
{
var req = new JoinChannelRequest(channel, api.LocalUser);
req.Success += addChannel;
req.Failure += ex => removeChannel(channel);
api.Queue(req);
return;
}
} }
// let's fetch a small number of messages to bring us up-to-date with the backlog. // let's fetch a small number of messages to bring us up-to-date with the backlog.
@ -415,39 +424,48 @@ namespace osu.Game.Overlays
loadedChannels.Remove(loadedChannels.Find(c => c.Channel == channel)); loadedChannels.Remove(loadedChannels.Find(c => c.Channel == channel));
channelTabs.RemoveItem(channel); channelTabs.RemoveItem(channel);
api.Queue(new LeaveChannelRequest(channel, api.LocalUser));
channel.Joined.Value = false; channel.Joined.Value = false;
} }
private void fetchInitialMessages(Channel channel) private void fetchInitialMessages(Channel channel)
{ {
var req = new GetMessagesRequest(new List<Channel> { channel }, null); var req = new GetMessagesRequest(channel);
req.Success += delegate (List<Message> messages) req.Success += messages =>
{ {
loading.Hide(); loading.Hide();
channel.AddNewMessages(messages.ToArray()); channel.AddNewMessages(messages.ToArray());
Debug.Write("success!"); Debug.Write("success!");
}; };
req.Failure += delegate
{ req.Failure += exception => Debug.Write("failure!");
Debug.Write("failure!");
};
api.Queue(req); api.Queue(req);
} }
private void fetchNewMessages() private void fetchUpdates()
{ {
if (fetchReq != null) return; if (fetchReq != null) return;
fetchReq = new GetMessagesRequest(careChannels, lastMessageId); fetchReq = new GetUpdatesRequest(lastMessageId);
fetchReq.Success += delegate (List<Message> messages) fetchReq.Success += updates =>
{ {
foreach (var group in messages.Where(m => m.TargetType == TargetType.Channel).GroupBy(m => m.TargetId)) // fuck the what.
careChannels.Find(c => c.Id == group.Key)?.AddNewMessages(group.ToArray()); if (updates?.Presence != null)
{
foreach (var channel in updates.Presence)
{
channel.Joined.Value = true;
addChannel(channel);
}
lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId; foreach (var group in updates.Messages.Where(m => m.TargetType == TargetType.Channel).GroupBy(m => m.TargetId))
careChannels.Find(c => c.Id == group.Key)?.AddNewMessages(group.ToArray());
lastMessageId = updates.Messages.LastOrDefault()?.Id ?? lastMessageId;
}
Debug.Write("success!"); Debug.Write("success!");
fetchReq = null; fetchReq = null;

View File

@ -18,7 +18,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.1.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="ppy.osu.Framework" Version="2018.920.1" /> <PackageReference Include="ppy.osu.Framework" Version="0.0.7306" />
<PackageReference Include="SharpCompress" Version="0.22.0" /> <PackageReference Include="SharpCompress" Version="0.22.0" />
<PackageReference Include="NUnit" Version="3.10.1" /> <PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="SharpRaven" Version="2.4.0" /> <PackageReference Include="SharpRaven" Version="2.4.0" />

View File

@ -218,6 +218,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GMT/@EntryIndexedValue">GMT</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GMT/@EntryIndexedValue">GMT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=QAT/@EntryIndexedValue">QAT</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=QAT/@EntryIndexedValue">QAT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BNG/@EntryIndexedValue">BNG</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BNG/@EntryIndexedValue">BNG</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&#xD; <s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&#xD;
&lt;Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"&gt;&#xD; &lt;Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"&gt;&#xD;