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