microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
core/integration-test-au

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

242lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Api.Activities;
5using Microsoft.Teams.Apps.Routing;
6
7namespace Microsoft.Teams.Apps.Activities;
8
9[AttributeUsage(AttributeTargets.Method, Inherited = true)]
10public class ActivityAttribute(string? name = null, Type? type = null) : Attribute
11{
12 public readonly ActivityType? Name = name is not null ? new(name) : null;
13 public readonly Type Type = type ?? typeof(Activity);
14
15 public virtual bool Select(IActivity activity) => Name is null || Name.Equals(activity.Type);
16 public virtual object Coerce(IContext<IActivity> context) => context.ToActivityType<Activity>();
17}
18
19public static partial class AppActivityExtensions
20{
21 [Obsolete("Use the handler with the cancellation token")]
22 public static App OnActivity(this App app, Func<IContext<IActivity>, Task> handler)
23 {
24 app.Router.Register(async (context) =>
25 {
26 await handler(context).ConfigureAwait(false);
27 return null;
28 });
29
30 return app;
31 }
32
33 public static App OnActivity(this App app, Func<IContext<IActivity>, CancellationToken, Task> handler)
34 {
35 app.Router.Register(async (context) =>
36 {
37 await handler(context, context.CancellationToken).ConfigureAwait(false);
38 return null;
39 });
40
41 return app;
42 }
43
44 [Obsolete("Use the handler with the cancellation token")]
45 public static App OnActivity(this App app, Func<IContext<IActivity>, Task<object?>> handler)
46 {
47 app.Router.Register(handler);
48 return app;
49 }
50
51 public static App OnActivity(this App app, Func<IContext<IActivity>, CancellationToken, Task<object?>> handler)
52 {
53 app.Router.Register((context) => handler(context, context.CancellationToken));
54 return app;
55 }
56
57 [Obsolete("Use the handler with the cancellation token")]
58 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, Task> handler)
59 {
60 app.Router.Register(new Route()
61 {
62 Name = type,
63 Type = app.Status is null ? RouteType.System : RouteType.User,
64 Handler = async (context) =>
65 {
66 await handler(context).ConfigureAwait(false);
67 return null;
68 },
69 Selector = (activity) => activity.Type.Equals(type),
70 });
71
72 return app;
73 }
74
75 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, CancellationToken, Task> handler)
76 {
77 app.Router.Register(new Route()
78 {
79 Name = type,
80 Type = app.Status is null ? RouteType.System : RouteType.User,
81 Handler = async (context) =>
82 {
83 await handler(context, context.CancellationToken).ConfigureAwait(false);
84 return null;
85 },
86 Selector = (activity) => activity.Type.Equals(type),
87 });
88
89 return app;
90 }
91
92 [Obsolete("Use the handler with the cancellation token")]
93 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, Task<object?>> handler)
94 {
95 app.Router.Register(new Route()
96 {
97 Name = type,
98 Type = app.Status is null ? RouteType.System : RouteType.User,
99 Handler = handler,
100 Selector = (activity) => activity.Type.Equals(type),
101 });
102
103 return app;
104 }
105
106 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, CancellationToken, Task<object?>> handler)
107 {
108 app.Router.Register(new Route()
109 {
110 Name = type,
111 Type = app.Status is null ? RouteType.System : RouteType.User,
112 Handler = (context) => handler(context, context.CancellationToken),
113 Selector = (activity) => activity.Type.Equals(type),
114 });
115
116 return app;
117 }
118
119 [Obsolete("Use the handler with the cancellation token")]
120 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, Task> handler) where TActivity : IActivity
121 {
122 app.Router.Register(new Route()
123 {
124 Name = "activity",
125 Type = app.Status is null ? RouteType.System : RouteType.User,
126 Handler = async (context) =>
127 {
128 await handler(context.ToActivityType<TActivity>()).ConfigureAwait(false);
129 return null;
130 },
131 Selector = (activity) => activity.GetType() == typeof(TActivity),
132 });
133
134 return app;
135 }
136
137 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, CancellationToken, Task> handler) where TActivity : IActivity
138 {
139 app.Router.Register(new Route()
140 {
141 Name = "activity",
142 Type = app.Status is null ? RouteType.System : RouteType.User,
143 Handler = async (context) =>
144 {
145 await handler(context.ToActivityType<TActivity>(), context.CancellationToken).ConfigureAwait(false);
146 return null;
147 },
148 Selector = (activity) => activity.GetType() == typeof(TActivity),
149 });
150
151 return app;
152 }
153
154 [Obsolete("Use the handler with the cancellation token")]
155 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, Task<object?>> handler) where TActivity : IActivity
156 {
157 app.Router.Register(new Route()
158 {
159 Name = "activity",
160 Type = app.Status is null ? RouteType.System : RouteType.User,
161 Handler = (context) => handler(context.ToActivityType<TActivity>()),
162 Selector = (activity) => activity.GetType() == typeof(TActivity),
163 });
164
165 return app;
166 }
167
168 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, CancellationToken, Task<object?>> handler) where TActivity : IActivity
169 {
170 app.Router.Register(new Route()
171 {
172 Name = "activity",
173 Type = app.Status is null ? RouteType.System : RouteType.User,
174 Handler = (context) => handler(context.ToActivityType<TActivity>(), context.CancellationToken),
175 Selector = (activity) => activity.GetType() == typeof(TActivity),
176 });
177
178 return app;
179 }
180
181 [Obsolete("Use the handler with the cancellation token")]
182 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, Task> handler)
183 {
184 app.Router.Register(new Route()
185 {
186 Name = "activity",
187 Type = app.Status is null ? RouteType.System : RouteType.User,
188 Selector = select,
189 Handler = async (context) =>
190 {
191 await handler(context).ConfigureAwait(false);
192 return null;
193 }
194 });
195
196 return app;
197 }
198
199 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, CancellationToken, Task> handler)
200 {
201 app.Router.Register(new Route()
202 {
203 Name = "activity",
204 Type = app.Status is null ? RouteType.System : RouteType.User,
205 Selector = select,
206 Handler = async (context) =>
207 {
208 await handler(context, context.CancellationToken).ConfigureAwait(false);
209 return null;
210 }
211 });
212
213 return app;
214 }
215
216 [Obsolete("Use the handler with the cancellation token")]
217 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, Task<object?>> handler)
218 {
219 app.Router.Register(new Route()
220 {
221 Name = "activity",
222 Type = app.Status is null ? RouteType.System : RouteType.User,
223 Selector = select,
224 Handler = handler
225 });
226
227 return app;
228 }
229
230 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, CancellationToken, Task<object?>> handler)
231 {
232 app.Router.Register(new Route()
233 {
234 Name = "activity",
235 Type = app.Status is null ? RouteType.System : RouteType.User,
236 Selector = select,
237 Handler = (context) => handler(context, context.CancellationToken)
238 });
239
240 return app;
241 }
242}