@@ -78,29 +78,6 @@ internal Chat(PubnubChatConfig chatConfig, PNConfiguration pubnubConfig, ChatLis
78
78
RateLimiter = new ExponentialRateLimiter ( chatConfig . RateLimitFactor ) ;
79
79
}
80
80
81
- /// <summary>
82
- /// Initializes a new instance of the <see cref="Chat"/> class.
83
- /// <para>
84
- /// Creates a new chat instance.
85
- /// </para>
86
- /// </summary>
87
- /// <param name="chatConfig">Config with Chat specific parameters</param>
88
- /// <param name="pubnub">An already initialised instance of Pubnub</param>
89
- /// /// <param name="listenerFactory">Optional injectable listener factory, used in Unity to allow for dispatching Chat callbacks on main thread.</param>
90
- /// <remarks>
91
- /// The constructor initializes the Chat object with the provided existing Pubnub instance.
92
- /// </remarks>
93
- public static async Task < Chat > CreateInstance ( PubnubChatConfig chatConfig , Pubnub pubnub , ChatListenerFactory ? listenerFactory = null )
94
- {
95
- var chat = new Chat ( chatConfig , pubnub , listenerFactory ) ;
96
- var user = await chat . GetCurrentUser ( ) ;
97
- if ( user == null )
98
- {
99
- await chat . CreateUser ( chat . PubnubInstance . GetCurrentUserId ( ) ) ;
100
- }
101
- return chat ;
102
- }
103
-
104
81
internal Chat ( PubnubChatConfig chatConfig , Pubnub pubnub , ChatListenerFactory ? listenerFactory = null )
105
82
{
106
83
Config = chatConfig ;
@@ -518,7 +495,9 @@ public async Task<ChannelsResponseWrapper> GetChannels(string filter = "", strin
518
495
/// <seealso cref="ChatChannelData"/>
519
496
public async Task < ChatOperationResult > UpdateChannel ( string channelId , ChatChannelData updatedData )
520
497
{
521
- throw new NotImplementedException ( ) ;
498
+ var result = new ChatOperationResult ( ) ;
499
+ result . RegisterOperation ( await Channel . UpdateChannelData ( this , channelId , updatedData ) ) ;
500
+ return result ;
522
501
}
523
502
524
503
/// <summary>
@@ -545,10 +524,54 @@ public async Task<ChatOperationResult> DeleteChannel(string channelId)
545
524
546
525
#region Users
547
526
548
- public async Task < UserMentionsWrapper > GetCurrentUserMentions ( string startTimeToken , string endTimeToken ,
527
+ public async Task < ChatOperationResult < UserMentionsWrapper > > GetCurrentUserMentions ( string startTimeToken , string endTimeToken ,
549
528
int count )
550
529
{
551
- throw new NotImplementedException ( ) ;
530
+ var result = new ChatOperationResult < UserMentionsWrapper > ( ) ;
531
+ var id = PubnubInstance . GetCurrentUserId ( ) ;
532
+ var getEventHistory = await GetEventsHistory ( id , startTimeToken , endTimeToken , count ) ;
533
+ if ( result . RegisterOperation ( getEventHistory ) )
534
+ {
535
+ return result ;
536
+ }
537
+ var wrapper = new UserMentionsWrapper ( )
538
+ {
539
+ IsMore = getEventHistory . Result . IsMore ,
540
+ Mentions = new List < UserMentionData > ( )
541
+ } ;
542
+ var mentionEvents = getEventHistory . Result . Events . Where ( x => x . Type == PubnubChatEventType . Mention ) ;
543
+ foreach ( var mentionEvent in mentionEvents )
544
+ {
545
+ var payloadDict =
546
+ PubnubInstance . JsonPluggableLibrary . DeserializeToDictionaryOfObject ( mentionEvent . Payload ) ;
547
+ if ( ! payloadDict . TryGetValue ( "text" , out var mentionText )
548
+ || ! payloadDict . TryGetValue ( "messageTimetoken" , out var messageTimeToken )
549
+ || ! payloadDict . TryGetValue ( "channel" , out var mentionChannel ) )
550
+ {
551
+ continue ;
552
+ }
553
+ var getMessage = await GetMessage ( mentionChannel . ToString ( ) , messageTimeToken . ToString ( ) ) ;
554
+ if ( getMessage . Error )
555
+ {
556
+ Logger . Warn ( $ "Could not find message with ID/Timetoken from mention event. Event payload: { mentionEvent . Payload } ") ;
557
+ continue ;
558
+ }
559
+
560
+ var mention = new UserMentionData ( )
561
+ {
562
+ ChannelId = mentionChannel . ToString ( ) ,
563
+ Event = mentionEvent ,
564
+ Message = getMessage . Result ,
565
+ UserId = mentionEvent . UserId
566
+ } ;
567
+ if ( payloadDict . TryGetValue ( "parentChannel" , out var parentChannelId ) )
568
+ {
569
+ mention . ParentChannelId = parentChannelId . ToString ( ) ;
570
+ }
571
+ wrapper . Mentions . Add ( mention ) ;
572
+ }
573
+ result . Result = wrapper ;
574
+ return result ;
552
575
}
553
576
554
577
/// <summary>
@@ -980,9 +1003,11 @@ public async Task UpdateUser(string userId, ChatUserData updatedData)
980
1003
/// chat.DeleteUser("user_id");
981
1004
/// </code>
982
1005
/// </example>
983
- public async Task DeleteUser ( string userId )
1006
+ public async Task < ChatOperationResult > DeleteUser ( string userId )
984
1007
{
985
- throw new NotImplementedException ( ) ;
1008
+ var result = new ChatOperationResult ( ) ;
1009
+ result . RegisterOperation ( await PubnubInstance . RemoveUuidMetadata ( ) . Uuid ( userId ) . ExecuteAsync ( ) ) ;
1010
+ return result ;
986
1011
}
987
1012
988
1013
#endregion
@@ -1205,22 +1230,32 @@ public async Task<ChatOperationResult<EventsHistoryWrapper>> GetMessageReportsHi
1205
1230
/// <returns>Message object if one was found, null otherwise.</returns>
1206
1231
public async Task < ChatOperationResult < Message > > GetMessage ( string channelId , string messageTimeToken )
1207
1232
{
1208
- throw new NotImplementedException ( ) ;
1209
- /*return await Task.Run(() =>
1233
+ var result = new ChatOperationResult < Message > ( ) ;
1234
+ var startTimeToken = ( long . Parse ( messageTimeToken ) + 1 ) . ToString ( ) ;
1235
+ var getHistory = await GetChannelMessageHistory ( channelId , startTimeToken , messageTimeToken , 1 ) ;
1236
+ if ( result . RegisterOperation ( getHistory ) )
1237
+ {
1238
+ return result ;
1239
+ }
1240
+ if ( ! getHistory . Result . Any ( ) )
1210
1241
{
1211
- var result = TryGetMessage(channelId, messageTimeToken, out var message);
1212
- return result ? message : null;
1213
- });*/
1242
+ result . Error = true ;
1243
+ result . Exception = new PNException ( $ "Didn't find any message with timetoken { messageTimeToken } on channel { channelId } ") ;
1244
+ return result ;
1245
+ }
1246
+ //TODO: wrappers rethink
1247
+ result . Result = getHistory . Result [ 0 ] ;
1248
+ return result ;
1214
1249
}
1215
1250
1216
- public async Task < MarkMessagesAsReadWrapper > MarkAllMessagesAsRead ( string filter = "" , string sort = "" ,
1251
+ public async Task < ChatOperationResult < MarkMessagesAsReadWrapper > > MarkAllMessagesAsRead ( string filter = "" , string sort = "" ,
1217
1252
int limit = 0 ,
1218
1253
PNPageObject page = null )
1219
1254
{
1220
1255
throw new NotImplementedException ( ) ;
1221
1256
}
1222
1257
1223
- public async Task < List < UnreadMessageWrapper > > GetUnreadMessagesCounts ( string filter = "" , string sort = "" ,
1258
+ public async Task < ChatOperationResult < List < UnreadMessageWrapper > > > GetUnreadMessagesCounts ( string filter = "" , string sort = "" ,
1224
1259
int limit = 0 ,
1225
1260
PNPageObject page = null )
1226
1261
{
@@ -1279,9 +1314,16 @@ public async Task<ChatOperationResult<ThreadChannel>> GetThreadChannel(Message m
1279
1314
return result ;
1280
1315
}
1281
1316
1282
- public async Task < ChatOperationResult > ForwardMessage ( Message message , Channel channel )
1317
+ public async Task < ChatOperationResult > ForwardMessage ( string messageTimeToken , string channelId )
1283
1318
{
1284
- throw new NotImplementedException ( ) ;
1319
+ var result = new ChatOperationResult ( ) ;
1320
+ var getMessage = await GetMessage ( channelId , messageTimeToken ) ;
1321
+ if ( result . RegisterOperation ( getMessage ) )
1322
+ {
1323
+ return result ;
1324
+ }
1325
+ result . RegisterOperation ( await getMessage . Result . Forward ( channelId ) ) ;
1326
+ return result ;
1285
1327
}
1286
1328
1287
1329
public async void AddListenerToMessagesUpdate ( string channelId , List < string > messageTimeTokens ,
@@ -1356,16 +1398,18 @@ public async Task<ChatOperationResult<List<Message>>> GetChannelMessageHistory(s
1356
1398
var getHistory = await PubnubInstance . FetchHistory ( ) . Channels ( new [ ] { channelId } )
1357
1399
. Start ( long . Parse ( startTimeToken ) ) . End ( long . Parse ( endTimeToken ) ) . MaximumPerChannel ( count ) . IncludeMessageActions ( true )
1358
1400
. IncludeMeta ( true ) . ExecuteAsync ( ) ;
1359
- if ( result . RegisterOperation ( getHistory ) || ! getHistory . Result . Messages . ContainsKey ( channelId ) )
1401
+ if ( result . RegisterOperation ( getHistory ) || getHistory . Result . Messages == null || ! getHistory . Result . Messages . ContainsKey ( channelId ) )
1360
1402
{
1361
1403
return result ;
1362
1404
}
1363
1405
1406
+ //TODO: should be in "MessageHistoryWrapper" object?
1364
1407
var isMore = getHistory . Result . More != null ;
1365
1408
foreach ( var historyItem in getHistory . Result . Messages [ channelId ] )
1366
1409
{
1367
1410
if ( ChatParsers . TryParseMessageFromHistory ( this , channelId , historyItem , out var message ) )
1368
1411
{
1412
+ //TODO: wrappers rethink
1369
1413
result . Result . Add ( message ) ;
1370
1414
}
1371
1415
}
0 commit comments