Skip to content

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

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

import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
import {noteText} from '@app/features/theme/fonts/ScriptFontLoader';
import type {GuildRole as WireGuildRole} from '@fluxer/schema/src/domains/guild/GuildRoleSchemas';

interface GuildRoleRecordOptions {
	instanceId?: string;
}

export class GuildRole {
	readonly instanceId: string;
	readonly id: string;
	readonly guildId: string;
	readonly name: string;
	readonly color: number;
	readonly position: number;
	readonly hoistPosition: number | null;
	readonly permissions: bigint;
	readonly hoist: boolean;
	readonly mentionable: boolean;
	readonly icon: string | null;
	readonly unicodeEmoji: string | null;

	constructor(guildId: string, guildRole: WireGuildRole, options?: GuildRoleRecordOptions) {
		this.instanceId = options?.instanceId ?? RuntimeConfig.localInstanceDomain;
		this.id = guildRole.id;
		this.guildId = guildId;
		this.name = guildRole.name;
		this.color = guildRole.color;
		this.position = guildRole.position;
		this.hoistPosition = guildRole.hoist_position ?? null;
		this.permissions = BigInt(guildRole.permissions);
		this.hoist = guildRole.hoist;
		this.mentionable = guildRole.mentionable;
		this.icon = guildRole.icon ?? null;
		this.unicodeEmoji = guildRole.unicode_emoji ?? null;
		noteText(this.name);
	}

	get effectiveHoistPosition(): number {
		return this.hoistPosition ?? this.position;
	}

	withUpdates(updates: Partial<WireGuildRole>): GuildRole {
		return new GuildRole(
			this.guildId,
			{
				id: this.id,
				name: updates.name ?? this.name,
				color: updates.color ?? this.color,
				position: updates.position ?? this.position,
				hoist_position: updates.hoist_position !== undefined ? updates.hoist_position : this.hoistPosition,
				permissions: updates.permissions ?? this.permissions.toString(),
				hoist: updates.hoist ?? this.hoist,
				mentionable: updates.mentionable ?? this.mentionable,
				icon: updates.icon !== undefined ? updates.icon : this.icon,
				unicode_emoji: updates.unicode_emoji !== undefined ? updates.unicode_emoji : this.unicodeEmoji,
			},
			{instanceId: this.instanceId},
		);
	}

	get isEveryone(): boolean {
		return this.id === this.guildId;
	}

	toJSON(): WireGuildRole {
		return {
			id: this.id,
			name: this.name,
			color: this.color,
			position: this.position,
			hoist_position: this.hoistPosition,
			permissions: this.permissions.toString(),
			hoist: this.hoist,
			mentionable: this.mentionable,
			icon: this.icon,
			unicode_emoji: this.unicodeEmoji,
		};
	}
}