Skip to content

Commit

Permalink
feat: dynamic frontend badge for endorsed by staff
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhoswe committed Feb 26, 2025
1 parent 46c4ad3 commit 4f9348f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
8 changes: 3 additions & 5 deletions nodebb-theme-harmony/templates/partials/topic/post.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@
</div>
{{{ end }}}
<div class="d-flex align-items-center gap-1 flex-grow-1 justify-content-end">
<a> Flag ID: {posts.flagId}</a>
{{{ if posts.flag }}}
<a> Test post </a>
{{{ if posts.endorsedByStaff }}}
<span class="badge bg-success">Endorsed by Staff</span>
{{{ end }}}

<span class="bookmarked opacity-0 text-primary"><i class="fa fa-bookmark-o"></i></span>
<a href="{config.relative_path}/post/{./pid}" class="post-index text-muted d-none d-md-inline">#{increment(./index, "1")}</a>
</div>
Expand Down Expand Up @@ -150,4 +148,4 @@
{widgets.mainpost-footer.html}
{{{ end }}}
</div>
{{{ end }}}
{{{ end }}}
24 changes: 23 additions & 1 deletion src/topics/posts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

'use strict';

const _ = require('lodash');
Expand Down Expand Up @@ -130,6 +129,24 @@ module.exports = function (Topics) {
Topics.addParentPosts(postData),
]);

// Get flag data for posts with flagId
const flaggedPosts = postData.filter(p => p && p.flagId);
const flagData = await Promise.all(
flaggedPosts.map(async (post) => {
try {
const reports = await require('../flags').getReports(post.flagId);
return { pid: post.pid, reports: reports };
} catch (err) {
return { pid: post.pid, reports: [] };
}
})
);
// Create a map of pid to flag data for quick lookup
const flagDataMap = {};
flagData.forEach((data) => {
flagDataMap[data.pid] = data.reports;
});

postData.forEach((postObj, i) => {
if (postObj) {
postObj.user = postObj.uid ? userData[postObj.uid] : { ...userData[postObj.uid] };
Expand All @@ -141,6 +158,11 @@ module.exports = function (Topics) {
postObj.replies = replies[i];
postObj.selfPost = parseInt(uid, 10) > 0 && parseInt(uid, 10) === postObj.uid;

// Check if post has a flag with "Endorsed by Admins" report
if (flagDataMap[postObj.pid]) {
postObj.endorsedByStaff = flagDataMap[postObj.pid].some(report => report.value && report.value.includes('Endorsed by Admins'));
}

// Username override for guests, if enabled
if (meta.config.allowGuestHandles && postObj.uid === 0 && postObj.handle) {
postObj.user.username = validator.escape(String(postObj.handle));
Expand Down

0 comments on commit 4f9348f

Please sign in to comment.