microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Schema/Entities/MentionEntity.cs
116lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using System.Text.Json.Nodes; |
| 6 | using System.Text.Json.Serialization; |
| 7 | using Microsoft.Teams.Bot.Core.Schema; |
| 8 | |
| 9 | namespace Microsoft.Teams.Bot.Apps.Schema.Entities; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Extension methods for Activity to handle mentions. |
| 13 | /// </summary> |
| 14 | public static class ActivityMentionExtensions |
| 15 | { |
| 16 | /// <summary> |
| 17 | /// Gets the MentionEntity from the activity's entities. |
| 18 | /// </summary> |
| 19 | /// <param name="activity">The activity to extract the mention from.</param> |
| 20 | /// <returns>The MentionEntity if found; otherwise, null.</returns> |
| 21 | public static IEnumerable<MentionEntity> GetMentions(this TeamsActivity activity) |
| 22 | { |
| 23 | ArgumentNullException.ThrowIfNull(activity); |
| 24 | if (activity.Entities == null) |
| 25 | { |
| 26 | return []; |
| 27 | } |
| 28 | return activity.Entities.Where(e => e is MentionEntity).Cast<MentionEntity>(); |
| 29 | } |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Adds a mention (@ mention) of a user or bot to the activity. |
| 33 | /// </summary> |
| 34 | /// <param name="activity">The activity to add the mention to. Cannot be null.</param> |
| 35 | /// <param name="account">The conversation account being mentioned. Cannot be null.</param> |
| 36 | /// <param name="text">Optional custom text for the mention. If null, uses the account name.</param> |
| 37 | /// <param name="addText">If true, prepends the mention text to the activity's existing text content. Defaults to true.</param> |
| 38 | /// <returns>The created MentionEntity that was added to the activity.</returns> |
| 39 | public static MentionEntity AddMention(this TeamsActivity activity, ConversationAccount account, string? text = null, bool addText = true) |
| 40 | { |
| 41 | ArgumentNullException.ThrowIfNull(activity); |
| 42 | ArgumentNullException.ThrowIfNull(account); |
| 43 | string? mentionText = text ?? account.Name; |
| 44 | if (addText && activity is MessageActivity msg) |
| 45 | { |
| 46 | msg.Text = $"<at>{mentionText}</at> {msg.Text}"; |
| 47 | } |
| 48 | activity.Entities ??= []; |
| 49 | MentionEntity mentionEntity = new(account, $"<at>{mentionText}</at>"); |
| 50 | activity.Entities.Add(mentionEntity); |
| 51 | activity.Rebase(); |
| 52 | return mentionEntity; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// <summary> |
| 57 | /// Mention entity. |
| 58 | /// </summary> |
| 59 | public class MentionEntity : Entity |
| 60 | { |
| 61 | /// <summary> |
| 62 | /// Creates a new instance of <see cref="MentionEntity"/>. |
| 63 | /// </summary> |
| 64 | public MentionEntity() : base("mention") { } |
| 65 | |
| 66 | /// <summary> |
| 67 | /// Creates a new instance of <see cref="MentionEntity"/> with the specified mentioned account and text. |
| 68 | /// </summary> |
| 69 | /// <param name="mentioned">The conversation account being mentioned.</param> |
| 70 | /// <param name="text">The text representation of the mention, typically formatted as "<at>name</at>".</param> |
| 71 | public MentionEntity(ConversationAccount mentioned, string? text) : base("mention") |
| 72 | { |
| 73 | Mentioned = mentioned; |
| 74 | Text = text; |
| 75 | } |
| 76 | |
| 77 | /// <summary> |
| 78 | /// Mentioned conversation account. |
| 79 | /// </summary> |
| 80 | [JsonPropertyName("mentioned")] |
| 81 | public ConversationAccount? Mentioned |
| 82 | { |
| 83 | get => base.Properties.TryGetValue("mentioned", out object? value) ? value as ConversationAccount : null; |
| 84 | set => base.Properties["mentioned"] = value; |
| 85 | } |
| 86 | |
| 87 | /// <summary> |
| 88 | /// Text of the mention. |
| 89 | /// </summary> |
| 90 | [JsonPropertyName("text")] |
| 91 | public string? Text |
| 92 | { |
| 93 | get => base.Properties.TryGetValue("text", out object? value) ? value?.ToString() : null; |
| 94 | set => base.Properties["text"] = value; |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
| 98 | /// Creates a new instance of the MentionEntity class from the specified JSON node. |
| 99 | /// </summary> |
| 100 | /// <param name="jsonNode">A JsonNode containing the data to deserialize. Must include a 'mentioned' property representing a |
| 101 | /// ConversationAccount.</param> |
| 102 | /// <returns>A MentionEntity object populated with values from the provided JSON node.</returns> |
| 103 | /// <exception cref="ArgumentNullException">Thrown if jsonNode is null or does not contain the required 'mentioned' property.</exception> |
| 104 | public static MentionEntity FromJsonElement(JsonNode? jsonNode) |
| 105 | { |
| 106 | MentionEntity res = new() |
| 107 | { |
| 108 | // TODO: Verify if throwing exceptions is okay here |
| 109 | Mentioned = jsonNode?["mentioned"] != null |
| 110 | ? JsonSerializer.Deserialize<ConversationAccount>(jsonNode["mentioned"]!.ToJsonString())! |
| 111 | : throw new ArgumentNullException(nameof(jsonNode), "mentioned property is required"), |
| 112 | Text = jsonNode?["text"]?.GetValue<string>() |
| 113 | }; |
| 114 | return res; |
| 115 | } |
| 116 | } |
| 117 | |