-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSession.controller.ts
More file actions
76 lines (60 loc) · 1.87 KB
/
Session.controller.ts
File metadata and controls
76 lines (60 loc) · 1.87 KB
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
import { App } from '../app'
import { Errors, throwError } from '../errors'
import { generateUUID } from '../utils/generateUUID';
import { retrieveLaunchParams } from "../utils/retrieveLaunchParams";
import { WebAppUser } from "../../declaration";
export class SessionController {
private sessionId: string;
private userData: WebAppUser;
private platform: string;
private webAppStartParam: string;
private appModule: App;
constructor(app: App) {
this.appModule = app;
}
public init() {
const lp = retrieveLaunchParams();
const initData = lp.tgWebAppData;
this.userData = initData?.user;
if (!this.userData) {
throwError(Errors.USER_DATA_IS_NOT_PROVIDED);
}
this.webAppStartParam = initData.start_param;
this.platform = lp.platform;
this.sessionId = generateUUID(String(this.getUserId()));
}
public getSessionId() {
return this.sessionId;
}
public getUserId() {
return this.userData.id;
}
public getWebAppStartParam() {
return this.webAppStartParam;
}
public getPlatform(){
return this.platform;
}
public getUserLocale() {
return this.userData.language_code;
}
public getUserData() {
return this.userData;
}
public getUserIsPremium() {
const userData = this.getUserData();
return Boolean(userData?.is_premium);
}
public assembleEventSession() {
return {
session_id: this.getSessionId(),
user_id: this.getUserId(),
app_name: this.appModule.getAppName(),
is_premium: this.getUserIsPremium(),
platform: this.getPlatform(),
locale: this.getUserLocale(),
start_param: this.getWebAppStartParam(),
client_timestamp: String(Date.now()),
}
}
}