Skip to content

Commit

Permalink
Redefine permission logic and Add tests for anonymous field and permi…
Browse files Browse the repository at this point in the history
…ssions (#49)

* add demp.rdb

* updated f24 brach from team repository

* dump.rdb issues

* Updating permission logic

* implementing the logic in src/posts/summary.js instead of src/middleware/user.js

* fixing lint test errors

* fixing more lint test errors

* demp.rdb issue

* fixing test case errors

* npm install to solve test errors

* minor changes to check test suites

* minor changes to get around a flaky test

* dump.rdb file change due to npm build tpl

* Added test for anonymous field and permissions, debugged to pass test

* resolving conflict with the main branch

* resolving conflicts

* resolving conflicts

* resolving conflicts

* resolving conflicts

* resolving conflicts in summary.js

* resolving conflicts in summary.js

* resolving conflicts in summary.js

* resolving conflicts in summary.js

* resolving conflicts in summary.js

* resolving conflicts in summary.js

* resolving conflicts in summary.js

* resolving conflicts in create.js

* resolving conflicts in create.js

* resolving conflicts in create.js

* resolving conflicts in create.js

* changes to pass some test cases and cleaning up

* commenting out unneeded code as addressed in code review

* commenting out unneeded code as addressed in code review
  • Loading branch information
sophiefeng18 authored Oct 10, 2024
1 parent e79f821 commit f3afeea
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
Binary file modified dump.rdb
Binary file not shown.
9 changes: 9 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ postsAPI.get = async function (caller, data) {
post.content = '[[topic:post-is-deleted]]';
}

// Reference to ChatGPT
// Add the anonymous field to the response
// post.anonymous = post.anonymous || false;

return post;
};

Expand All @@ -62,6 +66,11 @@ postsAPI.getSummary = async (caller, { pid }) => {

const postsData = await posts.getPostSummaryByPids([pid], caller.uid, { stripTags: false });
posts.modifyPostByPrivilege(postsData[0], topicPrivileges);

// Reference to ChatGPT
// Add the anonymous field to the post summary
// postsData[0].anonymous = postsData[0].anonymous || false;

return postsData[0];
};

Expand Down
1 change: 1 addition & 0 deletions src/controllers/admin/privileges.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ privilegesController.get = async function (req, res) {
}
}
});

if (!selectedCategory) {
selectedCategory = await categories.getCategoryFields(cid, ['cid', 'name', 'icon', 'bgColor', 'color']);
}
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ searchController.search = async function (req, res, next) {
recordSearch(data),
]);

// Reference to ChatGPT
// Add anonymous field to each post
searchData.posts = searchData.posts.map((post) => {
post.anonymous = post.anonymous ? 'Anonymous User' : '';
return post;
});

searchData.pagination = pagination.create(page, searchData.pageCount, req.query);
searchData.multiplePages = searchData.pageCount > 1;
searchData.search_query = validator.escape(String(req.query.term || ''));
Expand Down
10 changes: 10 additions & 0 deletions src/middleware/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ const passportAuthenticateAsync = function (req, res) {
});
};

// permisssions for admin to see username on anonymous posts
// const showUsername = async (userId, currentUser) => {
// const isAdmin = await user.isAdministrator(currentUser.uid);

// if (isAnonymous && !isAdmin) {
// return 'Anonymous User';
// }

// return user.getDisplayName(userId);
// };

module.exports = function (middleware) {
async function authenticate(req, res) {
Expand Down
9 changes: 8 additions & 1 deletion src/posts/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (Posts) {
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];

const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);
const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'anonymous', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);

let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
Expand All @@ -44,6 +44,13 @@ module.exports = function (Posts) {
post.uid = 0;
}
post.user = uidToUser[post.uid];
// Check if the post is anonymous
// Reference to ChatGPT
if (post.anonymous && !user.isAdministrator(uid)) {
post.user.username = 'Anonymous User';
} else {
post.user.username = users.find(u => u.uid === post.uid).username;
}
Posts.overrideGuestHandle(post, post.handle);
post.handle = undefined;
post.topic = tidToTopic[post.tid];
Expand Down
2 changes: 1 addition & 1 deletion src/upgrades/1.4.4/sound_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const map = {
'waterdrop-low.mp3': 'Default | Water drop (low)',
};

console.log('Sophie Feng');
// console.log('Sophie Feng');
function soundSettings(cb) {
const keys = ['chat-incoming', 'chat-outgoing', 'notification'];

Expand Down
46 changes: 46 additions & 0 deletions test/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,57 @@ describe('Post\'s', () => {
assert(data[0].user);
assert(data[0].topic);
assert(data[0].category);
assert(data[0].anonymous); // Anonymous data field exists
done();
});
});
});

// tests for admin permission to see anonymous poster's user name
// Reference to ChatGPT
describe('Post Anonymity', () => {
let uid;
let postData;
let adminUid;
let uid2;

before(async () => {
// Create two regular user
uid = await user.create({ username: 'regularuser' });
uid2 = await user.create({ username: 'regularuser2' });

// Create an admin user
adminUid = await user.create({ username: 'adminuser' });
await groups.join('administrators', adminUid); // Make the user an admin

// Create an anonymous post
({ postData } = await topics.post({
uid: uid,
cid: cid,
title: 'Anonymous Post Test',
content: 'This is an anonymous post',
anonymous: true,
}));
});

it('should display "Anonymous User" if the post is anonymous and the user is not an admin', async () => {
// Simulate a request by a non-admin user
const postSummary = await posts.getPostSummaryByPids([postData.pid], uid2, {});
// console.log(postSummary[0]);

// Check that the username is set to "Anonymous User"
assert.strictEqual(postSummary[0].user.username, 'Anonymous User');
});

it('should display the real username if the post is anonymous but the user is an admin', async () => {
// Simulate a request by an admin user
const postSummary = await posts.getPostSummaryByPids([postData.pid], adminUid, {});
// console.log(postSummary[0]);
// Check that the username is the actual user's username
assert.strictEqual(postSummary[0].user.username, 'regularuser');
});
});

it('should get recent poster uids', (done) => {
topics.reply({
uid: voterUid,
Expand Down

0 comments on commit f3afeea

Please sign in to comment.