microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-188-again

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps/Contexts/Context.cs

212lines · 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;
8using Microsoft.Teams.Api.Activities;
9using Microsoft.Teams.Api.Auth;
10using Microsoft.Teams.Api.Clients;
11using Microsoft.Teams.Apps.Plugins;
12using Microsoft.Teams.Common.Logging;
13using Microsoft.Teams.Common.Storage;
14
15namespace Microsoft.Teams.Apps;
16
17internal delegate Task ActivitySentHandler(IActivity activity, IContext<IActivity> context);
18
19public partial interface IContext<TActivity> where TActivity : IActivity
20{
21 /// <summary>
22 /// the plugin that received the activity
23 /// </summary>
24 public ISenderPlugin Sender { get; }
25
26 /// <summary>
27 /// the stream instance
28 /// </summary>
29 public IStreamer Stream { get; }
30
31 /// <summary>
32 /// the app id of the bot
33 /// </summary>
34 public string AppId { get; set; }
35
36 /// <summary>
37 /// the tenant id of the request/activity
38 /// </summary>
39 public string TenantId { get; set; }
40
41 /// <summary>
42 /// the app logger instance
43 /// </summary>
44 public ILogger Log { get; set; }
45
46 /// <summary>
47 /// the app storage instance
48 /// </summary>
49 public IStorage<string, object> Storage { get; set; }
50
51 /// <summary>
52 /// the api client
53 /// </summary>
54 public ApiClient Api { get; set; }
55
56 /// <summary>
57 /// the inbound activity
58 /// </summary>
59 public TActivity Activity { get; set; }
60
61 /// <summary>
62 /// the inbound activity conversation reference
63 /// </summary>
64 public ConversationReference Ref { get; set; }
65
66 /// <summary>
67 /// The user's access token to the Microsoft Graph API.
68 /// </summary>
69 public JsonWebToken? UserGraphToken { get; set; }
70
71 /// <summary>
72 /// any extra data
73 /// </summary>
74 public IDictionary<string, object?> Extra { get; set; }
75
76 /// <summary>
77 /// the cancellation token
78 /// </summary>
79 public CancellationToken CancellationToken { get; }
80
81 /// <summary>
82 /// destruct the context
83 /// </summary>
84 /// <param name="log">the ILogger instance</param>
85 /// <param name="api">the api client</param>
86 /// <param name="activity">the inbound activity</param>
87 public void Deconstruct(out ILogger log, out ApiClient api, out TActivity activity);
88
89 /// <summary>
90 /// destruct the context
91 /// </summary>
92 /// <param name="log">the ILogger instance</param>
93 /// <param name="api">the api client</param>
94 /// <param name="activity">the inbound activity</param>
95 /// <param name="send">the methods to send activities</param>
96 public void Deconstruct(out ILogger log, out ApiClient api, out TActivity activity, out IContext.Client client);
97
98 /// <summary>
99 /// destruct the context
100 /// </summary>
101 /// <param name="appId">the apps id</param>
102 /// <param name="log">the ILogger instance</param>
103 /// <param name="api">the api client</param>
104 /// <param name="activity">the inbound activity</param>
105 /// <param name="reference">the inbound conversation reference</param>
106 /// <param name="send">the methods to send activities</param>
107 public void Deconstruct(out string appId, out ILogger log, out ApiClient api, out TActivity activity, out ConversationReference reference, out IContext.Client client);
108
109 /// <summary>
110 /// called to continue the chain of route handlers,
111 /// if not called no other handlers in the sequence will be executed
112 /// </summary>
113 public Task<object?> Next();
114
115 /// <summary>
116 /// Called to continue the chain of route handlers using the specified context instance.
117 /// Use this overload when you want to invoke the next handler with a different or wrapped
118 /// <see cref="IContext{TActivity}"/> than the current one; if not called, no other handlers
119 /// in the sequence will be executed.
120 /// </summary>
121 /// <param name="context">The context to pass to the next handler in the chain.</param>
122 public Task<object?> Next(IContext<TActivity> context);
123
124 /// <summary>
125 /// convert the context to that of another activity type
126 /// </summary>
127 public IContext<IActivity> ToActivityType();
128
129 /// <summary>
130 /// convert the context to that of another activity type
131 /// </summary>
132 public IContext<TToActivity> ToActivityType<TToActivity>() where TToActivity : IActivity;
133}
134
135public partial class Context<TActivity>(ISenderPlugin sender, IStreamer stream) : IContext<TActivity> where TActivity : IActivity
136{
137 public ISenderPlugin Sender { get; set; } = sender;
138 public IStreamer Stream { get; set; } = stream;
139
140 public required string AppId { get; set; }
141 public required string TenantId { get; set; }
142 public required ILogger Log { get; set; }
143 public required IStorage<string, object> Storage { get; set; }
144 public required ApiClient Api { get; set; }
145 public required TActivity Activity { get; set; }
146 public required ConversationReference Ref { get; set; }
147 public required JsonWebToken? UserGraphToken { get; set; }
148 public IDictionary<string, object?> Extra { get; set; } = new Dictionary<string, object?>();
149 public CancellationToken CancellationToken { get; set; }
150
151 internal Func<IContext<IActivity>, Task<object?>> OnNext { get; set; } = (_) => Task.FromResult<object?>(null);
152 internal ActivitySentHandler OnActivitySent { get; set; } = (_, _) => Task.Run(() => { });
153
154 public void Deconstruct(out ILogger log, out ApiClient api, out TActivity activity)
155 {
156 log = Log;
157 api = Api;
158 activity = Activity;
159 }
160
161 public void Deconstruct(out ILogger log, out ApiClient api, out TActivity activity, out IContext.Client client)
162 {
163 log = Log;
164 api = Api;
165 activity = Activity;
166 client = new IContext.Client(ToActivityType());
167 }
168
169 public void Deconstruct(out string appId, out ILogger log, out ApiClient api, out TActivity activity, out ConversationReference reference, out IContext.Client client)
170 {
171 appId = AppId;
172 log = Log;
173 api = Api;
174 activity = Activity;
175 reference = Ref;
176 client = new IContext.Client(ToActivityType());
177 }
178
179 public Task<object?> Next() => OnNext(ToActivityType());
180 public Task<object?> Next(IContext<TActivity> context) => OnNext(context.ToActivityType());
181 public IContext<IActivity> ToActivityType() => ToActivityType<IActivity>();
182 public IContext<TToActivity> ToActivityType<TToActivity>() where TToActivity : IActivity
183 {
184 return new Context<TToActivity>(Sender, Stream)
185 {
186 Sender = Sender,
187 AppId = AppId,
188 TenantId = TenantId,
189 Log = Log,
190 Storage = Storage,
191 Api = Api,
192 Activity = (TToActivity)Activity.ToType(typeof(TToActivity), null),
193 Ref = Ref,
194 UserGraphToken = UserGraphToken,
195 IsSignedIn = IsSignedIn,
196 ConnectionName = ConnectionName,
197 Extra = Extra,
198 CancellationToken = CancellationToken,
199 OnNext = OnNext,
200 OnActivitySent = OnActivitySent
201 };
202 }
203
204 public override string ToString()
205 {
206 return JsonSerializer.Serialize(this, new JsonSerializerOptions()
207 {
208 WriteIndented = true,
209 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
210 });
211 }
212}