microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/Handlers/EventHandler.Activity.cs
126lines · 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.Apps.Schema; |
| 8 | using Microsoft.Teams.Core.Schema; |
| 9 | |
| 10 | namespace Microsoft.Teams.Apps.Handlers; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Represents an event activity. |
| 14 | /// </summary> |
| 15 | public class EventActivity : TeamsActivity |
| 16 | { |
| 17 | /// <summary> |
| 18 | /// Creates an EventActivity from a CoreActivity. |
| 19 | /// </summary> |
| 20 | public static new EventActivity FromActivity(CoreActivity activity) |
| 21 | { |
| 22 | ArgumentNullException.ThrowIfNull(activity); |
| 23 | return new EventActivity(activity); |
| 24 | } |
| 25 | |
| 26 | /// <summary> |
| 27 | /// Gets or sets the name of the event. See <see cref="EventNames"/> for common values. |
| 28 | /// </summary> |
| 29 | [JsonPropertyName("name")] |
| 30 | public string? Name { get; set; } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Gets or sets the value payload of the event activity. |
| 34 | /// </summary> |
| 35 | [JsonPropertyName("value")] |
| 36 | public JsonNode? Value { get; set; } |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Initializes a new instance of the <see cref="EventActivity"/> class. |
| 40 | /// </summary> |
| 41 | [JsonConstructor] |
| 42 | public EventActivity() : base(TeamsActivityTypes.Event) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | /// <summary> |
| 47 | /// Initializes a new instance of the <see cref="EventActivity"/> class with the specified name. |
| 48 | /// </summary> |
| 49 | public EventActivity(string name) : base(TeamsActivityTypes.Event) |
| 50 | { |
| 51 | Name = name; |
| 52 | } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Initializes a new instance of the <see cref="EventActivity"/> class from a CoreActivity. |
| 56 | /// </summary> |
| 57 | protected EventActivity(CoreActivity activity) : base(activity) |
| 58 | { |
| 59 | Name = activity.Properties.Extract<string>("name"); |
| 60 | Value = activity is EventActivity evt |
| 61 | ? evt.Value |
| 62 | : activity.Properties.Extract<JsonNode>("value"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// <summary> |
| 67 | /// Represents an event activity with a strongly-typed value. |
| 68 | /// </summary> |
| 69 | /// <typeparam name="TValue">The type of the value payload.</typeparam> |
| 70 | public class EventActivity<TValue> : EventActivity |
| 71 | { |
| 72 | /// <summary> |
| 73 | /// Gets or sets the strongly-typed value associated with the event activity. |
| 74 | /// Shadows the base class Value property, deserializing from the underlying JsonNode on access. |
| 75 | /// </summary> |
| 76 | public new TValue? Value |
| 77 | { |
| 78 | get => base.Value != null ? JsonSerializer.Deserialize<TValue>(base.Value.ToJsonString()) : default; |
| 79 | set => base.Value = value != null ? JsonSerializer.SerializeToNode(value) : null; |
| 80 | } |
| 81 | |
| 82 | /// <summary> |
| 83 | /// Initializes a new instance of the <see cref="EventActivity{TValue}"/> class. |
| 84 | /// </summary> |
| 85 | public EventActivity() : base() |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Initializes a new instance of the <see cref="EventActivity{TValue}"/> class with the specified name. |
| 91 | /// </summary> |
| 92 | public EventActivity(string name) : base(name) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | /// <summary> |
| 97 | /// Initializes a new instance of the <see cref="EventActivity{TValue}"/> class from an EventActivity. |
| 98 | /// </summary> |
| 99 | public EventActivity(EventActivity activity) : base(activity) |
| 100 | { |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// <summary> |
| 105 | /// String constants for event activity names. |
| 106 | /// </summary> |
| 107 | public static class EventNames |
| 108 | { |
| 109 | /// <summary>Meeting start event name.</summary> |
| 110 | public const string MeetingStart = "application/vnd.microsoft.meetingStart"; |
| 111 | |
| 112 | /// <summary>Meeting end event name.</summary> |
| 113 | public const string MeetingEnd = "application/vnd.microsoft.meetingEnd"; |
| 114 | |
| 115 | /// <summary>Meeting participant join event name.</summary> |
| 116 | public const string MeetingParticipantJoin = "application/vnd.microsoft.meetingParticipantJoin"; |
| 117 | |
| 118 | /// <summary>Meeting participant leave event name.</summary> |
| 119 | public const string MeetingParticipantLeave = "application/vnd.microsoft.meetingParticipantLeave"; |
| 120 | |
| 121 | //TODO : review read receipts |
| 122 | /* |
| 123 | /// <summary>Read receipt event name. Fired when a user reads a message in a 1:1 chat with the bot.</summary> |
| 124 | public const string ReadReceipt = "application/vnd.microsoft.readReceipt"; |
| 125 | */ |
| 126 | } |
| 127 | |