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

21.05% Statements 12/57
100% Branches 0/0
0% Functions 0/1
21.05% Lines 12/57

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 581x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                                                                          
import type {IChatAccountMap, IChatAttachment, IChatSendParams} from '@feasibleone/blong/types';
import {handler} from '@feasibleone/blong/types';
 
/**
 * `viber.message.send` — the request `send_message` is made with.
 *
 * A text message, a picture or a location, because those are the three the realm produces; anything
 * else a platform can carry stays in the platform until something needs it.
 */
const first = (attachments: IChatAttachment[] | undefined, type: string) =>
    (attachments ?? []).find(attachment => attachment.contentType?.startsWith(type));
 
export default handler(({lib: {error, viberTokenGet, viberBaseGet}}) => {
    const errors = error({
        'viber.accountMissing': {
            message: 'No Viber account {account} is configured',
            statusCode: 404,
        },
    });

    return function viberMessageSendRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: IChatSendParams,
    ) {
        const token = viberTokenGet(this.config?.accounts, params.account);
        if (!token) throw errors['viber.accountMissing']({params: {account: params.account ?? ''}});
        const picture = first(params.attachments, 'image/');
        const location = params.attachments?.find(attachment => attachment.details);
        const body = picture
            ? {
                  type: 'picture',
                  text: params.text,
                  media: picture.url,
                  ...(picture.thumbnail && {thumbnail: picture.thumbnail}),
              }
            : location
              ? {
                    type: 'location',
                    location: {lat: location.details?.lat, lon: location.details?.lon},
                }
              : {type: 'text', text: params.text};
        return {
            url: new URL(
                'send_message',
                viberBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'send_message',
            method: 'POST',
            responseType: 'json',
            headers: {
                'x-viber-auth-token': token,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {receiver: params.conversationId, ...body},
        };
    };
});