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

Feat: Add anonymous post and reply functionality #45

Merged
merged 2 commits into from
Feb 27, 2025
Merged
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
14 changes: 14 additions & 0 deletions nodebb-plugin-anonymous/library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const hooks = require.main.require('./src/plugins/hooks');

hooks.on('filter:composer.postData', async (hookData) => {
if (hookData.data && hookData.data.anonymous) {
console.log('[DEBUG] Anonymizing post data on server...');
// Override the user data to post anonymously
hookData.data.uid = 0;
hookData.data.username = 'Anonymous';
hookData.data.picture = '/assets/img/anonymous.png'; // Optional avatar
}
return hookData;
});
14 changes: 14 additions & 0 deletions nodebb-plugin-anonymous/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "nodebb-plugin-anonymous",
"version": "1.0.0",
"description": "Plugin to enable anonymous posting in NodeBB",
"main": "library.js",
"engines": {
"node": ">=18",
"nodebb": ">=3.8.4"
},
"license": "GPL-3.0",
"nodebb": {
"isPlugin": true
}
}
27 changes: 27 additions & 0 deletions nodebb-theme-harmony/public/harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,30 @@ $(document).ready(function () {
.on('hidden.bs.dropdown', toggleOverflow);
}
});

document.addEventListener('click', function (event) {
// look for clicks on [component="topic/reply-anonymously"]
const el = event.target.closest('[component="topic/reply-anonymously"]');
if (!el) return;
event.preventDefault();
console.log('[DEBUG] Anonymous reply button clicked!');
// get the topic ID from ajaxify.data if on a topic page
const tid = ajaxify.data && ajaxify.data.tid;
if (!tid) {
// alert('No topic ID found!');
return;
}
// call a function that opens the composer with an anonymous flag
app.anonymousReply({ tid: tid });
});
// define the function that sets up an "anonymous" composer
app.anonymousReply = async function (params) {
if (typeof params !== 'object') {
params = { tid: params };
}
const [hooks] = await app.require(['hooks', 'api']);
params.title = (ajaxify.data && ajaxify.data.titleRaw) || 'Untitled';
params.anonymous = true; // the important flag
console.log('[DEBUG] Opening composer with anonymous = true', params);
hooks.fire('action:composer.post.new', params);
};
26 changes: 25 additions & 1 deletion nodebb-theme-harmony/templates/partials/topic-list-bar.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,31 @@
<div class="d-flex gap-1 align-items-center">
{{{ if template.category }}}
{{{ if privileges.topics:create }}}
<a href="{config.relative_path}/compose?cid={cid}" component="category/post" id="new_topic" class="btn btn-primary btn-sm text-nowrap" data-ajaxify="false" role="button">[[category:new-topic-button]]</a>
<div class="btn-group">
<!-- Main New Topic Button -->
<a href="{config.relative_path}/compose?cid={cid}" component="category/post" id="new_topic" class="btn btn-primary btn-sm text-nowrap" data-ajaxify="false" role="button">
[[category:new-topic-button]]
</a>

<!-- Dropdown Toggle Button -->
<button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
</button>

<!-- Dropdown Menu -->
<ul class="dropdown-menu dropdown-menu-end p-1 text-sm" role="menu">
<li>
<a href="{config.relative_path}/compose?cid={cid}" component="category/post" id="new_topic" class="dropdown-item" data-ajaxify="false" role="button">
Regular Topic
</a>
</li>
<li>
<a href="{config.relative_path}/compose?cid={cid}" component="category/post" id="new_topic" class="dropdown-item" data-ajaxify="false" role="button">
Post Anonymously
</a>
</li>
</ul>
</div>
{{{ end }}}
{{{ else }}}
{{{ if canPost }}}
Expand Down
12 changes: 11 additions & 1 deletion nodebb-theme-harmony/templates/partials/topic/post.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@
<div class="d-flex align-items-start gap-3">
<div class="bg-body d-none d-sm-block rounded-circle" style="outline: 2px solid var(--bs-body-bg);">
<a class="d-inline-block position-relative text-decoration-none" href="{{{ if ./user.userslug }}}{config.relative_path}/user/{./user.userslug}{{{ else }}}#{{{ end }}}" aria-label="[[aria:user-avatar-for, {./user.username}]]">
{{{ if !posts.anonymous }}}
{buildAvatar(posts.user, "48px", true, "", "user/picture")}
<span component="user/status" class="position-absolute translate-middle-y border border-white border-2 rounded-circle status {posts.user.status}"><span class="visually-hidden">[[global:{posts.user.status}]]</span></span>
{{{ end }}}
</a>
</div>

<div class="post-container d-flex flex-grow-1 flex-column w-100" style="min-width:0;">
<div class="d-flex align-items-center gap-1 flex-wrap w-100 post-header mt-1" itemprop="author" itemscope itemtype="https://schema.org/Person">
<meta itemprop="name" content="{./user.username}">
{{{ if ./user.userslug }}}<meta itemprop="url" content="{config.relative_path}/user/{./user.userslug}">{{{ end }}}

<div class="bg-body d-sm-none">

<a class="d-inline-block position-relative text-decoration-none" href="{{{ if ./user.userslug }}}{config.relative_path}/user/{./user.userslug}{{{ else }}}#{{{ end }}}">
{{{ if !posts.anonymous }}}
{buildAvatar(posts.user, "20px", true, "", "user/picture")}
<span component="user/status" class="position-absolute translate-middle-y border border-white border-2 rounded-circle status {posts.user.status}"><span class="visually-hidden">[[global:{posts.user.status}]]</span></span>
{{{ end }}}
</a>
</div>

<a class="fw-bold text-nowrap" href="{{{ if ./user.userslug }}}{config.relative_path}/user/{./user.userslug}{{{ else }}}#{{{ end }}}" data-username="{posts.user.username}" data-uid="{posts.user.uid}">{posts.user.displayname}</a>
{{{ if posts.anonymous }}}
<span>ANONYMOUS</span>
{{{ else }}}
<a class="fw-bold text-nowrap" href="{{{ if ./user.userslug }}}{config.relative_path}/user/{./user.userslug}{{{ else }}}#{{{ end }}}" data-username="{posts.user.username}" data-uid="{posts.user.uid}">{posts.user.displayname}</a>
{{{ end }}}

{{{ each posts.user.selectedGroups }}}
{{{ if posts.user.selectedGroups.slug }}}
Expand Down
47 changes: 35 additions & 12 deletions nodebb-theme-harmony/templates/partials/topic/reply-button.tpl
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
{{{ if privileges.topics:reply }}}
<div component="topic/reply/container" class="btn-group">
<a href="{config.relative_path}/compose?tid={tid}" class="d-flex align-items-center btn btn-sm btn-primary px-3 fw-semibold" component="topic/reply" data-ajaxify="false" role="button"><i class="fa fa-reply d-sm-block d-md-none"></i><span class="d-none d-md-block"> [[topic:reply]]</span></a>
<button type="button" class="btn btn-sm btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" aria-label="[[topic:reply-options]]">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-end p-1 text-sm" role="menu">
<li><a class="dropdown-item rounded-1" href="#" component="topic/reply-as-topic" role="menuitem">[[topic:reply-as-topic]]</a></li>
</ul>
</div>
<!-- Anonymous Reply Button -->
<a href="#" class="d-flex align-items-center btn btn-sm btn-primary px-3 fw-semibold"
component="topic/reply-anonymously" role="button">
<i class="fa fa-reply d-sm-block d-md-none"></i>
<span class="d-none d-md-block">Reply anonymously</span>
</a>

<!-- Regular Reply Buttons -->
<div component="topic/reply/container" class="btn-group">
<a href="{config.relative_path}/compose?tid={tid}"
class="d-flex align-items-center btn btn-sm btn-primary px-3 fw-semibold"
component="topic/reply" data-ajaxify="false" role="button">
<i class="fa fa-reply d-sm-block d-md-none"></i>
<span class="d-none d-md-block">[[topic:reply]]</span>
</a>
<button type="button" class="btn btn-sm btn-primary dropdown-toggle" data-bs-toggle="dropdown"
aria-haspopup="true" aria-expanded="false" aria-label="[[topic:reply-options]]">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-end p-1 text-sm" role="menu">
<li>
<a class="dropdown-item rounded-1" href="#"
component="topic/reply-anonymously" role="menuitem">[[topic:reply-anonymously]]</a>
</li>
</ul>
</div>
{{{ end }}}

{{{ if loggedIn }}}
{{{ if !privileges.topics:reply }}}
{{{ if locked }}}
<a href="#" component="topic/reply/locked" class="d-flex gap-2 align-items-center fw-semibold btn btn-sm btn-primary disabled" disabled><i class="fa fa-lock"></i> [[topic:locked]]</a>
<a href="#" component="topic/reply/locked" class="d-flex gap-2 align-items-center fw-semibold btn btn-sm btn-primary disabled" disabled>
<i class="fa fa-lock"></i> [[topic:locked]]
</a>
{{{ end }}}
{{{ end }}}

{{{ if !locked }}}
<a href="#" component="topic/reply/locked" class="d-flex gap-2 align-items-center fw-semibold btn btn-sm btn-primary disabled hidden" disabled><i class="fa fa-lock"></i> [[topic:locked]]</a>
<a href="#" component="topic/reply/locked" class="d-flex gap-2 align-items-center fw-semibold btn btn-sm btn-primary disabled hidden" disabled>
<i class="fa fa-lock"></i> [[topic:locked]]
</a>
{{{ end }}}
{{{ else }}}
{{{ if !privileges.topics:reply }}}
<a component="topic/reply/guest" href="{config.relative_path}/login" class="d-flex align-items-center fw-semibold btn btn-sm btn-primary">[[topic:guest-login-reply]]</a>
<a component="topic/reply/guest" href="{config.relative_path}/login" class="d-flex align-items-center fw-semibold btn btn-sm btn-primary">
[[topic:guest-login-reply]]
</a>
{{{ end }}}
{{{ end }}}
1 change: 1 addition & 0 deletions public/language/en-GB/topic.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"notify-me": "Be notified of new replies in this topic",
"quote": "Quote",
"reply": "Reply",
"reply-anonymously": "Reply Anonymously",
"replies-to-this-post": "%1 Replies",
"one-reply-to-this-post": "1 Reply",
"last-reply-time": "Last reply",
Expand Down
1 change: 1 addition & 0 deletions public/language/en-US/topic.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"last-reply-time": "Last reply",
"reply-options": "Reply options",
"reply-as-topic": "Reply as topic",
"reply-anonymously": "Reply anonymously",
"guest-login-reply": "Log in to reply",
"login-to-view": "🔒 Log in to view",
"edit": "Edit",
Expand Down
2 changes: 2 additions & 0 deletions public/openapi/read/topic/topic_id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ get:
type: string
timestamp:
type: number
anonymous:
type: boolean
votes:
type: number
deleted:
Expand Down
2 changes: 2 additions & 0 deletions public/openapi/write/posts/pid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ get:
type: boolean
downvoted:
type: boolean
anonymous:
type: number
put:
tags:
- posts
Expand Down
20 changes: 20 additions & 0 deletions public/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ if (document.readyState === 'loading') {
hooks.fire('action:composer.post.new', params);
};

app.anonymousReply = async function (params) {
// For backwards compatibility, if someone calls app.anonymousReply(tid)
if (typeof params !== 'object') {
console.warn('[deprecated] app.anonymousReply(tid) — please pass in an object');
params = {
tid: params,
};
}
// We need hooks and api
const [hooks, api] = await app.require(['hooks', 'api']);
// If we're on a topic page, we can use ajaxify.data.titleRaw.
// Otherwise, fetch the topic info from the server.
params.title = (ajaxify.data.template.topic ?
ajaxify.data.titleRaw : (await api.get(`/topics/${params.tid}`)).titleRaw
);
// Mark this as an anonymous reply
params.anonymous = true;
// Fire the same composer hook as a normal reply, but with an 'anonymous' flag
hooks.fire('action:composer.post.new', params);
};
app.loadJQueryUI = function (callback) {
if (typeof $().autocomplete === 'function') {
return callback();
Expand Down
44 changes: 44 additions & 0 deletions public/src/client/topic/postTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ define('forum/topic/postTools', [
e.preventDefault();
onReplyClicked($(this), tid);
});
$('.topic').on('click', '[component="topic/replyAnonymously"]', function (e) {
e.preventDefault();
onAnonymousReplyClicked($(this), tid);
});


$('.topic').on('click', '[component="topic/reply-as-topic"]', function () {
translator.translate(`[[topic:link-back, ${ajaxify.data.titleRaw}, ${config.relative_path}/topic/${ajaxify.data.slug}]]`, function (body) {
Expand Down Expand Up @@ -269,9 +274,46 @@ define('forum/topic/postTools', [
}

async function onReplyClicked(button, tid) {
console.log('reply clicked');
const selectedNode = await getSelectedNode();
showStaleWarning(async function () {
let username = await getUserSlug(button);
if (getData(button, 'data-uid') === '0' || !getData(button, 'data-userslug')) {
username = '';
}

const toPid = button.is('[component="post/reply"]') ? getData(button, 'data-pid') : null;
const isQuoteToPid = !toPid || !selectedNode.pid || toPid === selectedNode.pid;

if (selectedNode.text && isQuoteToPid) {
username = username || selectedNode.username;
hooks.fire('action:composer.addQuote', {
tid: tid,
pid: toPid,
title: ajaxify.data.titleRaw,
username: username,
body: selectedNode.text,
selectedPid: selectedNode.pid,
});
} else {
console.log('non anonymous about to post');
hooks.fire('action:composer.post.new', {
tid: tid,
pid: toPid,
title: ajaxify.data.titleRaw,
body: username ? username + ' ' : ($('[component="topic/quickreply/text"]').val() || ''),
anonymous: false,
});
}
});
}

async function onAnonymousReplyClicked(button, tid) {
console.log('reply anonymously clicked');
const selectedNode = await getSelectedNode();

showStaleWarning(async function () {
console.log('entered showStaleWarning');
let username = await getUserSlug(button);
if (getData(button, 'data-uid') === '0' || !getData(button, 'data-userslug')) {
username = '';
Expand All @@ -291,11 +333,13 @@ define('forum/topic/postTools', [
selectedPid: selectedNode.pid,
});
} else {
console.log('anonymous reply about to fire hook!');
hooks.fire('action:composer.post.new', {
tid: tid,
pid: toPid,
title: ajaxify.data.titleRaw,
body: username ? username + ' ' : ($('[component="topic/quickreply/text"]').val() || ''),
anonymous: true,
});
}
});
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ exports.post = async function (req, res) {
};
req.body.noscript = 'true';

// ANONYMOUS OVERRIDE: If anonymous flag is set, post as Anonymous
if (body.anonymous) {
data.uid = 0;
data.username = 'Anonymous';
data.picture = '/assets/img/anonymous.png';
}

if (!data.content) {
return helpers.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
}
Expand Down
1 change: 1 addition & 0 deletions src/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function (Posts) {
tid: tid,
content: content,
timestamp: timestamp,
anonymous: Boolean(data.anonymous),
};

if (data.toPid) {
Expand Down
3 changes: 1 addition & 2 deletions src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const utils = require('../utils');
const intFields = [
'uid', 'pid', 'tid', 'deleted', 'timestamp',
'upvotes', 'downvotes', 'deleterUid', 'edited',
'replies', 'bookmarks',
'replies', 'bookmarks', 'anonymous',
];

module.exports = function (Posts) {
Posts.getPostsFields = async function (pids, fields) {
if (!Array.isArray(pids) || !pids.length) {
Expand Down
3 changes: 2 additions & 1 deletion src/topics/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ module.exports = function (Topics) {
data = await plugins.hooks.fire('filter:topic.reply', data);
const { tid } = data;
const { uid } = data;
const anonymous = !!data.anonymous;

const [topicData, isAdmin] = await Promise.all([
Topics.getTopicData(tid),
Expand All @@ -176,7 +177,7 @@ module.exports = function (Topics) {
await canReply(data, topicData);

data.cid = topicData.cid;

data.anonymous = Boolean(anonymous);
await guestHandleValid(data);
data.content = String(data.content || '').trimEnd();

Expand Down
Loading
Loading