add feature: sort comments in post
This commit is contained in:
parent
24b686fe1e
commit
40364d6b22
|
@ -27,7 +27,6 @@ Community instances:
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
* Sort comments in post
|
|
||||||
* User attributes
|
* User attributes
|
||||||
* Comment and post attributes (e.g. stickies)
|
* Comment and post attributes (e.g. stickies)
|
||||||
* Subreddit flairs and wikis
|
* Subreddit flairs and wikis
|
||||||
|
|
|
@ -692,6 +692,24 @@ input[type="submit"]:hover,
|
||||||
width: calc(100% - 30px);
|
width: calc(100% - 30px);
|
||||||
margin: 10px 0px 10px 30px;
|
margin: 10px 0px 10px 30px;
|
||||||
}
|
}
|
||||||
|
#post .comments-sort details {
|
||||||
|
float: left;
|
||||||
|
width: auto;
|
||||||
|
cursor: pointer;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
#post .comments-sort details ul li.active a {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
#post .comments-sort details li.active a {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
#post .comments-sort details {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
#post .comments-sort details ul {
|
||||||
|
margin-left: 20px;
|
||||||
|
}
|
||||||
#post .comment .meta p.stickied {
|
#post .comment .meta p.stickied {
|
||||||
color: green;
|
color: green;
|
||||||
}
|
}
|
||||||
|
|
98
routes.js
98
routes.js
|
@ -459,6 +459,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
||||||
let subreddit = req.params.subreddit
|
let subreddit = req.params.subreddit
|
||||||
let id = req.params.id
|
let id = req.params.id
|
||||||
let snippet = encodeURIComponent(req.params.snippet)
|
let snippet = encodeURIComponent(req.params.snippet)
|
||||||
|
let sortby = req.query.sort
|
||||||
let comment_id = ''
|
let comment_id = ''
|
||||||
let viewing_comment = false
|
let viewing_comment = false
|
||||||
let more_comments_cursor = req.query.cursor
|
let more_comments_cursor = req.query.cursor
|
||||||
|
@ -469,10 +470,20 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
||||||
viewing_comment = true
|
viewing_comment = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!sortby) {
|
||||||
|
sortby = config.post_comments_sort
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!['confidence', 'top', 'new', 'controversial', 'old', 'qa'].includes(sortby)) {
|
||||||
|
console.error(`Got invalid sort.`, req.originalUrl)
|
||||||
|
return res.redirect('/')
|
||||||
|
}
|
||||||
|
|
||||||
let comments_url = `/r/${subreddit}/comments/${id}/${snippet}/${comment_id}`
|
let comments_url = `/r/${subreddit}/comments/${id}/${snippet}/${comment_id}`
|
||||||
let post_url = `/r/${subreddit}/comments/${id}/${snippet}/`
|
let post_url = `/r/${subreddit}/comments/${id}/${snippet}/`
|
||||||
|
let comments_key = `${comments_url}:sort:${sortby}`
|
||||||
redis.get(comments_url, (error, json) => {
|
|
||||||
|
redis.get(comments_key, (error, json) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
console.error(`Error getting the ${comments_url} key from redis.`, error)
|
console.error(`Error getting the ${comments_url} key from redis.`, error)
|
||||||
return res.render('index', { post: null, user_preferences: req.cookies })
|
return res.render('index', { post: null, user_preferences: req.cookies })
|
||||||
|
@ -489,55 +500,57 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
||||||
viewing_comment: viewing_comment,
|
viewing_comment: viewing_comment,
|
||||||
post_url: post_url,
|
post_url: post_url,
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
|
sortby: sortby,
|
||||||
user_preferences: req.cookies
|
user_preferences: req.cookies
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let key = `morechildren:${post_url};1`
|
let key = `morechildren:${post_url};1`
|
||||||
redis.get(key, (error, json) => {
|
redis.get(key, (error, json) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
console.error(`Error getting the ${key} key from redis.`, error)
|
console.error(`Error getting the ${key} key from redis.`, error)
|
||||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||||
}
|
}
|
||||||
if(json) {
|
if(json) {
|
||||||
console.log(`Got ${key} key from redis.`);
|
console.log(`Got ${key} key from redis.`);
|
||||||
redis.get(post_url, (error, post_json) => {
|
redis.get(post_url, (error, post_json) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
console.error(`Error getting the ${post_url} key from redis.`, error)
|
console.error(`Error getting the ${post_url} key from redis.`, error)
|
||||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||||
}
|
}
|
||||||
if(post_json) {
|
if(post_json) {
|
||||||
redis.get(`morechildren_ids:${post_url}`, (error, morechildren_ids) => {
|
redis.get(`morechildren_ids:${post_url}`, (error, morechildren_ids) => {
|
||||||
(async () => {
|
(async () => {
|
||||||
post_json = JSON.parse(post_json)
|
post_json = JSON.parse(post_json)
|
||||||
json = JSON.parse(json)
|
json = JSON.parse(json)
|
||||||
post_json[1].data.children = json
|
post_json[1].data.children = json
|
||||||
let processed_json = await processJsonPost(post_json, true)
|
let processed_json = await processJsonPost(post_json, true)
|
||||||
let finalized_json = await finalizeJsonPost(processed_json, id, post_url, morechildren_ids)
|
let finalized_json = await finalizeJsonPost(processed_json, id, post_url, morechildren_ids)
|
||||||
|
|
||||||
return res.render('post', {
|
return res.render('post', {
|
||||||
post: finalized_json.post_data,
|
post: finalized_json.post_data,
|
||||||
comments: finalized_json.comments,
|
comments: finalized_json.comments,
|
||||||
viewing_comment: false,
|
viewing_comment: false,
|
||||||
post_url: post_url,
|
post_url: post_url,
|
||||||
subreddit: req.params.subreddit,
|
subreddit: req.params.subreddit,
|
||||||
more_comments_page: 1,
|
sortby: sortby,
|
||||||
user_preferences: req.cookies
|
more_comments_page: 1,
|
||||||
})
|
user_preferences: req.cookies
|
||||||
})()
|
})
|
||||||
})
|
})()
|
||||||
}
|
})
|
||||||
})
|
}
|
||||||
}
|
})
|
||||||
})
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
} else {
|
} else {
|
||||||
fetch(encodeURI(`https://oauth.reddit.com${comments_url}?api_type=json&sort=${config.post_comments_sort}&context=${context}`), redditApiGETHeaders())
|
fetch(encodeURI(`https://oauth.reddit.com${comments_url}?api_type=json&sort=${sortby}&context=${context}`), redditApiGETHeaders())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if(result.status === 200) {
|
if(result.status === 200) {
|
||||||
result.json()
|
result.json()
|
||||||
.then(json => {
|
.then(json => {
|
||||||
redis.setex(comments_url, config.setexs.posts, JSON.stringify(json), (error) => {
|
redis.setex(comments_key, config.setexs.posts, JSON.stringify(json), (error) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
console.error(`Error setting the ${comments_url} key to redis.`, error)
|
console.error(`Error setting the ${comments_url} key to redis.`, error)
|
||||||
return res.render('post', { post: null, user_preferences: req.cookies })
|
return res.render('post', { post: null, user_preferences: req.cookies })
|
||||||
|
@ -552,6 +565,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
||||||
viewing_comment: viewing_comment,
|
viewing_comment: viewing_comment,
|
||||||
post_url: post_url,
|
post_url: post_url,
|
||||||
subreddit: subreddit,
|
subreddit: subreddit,
|
||||||
|
sortby: sortby,
|
||||||
user_preferences: req.cookies
|
user_preferences: req.cookies
|
||||||
})
|
})
|
||||||
})()
|
})()
|
||||||
|
@ -753,7 +767,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
||||||
console.log(`Redirecting to ${post_url} with cursor...`);
|
console.log(`Redirecting to ${post_url} with cursor...`);
|
||||||
return res.redirect(`${post_url}?cursor=${page}&page=${page}`)
|
return res.redirect(`${post_url}?cursor=${page}&page=${page}`)
|
||||||
} else {
|
} else {
|
||||||
let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${ids_to_show}&limit_children=false&link_id=t3_${post_id}&sort=${config.post_comments_sort}`
|
let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${ids_to_show}&limit_children=false&link_id=t3_${post_id}`
|
||||||
fetch(encodeURI(url), redditApiGETHeaders())
|
fetch(encodeURI(url), redditApiGETHeaders())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if(result.status === 200) {
|
if(result.status === 200) {
|
||||||
|
|
|
@ -137,5 +137,33 @@ html
|
||||||
a(href="" + post_url + "") view the rest of the comments →
|
a(href="" + post_url + "") view the rest of the comments →
|
||||||
.comments-info
|
.comments-info
|
||||||
p all #{post.num_comments} comments
|
p all #{post.num_comments} comments
|
||||||
|
.comments-sort
|
||||||
|
details
|
||||||
|
summary
|
||||||
|
if sortby === 'confidence'
|
||||||
|
span sorted by: <strong>best</strong>
|
||||||
|
if sortby === 'top'
|
||||||
|
span sorted by: <strong>top</strong>
|
||||||
|
if sortby === 'new'
|
||||||
|
span sorted by: <strong>new</strong>
|
||||||
|
if sortby === 'controversial'
|
||||||
|
span sorted by: <strong>controversial</strong>
|
||||||
|
if sortby === 'old'
|
||||||
|
span sorted by: <strong>old</strong>
|
||||||
|
if sortby === 'qa'
|
||||||
|
span sorted by: <strong>q&a</strong>
|
||||||
|
ul
|
||||||
|
li(class=sortby === 'confidence' ? 'active' : '')
|
||||||
|
a(href="?sort=confidence") best
|
||||||
|
li(class=sortby === 'top' ? 'active' : '')
|
||||||
|
a(href="?sort=top") top
|
||||||
|
li(class=sortby === 'new' ? 'active' : '')
|
||||||
|
a(href="?sort=new") new
|
||||||
|
li(class=sortby === 'controversial' ? 'active' : '')
|
||||||
|
a(href="?sort=controversial") controversial
|
||||||
|
li(class=sortby === 'old' ? 'active' : '')
|
||||||
|
a(href="?sort=old") old
|
||||||
|
li(class=sortby === 'qa' ? 'active' : '')
|
||||||
|
a(href="?sort=qa") Q&A
|
||||||
!= comments
|
!= comments
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue