fix: 🐛 start transaction with multi

This commit is contained in:
ThatOneCalculator 2023-06-23 18:21:00 -07:00
parent 8bb5844feb
commit be83ac8f43
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
1 changed files with 25 additions and 19 deletions

View File

@ -468,17 +468,19 @@ export default async (
} else if (boostedByRelay && data.renote?.uri) { } else if (boostedByRelay && data.renote?.uri) {
// Use Redis transaction for atomicity // Use Redis transaction for atomicity
await redisClient.watch(`publishedNote:${data.renote.uri}`); await redisClient.watch(`publishedNote:${data.renote.uri}`);
const exists = await redisClient.exists( const exists = await redisClient.exists(`publishedNote:${data.renote.uri}`);
`publishedNote:${data.renote.uri}`,
);
if (exists === 0) { if (exists === 0) {
// Start the transaction // Start the transaction
redisClient.multi(); const transaction = redisClient.multi();
publishNotesStream(data.renote);
const key = `publishedNote:${data.renote.uri}`; const key = `publishedNote:${data.renote.uri}`;
await redisClient.set(key, 1, "EX", 30); transaction.set(key, 1, "EX", 30);
// Execute the transaction // Execute the transaction
redisClient.exec(); transaction.exec((err, replies) => {
// Publish after setting the key in Redis
if (!err && data.renote) {
publishNotesStream(data.renote);
}
});
} else { } else {
// Abort the transaction // Abort the transaction
redisClient.unwatch(); redisClient.unwatch();
@ -489,12 +491,16 @@ export default async (
const exists = await redisClient.exists(`publishedNote:${note.uri}`); const exists = await redisClient.exists(`publishedNote:${note.uri}`);
if (exists === 0) { if (exists === 0) {
// Start the transaction // Start the transaction
redisClient.multi(); const transaction = redisClient.multi();
publishNotesStream(note);
const key = `publishedNote:${note.uri}`; const key = `publishedNote:${note.uri}`;
await redisClient.set(key, 1, "EX", 30); transaction.set(key, 1, "EX", 30);
// Execute the transaction // Execute the transaction
redisClient.exec(); transaction.exec((err, replies) => {
// Publish after setting the key in Redis
if (!err) {
publishNotesStream(note);
}
});
} else { } else {
// Abort the transaction // Abort the transaction
redisClient.unwatch(); redisClient.unwatch();
@ -663,15 +669,15 @@ async function renderNoteOrRenoteActivity(data: Option, note: Note) {
const content = const content =
data.renote && data.renote &&
data.text == null && data.text == null &&
data.poll == null && data.poll == null &&
(data.files == null || data.files.length === 0) (data.files == null || data.files.length === 0)
? renderAnnounce( ? renderAnnounce(
data.renote.uri data.renote.uri
? data.renote.uri ? data.renote.uri
: `${config.url}/notes/${data.renote.id}`, : `${config.url}/notes/${data.renote.id}`,
note, note,
) )
: renderCreate(await renderNote(note, false), note); : renderCreate(await renderNote(note, false), note);
return renderActivity(content); return renderActivity(content);