interaction-payload-accessor.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import assert from 'assert';
  2. import { IInteractionPayloadAccessor } from '../interfaces/request-from-slack';
  3. import loggerFactory from './logger';
  4. const logger = loggerFactory('@growi/slack:utils:interaction-payload-accessor');
  5. export class InteractionPayloadAccessor implements IInteractionPayloadAccessor {
  6. private payload: any;
  7. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  8. constructor(payload: any) {
  9. this.payload = payload;
  10. }
  11. firstAction(): any | null {
  12. const actions = this.payload.actions;
  13. if (actions != null && actions[0] != null) {
  14. return actions[0];
  15. }
  16. return null;
  17. }
  18. getResponseUrl(): string {
  19. const responseUrl = this.payload.response_url;
  20. if (responseUrl != null) {
  21. return responseUrl;
  22. }
  23. const responseUrls = this.payload.response_urls;
  24. assert(responseUrls != null);
  25. assert(responseUrls[0] != null);
  26. return responseUrls[0].response_url;
  27. }
  28. getStateValues(): any | null {
  29. const state = this.payload.state;
  30. if (state != null && state.values != null) {
  31. return state.values;
  32. }
  33. const view = this.payload.view;
  34. if (view != null && view.state != null && view.state.values != null) {
  35. return view.state.values;
  36. }
  37. return null;
  38. }
  39. getViewPrivateMetaData(): any | null {
  40. const view = this.payload.view;
  41. if (view != null && view.private_metadata) {
  42. return JSON.parse(view.private_metadata);
  43. }
  44. return null;
  45. }
  46. getActionIdAndCallbackIdFromPayLoad(): {[key: string]: string} {
  47. const actionId = this.firstAction()?.action_id || '';
  48. const callbackId = this.payload.view?.callback_id || '';
  49. return { actionId, callbackId };
  50. }
  51. getChannelName(): string | null {
  52. // private_metadata should have the channelName parameter when view_submission
  53. const privateMetadata = this.getViewPrivateMetaData();
  54. if (privateMetadata != null && privateMetadata.channelName != null) {
  55. return privateMetadata.channelName;
  56. }
  57. const channel = this.payload.channel;
  58. if (channel != null) {
  59. return this.payload.channel.name;
  60. }
  61. return null;
  62. }
  63. getOriginalData(): any | null {
  64. const value = this.firstAction()?.value;
  65. if (value == null) return null;
  66. const { originalData } = JSON.parse(value);
  67. if (originalData == null) return JSON.parse(value);
  68. let parsedOriginalData;
  69. try {
  70. parsedOriginalData = JSON.parse(originalData);
  71. }
  72. catch (err) {
  73. logger.error('Failed to parse original data:\n', err);
  74. return null;
  75. }
  76. return parsedOriginalData;
  77. }
  78. }