mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-27 01:39:53 +01:00
feat: note attachments via link preview plugin
This commit is contained in:
54
src/posts/attachments.js
Normal file
54
src/posts/attachments.js
Normal file
@@ -0,0 +1,54 @@
|
||||
'use strict';
|
||||
|
||||
const crypto = require('crypto');
|
||||
|
||||
const db = require('../database');
|
||||
|
||||
const Attachments = module.exports;
|
||||
|
||||
Attachments.get = async (pid) => {
|
||||
const hashes = await db.getSortedSetMembers(`post:${pid}:attachments`);
|
||||
const keys = hashes.map(hash => `attachment:${hash}`);
|
||||
const attachments = (await db.getObjects(keys)).filter(Boolean);
|
||||
|
||||
return attachments;
|
||||
};
|
||||
|
||||
Attachments.update = async (pid, attachments) => {
|
||||
if (!attachments) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bulkOps = {
|
||||
hash: [],
|
||||
zset: {
|
||||
score: [],
|
||||
value: [],
|
||||
},
|
||||
};
|
||||
|
||||
attachments.filter(Boolean).forEach(({ _type, mediaType, url, name, width, height }, idx) => {
|
||||
if (!url) { // only required property
|
||||
return;
|
||||
}
|
||||
|
||||
const hash = crypto.createHash('sha256').update(url).digest('hex');
|
||||
const key = `attachment:${hash}`;
|
||||
|
||||
if (_type) {
|
||||
_type = 'attachment';
|
||||
}
|
||||
|
||||
bulkOps.hash.push([key, { _type, mediaType, url, name, width, height }]);
|
||||
bulkOps.zset.score.push(idx);
|
||||
bulkOps.zset.value.push(hash);
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
db.setObjectBulk(bulkOps.hash),
|
||||
db.sortedSetAdd(`post:${pid}:attachments`, bulkOps.zset.score, bulkOps.zset.value),
|
||||
]);
|
||||
};
|
||||
|
||||
// todo
|
||||
// Attachments.remove = async (pid) => { ... }
|
||||
@@ -27,6 +27,8 @@ require('./queue')(Posts);
|
||||
require('./diffs')(Posts);
|
||||
require('./uploads')(Posts);
|
||||
|
||||
Posts.attachments = require('./attachments');
|
||||
|
||||
Posts.exists = async function (pids) {
|
||||
return await db.exists(
|
||||
Array.isArray(pids) ? pids.map(pid => `post:${pid}`) : `post:${pids}`
|
||||
|
||||
Reference in New Issue
Block a user