Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP added calculateTimeSinceEdit, moment.js error still needs to be fixed #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified dump.rdb
Binary file not shown.
28 changes: 25 additions & 3 deletions public/src/client/topic/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand All @@ -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 {
Expand Down
39 changes: 36 additions & 3 deletions src/posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),
Expand All @@ -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() : '';
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down