microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/Schema/Entities/MentionEntity.cs
71lines · 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.Core.Schema; |
| 8 | |
| 9 | namespace Microsoft.Teams.Apps.Schema.Entities; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Mention entity. |
| 13 | /// </summary> |
| 14 | public class MentionEntity : Entity |
| 15 | { |
| 16 | /// <summary> |
| 17 | /// Creates a new instance of <see cref="MentionEntity"/>. |
| 18 | /// </summary> |
| 19 | public MentionEntity() : base("mention") { } |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Creates a new instance of <see cref="MentionEntity"/> with the specified mentioned account and text. |
| 23 | /// </summary> |
| 24 | /// <param name="mentioned">The conversation account being mentioned.</param> |
| 25 | /// <param name="text">The text representation of the mention, typically formatted as "<at>name</at>".</param> |
| 26 | public MentionEntity(ConversationAccount mentioned, string? text) : base("mention") |
| 27 | { |
| 28 | Mentioned = mentioned; |
| 29 | Text = text; |
| 30 | } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Mentioned conversation account. |
| 34 | /// </summary> |
| 35 | [JsonPropertyName("mentioned")] |
| 36 | public ConversationAccount? Mentioned |
| 37 | { |
| 38 | get => base.Properties.Get<ConversationAccount>("mentioned"); |
| 39 | set => base.Properties["mentioned"] = value; |
| 40 | } |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Text of the mention. |
| 44 | /// </summary> |
| 45 | [JsonPropertyName("text")] |
| 46 | public string? Text |
| 47 | { |
| 48 | get => base.Properties.TryGetValue("text", out object? value) ? value?.ToString() : null; |
| 49 | set => base.Properties["text"] = value; |
| 50 | } |
| 51 | |
| 52 | /// <summary> |
| 53 | /// Creates a new instance of the MentionEntity class from the specified JSON node. |
| 54 | /// </summary> |
| 55 | /// <param name="jsonNode">A JsonNode containing the data to deserialize. Must include a 'mentioned' property representing a |
| 56 | /// ConversationAccount.</param> |
| 57 | /// <returns>A MentionEntity object populated with values from the provided JSON node.</returns> |
| 58 | /// <exception cref="ArgumentNullException">Thrown if jsonNode is null or does not contain the required 'mentioned' property.</exception> |
| 59 | public static MentionEntity FromJsonElement(JsonNode? jsonNode) |
| 60 | { |
| 61 | MentionEntity res = new() |
| 62 | { |
| 63 | // TODO: Verify if throwing exceptions is okay here |
| 64 | Mentioned = jsonNode?["mentioned"] != null |
| 65 | ? JsonSerializer.Deserialize<ConversationAccount>(jsonNode["mentioned"]!.ToJsonString())! |
| 66 | : throw new ArgumentNullException(nameof(jsonNode), "mentioned property is required"), |
| 67 | Text = jsonNode?["text"]?.GetValue<string>() |
| 68 | }; |
| 69 | return res; |
| 70 | } |
| 71 | } |
| 72 | |