Skip to content

Commit dd74100

Browse files
implement message pinning and unpinning
1 parent 39dbe78 commit dd74100

File tree

7 files changed

+69
-42
lines changed

7 files changed

+69
-42
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ public async Task TestPinMessage()
232232

233233
await Task.Delay(2000);
234234

235-
Assert.True(channel.TryGetPinnedMessage(out var pinnedMessage) && pinnedMessage.MessageText == "message to pin");
235+
var pinned = TestUtils.AssertOperation(await channel.GetPinnedMessage());
236+
Assert.True(pinned.MessageText == "message to pin");
236237
receivedManualEvent.Set();
237238
};
238239
await channel.SendText("message to pin");
@@ -253,18 +254,20 @@ public async Task TestUnPinMessage()
253254
TestUtils.AssertOperation(await channel.PinMessage(message));
254255

255256
await Task.Delay(2000);
256-
257-
Assert.True(channel.TryGetPinnedMessage(out var pinnedMessage) && pinnedMessage.MessageText == "message to pin");
257+
258+
var pinned = TestUtils.AssertOperation(await channel.GetPinnedMessage());
259+
Assert.True(pinned.MessageText == "message to pin");
258260
TestUtils.AssertOperation(await channel.UnpinMessage());
259261

260-
await Task.Delay(2000);
261-
262-
Assert.False(channel.TryGetPinnedMessage(out _));
262+
await Task.Delay(15000);
263+
264+
var getPinned = await channel.GetPinnedMessage();
265+
Assert.True(getPinned.Error);
263266
receivedManualEvent.Set();
264267
};
265268
await channel.SendText("message to pin");
266269

267-
var received = receivedManualEvent.WaitOne(12000);
270+
var received = receivedManualEvent.WaitOne(35000);
268271
Assert.IsTrue(received);
269272
}
270273

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ public async Task TestPinMessage()
222222

223223
await Task.Delay(3000);
224224

225-
var got = pinTestChannel.TryGetPinnedMessage(out var pinnedMessage);
226-
Assert.True(got && pinnedMessage.MessageText == "message to pin");
225+
var pinnedMessage = TestUtils.AssertOperation(await pinTestChannel.GetPinnedMessage());
226+
Assert.True(pinnedMessage.MessageText == "message to pin");
227227
manualReceivedEvent.Set();
228228
};
229229
await pinTestChannel.SendText("message to pin");

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public async Task TestThreadChannelParentChannelPinning()
8282
await thread.PinMessageToParentChannel(threadMessage);
8383

8484
await Task.Delay(7000);
85-
86-
var hasPinned = channel.TryGetPinnedMessage(out var pinnedMessage);
87-
var correctText = hasPinned && pinnedMessage.MessageText == "thread init message";
88-
Assert.True(hasPinned && correctText);
85+
86+
var pinned = TestUtils.AssertOperation(await channel.GetPinnedMessage());
87+
Assert.True(pinned.MessageText == "thread init message");
8988
await thread.UnPinMessageFromParentChannel();
9089

9190
await Task.Delay(7000);
9291

93-
Assert.False(channel.TryGetPinnedMessage(out _));
92+
var getPinned = await channel.GetPinnedMessage();
93+
Assert.True(getPinned.Error);
9494
historyReadReset.Set();
9595
};
9696
await channel.SendText("thread_start_message");
@@ -145,13 +145,15 @@ public async Task TestThreadMessageParentChannelPinning()
145145

146146
await Task.Delay(5000);
147147

148-
Assert.True(channel.TryGetPinnedMessage(out var pinnedMessage) && pinnedMessage.MessageText == threadMessage.MessageText);
148+
var pinned = TestUtils.AssertOperation(await channel.GetPinnedMessage());
149+
Assert.True(pinned.MessageText == threadMessage.MessageText);
149150

150151
await threadMessage.UnPinMessageFromParentChannel();
151152

152153
await Task.Delay(5000);
153154

154-
Assert.False(channel.TryGetPinnedMessage(out _));
155+
var getPinned = await channel.GetPinnedMessage();
156+
Assert.True(getPinned.Error);
155157
historyReadReset.Set();
156158
};
157159
await channel.SendText("thread_start_message");

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Channel : UniqueChatEntity
4545
/// The custom data that can be used to store additional information about the channel.
4646
/// </para>
4747
/// </summary>
48-
public Dictionary<string, object> CustomData => channelData.CustomData;
48+
public Dictionary<string, object> CustomData => channelData.CustomData ?? new ();
4949

5050
/// <summary>
5151
/// The information about the last update of the channel.
@@ -191,7 +191,7 @@ internal static async Task<PNResult<PNSetChannelMetadataResult>> UpdateChannelDa
191191
{
192192
operation = operation.Status(data.Status);
193193
}
194-
if (data.CustomData != null && data.CustomData.Any())
194+
if (data.CustomData != null)
195195
{
196196
operation = operation.Custom(data.CustomData);
197197
}
@@ -373,32 +373,48 @@ public async Task<ChatOperationResult> StopTyping()
373373

374374
public async Task<ChatOperationResult> PinMessage(Message message)
375375
{
376-
throw new NotImplementedException();
376+
channelData.CustomData ??= new ();
377+
channelData.CustomData["pinnedMessageChannelID"] = message.ChannelId;
378+
channelData.CustomData["pinnedMessageTimetoken"] = message.TimeToken;
379+
return (await UpdateChannelData(chat, Id, channelData)).ToChatOperationResult();
377380
}
378381

379382
public async Task<ChatOperationResult> UnpinMessage()
380383
{
381-
throw new NotImplementedException();
384+
channelData.CustomData ??= new ();
385+
channelData.CustomData.Remove("pinnedMessageChannelID");
386+
channelData.CustomData.Remove("pinnedMessageTimetoken");
387+
return (await UpdateChannelData(chat, Id, channelData)).ToChatOperationResult();
382388
}
383389

384-
/// <summary>
385-
/// Tries to get the <c>Message</c> pinned to this <c>Channel</c>.
386-
/// </summary>
387-
/// <param name="pinnedMessage">The pinned Message object, null if there wasn't one.</param>
388-
/// <returns>True of a pinned Message was found, false otherwise.</returns>
389-
/// <seealso cref="GetPinnedMessageAsync"/>
390-
public bool TryGetPinnedMessage(out Message pinnedMessage)
391-
{
392-
throw new NotImplementedException();
393-
}
394390

395391
/// <summary>
396392
/// Asynchronously tries to get the <c>Message</c> pinned to this <c>Channel</c>.
397393
/// </summary>
398394
/// <returns>The pinned Message object if there was one, null otherwise.</returns>
399395
public async Task<ChatOperationResult<Message>> GetPinnedMessage()
400396
{
401-
throw new NotImplementedException();
397+
var result = new ChatOperationResult<Message>();
398+
if (result.RegisterOperation(await Refresh()))
399+
{
400+
return result;
401+
}
402+
if(!CustomData.TryGetValue("pinnedMessageChannelID", out var pinnedChannelId)
403+
|| !CustomData.TryGetValue("pinnedMessageTimetoken", out var pinnedMessageTimeToken))
404+
{
405+
result.Error = true;
406+
result.Exception = new PNException($"Channel \"{Id}\" doesn't have a pinned message.");
407+
return result;
408+
}
409+
410+
var getMessage = await chat.GetMessage(pinnedChannelId.ToString(), pinnedMessageTimeToken.ToString());
411+
if (result.RegisterOperation(getMessage))
412+
{
413+
return result;
414+
}
415+
416+
result.Result = getMessage.Result;
417+
return result;
402418
}
403419

404420
/// <summary>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,16 @@ public async Task<ChatOperationResult> RemoveThread()
338338
return result;
339339
}
340340

341-
public async Task Pin()
341+
public async Task<ChatOperationResult> Pin()
342342
{
343-
throw new NotImplementedException();
343+
var result = new ChatOperationResult();
344+
var getChannel = await chat.GetChannel(ChannelId);
345+
if (result.RegisterOperation(getChannel))
346+
{
347+
return result;
348+
}
349+
result.RegisterOperation(await getChannel.Result.PinMessage(this));
350+
return result;
344351
}
345352

346353
public async Task<ChatOperationResult> Report(string reason)

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using PubnubApi;
@@ -87,14 +86,14 @@ public override async Task<ChatOperationResult> EmitUserMention(string userId, s
8786
chat.PubnubInstance.JsonPluggableLibrary.SerializeToJsonString(jsonDict));
8887
}
8988

90-
public async Task PinMessageToParentChannel(ThreadMessage message)
89+
public async Task<ChatOperationResult> PinMessageToParentChannel(ThreadMessage message)
9190
{
92-
throw new NotImplementedException();
91+
return await chat.PinMessageToChannel(ParentChannelId, message);
9392
}
9493

95-
public async Task UnPinMessageFromParentChannel()
94+
public async Task<ChatOperationResult> UnPinMessageFromParentChannel()
9695
{
97-
throw new NotImplementedException();
96+
return await chat.UnpinMessageFromChannel(ParentChannelId);
9897
}
9998
}
10099
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ protected override SubscribeCallback CreateUpdateListener()
3434
});
3535
}
3636

37-
public async Task PinMessageToParentChannel()
37+
public async Task<ChatOperationResult> PinMessageToParentChannel()
3838
{
39-
throw new NotImplementedException();
39+
return await chat.PinMessageToChannel(ParentChannelId, this);
4040
}
4141

42-
public async Task UnPinMessageFromParentChannel()
42+
public async Task<ChatOperationResult> UnPinMessageFromParentChannel()
4343
{
44-
throw new NotImplementedException();
44+
return await chat.UnpinMessageFromChannel(ParentChannelId);
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)