1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 06:47:24 +08:00

Add chat sending support.

This commit is contained in:
Dean Herbert 2017-04-19 18:46:26 +09:00
parent 87f6dc9e5a
commit 3129708ccb
No known key found for this signature in database
GPG Key ID: 46D71BF4958ABB49
5 changed files with 87 additions and 17 deletions

View File

@ -0,0 +1,33 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Extensions;
using osu.Framework.IO.Network;
using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests
{
public class PostMessageRequest : APIRequest<Message>
{
private readonly Message message;
public PostMessageRequest(Message message)
{
this.message = message;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.Method = HttpMethod.POST;
req.AddParameter(@"target_type", message.TargetType.GetDescription());
req.AddParameter(@"target_id", message.TargetId.ToString());
req.AddParameter(@"message", message.Content);
return req;
}
protected override string Target => @"chat/messages";
}
}

View File

@ -3,7 +3,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using osu.Framework.Lists;
namespace osu.Game.Online.Chat
{
@ -21,7 +23,7 @@ namespace osu.Game.Online.Chat
[JsonProperty(@"channel_id")]
public int Id;
public List<Message> Messages = new List<Message>();
public SortedList<Message> Messages = new SortedList<Message>((m1, m2) => m1.Id.CompareTo(m2.Id));
//internal bool Joined;
@ -36,7 +38,10 @@ namespace osu.Game.Online.Chat
public void AddNewMessages(IEnumerable<Message> messages)
{
messages = messages.Except(Messages).ToList();
Messages.AddRange(messages);
purgeOldMessages();
NewMessagesArrived?.Invoke(messages);

View File

@ -11,7 +11,7 @@ namespace osu.Game.Online.Chat
public class Message
{
[JsonProperty(@"message_id")]
public long Id;
public readonly long Id;
//todo: this should be inside sender.
[JsonProperty(@"user_id")]
@ -36,6 +36,18 @@ namespace osu.Game.Online.Chat
public Message()
{
}
public override bool Equals(object obj)
{
var objMessage = obj as Message;
return Id == objMessage?.Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
public enum TargetType

View File

@ -89,25 +89,44 @@ namespace osu.Game.Overlays
return false;
}
private void postMessage(TextBox sender, bool newText)
private void postMessage(TextBox textbox, bool newText)
{
var postText = sender.Text;
var postText = textbox.Text;
if (!string.IsNullOrEmpty(postText) && api.LocalUser.Value != null)
{
//todo: actually send to server
careChannels.FirstOrDefault()?.AddNewMessages(new[]
{
new Message
{
User = api.LocalUser.Value,
Timestamp = DateTimeOffset.Now,
Content = postText
}
});
}
var currentChannel = careChannels.FirstOrDefault();
sender.Text = string.Empty;
if (currentChannel == null) return;
var message = new Message
{
User = api.LocalUser.Value,
Timestamp = DateTimeOffset.Now,
TargetType = TargetType.Channel, //TODO: read this from currentChannel
TargetId = currentChannel.Id,
Content = postText
};
textbox.ReadOnly = true;
var req = new PostMessageRequest(message);
req.Failure += e =>
{
textbox.FlashColour(Color4.Red, 1000);
textbox.ReadOnly = false;
};
req.Success += m =>
{
currentChannel.AddNewMessages(new[] { m });
textbox.ReadOnly = false;
textbox.Text = string.Empty;
};
api.Queue(req);
}
}
[BackgroundDependencyLoader]
@ -137,7 +156,7 @@ namespace osu.Game.Overlays
fetchReq.Success += delegate (List<Message> messages)
{
var ids = messages.Where(m => m.TargetType == TargetType.Channel).Select(m => m.TargetId).Distinct();
//batch messages per channel.
foreach (var id in ids)
careChannels.Find(c => c.Id == id)?.AddNewMessages(messages.Where(m => m.TargetId == id));

View File

@ -72,6 +72,7 @@
<Compile Include="Audio\SampleInfo.cs" />
<Compile Include="Beatmaps\Drawables\BeatmapBackgroundSprite.cs" />
<Compile Include="Beatmaps\DifficultyCalculator.cs" />
<Compile Include="Online\API\Requests\PostMessageRequest.cs" />
<Compile Include="Rulesets\Beatmaps\BeatmapConverter.cs" />
<Compile Include="Rulesets\Beatmaps\BeatmapProcessor.cs" />
<Compile Include="Beatmaps\Legacy\LegacyBeatmap.cs" />