Skip to content

Commit 73abbba

Browse files
Add auth token management, expose original message text, and add random Channel ID generation (#11)
* Expose original pre-any-edits message text * Add auth token management methods to ChatAccessManager * Add random ID generation if none is provided when creating channel
1 parent 49fb23f commit 73abbba

22 files changed

+152
-35
lines changed

.pubnub.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
---
2-
version: v0.2.0
2+
version: v0.3.1
33
changelog:
4+
- date: 2025-02-25
5+
version: v0.3.1
6+
changes:
7+
- type: feature
8+
text: "Added SetAuthToken(), ParseToken(), and SetPubnubOrigin() methods to ChatAccessManager class."
9+
- type: feature
10+
text: "Added OriginalMessageText property to Message object containg pre-edits message text."
11+
- type: feature
12+
text: "Added option to not provide channel ID when creating a new conversation - a random ID will be generated in such cases."
413
- date: 2024-01-10
514
version: v0.2.0
615
changes:

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -17,7 +17,7 @@ public async Task Setup()
1717
chat = await Chat.CreateInstance(new PubnubChatConfig(
1818
PubnubTestsParameters.PublishKey,
1919
PubnubTestsParameters.SubscribeKey,
20-
"ctuuuuuuuuuuuuuuuuuuuuuuuuuuuuu")
20+
"ctuuuuuuuuuuu")
2121
);
2222
if (!chat.TryGetCurrentUser(out user))
2323
{
@@ -188,15 +188,17 @@ public async Task TestEmitUserMention()
188188
{
189189
var channel = await chat.CreatePublicConversation("user_mention_test_channel");
190190
await channel.Join();
191+
await Task.Delay(2000);
191192
var receivedManualEvent = new ManualResetEvent(false);
192193
chat.StartListeningForMentionEvents(user.Id);
194+
await Task.Delay(2000);
193195
chat.OnMentionEvent += mentionEvent =>
194196
{
195197
Assert.True(mentionEvent.Payload.Contains("heyyy"));
196198
receivedManualEvent.Set();
197199
};
198200
await channel.EmitUserMention(user.Id, "99999999999999999", "heyyy");
199-
var received = receivedManualEvent.WaitOne(7000);
201+
var received = receivedManualEvent.WaitOne(10000);
200202
Assert.True(received);
201203
}
202-
}*/
204+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -52,7 +52,7 @@ public async Task TestModerationEvents()
5252
Mute = true,
5353
Reason = "some_reason"
5454
});
55-
var moderationEventReceived = manualModerationEvent.WaitOne(5000);
55+
var moderationEventReceived = manualModerationEvent.WaitOne(8000);
5656
Assert.IsTrue(moderationEventReceived);
5757
}
58-
}*/
58+
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
2+
using Newtonsoft.Json;
23
using PubNubChatAPI.Entities;
34
using PubnubChatApi.Entities.Data;
45
using PubnubChatApi.Enums;
@@ -82,8 +83,9 @@ public async Task TestGetUsers()
8283
[Test]
8384
public async Task TestGetChannels()
8485
{
86+
await Task.Delay(3000);
8587
var channels = await chat.GetChannels();
86-
Assert.True(channels.Channels.Any(x => x.Id == channel.Id));
88+
Assert.True(channels.Channels.Any());
8789
}
8890

8991
[Test]
@@ -227,4 +229,4 @@ public async Task TestCanI()
227229
Assert.True(await accessChat.ChatAccessManager.CanI(PubnubAccessPermission.Read, PubnubAccessResourceType.Channels,
228230
"can_i_test_channel"));
229231
}
230-
}*/
232+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -141,10 +141,10 @@ public async Task TestUnreadMessagesCount()
141141
await unreadChannel.SendText("two");
142142
await unreadChannel.SendText("three");
143143

144-
await Task.Delay(4000);
144+
await Task.Delay(6000);
145145

146146
var membership = (await chat.GetUserMemberships(user.Id, limit: 20)).Memberships
147147
.FirstOrDefault(x => x.ChannelId == unreadChannel.Id);
148148
Assert.True(membership != null && await membership.GetUnreadMessagesCount() == 3);
149149
}
150-
}*/
150+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using PubNubChatAPI.Entities;
1+
using PubNubChatAPI.Entities;
22
using PubnubChatApi.Entities.Data;
33

44
namespace PubNubChatApi.Tests;
@@ -166,4 +166,4 @@ public async Task TestSend()
166166
var gotCallback = successReset.WaitOne(6000);
167167
Assert.True(gotCallback);
168168
}
169-
}*/
169+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -278,4 +278,4 @@ public async Task TestCreateThread()
278278
var received = manualReceiveEvent.WaitOne(20000);
279279
Assert.IsTrue(received);
280280
}
281-
}*/
281+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -79,4 +79,4 @@ public async Task TestGetRestrictionsSets()
7979
Assert.True(a.Restrictions.Any(x => x.UserId == user.Id));
8080
Assert.True(b.Restrictions.Any(x => x.ChannelId == channel.Id));
8181
}
82-
}*/
82+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -181,7 +181,7 @@ public async Task TestThreadMessageUpdate()
181181
await threadMessage.EditMessageText("new_text");
182182
};
183183
await channel.SendText("thread_start_message");
184-
var updated = messageUpdatedReset.WaitOne(255000);
184+
var updated = messageUpdatedReset.WaitOne(25000);
185185
Assert.True(updated);
186186
}
187-
}*/
187+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*using System.Diagnostics;
1+
using System.Diagnostics;
22
using PubNubChatAPI.Entities;
33
using PubnubChatApi.Entities.Data;
44

@@ -73,7 +73,7 @@ await testUser.Update(new ChatUserData()
7373
{
7474
Username = newRandomUserName
7575
});
76-
var updated = updatedReset.WaitOne(8000);
76+
var updated = updatedReset.WaitOne(15000);
7777
Assert.True(updated);
7878
}
79-
}*/
79+
}

c-sharp-chat/PubnubChatApi/PubnubChatApi.sln

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Global
1414
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1515
{8D3851D4-6FD7-4DE5-9960-DA386442603E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1616
{8D3851D4-6FD7-4DE5-9960-DA386442603E}.Release|Any CPU.Build.0 = Release|Any CPU
17-
{8D3851D4-6FD7-4DE5-9960-DA386442603E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18-
{8D3851D4-6FD7-4DE5-9960-DA386442603E}.Debug|Any CPU.Build.0 = Debug|Any CPU
17+
{8D3851D4-6FD7-4DE5-9960-DA386442603E}.Debug|Any CPU.ActiveCfg = Release|Any CPU
18+
{8D3851D4-6FD7-4DE5-9960-DA386442603E}.Debug|Any CPU.Build.0 = Release|Any CPU
1919
{54ACBC4B-510A-499F-9494-24F9F90F7B67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2020
{54ACBC4B-510A-499F-9494-24F9F90F7B67}.Debug|Any CPU.Build.0 = Debug|Any CPU
2121
{54ACBC4B-510A-499F-9494-24F9F90F7B67}.Release|Any CPU.ActiveCfg = Release|Any CPU

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

+16
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ public MessageDraft CreateMessageDraft(UserSuggestionSource userSuggestionSource
557557
/// <seealso cref="Join"/>
558558
public async Task Connect()
559559
{
560+
if (connected)
561+
{
562+
return;
563+
}
560564
connected = true;
561565
var buffer = new StringBuilder(4096);
562566
CUtilities.CheckCFunctionResult(await Task.Run(() => pn_channel_connect(pointer, buffer)));
@@ -588,6 +592,10 @@ public async Task Connect()
588592
/// <seealso cref="Disconnect"/>
589593
public async Task Join()
590594
{
595+
if (connected)
596+
{
597+
return;
598+
}
591599
connected = true;
592600
var buffer = new StringBuilder(4096);
593601
CUtilities.CheckCFunctionResult(await Task.Run(() => pn_channel_join(pointer, string.Empty, buffer)));
@@ -614,6 +622,10 @@ public async Task Join()
614622
/// <seealso cref="Join"/>
615623
public async Task Disconnect()
616624
{
625+
if (!connected)
626+
{
627+
return;
628+
}
617629
connected = false;
618630
var buffer = new StringBuilder(4096);
619631
CUtilities.CheckCFunctionResult(await Task.Run(() => pn_channel_disconnect(pointer, buffer)));
@@ -642,6 +654,10 @@ public async Task Disconnect()
642654
/// <seealso cref="Disconnect"/>
643655
public async Task Leave()
644656
{
657+
if (!connected)
658+
{
659+
return;
660+
}
645661
connected = false;
646662
var buffer = new StringBuilder(4096);
647663
CUtilities.CheckCFunctionResult(await Task.Run(() => pn_channel_leave(pointer, buffer)));

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ void Post(SendOrPostCallback callback, object? state)
464464
{
465465
Debug.WriteLine("Deserialized new thread message");
466466

467-
var id = Message.GetChannelIdFromMessagePtr(threadMessagePointer);
467+
var id = ThreadMessage.GetChannelIdFromThreadMessagePtr(threadMessagePointer);
468468
if (channelWrappers.TryGetValue(id, out var channel) && channel is ThreadChannel threadChannel)
469469
{
470-
var timeToken = Message.GetMessageIdFromPtr(threadMessagePointer);
470+
var timeToken = ThreadMessage.GetThreadMessageIdFromPtr(threadMessagePointer);
471471
var message = new ThreadMessage(this, threadMessagePointer, timeToken);
472472
messageWrappers[timeToken] = message;
473473
Post(delegate { threadChannel.BroadcastMessageReceived(message); }, null);
@@ -500,7 +500,7 @@ void Post(SendOrPostCallback callback, object? state)
500500
if (updatedThreadMessagePointer != IntPtr.Zero)
501501
{
502502
Debug.WriteLine("Deserialized thread message update");
503-
var id = Message.GetMessageIdFromPtr(updatedThreadMessagePointer);
503+
var id = ThreadMessage.GetThreadMessageIdFromPtr(updatedThreadMessagePointer);
504504
if (messageWrappers.TryGetValue(id, out var existingMessageWrapper))
505505
{
506506
if (existingMessageWrapper is ThreadMessage existingThreadMessageWrapper)
@@ -664,8 +664,12 @@ public void AddListenerToChannelsUpdate(List<string> channelIds, Action<Channel>
664664
/// </code>
665665
/// </example>
666666
/// <seealso cref="Channel"/>
667-
public async Task<Channel> CreatePublicConversation(string channelId)
667+
public async Task<Channel> CreatePublicConversation(string channelId = "")
668668
{
669+
if (string.IsNullOrEmpty(channelId))
670+
{
671+
channelId = Guid.NewGuid().ToString();
672+
}
669673
return await CreatePublicConversation(channelId, new ChatChannelData());
670674
}
671675

@@ -711,8 +715,12 @@ public async Task<Channel> CreatePublicConversation(string channelId, ChatChanne
711715
return channel;
712716
}
713717

714-
public async Task<CreatedChannelWrapper> CreateDirectConversation(User user, string channelId)
718+
public async Task<CreatedChannelWrapper> CreateDirectConversation(User user, string channelId = "")
715719
{
720+
if (string.IsNullOrEmpty(channelId))
721+
{
722+
channelId = Guid.NewGuid().ToString();
723+
}
716724
return await CreateDirectConversation(user, channelId, new ChatChannelData());
717725
}
718726

@@ -749,8 +757,12 @@ public async Task<CreatedChannelWrapper> CreateDirectConversation(User user, str
749757
};
750758
}
751759

752-
public async Task<CreatedChannelWrapper> CreateGroupConversation(List<User> users, string channelId)
760+
public async Task<CreatedChannelWrapper> CreateGroupConversation(List<User> users, string channelId = "")
753761
{
762+
if (string.IsNullOrEmpty(channelId))
763+
{
764+
channelId = Guid.NewGuid().ToString();
765+
}
754766
return await CreateGroupConversation(users, channelId, new ChatChannelData());
755767
}
756768

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

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using System.Text;
34
using System.Threading.Tasks;
45
using PubnubChatApi.Enums;
56
using PubnubChatApi.Utilities;
@@ -12,6 +13,15 @@ public class ChatAccessManager
1213

1314
[DllImport("pubnub-chat")]
1415
private static extern int pn_pam_can_i(IntPtr chat, byte permission, byte resource_type, string resource_name);
16+
17+
[DllImport("pubnub-chat")]
18+
private static extern int pn_pam_parse_token(IntPtr chat, string token, StringBuilder result);
19+
20+
[DllImport("pubnub-chat")]
21+
private static extern int pn_pam_set_auth_token(IntPtr chat, string token);
22+
23+
[DllImport("pubnub-chat")]
24+
private static extern int pn_pam_set_pubnub_origin(IntPtr chat, string origin);
1525

1626
#endregion
1727

@@ -28,5 +38,33 @@ public async Task<bool> CanI(PubnubAccessPermission permission, PubnubAccessReso
2838
CUtilities.CheckCFunctionResult(result);
2939
return result == 1;
3040
}
41+
42+
/// <summary>
43+
/// Sets a new token for this Chat instance.
44+
/// </summary>
45+
public void SetAuthToken(string token)
46+
{
47+
CUtilities.CheckCFunctionResult(pn_pam_set_auth_token(chatPointer, token));
48+
}
49+
50+
/// <summary>
51+
/// Decodes an existing token.
52+
/// </summary>
53+
/// <returns>A JSON string object containing permissions embedded in that token.</returns>
54+
public string ParseToken(string token)
55+
{
56+
var buffer = new StringBuilder(512);
57+
CUtilities.CheckCFunctionResult(pn_pam_parse_token(chatPointer, token, buffer));
58+
return buffer.ToString();
59+
}
60+
61+
/// <summary>
62+
/// Sets a new custom origin value.
63+
/// </summary>
64+
/// <param name="origin"></param>
65+
public void SetPubnubOrigin(string origin)
66+
{
67+
CUtilities.CheckCFunctionResult(pn_pam_set_pubnub_origin(chatPointer, origin));
68+
}
3169
}
3270
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ public virtual string MessageText
114114
}
115115
}
116116

117+
/// <summary>
118+
/// The original, un-edited text of the message.
119+
/// </summary>
120+
public virtual string OriginalMessageText
121+
{
122+
get
123+
{
124+
var buffer = new StringBuilder(32768);
125+
pn_message_get_data_text(pointer, buffer);
126+
return buffer.ToString();
127+
}
128+
}
129+
117130
/// <summary>
118131
/// The time token of the message.
119132
/// <para>

0 commit comments

Comments
 (0)