@@ -35,6 +35,7 @@ import {
3535 AddonModForumDiscussion ,
3636 AddonModForumPost ,
3737 AddonModForumPostFormData ,
38+ AddonModForumPrepareDraftAreaForPostWSResponse ,
3839} from '../../services/forum' ;
3940import { CoreTag } from '@features/tag/services/tag' ;
4041import { Translate } from '@singletons' ;
@@ -47,7 +48,7 @@ import { AddonModForumOffline } from '../../services/forum-offline';
4748import { CoreUtils } from '@services/utils/utils' ;
4849import { CoreRatingInfo } from '@features/rating/services/rating' ;
4950import { CoreForms } from '@singletons/form' ;
50- import { CoreFileEntry } from '@services/file-helper' ;
51+ import { CoreFileEntry , CoreFileHelper } from '@services/file-helper' ;
5152import { AddonModForumSharedPostFormData } from '../../pages/discussion/discussion' ;
5253import { CoreDom } from '@singletons/dom' ;
5354import { CoreAnalytics , CoreAnalyticsEventType } from '@services/analytics' ;
@@ -95,6 +96,8 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
9596 displaySubject = true ;
9697 optionsMenuEnabled = false ;
9798
99+ protected preparePostData ?: AddonModForumPrepareDraftAreaForPostWSResponse ;
100+
98101 constructor (
99102 protected elementRef : ElementRef ,
100103 ) { }
@@ -226,6 +229,10 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
226229
227230 // Show advanced fields if any of them has not the default value.
228231 this . advanced = this . formData . files . length > 0 ;
232+
233+ if ( ! isEditing || ! postId || postId <= 0 ) {
234+ this . preparePostData = undefined ;
235+ }
229236 }
230237
231238 /**
@@ -314,6 +321,28 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
314321 // Ask confirm if there is unsaved data.
315322 try {
316323 await this . confirmDiscard ( ) ;
324+ } catch {
325+ // Cancelled.
326+ return ;
327+ }
328+
329+ const modal = await CoreLoadings . show ( ) ;
330+
331+ try {
332+ let message = this . post . message ;
333+
334+ if ( this . post . id > 0 ) {
335+ // Call prepare post for edition to retrieve the message without any added content (like filters and plagiarism).
336+ this . preparePostData = await AddonModForum . preparePostForEdition ( this . post . id , 'post' ) ;
337+
338+ const { text } = CoreFileHelper . replaceDraftfileUrls (
339+ CoreSites . getRequiredCurrentSite ( ) . getURL ( ) ,
340+ this . preparePostData . messagetext ,
341+ this . post . messageinlinefiles ?. length ? this . post . messageinlinefiles : ( this . preparePostData . files ?? [ ] ) ,
342+ ) ;
343+
344+ message = text ;
345+ }
317346
318347 this . formData . syncId = AddonModForumSync . getDiscussionSyncId ( this . discussionId ) ;
319348 CoreSync . blockOperation ( ADDON_MOD_FORUM_COMPONENT , this . formData . syncId ) ;
@@ -322,7 +351,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
322351 this . post . parentid ,
323352 true ,
324353 this . post . subject ,
325- this . post . message ,
354+ message ,
326355 this . post . attachments ,
327356 this . post . isprivatereply ,
328357 this . post . id > 0 ? this . post . id : undefined ,
@@ -331,8 +360,10 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
331360 this . scrollToForm ( ) ;
332361
333362 this . analyticsLogEvent ( 'mod_forum_update_discussion_post' , `/mod/forum/post.php?edit=${ this . post . id } ` ) ;
334- } catch {
335- // Cancelled.
363+ } catch ( error ) {
364+ CoreDomUtils . showErrorModalDefault ( error , 'addon.mod_forum.errorgetpost' , true ) ;
365+ } finally {
366+ modal . dismiss ( ) ;
336367 }
337368 }
338369
@@ -369,6 +400,16 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
369400 const isEditOnline = this . formData . id && this . formData . id > 0 ;
370401 const modal = await CoreLoadings . show ( 'core.sending' , true ) ;
371402
403+ if ( isEditOnline && this . preparePostData ) {
404+ // Restore the draft file URLs, otherwise the treated URLs would be saved in the content, which can cause problems.
405+ message = CoreFileHelper . restoreDraftfileUrls (
406+ CoreSites . getRequiredCurrentSite ( ) . getURL ( ) ,
407+ message ,
408+ this . preparePostData . messagetext ,
409+ this . post . messageinlinefiles ?. length ? this . post . messageinlinefiles : ( this . preparePostData . files ?? [ ] ) ,
410+ ) ;
411+ }
412+
372413 // Add some HTML to the message if needed.
373414 message = CoreText . formatHtmlLines ( message ) ;
374415
@@ -401,6 +442,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
401442 if ( isEditOnline ) {
402443 sent = await AddonModForum . updatePost ( this . formData . id ! , subject , message , {
403444 attachmentsid : attachments ,
445+ inlineattachmentsid : this . preparePostData ?. draftitemid ,
404446 } ) ;
405447 } else if ( saveOffline ) {
406448 // Save post in offline.
0 commit comments