summaryrefslogtreecommitdiff
path: root/packages/backend
diff options
context:
space:
mode:
authorHazel K <acomputerdog@gmail.com>2024-10-02 11:38:21 -0400
committerHazel K <acomputerdog@gmail.com>2024-10-02 11:38:21 -0400
commitef7cde6bc6a2159f0fd041d26a3cb77cb0d53be9 (patch)
tree20a2927cb9bee70a0c7932c7b2ef16eb6cb858e2 /packages/backend
parentRevert "respect CWs in note summaries" (diff)
downloadsharkey-ef7cde6bc6a2159f0fd041d26a3cb77cb0d53be9.tar.gz
sharkey-ef7cde6bc6a2159f0fd041d26a3cb77cb0d53be9.tar.bz2
sharkey-ef7cde6bc6a2159f0fd041d26a3cb77cb0d53be9.zip
fixes from peer review
Diffstat (limited to 'packages/backend')
-rw-r--r--packages/backend/src/core/NoteCreateService.ts5
-rw-r--r--packages/backend/src/core/NoteDeleteService.ts20
2 files changed, 16 insertions, 9 deletions
diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts
index 0af65b81b1..daf0894cfd 100644
--- a/packages/backend/src/core/NoteCreateService.ts
+++ b/packages/backend/src/core/NoteCreateService.ts
@@ -1134,7 +1134,8 @@ export class NoteCreateService implements OnApplicationShutdown {
}
private async updateLatestNote(note: MiNote) {
- // Ignore DMs
+ // Ignore DMs.
+ // Followers-only posts are *included*, as this table is used to back the "following" feed.
if (note.visibility === 'specified') return;
// Ignore pure renotes
@@ -1143,7 +1144,7 @@ export class NoteCreateService implements OnApplicationShutdown {
// Make sure that this isn't an *older* post.
// We can get older posts through replies, lookups, etc.
const currentLatest = await this.latestNotesRepository.findOneBy({ userId: note.userId });
- if (currentLatest != null && currentLatest.userId >= note.id) return;
+ if (currentLatest != null && currentLatest.noteId >= note.id) return;
// Record this as the latest note for the given user
const latestNote = new LatestNote({
diff --git a/packages/backend/src/core/NoteDeleteService.ts b/packages/backend/src/core/NoteDeleteService.ts
index de753a3aa2..3f86f41942 100644
--- a/packages/backend/src/core/NoteDeleteService.ts
+++ b/packages/backend/src/core/NoteDeleteService.ts
@@ -240,6 +240,10 @@ export class NoteDeleteService {
// If it's a DM, then it can't possibly be the latest note so we can safely skip this.
if (note.visibility === 'specified') return;
+ // Check if the deleted note was possibly the latest for the user
+ const hasLatestNote = await this.latestNotesRepository.existsBy({ userId: note.userId });
+ if (hasLatestNote) return;
+
// Find the newest remaining note for the user.
// We exclude DMs and pure renotes.
const nextLatest = await this.notesRepository
@@ -269,12 +273,14 @@ export class NoteDeleteService {
noteId: nextLatest.id,
});
- // We use an upsert because this deleted note might not have been the newest.
- // In that case, the latest note may already be populated for this user.
- // We want postgres to do nothing instead of replacing the value or returning an error.
- await this.latestNotesRepository.upsert(latestNote, {
- conflictPaths: ['userId'],
- skipUpdateIfNoValuesChanged: true,
- });
+ // When inserting the latest note, it's possible that another worker has "raced" the insert and already added a newer note.
+ // We must use orIgnore() to ensure that the query ignores conflicts, otherwise an exception may be thrown.
+ await this.latestNotesRepository
+ .createQueryBuilder('latest')
+ .insert()
+ .into(LatestNote)
+ .values(latestNote)
+ .orIgnore()
+ .execute();
}
}