



























@@ -1,98 +1,16 @@
11import { resolveComparableTargetForLoadedChannel } from "../channels/plugins/target-parsing-loaded.js";
2+import { normalizeOptionalString } from "../shared/string-coerce.js";
23import {
3-normalizeOptionalLowercaseString,
4-normalizeOptionalString,
5-normalizeOptionalThreadValue,
6-} from "../shared/string-coerce.js";
4+deliveryContextFromSession,
5+mergeDeliveryContext,
6+normalizeDeliveryContext,
7+} from "../utils/delivery-context.shared.js";
8+import type {
9+DeliveryContext,
10+DeliveryContextSessionSource,
11+} from "../utils/delivery-context.types.js";
712import { isInternalMessageChannel } from "../utils/message-channel.js";
8-9-export type DeliveryContext = {
10-channel?: string;
11-to?: string;
12-accountId?: string;
13-threadId?: string | number;
14-};
15-16-type DeliveryContextSource = {
17-channel?: string;
18-lastChannel?: string;
19-lastTo?: string;
20-lastAccountId?: string;
21-lastThreadId?: string | number;
22-origin?: {
23-provider?: string;
24-accountId?: string;
25-threadId?: string | number;
26-};
27-deliveryContext?: DeliveryContext;
28-};
29-30-function normalizeDeliveryContext(context?: DeliveryContext): DeliveryContext | undefined {
31-if (!context) {
32-return undefined;
33-}
34-const normalized: DeliveryContext = {
35-channel: normalizeOptionalLowercaseString(context.channel),
36-to: normalizeOptionalString(context.to),
37-accountId: normalizeOptionalString(context.accountId),
38-};
39-const threadId = normalizeOptionalThreadValue(context.threadId);
40-if (threadId != null) {
41-normalized.threadId = threadId;
42-}
43-if (
44-!normalized.channel &&
45-!normalized.to &&
46-!normalized.accountId &&
47-normalized.threadId == null
48-) {
49-return undefined;
50-}
51-return normalized;
52-}
53-54-function mergeDeliveryContext(
55-primary?: DeliveryContext,
56-fallback?: DeliveryContext,
57-): DeliveryContext | undefined {
58-const normalizedPrimary = normalizeDeliveryContext(primary);
59-const normalizedFallback = normalizeDeliveryContext(fallback);
60-if (!normalizedPrimary && !normalizedFallback) {
61-return undefined;
62-}
63-const channelsConflict =
64-normalizedPrimary?.channel &&
65-normalizedFallback?.channel &&
66-normalizedPrimary.channel !== normalizedFallback.channel;
67-return normalizeDeliveryContext({
68-channel: normalizedPrimary?.channel ?? normalizedFallback?.channel,
69-to: channelsConflict
70- ? normalizedPrimary?.to
71- : (normalizedPrimary?.to ?? normalizedFallback?.to),
72-accountId: channelsConflict
73- ? normalizedPrimary?.accountId
74- : (normalizedPrimary?.accountId ?? normalizedFallback?.accountId),
75-threadId: channelsConflict
76- ? normalizedPrimary?.threadId
77- : (normalizedPrimary?.threadId ?? normalizedFallback?.threadId),
78-});
79-}
80-81-function deliveryContextFromSession(entry?: DeliveryContextSource): DeliveryContext | undefined {
82-if (!entry) {
83-return undefined;
84-}
85-return normalizeDeliveryContext({
86-channel:
87-entry.deliveryContext?.channel ??
88-entry.lastChannel ??
89-entry.channel ??
90-entry.origin?.provider,
91-to: entry.deliveryContext?.to ?? entry.lastTo,
92-accountId: entry.deliveryContext?.accountId ?? entry.lastAccountId ?? entry.origin?.accountId,
93-threadId: entry.deliveryContext?.threadId ?? entry.lastThreadId ?? entry.origin?.threadId,
94-});
95-}
13+export type { DeliveryContext } from "../utils/delivery-context.types.js";
96149715function stripThreadRouteSuffix(target: string): string {
9816return /^(.*):topic:[^:]+$/u.exec(target)?.[1] ?? target;
@@ -103,7 +21,7 @@ function normalizeAnnounceRouteTarget(context?: DeliveryContext): string | undef
10321if (!rawTo) {
10422return undefined;
10523}
106-const channel = normalizeOptionalLowercaseString(context?.channel);
24+const channel = normalizeOptionalString(context?.channel);
10725const parsed = channel
10826 ? resolveComparableTargetForLoadedChannel({
10927 channel,
@@ -141,7 +59,7 @@ function shouldStripThreadFromAnnounceEntry(
14159}
1426014361export function resolveAnnounceOrigin(
144-entry?: DeliveryContextSource,
62+entry?: DeliveryContextSessionSource,
14563requesterOrigin?: DeliveryContext,
14664): DeliveryContext | undefined {
14765const normalizedRequester = normalizeDeliveryContext(requesterOrigin);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。