all repos — stealth-developers @ 26f02a7c5bf180e51541a9d20e2468bd36593736

pkgs/db/src/utils/user.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
import db, { and, eq } from "..";
import { actors, guilds, users } from "../schemas";
import { upsertGuild } from "./guild";

export async function upsertUser(user: {
	username: string;
	displayName: string | null;
	id: string;
	avatar: string | null;
}) {
	const [upsertedUser] = await db
		.insert(users)
		.values({
			discordId: user.id,
			username: user.username,
			displayName: user.displayName,
			avatar: user.avatar,
			createdAt: new Date(),
			updatedAt: new Date(),
		})
		.onConflictDoUpdate({
			target: users.discordId,
			set: {
				username: user.username,
				displayName: user.displayName,
				avatar: user.avatar,
				updatedAt: new Date(),
			},
		})
		.returning();

	return upsertedUser;
}

export async function upsertActor(data: {
	guild: {
		discordId: string;
		name: string;
		icon: string | null;
	};
	user: {
		discordId: string;
		username: string;
		displayName: string | null;
		avatar: string | null;
	};
	moderator: boolean;
}) {
	const dbGuild = await upsertGuild(data.guild);
	const dbUser = await upsertUser({
		id: data.user.discordId,
		username: data.user.username,
		displayName: data.user.displayName,
		avatar: data.user.avatar,
	});

	if (!dbGuild || !dbUser) {
		throw new Error("Failed to upsert guild or user");
	}

	const [actor] = await db
		.insert(actors)
		.values({
			guildId: dbGuild.id,
			userId: dbUser.id,
			discordId: data.user.discordId,
			username: data.user.username,
			displayName: data.user.displayName,
			avatar: data.user.avatar,
			moderator: data.moderator,
			createdAt: new Date(),
			updatedAt: new Date(),
		})
		.onConflictDoUpdate({
			target: [actors.guildId, actors.userId],
			set: {
				username: data.user.username,
				displayName: data.user.displayName,
				avatar: data.user.avatar,
				moderator: data.moderator,
				updatedAt: new Date(),
			},
		})
		.returning();

	return actor;
}

export async function anonymiseUser(id: number) {
	const result = await db
		.update(actors)
		.set({
			username: `Anonymised User`,
			displayName: null,
			avatar: null,
			updatedAt: new Date(),
		})
		.where(eq(actors.id, id))
		.returning();

	return result;
}

export async function getUser(id: number) {
	const [result] = await db.select().from(users).where(eq(users.id, id));
	return result;
}

export async function getUserByDiscordId(discordId: string) {
	const [result] = await db.select().from(users).where(eq(users.discordId, discordId));
	return result;
}

export async function getActor(id: number) {
	const [result] = await db.select().from(actors).where(eq(actors.id, id));
	return result;
}

export async function getActorByDiscordId(discordId: string, guildId: number) {
	const [result] = await db
		.select()
		.from(actors)
		.where(and(eq(actors.discordId, discordId), eq(actors.guildId, guildId)));
	return result;
}

export async function getActorByDiscordIdAndGuildDiscordId(
	discordId: string,
	guildDiscordId: string,
) {
	const [guild] = await db.select().from(guilds).where(eq(guilds.discordId, guildDiscordId));
	if (!guild) return null;
	return getActorByDiscordId(discordId, guild.id);
}

export async function getModerators(guildId: number) {
	const result = await db
		.select()
		.from(actors)
		.where(and(eq(actors.guildId, guildId), eq(actors.moderator, true)));
	return result;
}