Skip to content

Commit

Permalink
chore(backend): 업데이트된 노트가 없어질 리가 없긴 한데
Browse files Browse the repository at this point in the history
만약 없어지면 ApiError 던지는 거로 변경
  • Loading branch information
HotoRas committed Aug 4, 2024
1 parent 95a615e commit f1a0ad8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
14 changes: 10 additions & 4 deletions packages/backend/src/core/NoteUpdateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MiPoll, IPoll } from "@/models/Poll.js";
import { concat } from "@/misc/prelude/array.js";
import { extractHashtags } from "@/misc/extract-hashtags.js";
import { extractCustomEmojisFromMfm } from "@/misc/extract-custom-emojis-from-mfm.js";
import { ApiError } from "@/server/api/error.js";

type MinimumUser = {
id: MiUser['id'];
Expand Down Expand Up @@ -76,7 +77,7 @@ export class NoteUpdateService implements OnApplicationShutdown {
username: MiUser['username'],
host: MiUser['host'],
isBot: MiUser['isBot'],
}, data: Option, note: MiNote, silent = false): Promise<MiNote | null> {
}, data: Option, note: MiNote, silent = false): Promise<MiNote> {
if (data.updatedAt == null) data.updatedAt = new Date();

if (data.text) {
Expand Down Expand Up @@ -105,7 +106,10 @@ export class NoteUpdateService implements OnApplicationShutdown {
emojis = data.apEmojis ?? extractCustomEmojisFromMfm(combinedTokens);
}

const updatedNote = await this.updateNote(user, note, data, (tags ?? []), (emojis ?? []));
var updatedNote: MiNote;

Check failure on line 109 in packages/backend/src/core/NoteUpdateService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

Unexpected var, use let or const instead
try {

Check failure on line 110 in packages/backend/src/core/NoteUpdateService.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

Unnecessary try/catch wrapper
updatedNote = await this.updateNote(user, note, data, (tags ?? []), (emojis ?? []));
} catch (e) { throw e; }

if (updatedNote) {
setImmediate('post updated', { signal: this.#shutdownController.signal }).then(
Expand All @@ -121,7 +125,7 @@ export class NoteUpdateService implements OnApplicationShutdown {
private async updateNote(user: {
id: MiUser['id'],
host: MiUser['host'],
}, note: MiNote, data: Option, tags: string[], emojis: string[]): Promise<MiNote | null> {
}, note: MiNote, data: Option, tags: string[], emojis: string[]): Promise<MiNote> {
if (data.updatedAt === null || data.updatedAt === undefined) {
data.updatedAt = new Date();
}
Expand Down Expand Up @@ -199,7 +203,9 @@ export class NoteUpdateService implements OnApplicationShutdown {
await this.notesRepository.update({ id: note.id }, values);
}

return await this.notesRepository.findOneBy({ id: note.id });
const updatedNote = await this.notesRepository.findOneBy({ id: note.id });
if (updatedNote) return updatedNote;
throw new ApiError({ message: 'Updated note has gone.', id: '6dffaa9b-f578-45ac-9755-9e64290b4528', code: 'NOTE_UPDATE_GONE' }, { noteId: note.id });
} catch (e) {
console.error(e); throw e;
}
Expand Down
19 changes: 14 additions & 5 deletions packages/backend/src/server/api/endpoints/notes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export const meta = {
code: 'CANNOT_CREATE_ALREADY_EXPIRED_POLL',
id: '04da457d-b083-4055-9082-955525eda5a5',
},
updatedNoteGone: {
message: 'Updated note has gone.',
code: 'NOTE_UPDATE_GONE',
id: '6dffaa9b-f578-45ac-9755-9e64290b4528',
},
},
} as const;

Expand Down Expand Up @@ -149,11 +154,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
} : undefined,
};

const updatedNote = await this.noteUpdateService.update(me, data, note, false);

return {
updatedNote: await this.noteEntityService.pack(updatedNote!, me),
};
try {

Check failure on line 157 in packages/backend/src/server/api/endpoints/notes/update.ts

View workflow job for this annotation

GitHub Actions / lint (backend)

Unnecessary try/catch wrapper
const updatedNote = await this.noteUpdateService.update(me, data, note, false);

return {
updatedNote: await this.noteEntityService.pack(updatedNote, me),
};
} catch (err) {
throw err;
}
});
}
}

0 comments on commit f1a0ad8

Please sign in to comment.