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

Redefine permission logic and Add tests for anonymous field and permissions #49

Merged
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4377c55
add demp.rdb
sophiefeng18 Oct 2, 2024
6082d94
updated f24 brach from team repository
sophiefeng18 Oct 2, 2024
f64506d
remove dump rdb
sophiefeng18 Oct 2, 2024
3252d6a
dump.rdb issues
sophiefeng18 Oct 2, 2024
e02caa6
Updating permission logic
sophiefeng18 Oct 2, 2024
7523581
implementing the logic in src/posts/summary.js instead of src/middlew…
sophiefeng18 Oct 2, 2024
c47134a
fixing lint test errors
sophiefeng18 Oct 2, 2024
2d746ad
fixing more lint test errors
sophiefeng18 Oct 2, 2024
584ddeb
demp.rdb issue
sophiefeng18 Oct 4, 2024
e45958a
fixing test case errors
sophiefeng18 Oct 4, 2024
553186c
npm install to solve test errors
sophiefeng18 Oct 4, 2024
78def58
minor changes to check test suites
sophiefeng18 Oct 6, 2024
1ea0e37
minor changes to get around a flaky test
sophiefeng18 Oct 6, 2024
72d1ee7
dump.rdb file change due to npm build tpl
sophiefeng18 Oct 9, 2024
0ed94d2
Added test for anonymous field and permissions, debugged to pass test
sophiefeng18 Oct 9, 2024
fa766f1
resolving conflict with the main branch
sophiefeng18 Oct 9, 2024
67996c3
resolving conflicts
sophiefeng18 Oct 9, 2024
d6867d2
resolving conflicts
sophiefeng18 Oct 9, 2024
0cca74d
resolving conflicts
sophiefeng18 Oct 9, 2024
737c1ad
resolving conflicts
sophiefeng18 Oct 9, 2024
cefd37e
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
e4684b1
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
4900fdd
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
d20eecd
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
d773a90
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
413d4ae
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
19eca16
resolving conflicts in summary.js
sophiefeng18 Oct 9, 2024
47a26e8
resolving conflicts in create.js
sophiefeng18 Oct 9, 2024
1ee787b
resolving conflicts in create.js
sophiefeng18 Oct 9, 2024
abc710e
resolving conflicts in create.js
sophiefeng18 Oct 9, 2024
e1899de
resolving conflicts in create.js
sophiefeng18 Oct 9, 2024
f00142a
changes to pass some test cases and cleaning up
sophiefeng18 Oct 10, 2024
4813401
commenting out unneeded code as addressed in code review
sophiefeng18 Oct 10, 2024
9ba02eb
commenting out unneeded code as addressed in code review
sophiefeng18 Oct 10, 2024
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 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.isAnon || 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].isAnon || 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
8 changes: 8 additions & 0 deletions src/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ 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.isAnon ? 'Anonymous User' : '';
delete post.isAnon;
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: 7 additions & 2 deletions src/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ module.exports = function (Posts) {
const content = data.content.toString();
const timestamp = data.timestamp || Date.now();
const isMain = data.isMain || false;
const isAnon = true;
// const anonymous = false; // hard code anonymous to become false
const anonymous = true; // hard code anonymous to become true
// attempted to get id from the tpl but we don't know how to do it
// const anonymous = data.getElementById('anonymousInput').value === 'true';
// log anonymous field to see which variable it is
// console.log('get anon value:', anonymous);

if (!uid && parseInt(uid, 10) !== 0) {
throw new Error('[[error:invalid-uid]]');
Expand All @@ -36,7 +41,7 @@ module.exports = function (Posts) {
tid: tid,
content: content,
timestamp: timestamp,
anonymous: isAnon,
anonymous: anonymous, // set anonymous datafield to be anonymous value
};

if (data.toPid) {
Expand Down
11 changes: 9 additions & 2 deletions src/posts/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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 @@ -45,14 +45,21 @@ 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];
post.category = post.topic && cidToCategory[post.topic.cid];
post.isMainPost = post.topic && post.pid === post.topic.mainPid;
post.deleted = post.deleted === 1;
post.timestampISO = utils.toISOString(post.timestamp);
post.anonymous = post.anonymous ? post.anonymous : 'false'; // checks if anonymous is true if not then false
post.anonymous = post.anonymous ? post.anonymous : 'false'; // makes sure anonymous is a required field for every post, false if anonymous field is undefined
});

posts = posts.filter(post => 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
43 changes: 43 additions & 0 deletions test/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,49 @@ describe('Post\'s', () => {
});
});

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

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

// 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], uid, {});
// 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
Loading