Creating tags from file?
-
@NodeHam please check this topic , @baris provided codes to ease this process previously:
https://community.nodebb.org/topic/16520/tag-list-to-choose-from
-
Thanks. That topic might not be what I'm after.
From what I read, that sounds like someone wants fixed tags then preventing users from entering them.In my application, I need to have a huge list of pre-entered tags but I also want users to enter their own as they post.
-
Here is a slightly modified version of Baris' script that reads a list of tags from a JSON file (tags.json) and creates them
'use strict'; const nconf = require('nconf'); nconf.file({ file: 'config.json', }); nconf.defaults({ base_dir: __dirname, views_dir: './build/public/templates', upload_path: 'public/uploads', }); var db = require('./src/database'); const meta = require('./src/meta'); const categories = require('./src/categories'); const cache = require('./src/cache'); const tags = require('./tags.json'); db.init(function (err) { if (err) { console.log('NodeBB could not connect to your database. Error: ' + err.message); process.exit(); } setTags(function (err) { console.log('setting'); if (err) { console.error(err); process.exit(); } console.log('done'); process.exit(); }); }); async function createEmptyTag(tag) { if (!tag) { throw new Error('[[error:invalid-tag]]'); } const isMember = await db.isSortedSetMember('tags:topic:count', tag); console.log(1, isMember); if (!isMember) { await db.sortedSetAdd('tags:topic:count', 0, tag); cache.del('tags:topic:count'); } const allCids = await categories.getAllCidsFromSet('categories:cid'); const isMembers = await db.isMemberOfSortedSets( allCids.map(cid => `cid:${cid}:tags`), tag ); const bulkAdd = allCids.filter((cid, index) => !isMembers[index]) .map(cid => ([`cid:${cid}:tags`, 0, tag])); await db.sortedSetAddBulk(bulkAdd); console.log('added ' + tag); }; async function setTags(callback) { tags.forEach(async (tag) => { console.log('adding tags', tag); await createEmptyTag(tag) }); //callback(); }
Copyright © 2024 NodeBB | Contributors