-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQReddit.js
447 lines (380 loc) · 18.2 KB
/
QReddit.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
.import QtQuick.LocalStorage 2.0 as LocalStorage
Qt.include('QRObjects.js');
var QReddit = function(userAgent, applicationName) {
//BaseReddit.apply(this, arguments);
this.userAgent = userAgent
this.applicationName = applicationName
this.notifier = createObject("NotifierObject.qml");
this.toString = function() {
return "[object RedditObject]"
}
var _userHandler = (function(that) {
//extends QReddit with user handling methods from within an anonymous function
//avoid directly manipulating variable activeuser
var activeUser = "";
function getDatabase() {
return LocalStorage.LocalStorage.openDatabaseSync(that.applicationName, "1.0", "User Storage Database", 1000000);
}
function getDatabaseTransaction(statement, values) {
var database = getDatabase();
var response;
database.transaction(function(transaction) {
response = values ? transaction.executeSql(statement, values) : transaction.executeSql(statement);
});
return response;
}
that._addUser = function(username, passwd) {
var subscribed = "";
var dbTransaction = getDatabaseTransaction('INSERT OR REPLACE INTO RedditUsers VALUES (?,?,?);',
[username, passwd, subscribed]);
if (dbTransaction.rowsAffected > 0) {
console.log("Log: Added user \"" + username + "\"");
} else {
throw "Error: _addUser(): Transaction failed.";
}
}
that._removeUser = function(username) {
//Check if username is stored in database
if(!that._isUserStored(username)) {
throw "Error: _removeUser(): Username \"" + username + "\" is not stored in table.";
}
//Check if username given is the active user
if(username === activeUser) {
throw "Error: _removeUser(): Username \"" + username + "\" is the active user.";
}
var dbTransaction = getDatabaseTransaction('DELETE FROM RedditUsers WHERE username IN (?);', username);
if (dbTransaction.rowsAffected > 0){
console.log("Log: Removed user \"" + username + "\"");
} else {
throw "Error: _removeUser(): Transaction failed.";
}
}
that._getUser = function(username) {
//Check if username is stored in database
if(!that._isUserStored(username)) {
throw "Error: _getUser(): Username \"" + username + "\" is not stored in table.";
}
var userObj = {
user: username,
passwd: ''
}
try {
var dbTransaction = getDatabaseTransaction('SELECT passwd FROM RedditUsers WHERE username=?;', [username]);
if(dbTransaction.rows.length > 0) {
userObj.passwd = dbTransaction.rows.item(0).passwd;
} else {
throw "Transaction failed."
}
} catch (error) {
console.error("Error: _getUser(): \"" + error + "\"");
}
return userObj;
}
that._getActiveUserFromDB = function() {
//Do not use this function. Use getActiveUser(). This function is for initialization purposes only.
var activeUser = "";
try {
var dbTransaction = getDatabaseTransaction('SELECT username FROM ActiveRedditUser LIMIT 1 OFFSET 0');
if(dbTransaction.rows.item(0)) {
activeUser = dbTransaction.rows.item(0).username || ""
}
} catch (error) {
console.error("Error: _getActiveUserFromDB(): \"" + error + "\"");
}
return activeUser;
}
function checkLoginError(response) {
if (response.json.data === undefined) throw response.json.errors[0][1];
}
that._loginUser = function(username, passwd, callback) {
var loginConnObj;
that.notifier.authStatus = 'loading';
that.notifier.subscribedLoading = true;
if (username !== that.notifier.currentAuthUser && that.notifier.currentAuthUser !== "") {
//A different user is already logged in. We must log out first.
//Since we're calling getAPIConnection() inside a function, we can't return the Connection object it gives.
//Instead we mirror its responses with a dummy Connection object.
loginConnObj = createObject("ConnectionObject.qml");
var logoutConnObj = that.logout(true);
logoutConnObj.onSuccess.connect(function(){
var apiLoginConnObj = that.getAPIConnection('login', {
user: username,
passwd: passwd
});
apiLoginConnObj.onConnection.connect(loginConnObj.connection);
apiLoginConnObj.onSuccess.connect(loginConnObj.success);
apiLoginConnObj.onRaiseRetry.connect(loginConnObj.raiseRetry);
apiLoginConnObj.onError.connect(loginConnObj.error);
});
} else {
//No one is logged in.
loginConnObj = that.getAPIConnection('login', {
user: username,
passwd: passwd
});
}
loginConnObj.onConnection.connect(function (response){
try {
checkLoginError(response);
if(callback !== undefined) callback();
} catch (error) {
loginConnObj.error(error);
return false;
}
loginConnObj.response = response.json.data;
loginConnObj.success();
});
loginConnObj.onSuccess.connect(function() {
that.modhash = loginConnObj.response.modhash;
console.log("Log: Logged in \"" + username + "\" successfully.");
that.notifier.authStatus = 'done';
that.notifier.subscribedLoading = false;
that.notifier.currentAuthUser = username;
});
loginConnObj.onError.connect(function (response) {
that.notifier.authStatus = 'error';
});
return loginConnObj;
}
that._isUserStored = function(username) {
var storedUsers = that.getUsers();
return (storedUsers.indexOf(username) !== -1 || username === "");
}
that._setActiveUser = function(username) {
//Check if username is stored in database
if(!that._isUserStored(username) && username !== "") {
throw "Error: _setActiveUser(): Username \"" + username + "\" is not stored in table.";
}
var dbTransaction = getDatabaseTransaction('UPDATE ActiveRedditUser SET username=?;', [username]);
if (dbTransaction.rowsAffected > 0){
console.log("Log: Set \"" + username + "\" as the active user")
activeUser = username
that.notifier.activeUser = activeUser;
} else {
throw "Error: _setActiveUser(): Transaction failed."
}
}
that.getUsers = function() {
//Returns an array of usernames stored in the `users` table.
var users = [];
try {
var dbTransaction = getDatabaseTransaction('SELECT username FROM RedditUsers;');
for (var i = 0; i < dbTransaction.rows.length; i++) {
var username = dbTransaction.rows.item(i).username
if (username !== "") users.push(username);
}
} catch (error) {
console.error("Error: getUsers(): \"" + error + "\" Returning empty array.")
}
return users;
}
that.getActiveUser = function() {
return activeUser || "";
}
that.updateSubscribedArray = function() {
//Returns a Connection QML object. Updates the activeUser's subscribed subreddits from the internet.
var username = that.getActiveUser();
var isAUser = username !== "";
var subsrConnObj;
function parseListing(listing) {
var subsrArray = [];
for (var i = 0; i < listing.length; i++) {
var subsrName = listing[i].data.display_name;
subsrName = subsrName.charAt(0).toUpperCase() + subsrName.slice(1)//.toLowerCase();
subsrArray.push(subsrName);
}
return subsrArray;
}
function updateMoreSubscribed(after) {
var subsrConnObj = that.getAPIConnection("subreddits_mine subscriber", {
after: after,
limit: 100
});
subsrConnObj.onConnection.connect(function(response){
var subsrArray = parseListing(response.data.children);
if(typeof response.data.after === "string") {
var updConnObj = updateMoreSubscribed(response.data.after);
updConnObj.onSuccess.connect(function(){
subsrArray = subsrArray.concat(updConnObj.response);
subsrConnObj.response = subsrArray;
subsrConnObj.success();
});
} else {
subsrConnObj.response = subsrArray;
subsrConnObj.success();
}
});
return subsrConnObj;
}
if (isAUser) {
subsrConnObj = that.getAPIConnection("subreddits_mine subscriber", {limit: 100});
} else {
subsrConnObj = that.getAPIConnection("subreddits_default");
}
that.notifier.subscribedLoading = true
subsrConnObj.onConnection.connect(function(response){
var subsrArray = parseListing(response.data.children);
if(typeof response.data.after === "string" && isAUser) {
var updConnObj = updateMoreSubscribed(response.data.after);
updConnObj.onSuccess.connect(function(){
subsrArray = subsrArray.concat(updConnObj.response);
subsrArray = subsrArray.sort();
subsrConnObj.response = subsrArray.join();
subsrConnObj.success();
});
} else {
subsrArray = subsrArray.sort();
subsrConnObj.response = subsrArray.join();
subsrConnObj.success();
}
});
subsrConnObj.onSuccess.connect(function(){
var dbTransaction = getDatabaseTransaction('UPDATE RedditUsers SET subscribed=? WHERE username=?;',
[subsrConnObj.response, username]);
if (dbTransaction.rowsAffected > 0){
console.log("Log: Set updated subreddit list for \"" + username + "\"");
} else {
throw "Error: _setActiveUser(): Transaction failed.";
}
that.notifier.subscribedLoading = false
});
return subsrConnObj;
}
that.getSubscribedArray = function(username) {
//Returns a stored user's subscribed subreddits.
//If username is not passed, the activeUser's subreddits will be returned.
var subscribed;
username = username || that.getActiveUser();
//Check if username is stored in database
if(!that._isUserStored(username)) {
username = that.getActiveUser();
}
try {
var dbTransaction = getDatabaseTransaction('SELECT subscribed FROM RedditUsers WHERE username=?;', [username]);
if(dbTransaction.rows.length > 0) {
subscribed = dbTransaction.rows.item(0).subscribed;
} else {
throw "Transaction failed.";
}
} catch (error) {
subscribed = "";
}
return subscribed.split(',');
}
var initializeDatabase = (function() {
try {
//Create tables RedditUsers and ActiveRedditUser.
getDatabaseTransaction('CREATE TABLE IF NOT EXISTS RedditUsers(username TEXT UNIQUE, passwd TEXT, subscribed TEXT);');
getDatabaseTransaction('CREATE TABLE IF NOT EXISTS ActiveRedditUser(username TEXT UNIQUE);');
getDatabaseTransaction('INSERT INTO ActiveRedditUser SELECT "" WHERE NOT EXISTS (SELECT * FROM ActiveRedditUser LIMIT 1)');
getDatabaseTransaction('INSERT INTO RedditUsers(username, passwd, subscribed) SELECT "", "", "" WHERE NOT EXISTS (SELECT * FROM RedditUsers LIMIT 1)');
//Set the active user
activeUser = that._getActiveUserFromDB();
that.notifier.activeUser = activeUser;
} catch (error) {
throw "Error: QReddit initializeDatabase: \"" + error + "\""
}
console.log("Log: QReddit has been initialized")
}());
}(this));
this.loginNewUser = function(username, password) {
//Returns a Connection QML object. Authenticates a new user.
// If successful, stores the new user to the `RedditUsers` table and sets it as the active user.
var that = this;
var loginConnObj = this._loginUser(username, password, function() {
that._addUser(username, password);
that._setActiveUser(username);
});
return loginConnObj;
}
this.loginActiveUser = function() {
//Returns a Connection QML object. Logs in the currently active user.
//TODO: refactor code into one try-catch statement, one if the activeUser is stored/anonymous and one if not
var username = this.getActiveUser();
var password = "";
if (username !== "") {
try {
password = this._getUser(username).passwd;
} catch (error) {
password = "";
}
var loginConnObj = this._loginUser(username, password);
//If the activeUser is not actually stored, or the stored password is blank, raise an error
if (password === "") {
var loginTimer = createTimer(1);
loginTimer.onTriggered.connect(function() {
loginTimer.destroy();
loginConnObj.error("Password error.");
});
}
return loginConnObj;
}
var noLoginConnObj = createObject("ConnectionObject.qml");
console.log("Log: No user is logged in.");
this.notifier.authStatus = 'none';
this.notifier.subscribedLoading = false;
var noLoginTimer = createTimer(1);
noLoginTimer.onTriggered.connect(function() {
noLoginTimer.destroy();
noLoginConnObj.success();
});
return noLoginConnObj;
}
this.switchActiveUser = function(username) {
//Returns a Connection QML object. Logs a stored user into Reddit and sets it as the active user if successful.
if (username === this.getActiveUser()) return; //Do nothing if the user given is already the active user
if(username === "") {
//Simply logout
var outConnObj = this.logout();
return outConnObj;
}
var password = "",
userError = "",
that = this;
try {
password = this._getUser(username).passwd;
} catch (error) {
//TODO: return dummy connection object with error
userError = error;
}
var loginConnObj = this._loginUser(username, password, function() {
that._setActiveUser(username);
});
return loginConnObj;
}
this.logout = function(loadingAuth) {
//Returns a Connection QML object. Logs the app out of Reddit.
//Passing true to logout() will stop the Connection object from changing the notifier's authStatus when successful.
//* Reddit's logout api returns a 404, despite it working fine. Do not connect to onError as it is unreliable.
this.notifier.authStatus = 'loading';
this.notifier.subscribedLoading = true;
var logoutConnObj = this.getAPIConnection('logout');
var that = this;
logoutConnObj.onError.connect(function(error){
logoutConnObj.success();
});
logoutConnObj.onSuccess.connect(function(){
that.notifier.currentAuthUser = "";
that._setActiveUser("");
if(!loadingAuth) that.notifier.authStatus = 'none';
that.notifier.subscribedLoading = false;
});
return logoutConnObj;
}
this.deleteUser = function(username) {
//Returns true if the given user to be deleted is the activeUser. Removes a user from the `RedditUsers` table.
var isActiveUser = username === this.getActiveUser();
if (isActiveUser) this._setActiveUser("");
this._removeUser(username);
return isActiveUser;
}
this.getSubredditObj = function(srName) {
//Returns a Subreddit Object. If srName is omitted, the Subreddit Object will correspond to the Reddit Frontpage.
return srName ? new SubredditObj(this, srName) : new SubredditObj(this);
}
this.getUserObj = function(username) {
//Returns a User Object.
return new UserObj(this, username || "");
}
}
QReddit.prototype = new BaseReddit()