-
Notifications
You must be signed in to change notification settings - Fork 87
/
helpers.ts
78 lines (67 loc) · 2.63 KB
/
helpers.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
Attachment,
ExternalAttachment,
PollAttachment,
GiftAttachment,
WallAttachment,
LinkAttachment,
PhotoAttachment,
AudioAttachment,
StoryAttachment,
VideoAttachment,
MarketAttachment,
StickerAttachment,
GraffitiAttachment,
DocumentAttachment,
WallReplyAttachment,
MarketAlbumAttachment,
AudioMessageAttachment,
} from '.';
import { API } from '../../api';
import { AttachmentType } from '../../utils/constants';
const attachmentsTypes = {
[AttachmentType.ALBUM]: undefined,
[AttachmentType.PODCAST]: undefined,
[AttachmentType.TEXTLIVE]: undefined,
[AttachmentType.POLL]: (): typeof PollAttachment => PollAttachment,
[AttachmentType.GIFT]: (): typeof GiftAttachment => GiftAttachment,
[AttachmentType.WALL]: (): typeof WallAttachment => WallAttachment,
[AttachmentType.LINK]: (): typeof LinkAttachment => LinkAttachment,
[AttachmentType.PHOTO]: (): typeof PhotoAttachment => PhotoAttachment,
[AttachmentType.AUDIO]: (): typeof AudioAttachment => AudioAttachment,
[AttachmentType.STORY]: (): typeof StoryAttachment => StoryAttachment,
[AttachmentType.VIDEO]: (): typeof VideoAttachment => VideoAttachment,
[AttachmentType.DOCUMENT]: (): typeof DocumentAttachment => DocumentAttachment,
[AttachmentType.MARKET]: (): typeof MarketAttachment => MarketAttachment,
[AttachmentType.STICKER]: (): typeof StickerAttachment => StickerAttachment,
[AttachmentType.GRAFFITI]: (): typeof GraffitiAttachment => GraffitiAttachment,
[AttachmentType.WALL_REPLY]: (): typeof WallReplyAttachment => WallReplyAttachment,
[AttachmentType.MARKET_ALBUM]: (): typeof MarketAlbumAttachment => MarketAlbumAttachment,
[AttachmentType.AUDIO_MESSAGE]: (): typeof AudioMessageAttachment => AudioMessageAttachment,
};
/**
* Transform raw attachments to wrapper
*/
export const transformAttachments = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rawAttachments: any[],
api: API,
// eslint-disable-next-line function-paren-newline
): (Attachment | ExternalAttachment)[] => {
const attachments: (Attachment | ExternalAttachment)[] = [];
for (const rawAttachment of rawAttachments) {
const type = rawAttachment.type as AttachmentType;
const attachmentFactory = attachmentsTypes[type];
if (attachmentFactory === undefined) {
continue;
}
const AttachmentConstructor = attachmentFactory();
attachments.push(
new AttachmentConstructor({
api,
payload: rawAttachment[type],
}),
);
}
return attachments;
};