mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: thumbs.associate logic fix + tests
This commit is contained in:
@@ -62,7 +62,7 @@ async function getThumbs(set) {
|
|||||||
if (cached !== undefined) {
|
if (cached !== undefined) {
|
||||||
return cached.slice();
|
return cached.slice();
|
||||||
}
|
}
|
||||||
const thumbs = await db.getSortedSetRange(set, 0, -1);
|
const thumbs = await db.getSortedSetRangeByScore(set, 0, -1, '-inf', '+inf');
|
||||||
cache.set(set, thumbs);
|
cache.set(set, thumbs);
|
||||||
return thumbs.slice();
|
return thumbs.slice();
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ Thumbs.associate = async function ({ id, path, score }) {
|
|||||||
path = path.replace(nconf.get('upload_path'), '');
|
path = path.replace(nconf.get('upload_path'), '');
|
||||||
}
|
}
|
||||||
const topics = require('.');
|
const topics = require('.');
|
||||||
await db.sortedSetAdd(set, score || numThumbs, path);
|
await db.sortedSetAdd(set, isFinite(score) ? score : numThumbs, path);
|
||||||
if (!isDraft) {
|
if (!isDraft) {
|
||||||
const numThumbs = await db.sortedSetCard(set);
|
const numThumbs = await db.sortedSetCard(set);
|
||||||
await topics.setTopicField(id, 'numThumbs', numThumbs);
|
await topics.setTopicField(id, 'numThumbs', numThumbs);
|
||||||
@@ -97,6 +97,7 @@ Thumbs.migrate = async function (uuid, id) {
|
|||||||
// Converts the draft thumb zset to the topic zset (combines thumbs if applicable)
|
// Converts the draft thumb zset to the topic zset (combines thumbs if applicable)
|
||||||
const set = `draft:${uuid}:thumbs`;
|
const set = `draft:${uuid}:thumbs`;
|
||||||
const thumbs = await db.getSortedSetRangeWithScores(set, 0, -1);
|
const thumbs = await db.getSortedSetRangeWithScores(set, 0, -1);
|
||||||
|
console.log(thumbs);
|
||||||
await Promise.all(thumbs.map(async thumb => await Thumbs.associate({
|
await Promise.all(thumbs.map(async thumb => await Thumbs.associate({
|
||||||
id,
|
id,
|
||||||
path: thumb.value,
|
path: thumb.value,
|
||||||
|
|||||||
@@ -167,6 +167,31 @@ describe('Topic thumbs', () => {
|
|||||||
assert.strictEqual(parseInt(numThumbs, 10), 2);
|
assert.strictEqual(parseInt(numThumbs, 10), 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should successfully associate a thumb with a topic even if it already contains that thumbnail (updates score)', async () => {
|
||||||
|
await topics.thumbs.associate({
|
||||||
|
id: tid,
|
||||||
|
path: relativeThumbPaths[0],
|
||||||
|
});
|
||||||
|
|
||||||
|
const score = await db.sortedSetScore(`topic:2:thumbs`, relativeThumbPaths[0]);
|
||||||
|
|
||||||
|
assert(isFinite(score)); // exists in set
|
||||||
|
assert.strictEqual(score, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update the score to be passed in as the third argument', async () => {
|
||||||
|
await topics.thumbs.associate({
|
||||||
|
id: tid,
|
||||||
|
path: relativeThumbPaths[0],
|
||||||
|
score: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const score = await db.sortedSetScore(`topic:2:thumbs`, relativeThumbPaths[0]);
|
||||||
|
|
||||||
|
assert(isFinite(score)); // exists in set
|
||||||
|
assert.strictEqual(score, 0);
|
||||||
|
});
|
||||||
|
|
||||||
it('should associate the thumbnail with that topic\'s main pid\'s uploads', async () => {
|
it('should associate the thumbnail with that topic\'s main pid\'s uploads', async () => {
|
||||||
const uploads = await posts.uploads.list(mainPid);
|
const uploads = await posts.uploads.list(mainPid);
|
||||||
assert(uploads.includes(path.basename(relativeThumbPaths[0])));
|
assert(uploads.includes(path.basename(relativeThumbPaths[0])));
|
||||||
@@ -193,13 +218,13 @@ describe('Topic thumbs', () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
name: 'example.org',
|
name: 'test2.png',
|
||||||
url: 'https://example.org',
|
url: `${nconf.get('relative_path')}${nconf.get('upload_url')}${relativeThumbPaths[1]}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
name: 'test2.png',
|
name: 'example.org',
|
||||||
url: `${nconf.get('relative_path')}${nconf.get('upload_url')}${relativeThumbPaths[1]}`,
|
url: 'https://example.org',
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user