microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/Schema/Entities/CitationEntity.Extensions.cs
53lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Teams.Apps.Schema.Entities; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Citation entity extension methods. |
| 8 | /// </summary> |
| 9 | public static class CitationEntityExtensions |
| 10 | { |
| 11 | /// <summary> |
| 12 | /// Gets the first citation entity from the activity. |
| 13 | /// </summary> |
| 14 | public static CitationEntity? GetCitation(this TeamsActivity activity) |
| 15 | { |
| 16 | ArgumentNullException.ThrowIfNull(activity); |
| 17 | if (activity.Entities == null) |
| 18 | { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | return activity.Entities.FirstOrDefault(e => e is CitationEntity) as CitationEntity; |
| 23 | } |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Internal helper to add a citation claim to an activity. |
| 27 | /// </summary> |
| 28 | internal static CitationEntity AddToActivity(TeamsActivity activity, int position, CitationAppearance appearance) |
| 29 | { |
| 30 | ArgumentNullException.ThrowIfNull(activity); |
| 31 | ArgumentNullException.ThrowIfNull(appearance); |
| 32 | |
| 33 | activity.Entities ??= []; |
| 34 | |
| 35 | // Get or create the root message entity |
| 36 | OMessageEntity existingMessageEntity = OMessageEntityExtensions.GetOrCreateRootMessageEntity(activity); |
| 37 | |
| 38 | // Remove existing message entity to replace with citation entity |
| 39 | activity.Entities.Remove(existingMessageEntity); |
| 40 | |
| 41 | // Create citation entity from message entity |
| 42 | CitationEntity citationEntity = new(existingMessageEntity); |
| 43 | citationEntity.Citation ??= []; |
| 44 | citationEntity.Citation.Add(new CitationClaim() |
| 45 | { |
| 46 | Position = position, |
| 47 | Appearance = appearance.ToDocument() |
| 48 | }); |
| 49 | |
| 50 | activity.Entities.Add(citationEntity); |
| 51 | return citationEntity; |
| 52 | } |
| 53 | } |
| 54 | |