|
|
@@ -1,16 +1,25 @@
|
|
|
-import { Service } from '@tsed/di';
|
|
|
-import axios from 'axios';
|
|
|
+import { Inject, Service } from '@tsed/di';
|
|
|
|
|
|
import { GrowiCommand, generateWebClient } from '@growi/slack';
|
|
|
import { AuthorizeResult } from '@slack/oauth';
|
|
|
|
|
|
import { GrowiCommandProcessor } from '~/interfaces/slack-to-growi/growi-command-processor';
|
|
|
-import { RelationRepository } from '~/repositories/relation';
|
|
|
import { Installation } from '~/entities/installation';
|
|
|
+import { Relation } from '~/entities/relation';
|
|
|
+import { RelationRepository } from '~/repositories/relation';
|
|
|
|
|
|
|
|
|
+export type SelectedGrowiInformation = {
|
|
|
+ relation: Relation,
|
|
|
+ growiCommand: GrowiCommand,
|
|
|
+ sendCommandBody: any,
|
|
|
+}
|
|
|
+
|
|
|
@Service()
|
|
|
-export class SelectRequestService implements GrowiCommandProcessor {
|
|
|
+export class SelectGrowiService implements GrowiCommandProcessor {
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ relationRepository: RelationRepository;
|
|
|
|
|
|
async process(growiCommand: GrowiCommand, authorizeResult: AuthorizeResult, body: {[key:string]:string } & {growiUris:string[]}): Promise<void> {
|
|
|
const { botToken } = authorizeResult;
|
|
|
@@ -68,40 +77,34 @@ export class SelectRequestService implements GrowiCommandProcessor {
|
|
|
}
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
- async forwardRequest(relationRepository:RelationRepository, installation:Installation | undefined, payload:any):Promise<void> {
|
|
|
+ async handleSelectInteraction(installation:Installation | undefined, payload:any): Promise<SelectedGrowiInformation> {
|
|
|
const { trigger_id: triggerId } = payload;
|
|
|
const { state, private_metadata: privateMetadata } = payload?.view;
|
|
|
const { value: growiUri } = state?.values?.select_growi?.growi_app?.selected_option;
|
|
|
|
|
|
const parsedPrivateMetadata = JSON.parse(privateMetadata);
|
|
|
- const { growiCommand, body } = parsedPrivateMetadata;
|
|
|
+ const { growiCommand, body: sendCommandBody } = parsedPrivateMetadata;
|
|
|
|
|
|
- if (growiCommand == null || body == null) {
|
|
|
- throw new Error('growiCommand and body are required.');
|
|
|
+ if (growiCommand == null || sendCommandBody == null) {
|
|
|
+ // TODO: postEphemeralErrors
|
|
|
+ throw new Error('growiCommand and body params are required in private_metadata.');
|
|
|
}
|
|
|
|
|
|
// ovverride trigger_id
|
|
|
- body.trigger_id = triggerId;
|
|
|
+ sendCommandBody.trigger_id = triggerId;
|
|
|
|
|
|
- const relation = await relationRepository.findOne({ installation, growiUri });
|
|
|
+ const relation = await this.relationRepository.findOne({ installation, growiUri });
|
|
|
|
|
|
if (relation == null) {
|
|
|
+ // TODO: postEphemeralErrors
|
|
|
throw new Error('No relation found.');
|
|
|
}
|
|
|
|
|
|
- /*
|
|
|
- * forward to GROWI server
|
|
|
- */
|
|
|
- // generate API URL
|
|
|
- const url = new URL('/_api/v3/slack-integration/proxied/commands', relation.growiUri);
|
|
|
- await axios.post(url.toString(), {
|
|
|
- ...body,
|
|
|
+ return {
|
|
|
+ relation,
|
|
|
growiCommand,
|
|
|
- }, {
|
|
|
- headers: {
|
|
|
- 'x-growi-ptog-tokens': relation.tokenPtoG,
|
|
|
- },
|
|
|
- });
|
|
|
+ sendCommandBody,
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
}
|