/
vk-app-payload.ts
78 lines (66 loc) · 1.96 KB
/
vk-app-payload.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 { Context, ContextFactoryOptions, ContextDefaultState } from './context';
import { pickProperties } from '../../utils/helpers';
import { kSerializeData } from '../../utils/constants';
export type VKAppPayloadContextType = 'vk_app_event';
export type VKAppPayloadContextSubType = 'app_payload';
export interface IVKAppPayloadPayload {
user_id: number;
app_id: number;
payload: string;
group_id: number;
}
export type VKAppPayloadContextOptions<S> =
ContextFactoryOptions<IVKAppPayloadPayload, S>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class VKAppPayloadContext<S = ContextDefaultState, P extends Record<string, any> = object>
extends Context<
IVKAppPayloadPayload,
S,
VKAppPayloadContextType,
VKAppPayloadContextSubType
> {
public constructor(options: VKAppPayloadContextOptions<S>) {
super({
...options,
type: 'vk_app_event',
subTypes: [
options.updateType as VKAppPayloadContextSubType,
],
});
}
/**
* Returns the identifier of the user whose action the event was sent to in the application
*/
public get userId(): number {
return this.payload.user_id;
}
/**
* Returns the identifier of the application from which the event was sent
*/
public get appId(): number {
return this.payload.app_id;
}
/**
* Returns the identifier of the community to which the notification was sent
*/
public get groupId(): number {
return this.payload.group_id;
}
/**
* Returns the transferred useful data
*/
public get eventPayload(): P {
return JSON.parse(this.payload.payload);
}
/**
* Returns the custom data
*/
public [kSerializeData](): object {
return pickProperties(this, [
'userId',
'appId',
'groupId',
'eventPayload',
]);
}
}