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

Feat: Post Queue enchancements - Thumbs & Tags #9680

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/posts/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ module.exports = function (Posts) {
const id = `${type}-${now}`;
await canPost(type, data);

if (type === 'topic') {
const { uuid: oldUUID } = data;
const hasThumbs = await topics.thumbs.hasThumbs(oldUUID);
if (hasThumbs) {
data.uuid = utils.generateUUID();
await topics.thumbs.migrateToUuid(oldUUID, data.uuid);
}
}

let payload = {
id: id,
uid: data.uid,
Expand Down Expand Up @@ -232,6 +241,12 @@ module.exports = function (Posts) {
if (!data) {
return;
}
if (data.type === 'topic') {
const hasThumbs = await topics.thumbs.hasThumbs(data.data.uuid);
if (hasThumbs) {
await topics.thumbs.deleteAll(data.data.uuid);
}
}
await removeQueueNotification(id);
await db.sortedSetRemove('post:queue', id);
await db.delete(`post:queue:${id}`);
Expand Down Expand Up @@ -267,6 +282,10 @@ module.exports = function (Posts) {

async function createTopic(data) {
const result = await topics.post(data);
const hasThumbs = await topics.thumbs.hasThumbs(data.uuid);
if (hasThumbs) {
await topics.thumbs.migrate(data.uuid, result.topicData.tid);
}
socketHelpers.notifyNew(data.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
return result;
}
Expand Down
53 changes: 53 additions & 0 deletions src/topics/thumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ const cache = require('../cache');

const Thumbs = module.exports;

Thumbs.hasThumbs = async function (id) {
const isDraft = validator.isUUID(String(id));
const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;
const count = await db.sortedSetCard(set);
return count > 0;
};

Thumbs.exists = async function (id, path) {
const isDraft = validator.isUUID(String(id));
const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;
Expand Down Expand Up @@ -108,6 +115,14 @@ Thumbs.migrate = async function (uuid, id) {
cache.del(set);
};

Thumbs.migrateToUuid = async function (uuid, newUuid) {
// Converts the draft thumb zset to the topic zset (combines thumbs if applicable)
const set = `draft:${uuid}:thumbs`;
const newSet = `draft:${newUuid}:thumbs`;
await db.rename(set, newSet);
cache.del(set);
};

Thumbs.delete = async function (id, relativePath) {
const isDraft = validator.isUUID(String(id));
const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;
Expand Down Expand Up @@ -137,3 +152,41 @@ Thumbs.delete = async function (id, relativePath) {
}
}
};

Thumbs.deleteAll = async function (id) {
const isDraft = validator.isUUID(String(id));
const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;

const thumbs = await db.getSortedSetRange(set, 0, -1);
const absolutePaths = thumbs.map(thumb => path.posix.join(nconf.get('upload_path'), thumb));
const existsOnDisk = await Promise.all(absolutePaths.map(el => file.exists(el)));

const filesToDeletePromises = [];
const postsToDissociatePromises = [];

for (let i = 0, len = thumbs.length; i < len; i++) {
const thumb = thumbs[i];

if (existsOnDisk[i]) {
filesToDeletePromises.push(file.delete(absolutePaths[i]));
}

// Dissociate thumbnails with the main pid
if (!isDraft) {
const topics = require('.');
postsToDissociatePromises.push(
topics.getTopicsFields(id, ['mainPid']).then(
mainPid => {console.log(mainPid); posts.uploads.dissociate(mainPid, thumb.replace('/files/', ''))}
)
);
}
}

await Promise.all(filesToDeletePromises);
await Promise.all(postsToDissociatePromises);

await db.delete(set);
await db.deleteObjectField(`topic:${id}`, 'numThumbs');

cache.del(set);
};