refactor!: rename chat providers to channels
This commit is contained in:
52
src/infra/channel-activity.ts
Normal file
52
src/infra/channel-activity.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import type { ChannelId } from "../channels/plugins/types.js";
|
||||
export type ChannelDirection = "inbound" | "outbound";
|
||||
|
||||
type ActivityEntry = {
|
||||
inboundAt: number | null;
|
||||
outboundAt: number | null;
|
||||
};
|
||||
|
||||
const activity = new Map<string, ActivityEntry>();
|
||||
|
||||
function keyFor(channel: ChannelId, accountId: string) {
|
||||
return `${channel}:${accountId || "default"}`;
|
||||
}
|
||||
|
||||
function ensureEntry(channel: ChannelId, accountId: string): ActivityEntry {
|
||||
const key = keyFor(channel, accountId);
|
||||
const existing = activity.get(key);
|
||||
if (existing) return existing;
|
||||
const created: ActivityEntry = { inboundAt: null, outboundAt: null };
|
||||
activity.set(key, created);
|
||||
return created;
|
||||
}
|
||||
|
||||
export function recordChannelActivity(params: {
|
||||
channel: ChannelId;
|
||||
accountId?: string | null;
|
||||
direction: ChannelDirection;
|
||||
at?: number;
|
||||
}) {
|
||||
const at = typeof params.at === "number" ? params.at : Date.now();
|
||||
const accountId = params.accountId?.trim() || "default";
|
||||
const entry = ensureEntry(params.channel, accountId);
|
||||
if (params.direction === "inbound") entry.inboundAt = at;
|
||||
if (params.direction === "outbound") entry.outboundAt = at;
|
||||
}
|
||||
|
||||
export function getChannelActivity(params: {
|
||||
channel: ChannelId;
|
||||
accountId?: string | null;
|
||||
}): ActivityEntry {
|
||||
const accountId = params.accountId?.trim() || "default";
|
||||
return (
|
||||
activity.get(keyFor(params.channel, accountId)) ?? {
|
||||
inboundAt: null,
|
||||
outboundAt: null,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function resetChannelActivityForTest() {
|
||||
activity.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user