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