microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
5e595aec73b538083f2def3e2971f2cd7f31ca22

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Core/Schema/CoreActivityBuilder.cs

229lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4namespace Microsoft.Teams.Bot.Core.Schema;
5
6/// <summary>
7/// Provides a fluent API for building CoreActivity instances.
8/// </summary>
9/// <typeparam name="TActivity">The type of activity being built.</typeparam>
10/// <typeparam name="TBuilder">The type of the builder (for fluent method chaining).</typeparam>
11public abstract class CoreActivityBuilder<TActivity, TBuilder>
12 where TActivity : CoreActivity
13 where TBuilder : CoreActivityBuilder<TActivity, TBuilder>
14{
15 /// <summary>
16 /// The activity being built.
17 /// </summary>
18#pragma warning disable CA1051 // Do not declare visible instance fields
19 protected readonly TActivity _activity;
20#pragma warning restore CA1051 // Do not declare visible instance fields
21
22 /// <summary>
23 /// Initializes a new instance of the CoreActivityBuilder class.
24 /// </summary>
25 /// <param name="activity">The activity to build upon.</param>
26 protected CoreActivityBuilder(TActivity activity)
27 {
28 ArgumentNullException.ThrowIfNull(activity);
29 _activity = activity;
30 }
31
32 /// <summary>
33 /// Apply Conversation Reference
34 /// </summary>
35 /// <param name="activity">The source activity to copy conversation reference from.</param>
36 /// <returns>The builder instance for chaining.</returns>
37 public TBuilder WithConversationReference(TActivity activity)
38 {
39 ArgumentNullException.ThrowIfNull(activity);
40 ArgumentNullException.ThrowIfNull(activity.ChannelId);
41 ArgumentNullException.ThrowIfNull(activity.ServiceUrl);
42 ArgumentNullException.ThrowIfNull(activity.Conversation);
43 ArgumentNullException.ThrowIfNull(activity.From);
44 ArgumentNullException.ThrowIfNull(activity.Recipient);
45
46 WithServiceUrl(activity.ServiceUrl);
47 WithChannelId(activity.ChannelId);
48 SetConversation(activity.Conversation);
49 SetFrom(activity.Recipient);
50 SetRecipient(activity.From);
51
52 return (TBuilder)this;
53 }
54
55 /// <summary>
56 /// Sets the conversation (to be overridden by derived classes for type-specific behavior).
57 /// </summary>
58 protected abstract void SetConversation(Conversation conversation);
59
60 /// <summary>
61 /// Sets the From account (to be overridden by derived classes for type-specific behavior).
62 /// </summary>
63 protected abstract void SetFrom(ConversationAccount from);
64
65 /// <summary>
66 /// Sets the Recipient account (to be overridden by derived classes for type-specific behavior).
67 /// </summary>
68 protected abstract void SetRecipient(ConversationAccount recipient);
69
70 /// <summary>
71 /// Sets the activity ID.
72 /// </summary>
73 /// <param name="id">The activity ID.</param>
74 /// <returns>The builder instance for chaining.</returns>
75 public TBuilder WithId(string id)
76 {
77 _activity.Id = id;
78 return (TBuilder)this;
79 }
80
81 /// <summary>
82 /// Sets the service URL.
83 /// </summary>
84 /// <param name="serviceUrl">The service URL.</param>
85 /// <returns>The builder instance for chaining.</returns>
86 public TBuilder WithServiceUrl(Uri serviceUrl)
87 {
88 _activity.ServiceUrl = serviceUrl;
89 return (TBuilder)this;
90 }
91
92 /// <summary>
93 /// Sets the channel ID.
94 /// </summary>
95 /// <param name="channelId">The channel ID.</param>
96 /// <returns>The builder instance for chaining.</returns>
97 public TBuilder WithChannelId(string channelId)
98 {
99 _activity.ChannelId = channelId;
100 return (TBuilder)this;
101 }
102
103 /// <summary>
104 /// Sets the activity type.
105 /// </summary>
106 /// <param name="type">The activity type.</param>
107 /// <returns>The builder instance for chaining.</returns>
108 public TBuilder WithType(string type)
109 {
110 _activity.Type = type;
111 return (TBuilder)this;
112 }
113
114 /// <summary>
115 /// Adds or updates a property in the activity's Properties dictionary.
116 /// </summary>
117 /// <param name="name">Name of the property.</param>
118 /// <param name="value">Value of the property.</param>
119 /// <returns>The builder instance for chaining.</returns>
120 public TBuilder WithProperty<T>(string name, T? value)
121 {
122 _activity.Properties[name] = value;
123 return (TBuilder)this;
124 }
125
126 /// <summary>
127 /// Sets the sender account information.
128 /// </summary>
129 /// <param name="from">The sender account.</param>
130 /// <returns>The builder instance for chaining.</returns>
131 public TBuilder WithFrom(ConversationAccount from)
132 {
133 SetFrom(from);
134 return (TBuilder)this;
135 }
136
137 /// <summary>
138 /// Sets the recipient account information.
139 /// </summary>
140 /// <param name="recipient">The recipient account.</param>
141 /// <returns>The builder instance for chaining.</returns>
142 public TBuilder WithRecipient(ConversationAccount recipient)
143 {
144 SetRecipient(recipient);
145 return (TBuilder)this;
146 }
147
148 /// <summary>
149 /// Sets the conversation information.
150 /// </summary>
151 /// <param name="conversation">The conversation information.</param>
152 /// <returns>The builder instance for chaining.</returns>
153 public TBuilder WithConversation(Conversation conversation)
154 {
155 SetConversation(conversation);
156 return (TBuilder)this;
157 }
158
159 /// <summary>
160 /// Sets the channel-specific data (to be overridden by derived classes for type-specific behavior).
161 /// </summary>
162 /// <param name="channelData">The channel data.</param>
163 /// <returns>The builder instance for chaining.</returns>
164 public virtual TBuilder WithChannelData(ChannelData? channelData)
165 {
166 _activity.ChannelData = channelData;
167 return (TBuilder)this;
168 }
169
170 /// <summary>
171 /// Builds and returns the configured activity instance.
172 /// </summary>
173 /// <returns>The configured activity.</returns>
174 public abstract TActivity Build();
175}
176
177/// <summary>
178/// Provides a fluent API for building CoreActivity instances.
179/// </summary>
180public class CoreActivityBuilder : CoreActivityBuilder<CoreActivity, CoreActivityBuilder>
181{
182 /// <summary>
183 /// Initializes a new instance of the CoreActivityBuilder class.
184 /// </summary>
185 internal CoreActivityBuilder() : base(new CoreActivity())
186 {
187 }
188
189 /// <summary>
190 /// Initializes a new instance of the CoreActivityBuilder class with an existing activity.
191 /// </summary>
192 /// <param name="activity">The activity to build upon.</param>
193 internal CoreActivityBuilder(CoreActivity activity) : base(activity)
194 {
195 }
196
197 /// <summary>
198 /// Sets the conversation.
199 /// </summary>
200 protected override void SetConversation(Conversation conversation)
201 {
202 _activity.Conversation = conversation;
203 }
204
205 /// <summary>
206 /// Sets the From account.
207 /// </summary>
208 protected override void SetFrom(ConversationAccount from)
209 {
210 _activity.From = from;
211 }
212
213 /// <summary>
214 /// Sets the Recipient account.
215 /// </summary>
216 protected override void SetRecipient(ConversationAccount recipient)
217 {
218 _activity.Recipient = recipient;
219 }
220
221 /// <summary>
222 /// Builds and returns the configured CoreActivity instance.
223 /// </summary>
224 /// <returns>The configured CoreActivity.</returns>
225 public override CoreActivity Build()
226 {
227 return _activity;
228 }
229}