src/handlers/MessageHandler.ts   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 10

Size

Lines of Code 55
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 46
mnd 9
bc 9
fnc 1
dl 0
loc 55
rs 10
bpm 9
cpm 10
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C MessageHandler.process 0 44 10
1
import {chat_v1 as chatV1} from '@googleapis/chat';
2
import BaseHandler from './BaseHandler';
3
import {splitMessage} from '../helpers/utils';
4
import PollCard from '../cards/PollCard';
5
import {generateHelpText, helperButtonCard} from '../helpers/helper';
6
7
export default class MessageHandler extends BaseHandler {
8
  process(): chatV1.Schema$Message {
9
    const argumentText = this.event.message?.argumentText?.trim() ?? '';
10
11
    const helpResponse = {
12
      thread: this.event.message!.thread,
13
      actionResponse: {
14
        type: 'NEW_MESSAGE',
15
      },
16
      text: '',
17
      cardsV2: [helperButtonCard],
18
    };
19
    const isPrivate = this.event.space?.type === 'DM';
20
21
    switch (argumentText) {
22
      case 'help':
23
        helpResponse.text = generateHelpText(isPrivate);
24
        return helpResponse;
25
      case 'test dyas':
26
        helpResponse.text = 'Hello <https://github.com/dyaskur/google-chat-poll|google-chat-poll>';
27
        return helpResponse;
28
      default:
29
        const choices = splitMessage(argumentText);
30
        const annotation = this.getAnnotationByType('USER_MENTION');
31
        if (annotation && choices.length > 2) {
32
          const pollCard = new PollCard({
33
            choiceCreator: undefined,
34
            topic: choices.shift() ?? '',
35
            author: this.event.user,
36
            choices: choices,
37
            votes: {},
38
            anon: false,
39
            optionable: true,
40
          }, this.getUserTimezone());
41
          const message = pollCard.createMessage();
42
          return {
43
            thread: this.event.message!.thread,
44
            actionResponse: {
45
              type: 'NEW_MESSAGE',
46
            },
47
            ...message,
48
          };
49
        }
50
        helpResponse.text = generateHelpText(isPrivate);
51
        return helpResponse;
52
    }
53
  }
54
}
55