Skip to content

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

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

import Channels from '@app/features/channel/state/Channels';
import Guilds from '@app/features/guild/state/Guilds';
import GuildMembers from '@app/features/member/state/GuildMembers';
import type {Message as MessageModel} from '@app/features/messaging/models/MessagingMessage';
import Messages from '@app/features/messaging/state/MessagingMessages';
import {resolveReadStateEntryStatus} from '@app/features/read_state/state/read_states/ReadStateEntryStatusMachine';
import {resolveReadStateMention} from '@app/features/read_state/state/read_states/ReadStateMentionMachine';
import {
	compareMessageIds,
	normalizeCount,
	parseTimestamp,
	snowflakeTimestamp,
} from '@app/features/read_state/state/read_states/shared';
import Relationships from '@app/features/relationship/state/Relationships';
import ThreadMemberships from '@app/features/threads/state/ThreadMemberships';
import UserGuildSettings from '@app/features/user/state/UserGuildSettings';
import Users from '@app/features/user/state/Users';
import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
import type {Message as WireMessage} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';

export class ReadStateEntry {
	readonly channelId: string;
	storedGuildId: string | null = null;
	messagesLoaded = false;
	private storedLastMessageId: string | null = null;
	private storedLastMessageTimestamp = 0;
	private storedAckMessageId: string | null = null;
	private storedAckMessageTimestamp = 0;
	acknowledgedPinTimestamp = 0;
	lastPinTimestamp = 0;
	ackedManually = false;
	private storedOldestUnreadMessageId: string | null = null;
	oldestUnreadNeedsRecompute = false;
	private storedStickyUnreadMessageId: string | null = null;
	estimated = false;
	private storedUnreadCount = 0;
	private storedMentionCount = 0;
	inFlightAckMessageId: string | null = null;
	serverVersion: string | null = null;
	constructor(channelId: string) {
		this.channelId = channelId;
	}

	get guildId(): string | null {
		const channel = Channels.getChannel(this.channelId);
		return channel?.guildId ?? this.storedGuildId ?? null;
	}

	get lastMessageId(): string | null {
		return this.storedLastMessageId;
	}

	set lastMessageId(messageId: string | null) {
		this.storedLastMessageId = messageId;
		this.storedLastMessageTimestamp = snowflakeTimestamp(messageId);
	}

	get ackMessageId(): string | null {
		return this.storedAckMessageId;
	}

	set ackMessageId(messageId: string | null) {
		this.storedAckMessageId = messageId;
		this.storedAckMessageTimestamp = snowflakeTimestamp(messageId);
	}

	get oldestUnreadMessageId(): string | null {
		return this.storedOldestUnreadMessageId;
	}

	set oldestUnreadMessageId(messageId: string | null) {
		this.storedOldestUnreadMessageId = messageId;
		this.oldestUnreadNeedsRecompute = false;
	}

	get stickyUnreadMessageId(): string | null {
		return this.storedStickyUnreadMessageId;
	}

	set stickyUnreadMessageId(messageId: string | null) {
		this.storedStickyUnreadMessageId = messageId;
	}

	get visualUnreadMessageId(): string | null {
		return this.storedStickyUnreadMessageId ?? this.storedOldestUnreadMessageId;
	}

	clearStickyUnread(): void {
		this.storedStickyUnreadMessageId = null;
	}

	get unreadCount(): number {
		return this.storedUnreadCount;
	}

	set unreadCount(count: number) {
		this.storedUnreadCount = normalizeCount(count);
	}

	get mentionCount(): number {
		return this.storedMentionCount;
	}

	set mentionCount(count: number) {
		this.storedMentionCount = normalizeCount(count);
	}

	get ackTimestamp(): number {
		if (this.storedAckMessageTimestamp !== 0 && !Number.isNaN(this.storedAckMessageTimestamp)) {
			return this.storedAckMessageTimestamp;
		}
		return this.ackFloorTimestamp;
	}

	private get ackFloorTimestamp(): number {
		const guildId = this.guildId;
		if (guildId != null) {
			const joinedAt = parseTimestamp(Guilds.getGuild(guildId)?.joinedAt);
			if (joinedAt !== 0) {
				return joinedAt;
			}
		}
		return snowflakeTimestamp(this.channelId);
	}

	isNewerThanAck(messageId: string): boolean {
		if (this.storedAckMessageId != null) {
			return compareMessageIds(messageId, this.storedAckMessageId) > 0;
		}
		return snowflakeTimestamp(messageId) > this.ackTimestamp;
	}

	get isPrivate(): boolean {
		const channel = Channels.getChannel(this.channelId);
		return channel?.isPrivate() ?? false;
	}

	supportsUnreadTracking(): boolean {
		const channel = Channels.getChannel(this.channelId);
		const guildId = channel?.guildId ?? this.storedGuildId;
		if (guildId != null && Guilds.getGuild(guildId)?.visiting === true) return false;
		return channel != null || this.storedGuildId != null;
	}

	private get statusModel() {
		return resolveReadStateEntryStatus({
			supportsUnreadTracking: this.supportsUnreadTracking(),
			hasBlockedDirectMessageRecipient: this.hasBlockedDirectMessageRecipient(),
			lastMessageId: this.storedLastMessageId,
			ackMessageId: this.storedAckMessageId,
			ackTimestamp: this.ackTimestamp,
			lastMessageTimestamp: this.storedLastMessageTimestamp,
			mentionCount: this.mentionCount,
		});
	}

	canBeUnread(): boolean {
		return this.statusModel.canBeUnread;
	}

	supportsMentions(): boolean {
		return this.statusModel.supportsMentions;
	}

	hasUnread(): boolean {
		return this.statusModel.hasUnread;
	}

	private hasBlockedDirectMessageRecipient(): boolean {
		const channel = Channels.getChannel(this.channelId);
		if (channel?.type !== ChannelTypes.DM) {
			return false;
		}
		const recipientId = channel.getRecipientId();
		return recipientId != null && Relationships.isBlocked(recipientId);
	}

	hasMentions(): boolean {
		return this.mentionCount > 0;
	}

	isUnreadOrMentioned(): boolean {
		return this.statusModel.isUnreadOrMentioned;
	}

	rebuild(
		ackMessageId?: string | null,
		{
			recomputeMentions = false,
		}: {
			recomputeMentions?: boolean;
		} = {},
	): void {
		const previousUnreadCount = this.storedUnreadCount;
		const previousOldestUnreadMessageId = this.storedOldestUnreadMessageId;
		if (ackMessageId != null) {
			this.ackMessageId = ackMessageId;
		} else {
			this.ackMessageId = this.storedAckMessageId ?? this.guessAckMessageId();
		}
		this.oldestUnreadMessageId = null;
		this.estimated = false;
		this.unreadCount = 0;
		if (recomputeMentions) {
			this.mentionCount = 0;
		}
		if (!this.hasUnread()) {
			return;
		}
		const currentUser = Users.getCurrentUser();
		if (currentUser == null) {
			return;
		}
		const messages = Messages.getMessages(this.channelId);
		const isPrivate = this.isPrivate;
		const userId = currentUser.id;
		const guildId = this.guildId;
		const channelId = this.channelId;
		const suppressEveryone = recomputeMentions ? UserGuildSettings.isEveryoneMentionSuppressed(guildId) : false;
		const suppressRoles = recomputeMentions ? UserGuildSettings.isRoleMentionSuppressed(guildId) : false;
		const isMuted = recomputeMentions ? UserGuildSettings.isGuildOrChannelMuted(guildId, channelId) : false;
		const member = recomputeMentions && guildId ? GuildMembers.getMember(guildId, userId) : null;
		const memberRoles = member?.roles ?? null;
		let foundAckMessage = false;
		let loadedOlderMessages = false;
		let oldestUnread: string | null = null;
		let loadedUnreadCount = 0;
		messages.forEachBuffered((message) => {
			if (!foundAckMessage) {
				foundAckMessage = message.id === this.storedAckMessageId;
			} else if (this.storedOldestUnreadMessageId == null) {
				this.storedOldestUnreadMessageId = message.id;
			}
			if (this.isNewerThanAck(message.id)) {
				loadedUnreadCount++;
				if (recomputeMentions && !Relationships.isBlocked(message.author.id)) {
					const mentions = message.mentions;
					const mentionEveryone = message.mentionEveryone;
					const mentionRoles = message.mentionRoles;
					const hasUserMention = mentions?.some((m) => m.id === userId) ?? false;
					const hasEveryoneMention = !suppressEveryone && !!mentionEveryone && !this.isNonMemberThread();
					const hasRoleMention = !suppressRoles && hasMatchingRoleMention(mentionRoles, memberRoles);
					const mention = resolveReadStateMention({
						authorBlocked: false,
						hasUserMention,
						hasEveryoneMention,
						hasRoleMention,
						isPrivate,
						isMuted,
					});
					if (mention.shouldMention) {
						this.mentionCount++;
					}
				}
				oldestUnread ??= message.id;
			} else {
				loadedOlderMessages = true;
			}
		});
		const hasUnreadBoundary = foundAckMessage || loadedOlderMessages || !messages.hasMoreBefore;
		const hasNewestMessages = messages.hasNewestMessages();
		this.estimated = !hasNewestMessages || (!hasUnreadBoundary && messages.length === loadedUnreadCount);
		if (this.estimated) {
			this.unreadCount = Math.max(previousUnreadCount, loadedUnreadCount);
		} else {
			this.unreadCount = loadedUnreadCount;
		}
		const resolvedOldestUnread = this.storedOldestUnreadMessageId ?? oldestUnread;
		this.oldestUnreadMessageId =
			resolvedOldestUnread ?? (this.estimated && this.unreadCount > 0 ? previousOldestUnreadMessageId : null);
	}

	private guessAckMessageId(): string | null {
		if (!this.isPrivate) {
			return null;
		}
		const messages = Messages.getMessages(this.channelId);
		if (!messages.hasNewestMessages()) {
			return null;
		}
		if (!this.hasMentions()) {
			return this.storedLastMessageId;
		}
		const currentUserId = Users.getCurrentUser()?.id;
		const candidates: Array<{id: string; authorId: string}> = [];
		messages.forEachBuffered((message) => {
			candidates.push({id: message.id, authorId: message.author.id});
		});
		let remaining = this.mentionCount;
		for (let index = candidates.length - 1; index >= 0; index--) {
			const candidate = candidates[index];
			if (remaining > 0 && candidate.authorId !== currentUserId) {
				remaining--;
			} else if (remaining === 0) {
				return candidate.id;
			}
		}
		return null;
	}

	shouldMentionFor(message: MessageModel | WireMessage, userId: string, isPrivate: boolean): boolean {
		const authorBlocked = Relationships.isBlocked(message.author.id);
		const suppressEveryone = UserGuildSettings.isEveryoneMentionSuppressed(this.guildId);
		const suppressRoles = UserGuildSettings.isRoleMentionSuppressed(this.guildId);
		const mentions = message.mentions;
		const mentionEveryone = 'mentionEveryone' in message ? message.mentionEveryone : message.mention_everyone;
		const mentionRoles = 'mentionRoles' in message ? message.mentionRoles : message.mention_roles;
		const hasUserMention = mentions?.some((m) => m.id === userId) ?? false;
		const hasEveryoneMention = !suppressEveryone && !!mentionEveryone && !this.isNonMemberThread();
		const hasRoleMention = !suppressRoles && this.hasMatchingMemberRoleMention(userId, mentionRoles);
		const isMuted = UserGuildSettings.isGuildOrChannelMuted(this.guildId, this.channelId);
		return resolveReadStateMention({
			authorBlocked,
			hasUserMention,
			hasEveryoneMention,
			hasRoleMention,
			isPrivate,
			isMuted,
		}).shouldMention;
	}

	private isNonMemberThread(): boolean {
		return Channels.getChannel(this.channelId)?.isThread() === true && !ThreadMemberships.isMember(this.channelId);
	}

	private hasMatchingMemberRoleMention(userId: string, mentionRoles?: ReadonlyArray<string> | null): boolean {
		const guildId = this.guildId;
		if (!guildId) return false;
		const member = GuildMembers.getMember(guildId, userId);
		return hasMatchingRoleMention(mentionRoles, member?.roles ?? null);
	}

	computeMentionCountAfterAck(messageId: string): number {
		const currentUser = Users.getCurrentUser();
		if (currentUser == null) {
			return 0;
		}
		const ackTimestamp = snowflakeTimestamp(messageId);
		if (ackTimestamp === 0 || Number.isNaN(ackTimestamp)) {
			return 0;
		}
		const messages = Messages.getMessages(this.channelId);
		const isPrivate = this.isPrivate;
		let mentionCount = 0;
		messages.forEachBuffered((message) => {
			if (snowflakeTimestamp(message.id) <= ackTimestamp) {
				return;
			}
			if (this.shouldMentionFor(message, currentUser.id, isPrivate)) {
				mentionCount++;
			}
		});
		return mentionCount;
	}
}

function hasMatchingRoleMention(
	mentionRoles: ReadonlyArray<string> | null | undefined,
	memberRoles: ReadonlySet<string> | null,
): boolean {
	if (memberRoles == null || mentionRoles == null || mentionRoles.length === 0) return false;
	for (const roleId of mentionRoles) {
		if (memberRoles.has(roleId)) return true;
	}
	return false;
}