microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/Schema/Entities/MentionEntity.Extensions.cs
58lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Core.Schema; |
| 5 | |
| 6 | namespace Microsoft.Teams.Apps.Schema.Entities; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Mention entity extension methods. |
| 10 | /// </summary> |
| 11 | public static class MentionEntityExtensions |
| 12 | { |
| 13 | /// <summary> |
| 14 | /// Gets all mention entities from the activity. |
| 15 | /// </summary> |
| 16 | public static IEnumerable<MentionEntity> GetMentions(this TeamsActivity activity) |
| 17 | { |
| 18 | ArgumentNullException.ThrowIfNull(activity); |
| 19 | if (activity.Entities == null) |
| 20 | { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | return activity.Entities.Where(e => e is MentionEntity).Cast<MentionEntity>(); |
| 25 | } |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Internal helper to add a mention to an activity. |
| 29 | /// </summary> |
| 30 | internal static void AddToActivity(TeamsActivity activity, ConversationAccount account, string? text, bool addText) |
| 31 | { |
| 32 | ArgumentNullException.ThrowIfNull(activity); |
| 33 | ArgumentNullException.ThrowIfNull(account); |
| 34 | |
| 35 | string? mentionText = text ?? account.Name; |
| 36 | |
| 37 | if (addText) |
| 38 | { |
| 39 | string? currentText = activity is MessageActivity message |
| 40 | ? message.Text |
| 41 | : (activity.Properties.TryGetValue("text", out object? value) ? value?.ToString() : null); |
| 42 | |
| 43 | string updatedText = $"<at>{mentionText}</at> {currentText}"; |
| 44 | |
| 45 | if (activity is MessageActivity msg) |
| 46 | { |
| 47 | msg.Text = updatedText; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | activity.Properties["text"] = updatedText; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | activity.Entities ??= []; |
| 56 | activity.Entities.Add(new MentionEntity(account, $"<at>{mentionText}</at>")); |
| 57 | } |
| 58 | } |