Creating new categories using plugins in NodeBB

I have implemented a function to create new categories using NodeBB plugins. This article explains how to implement this. Purpose As a forum for developers that I am currently developing, I would like to be able to handle the root category as a community. For example, if you create a root category such as JavaScript and PHP, you will be able to access it with a URL such as https://example.com/javascript. Each root category (community) has three groups. Administrator Member Ban (blacklist) These groups are used to set category permissions. Implementation Overview The implemented functions are as follows. Displaying the modal for creating communities WebSocket communication from the client side Creating categories on the server side Setting permissions Adding subcategories Adding a template First, we'll add a template for displaying the modal for creating communities. This will be placed in the templates directory as a .tpl file. Create a community × Name Description Cancel Create This template uses the Bootstrap modal component. The modal is identified by id=‘community-create-modal’ and the form input values can be obtained by the names name and description. Client-side implementation On the client-side, we will add JavaScript and CSS (Less). These are specified in plugin.json and loaded. { "id": "nodebb-plugin-caiz", "name": "NodeBB Plugin for Caiz", "description": "NodeBB Plugin for Caiz", "version": "1.0.0", "library": "./library.js", "staticDirs": { "static": "./static" }, "scripts": [ "static/modal.js" ], "less": [ "static/style.less" ] } In plugin.json, the following settings are made: staticDirs: Specify the directory for static files (JavaScript, CSS, images, etc.) scripts: Specify the JavaScript files to be loaded on the client side less: Specify the Less files to be loaded on the client side The client-side JavaScript implements the display of the modal and WebSocket communication. 'use strict'; async function getAlert() { return new Promise((resolve, reject) => { require(['alerts'], resolve); }); } $(document).ready(function () { // Trigger of showing modal $(document).on('click', '#create-community-trigger', function (e) { e.preventDefault(); $('#community-create-modal').modal('show'); }); // Click event in the modal $('#submit-community-create').on('click', async () => { const form = $('#community-create-form'); const formData = form.serializeArray().reduce((obj, item) => { obj[item.name] = item.value; return obj; }, {}); // Client validation const { alert } = await getAlert(); if (!formData.name) { alert({ type: 'warning', message: '[[caiz:error.name_required]]', timeout: 3000, }); return; } // Disable to the button const submitBtn = $(this); submitBtn.prop('disabled', true).addClass('disabled'); // Send to server by WebSocket socket.emit('plugins.caiz.createCommunity', formData, function(err, response) { if (err) { alert({ type: 'error', message: err.message || '[[caiz:error.generic]]', timeout: 3000, }); } else { $('#community-create-modal').modal('hide'); alert({ type: 'success', message: '[[caiz:success.community_created]]', timeout: 3000, }); // Redirect to community you made ajaxify.go(`/${response.community.handle}`); } form[0].reset(); submitBtn.prop('disabled', false).removeClass('disabled'); }); }); }); In this code: getAlert() loads the NodeBB alert module Set up the trigger to display the modal Monitor the click event on the create button: Get the form data and convert it into an object Perform client-side validation Disable the button to prevent double submissions Send the data to the server via WebSocket If successful, display the alert and redirect If there is an error, display the error message After processing is complete, reset the form and enable the button Notes In NodeBB, server-side functions are called using WebSocket, not Ajax. WebSocket functions are specified in module.exports.sockets, and if a function such as module.exports.sockets.caiz.createCommunity is defined on the server side, it can be called on the client side in an RPC-like way, for example

Apr 5, 2025 - 07:16
 0
Creating new categories using plugins in NodeBB

I have implemented a function to create new categories using NodeBB plugins. This article explains how to implement this.

Purpose

As a forum for developers that I am currently developing, I would like to be able to handle the root category as a community. For example, if you create a root category such as JavaScript and PHP, you will be able to access it with a URL such as https://example.com/javascript.

Each root category (community) has three groups.

  • Administrator
  • Member
  • Ban (blacklist)

These groups are used to set category permissions.

Implementation Overview

The implemented functions are as follows.

  • Displaying the modal for creating communities
  • WebSocket communication from the client side
  • Creating categories on the server side
  • Setting permissions
  • Adding subcategories

Adding a template

First, we'll add a template for displaying the modal for creating communities. This will be placed in the templates directory as a .tpl file.

 class="modal fade" id="community-create-modal" tabindex="-1" role="dialog">
   class="modal-dialog" role="document">
     class="modal-content">
       class="modal-header">
         class="modal-title">Create a community
         type="button" class="close" data-dismiss="modal">
          ×
        
      
class="modal-body"> id="community-create-form"> class="form-group"> for="name">Name type="text" class="form-control" id="name" name="name" required>
class="form-group"> for="description">Description class="form-control" id="description" name="description" rows="3">