// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Teams.Core.Schema;
///
/// Conversation ID helpers for threaded messaging.
///
public static class ConversationExtensions
{
///
/// The thread root portion of the conversation ID, with any ;messageid= suffix stripped.
///
public static string ThreadId(this Conversation conversation)
{
ArgumentNullException.ThrowIfNull(conversation);
string[] parts = conversation.Id.Split(';');
return parts.Length > 1 ? parts[0] : conversation.Id;
}
///
/// Construct a threaded conversation ID by appending ;messageid={messageId}
/// to the conversation ID. This is the format APX uses to route messages
/// to a specific thread in a channel.
///
/// the conversation to thread into (e.g. 19:abc@thread.skype)
/// the thread root message ID (must be a non-zero numeric string)
/// the threaded conversation ID (e.g. 19:abc@thread.skype;messageid=123)
public static string ToThreadedConversationId(string conversationId, string messageId)
{
if (string.IsNullOrEmpty(conversationId))
{
throw new ArgumentException("conversationId must be a non-empty string", nameof(conversationId));
}
if (string.IsNullOrEmpty(messageId) || !ulong.TryParse(messageId, out ulong parsed) || parsed == 0)
{
throw new ArgumentException($"Invalid messageId \"{messageId}\": must be a non-zero numeric value", nameof(messageId));
}
string baseId = conversationId.Split(';')[0];
return $"{baseId};messageid={messageId}";
}
}