Skip to content

Commit

Permalink
Adds role to topics upon posting and post replies
Browse files Browse the repository at this point in the history
  • Loading branch information
Evelyn Zheng committed Oct 10, 2024
1 parent 898ab02 commit e9f0428
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
Binary file added dump.rdb
Binary file not shown.
18 changes: 18 additions & 0 deletions src/categories/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ module.exports = function (Categories) {
const tids = await Categories.getTopicIds(results);
let topicsData = await topics.getTopicsByTids(tids, data.uid);
topicsData = await user.blocks.filter(data.uid, topicsData);

Check failure on line 19 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
const userRoles = await Promise.all(
topicsData.map(async (topic) => {

Check failure on line 21 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces
const isAdmin = await user.isAdministrator(topic.uid);

Check failure on line 22 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces
const isMod = await user.isModerator(topic.uid);

Check failure on line 23 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces

Check failure on line 24 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
if (isAdmin) {

Check failure on line 25 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces
return 'Administrator';

Check failure on line 26 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 5 tabs but found 20 spaces
} else if (isMod) {

Check failure on line 27 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces
return 'Moderator';

Check failure on line 28 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 5 tabs but found 20 spaces
} else {

Check failure on line 29 in src/categories/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces
return 'user';
}
})
);

topicsData.forEach((topic, index) => {
topic.authorRole = userRoles[index] || 'user';
});
if (!topicsData.length) {
return { topics: [], uid: data.uid };
}
Expand Down
15 changes: 9 additions & 6 deletions src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ const accountHelpers = require('./accounts/helpers');
const userController = module.exports;

userController.getCurrentUser = async function (req, res) {
if (!req.loggedIn) {
return res.status(401).json('not-authorized');
}
const userslug = await user.getUserField(req.uid, 'userslug');
const userData = await accountHelpers.getUserDataByUserSlug(userslug, req.uid, req.query);
res.json(userData);
if (!req.loggedIn) {
return res.status(401).json('not-authorized');
}
const userslug = await user.getUserField(req.uid, 'userslug');
const role = await user.isAdministrator(req.uid) ? 'Administrator' :
(user.isModerator(req.uid) ? 'Moderator' : 'user');
const userData = await accountHelpers.getUserDataByUserSlug(userslug, req.uid, req.query);
userData.role = role;
res.json(userData);
};

userController.getUserByUID = async function (req, res, next) {
Expand Down
4 changes: 4 additions & 0 deletions src/topics/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ module.exports = function (Topics) {
postData.forEach((postObj, i) => {
if (postObj) {
postObj.user = postObj.uid ? userData[postObj.uid] : { ...userData[postObj.uid] };
const isAdmin = user.isAdministrator(postObj.user);
const isMod = user.isModerator(postObj.user);
postObj.user.role = isAdmin ? 'Administrator' : (isMod ? 'Moderator' : 'user');
postObj.editor = postObj.editor ? editors[postObj.editor] : null;
postObj.bookmarked = bookmarks[i];
postObj.upvoted = voteData.upvotes[i];
Expand All @@ -144,6 +147,7 @@ module.exports = function (Topics) {
if (meta.config.allowGuestHandles && postObj.uid === 0 && postObj.handle) {
postObj.user.username = validator.escape(String(postObj.handle));
postObj.user.displayname = postObj.user.username;
postObj.user.role = 'guest';
}
}
});
Expand Down
11 changes: 10 additions & 1 deletion src/views/partials/data/category.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
data-tid="{topics.tid}" data-index="{topics.index}" data-cid="{topics.cid}" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"
<div data-tid="{topics.tid}"
data-index="{topics.index}"
data-cid="{topics.cid}"
data-role="{topics.authorRole}"
itemprop="itemListElement"
itemscope
itemtype="https://schema.org/ListItem">
<span class="badge bg-info text-white ms-2">
{topics.authorRole}
</span>

0 comments on commit e9f0428

Please sign in to comment.