Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/updaters/thread-updaters.js
Show All 17 Lines | import { | ||||
type RoleChangeRequest, | type RoleChangeRequest, | ||||
type ChangeThreadSettingsResult, | type ChangeThreadSettingsResult, | ||||
type RemoveMembersRequest, | type RemoveMembersRequest, | ||||
type LeaveThreadRequest, | type LeaveThreadRequest, | ||||
type LeaveThreadResult, | type LeaveThreadResult, | ||||
type UpdateThreadRequest, | type UpdateThreadRequest, | ||||
type ServerThreadJoinRequest, | type ServerThreadJoinRequest, | ||||
type ThreadJoinResult, | type ThreadJoinResult, | ||||
type ToggleMessagePinRequest, | |||||
threadPermissions, | threadPermissions, | ||||
threadTypes, | threadTypes, | ||||
} from 'lib/types/thread-types.js'; | } from 'lib/types/thread-types.js'; | ||||
import { updateTypes } from 'lib/types/update-types.js'; | import { updateTypes } from 'lib/types/update-types.js'; | ||||
import { ServerError } from 'lib/utils/errors.js'; | import { ServerError } from 'lib/utils/errors.js'; | ||||
import { promiseAll } from 'lib/utils/promises.js'; | import { promiseAll } from 'lib/utils/promises.js'; | ||||
import { firstLine } from 'lib/utils/string-utils.js'; | import { firstLine } from 'lib/utils/string-utils.js'; | ||||
▲ Show 20 Lines • Show All 802 Lines • ▼ Show 20 Lines | updateDatas.push({ | ||||
threadID: threadID, | threadID: threadID, | ||||
targetSession: viewer.session, | targetSession: viewer.session, | ||||
}); | }); | ||||
} | } | ||||
await createUpdates(updateDatas); | await createUpdates(updateDatas); | ||||
} | } | ||||
async function toggleMessagePinForThread( | |||||
viewer: Viewer, | |||||
request: ToggleMessagePinRequest, | |||||
): Promise<void> { | |||||
const { messageID, action } = request; | |||||
const threadQuery = SQL`SELECT thread FROM messages WHERE id = ${messageID}`; | |||||
const [threadResult] = await dbQuery(threadQuery); | |||||
const threadID = threadResult[0].thread.toString(); | |||||
const hasPermission = await checkThreadPermission( | |||||
viewer, | |||||
threadID, | |||||
threadPermissions.MANAGE_PINS, | |||||
); | |||||
if (!hasPermission) { | |||||
throw new ServerError('invalid_credentials'); | |||||
} | |||||
const pinnedValue = action === 'pin' ? 1 : 0; | |||||
const pinTimeValue = action === 'pin' ? Date.now() : null; | |||||
const togglePinQuery = SQL` | |||||
UPDATE messages | |||||
SET pinned = ${pinnedValue}, pin_time = ${pinTimeValue} | |||||
WHERE id = ${messageID} AND thread = ${threadID} | |||||
`; | |||||
await dbQuery(togglePinQuery); | |||||
} | |||||
export { | export { | ||||
updateRole, | updateRole, | ||||
removeMembers, | removeMembers, | ||||
leaveThread, | leaveThread, | ||||
updateThread, | updateThread, | ||||
joinThread, | joinThread, | ||||
updateThreadMembers, | updateThreadMembers, | ||||
toggleMessagePinForThread, | |||||
}; | }; |