Skip to content

Commit

Permalink
feat: add a migration
Browse files Browse the repository at this point in the history
  • Loading branch information
oplik0 committed Aug 4, 2022
1 parent f84616c commit 7a4e679
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "nodebb"
"extends": "nodebb",
"parserOptions": {
"ecmaVersion": 2020
}
}
5 changes: 3 additions & 2 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"typescript": {
"useTabs": true,
"quoteStyle": "preferSingle"
"quoteStyle": "preferSingle",
"functionExpression.spaceBeforeParentheses": true
},
"json": {
"useTabs": true
Expand All @@ -19,4 +20,4 @@
"https://plugins.dprint.dev/json-0.15.4.wasm",
"https://plugins.dprint.dev/markdown-0.13.3.wasm"
]
}
}
34 changes: 20 additions & 14 deletions library.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
'use strict';

const { escapeHTML } = require.main.require('./src/utils');
const { events, getTopicField } = require.main.require('./src/topics');
const { getPostData } = require.main.require('./src/posts');
const { addSystemMessage } = require.main.require('./src/messaging');
const { emitToUids } = require.main.require('./src/socket.io/helpers');
const { getUidsFromSet } = require.main.require('./src/user');
const { filterUids } = require.main.require('./src/privileges/categories');
const winston = require.main.require('winston');
const {
DiceRoller,
NumberGenerator,
Expand All @@ -16,16 +8,30 @@ const {
Results: { RollResults },
Dice: { FudgeDice },
} = require('@dice-roller/rpg-dice-roller');
const runMigrations = require('./migrations');

const { escapeHTML } = require.main.require('./src/utils');
const { events, getTopicField } = require.main.require('./src/topics');
const { getPostData } = require.main.require('./src/posts');
const { addSystemMessage } = require.main.require('./src/messaging');
const { emitToUids } = require.main.require('./src/socket.io/helpers');
const { getUidsFromSet } = require.main.require('./src/user');
const { filterUids } = require.main.require('./src/privileges/categories');

const plugin = {};

const dice = ['d2', 'd4', 'd6', 'd8', 'd10', 'd12', 'd20', 'dF'];

const roller = new DiceRoller();

const generator = NumberGenerator.generator;
const { generator } = NumberGenerator;
generator.engines = NumberGenerator.engines.nodeCrypto;

plugin.addTopicEvents = async function({ types }) {
plugin.init = async function () {
await runMigrations('ws-dice-settings');
};

plugin.addTopicEvents = async function ({ types }) {
types.dice = {
icon: 'fa-dice',
};
Expand Down Expand Up @@ -194,19 +200,19 @@ async function parseChatCommands(message) {
return results;
}

plugin.createPost = async function({ post, data }) {
plugin.createPost = async function ({ post, data }) {
post.content = post.content.replaceAll(/^\s*\/\u200B+roll/giu, () => '/roll');
post = await parseCommands(post);
post.content = post.content.replaceAll(/^\s*\/roll/g, () => '/\u200Broll');
return { post, data };
};
plugin.editPost = async function({ post, data }) {
plugin.editPost = async function ({ post, data }) {
let postData = await getPostData(data.pid);
postData = await parseCommands(postData);
post.content = post.content.replaceAll(/^\s*\/roll/g, () => '/\u200Broll');
return { post, data };
};
plugin.parsePost = async function({ postData }) {
plugin.parsePost = async function ({ postData }) {
if (postData.content.split('<p').length > 2) {
postData.content = postData.content.replaceAll(
/^(<p dir="auto">)?\/\u200B?roll(?<rollData>[^#\n]+)(?<rollComment>#[^\n]+)?(<br>|<\/p>|$)/gimu,
Expand All @@ -223,7 +229,7 @@ plugin.parsePost = async function({ postData }) {
return { postData };
};

plugin.onSentMessage = async function({ message, data }) {
plugin.onSentMessage = async function ({ message, data }) {
let results = await parseChatCommands(message);

for (const result of results ?? []) {
Expand Down
20 changes: 20 additions & 0 deletions migrations/2.3.0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const { scan, setObjectBulk, getObjects } = require.main.require('./src/database');
async function useSpan() {
const migrationRegex = /^(?!<span class="dice-event-text">).+/;
const keys = await scan({ match: 'topicEvent:*' });
const events = await getObjects(keys);
const modifiedEvents = events
.map((event, index) => [keys[index], event])
.filter(event => event[1].type === 'dice' && migrationRegex.test(event[1].text))
.map(
(event) => {
event[1].text = `<span class="dice-event-text">${event[1].text}</span>`;
return event;
},
);
await setObjectBulk(modifiedEvents);
}

module.exports = useSpan;
25 changes: 25 additions & 0 deletions migrations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const fs = require('node:fs/promises');
const semver = require('semver');

const { settings } = require.main.require('./src/meta');

async function runMigrations(settings_key) {
const plugin_settings = await settings.get(settings_key);
let { version } = plugin_settings ?? {};
if (!semver.valid(version)) {
version = '0.0.0';
}
const files = await fs.readdir(__dirname);
const migrations = files
.map(filename => semver.clean(filename.replace('.js', '')))
.filter(filename => semver.valid(filename) && semver.gt(filename, version));
for (const migration of migrations) {
// eslint-disable-next-line no-await-in-loop
await require(`./${migration}`)();
version = semver.gt(migration, version) ? migration : version;
}
await settings.set(settings_key, { version });
}
module.exports = runMigrations;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodebb-plugin-ws-dice",
"version": "2.2.3",
"version": "2.3.0",
"description": "A dice plugin integrated with topic events",
"main": "library.js",
"scripts": {
Expand Down Expand Up @@ -53,7 +53,8 @@
},
"dependencies": {
"@dice-roller/rpg-dice-roller": "^5.1.0",
"expressionparser": "^1.1.5"
"expressionparser": "^1.1.5",
"semver": "^7.3.7"
},
"packageManager": "[email protected]"
}
4 changes: 4 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"url": "https://github.com/NodeBB/nodebb-plugin-ws-dice",
"library": "./library.js",
"hooks": [
{
"hook": "static:app.load",
"method": "init"
},
{
"hook": "filter:topicEvents.init",
"method": "addTopicEvents"
Expand Down
5 changes: 2 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a4e679

Please sign in to comment.