@@ -207,3 +207,52 @@ exports.changeCommentText = async (padId, commentId, commentText, authorId) => {
207207 // save the comment updated back
208208 await db . set ( prefix + padId , comments ) ;
209209} ;
210+
211+ exports . changeComment = async ( padId , commentId , commentText , changeFrom , changeTo , authorId , state ) => { // eslint-disable max-len
212+ if ( commentText . length <= 0 ) {
213+ logger . debug ( `ignoring attempt to change comment ${ commentId } to the empty string` ) ;
214+ throw new Error ( 'comment_cannot_be_empty' ) ;
215+ }
216+
217+ // Given a comment we update the comment text
218+
219+ // If we're dealing with comment replies we need to a different query
220+ let prefix = 'comments:' ;
221+ if ( commentId . substring ( 0 , 7 ) === 'c-reply' ) {
222+ prefix = 'comment-replies:' ;
223+ }
224+
225+ // get the entry
226+ const comments = await db . get ( prefix + padId ) ;
227+ if ( comments == null || comments [ commentId ] == null ) {
228+ logger . debug ( `ignoring attempt to edit non-existent comment ${ commentId } ` ) ;
229+ throw new Error ( 'no_such_comment' ) ;
230+ }
231+ if ( comments [ commentId ] . author !== authorId ) {
232+ logger . debug ( `author ${ authorId } attempted to edit comment ${ commentId } ` +
233+ `belonging to author ${ comments [ commentId ] . author } ` ) ;
234+ throw new Error ( 'unauth' ) ;
235+ }
236+ // update the comment text
237+ comments [ commentId ] . text = commentText ;
238+ if ( changeTo ) {
239+ comments [ commentId ] . changeTo = changeTo ;
240+ if ( ! comments [ commentId ] . changeFrom ) {
241+ comments [ commentId ] . changeFrom = changeFrom ;
242+ }
243+ }
244+
245+ if ( comments [ commentId ] . changeTo && ! changeTo ) {
246+ comments [ commentId ] . changeTo = null ;
247+ }
248+
249+ if ( state ) {
250+ comments [ commentId ] . changeAccepted = true ;
251+ comments [ commentId ] . changeReverted = false ;
252+ } else {
253+ comments [ commentId ] . changeAccepted = false ;
254+ comments [ commentId ] . changeReverted = true ;
255+ }
256+ // save the comment updated back
257+ await db . set ( prefix + padId , comments ) ;
258+ } ;
0 commit comments