11// @flow
22import type { GraphQLContext } from '../../' ;
3+ import { convertToRaw } from 'draft-js' ;
4+ import { stateFromMarkdown } from 'draft-js-import-markdown' ;
35import UserError from '../../utils/UserError' ;
46import {
57 getMessage ,
@@ -18,6 +20,7 @@ import { trackQueue } from 'shared/bull/queues';
1820type Args = {
1921 input : {
2022 id : string ,
23+ messageType ?: 'draftjs' | 'text' | 'media' ,
2124 content : {
2225 body : string ,
2326 } ,
@@ -26,7 +29,7 @@ type Args = {
2629
2730export default requireAuth ( async ( _ : any , args : Args , ctx : GraphQLContext ) => {
2831 const {
29- input : { id, content } ,
32+ input : { id, content, messageType } ,
3033 } = args ;
3134 const { user, loaders } = ctx ;
3235
@@ -43,15 +46,85 @@ export default requireAuth(async (_: any, args: Args, ctx: GraphQLContext) => {
4346 return new UserError ( 'This message does not exist.' ) ;
4447 }
4548
46- if ( content . body === message . content . body ) {
47- return message ;
49+ let body = content . body ;
50+ if ( messageType === 'text' ) {
51+ body = JSON . stringify (
52+ convertToRaw (
53+ stateFromMarkdown ( body , {
54+ parserOptions : {
55+ breaks : true ,
56+ } ,
57+ } )
58+ )
59+ ) ;
60+ messageType === 'draftjs' ;
4861 }
4962
5063 const eventFailed =
5164 message . threadType === 'story'
5265 ? events . MESSAGE_EDITED_FAILED
5366 : events . DIRECT_MESSAGE_EDITED_FAILED ;
5467
68+ if ( messageType === 'draftjs' ) {
69+ let parsed ;
70+ try {
71+ parsed = JSON . parse ( body ) ;
72+ } catch ( err ) {
73+ trackQueue . add ( {
74+ userId : user . id ,
75+ event : eventFailed ,
76+ properties : {
77+ reason : 'invalid draftjs data' ,
78+ message,
79+ } ,
80+ } ) ;
81+
82+ return new UserError (
83+ 'Please provide serialized raw DraftJS content state as content.body'
84+ ) ;
85+ }
86+ if ( ! parsed . blocks || ! Array . isArray ( parsed . blocks ) || ! parsed . entityMap ) {
87+ trackQueue . add ( {
88+ userId : user . id ,
89+ event : eventFailed ,
90+ properties : {
91+ reason : 'invalid draftjs data' ,
92+ message,
93+ } ,
94+ } ) ;
95+
96+ return new UserError (
97+ 'Please provide serialized raw DraftJS content state as content.body'
98+ ) ;
99+ }
100+ if (
101+ parsed . blocks . some (
102+ ( { type } ) =>
103+ ! type ||
104+ ( type !== 'unstyled' &&
105+ type !== 'code-block' &&
106+ type !== 'blockquote' )
107+ )
108+ ) {
109+ trackQueue . add ( {
110+ userId : user . id ,
111+ event : eventFailed ,
112+ properties : {
113+ reason : 'invalid draftjs data' ,
114+ message,
115+ } ,
116+ } ) ;
117+
118+ return new UserError (
119+ 'Invalid DraftJS block type specified. Supported block types: "unstyled", "code-block".'
120+ ) ;
121+ }
122+ }
123+
124+ if ( body === message . content . body ) {
125+ return message ;
126+ }
127+
55128 if ( message . senderId !== user . id ) {
56129 trackQueue . add ( {
57130 userId : user . id ,
@@ -65,5 +138,14 @@ export default requireAuth(async (_: any, args: Args, ctx: GraphQLContext) => {
65138 return new UserError ( 'You can only edit your own messages.' ) ;
66139 }
67140
68- return editMessage ( args . input , user . id ) ;
141+ return editMessage (
142+ {
143+ ...args . input ,
144+ content : {
145+ body,
146+ } ,
147+ messageType,
148+ } ,
149+ user . id
150+ ) ;
69151} ) ;
0 commit comments