Skip to content

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

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

import type {UserID} from '@app/api/BrandedTypes';
import type {
	OperatorPassOfferRow,
	OperatorPassOfferStatus,
	OperatorPassPurchaseRow,
	OperatorPassUnitRow,
} from '@app/api/database/types/OperatorPassTypes';
import type {User} from '@app/api/models/User';
import {
	isOperatorPassRemovalActive,
	loadOperatorPassEntitlementState,
	resolveOperatorPassHolder,
} from '@app/api/operator_pass/OperatorPassEntitlementService';
import {
	getLabsGuildId,
	getLabsOperatorsChannelId,
	hidesOperatorsChannel,
} from '@app/api/operator_pass/OperatorPassLabsRole';
import {
	getOperatorPassCertificateUrl,
	getOperatorPassWithdrawalDeadline,
	getOperatorPassWithdrawalEndsOn,
	isOperatorPassSinglePassWithdrawable,
} from '@app/api/operator_pass/OperatorPassPurchaseRules';
import type {OperatorPassRepository} from '@app/api/operator_pass/OperatorPassRepository';
import {operatorPassNow} from '@app/api/operator_pass/OperatorPassSchedule';
import {
	describeOperatorPassVisionary,
	isOperatorPassVisionary,
	resolveOperatorPassVisionarySince,
} from '@app/api/operator_pass/OperatorPassVisionaryService';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import {UnknownOperatorPassOfferError} from '@fluxer/errors/src/domains/operator_pass/UnknownOperatorPassOfferError';
import type {
	OperatorPassOfferResponse,
	OperatorPassPassesResponse,
	OperatorPassUnitResponse,
} from '@fluxer/schema/src/domains/operator_pass/OperatorPassSchemas';

interface OperatorPassHoldingQueryDependencies {
	repository: OperatorPassRepository;
	userRepository: UserRepository;
	findPurchase: (purchaseId: string) => Promise<OperatorPassPurchaseRow | null>;
	isLabsMember: (userId: UserID) => Promise<boolean>;
}

const OPEN_OFFER_STATUSES: ReadonlyArray<OperatorPassOfferStatus> = ['pending', 'accepted'];

export function isOperatorPassOfferOpen(offer: OperatorPassOfferRow): boolean {
	return OPEN_OFFER_STATUSES.includes(offer.status);
}

function mapOperatorPassOfferUser(user: User | undefined): OperatorPassOfferResponse['sender'] {
	if (!user) {
		return null;
	}
	return {
		id: user.id.toString(),
		username: user.username,
		global_name: user.globalName ?? null,
		avatar: user.avatarHash ?? null,
	};
}

function mapOperatorPassOffer(
	offer: OperatorPassOfferRow,
	users: ReadonlyMap<UserID, User>,
): OperatorPassOfferResponse {
	return {
		id: offer.offer_id,
		pass_id: offer.pass_id,
		status: offer.status,
		sender: mapOperatorPassOfferUser(users.get(offer.sender_id)),
		recipient: mapOperatorPassOfferUser(users.get(offer.recipient_id)),
		created_at: offer.created_at.toISOString(),
		accepted_at: offer.accepted_at?.toISOString() ?? null,
		ended_at: offer.ended_at?.toISOString() ?? null,
	};
}

export class OperatorPassHoldingQueryService {
	constructor(private readonly deps: OperatorPassHoldingQueryDependencies) {}

	async listHeldUnits(userId: UserID): Promise<Array<OperatorPassUnitRow>> {
		return (await this.deps.repository.listUnitsByHolder(userId)).filter((unit) => unit.status === 'held');
	}

	async requireOffer(offerId: string): Promise<OperatorPassOfferRow> {
		const offer = await this.deps.repository.findOffer(offerId);
		if (!offer) {
			throw new UnknownOperatorPassOfferError();
		}
		return offer;
	}

	async describePasses(user: User): Promise<OperatorPassPassesResponse> {
		const now = operatorPassNow();
		const [units, offers, state, activation, labs, labsMember] = await Promise.all([
			this.listHeldUnits(user.id),
			this.deps.repository.listOffersByUser(user.id),
			loadOperatorPassEntitlementState(this.deps.repository, user.id),
			this.deps.repository.findVisionaryActivation(user.id),
			this.deps.repository.findLabsMember(user.id),
			this.deps.isLabsMember(user.id),
		]);
		const open = offers.filter(isOperatorPassOfferOpen);
		const completed = offers
			.filter((offer) => offer.status === 'completed')
			.sort((left, right) => (left.ended_at?.getTime() ?? 0) - (right.ended_at?.getTime() ?? 0));
		const users = await this.loadOfferUsers([...open, ...completed]);
		const purchases = new Map<string, OperatorPassPurchaseRow | null>();
		for (const purchaseId of new Set(units.map((unit) => unit.purchase_id).filter((id) => id !== null))) {
			purchases.set(purchaseId, await this.deps.findPurchase(purchaseId));
		}
		units.sort((left, right) => left.delivered_at.getTime() - right.delivered_at.getTime());
		const certificates = await Promise.all(
			units.map((unit) => this.deps.repository.findLatestCertificateLink(unit.pass_id)),
		);
		const passes: Array<OperatorPassUnitResponse> = units.map((unit, index) => {
			const purchase = unit.purchase_id ? (purchases.get(unit.purchase_id) ?? null) : null;
			const canPassOn = unit.transferable && unit.source === 'purchase';
			const withdrawable = purchase !== null && isOperatorPassSinglePassWithdrawable(purchase, unit, now);
			const offer = open.find((candidate) => candidate.pass_id === unit.pass_id && candidate.sender_id === user.id);
			const received = completed.findLast(
				(candidate) => candidate.pass_id === unit.pass_id && candidate.recipient_id === user.id,
			);
			return {
				id: unit.pass_id,
				source: unit.source,
				status: unit.status,
				purchase_id: unit.purchase_id,
				delivered_at: unit.delivered_at.toISOString(),
				purchased_at:
					unit.source === 'purchase' && purchase?.purchaser_id === user.id
						? (purchase.purchased_at?.toISOString() ?? null)
						: null,
				paused: unit.paused_at != null,
				can_pass_on: canPassOn,
				can_withdraw: withdrawable,
				withdrawal_ends_at:
					purchase?.withdrawal_days != null && unit.source === 'purchase'
						? (getOperatorPassWithdrawalDeadline(purchase)?.toISOString() ?? null)
						: null,
				withdrawal_ends_on:
					purchase?.withdrawal_days != null && purchase.withdrawal_ends_at && unit.source === 'purchase'
						? getOperatorPassWithdrawalEndsOn(purchase.withdrawal_ends_at)
						: null,
				offer: offer ? mapOperatorPassOffer(offer, users) : null,
				received_from: received ? mapOperatorPassOfferUser(users.get(received.sender_id)) : null,
				certificate_number: certificates[index]?.number ?? null,
			};
		});
		const visionaryPage =
			activation?.pass_id && activation.completed_at && !activation.ended_at
				? await this.deps.repository.findLatestCertificateLink(activation.pass_id)
				: null;
		const visionary =
			activation?.activated_at || isOperatorPassVisionary(user)
				? describeOperatorPassVisionary({
						user,
						visionarySince:
							activation?.visionary_since ?? (await resolveOperatorPassVisionarySince(this.deps.userRepository, user)),
						activation,
						removal: state.removal,
						labsMember,
						certificateUrl:
							visionaryPage?.link_token && visionaryPage.cancelled_at == null
								? getOperatorPassCertificateUrl(visionaryPage.link_token)
								: null,
					})
				: undefined;
		return {
			...(visionary ? {visionary} : {}),
			count: isOperatorPassRemovalActive(state.removal, now) ? 0 : units.length,
			recognition_count: resolveOperatorPassHolder(state, now)?.count ?? 0,
			passes,
			incoming_offers: open
				.filter((offer) => offer.recipient_id === user.id && offer.sender_id !== user.id)
				.map((offer) => mapOperatorPassOffer(offer, users)),
			outgoing_offers: open
				.filter((offer) => offer.sender_id === user.id)
				.map((offer) => mapOperatorPassOffer(offer, users)),
			passed_on: completed
				.filter((offer) => offer.sender_id === user.id && offer.recipient_id !== user.id)
				.map((offer) => mapOperatorPassOffer(offer, users)),
			labs: {
				greeting: labs?.greeting !== false,
				show_operators: !hidesOperatorsChannel(user),
				greeted: labs?.greeted_at != null,
				left: labs?.left_at != null,
				member: labsMember,
				guild_id: getLabsGuildId()?.toString() ?? null,
				operators_channel_id: getLabsOperatorsChannelId()?.toString() ?? null,
			},
		};
	}

	async describeOffer(user: User, offerId: string): Promise<OperatorPassOfferResponse> {
		const offer = await this.requireOffer(offerId);
		if (offer.sender_id !== user.id && offer.recipient_id !== user.id) {
			throw new UnknownOperatorPassOfferError();
		}
		return mapOperatorPassOffer(offer, await this.loadOfferUsers([offer]));
	}

	private async loadOfferUsers(offers: ReadonlyArray<OperatorPassOfferRow>): Promise<Map<UserID, User>> {
		const ids = new Set<UserID>();
		for (const offer of offers) {
			ids.add(offer.sender_id);
			ids.add(offer.recipient_id);
		}
		const users = new Map<UserID, User>();
		for (const id of ids) {
			const found = await this.deps.userRepository.findUnique(id);
			if (found) {
				users.set(id, found);
			}
		}
		return users;
	}
}