Skip to content

Commit ec5c8d6

Browse files
committed
MOBILE-4482 forum: Fix filtered content displayed when editing post
When editing a post, the editor displayed the content already filtered, and it could also include some exra content that doesn't belong to the post, like plagiarism info.
1 parent 3fdc860 commit ec5c8d6

File tree

2 files changed

+127
-5
lines changed

2 files changed

+127
-5
lines changed

src/addons/mod/forum/components/post/post.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
AddonModForumDiscussion,
3636
AddonModForumPost,
3737
AddonModForumPostFormData,
38+
AddonModForumPrepareDraftAreaForPostWSResponse,
3839
} from '../../services/forum';
3940
import { CoreTag } from '@features/tag/services/tag';
4041
import { Translate } from '@singletons';
@@ -47,7 +48,7 @@ import { AddonModForumOffline } from '../../services/forum-offline';
4748
import { CoreUtils } from '@services/utils/utils';
4849
import { CoreRatingInfo } from '@features/rating/services/rating';
4950
import { CoreForms } from '@singletons/form';
50-
import { CoreFileEntry } from '@services/file-helper';
51+
import { CoreFileEntry, CoreFileHelper } from '@services/file-helper';
5152
import { AddonModForumSharedPostFormData } from '../../pages/discussion/discussion';
5253
import { CoreDom } from '@singletons/dom';
5354
import { 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.

src/addons/mod/forum/services/forum.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ import { CoreGroups } from '@services/groups';
2626
import { CoreSitesCommonWSOptions, CoreSites, CoreSitesReadingStrategy } from '@services/sites';
2727
import { CoreUrl } from '@singletons/url';
2828
import { CoreUtils } from '@services/utils/utils';
29-
import { CoreStatusWithWarningsWSResponse, CoreWSExternalFile, CoreWSExternalWarning, CoreWSStoredFile } from '@services/ws';
29+
import {
30+
CoreStatusWithWarningsWSResponse,
31+
CoreWSExternalFile,
32+
CoreWSExternalWarning,
33+
CoreWSFile,
34+
CoreWSStoredFile,
35+
} from '@services/ws';
3036
import { makeSingleton, Translate } from '@singletons';
3137
import { AddonModForumOffline, AddonModForumOfflineDiscussion, AddonModForumReplyOptions } from './forum-offline';
3238
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';
@@ -606,7 +612,11 @@ export class AddonModForumProvider {
606612
};
607613

608614
const site = await CoreSites.getSite(options.siteId);
615+
609616
const isGetDiscussionPostsAvailable = this.isGetDiscussionPostsAvailable(site);
617+
if (isGetDiscussionPostsAvailable && site.isVersionGreaterEqualThan('4.0')) {
618+
(params as AddonModForumGetDiscussionPostsWSParams).includeinlineattachments = true;
619+
}
610620

611621
const response = isGetDiscussionPostsAvailable
612622
? await site.read<AddonModForumGetDiscussionPostsWSResponse>('mod_forum_get_discussion_posts', params, preSets)
@@ -1264,6 +1274,35 @@ export class AddonModForumProvider {
12641274
CoreUser.storeUsers(CoreUtils.objectToArray(users));
12651275
}
12661276

1277+
/**
1278+
* Prepare post for edition.
1279+
*
1280+
* @param postId Post ID.
1281+
* @param area Area to prepare.
1282+
* @param options Other options.
1283+
* @returns Data of prepared area.
1284+
*/
1285+
async preparePostForEdition(
1286+
postId: number,
1287+
area: 'attachment'|'post',
1288+
options: AddonModForumPreparePostOptions = {},
1289+
): Promise<AddonModForumPrepareDraftAreaForPostWSResponse> {
1290+
const site = await CoreSites.getSite(options.siteId);
1291+
1292+
const params: AddonModForumPrepareDraftAreaForPostWSParams = {
1293+
postid: postId,
1294+
area: area,
1295+
};
1296+
if (options.filesToKeep?.length) {
1297+
params.filestokeep = options.filesToKeep.map(file => ({
1298+
filename: file.filename ?? '',
1299+
filepath: file.filepath ?? '',
1300+
}));
1301+
}
1302+
1303+
return await site.write('mod_forum_prepare_draft_area_for_post', params);
1304+
}
1305+
12671306
/**
12681307
* Update a certain post.
12691308
*
@@ -1903,6 +1942,7 @@ export type AddonModForumGetDiscussionPostsWSParams = {
19031942
discussionid: number; // The ID of the discussion from which to fetch posts.
19041943
sortby?: string; // Sort by this element: id, created or modified.
19051944
sortdirection?: string; // Sort direction: ASC or DESC.
1945+
includeinlineattachments?: boolean; // @since 4.0. Whether inline attachments should be included or not.
19061946
};
19071947

19081948
/**
@@ -2086,6 +2126,46 @@ export type AddonModForumUpdateDiscussionPostWSParams = {
20862126
*/
20872127
export type AddonModForumUpdateDiscussionPostWSResponse = CoreStatusWithWarningsWSResponse;
20882128

2129+
/**
2130+
* Params of mod_forum_prepare_draft_area_for_post WS.
2131+
*/
2132+
type AddonModForumPrepareDraftAreaForPostWSParams = {
2133+
postid: number; // Post to prepare the draft area for.
2134+
area: string; // Area to prepare: attachment or post.
2135+
draftitemid?: number; // The draft item id to use. 0 to generate one.
2136+
filestokeep?: AddonModForumFileToKeep[]; // Only keep these files in the draft file area. Empty for keeping all.
2137+
};
2138+
2139+
/**
2140+
* Data to pass to mod_forum_prepare_draft_area_for_post to keep a file in the area.
2141+
*/
2142+
type AddonModForumFileToKeep = {
2143+
filename: string; // File name.
2144+
filepath: string; // File path.
2145+
};
2146+
2147+
/**
2148+
* Data returned by mod_forum_prepare_draft_area_for_post WS.
2149+
*/
2150+
export type AddonModForumPrepareDraftAreaForPostWSResponse = {
2151+
draftitemid: number; // Draft item id for the file area.
2152+
files?: CoreWSExternalFile[];
2153+
areaoptions: { // Draft file area options.
2154+
name: string; // Name of option.
2155+
value: string; // Value of option.
2156+
}[];
2157+
messagetext: string; // Message text with URLs rewritten.
2158+
warnings?: CoreWSExternalWarning[];
2159+
};
2160+
2161+
/**
2162+
* Options to pass to preparePostForEdition.
2163+
*/
2164+
export type AddonModForumPreparePostOptions = {
2165+
filesToKeep?: CoreWSFile[]; // Only keep these files in the draft file area. Undefined or empty array for keeping all.
2166+
siteId?: string;
2167+
};
2168+
20892169
/**
20902170
* Data passed to NEW_DISCUSSION_EVENT event.
20912171
*/

0 commit comments

Comments
 (0)