microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-typo-in-documentation-again

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps/Routing/Router.cs

71lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Api.Activities;
5
6namespace Microsoft.Teams.Apps.Routing;
7
8public interface IRouter
9{
10 public int Length { get; }
11
12 public IList<IRoute> Select(IActivity activity);
13 public IRouter Register(IRoute route);
14 public IRouter Register(Func<IContext<IActivity>, Task<object?>> handler);
15 public IRouter Register(string name, Func<IContext<IActivity>, Task<object?>> handler);
16}
17
18public class Router : IRouter
19{
20 public int Length { get => _routes.Count; }
21
22 protected readonly List<IRoute> _routes = [];
23
24 public IList<IRoute> Select(IActivity activity)
25 {
26 return _routes
27 .Where(route => route.Select(activity))
28 .ToList();
29 }
30
31 public IRouter Register(IRoute route)
32 {
33 if (route.Type == RouteType.User)
34 {
35 var i = _routes.FindIndex(r => r.Name == route.Name && r.Type == RouteType.System);
36
37 if (i > -1)
38 {
39 _routes.RemoveAt(i);
40 }
41 }
42
43 _routes.Add(route);
44 return this;
45 }
46
47 public IRouter Register(Func<IContext<IActivity>, Task<object?>> handler)
48 {
49 return Register(new Route()
50 {
51 Name = "activity",
52 Selector = _ => true,
53 Handler = handler
54 });
55 }
56
57 public IRouter Register(string name, Func<IContext<IActivity>, Task<object?>> handler)
58 {
59 return Register(new Route()
60 {
61 Name = name,
62 Handler = handler,
63 Selector = activity =>
64 {
65 if (name == "activity") return true;
66 if (activity.Type.Equals(name)) return true;
67 return false;
68 }
69 });
70 }
71}