Skip to content

Commit 3c01b9f

Browse files
committed
Fix duplicates in the actor tags
1 parent 6ba8e1d commit 3c01b9f

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

src/bot-impl.ts

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
7575
readonly icon?: URL;
7676
readonly image?: URL;
7777
readonly properties: Record<string, Text<"block" | "inline", TContextData>>;
78+
#properties: { pairs: PropertyValue[]; tags: Link[] } | null;
7879
readonly kv: KvStore;
7980
readonly kvPrefixes: BotKvPrefixes;
8081
readonly software?: Software;
@@ -96,6 +97,7 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
9697
this.icon = options.icon;
9798
this.image = options.image;
9899
this.properties = options.properties ?? {};
100+
this.#properties = null;
99101
this.kv = options.kv;
100102
this.kvPrefixes = {
101103
keyPairs: ["_botkit", "keyPairs"],
@@ -181,51 +183,68 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
181183
}
182184
}
183185

184-
async dispatchActor(
185-
ctx: Context<TContextData>,
186-
identifier: string,
187-
): Promise<Actor | null> {
188-
if (this.identifier !== identifier) return null;
189-
const session = this.getSession(ctx);
190-
let summary: string | null = null;
191-
let tags: Link[] = [];
192-
if (this.summary != null) {
193-
if (this.#summary == null) {
194-
summary = "";
195-
for await (const chunk of this.summary.getHtml(session)) {
196-
summary += chunk;
197-
}
198-
for await (const tag of this.summary.getTags(session)) {
199-
tags.push(tag);
200-
}
201-
this.#summary = { text: summary, tags };
202-
} else {
203-
summary = this.#summary.text;
204-
tags = this.#summary.tags;
186+
async #getSummary(
187+
session: Session<TContextData>,
188+
): Promise<{ text: string; tags: Link[] } | null> {
189+
if (this.summary == null) return null;
190+
if (this.#summary == null) {
191+
let summary = "";
192+
const tags: Link[] = [];
193+
for await (const chunk of this.summary.getHtml(session)) {
194+
summary += chunk;
195+
}
196+
for await (const tag of this.summary.getTags(session)) {
197+
tags.push(tag);
205198
}
199+
return this.#summary = { text: summary, tags };
206200
}
207-
const attachments: (Object | Link | PropertyValue)[] = [];
201+
return this.#summary;
202+
}
203+
204+
async #getProperties(
205+
session: Session<TContextData>,
206+
): Promise<{ pairs: PropertyValue[]; tags: Link[] }> {
207+
if (this.#properties != null) return this.#properties;
208+
const pairs: PropertyValue[] = [];
209+
const tags: Link[] = [];
208210
for (const name in this.properties) {
209211
const value = this.properties[name];
210212
const pair = new PropertyValue({
211213
name,
212214
value: (await Array.fromAsync(value.getHtml(session))).join(""),
213215
});
214-
attachments.push(pair);
216+
pairs.push(pair);
215217
for await (const tag of value.getTags(session)) {
216218
tags.push(tag);
217219
}
218220
}
221+
return this.#properties = { pairs, tags };
222+
}
223+
224+
async dispatchActor(
225+
ctx: Context<TContextData>,
226+
identifier: string,
227+
): Promise<Actor | null> {
228+
if (this.identifier !== identifier) return null;
229+
const session = this.getSession(ctx);
230+
const summary = await this.#getSummary(session);
231+
const { pairs, tags } = await this.#getProperties(session);
232+
const allTags = summary == null ? tags : [...tags, ...summary.tags];
219233
const keyPairs = await ctx.getActorKeyPairs(identifier);
220234
return new this.class({
221235
id: ctx.getActorUri(identifier),
222236
preferredUsername: this.username,
223237
name: this.name,
224-
summary,
225-
tags,
238+
summary: summary == null ? null : summary.text,
239+
attachments: pairs,
240+
tags: allTags.filter((tag, i) =>
241+
allTags.findIndex((t) =>
242+
t.name?.toString() === tag.name?.toString() &&
243+
t.href?.href === tag.href?.href
244+
) === i
245+
),
226246
icon: new Image({ url: this.icon }),
227247
image: new Image({ url: this.image }),
228-
attachments,
229248
inbox: ctx.getInboxUri(identifier),
230249
endpoints: new Endpoints({
231250
sharedInbox: ctx.getInboxUri(),

0 commit comments

Comments
 (0)