server: simplify validateNote

Instead of returning an error that isn't even used, just throw the
error right away.
This commit is contained in:
Johann150 2024-11-21 19:54:14 +01:00
parent 42e8cc5989
commit ce5c6f8309
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -30,20 +30,20 @@ import { extractApMentions } from './mention.js';
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
import { sideEffects } from '@/services/note/side-effects.js';
export function validateNote(object: IObject): Error | null {
function validateNote(object: IObject) {
if (object == null) {
return new Error('invalid Note: object is null');
throw new Error('invalid Note: object is null');
}
if (!isPost(object)) {
return new Error(`invalid Note: invalid object type ${getApType(object)}`);
throw new Error(`invalid Note: invalid object type ${getApType(object)}`);
}
const id = getApId(object);
if (id == null) {
// Only transient objects or anonymous objects may not have an id or an id that is explicitly null.
// We consider all Notes as not transient and not anonymous so require ids for them.
return new Error(`invalid Note: id required but not present`);
throw new Error(`invalid Note: id required but not present`);
}
// Check that the server is authorized to act on behalf of this author.
@ -52,13 +52,11 @@ export function validateNote(object: IObject): Error | null {
? extractPunyHost(getOneApId(object.attributedTo))
: null;
if (attributedToHost !== expectHost) {
return new Error(`invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${attributedToHost}`);
throw new Error(`invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${attributedToHost}`);
}
if (attributedToHost === config.hostname) {
return new Error('invalid Note: by local author');
throw new Error('invalid Note: by local author');
}
return null;
}
/**
@ -127,11 +125,7 @@ export async function fetchNote(object: string | IObject): Promise<Note | null>
export async function createNote(value: string | IObject, resolver: Resolver, silent = false): Promise<Note | null> {
const object: IObject = await resolver.resolve(value);
const err = validateNote(object);
if (err) {
apLogger.error(`${err.message}`);
throw new Error('invalid note');
}
validateNote(object);
const note: IPost = object;
@ -339,11 +333,7 @@ export async function resolveNote(value: string | IObject, resolver: Resolver):
* If the target Note is not registered, it will be ignored.
*/
export async function updateNote(value: IPost, actor: User, resolver: Resolver): Promise<Note | null> {
const err = validateNote(value);
if (err) {
apLogger.error(`${err.message}`);
throw new Error('invalid updated note');
}
validateNote(value);
const uri = getApId(value);
const exists = await Notes.findOneBy({ uri });