Skip to content

Commit

Permalink
quickaction api
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Apr 12, 2024
1 parent 2b02fc7 commit 9819a8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
6 changes: 0 additions & 6 deletions d.ts/src/helpers/socket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@ export type ClientToServerEventsWithNamespace = {
'generic::save': generic<WidgetCustomInterface>['save'];
'generic::deleteById': generic<WidgetCustomInterface>['deleteById'];
},
'/widgets/quickaction': GenericEvents & {
'generic::deleteById': generic<QuickActions>['deleteById'],
'generic::save': generic<QuickActions>['save'],
'generic::getAll': (userId: string, cb: (error: Error | string | null | unknown, items: Readonly<Required<QuickActions>>[]) => void) => void,
'trigger': (data: { user: { userId: string, userName: string }, id: string, value?: any}) => void,
},
'/widgets/social': GenericEvents & {
'generic::getAll': generic<WidgetSocialInterface>['getAll'];
},
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ const withScope = (allowedScopes: string[], isPublic: boolean = false) => {
}

const token = jwt.verify(authToken, socket.JWTKey) as {
userId: string; username: string; privileges: Unpacked<ReturnType<typeof getPrivileges>>;
userId: string; userName: string; privileges: Unpacked<ReturnType<typeof getPrivileges>>;
};

token.privileges.scopes = token.privileges.scopes || [];
req.headers.scopes = token.privileges.scopes.sort();
req.headers.authUser = { userId: token.userId, userName: token.userName };

if (!token.privileges.scopes.some(scope => allowedScopes.includes(scope))) {
req.headers.private = false;
Expand Down
45 changes: 23 additions & 22 deletions src/widgets/quickaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { setValueOf } from '../helpers/customvariables/index.js';
import { info } from '../helpers/log.js';

import { AppDataSource } from '~/database.js';
import { adminEndpoint } from '~/helpers/socket.js';
import { Delete, Get, Post } from '~/decorators/endpoint.js';

const trigger = async (item: QuickActions.Item, user: { userId: string, userName: string }, value?: string) => {
info(`Quick Action ${item.id} triggered by ${user.userName}#${user.userId}`);
Expand Down Expand Up @@ -47,27 +47,28 @@ const trigger = async (item: QuickActions.Item, user: { userId: string, userName
};

class QuickAction extends Widget {
public sockets() {
adminEndpoint('/widgets/quickaction', 'generic::deleteById', async (id, cb) => {
try {
const item = await AppDataSource.getRepository(QuickActionEntity).findOneByOrFail({ id });
await AppDataSource.getRepository(QuickActionEntity).remove(item);
cb(null);
} catch (e) {
cb(e as Error);
}
});
adminEndpoint('/widgets/quickaction', 'generic::save', async (item, cb) => {
cb(null, await AppDataSource.getRepository(QuickActionEntity).save(item));
});
adminEndpoint('/widgets/quickaction', 'generic::getAll', async (userId, cb) => {
const items = await AppDataSource.getRepository(QuickActionEntity).find({ where: { userId } });
cb(null, items);
});
adminEndpoint('/widgets/quickaction', 'trigger', async ({ user, id, value }) => {
const item = await AppDataSource.getRepository(QuickActionEntity).findOneByOrFail({ id, userId: user.userId });
trigger(item, { userId: user.userId, userName: user.userName }, value);
});
@Get('/', 'read')
getAll(req: any) {
return AppDataSource.getRepository(QuickActionEntity).findBy({ userId: req.headers.authUser.userId });
}

@Delete('/:id')
async deleteOne(req: any) {
const it = await AppDataSource.getRepository(QuickActionEntity).delete({ id: req.params.id, userId: req.headers.authUser.userId });
if (it.affected === 0) {
throw new Error();
}
}

@Post('/')
saveOne(req: any) {
return AppDataSource.getRepository(QuickActionEntity).save(req.body);
}

@Post('/:id', { action: 'trigger' })
async trigger(req: any) {
const item = await AppDataSource.getRepository(QuickActionEntity).findOneByOrFail({ id: req.params.id, userId: req.headers.authUser.userId });
trigger(item, { userId: req.headers.authUser.userId, userName: req.headers.authUser.userName }, req.body.value);
}
}

Expand Down

0 comments on commit 9819a8c

Please sign in to comment.