forked from zhuowei/hipster.house
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
109 lines (100 loc) · 2.72 KB
/
common.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function getHeaders(useGuestMode) {
const headers = {
'User-Agent': 'clubhouse/269 (iPhone; iOS 14.1; Scale/3.00)',
'CH-Languages': 'en-US',
'CH-Locale': 'en_US',
'CH-AppVersion': '0.1.15',
'CH-AppBuild': '269',
'CH-UserID': '(null)',
'CH-DeviceId': getDeviceId(),
};
if (isLoggedIn()) {
const auth = getConfig().auth;
headers['Authorization'] = 'Token ' + auth.auth_token;
headers['CH-UserID'] = auth.user_profile.user_id;
} else if (useGuestMode) {
// Hi, View Source user: please don't get this banned, ok?
headers['Authorization'] = 'Token 6d2e7064fbe7a06c4d423015581e20c6dbc24302';
headers['CH-UserID'] = '69577135';
headers['CH-DeviceId'] = '2E76C714-7730-4920-A56C-896622A7A794';
}
return headers;
}
function isLoggedIn() {
const config = getConfig();
return config.auth && config.auth.auth_token;
}
function apiUrl(api) {
return 'https://api.hipster.house/api/' + api;
}
async function apiPost(api, body, opts) {
let useGuestMode = true;
if (opts && opts.disableGuestMode) {
useGuestMode = false;
}
const response = await fetch(apiUrl(api), {
method: 'POST',
body: JSON.stringify(body),
headers: {
'content-type': 'application/json; charset=utf-8',
...getHeaders(useGuestMode),
},
});
const responseBody = await response.json();
return responseBody;
}
async function apiGet(api) {
const response = await fetch(apiUrl(api), {
method: 'GET',
headers: getHeaders(/*useGuestMode=*/ true),
});
const responseBody = await response.json();
return responseBody;
}
function getConfig() {
if (!localStorage.hipsterHouse) return {};
return JSON.parse(localStorage.hipsterHouse);
}
function setConfig(newConfig) {
localStorage.hipsterHouse = JSON.stringify({
...getConfig(),
...newConfig,
});
}
async function updateUser() {
const waitlistResponse = await apiGet('check_waitlist_status');
const response = waitlistResponse && waitlistResponse.is_waitlisted ?
{} :
await apiPost('me', {});
setConfig({user: response, waitlist: waitlistResponse});
}
async function doRedirect(needsOffWaitlist) {
if (!isLoggedIn()) {
// TODO(zhuowei): guest mode
location = '/login.html';
}
if (!isLoggedIn()) {
if (needsOffWaitlist) {
location = '/';
return;
}
return;
}
await updateUser();
if (needsOffWaitlist && isWaitlisted()) {
location = '/';
return;
}
}
function isWaitlisted() {
if (getConfig().bypass) return false;
return getConfig().waitlist && getConfig().waitlist.is_waitlisted;
}
function getDeviceId() {
let deviceId = getConfig().deviceId;
if (!deviceId) {
deviceId = uuidv4().toUpperCase();
setConfig({deviceId});
}
return deviceId;
}