2023-01-13 05:40:33 +01:00
|
|
|
import { IdentifiableError } from "@/misc/identifiable-error.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import type { Note } from "@/models/entities/note.js";
|
|
|
|
import { Notes, Users } from "@/models/index.js";
|
|
|
|
import { generateVisibilityQuery } from "./generate-visibility-query.js";
|
2019-01-19 19:07:12 +01:00
|
|
|
|
|
|
|
/**
|
2022-07-25 18:15:21 +02:00
|
|
|
* Get note for API processing, taking into account visibility.
|
2019-01-19 19:07:12 +01:00
|
|
|
*/
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function getNote(
|
|
|
|
noteId: Note["id"],
|
|
|
|
me: { id: User["id"] } | null,
|
|
|
|
) {
|
|
|
|
const query = Notes.createQueryBuilder("note").where("note.id = :id", {
|
|
|
|
id: noteId,
|
|
|
|
});
|
2022-07-25 18:15:21 +02:00
|
|
|
|
|
|
|
generateVisibilityQuery(query, me);
|
|
|
|
|
|
|
|
const note = await query.getOne();
|
2019-01-19 19:07:12 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (note == null) {
|
2023-01-13 05:40:33 +01:00
|
|
|
throw new IdentifiableError(
|
|
|
|
"9725d0ce-ba28-4dde-95a7-2cbb2c15de24",
|
|
|
|
"No such note.",
|
|
|
|
);
|
2019-01-19 19:07:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
2019-01-26 09:53:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user for API processing
|
|
|
|
*/
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function getUser(userId: User["id"]) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({ id: userId });
|
2019-01-26 09:53:35 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user == null) {
|
2023-01-13 05:40:33 +01:00
|
|
|
throw new IdentifiableError(
|
|
|
|
"15348ddd-432d-49c2-8a5a-8069753becff",
|
|
|
|
"No such user.",
|
|
|
|
);
|
2019-01-26 09:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get remote user for API processing
|
|
|
|
*/
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function getRemoteUser(userId: User["id"]) {
|
2019-01-26 09:53:35 +01:00
|
|
|
const user = await getUser(userId);
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (!Users.isRemoteUser(user)) {
|
2023-01-13 05:40:33 +01:00
|
|
|
throw new Error("user is not a remote user");
|
2019-01-26 09:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get local user for API processing
|
|
|
|
*/
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function getLocalUser(userId: User["id"]) {
|
2019-01-26 09:53:35 +01:00
|
|
|
const user = await getUser(userId);
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (!Users.isLocalUser(user)) {
|
2023-01-13 05:40:33 +01:00
|
|
|
throw new Error("user is not a local user");
|
2019-01-26 09:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|