浏览代码

dev: add godmode toggle command for apps

KernelDeimos 3 月之前
父节点
当前提交
5513e1c93b
共有 1 个文件被更改,包括 44 次插入0 次删除
  1. 44 0
      src/backend/src/modules/selfhosted/SelfhostedService.js

+ 44 - 0
src/backend/src/modules/selfhosted/SelfhostedService.js

@@ -16,7 +16,10 @@
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
+const { Actor } = require("../../services/auth/Actor");
 const BaseService = require("../../services/BaseService");
+const { DB_WRITE } = require("../../services/database/consts");
+const { Context } = require("../../util/context");
 
 class SelfhostedService extends BaseService {
     static description = `
@@ -24,6 +27,47 @@ class SelfhostedService extends BaseService {
     `
 
     async _init () {
+        this._register_commands(this.services.get('commands'));
+    }
+ 
+    _register_commands (commands) {
+        const db = this.services.get('database').get(DB_WRITE, 'selfhosted');
+        commands.registerCommands('app', [
+            {
+                id: 'godmode-on',
+                description: 'Toggle godmode for an app',
+                handler: async (args, log) => {
+                    const svc_su = this.services.get('su');
+                    await await svc_su.sudo(async () => {
+                        const [app_uid] = args;
+                        const es_app = await this.services.get('es:app');
+                        const app = await es_app.read(app_uid);
+                        if ( ! app ) {
+                            throw new Error(`App ${app_uid} not found`);
+                        }
+                        await db.write('UPDATE apps SET godmode = 1 WHERE uid = ?', [app_uid]);
+                    });
+                }
+            }
+        ]);
+        commands.registerCommands('app', [
+            {
+                id: 'godmode-off',
+                description: 'Toggle godmode for an app',
+                handler: async (args, log) => {
+                    const svc_su = this.services.get('su');
+                    await await svc_su.sudo(async () => {
+                        const [app_uid] = args;
+                        const es_app = await this.services.get('es:app');
+                        const app = await es_app.read(app_uid);
+                        if ( ! app ) {
+                            throw new Error(`App ${app_uid} not found`);
+                        }
+                        await db.write('UPDATE apps SET godmode = 0 WHERE uid = ?', [app_uid]);
+                    });
+                }
+            }
+        ]);
     }
 }