microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-338

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

241lines · 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 return WithRecipient(recipient, false);
145 }
146
147 /// <summary>
148 /// Sets the recipient account information and optionally marks this as a targeted message.
149 /// </summary>
150 /// <param name="recipient">The recipient account.</param>
151 /// <param name="isTargeted">If true, marks this as a targeted message visible only to the specified recipient.</param>
152 /// <returns>The builder instance for chaining.</returns>
153 public TBuilder WithRecipient(ConversationAccount recipient, bool isTargeted)
154 {
155 SetRecipient(recipient);
156 _activity.IsTargeted = isTargeted;
157 return (TBuilder)this;
158 }
159
160 /// <summary>
161 /// Sets the conversation information.
162 /// </summary>
163 /// <param name="conversation">The conversation information.</param>
164 /// <returns>The builder instance for chaining.</returns>
165 public TBuilder WithConversation(Conversation conversation)
166 {
167 SetConversation(conversation);
168 return (TBuilder)this;
169 }
170
171 /// <summary>
172 /// Sets the channel-specific data (to be overridden by derived classes for type-specific behavior).
173 /// </summary>
174 /// <param name="channelData">The channel data.</param>
175 /// <returns>The builder instance for chaining.</returns>
176 public virtual TBuilder WithChannelData(ChannelData? channelData)
177 {
178 _activity.ChannelData = channelData;
179 return (TBuilder)this;
180 }
181
182 /// <summary>
183 /// Builds and returns the configured activity instance.
184 /// </summary>
185 /// <returns>The configured activity.</returns>
186 public abstract TActivity Build();
187}
188
189/// <summary>
190/// Provides a fluent API for building CoreActivity instances.
191/// </summary>
192public class CoreActivityBuilder : CoreActivityBuilder<CoreActivity, CoreActivityBuilder>
193{
194 /// <summary>
195 /// Initializes a new instance of the CoreActivityBuilder class.
196 /// </summary>
197 internal CoreActivityBuilder() : base(new CoreActivity())
198 {
199 }
200
201 /// <summary>
202 /// Initializes a new instance of the CoreActivityBuilder class with an existing activity.
203 /// </summary>
204 /// <param name="activity">The activity to build upon.</param>
205 internal CoreActivityBuilder(CoreActivity activity) : base(activity)
206 {
207 }
208
209 /// <summary>
210 /// Sets the conversation.
211 /// </summary>
212 protected override void SetConversation(Conversation conversation)
213 {
214 _activity.Conversation = conversation;
215 }
216
217 /// <summary>
218 /// Sets the From account.
219 /// </summary>
220 protected override void SetFrom(ConversationAccount from)
221 {
222 _activity.From = from;
223 }
224
225 /// <summary>
226 /// Sets the Recipient account.
227 /// </summary>
228 protected override void SetRecipient(ConversationAccount recipient)
229 {
230 _activity.Recipient = recipient;
231 }
232
233 /// <summary>
234 /// Builds and returns the configured CoreActivity instance.
235 /// </summary>
236 /// <returns>The configured CoreActivity.</returns>
237 public override CoreActivity Build()
238 {
239 return _activity;
240 }
241}
242