diff --git a/dump.rdb b/dump.rdb index bff9e0b..4e63384 100644 Binary files a/dump.rdb and b/dump.rdb differ diff --git a/public/src/client/topic/events.js b/public/src/client/topic/events.js index e091dd6..59f72d2 100644 --- a/public/src/client/topic/events.js +++ b/public/src/client/topic/events.js @@ -13,6 +13,7 @@ define('forum/topic/events', [ 'helpers', ], function (postTools, threadTools, posts, images, components, translator, hooks, helpers) { const Events = {}; + const moment = window.moment || require('moment'); const events = { 'event:user_status_change': onUserStatusChange, @@ -135,6 +136,7 @@ define('forum/topic/events', [ }); } + //change code if (data.post.changed) { editedPostEl.fadeOut(250, function () { editedPostEl.html(translator.unescape(data.post.content)); @@ -146,17 +148,37 @@ define('forum/topic/events', [ if (data.post.edited) { const editData = { editor: data.editor, - editedISO: utils.toISOString(data.post.edited), + //editedISO: utils.toISOString(data.post.edited), + editedTimeAgo: data.post.editedTimeAgo, }; + // app.parseAndTranslate('partials/topic/post-editor', editData, function (html) { + // editorEl.replaceWith(html); + // postContainer.find('[component="post/edit-indicator"]') + // .removeClass('hidden') + // .translateAttr('title', `[[global:edited-timestamp, ${helpers.isoTimeToLocaleString(editData.editedISO, config.userLang)}]]`); + // postContainer.find('[component="post/editor"] .timeago').timeago(); + // hooks.fire('action:posts.edited', data); + // }); + // app.parseAndTranslate('partials/topic/post-editor', editData, function (html) { + // editorEl.replaceWith(html); + + // // Update the edit indicator with relative time instead of a timestamp + // postContainer.find('[component="post/edit-indicator"]') + // .removeClass('hidden') + // .text(`Edited: ${editData.editedTimeAgo}`); // Shows "X minutes/hours ago" + + // hooks.fire('action:posts.edited', data); + // }); app.parseAndTranslate('partials/topic/post-editor', editData, function (html) { editorEl.replaceWith(html); postContainer.find('[component="post/edit-indicator"]') .removeClass('hidden') - .translateAttr('title', `[[global:edited-timestamp, ${helpers.isoTimeToLocaleString(editData.editedISO, config.userLang)}]]`); - postContainer.find('[component="post/editor"] .timeago').timeago(); + .text(`Edited: ${editData.editedTimeAgo}`); // Directly set "X minutes/hours ago" + hooks.fire('action:posts.edited', data); }); + } }); } else { diff --git a/src/posts/edit.js b/src/posts/edit.js index a63f34c..c87951e 100644 --- a/src/posts/edit.js +++ b/src/posts/edit.js @@ -13,6 +13,27 @@ const pubsub = require('../pubsub'); const utils = require('../utils'); const slugify = require('../slugify'); const translator = require('../translator'); +const moment = require('moment'); + + +function calculateTimeSinceEdit(editTime){ + const now = moment(); + const editedMoment = moment(editTime); + const diffMinutes = now.diff(editedMoment, 'minutes'); + + if (diffMinutes < 60){ + return `${diffMinutes} minutes ago`; + + } + + const diffHours = Math.floor(diffMinutes/60); + if (diffHours < 24){ + return `${diffHours} hours ago`; + } + + const diffDays = Math.floor(diffHours / 24); + return `${diffDays} days ago`; +} module.exports = function (Posts) { pubsub.on('post:edit', (pid) => { @@ -74,14 +95,22 @@ module.exports = function (Posts) { // Normalize data prior to constructing returnPostData (match types with getPostSummaryByPids) postData.deleted = !!postData.deleted; + const moment = require('moment'); + + const returnPostData = { ...postData, ...result.post }; returnPostData.cid = topic.cid; returnPostData.topic = topic; - returnPostData.editedISO = utils.toISOString(editPostData.edited); + + //returnPostData.editedISO = utils.toISOString(editPostData.edited); + returnPostData.editedTimeAgo = calculateTimeSinceEdit(editPostData.edited); returnPostData.changed = contentChanged; returnPostData.oldContent = oldContent; returnPostData.newContent = data.content; + + + await topics.notifyFollowers(returnPostData, data.uid, { type: 'post-edit', bodyShort: translator.compile('notifications:user-edited-post', editor.username, topic.title), @@ -103,6 +132,11 @@ module.exports = function (Posts) { }; }; + //new code + + + + async function editMainPost(data, postData, topicData) { const { tid } = postData; const title = data.title ? data.title.trim() : ''; @@ -145,8 +179,7 @@ module.exports = function (Posts) { const results = await plugins.hooks.fire('filter:topic.edit', { req: data.req, topic: newTopicData, - data: data, - }); + data: data,}); await db.setObject(`topic:${tid}`, results.topic); if (tagsupdated) { await topics.updateTopicTags(tid, data.tags); diff --git a/src/posts/index.js b/src/posts/index.js index 9db52c6..13ccbba 100644 --- a/src/posts/index.js +++ b/src/posts/index.js @@ -50,6 +50,10 @@ Posts.getPostsByPids = async function (pids, uid) { if (!data || !Array.isArray(data.posts)) { return []; } + posts.forEach(post => { + post.editedISO = post.edited ? new Date(post.edited).toISOString() : null; + }); + return data.posts.filter(Boolean); };