-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
202 lines (175 loc) · 4.39 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const model = {
token: '',
user: null,
rooms: [],
showRoomList: false,
roomFilter: '',
roomId: '',
currentRoom: '',
folder: null,
busy: false,
downloading: false,
showDownloading: false,
showLogList: false,
settings: {
maxMessages: 5000,
maxFileSize: 10_000_000,
beforeDate: '',
fileType: 'all', // all, image
downloadFiles: false,
downloadPeople: false,
},
filesizes: [
{ bytes: 0, name: 'No limit' },
// { bytes: 1_000_000, name: '1 MB' },
{ bytes: 10_000_000, name: '10 MB' },
{ bytes: 50_000_000, name: '50 MB' },
{ bytes: 100_000_000, name: '100 MB' },
{ bytes: 200_000_000, name: '200 MB' },
{ bytes: 500_000_000, name: '500 MB' },
{ bytes: 1_000_000_000, name: '1 GB' },
],
downloader: null,
logger: null,
init() {
this.loadPrefs();
this.setTips();
},
setTips() {
if (typeof tippy !== 'undefined') {
tippy('[data-tippy-content]');
}
},
get compatible() {
return typeof window.showDirectoryPicker === 'function';
},
async loadPrefs() {
try {
const stored = JSON.parse(localStorage.getItem('archiver-prefs'));
this.setToken(stored.token);
if (this.token) {
await this.checkToken();
this.findRooms();
}
if (stored.roomId) {
this.roomId = stored.roomId;
await this.checkRoom();
}
this.settings = Object.assign(this.settings, stored.settings);
}
catch(e) {}
},
savePrefs() {
const data = {
token: this.token,
roomId: this.roomId,
settings: this.settings,
};
localStorage.setItem('archiver-prefs', JSON.stringify(data));
},
async startDownload() {
this.savePrefs();
this.logger = new Logger();
this.downloader = new Downloader(this.folder, this.token, this.logger);
const { downloader, currentRoom, settings } = this;
const alreadyExists = await this.downloader.spaceAlreadyExists(this.folder, currentRoom);
if (alreadyExists) {
const msg = `The folder "${this.folder.name}" already contains an archive for the selected space. Do you want to overwrite it?`;
const overwrite = confirm(msg);
if (!overwrite) {
return;
}
}
this.downloading = true;
this.showDownloading = true;
await downloader.saveAll(currentRoom, settings);
this.downloading = false;
},
async checkToken() {
this.user = null;
this.busy = 'Verifying token';
const token = this.token.trim();
if (!token) {
this.busy = false;
return;
}
try {
const res = await whoAmI(token);
if (res.ok) {
this.user = await res.json();
this.savePrefs();
}
else {
alert('The current token does not seem to be valid. Perhaps it has expired?');
}
this.busy = false;
}
catch(e) {
console.error('not able to check token');
this.busy = false;
}
},
pasteRoom(text) {
this.roomId = fixId(text);
this.checkRoom();
},
async findRooms() {
if (!this.user) return;
try {
const res = await getRooms(this.token);
if (res.ok) {
this.rooms = (await res.json()).items;
}
else {
console.warn('not able to find rooms', await res.text());
}
}
catch(e) {
console.log("not able to fetch rooms", e.message);
}
},
setToken(token) {
this.token = token;
},
async setRoom(id) {
this.roomId = id;
this.checkRoom();
this.showRoomList = false;
},
async selectFolder() {
try {
this.folder = await window.showDirectoryPicker({ mode: 'readwrite' });
}
catch {}
},
get roomsFiltered() {
const { rooms, roomFilter } = this;
return rooms?.filter(r => r.title.toLowerCase().includes(roomFilter));
},
get readyToDownload() {
return this.user && !!this.currentRoom && this.folder;
},
async checkRoom() {
this.currentRoom = null;
const token = this.token;
const roomId = fixId(this.roomId.trim());
this.roomId = roomId;
if (!roomId || !token) return;
this.busy = 'Verifying room id';
try {
const res = await getRoomDetails(token, roomId);
if (res.ok) {
this.currentRoom = await res.json();
this.savePrefs();
}
else {
console.warn(await res.text());
}
this.busy = false;
}
catch(e) {
console.log(e.message);
this.busy = false;
}
}
};