All files / core/blong-gogo/src/adapter/server/viber viberPromptSettleRequestSend.ts

20.33% Statements 12/59
100% Branches 0/0
100% Functions 0/0
20.33% Lines 12/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                
import type {IChatAccountMap, IChatPromptSettleParams} from '@feasibleone/blong/types';
import {handler} from '@feasibleone/blong/types';
 
/**
 * `viber.prompt.settle` — the question's own message, rewritten once it is answered.
 *
 * Viber edits by `message_token`, the id `send_message` answered with, and the keyboard is what has to
 * go: a message whose buttons no longer answer anything is the same trap the Slack side removes
 * (`@@D-692`). The answer is written as text because a Viber message has no blocks — a ticked option
 * cannot be marked in place, so the answer is named instead.
 */
export default handler(({lib: {error, viberTokenGet, viberBaseGet}}) => {
    const errors = error({
        'viber.accountMissing': {
            message: 'No Viber account {account} is configured',
            statusCode: 404,
        },
        'viber.messageUnknown': {
            message:
                'The Viber message for prompt {promptId} was never reported, so it cannot be edited',
            statusCode: 409,
        },
    });

    return function viberPromptSettleRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: IChatPromptSettleParams,
    ) {
        const token = viberTokenGet(this.config?.accounts, params.account);
        if (!token) throw errors['viber.accountMissing']({params: {account: params.account ?? ''}});
        if (!params.messageId)
            throw errors['viber.messageUnknown']({params: {promptId: params.prompt.promptId}});
        return {
            url: new URL(
                'edit_message',
                viberBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'edit_message',
            method: 'POST',
            responseType: 'json',
            headers: {
                'x-viber-auth-token': token,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {
                receiver: params.conversationId,
                message_token: Number(params.messageId),
                type: 'text',
                // Viber has no mentions, so the name is written out; without one the answer is
                // named instead, the same way the Slack line falls back (`@@D-693`).
                // Viber has no mentions, so the name is written out; without one the answer speaks for
                // itself, the same way the Slack line falls back (`@@D-693`).
                text: `${params.prompt.text} — ${
                    params.answer.sender?.name ?? params.answer.sender?.id ?? 'Answer'
                } answered: ${params.answerText}`,
            },
        };
    };
});