/
index.js
640 lines (595 loc) · 19.1 KB
/
index.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import { chunk, flatten, fetchWithTimeout, promiseDelay,
getRandomInt, paramString,
} from 'utils'
import { getAuth } from './auth'
import { mapRedditObj, getModeratorsPostProcess,
subredditHasModlogs, U_PUBLICMODLOGS_CODE,
} from 'api/common'
import { getModerators } from 'api/reveddit'
const https = 'https://'
export const oauth_reddit = https+'oauth.reddit.com/'
let can_use_oauth_reddit_rev = true
export const www_reddit = https+'www.reddit.com'
export const old_reddit = https+'old.reddit.com'
const reveddit_cors = REVEDDIT_CORS_ANWHERE_HOST
const u_publicmodlogs_feed = '7e9b27126097f51ae6c9cd5b049af34891da6ba6'
const numRequestsBeforeWait = 100
const waitInterval = 5000
const maxNumItems = 100
const commentSortOptions = ['confidence', 'new', 'controversial', 'old', 'qa']
const MIN_COMMENT_KARMA = 1000
const errorHandler = (e) => {
throw new Error(`Could not connect to Reddit: ${e}`)
}
export const getComments = ({objects = undefined, ids = [], quarantined_subreddits, useProxy = false}) => {
const full_ids = getFullIDsForObjects(objects, ids, 't1_')
return getItems({ids: full_ids, quarantined_subreddits, key: 'id', useProxy})
}
export const getPosts = ({objects = undefined, ids = [], quarantined_subreddits, useProxy = false}) => {
const full_ids = getFullIDsForObjects(objects, ids, 't3_')
return getItems({ids: full_ids, quarantined_subreddits, key: 'id', useProxy})
.then(posts => {
for (const p of Object.values(posts)) {
p.url = p.url || ''
p.domain = p.domain || ''
}
return posts
})
}
export const getModeratedSubreddits = (user) => {
const url = oauth_reddit + `user/${user}/moderated_subreddits/.json`
return getAuth()
.then(auth => window.fetch(url, auth))
.then(response => response.json())
.then(results => {
return (results.data || []).reduce((map, obj) => (map[obj.sr.toLowerCase()] = true, map), {['u_'+user]: true})
})
}
export const getSubredditAbout = (subreddit, useProxy = false) => {
const host = getHost(useProxy)
const url = host + `r/${subreddit}/about/.json`
return getAuth()
.then(auth => window.fetch(url, auth))
.then(response => response.json())
.catch(error => {return {}})
.then(results => {
if (results.reason === 'quarantined') {
throw results
}
return results.data || results
})
}
export const getPostsForURLs = async (urls, auth = null) => {
const results = {}
if (! auth) {
auth = await getAuth()
}
return groupRequests(getPostsByURL, urls, [auth, results], 1)
.then(res => results)
.catch(errorHandler)
}
const getPostsByURL = (urlParam, auth = null, results) => {
const params = {url: encodeURIComponent(urlParam), raw_json:1, limit:100}
const url = oauth_reddit + 'api/info' + '?'+paramString(params)
return query(url, auth, 'id', results)
.catch(errorHandler)
}
const getFullIDsForObjects = (objects, ids, prefix) => {
let full_ids = []
if (typeof(objects) === 'object') {
if (Array.isArray(objects)) {
full_ids = objects.map(o => prefix+o.id)
} else {
full_ids = Object.values(objects).map(o => prefix+o.id)
}
} else {
full_ids = ids.map(id => prefix+id)
}
return full_ids
}
export const getItems = async ({ids, quarantined_subreddits, key = 'name', useProxy = false}) => {
const results = {}
if (! ids.length) {
return results
}
if (quarantined_subreddits) {
useProxy = true
}
const host = getHost(useProxy)
await getAuth()
return groupRequests(queryByID, ids, [quarantined_subreddits, key, results, host], maxNumItems)
.then(() => {
return results
})
}
const groupRequests = async (func, items, params, localMaxNumItems) => {
const promises = []
let i = 0
for (let items_chunk of chunk(items, localMaxNumItems)) {
if (i > 0 && i % numRequestsBeforeWait === 0) {
await promiseDelay(waitInterval)
}
if (localMaxNumItems == 1) {
items_chunk = items_chunk[0]
}
promises.push(func(items_chunk, ...params))
i += 1
}
return Promise.all(promises)
}
export const getPostWithComments = ({
threadID, commentID: comment, context = 0, sort = 'old', limit=500, useProxy = false,
subreddit,
}) => {
const params = {
limit,
sort,
depth: 10,
threaded: false,
showmore: true,
...(comment && {comment}),
...(context && {context})
}
const thisQueryParams = {
...params,
subreddit: subreddit,
}
const host = getHost(useProxy)
if (host === OAUTH_REDDIT_REV && subreddit) {
params.quarantined_subreddits = subreddit
}
const url = host + `comments/${threadID}.json`+'?'+paramString(params)
return getAuth()
.then(auth => window.fetch(url, auth))
.then(response => response.json())
.then(results => {
if (! Array.isArray(results)) {
throw results
}
const post = results[0].data.children[0].data
post.url = post.url || ''
post.domain = post.domain || ''
const items = results[1].data.children
const comments = {}, moreComments = {}
let oldestComment = {}, newestComment = {}
items.forEach(item => {
const itemData = item.data
if (item.kind === 't1') {
comments[itemData.id] = itemData
oldestComment = get_by_op_on_created_utc('<', itemData, oldestComment)
//newestComment = get_by_op_on_created_utc('>', itemData, newestComment)
} else if (item.kind === 'more') {
// NOTE: This object is also used to make "continue this thread" links, per reddit.com/54qknz
// Those instances have itemData.count == 0
moreComments[itemData.parent_id] = true
}
})
return {
post,
comments, moreComments, oldestComment,
//newestComment, // not currently used
}
})
}
const operators = {
'<': (a,b)=>a<b,
'>': (a,b)=>a>b,
}
// compareComment < varComment, or compareComment > varComment
const get_by_op_on_created_utc = (op, compareComment, varComment) => {
if (! varComment.created_utc || operators[op](compareComment.created_utc, varComment.created_utc)) {
return compareComment
}
return varComment
}
export const OVERVIEW = 'overview', SUBMITTED = 'submitted', COMMENTS='comments', GILDED='gilded'
// when kind = 'posts', page redirects via src/index.js
const kinds = {
'c': COMMENTS,
's': SUBMITTED,
'g': GILDED,
'o': OVERVIEW,
[COMMENTS]: COMMENTS,
[SUBMITTED]: SUBMITTED,
[GILDED]: GILDED,
[OVERVIEW]: OVERVIEW,
'': '',
null: '',
undefined: '',
}
export const kindsReverse = {
[COMMENTS]: 'c',
[SUBMITTED]: 's',
[GILDED]: 'g',
[OVERVIEW]: 'o',
c: 'c',
s: 's',
g: 'g',
o: 'o',
'': '',
}
export const queryUserPageCombined = async (params) => {
return queryUserPage({
useProxy: true,
include_info:true,
include_parents:true,
...params,
})
}
const getHost = (useProxy = false) => {
if (! useProxy || ! can_use_oauth_reddit_rev) {
return oauth_reddit
} else {
return OAUTH_REDDIT_REV
}
}
const addQuarantineParam = (host, params) => {
if (host === OAUTH_REDDIT_REV) {
params.quarantined = true
}
}
export const queryUserPage = async ({user, kind, sort, before, after, t, limit = 100,
quarantined_subreddits,
useProxy=false, include_info=false, include_parents=false}) => {
const host = getHost(useProxy)
const host_is_proxy = host === OAUTH_REDDIT_REV
const params = {
...(sort && {sort}),
...(limit && {limit}),
...(t && {t}),
...(after && {after}),
...(before && {before}),
}
if (host_is_proxy) {
params.include_info = include_info
params.include_parents = include_parents
if (quarantined_subreddits) {
params.quarantined_subreddits = quarantined_subreddits
}
}
let auth = await getAuth()
if (host_is_proxy) {
auth.method = 'POST'
auth.body = auth.headers.Authorization
delete auth.headers.Authorization
}
let response
try {
response = await window.fetch(host + `user/${user}/${kinds[kind]}.json` + '?'+paramString(params), auth)
} catch (e) {
if (host !== oauth_reddit && e.message !== 'Forbidden') {
can_use_oauth_reddit_rev = false
return queryUserPageCombined({user, kind, ...params}) // host will be oauth_reddit for this query
}
errorHandler(e)
}
const json = await response.json()
if (host === OAUTH_REDDIT_REV) {
if (! include_info && ! include_parents && json.user) {
return json.user
} else {
return json
}
} else if (json.data?.children) {
let items = []
if (! include_info && ! include_parents) {
items = json.data.children.map(item => item.data)
return {
items,
after: json.data.after,
}
} else {
const result = {
user: {
items,
after: json.data.after,
}
}
const commentIDs = []
const parents_set = new Set()
for (const item_listing of json.data.children) {
const item = item_listing.data
items.push(item)
if (item.parent_id) {
commentIDs.push(item.name)
parents_set.add(item.parent_id)
parents_set.add(item.link_id)
}
}
const promises = []
if (include_info) {
promises.push(getItems({ids: commentIDs})
.then(info => {
result.info = info
}))
}
if (include_parents) {
promises.push(getItems({ids: Array.from(parents_set)})
.then(parents => {
result.parents = parents
}))
}
await Promise.all(promises)
return result
}
return result
} else {
let empty = {items: [], after: null}
if ('message' in json && 'error' in json) {
empty.message = json.message
empty.error = json.error
}
return empty
}
}
export const usernameAvailable = (user) => {
var params = {user: user, raw_json:1}
const url = oauth_reddit + 'api/username_available' + '?'+paramString(params)
return getAuth()
.then(auth => window.fetch(url, auth))
.then(response => response.json())
.catch(errorHandler)
}
export const userPageHTML = (user) => {
const url = reveddit_cors+`old.reddit.com/user/${user}`
return fetchWithTimeout(url, {'Accept-Language': 'en'}, 3000)
.then(response => response.text())
.then(html => {
return {html: html}
})
.catch(error => {
return {error: error.message}
})
}
const queryByID = async (ids, quarantined_subreddits, key = 'name', results = {}, host = oauth_reddit) => {
var params = {id: ids.join(), raw_json:1}
if (host === OAUTH_REDDIT_REV && quarantined_subreddits) {
params.quarantined_subreddits = quarantined_subreddits
}
const auth = await getAuth()
const queryForHost = (host) => query(host + 'api/info' + '?'+paramString(params),
auth, key, results)
return queryForHost(host)
.catch(e => {
if (host !== oauth_reddit && e.message !== 'Forbidden') {
can_use_oauth_reddit_rev = false
return queryForHost(oauth_reddit)
.catch(errorHandler)
}
errorHandler(e)
})
}
const query = async (url, auth, key, results = {}) => {
return window.fetch(url, auth)
.then(response => response.json())
.then(json => json.data.children.reduce((map, obj) => mapRedditObj(map, obj, key), results))
}
// Results must include a post w/created_utc less than encompass_created_utc
// reference on async recursion w/promises: https://blog.scottlogic.com/2017/09/14/asynchronous-recursion.html#asynchronous-recursion-with-promises
export const querySubredditPageUntil = (subreddit, encompass_created_utc, after = '') => {
return querySubredditPage({subreddit, sort: 'new', after})
.then(data => {
if (data.posts.slice(-1)[0].created_utc > encompass_created_utc && data.after) {
return querySubredditPageUntil(subreddit, encompass_created_utc, data.after)
.then(nextData => data.posts.concat(nextData))
} else {
return data.posts
}
})
}
export const querySubredditPage = async ({subreddit, sort, after = '', t = '', useProxy = false}) => {
const host = getHost(useProxy)
const params = {
...(after && {after}),
...(t && {t}),
limit: 100,
raw_json:1}
addQuarantineParam(host, params)
const url = host + `r/${subreddit}/${sort}.json` + '?'+paramString(params)
const auth = await getAuth()
return window.fetch(url, auth)
.then(response => response.json())
.then(results => {
if (results.data) {
return {
posts: results.data.children.map(post => post.data),
after: results.data.after,
useProxy: host === OAUTH_REDDIT_REV,
}
} else if (results.reason === 'quarantined' && host !== OAUTH_REDDIT_REV) {
return querySubredditPage({subreddit, sort, after, t, useProxy:true})
}
})
.catch(errorHandler)
}
export const querySearch = ({selftexts = [], urls = []}) => {
var params = {q:'', sort:'new', limit:100, t:'all'}
if (selftexts.length) {
params.q += encodeURIComponent(selftexts.map(x => `selftext:"${x}"`).join(' OR '))
}
if (urls.length) {
params.q += encodeURIComponent(urls.map(x => `url:"${x}"`).join(' OR '))
}
const url = oauth_reddit + 'search/.json' + '?'+paramString(params)
return getAuth()
.then(auth => window.fetch(url, auth))
.then(response => response.json())
.then(json =>
json.data.children.reduce((map, obj) => mapRedditObj(map, obj, 'id'), {})
)
.catch(() => {
console.error('reddit.com/search failed')
return {}
})
}
const NUM_TOP_POSTS_TO_CONSIDER = 10
export const randomRedditor = async (subreddit = 'all') => {
const mods_promise = subreddit === 'all' ? Promise.resolve({}) : getModerators(subreddit)
return querySubredditPage({subreddit, sort: 'top', t: 'month'})
.then(data => {
const posts_with_most_comments = data.posts
.sort((a,b) => b.num_comments - a.num_comments)
.slice(0,NUM_TOP_POSTS_TO_CONSIDER)
const post = posts_with_most_comments[getRandomInt(posts_with_most_comments.length)]
return selectRandomCommenter(
post,
commentSortOptions[getRandomInt(commentSortOptions.length)],
mods_promise,
data.useProxy,
)
})
}
const NUM_AUTHORS_WHEN_COMMENT_KARMA_IS_LOW = 20
export const selectRandomCommenter = async (post, sort = 'new', mods_promise, useProxy = false) => {
const host = getHost(useProxy)
const params = {
sort,
limit: 200,
}
addQuarantineParam(host, params)
const url = host + `r/${post.subreddit}/comments/${post.id}.json`+'?'+paramString(params)
const auth = await getAuth()
return window.fetch(url, auth)
.then(response => response.json())
.then(result => result[1].data.children)
.then(traverseComments_collectAuthors)
.then(authors => getAuthorInfo(Object.keys(authors)))
.then(async (authorInfo) => {
const mods = await Promise.resolve(mods_promise)
const authorsWithoutMods = Object.values(authorInfo)
.filter(x => ! mods[x.name])
let authors_with_most_comment_karma = authorsWithoutMods
.filter(x => x.comment_karma >= MIN_COMMENT_KARMA)
if (authors_with_most_comment_karma.length < 5) {
authors_with_most_comment_karma = authorsWithoutMods
.sort((a,b) => b.comment_karma - a.comment_karma)
.slice(0,NUM_AUTHORS_WHEN_COMMENT_KARMA_IS_LOW)
}
const author = authors_with_most_comment_karma[getRandomInt(authors_with_most_comment_karma.length)]
return author.name
})
}
const traverseComments_collectAuthors = (comments, authors = {}) => {
comments.forEach(child => {
const c = child.data
if (c && c.author_fullname && ! c.distinguished) {
authors[c.author_fullname] = c.author
}
if (c.replies && typeof c.replies === 'object' && c.replies.kind === 'Listing' && c.replies.data.children) {
traverseComments_collectAuthors(c.replies.data.children, authors)
}
})
return authors
}
export const getAuthorInfo = (ids, results = {}) => {
const url = oauth_reddit + `api/user_data_by_account_ids.json?ids=${ids.join(',')}`
return getAuth()
.then(auth => window.fetch(url, auth))
.then(result => result.json())
.then(data => {
Object.assign(results, data)
return data
})
}
export const getAuthorInfoByName = (ids) => {
const results = {}
return groupRequests(getAuthorInfo, ids, [results], 500)
.then(() => {
const authors = {}
if (! ('error' in results) || ! ('message' in results)) {
Object.values(results).forEach(obj => {
obj.combined_karma = obj.link_karma + obj.comment_karma
authors[obj.name] = obj
})
}
return {
authors,
author_fullnames: results,
}
})
.catch(errorHandler)
}
const getModlogsItems = async ({subreddit, itemType, limit = 100, link_id}) => {
const hasModlogs = await subredditHasModlogs(subreddit, U_PUBLICMODLOGS_CODE)
if (! hasModlogs) {
return {}
}
const approved = (link_id && itemType == 'link') ? '&type=approvelink' : ''
const remove = '&type=remove'+itemType
const spam = '&type=spam'+itemType
let auth = {}
const urls = []
const subreddit_lower = subreddit.toLowerCase()
const baseUrl = oauth_reddit + `r/${subreddit}/about/log/.json?feed=${u_publicmodlogs_feed}&user=publicmodlogs`
auth = await getAuth()
urls.push(...[
baseUrl + remove + `&limit=${limit}`,
baseUrl + spam + "&limit=50"
])
if (approved) {
urls.push(baseUrl + approved + "&limit=50")
}
const promises = urls.map(u => getJson(u, auth))
return Promise.all(promises)
.then(listings => postProcessModlogsListings(listings, link_id))
}
export const getModlogsPosts = ({subreddit, limit = 100, link_id}) => {
return getModlogsItems({subreddit, itemType: 'link', limit, link_id})
}
export const getModlogsComments = ({subreddit, limit = 500, link_id}) => {
return getModlogsItems({subreddit, itemType: 'comment', limit, link_id})
}
const postProcessModlogsListings = (listings, link_id = '') => {
const items = {}
for (const listing of listings) {
if (listing && listing.data) {
postProcessModlogsList(listing.data.children, link_id, items)
}
}
return items
}
//input: list, link_id
//output: items
const postProcessModlogsList = (list, link_id = '', items = {}) => {
for (const item of list) {
let data = item // coronavirus log items have no 'data' item -> that service is down now, but keep this just in case
if (item.data) {
data = item.data
}
data.target_permalink = data.target_permalink.replace(/^https?:\/\/[^/]*/,'')
data.link_id = 't3_'+data.target_permalink.split('/')[4]
if (link_id && data.link_id !== 't3_'+link_id) {
continue
}
data.id = data.target_fullname.slice(3)
data.log_source = 'u_publicmodlogs'
items[data.id] = data
}
return items
}
export const getSticky = async (subreddit, num) => {
const url = oauth_reddit+`r/${subreddit}/hot.json?limit=2`
const auth = await getAuth()
return getJson(url, auth, '')
.then(data => {
let count = 1, prevStickyPermalink
for (const child of data.data.children) {
if (child.data.stickied) {
if (count == num) {
return child.data.permalink
}
prevStickyPermalink = child.data.permalink
count += 1
}
}
return prevStickyPermalink
})
.catch(error => '')
}
export const getJson = (url, options, valueOnError = {}) => {
return window.fetch(url, options)
.then(response => response.json())
.catch(error => {
return valueOnError
})
}