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

Popularity test #86

Closed
wants to merge 3 commits into from
Closed
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
29 changes: 28 additions & 1 deletion src/categories/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,35 @@ module.exports = function (Categories) {
most_votes: `cid:${cid}:tids:votes`,
most_views: `cid:${cid}:tids:views`,
};
let set;
if (sort === 'popularity') {
const voteSortedSet = `cid:${cid}:tids:votes`;
const createSortedSet = `cid:${cid}:tids:create`;
const postIdsWithScores = await db.getSortedSetRangeWithScores(voteSortedSet, 0, -1);
const tempSortedSet = `cid:${cid}:tids:popular`;
await db.delete(tempSortedSet);
const recentIdsWithScores = await db.getSortedSetRangeWithScores(createSortedSet, 0, -1);

let set = sortToSet.hasOwnProperty(sort) ? sortToSet[sort] : `cid:${cid}:tids`;
const currentTime = Date.now();
for (let i = 0; i < postIdsWithScores.length; i += 1) {
const postId = postIdsWithScores[i];
const votes = postIdsWithScores[i];
const result = recentIdsWithScores.find(item => item.value === postId.value);
const postCreationTime = result ? result.score : null;
if (postCreationTime) {
const timeSinceCreation = (currentTime - postCreationTime) / 3600000;
const votes_num = votes.score + 1;
const time = (timeSinceCreation + 2) ** 1.8;
const score = votes_num / time;
db.sortedSetAdd(tempSortedSet, score, postId.value);
}
}


set = tempSortedSet;
} else {
set = sortToSet.hasOwnProperty(sort) ? sortToSet[sort] : `cid:${cid}:tids`;
}

if (data.tag) {
if (Array.isArray(data.tag)) {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const categoryController = module.exports;
const url = nconf.get('url');
const relative_path = nconf.get('relative_path');
const validSorts = [
'recently_replied', 'recently_created', 'most_posts', 'most_votes', 'most_views',
'recently_replied', 'recently_created', 'most_posts', 'most_votes', 'most_views', 'popularity',
];

categoryController.get = async function (req, res, next) {
Expand Down
1 change: 1 addition & 0 deletions src/views/admin/settings/post.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<select id="categoryTopicSort" class="form-select" data-field="categoryTopicSort">
<option value="recently_replied">[[admin/settings/post:sorting.recently-replied]]</option>
<option value="recently_created">[[admin/settings/post:sorting.recently-created]]</option>
<option value="popularity">[[admin/settings/post:sorting.popularity]]</option>
<option value="most_posts">[[admin/settings/post:sorting.most-posts]]</option>
<option value="most_votes">[[admin/settings/post:sorting.most-votes]]</option>
<option value="most_views">[[admin/settings/post:sorting.most-views]]</option>
Expand Down
14 changes: 14 additions & 0 deletions test/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,20 @@ describe('Controllers', () => {
assert.equal(body.topics[1].postcount, 1);
});

it('should sort topics by popularity', async () => {
const category = await categories.create({ name: 'popularity-category' });
const t1 = await topics.post({ uid: fooUid, cid: category.cid, title: 'topic 1', content: 'topic 1 OP' });
const t2 = await topics.post({ uid: fooUid, cid: category.cid, title: 'topic 2', content: 'topic 2 OP' });
await topics.vote({ uid: fooUid, tid: t1.topicData.tid, direction: 1 });
await topics.reply({ uid: fooUid, content: 'topic 1 reply', tid: t1.topicData.tid });
await topics.vote({ uid: fooUid, tid: t2.topicData.tid, direction: 2 });
await topics.reply({ uid: fooUid, content: 'topic 2 reply', tid: t2.topicData.tid });
const { response, body } = await request.get(`${nconf.get('url')}/api/category/${category.slug}?sort=popularity`, { jar });
assert.equal(response.statusCode, 200);
assert.equal(body.topics[0].title, 'topic 2');
assert.equal(body.topics[1].title, 'topic 1');
});

it('should load a specific users topics from a category with tags', async () => {
const category = await categories.create({ name: 'filtered-category' });
await topics.post({ uid: fooUid, cid: category.cid, title: 'topic 1', content: 'topic 1 OP', tags: ['java', 'cpp'] });
Expand Down
Loading