2019-01-24 16:43:03 +08:00
// 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.
2018-04-14 19:32:48 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2018-12-10 20:08:14 +08:00
using System.Threading.Tasks ;
2018-04-14 19:32:48 +08:00
using osu.Framework.Allocation ;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables ;
2018-04-14 19:32:48 +08:00
using osu.Framework.Logging ;
using osu.Game.Online.API ;
using osu.Game.Online.API.Requests ;
2019-05-12 18:11:16 +08:00
using osu.Game.Overlays.Chat.Tabs ;
2018-04-14 19:32:48 +08:00
using osu.Game.Users ;
namespace osu.Game.Online.Chat
{
/// <summary>
/// Manages everything channel related
/// </summary>
2018-12-10 20:08:14 +08:00
public class ChannelManager : PollingComponent
2018-04-14 19:32:48 +08:00
{
/// <summary>
/// The channels the player joins on startup
/// </summary>
private readonly string [ ] defaultChannels =
{
@"#lazer" ,
@"#osu" ,
@"#lobby"
} ;
2019-01-07 17:50:27 +08:00
private readonly BindableList < Channel > availableChannels = new BindableList < Channel > ( ) ;
private readonly BindableList < Channel > joinedChannels = new BindableList < Channel > ( ) ;
2018-11-22 02:15:55 +08:00
2018-04-14 19:32:48 +08:00
/// <summary>
/// The currently opened channel
/// </summary>
public Bindable < Channel > CurrentChannel { get ; } = new Bindable < Channel > ( ) ;
/// <summary>
/// The Channels the player has joined
/// </summary>
2019-01-07 17:50:27 +08:00
public IBindableList < Channel > JoinedChannels = > joinedChannels ;
2018-04-14 19:32:48 +08:00
/// <summary>
/// The channels available for the player to join
/// </summary>
2019-01-07 17:50:27 +08:00
public IBindableList < Channel > AvailableChannels = > availableChannels ;
2018-04-14 19:32:48 +08:00
2018-04-19 02:46:42 +08:00
private IAPIProvider api ;
2018-12-10 20:08:14 +08:00
public readonly BindableBool HighPollRate = new BindableBool ( ) ;
2018-04-14 19:32:48 +08:00
2018-09-14 11:06:04 +08:00
public ChannelManager ( )
{
CurrentChannel . ValueChanged + = currentChannelChanged ;
2018-12-10 20:08:14 +08:00
2019-02-22 19:13:38 +08:00
HighPollRate . BindValueChanged ( enabled = > TimeBetweenPolls = enabled . NewValue ? 1000 : 6000 , true ) ;
2018-09-14 11:06:04 +08:00
}
2018-07-24 10:54:11 +08:00
/// <summary>
/// Opens a channel or switches to the channel if already opened.
/// </summary>
/// <exception cref="ChannelNotFoundException">If the name of the specifed channel was not found this exception will be thrown.</exception>
/// <param name="name"></param>
2018-04-14 19:32:48 +08:00
public void OpenChannel ( string name )
{
if ( name = = null )
throw new ArgumentNullException ( nameof ( name ) ) ;
2018-11-14 12:19:20 +08:00
CurrentChannel . Value = AvailableChannels . FirstOrDefault ( c = > c . Name = = name ) ? ? throw new ChannelNotFoundException ( name ) ;
2018-04-14 19:32:48 +08:00
}
2018-07-24 10:54:11 +08:00
/// <summary>
/// Opens a new private channel.
/// </summary>
2018-07-24 11:14:33 +08:00
/// <param name="user">The user the private channel is opened with.</param>
2018-07-24 10:54:11 +08:00
public void OpenPrivateChannel ( User user )
2018-04-14 19:32:48 +08:00
{
if ( user = = null )
throw new ArgumentNullException ( nameof ( user ) ) ;
2018-11-13 14:20:40 +08:00
CurrentChannel . Value = JoinedChannels . FirstOrDefault ( c = > c . Type = = ChannelType . PM & & c . Users . Count = = 1 & & c . Users . Any ( u = > u . Id = = user . Id ) )
2018-12-07 12:56:21 +08:00
? ? new Channel ( user ) ;
2018-04-14 19:32:48 +08:00
}
2019-05-12 07:13:48 +08:00
private void currentChannelChanged ( ValueChangedEvent < Channel > e )
{
2019-05-12 18:31:11 +08:00
if ( ! ( e . NewValue is ChannelSelectorTabItem . ChannelSelectorTabChannel ) )
2019-05-12 07:13:48 +08:00
JoinChannel ( e . NewValue ) ;
}
2018-11-13 16:24:11 +08:00
/// <summary>
/// Ensure we run post actions in sequence, once at a time.
/// </summary>
private readonly Queue < Action > postQueue = new Queue < Action > ( ) ;
2018-04-14 19:32:48 +08:00
/// <summary>
/// Posts a message to the currently opened channel.
/// </summary>
/// <param name="text">The message text that is going to be posted</param>
/// <param name="isAction">Is true if the message is an action, e.g.: user is currently eating </param>
2018-12-20 16:01:08 +08:00
/// <param name="target">An optional target channel. If null, <see cref="CurrentChannel"/> will be used.</param>
public void PostMessage ( string text , bool isAction = false , Channel target = null )
2018-04-14 19:32:48 +08:00
{
2018-12-20 16:01:08 +08:00
if ( target = = null )
target = CurrentChannel . Value ;
2018-04-14 19:32:48 +08:00
2018-12-20 16:01:08 +08:00
if ( target = = null )
return ;
2018-07-10 03:00:39 +08:00
2018-11-13 16:24:11 +08:00
void dequeueAndRun ( )
2018-04-14 19:32:48 +08:00
{
2018-11-13 16:24:11 +08:00
if ( postQueue . Count > 0 )
postQueue . Dequeue ( ) . Invoke ( ) ;
2018-04-14 19:32:48 +08:00
}
2018-11-13 16:24:11 +08:00
postQueue . Enqueue ( ( ) = >
2018-04-14 19:32:48 +08:00
{
2018-11-13 16:24:11 +08:00
if ( ! api . IsLoggedIn )
{
2018-12-20 16:01:08 +08:00
target . AddNewMessages ( new ErrorMessage ( "Please sign in to participate in chat!" ) ) ;
2018-11-13 16:24:11 +08:00
return ;
}
2018-04-14 19:32:48 +08:00
2018-11-13 16:24:11 +08:00
var message = new LocalEchoMessage
{
Sender = api . LocalUser . Value ,
Timestamp = DateTimeOffset . Now ,
2018-12-20 16:01:08 +08:00
ChannelId = target . Id ,
2018-11-13 16:24:11 +08:00
IsAction = isAction ,
Content = text
} ;
2018-04-14 19:32:48 +08:00
2018-12-20 16:01:08 +08:00
target . AddLocalEcho ( message ) ;
2018-11-13 16:24:11 +08:00
// if this is a PM and the first message, we need to do a special request to create the PM channel
2019-02-21 17:56:34 +08:00
if ( target . Type = = ChannelType . PM & & ! target . Joined . Value )
2018-11-13 16:24:11 +08:00
{
2018-12-20 16:01:08 +08:00
var createNewPrivateMessageRequest = new CreateNewPrivateMessageRequest ( target . Users . First ( ) , message ) ;
2018-11-14 12:59:02 +08:00
2018-11-13 16:24:11 +08:00
createNewPrivateMessageRequest . Success + = createRes = >
{
2018-12-20 16:01:08 +08:00
target . Id = createRes . ChannelID ;
target . ReplaceMessage ( message , createRes . Message ) ;
2018-11-13 16:24:11 +08:00
dequeueAndRun ( ) ;
} ;
2018-11-14 12:59:02 +08:00
2018-11-13 16:24:11 +08:00
createNewPrivateMessageRequest . Failure + = exception = >
{
Logger . Error ( exception , "Posting message failed." ) ;
2018-12-20 16:01:08 +08:00
target . ReplaceMessage ( message , null ) ;
2018-11-13 16:24:11 +08:00
dequeueAndRun ( ) ;
} ;
api . Queue ( createNewPrivateMessageRequest ) ;
return ;
}
var req = new PostMessageRequest ( message ) ;
2018-11-14 12:59:02 +08:00
2018-11-13 16:24:11 +08:00
req . Success + = m = >
{
2018-12-20 16:01:08 +08:00
target . ReplaceMessage ( message , m ) ;
2018-11-13 16:24:11 +08:00
dequeueAndRun ( ) ;
} ;
2018-11-14 12:59:02 +08:00
2018-11-13 16:24:11 +08:00
req . Failure + = exception = >
{
Logger . Error ( exception , "Posting message failed." ) ;
2018-12-20 16:01:08 +08:00
target . ReplaceMessage ( message , null ) ;
2018-11-13 16:24:11 +08:00
dequeueAndRun ( ) ;
} ;
2018-11-14 12:59:02 +08:00
2018-11-13 16:24:11 +08:00
api . Queue ( req ) ;
} ) ;
// always run if the queue is empty
if ( postQueue . Count = = 1 )
dequeueAndRun ( ) ;
2018-04-14 19:32:48 +08:00
}
2018-07-24 11:14:33 +08:00
/// <summary>
/// Posts a command locally. Commands like /help will result in a help message written in the current channel.
/// </summary>
/// <param name="text">the text containing the command identifier and command parameters.</param>
2018-12-20 16:01:08 +08:00
/// <param name="target">An optional target channel. If null, <see cref="CurrentChannel"/> will be used.</param>
public void PostCommand ( string text , Channel target = null )
2018-04-14 19:32:48 +08:00
{
2018-12-20 16:01:08 +08:00
if ( target = = null )
target = CurrentChannel . Value ;
if ( target = = null )
2018-04-14 19:32:48 +08:00
return ;
var parameters = text . Split ( new [ ] { ' ' } , 2 ) ;
string command = parameters [ 0 ] ;
string content = parameters . Length = = 2 ? parameters [ 1 ] : string . Empty ;
switch ( command )
{
case "me" :
if ( string . IsNullOrWhiteSpace ( content ) )
{
2018-12-20 16:01:08 +08:00
target . AddNewMessages ( new ErrorMessage ( "Usage: /me [action]" ) ) ;
2018-04-14 19:32:48 +08:00
break ;
}
PostMessage ( content , true ) ;
break ;
case "help" :
2018-12-20 16:01:08 +08:00
target . AddNewMessages ( new InfoMessage ( "Supported commands: /help, /me [action]" ) ) ;
2018-04-14 19:32:48 +08:00
break ;
default :
2018-12-20 16:01:08 +08:00
target . AddNewMessages ( new ErrorMessage ( $@"""/{command}"" is not supported! For a list of supported commands see /help" ) ) ;
2018-04-14 19:32:48 +08:00
break ;
}
}
private void handleChannelMessages ( IEnumerable < Message > messages )
{
var channels = JoinedChannels . ToList ( ) ;
2018-11-12 19:41:10 +08:00
foreach ( var group in messages . GroupBy ( m = > m . ChannelId ) )
2018-04-14 19:32:48 +08:00
channels . Find ( c = > c . Id = = group . Key ) ? . AddNewMessages ( group . ToArray ( ) ) ;
}
2018-11-13 16:24:11 +08:00
private void initializeChannels ( )
2018-04-14 19:32:48 +08:00
{
var req = new ListChannelsRequest ( ) ;
2018-11-13 16:24:11 +08:00
var joinDefaults = JoinedChannels . Count = = 0 ;
2018-11-13 14:20:40 +08:00
2018-04-14 19:32:48 +08:00
req . Success + = channels = >
{
2018-07-24 23:51:20 +08:00
foreach ( var channel in channels )
{
2018-11-21 16:15:10 +08:00
var ch = getChannel ( channel , addToAvailable : true ) ;
2018-07-24 23:51:20 +08:00
// join any channels classified as "defaults"
2018-11-13 16:24:11 +08:00
if ( joinDefaults & & defaultChannels . Any ( c = > c . Equals ( channel . Name , StringComparison . OrdinalIgnoreCase ) ) )
2018-11-21 16:15:10 +08:00
JoinChannel ( ch ) ;
2018-07-24 23:51:20 +08:00
}
2018-04-14 19:32:48 +08:00
} ;
2018-07-24 10:54:11 +08:00
req . Failure + = error = >
{
Logger . Error ( error , "Fetching channel list failed" ) ;
2018-11-13 16:24:11 +08:00
initializeChannels ( ) ;
2018-07-24 10:54:11 +08:00
} ;
2018-04-14 19:32:48 +08:00
api . Queue ( req ) ;
}
2018-07-30 03:40:43 +08:00
/// <summary>
/// Fetches inital messages of a channel
///
/// TODO: remove this when the API supports returning initial fetch messages for more than one channel by specifying the last message id per channel instead of one last message id globally.
/// right now it caps out at 50 messages and therefore only returns one channel's worth of content.
/// </summary>
/// <param name="channel">The channel </param>
2018-11-13 14:20:40 +08:00
private void fetchInitalMessages ( Channel channel )
2018-07-30 03:40:43 +08:00
{
2018-11-13 16:24:11 +08:00
if ( channel . Id < = 0 ) return ;
2018-11-12 19:41:10 +08:00
var fetchInitialMsgReq = new GetMessagesRequest ( channel ) ;
2018-11-13 16:24:11 +08:00
fetchInitialMsgReq . Success + = messages = >
{
handleChannelMessages ( messages ) ;
2018-11-14 12:19:20 +08:00
channel . MessagesLoaded = true ; // this will mark the channel as having received messages even if there were none.
2018-11-13 16:24:11 +08:00
} ;
2018-07-30 03:40:43 +08:00
api . Queue ( fetchInitialMsgReq ) ;
}
2018-11-21 16:15:10 +08:00
/// <summary>
/// Find an existing channel instance for the provided channel. Lookup is performed basd on ID.
/// The provided channel may be used if an existing instance is not found.
/// </summary>
/// <param name="lookup">A candidate channel to be used for lookup or permanently on lookup failure.</param>
/// <param name="addToAvailable">Whether the channel should be added to <see cref="AvailableChannels"/> if not already.</param>
/// <param name="addToJoined">Whether the channel should be added to <see cref="JoinedChannels"/> if not already.</param>
/// <returns>The found channel.</returns>
private Channel getChannel ( Channel lookup , bool addToAvailable = false , bool addToJoined = false )
2018-11-13 14:20:40 +08:00
{
2018-11-21 16:15:10 +08:00
Channel found = null ;
2018-11-13 14:20:40 +08:00
2018-11-22 17:27:22 +08:00
bool lookupCondition ( Channel ch ) = > lookup . Id > 0 ? ch . Id = = lookup . Id : lookup . Name = = ch . Name ;
var available = AvailableChannels . FirstOrDefault ( lookupCondition ) ;
2018-11-21 16:15:10 +08:00
if ( available ! = null )
found = available ;
2018-11-13 14:20:40 +08:00
2018-11-22 17:27:22 +08:00
var joined = JoinedChannels . FirstOrDefault ( lookupCondition ) ;
2018-11-21 16:15:10 +08:00
if ( found = = null & & joined ! = null )
found = joined ;
if ( found = = null )
2018-11-13 14:20:40 +08:00
{
2018-11-21 16:15:10 +08:00
found = lookup ;
// if we're using a channel object from the server, we want to remove ourselves from the users list.
// this is because we check the first user in the channel to display a name/icon on tabs for now.
var foundSelf = found . Users . FirstOrDefault ( u = > u . Id = = api . LocalUser . Value . Id ) ;
2018-11-13 14:20:40 +08:00
if ( foundSelf ! = null )
2018-11-21 16:15:10 +08:00
found . Users . Remove ( foundSelf ) ;
}
2018-11-22 06:21:27 +08:00
if ( joined = = null & & addToJoined ) joinedChannels . Add ( found ) ;
if ( available = = null & & addToAvailable ) availableChannels . Add ( found ) ;
2018-11-21 16:15:10 +08:00
return found ;
}
2018-11-13 14:20:40 +08:00
2018-11-21 16:15:10 +08:00
/// <summary>
/// Joins a channel if it has not already been joined.
/// </summary>
/// <param name="channel">The channel to join.</param>
/// <param name="alreadyJoined">Whether the channel has already been joined server-side. Will skip a join request.</param>
/// <returns>The joined channel. Note that this may not match the parameter channel as it is a backed object.</returns>
public Channel JoinChannel ( Channel channel , bool alreadyJoined = false )
{
if ( channel = = null ) return null ;
2018-11-13 14:20:40 +08:00
2018-11-21 16:15:10 +08:00
channel = getChannel ( channel , addToJoined : true ) ;
// ensure we are joined to the channel
if ( ! channel . Joined . Value )
{
2018-11-21 19:44:41 +08:00
if ( alreadyJoined )
channel . Joined . Value = true ;
else
2018-11-13 14:20:40 +08:00
{
2018-11-21 19:44:41 +08:00
switch ( channel . Type )
{
case ChannelType . Public :
2019-02-21 17:56:34 +08:00
var req = new JoinChannelRequest ( channel , api . LocalUser . Value ) ;
2018-11-21 19:44:41 +08:00
req . Success + = ( ) = > JoinChannel ( channel , true ) ;
req . Failure + = ex = > LeaveChannel ( channel ) ;
api . Queue ( req ) ;
return channel ;
}
2018-11-13 14:20:40 +08:00
}
}
if ( CurrentChannel . Value = = null )
CurrentChannel . Value = channel ;
2018-11-13 16:24:11 +08:00
if ( ! channel . MessagesLoaded )
2018-11-13 14:20:40 +08:00
{
// let's fetch a small number of messages to bring us up-to-date with the backlog.
fetchInitalMessages ( channel ) ;
}
2018-11-21 16:15:10 +08:00
return channel ;
2018-11-13 14:20:40 +08:00
}
public void LeaveChannel ( Channel channel )
{
if ( channel = = null ) return ;
2018-11-22 02:15:55 +08:00
if ( channel = = CurrentChannel . Value )
CurrentChannel . Value = null ;
2018-11-13 14:20:40 +08:00
2018-11-22 02:15:55 +08:00
joinedChannels . Remove ( channel ) ;
2018-11-13 14:20:40 +08:00
if ( channel . Joined . Value )
{
2019-02-21 17:56:34 +08:00
api . Queue ( new LeaveChannelRequest ( channel , api . LocalUser . Value ) ) ;
2018-11-13 14:20:40 +08:00
channel . Joined . Value = false ;
}
}
private long lastMessageId ;
2018-11-13 16:24:11 +08:00
private bool channelsInitialised ;
2018-12-10 20:08:14 +08:00
protected override Task Poll ( )
2018-11-13 14:20:40 +08:00
{
2018-12-10 20:08:14 +08:00
if ( ! api . IsLoggedIn )
return base . Poll ( ) ;
var fetchReq = new GetUpdatesRequest ( lastMessageId ) ;
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
var tcs = new TaskCompletionSource < bool > ( ) ;
fetchReq . Success + = updates = >
{
if ( updates ? . Presence ! = null )
2018-11-13 14:20:40 +08:00
{
2018-12-10 20:08:14 +08:00
foreach ( var channel in updates . Presence )
2018-11-13 14:20:40 +08:00
{
2018-12-10 20:08:14 +08:00
// we received this from the server so should mark the channel already joined.
JoinChannel ( channel , true ) ;
}
2018-11-13 16:24:11 +08:00
2018-12-10 20:08:14 +08:00
//todo: handle left channels
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
handleChannelMessages ( updates . Messages ) ;
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
foreach ( var group in updates . Messages . GroupBy ( m = > m . ChannelId ) )
JoinedChannels . FirstOrDefault ( c = > c . Id = = group . Key ) ? . AddNewMessages ( group . ToArray ( ) ) ;
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
lastMessageId = updates . Messages . LastOrDefault ( ) ? . Id ? ? lastMessageId ;
}
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
if ( ! channelsInitialised )
{
channelsInitialised = true ;
// we want this to run after the first presence so we can see if the user is in any channels already.
initializeChannels ( ) ;
}
2018-11-21 16:14:48 +08:00
2018-12-10 20:08:14 +08:00
tcs . SetResult ( true ) ;
} ;
fetchReq . Failure + = _ = > tcs . SetResult ( false ) ;
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
api . Queue ( fetchReq ) ;
2018-11-13 14:20:40 +08:00
2018-12-10 20:08:14 +08:00
return tcs . Task ;
2018-11-13 14:20:40 +08:00
}
2018-04-14 19:32:48 +08:00
[BackgroundDependencyLoader]
private void load ( IAPIProvider api )
{
2018-04-19 02:46:42 +08:00
this . api = api ;
2018-04-14 19:32:48 +08:00
}
}
2018-07-10 00:45:11 +08:00
2018-07-24 11:14:33 +08:00
/// <summary>
/// An exception thrown when a channel could not been found.
/// </summary>
2018-07-10 00:45:11 +08:00
public class ChannelNotFoundException : Exception
{
public ChannelNotFoundException ( string channelName )
: base ( $"A channel with the name {channelName} could not be found." )
{
}
}
2018-04-14 19:32:48 +08:00
}