Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
3.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {ResettableStates} from '@app/features/app/state/ResettableStates';
import GuildMembers from '@app/features/member/state/GuildMembers';
import {
	type CustomStatus,
	fromGatewayCustomStatus,
	type GatewayCustomStatusPayload,
} from '@app/features/user/state/CustomStatus';
import {isOfflineStatus, normalizeStatus, type StatusType} from '@fluxer/constants/src/StatusConstants';
import type {GuildMemberData} from '@fluxer/schema/src/domains/guild/GuildMemberSchemas';
import {makeAutoObservable, observable} from 'mobx';

const MAX_MEMBER_LIST_SUBSCRIPTIONS = 10;

export interface ThreadMemberListPayload {
	guild_id: string;
	thread_id: string;
	members: ReadonlyArray<{
		user_id: string;
		join_timestamp: string | null;
		flags: number;
		member: GuildMemberData | null;
		presence?: {
			status?: string | null;
			mobile?: boolean;
			custom_status?: GatewayCustomStatusPayload | null;
		} | null;
	}>;
}

export interface ThreadRosterMember {
	readonly userId: string;
	readonly joinTimestamp: string | null;
	readonly status: StatusType;
	readonly mobile: boolean;
	readonly customStatus: CustomStatus | null;
}

class ThreadRoster {
	private readonly rosters = observable.map<string, ReadonlyArray<ThreadRosterMember>>();
	private readonly subscriptionCounts = observable.map<string, {guildId: string; count: number}>();

	constructor() {
		makeAutoObservable<this, 'rosters' | 'subscriptionCounts'>(
			this,
			{rosters: false, subscriptionCounts: false},
			{autoBind: true},
		);
	}

	getMembers(threadId: string): ReadonlyArray<ThreadRosterMember> | undefined {
		return this.rosters.get(threadId);
	}

	subscribedThreadIds(guildId: string): ReadonlyArray<string> {
		const ids: Array<string> = [];
		for (const [threadId, entry] of this.subscriptionCounts) {
			if (entry.guildId === guildId) ids.push(threadId);
		}
		ids.sort();
		return ids.slice(0, MAX_MEMBER_LIST_SUBSCRIPTIONS);
	}

	subscribe(guildId: string, threadId: string): () => void {
		const entry = this.subscriptionCounts.get(threadId);
		this.subscriptionCounts.set(threadId, {guildId, count: (entry?.count ?? 0) + 1});
		return () => this.release(threadId);
	}

	private release(threadId: string): void {
		const entry = this.subscriptionCounts.get(threadId);
		if (!entry) return;
		if (entry.count <= 1) {
			this.subscriptionCounts.delete(threadId);
			queueMicrotask(() => this.dropUnsubscribedRoster(threadId));
			return;
		}
		this.subscriptionCounts.set(threadId, {...entry, count: entry.count - 1});
	}

	private dropUnsubscribedRoster(threadId: string): void {
		if (!this.subscriptionCounts.has(threadId)) this.rosters.delete(threadId);
	}

	handleListUpdate(payload: ThreadMemberListPayload): void {
		if (!this.subscriptionCounts.has(payload.thread_id)) return;
		const members: Array<ThreadRosterMember> = [];
		for (const entry of payload.members) {
			if (entry.member) {
				GuildMembers.hydrateOrRefresh(payload.guild_id, entry.member);
			}
			const status = normalizeStatus(entry.presence?.status);
			members.push({
				userId: entry.user_id,
				joinTimestamp: entry.join_timestamp,
				status,
				mobile: !isOfflineStatus(status) && entry.presence?.mobile === true,
				customStatus: isOfflineStatus(status) ? null : fromGatewayCustomStatus(entry.presence?.custom_status),
			});
		}
		this.rosters.set(payload.thread_id, members);
	}

	reset(): void {
		this.rosters.clear();
	}
}

const threadRoster = new ThreadRoster();

ResettableStates.register(threadRoster, () => threadRoster.reset());

export default threadRoster;