@@ -4,6 +4,7 @@ import UserError from '../../utils/UserError';
44import { getThreads , setThreadLock } from '../../models/thread' ;
55import { getUserPermissionsInChannel } from '../../models/usersChannels' ;
66import { getUserPermissionsInCommunity } from '../../models/usersCommunities' ;
7+ import type { DBThread } from 'shared/types' ;
78
89export default async (
910 _ : any ,
@@ -17,13 +18,23 @@ export default async (
1718 return new UserError ( 'You must be signed in to make changes.' ) ;
1819 }
1920
20- const thread = await loaders . thread . load ( threadId ) ;
21+ const thread : DBThread = await loaders . thread . load ( threadId ) ;
2122
2223 // if the thread doesn't exist
2324 if ( ! thread || thread . deletedAt ) {
2425 return new UserError ( `Could not find thread with ID '${ threadId } '.` ) ;
2526 }
2627
28+ // A threads author can always lock their thread, but only unlock it if
29+ // it was locked by themselves. (if a mod locks a thread an author cannot
30+ // unlock it anymore)
31+ const isAuthor = thread . creatorId === currentUser . id ;
32+ const authorCanLock =
33+ ! thread . isLocked || thread . lockedBy === thread . creatorId ;
34+ if ( isAuthor && authorCanLock ) {
35+ return setThreadLock ( threadId , value , currentUser . id ) ;
36+ }
37+
2738 // get the channel permissions
2839 let [
2940 currentUserChannelPermissions ,
@@ -46,10 +57,15 @@ export default async (
4657 currentUserCommunityPermissions . isOwner ||
4758 currentUserCommunityPermissions . isModerator
4859 ) {
49- return setThreadLock ( threadId , value ) ;
60+ return setThreadLock ( threadId , value , currentUser . id ) ;
5061 }
5162
5263 // if the user is not a channel or community owner, the thread can't be locked
64+ if ( isAuthor ) {
65+ return new UserError (
66+ "You don't have permission to unlock this thread as it was locked by a moderator."
67+ ) ;
68+ }
5369 return new UserError (
5470 "You don't have permission to make changes to this thread."
5571 ) ;
0 commit comments