Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

217 lines
5.2 KiB
TypeScript

import type { IUser, UserPresence } from '@rocket.chat/core-typings';
import { UserStatus } from '@rocket.chat/core-typings';
import type { EventHandlerOf } from '@rocket.chat/emitter';
import { Emitter } from '@rocket.chat/emitter';
import { Meteor } from 'meteor/meteor';
import { sdk } from './SDKClient';
import { getDdpSdk } from './sdk/ddpSdk';
import { isSdkTransportEnabled } from './sdk/sdkTransportEnabled';
const sdkTransportEnabled = isSdkTransportEnabled();
const subscribeUserPresence = (payload: { added?: string[]; removed?: string[] }): void => {
if (!sdkTransportEnabled) {
// Flag off: route directly through Meteor.subscribe — bit-for-bit develop
// behaviour, no DDPSDK socket created, no proxy in the call path.
Meteor.subscribe('stream-user-presence', '', payload);
return;
}
const ddp = getDdpSdk();
if (ddp.connection.status === 'connected' && ddp.account.uid) {
// Fire the command-style subscription over our SDK; it has no lifecycle
// (the server registers the added/removed uids and moves on), matching
// Meteor.subscribe's behaviour here.
ddp.client.subscribe('stream-user-presence', '', payload);
return;
}
Meteor.subscribe('stream-user-presence', '', payload);
};
type InternalEvents = {
remove: IUser['_id'];
reset: undefined;
restart: undefined;
};
type ExternalEvents = {
[key: string]: UserPresence | undefined;
};
type Events = InternalEvents & ExternalEvents;
const emitter = new Emitter<Events>();
const store = new Map<string, UserPresence>();
const isUid = (eventType: keyof Events): eventType is UserPresence['_id'] =>
Boolean(eventType) && typeof eventType === 'string' && !['reset', 'restart', 'remove'].includes(eventType);
const uids = new Set<UserPresence['_id']>();
const update: EventHandlerOf<ExternalEvents, string> = (update) => {
if (update?._id) {
store.set(update._id, { ...store.get(update._id), ...update, ...(status === 'disabled' && { status: UserStatus.DISABLED }) });
uids.delete(update._id);
}
};
const notify = (presence: UserPresence): void => {
if (presence._id) {
update(presence);
emitter.emit(presence._id, store.get(presence._id));
}
};
const getPresence = ((): ((uid: UserPresence['_id']) => void) => {
let timer: ReturnType<typeof setTimeout>;
const deletedUids = new Set<UserPresence['_id']>();
const fetch = (delay = 500): void => {
timer && clearTimeout(timer);
timer = setTimeout(async () => {
const currentUids = new Set(uids);
uids.clear();
const ids = Array.from(currentUids);
const removed = Array.from(deletedUids);
subscribeUserPresence({
...(ids.length > 0 && { added: Array.from(currentUids) }),
...(removed.length && { removed: Array.from(deletedUids) }),
});
deletedUids.clear();
if (ids.length === 0) {
return;
}
try {
const params = {
ids: [...currentUids],
};
const { users } = await sdk.rest.get('/v1/users.presence', params);
const fallbackStatus = status === 'disabled' ? UserStatus.DISABLED : UserStatus.OFFLINE;
users.forEach(({ statusExpiresAt, ...rest }) => {
if (!store.has(rest._id)) {
notify({
...rest,
...(statusExpiresAt && { statusExpiresAt: new Date(statusExpiresAt) }),
});
}
currentUids.delete(rest._id);
});
currentUids.forEach((uid) => {
notify({ _id: uid, status: fallbackStatus });
});
currentUids.clear();
} catch {
fetch(delay + delay);
} finally {
currentUids.forEach((item) => uids.add(item));
}
}, delay);
};
const get = (uid: UserPresence['_id']): void => {
uids.add(uid);
fetch();
};
const stop = (uid: UserPresence['_id']): void => {
deletedUids.add(uid);
fetch();
};
emitter.on('remove', (uid) => {
if (emitter.has(uid)) {
return;
}
store.delete(uid);
stop(uid);
});
emitter.on('reset', () => {
emitter
.events()
.filter(isUid)
.forEach((uid) => {
emitter.emit(uid, undefined);
});
emitter.once('restart', () => {
emitter.events().filter(isUid).forEach(get);
});
});
return get;
})();
const listen = (uid: UserPresence['_id'], handler: EventHandlerOf<ExternalEvents, UserPresence['_id']> | (() => void)): void => {
if (!uid) {
return;
}
emitter.on(uid, handler);
const user = store.has(uid) && store.get(uid);
if (user) {
return;
}
getPresence(uid);
};
const stop = (uid: UserPresence['_id'], handler: EventHandlerOf<ExternalEvents, UserPresence['_id']> | (() => void)): void => {
emitter.off(uid, handler);
setTimeout(() => {
emitter.emit('remove', uid);
}, 5000);
};
const reset = (): void => {
store.clear();
emitter.emit('reset');
};
const restart = (): void => {
emitter.emit('restart');
};
const get = async (uid: UserPresence['_id']): Promise<UserPresence | undefined> =>
new Promise((resolve) => {
const user = store.has(uid) && store.get(uid);
if (user) {
return resolve(user);
}
const callback: EventHandlerOf<ExternalEvents, UserPresence['_id']> = (args): void => {
resolve(args);
stop(uid, callback);
};
listen(uid, callback);
});
let status = 'enabled';
const setStatus = (newStatus: 'enabled' | 'disabled'): void => {
status = newStatus;
reset();
};
export const Presence = {
setStatus,
status,
listen,
stop,
reset,
restart,
notify,
store,
get,
};