microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Api/Activities/Activity.cs

416lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Text.Json;
5using System.Text.Json.Serialization;
6
7using Microsoft.Teams.Api.Entities;
8using Microsoft.Teams.Common;
9
10namespace Microsoft.Teams.Api.Activities;
11
12[JsonConverter(typeof(JsonConverter<ActivityType>))]
13public partial class ActivityType(string value) : StringEnum(value)
14{
15 public Type ToType()
16 {
17 if (IsTyping) return typeof(TypingActivity);
18 if (IsCommand) return typeof(CommandActivity);
19 if (IsCommandResult) return typeof(CommandResultActivity);
20 if (IsConversationUpdate) return typeof(ConversationUpdateActivity);
21 if (IsEndOfConversation) return typeof(EndOfConversationActivity);
22 if (IsInstallUpdate) return typeof(InstallUpdateActivity);
23 if (IsMessage) return typeof(MessageActivity);
24 if (IsMessageUpdate) return typeof(MessageUpdateActivity);
25 if (IsMessageDelete) return typeof(MessageDeleteActivity);
26 if (IsMessageReaction) return typeof(MessageReactionActivity);
27 if (IsEvent) return typeof(EventActivity);
28 if (IsInvoke) return typeof(InvokeActivity);
29 return typeof(Activity);
30 }
31
32 public string ToPrettyString()
33 {
34 var value = ToString();
35 return $"{value.First().ToString().ToUpper()}{value.AsSpan(1).ToString()}";
36 }
37}
38
39[JsonConverter(typeof(JsonConverter))]
40public partial interface IActivity : IConvertible, ICloneable
41{
42 public string Id { get; set; }
43
44 public ActivityType Type { get; set; }
45
46 public string? ReplyToId { get; set; }
47
48 public ChannelId ChannelId { get; set; }
49
50 public Account From { get; set; }
51
52 public Account Recipient { get; set; }
53
54 public Conversation Conversation { get; set; }
55
56 public ConversationReference? RelatesTo { get; set; }
57
58 public string? ServiceUrl { get; set; }
59
60 public string? Locale { get; set; }
61
62 public DateTime? Timestamp { get; set; }
63
64 public DateTime? LocalTimestamp { get; set; }
65
66 public IList<IEntity>? Entities { get; set; }
67
68 public ChannelData? ChannelData { get; set; }
69
70 public IDictionary<string, object?> Properties { get; set; }
71
72 /// <summary>
73 /// is this a streaming activity
74 /// </summary>
75 [JsonIgnore]
76 public bool IsStreaming { get; }
77
78 /// <summary>
79 /// get the activity type/name path
80 /// </summary>
81 public string GetPath();
82
83 /// <summary>
84 /// get the quote reply string form of this activity
85 /// </summary>
86 public string ToQuoteReply();
87}
88
89[JsonConverter(typeof(JsonConverter))]
90public partial class Activity : IActivity
91{
92 [JsonPropertyName("id")]
93 [JsonPropertyOrder(0)]
94 public string Id { get; set; }
95
96 [JsonPropertyName("type")]
97 [JsonPropertyOrder(10)]
98 public ActivityType Type { get; set; }
99
100 [JsonPropertyName("replyToId")]
101 [JsonPropertyOrder(20)]
102 public string? ReplyToId { get; set; }
103
104 [JsonPropertyName("channelId")]
105 [JsonPropertyOrder(30)]
106 public ChannelId ChannelId { get; set; } = ChannelId.MsTeams;
107
108 [JsonPropertyName("from")]
109 [JsonPropertyOrder(40)]
110 public Account From { get; set; }
111
112 [JsonPropertyName("recipient")]
113 [JsonPropertyOrder(50)]
114 public Account Recipient { get; set; }
115
116 [JsonPropertyName("conversation")]
117 [JsonPropertyOrder(60)]
118 public Conversation Conversation { get; set; }
119
120 [JsonPropertyName("relatesTo")]
121 [JsonPropertyOrder(70)]
122 public ConversationReference? RelatesTo { get; set; }
123
124 [JsonPropertyName("serviceUrl")]
125 [JsonPropertyOrder(80)]
126 public string? ServiceUrl { get; set; }
127
128 [JsonPropertyName("locale")]
129 [JsonPropertyOrder(90)]
130 public string? Locale { get; set; }
131
132 [JsonPropertyName("timestamp")]
133 [JsonPropertyOrder(100)]
134 public DateTime? Timestamp { get; set; }
135
136 [JsonPropertyName("localTimestamp")]
137 [JsonPropertyOrder(110)]
138 public DateTime? LocalTimestamp { get; set; }
139
140 [JsonPropertyName("entities")]
141 [JsonPropertyOrder(120)]
142 public IList<IEntity>? Entities { get; set; }
143
144 [JsonPropertyName("channelData")]
145 [JsonPropertyOrder(130)]
146 public ChannelData? ChannelData { get; set; }
147
148 [JsonExtensionData]
149 public IDictionary<string, object?> Properties { get; set; } = new Dictionary<string, object?>();
150
151 [JsonConstructor]
152 public Activity(string type)
153 {
154 Type = new(type);
155 }
156
157 public Activity(ActivityType type)
158 {
159 Type = type;
160 }
161
162 public Activity(IActivity activity)
163 {
164 Id = activity.Id;
165 Type = activity.Type;
166 ReplyToId = activity.ReplyToId;
167 ChannelId = activity.ChannelId;
168 From = activity.From;
169 Recipient = activity.Recipient;
170 Conversation = activity.Conversation;
171 RelatesTo = activity.RelatesTo;
172 ServiceUrl = activity.ServiceUrl;
173 Locale = activity.Locale;
174 Timestamp = activity.Timestamp;
175 LocalTimestamp = activity.LocalTimestamp;
176 Entities = activity.Entities;
177 ChannelData = activity.ChannelData;
178 Properties = activity.Properties;
179 }
180
181 [JsonIgnore]
182 public bool IsStreaming => Entities?.Any(entity => entity.Type == "streaminfo" && entity is StreamInfoEntity) ?? false;
183
184 public object Clone() => MemberwiseClone();
185 public virtual Activity Copy() => (Activity)Clone();
186 public virtual string GetPath() => string.Join(".", ["Activity", Type.ToPrettyString()]);
187
188 public virtual Activity WithId(string value)
189 {
190 Id = value;
191 return this;
192 }
193
194 public virtual Activity WithReplyToId(string value)
195 {
196 ReplyToId = value;
197 return this;
198 }
199
200 public virtual Activity WithChannelId(ChannelId value)
201 {
202 ChannelId = value;
203 return this;
204 }
205
206 public virtual Activity WithFrom(Account value)
207 {
208 From = value;
209 return this;
210 }
211
212 public virtual Activity WithConversation(Conversation value)
213 {
214 Conversation = value;
215 return this;
216 }
217
218 public virtual Activity WithRelatesTo(ConversationReference value)
219 {
220 RelatesTo = value;
221 return this;
222 }
223
224 public virtual Activity WithRecipient(Account value)
225 {
226 Recipient = value;
227 return this;
228 }
229
230 public virtual Activity WithServiceUrl(string value)
231 {
232 ServiceUrl = value;
233 return this;
234 }
235
236 public virtual Activity WithTimestamp(DateTime value)
237 {
238 Timestamp = value;
239 return this;
240 }
241
242 public virtual Activity WithLocale(string value)
243 {
244 Locale = value;
245 return this;
246 }
247
248 public virtual Activity WithLocalTimestamp(DateTime value)
249 {
250 LocalTimestamp = value;
251 return this;
252 }
253
254 public virtual Activity WithData(ChannelData value)
255 {
256 ChannelData ??= new();
257 ChannelData.Merge(value);
258 return this;
259 }
260
261 public virtual Activity WithData(string key, object? value)
262 {
263 ChannelData ??= new();
264 ChannelData.Properties[key] = value;
265 return this;
266 }
267
268 public virtual Activity WithAppId(string value)
269 {
270 ChannelData ??= new();
271 ChannelData.App ??= new App() { Id = value };
272 return this;
273 }
274
275 /// <summary>
276 /// add an entity
277 /// </summary>
278 public virtual Activity AddEntity(params IEntity[] entities)
279 {
280 Entities ??= [];
281
282 foreach (var entity in entities)
283 {
284 Entities.Add(entity);
285 }
286
287 return this;
288 }
289
290 /// <summary>
291 /// add the `Generated By AI` label
292 /// </summary>
293 public virtual Activity AddAIGenerated()
294 {
295 return AddEntity(new MessageEntity()
296 {
297 Type = "https://schema.org/Message",
298 OType = "Message",
299 OContext = "https://schema.org",
300 AdditionalType = ["AIGeneratedContent"]
301 });
302 }
303
304 /// <summary>
305 /// add content sensitivity label
306 /// </summary>
307 /// <param name="name">the content title</param>
308 /// <param name="description">the content description</param>
309 /// <param name="pattern">the pattern</param>
310 public virtual Activity AddSensitivityLabel(string name, string? description = null, DefinedTerm? pattern = null)
311 {
312 return AddEntity(new SensitiveUsageEntity()
313 {
314 Name = name,
315 Description = description,
316 Pattern = pattern
317 });
318 }
319
320 /// <summary>
321 /// enable/disable message feedback
322 /// </summary>
323 public virtual Activity AddFeedback(bool value = true)
324 {
325 ChannelData ??= new();
326 ChannelData.FeedbackLoopEnabled = value;
327 return this;
328 }
329
330 /// <summary>
331 /// add a citation
332 /// </summary>
333 public virtual Activity AddCitation(int position, CitationAppearance appearance)
334 {
335 return AddEntity(new CitationEntity()
336 {
337 Position = position,
338 Appearance = appearance.ToDocument()
339 });
340 }
341
342 public CommandActivity ToCommand() => (CommandActivity)this;
343 public CommandResultActivity ToCommandResult() => (CommandResultActivity)this;
344 public TypingActivity ToTyping() => (TypingActivity)this;
345 public InstallUpdateActivity ToInstallUpdate() => (InstallUpdateActivity)this;
346 public MessageActivity ToMessage() => (MessageActivity)this;
347 public MessageUpdateActivity ToMessageUpdate() => (MessageUpdateActivity)this;
348 public MessageDeleteActivity ToMessageDelete() => (MessageDeleteActivity)this;
349 public MessageReactionActivity ToMessageReaction() => (MessageReactionActivity)this;
350 public ConversationUpdateActivity ToConversationUpdate() => (ConversationUpdateActivity)this;
351 public EndOfConversationActivity ToEndOfConversation() => (EndOfConversationActivity)this;
352 public EventActivity ToEvent() => (EventActivity)this;
353 public InvokeActivity ToInvoke() => (InvokeActivity)this;
354
355 public Activity Merge(Activity from)
356 {
357 Id ??= from.Id;
358 ReplyToId ??= from.ReplyToId;
359 ChannelId ??= from.ChannelId;
360 From ??= from.From;
361 Recipient ??= from.Recipient;
362 Conversation ??= from.Conversation;
363 RelatesTo ??= from.RelatesTo;
364 ServiceUrl ??= from.ServiceUrl;
365 Locale ??= from.Locale;
366 Timestamp ??= from.Timestamp;
367 LocalTimestamp ??= from.LocalTimestamp;
368 AddEntity(from.Entities?.ToArray() ?? []);
369
370 if (from.ChannelData is not null)
371 {
372 WithData(from.ChannelData);
373 }
374
375 if (from.Properties is not null)
376 {
377 Properties ??= new Dictionary<string, object?>();
378
379 foreach (var kv in from.Properties)
380 {
381 Properties[kv.Key] = kv.Value;
382 }
383 }
384
385 return this;
386 }
387
388 public string ToQuoteReply()
389 {
390 var text = string.Empty;
391
392 if (this is MessageActivity message)
393 {
394 text = $"<p itemprop=\"preview\">{message.Text}</p>";
395 }
396
397 return $"""
398 <blockquote itemscope="" itemtype="http://schema.skype.com/Reply" itemid="{Id}">
399 <strong itemprop="mri" itemid="{From.Id}">
400 {From.Name}
401 </strong>
402 <span itemprop="time" itemid="{Id}"></span>
403 {text}
404 </blockquote>
405 """;
406 }
407
408 public override string ToString()
409 {
410 return JsonSerializer.Serialize(this, new JsonSerializerOptions()
411 {
412 WriteIndented = true,
413 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
414 });
415 }
416}