microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
99620f7f6ecccb78c4ebfb54686bb2a4e56dcea8

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

276lines · 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 // TODO: Is this correct ? In a reply scenario, the From of the incoming activity becomes the Recipient of the reply, and vice versa.
52
53 if (!string.IsNullOrEmpty(activity.Id))
54 {
55 WithReplyToId(activity.Id);
56 }
57
58 return (TBuilder)this;
59 }
60
61 /// <summary>
62 /// Sets the conversation (to be overridden by derived classes for type-specific behavior).
63 /// </summary>
64 protected abstract void SetConversation(Conversation? conversation);
65
66 /// <summary>
67 /// Sets the From account (to be overridden by derived classes for type-specific behavior).
68 /// </summary>
69 protected abstract void SetFrom(ConversationAccount? from);
70
71 /// <summary>
72 /// Sets the Recipient account (to be overridden by derived classes for type-specific behavior).
73 /// </summary>
74 protected abstract void SetRecipient(ConversationAccount? recipient);
75
76 /// <summary>
77 /// Sets the activity ID.
78 /// </summary>
79 /// <param name="id">The activity ID.</param>
80 /// <returns>The builder instance for chaining.</returns>
81 public TBuilder WithId(string id)
82 {
83 _activity.Id = id;
84 return (TBuilder)this;
85 }
86
87 /// <summary>
88 /// Sets the Reply to id
89 /// </summary>
90 /// <param name="replyToId"></param>
91 /// <returns></returns>
92 public TBuilder WithReplyToId(string replyToId)
93 {
94 _activity.ReplyToId = replyToId;
95 return (TBuilder)this;
96 }
97
98 /// <summary>
99 /// Sets the service URL.
100 /// </summary>
101 /// <param name="serviceUrl">The service URL.</param>
102 /// <returns>The builder instance for chaining.</returns>
103 public TBuilder WithServiceUrl(Uri serviceUrl)
104 {
105 _activity.ServiceUrl = serviceUrl;
106 return (TBuilder)this;
107 }
108 /// <summary>
109 /// Sets the service URL from a string.
110 /// </summary>
111 /// <param name="serviceUrlString"></param>
112 /// <returns></returns>
113 public TBuilder WithServiceUrl(string serviceUrlString)
114 {
115 _activity.ServiceUrl = new Uri(serviceUrlString);
116 return (TBuilder)this;
117 }
118
119 /// <summary>
120 /// Sets the channel ID.
121 /// </summary>
122 /// <param name="channelId">The channel ID.</param>
123 /// <returns>The builder instance for chaining.</returns>
124 public TBuilder WithChannelId(string channelId)
125 {
126 _activity.ChannelId = channelId;
127 return (TBuilder)this;
128 }
129
130 /// <summary>
131 /// Sets the activity type.
132 /// </summary>
133 /// <param name="type">The activity type.</param>
134 /// <returns>The builder instance for chaining.</returns>
135 public TBuilder WithType(string type)
136 {
137 _activity.Type = type;
138 return (TBuilder)this;
139 }
140
141 /// <summary>
142 /// Adds or updates a property in the activity's Properties dictionary.
143 /// </summary>
144 /// <param name="name">Name of the property.</param>
145 /// <param name="value">Value of the property.</param>
146 /// <returns>The builder instance for chaining.</returns>
147 public TBuilder WithProperty<T>(string name, T? value)
148 {
149 _activity.Properties[name] = value;
150 return (TBuilder)this;
151 }
152
153 /// <summary>
154 /// Sets the sender account information.
155 /// </summary>
156 /// <param name="from">The sender account.</param>
157 /// <returns>The builder instance for chaining.</returns>
158 public TBuilder WithFrom(ConversationAccount? from)
159 {
160 SetFrom(from);
161 return (TBuilder)this;
162 }
163
164 /// <summary>
165 /// Sets the recipient account information.
166 /// </summary>
167 /// <param name="recipient">The recipient account.</param>
168 /// <returns>The builder instance for chaining.</returns>
169 public TBuilder WithRecipient(ConversationAccount? recipient)
170 {
171 SetRecipient(recipient);
172 return (TBuilder)this;
173 }
174
175 /// <summary>
176 /// Sets the recipient account information and optionally marks this as a targeted message.
177 /// </summary>
178 /// <param name="recipient">The recipient account.</param>
179 /// <param name="isTargeted">If true, marks this as a targeted message visible only to the specified recipient.</param>
180 /// <returns>The builder instance for chaining.</returns>
181 public TBuilder WithRecipient(ConversationAccount? recipient, bool isTargeted)
182 {
183 if (recipient is null)
184 {
185 SetRecipient(null);
186 }
187 else
188 {
189 recipient.IsTargeted = isTargeted ? true : null;
190 SetRecipient(recipient);
191 }
192 return (TBuilder)this;
193 }
194
195 /// <summary>
196 /// Sets the conversation information.
197 /// </summary>
198 /// <param name="conversation">The conversation information.</param>
199 /// <returns>The builder instance for chaining.</returns>
200 public TBuilder WithConversation(Conversation? conversation)
201 {
202 SetConversation(conversation);
203 return (TBuilder)this;
204 }
205
206 /// <summary>
207 /// Sets the channel-specific data (to be overridden by derived classes for type-specific behavior).
208 /// </summary>
209 /// <param name="channelData">The channel data.</param>
210 /// <returns>The builder instance for chaining.</returns>
211 public virtual TBuilder WithChannelData(ChannelData? channelData)
212 {
213 _activity.ChannelData = channelData;
214 return (TBuilder)this;
215 }
216
217 /// <summary>
218 /// Builds and returns the configured activity instance.
219 /// </summary>
220 /// <returns>The configured activity.</returns>
221 public abstract TActivity Build();
222}
223
224/// <summary>
225/// Provides a fluent API for building CoreActivity instances.
226/// </summary>
227public class CoreActivityBuilder : CoreActivityBuilder<CoreActivity, CoreActivityBuilder>
228{
229 /// <summary>
230 /// Initializes a new instance of the CoreActivityBuilder class.
231 /// </summary>
232 internal CoreActivityBuilder() : base(new CoreActivity())
233 {
234 }
235
236 /// <summary>
237 /// Initializes a new instance of the CoreActivityBuilder class with an existing activity.
238 /// </summary>
239 /// <param name="activity">The activity to build upon.</param>
240 internal CoreActivityBuilder(CoreActivity activity) : base(activity)
241 {
242 }
243
244 /// <summary>
245 /// Sets the conversation.
246 /// </summary>
247 protected override void SetConversation(Conversation? conversation)
248 {
249 _activity.Conversation = conversation;
250 }
251
252 /// <summary>
253 /// Sets the From account.
254 /// </summary>
255 protected override void SetFrom(ConversationAccount? from)
256 {
257 _activity.From = from;
258 }
259
260 /// <summary>
261 /// Sets the Recipient account.
262 /// </summary>
263 protected override void SetRecipient(ConversationAccount? recipient)
264 {
265 _activity.Recipient = recipient;
266 }
267
268 /// <summary>
269 /// Builds and returns the configured CoreActivity instance.
270 /// </summary>
271 /// <returns>The configured CoreActivity.</returns>
272 public override CoreActivity Build()
273 {
274 return _activity;
275 }
276}
277