microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Core/Schema/ConversationExtensions.cs
44lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Teams.Core.Schema; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Conversation ID helpers for threaded messaging. |
| 8 | /// </summary> |
| 9 | public static class ConversationExtensions |
| 10 | { |
| 11 | /// <summary> |
| 12 | /// The thread root portion of the conversation ID, with any <c>;messageid=</c> suffix stripped. |
| 13 | /// </summary> |
| 14 | public static string ThreadId(this Conversation conversation) |
| 15 | { |
| 16 | ArgumentNullException.ThrowIfNull(conversation); |
| 17 | string[] parts = conversation.Id.Split(';'); |
| 18 | return parts.Length > 1 ? parts[0] : conversation.Id; |
| 19 | } |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Construct a threaded conversation ID by appending <c>;messageid={messageId}</c> |
| 23 | /// to the conversation ID. This is the format APX uses to route messages |
| 24 | /// to a specific thread in a channel. |
| 25 | /// </summary> |
| 26 | /// <param name="conversationId">the conversation to thread into (e.g. <c>19:abc@thread.skype</c>)</param> |
| 27 | /// <param name="messageId">the thread root message ID (must be a non-zero numeric string)</param> |
| 28 | /// <returns>the threaded conversation ID (e.g. <c>19:abc@thread.skype;messageid=123</c>)</returns> |
| 29 | public static string ToThreadedConversationId(string conversationId, string messageId) |
| 30 | { |
| 31 | if (string.IsNullOrEmpty(conversationId)) |
| 32 | { |
| 33 | throw new ArgumentException("conversationId must be a non-empty string", nameof(conversationId)); |
| 34 | } |
| 35 | |
| 36 | if (string.IsNullOrEmpty(messageId) || !ulong.TryParse(messageId, out ulong parsed) || parsed == 0) |
| 37 | { |
| 38 | throw new ArgumentException($"Invalid messageId \"{messageId}\": must be a non-zero numeric value", nameof(messageId)); |
| 39 | } |
| 40 | |
| 41 | string baseId = conversationId.Split(';')[0]; |
| 42 | return $"{baseId};messageid={messageId}"; |
| 43 | } |
| 44 | } |
| 45 | |