From 4cd2267d6490137085aedbffb4467066c2810dd9 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 5 Jul 2023 14:58:06 +0800 Subject: [PATCH] feat: create discover plugin and define db schema --- .../{{id}}/web/plugins/{{id}}/src/index.tsx | 5 ++- server/packages/sdk/src/db/typegoose.ts | 1 + .../com.msgbyte.discover/.ministarrc.js | 14 ++++++ .../com.msgbyte.discover/models/discover.ts | 30 +++++++++++++ .../plugins/com.msgbyte.discover/package.json | 20 +++++++++ .../services/discover.service.dev.ts | 45 +++++++++++++++++++ .../com.msgbyte.discover/manifest.json | 9 ++++ .../plugins/com.msgbyte.discover/package.json | 16 +++++++ .../com.msgbyte.discover/src/index.tsx | 1 + .../com.msgbyte.discover/tsconfig.json | 7 +++ .../com.msgbyte.discover/types/tailchat.d.ts | 2 + 11 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 server/plugins/com.msgbyte.discover/.ministarrc.js create mode 100644 server/plugins/com.msgbyte.discover/models/discover.ts create mode 100644 server/plugins/com.msgbyte.discover/package.json create mode 100644 server/plugins/com.msgbyte.discover/services/discover.service.dev.ts create mode 100644 server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/manifest.json create mode 100644 server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/package.json create mode 100644 server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/src/index.tsx create mode 100644 server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/tsconfig.json create mode 100644 server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/types/tailchat.d.ts diff --git a/apps/cli/templates/server-plugin-full/{{id}}/web/plugins/{{id}}/src/index.tsx b/apps/cli/templates/server-plugin-full/{{id}}/web/plugins/{{id}}/src/index.tsx index 304ed5ea..ca5e1c45 100644 --- a/apps/cli/templates/server-plugin-full/{{id}}/web/plugins/{{id}}/src/index.tsx +++ b/apps/cli/templates/server-plugin-full/{{id}}/web/plugins/{{id}}/src/index.tsx @@ -1 +1,4 @@ -console.log('Plugin {{name}} is loaded'); +const PLUGIN_ID = '{{id}}'; +const PLUGIN_NAME = '{{name}}'; + +console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`); diff --git a/server/packages/sdk/src/db/typegoose.ts b/server/packages/sdk/src/db/typegoose.ts index 41b85ab7..50b14ac0 100644 --- a/server/packages/sdk/src/db/typegoose.ts +++ b/server/packages/sdk/src/db/typegoose.ts @@ -3,6 +3,7 @@ export { prop, modelOptions, Severity, + index, } from '@typegoose/typegoose'; export type { DocumentType, Ref, ReturnModelType } from '@typegoose/typegoose'; export { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses'; diff --git a/server/plugins/com.msgbyte.discover/.ministarrc.js b/server/plugins/com.msgbyte.discover/.ministarrc.js new file mode 100644 index 00000000..3c4db179 --- /dev/null +++ b/server/plugins/com.msgbyte.discover/.ministarrc.js @@ -0,0 +1,14 @@ +const path = require('path'); + +module.exports = { + externalDeps: [ + 'react', + 'react-router', + 'axios', + 'styled-components', + 'zustand', + 'zustand/middleware/immer', + ], + pluginRoot: path.resolve(__dirname, './web'), + outDir: path.resolve(__dirname, '../../public'), +}; diff --git a/server/plugins/com.msgbyte.discover/models/discover.ts b/server/plugins/com.msgbyte.discover/models/discover.ts new file mode 100644 index 00000000..d5bd23d6 --- /dev/null +++ b/server/plugins/com.msgbyte.discover/models/discover.ts @@ -0,0 +1,30 @@ +import { db } from 'tailchat-server-sdk'; +import { Group } from '../../../models/group/group'; +const { getModelForClass, prop, modelOptions, TimeStamps } = db; + +@modelOptions({ + options: { + customName: 'p_discover', + }, +}) +export class Discover extends TimeStamps implements db.Base { + _id: db.Types.ObjectId; + id: string; + + @prop({ ref: () => Group }) + groupId: db.Ref; + + @prop({ default: true }) + active: boolean; + + @prop({ default: 0 }) + order: number; +} + +export type DiscoverDocument = db.DocumentType; + +const model = getModelForClass(Discover); + +export type DiscoverModel = typeof model; + +export default model; diff --git a/server/plugins/com.msgbyte.discover/package.json b/server/plugins/com.msgbyte.discover/package.json new file mode 100644 index 00000000..6ecd1301 --- /dev/null +++ b/server/plugins/com.msgbyte.discover/package.json @@ -0,0 +1,20 @@ +{ + "name": "tailchat-plugin-discover", + "version": "1.0.0", + "main": "index.js", + "author": "moonrailgun", + "description": "Add Discover panel which can help user found groups", + "license": "MIT", + "private": true, + "scripts": { + "build:web": "ministar buildPlugin all", + "build:web:watch": "ministar watchPlugin all" + }, + "devDependencies": { + "@types/react": "18.0.20", + "mini-star": "*" + }, + "dependencies": { + "tailchat-server-sdk": "*" + } +} diff --git a/server/plugins/com.msgbyte.discover/services/discover.service.dev.ts b/server/plugins/com.msgbyte.discover/services/discover.service.dev.ts new file mode 100644 index 00000000..52e1548f --- /dev/null +++ b/server/plugins/com.msgbyte.discover/services/discover.service.dev.ts @@ -0,0 +1,45 @@ +import type { TcContext } from 'tailchat-server-sdk'; +import { TcService, TcDbService } from 'tailchat-server-sdk'; +import type { DiscoverDocument, DiscoverModel } from '../models/discover'; + +/** + * Discover + * + * Add Discover panel which can help user found groups + */ +interface DiscoverService + extends TcService, + TcDbService {} +class DiscoverService extends TcService { + get serviceName() { + return 'plugin:com.msgbyte.discover'; + } + + onInit() { + this.registerLocalDb(require('../models/discover').default); + + this.registerAction('all', this.all, { + params: { + page: { type: 'number', default: 1 }, + size: { type: 'number', default: 20 }, + }, + }); + } + + async all(ctx: TcContext<{ page: number; size: number }>) { + const { page, size } = ctx.params; + + const docs = await this.adapter.model + .find({ + active: true, + }) + .limit(size) + .skip(size * (page - 1)); + + const list = await this.transformDocuments(ctx, {}, docs); + + return { list }; + } +} + +export default DiscoverService; diff --git a/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/manifest.json b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/manifest.json new file mode 100644 index 00000000..6a3510da --- /dev/null +++ b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/manifest.json @@ -0,0 +1,9 @@ +{ + "label": "Discover", + "name": "com.msgbyte.discover", + "url": "{BACKEND}/plugins/com.msgbyte.discover/index.js", + "version": "0.0.0", + "author": "moonrailgun", + "description": "Add Discover panel which can help user found groups", + "requireRestart": true +} diff --git a/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/package.json b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/package.json new file mode 100644 index 00000000..16a3ecf2 --- /dev/null +++ b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/package.json @@ -0,0 +1,16 @@ +{ + "name": "@plugins/com.msgbyte.discover", + "main": "src/index.tsx", + "version": "0.0.0", + "description": "Add Discover panel which can help user found groups", + "private": true, + "scripts": { + "sync:declaration": "tailchat declaration github" + }, + "dependencies": {}, + "devDependencies": { + "@types/styled-components": "^5.1.26", + "react": "18.2.0", + "styled-components": "^5.3.6" + } +} diff --git a/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/src/index.tsx b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/src/index.tsx new file mode 100644 index 00000000..07705b5d --- /dev/null +++ b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/src/index.tsx @@ -0,0 +1 @@ +console.log('Plugin Discover is loaded'); diff --git a/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/tsconfig.json b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/tsconfig.json new file mode 100644 index 00000000..d9b47ed0 --- /dev/null +++ b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "jsx": "react", + "importsNotUsedAsValues": "error" + } +} diff --git a/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/types/tailchat.d.ts b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/types/tailchat.d.ts new file mode 100644 index 00000000..49f524ae --- /dev/null +++ b/server/plugins/com.msgbyte.discover/web/plugins/com.msgbyte.discover/types/tailchat.d.ts @@ -0,0 +1,2 @@ +declare module '@capital/common'; +declare module '@capital/component';