Skip to content

Commit

Permalink
Merge pull request #18 from CMU-313/anonymous_backend
Browse files Browse the repository at this point in the history
Added anonymous checkbox option for users when quick replying and adjusted the published post to be set to a default, masked display if anonymous
  • Loading branch information
RyanChernoff authored Feb 27, 2025
2 parents d85381e + 794690f commit 4d72094
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
21 changes: 16 additions & 5 deletions nodebb-theme-harmony/templates/partials/topic/post.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
{{{ end }}}
<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);">

{{{ if posts.contentAnonymous }}}
<!-- Render an anonymous avatar via CSS -->
<div class="anonymous-avatar"></div>
{{{ else }}}
<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}]]">
{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>
{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>
</a>
</div>
{{{ end }}}

</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}">
Expand All @@ -24,8 +31,12 @@
</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.contentAnonymous }}}
<a class="fw-bold text-nowrap" data-username="{posts.user.username}" data-uid="{posts.user.uid}">{"Anonymous"}</a>
{{{ 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 }}}
<!-- IMPORT partials/groups/badge.tpl -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</div>
<div>
<div class="d-flex justify-content-end gap-2">
<input type="checkbox" name="anonymous" component="topic/quickreply/anonymous" class="form-check-input" />
<button type="submit" component="topic/quickreply/expand" class="btn-ghost-sm border" title="[[topic:open-composer]]"><i class="fa fa-expand"></i></button>
<button type="submit" component="topic/quickreply/button" class="btn btn-sm btn-primary">[[topic:post-quick-reply]]</button>
</div>
Expand Down
2 changes: 2 additions & 0 deletions public/src/modules/quickreply.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ define('quickreply', [
}

const replyMsg = components.get('topic/quickreply/text').val();
const is_anonymous = components.get('topic/quickreply/anonymous').is(':checked');
const replyData = {
tid: ajaxify.data.tid,
handle: undefined,
content: replyMsg,
anonymous: is_anonymous,
};
const replyLen = replyMsg.length;
if (replyLen < parseInt(config.minimumPostLength, 10)) {
Expand Down
3 changes: 3 additions & 0 deletions src/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module.exports = function (Posts) {
postData.contentFlag = data.contentFlag;
}

// set contentAnonymous based on if the anonymous checkbox was checked
postData.contentAnonymous = data.anonymous;

let result = await plugins.hooks.fire('filter:post.create', { post: postData, data: data });
postData = result.post;
await db.setObject(`post:${postData.pid}`, postData);
Expand Down
18 changes: 18 additions & 0 deletions test/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ describe('Topic\'s', () => {
assert.equal(postData[1].contentFlag, 'true', 'result should have a content flag');
});

it('should be anonymous', async () => {
const result = await topics.reply({ uid: topic.userId, content: 'hello this is anonymous', tid: newTopic.tid, toPid: newPost.pid, anonymous: true });
assert.ok(result);

const postData = await apiPosts.getReplies({ uid: 0 }, { pid: newPost.pid });
assert.ok(postData);
assert(postData[2].contentAnonymous);
});

it('should not be anonymous', async () => {
const result = await topics.reply({ uid: topic.userId, content: 'hello this is not anonymous', tid: newTopic.tid, toPid: newPost.pid, anonymous: false });
assert.ok(result);

const postData = await apiPosts.getReplies({ uid: 0 }, { pid: newPost.pid });
assert.ok(postData);
assert(postData[3].contentAnonymous === 'false');
});

it('should error if pid is not a number', async () => {
await assert.rejects(
apiPosts.getReplies({ uid: 0 }, { pid: 'abc' }),
Expand Down

0 comments on commit 4d72094

Please sign in to comment.