summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/common/getters.ts
blob: 783ea9ef757b98db56fbc7988f42472cb212a3c6 (plain)
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
import { IdentifiableError } from '@/misc/identifiable-error.js';
import { User } from '@/models/entities/user.js';
import { Note } from '@/models/entities/note.js';
import { Notes, Users } from '@/models/index.js';

/**
 * Get note for API processing
 */
export async function getNote(noteId: Note['id']) {
	const note = await Notes.findOneBy({ id: noteId });

	if (note == null) {
		throw new IdentifiableError('9725d0ce-ba28-4dde-95a7-2cbb2c15de24', 'No such note.');
	}

	return note;
}

/**
 * Get user for API processing
 */
export async function getUser(userId: User['id']) {
	const user = await Users.findOneBy({ id: userId });

	if (user == null) {
		throw new IdentifiableError('15348ddd-432d-49c2-8a5a-8069753becff', 'No such user.');
	}

	return user;
}

/**
 * Get remote user for API processing
 */
export async function getRemoteUser(userId: User['id']) {
	const user = await getUser(userId);

	if (!Users.isRemoteUser(user)) {
		throw new Error('user is not a remote user');
	}

	return user;
}

/**
 * Get local user for API processing
 */
export async function getLocalUser(userId: User['id']) {
	const user = await getUser(userId);

	if (!Users.isLocalUser(user)) {
		throw new Error('user is not a local user');
	}

	return user;
}