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

[Issues #21,#22,#23] Role Display Feature #24

Open
wants to merge 7 commits into
base: f24
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 added dump.rdb
Binary file not shown.
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/PostObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ PostObject:
type: string
description: An URL-safe variant of the username (i.e. lower-cased, spaces
removed, etc.)
role:
type: string
picture:
type: string
nullable: true
Expand Down
13 changes: 13 additions & 0 deletions public/openapi/components/schemas/TopicObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ TopicObject:
type: string
description: An URL-safe variant of the username (i.e. lower-cased, spaces
removed, etc.)
role:
type: string
description: A description of the user's role
reputation:
type: number
postcount:
Expand Down Expand Up @@ -79,6 +82,7 @@ TopicObject:
- uid
- username
- userslug
- role
- reputation
- postcount
- picture
Expand All @@ -101,6 +105,11 @@ TopicObject:
tid:
type: number
description: A topic identifier
authorRole:
type: string
role:
type: string
description: topic poster's role
content:
type: string
timestampISO:
Expand Down Expand Up @@ -178,6 +187,8 @@ TopicObject:
description: A topic identifier
thumb:
type: string
role:
type: string
pinExpiry:
type: number
description: A UNIX timestamp indicating when a pinned topic will no longer be pinned (i.e. the pin has expired)
Expand All @@ -199,6 +210,8 @@ TopicObjectSlim:
uid:
type: number
description: A user identifier
role:
type: string
cid:
type: number
description: A category identifier
Expand Down
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/UserObj.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ UserObj:
userslug:
type: string
example: dragon-fruit
role:
type: string
email:
type: string
example: [email protected]
Expand Down
3 changes: 3 additions & 0 deletions public/openapi/components/schemas/UserObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ UserObject:
type: string
description: A friendly name for a given user account
example: Dragon Fruit
role:
type: string
userslug:
type: string
description: An URL-safe variant of the username (i.e. lower-cased, spaces removed, etc.)
Expand Down Expand Up @@ -168,6 +170,7 @@ UserObject:
- uid
- username
- userslug
- role
- 'email:confirmed'
- joindate
- lastonline
Expand Down
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/admin/tokenObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ TokenObject:
uid:
type: number
description: A valid user id
role:
type: string
description:
type: string
description: Optional descriptor to differentiate tokens.
Expand Down
2 changes: 1 addition & 1 deletion src/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ usersAPI.follow = async function (caller, data) {
toUid: data.uid,
});

const userData = await user.getUserFields(caller.uid, ['username', 'userslug']);
const userData = await user.getUserFields(caller.uid, ['username', 'userslug', 'role']);
const { displayname } = userData;

const notifObj = await notifications.create({
Expand Down
17 changes: 17 additions & 0 deletions src/categories/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ module.exports = function (Categories) {
let topicsData = await topics.getTopicsByTids(tids, data.uid);
topicsData = await user.blocks.filter(data.uid, topicsData);

const userRoles = await Promise.all(
topicsData.map(async (topic) => {
const isAdmin = await user.isAdministrator(topic.uid);
const isMod = await user.isModerator(topic.uid);

if (isAdmin) {
return 'Administrator';
} else if (isMod) {
return 'Moderator';
}
return 'user';
})
);

topicsData.forEach((topic, index) => {
topic.role = userRoles[index] || 'user';
});
if (!topicsData.length) {
return { topics: [], uid: data.uid };
}
Expand Down
9 changes: 9 additions & 0 deletions src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ userController.getCurrentUser = async function (req, res) {
return res.status(401).json('not-authorized');
}
const userslug = await user.getUserField(req.uid, 'userslug');
let role = 'user';
const isAdmin = await user.isAdministrator(req.uid);
const isMod = await user.isModerator(req.uid);
if (isAdmin) {
role = 'Administrator';
} else if (isMod) {
role = 'Moderator';
}
const userData = await accountHelpers.getUserDataByUserSlug(userslug, req.uid, req.query);
userData.role = role;
res.json(userData);
};

Expand Down
9 changes: 9 additions & 0 deletions src/topics/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ 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 = 'user';
if (isAdmin) {
postObj.user.role = 'Administrator';
} else if (isMod) {
postObj.user.role = 'Moderator';
}
postObj.editor = postObj.editor ? editors[postObj.editor] : null;
postObj.bookmarked = bookmarks[i];
postObj.upvoted = voteData.upvotes[i];
Expand All @@ -144,6 +152,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>
18 changes: 18 additions & 0 deletions test/topics/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ describe('Topic Events', () => {
});
});

describe('.getUserRole()', () => {
it('should ensure each topic has a user with a role', async () => {
const topicData = await topics.getTopic(topic.topicData.tid);
assert(topicData);
assert(topicData.user);
let role = 'user';
const isAdmin = await user.isAdministrator(topicData.user.uid);
const isMod = await user.isModerator(topicData.user.uid);
if (isAdmin) {
role = 'Administrator';
} else if (isMod) {
role = 'Moderator';
}

assert.strictEqual(topicData.user.role, role);
});
});

describe('.purge()', () => {
let eventIds;

Expand Down
Loading