Skip to content

Commit 9716a96

Browse files
Add User.Active + related functionality (#5)
1 parent 8ca3440 commit 9716a96

File tree

17 files changed

+75
-32
lines changed

17 files changed

+75
-32
lines changed

c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/UserTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public void Setup()
1616
chat = new Chat(new PubnubChatConfig(
1717
PubnubTestsParameters.PublishKey,
1818
PubnubTestsParameters.SubscribeKey,
19-
"user_tests_user")
19+
"user_tests_user",
20+
storeUserActivityTimestamp: true)
2021
);
2122
channel = chat.CreatePublicConversation("user_tests_channel");
2223
if (!chat.TryGetCurrentUser(out user))
@@ -26,6 +27,13 @@ public void Setup()
2627
channel.Join();
2728
}
2829

30+
[Test]
31+
public async Task TestUserActive()
32+
{
33+
await Task.Delay(500);
34+
Assert.True(user.Active);
35+
}
36+
2937
[Test]
3038
public async Task TestUserUpdate()
3139
{

c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Channel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ internal bool TryParseAndBroadcastTypingEvent(ChatEvent chatEvent)
417417
}
418418

419419
//Create and start new timer
420-
//TODO: Get this from config
421-
var newTimer = new Timer(5000);
420+
var newTimer = new Timer(chat.Config.TypingTimeout);
422421
newTimer.Elapsed += (_, _) =>
423422
{
424423
typingIndicators.Remove(chatEvent.UserId);

c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Chat.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ private static extern IntPtr pn_chat_new(
3434
string user_id,
3535
string auth_key,
3636
int typing_timeout,
37-
int typing_timeout_difference);
37+
int typing_timeout_difference,
38+
int store_user_activity_interval,
39+
bool store_user_activity_timestamps);
3840

3941
[DllImport("pubnub-chat")]
4042
private static extern void pn_chat_delete(IntPtr chat);
@@ -315,6 +317,7 @@ private static extern int pn_chat_get_current_user_mentions(IntPtr chat, string
315317
public event Action<ChatEvent> OnAnyEvent;
316318

317319
public ChatAccessManager ChatAccessManager { get; }
320+
public PubnubChatConfig Config { get; }
318321

319322
/// <summary>
320323
/// Initializes a new instance of the <see cref="Chat"/> class.
@@ -329,9 +332,11 @@ private static extern int pn_chat_get_current_user_mentions(IntPtr chat, string
329332
public Chat(PubnubChatConfig config)
330333
{
331334
chatPointer = pn_chat_new(config.PublishKey, config.SubscribeKey, config.UserId, config.AuthKey,
332-
config.TypingTimeout, config.TypingTimeoutDifference);
335+
config.TypingTimeout, config.TypingTimeoutDifference, config.StoreUserActivityInterval,
336+
config.StoreUserActivityTimestamp);
333337
CUtilities.CheckCFunctionResult(chatPointer);
334338

339+
Config = config;
335340
ChatAccessManager = new ChatAccessManager(chatPointer);
336341

337342
fetchUpdatesThread = new Thread(FetchUpdatesLoop) { IsBackground = true };
@@ -392,7 +397,6 @@ internal void ParseJsonUpdatePointers(string jsonPointers)
392397
{
393398
failedToInvoke = true;
394399
}
395-
396400
break;
397401
case PubnubChatEventType.Report:
398402
OnReportEvent?.Invoke(chatEvent);
@@ -403,7 +407,6 @@ internal void ParseJsonUpdatePointers(string jsonPointers)
403407
{
404408
readReceiptChannel.BroadcastReadReceipt(chatEvent);
405409
}
406-
407410
break;
408411
case PubnubChatEventType.Mention:
409412
OnMentionEvent?.Invoke(chatEvent);
@@ -445,10 +448,6 @@ internal void ParseJsonUpdatePointers(string jsonPointers)
445448
messageWrappers[timeToken] = message;
446449
channel.BroadcastMessageReceived(message);
447450
}
448-
else
449-
{
450-
Debug.WriteLine("BBBBBBBBBBB");
451-
}
452451

453452
pn_dispose_message(pointer);
454453
continue;
@@ -481,10 +480,8 @@ internal void ParseJsonUpdatePointers(string jsonPointers)
481480
var id = Message.GetMessageIdFromPtr(updatedThreadMessagePointer);
482481
if (messageWrappers.TryGetValue(id, out var existingMessageWrapper))
483482
{
484-
Debug.WriteLine("KURWA");
485483
if (existingMessageWrapper is ThreadMessage existingThreadMessageWrapper)
486484
{
487-
Debug.WriteLine("MAĆ");
488485
existingThreadMessageWrapper.UpdateWithPartialPtr(updatedThreadMessagePointer);
489486
existingThreadMessageWrapper.BroadcastMessageUpdate();
490487
}
@@ -494,10 +491,6 @@ internal void ParseJsonUpdatePointers(string jsonPointers)
494491
"Thread message was stored as a regular message - SHOULD NEVER HAPPEN!");
495492
}
496493
}
497-
else
498-
{
499-
Debug.WriteLine("CHUJ");
500-
}
501494

502495
pn_dispose_message(pointer);
503496
continue;
@@ -844,6 +837,16 @@ internal bool TryGetChannel(string channelId, IntPtr channelPointer, out Channel
844837
() => new Channel(this, channelId, channelPointer), out channel);
845838
}
846839

840+
//The TryGetChannel updates the pointer, these methods are for internal logic explicity sake
841+
internal void UpdateChannelPointer(IntPtr newPointer)
842+
{
843+
TryGetChannel(newPointer, out _);
844+
}
845+
internal void UpdateChannelPointer(string id, IntPtr newPointer)
846+
{
847+
TryGetChannel(id, newPointer, out _);
848+
}
849+
847850
public ChannelsResponseWrapper GetChannels(string filter = "", string sort = "", int limit = 0,
848851
Page page = null)
849852
{

c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/ThreadChannel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,14 @@ public void PinMessageToParentChannel(Message message)
129129
{
130130
var newChannelPointer = pn_thread_channel_pin_message_to_parent_channel(pointer, message.Pointer);
131131
CUtilities.CheckCFunctionResult(newChannelPointer);
132-
//TODO: this is to update the pointer of the existing wrapper, but isn't very explicit about the fact it does that
133-
chat.TryGetChannel(ParentChannelId, newChannelPointer, out _);
132+
chat.UpdateChannelPointer(ParentChannelId, newChannelPointer);
134133
}
135134

136135
public void UnPinMessageFromParentChannel()
137136
{
138137
var newChannelPointer = pn_thread_channel_unpin_message_from_parent_channel(pointer);
139138
CUtilities.CheckCFunctionResult(newChannelPointer);
140-
//TODO: this is to update the pointer of the existing wrapper, but isn't very explicit about the fact it does that
141-
chat.TryGetChannel(ParentChannelId, newChannelPointer, out _);
139+
chat.UpdateChannelPointer(ParentChannelId, newChannelPointer);
142140
}
143141

144142
protected override void DisposePointer()

c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/ThreadMessage.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,14 @@ public void PinMessageToParentChannel()
7474
{
7575
var newChannelPointer = pn_thread_message_pin_to_parent_channel(pointer);
7676
CUtilities.CheckCFunctionResult(newChannelPointer);
77-
//TODO: this is to update the pointer of the existing wrapper, but isn't very explicit about the fact it does that
78-
chat.TryGetChannel(newChannelPointer, out _);
77+
chat.UpdateChannelPointer(newChannelPointer);
7978
}
8079

8180
public void UnPinMessageFromParentChannel()
8281
{
8382
var newChannelPointer = pn_thread_message_unpin_from_parent_channel(pointer);
8483
CUtilities.CheckCFunctionResult(newChannelPointer);
85-
//TODO: this is to update the pointer of the existing wrapper, but isn't very explicit about the fact it does that
86-
chat.TryGetChannel(newChannelPointer, out _);
84+
chat.UpdateChannelPointer(newChannelPointer);
8785
}
8886

8987
protected override void DisposePointer()

c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/User.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ private static extern int pn_user_get_channel_restrictions(
6464
[DllImport("pubnub-chat")]
6565
private static extern int pn_user_get_channels_restrictions(IntPtr user, string sort, int limit, string next,
6666
string prev, StringBuilder result);
67+
68+
[DllImport("pubnub-chat")]
69+
private static extern int pn_user_active(IntPtr user);
70+
[DllImport("pubnub-chat")]
71+
private static extern int pn_user_last_active_timestamp(IntPtr user, StringBuilder result);
6772

6873
#endregion
6974

@@ -179,11 +184,13 @@ public string DataType
179184
}
180185
}
181186

182-
/*public bool Active
187+
public bool Active
183188
{
184189
get
185190
{
186-
throw new NotImplementedException();
191+
var result = pn_user_active(pointer);
192+
CUtilities.CheckCFunctionResult(result);
193+
return result == 1;
187194
}
188195
}
189196

@@ -192,9 +199,10 @@ public string LastActiveTimeStamp
192199
get
193200
{
194201
var buffer = new StringBuilder(64);
195-
throw new NotImplementedException();
202+
CUtilities.CheckCFunctionResult(pn_user_last_active_timestamp(pointer, buffer));
203+
return buffer.ToString();
196204
}
197-
}*/
205+
}
198206

199207
private Chat chat;
200208

25.5 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)