1
|
|
|
import {ClosableType, PollConfig, PollFormInputs, PollState} from './interfaces'; |
2
|
|
|
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1'; |
3
|
|
|
import {MAX_NUM_OF_OPTIONS} from '../config/default'; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Add a new option to the state(like DB) |
7
|
|
|
* |
8
|
|
|
* @param {string} option - the new option name |
9
|
|
|
* @param {object} state - the current message state |
10
|
|
|
* @param {string} creator - who add the new option |
11
|
|
|
* @returns {void} card |
12
|
|
|
*/ |
13
|
|
|
export function addOptionToState(option: string, state: PollState, creator = '') { |
14
|
|
|
const choiceLength = state.choices.length; |
15
|
|
|
state.choices.push(option); |
16
|
|
|
if (state.choiceCreator === undefined) { |
17
|
|
|
state.choiceCreator = {[choiceLength]: creator}; |
18
|
|
|
} else { |
19
|
|
|
state.choiceCreator[choiceLength] = creator; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) { |
24
|
|
|
const card = event.message?.cardsV2?.[0]?.card as chatV1.Schema$GoogleAppsCardV1Card; |
25
|
|
|
return getStateFromCardName(card) || getStateFromParameter(event) || getStateFromCardWhenNoHeader(card) || |
26
|
|
|
getStateFromCardWhenHasHeader(card); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
export function getConfigFromInput(formValues: PollFormInputs) { |
30
|
|
|
const state: PollConfig = {topic: '', choices: []}; |
31
|
|
|
state.topic = formValues.topic.stringInputs!.value![0]!.trim() ?? ''; |
32
|
|
|
state.anon = formValues.is_anonymous?.stringInputs!.value![0] === '1'; |
33
|
|
|
state.optionable = formValues.allow_add_option?.stringInputs!.value![0] === '1'; |
34
|
|
|
state.type = parseInt(formValues.type?.stringInputs!.value![0] ?? '1') as ClosableType; |
35
|
|
|
|
36
|
|
|
for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) { |
37
|
|
|
const choice = formValues[`option${i}`]?.stringInputs!.value![0]!.trim(); |
38
|
|
|
if (choice) { |
39
|
|
|
state.choices.push(choice); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
return state; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function getStateFromCardWhenNoHeader(card: chatV1.Schema$GoogleAppsCardV1Card) { |
46
|
|
|
return card?.sections?.[0].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function getStateFromCardWhenHasHeader(card: chatV1.Schema$GoogleAppsCardV1Card) { |
50
|
|
|
// when has header the first section is header |
51
|
|
|
return card?.sections?.[1].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function getStateFromCardName(card: chatV1.Schema$GoogleAppsCardV1Card) { |
55
|
|
|
// when has header the first section is header |
56
|
|
|
return card?.name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function getStateFromParameter(event: chatV1.Schema$DeprecatedEvent) { |
60
|
|
|
const parameters = event.common?.parameters; |
61
|
|
|
|
62
|
|
|
return parameters?.['state']; |
63
|
|
|
} |
64
|
|
|
|