-
Notifications
You must be signed in to change notification settings - Fork 1
/
structure.ts
108 lines (96 loc) · 3.95 KB
/
structure.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
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
import sql from 'sql-template-strings';
import db from './index';
import * as DateFns from 'date-fns';
import UserManager from '../managers/userManager';
export async function createDatabaseStructure() {
await db.query(sql`
CREATE TABLE IF NOT EXISTS users (
id varchar NOT NULL,
password_hash varchar NOT NULL,
username varchar(32) NOT NULL,
tag varchar(4) NOT NULL,
email varchar(255) NOT NULL UNIQUE,
avatar varchar(64),
last_seen bigint NOT NULL,
created_at bigint NOT NULL,
two_factor_secret varchar(64),
is_system boolean NOT NULL DEFAULT FALSE,
is_admin boolean NOT NULL DEFAULT FALSE,
is_banned boolean NOT NULL DEFAULT FALSE,
is_email_confirmed boolean NOT NULL DEFAULT FALSE,
is_demo boolean NOT NULL DEFAULT FALSE,
is_two_factor_enabled boolean NOT NULL DEFAULT FALSE,
last_email_confirm_attempt bigint,
last_pass_reset_attempt bigint,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS chats (
id varchar NOT NULL,
is_group boolean NOT NULL,
name varchar(32),
avatar varchar(64),
color SMALLINT DEFAULT 0,
creator_id varchar REFERENCES users ON DELETE CASCADE,
created_at bigint NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS messages (
id varchar NOT NULL,
chat_id varchar REFERENCES chats ON DELETE CASCADE,
author_id varchar REFERENCES users ON DELETE CASCADE,
content text,
created_at bigint NOT NULL,
attachment varchar(64),
attachment_type varchar(16),
attachment_height smallint,
attachment_width smallint,
is_system boolean NOT NULL DEFAULT FALSE,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS blocks (
id varchar NOT NULL,
blocker_id varchar REFERENCES users ON DELETE CASCADE,
target_id varchar REFERENCES users ON DELETE CASCADE,
created_at bigint NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS participants (
id varchar NOT NULL,
user_id varchar REFERENCES users ON DELETE CASCADE,
chat_id varchar REFERENCES chats ON DELETE CASCADE,
last_read varchar REFERENCES messages,
created_at bigint NOT NULL,
is_hidden boolean NOT NULL DEFAULT FALSE,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS email_blacklist (
id varchar NOT NULL,
email varchar(255) NOT NULL UNIQUE,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS invites (
id varchar NOT NULL,
chat_id varchar REFERENCES chats ON DELETE CASCADE,
author_id varchar REFERENCES participants ON DELETE CASCADE,
invite_key varchar(8) NOT NULL,
created_at bigint NOT NULL,
PRIMARY KEY (id)
);
-- MIGRATIONS
ALTER TABLE users ADD COLUMN IF NOT EXISTS two_factor_secret varchar(64);
ALTER TABLE users ADD COLUMN IF NOT EXISTS is_two_factor_enabled boolean NOT NULL DEFAULT FALSE;
ALTER TABLE participants ADD COLUMN IF NOT EXISTS last_read varchar REFERENCES messages;
`);
await removeDemoAccounts();
}
async function removeDemoAccounts() {
const timestampToRemove = DateFns.getUnixTime(DateFns.subHours(new Date(), 24));
const usersQuery = await db.query(sql`
SELECT id FROM users WHERE is_demo IS TRUE AND created_at <= $1`,
[ timestampToRemove ]);
for(const user of usersQuery.rows) {
const userData = await UserManager.getUserJwtDataById(user.id);
await UserManager.deleteUser(userData);
console.log(`Removed old demo account - ${userData.username}#${userData.tag}`);
}
}