diff --git a/keyserver/src/updaters/thread-updaters.js b/keyserver/src/updaters/thread-updaters.js --- a/keyserver/src/updaters/thread-updaters.js +++ b/keyserver/src/updaters/thread-updaters.js @@ -860,48 +860,38 @@ const pinnedValue = action === 'pin' ? 1 : 0; const pinTimeValue = action === 'pin' ? Date.now() : null; + const pinnedCountValue = action === 'pin' ? 1 : -1; - const togglePinQuery = SQL` - UPDATE messages - SET pinned = ${pinnedValue}, - pin_time = ${pinTimeValue} - WHERE id = ${messageID} - AND thread = ${threadID} + const query = SQL` + UPDATE messages AS m, threads AS t + SET m.pinned = ${pinnedValue}, + m.pin_time = ${pinTimeValue}, + t.pinned_count = t.pinned_count + ${pinnedCountValue} + WHERE m.id = ${messageID} + AND m.thread = ${threadID} + AND t.id = ${threadID} + AND m.pinned <> ${pinnedValue} `; - const messageData = { - type: messageTypes.TOGGLE_PIN, - threadID, - targetMessageID: messageID, - action, - pinnedContent: getPinnedContentFromMessage(targetMessage), - creatorID: viewer.userID, - time: Date.now(), - }; - - let updateThreadQuery; - if (action === 'pin') { - updateThreadQuery = SQL` - UPDATE threads - SET pinned_count = pinned_count + 1 - WHERE id = ${threadID} - `; - } else { - updateThreadQuery = SQL` - UPDATE threads - SET pinned_count = pinned_count - 1 - WHERE id = ${threadID} - `; - } - - const [{ threadInfos: serverThreadInfos }] = await Promise.all([ - fetchServerThreadInfos({ threadID }), - dbQuery(togglePinQuery), - dbQuery(updateThreadQuery), - ]); + const [result] = await dbQuery(query); - const newMessageInfos = await createMessages(viewer, [messageData]); + let newMessageInfos = []; + if (result.affectedRows > 0) { + const messageData = { + type: messageTypes.TOGGLE_PIN, + threadID, + targetMessageID: messageID, + action, + pinnedContent: getPinnedContentFromMessage(targetMessage), + creatorID: viewer.userID, + time: Date.now(), + }; + newMessageInfos = await createMessages(viewer, [messageData]); + } + const { threadInfos: serverThreadInfos } = await fetchServerThreadInfos({ + threadID, + }); const time = Date.now(); const updates = []; for (const member of serverThreadInfos[threadID].members) {