mirror of
https://codeberg.org/teddit/teddit
synced 2025-02-17 20:50:35 +01:00
show subreddit moderators list on subreddit sidebar (ref #104)
This commit is contained in:
parent
670e3104b7
commit
a72e6e65af
@ -3,7 +3,20 @@ module.exports = function() {
|
|||||||
this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => {
|
this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
(async () => {
|
(async () => {
|
||||||
if(subreddit && !subreddit.includes('+')) {
|
if(subreddit && !subreddit.includes('+') && subreddit !== 'all') {
|
||||||
|
function returnRelevantKeys(json) {
|
||||||
|
return {
|
||||||
|
title: json.data.title,
|
||||||
|
public_description_html: json.data.public_description_html,
|
||||||
|
active_user_count: json.data.active_user_count,
|
||||||
|
subscribers: json.data.subscribers,
|
||||||
|
created_utc: json.data.created_utc,
|
||||||
|
over18: json.data.over18,
|
||||||
|
description_html: json.data.description_html,
|
||||||
|
moderators: json.moderators
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let key = `${subreddit}:sidebar`
|
let key = `${subreddit}:sidebar`
|
||||||
redis.get(key, (error, json) => {
|
redis.get(key, (error, json) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
@ -12,46 +25,53 @@ module.exports = function() {
|
|||||||
}
|
}
|
||||||
if(json) {
|
if(json) {
|
||||||
json = JSON.parse(json)
|
json = JSON.parse(json)
|
||||||
let obj = {
|
resolve(returnRelevantKeys(json))
|
||||||
title: json.data.title,
|
|
||||||
public_description_html: json.data.public_description_html,
|
|
||||||
active_user_count: json.data.active_user_count,
|
|
||||||
subscribers: json.data.subscribers,
|
|
||||||
created_utc: json.data.created_utc,
|
|
||||||
over18: json.data.over18,
|
|
||||||
description_html: json.data.description_html
|
|
||||||
}
|
|
||||||
resolve(obj)
|
|
||||||
} else {
|
} else {
|
||||||
if(subreddit !== 'all') {
|
let url = `https://reddit.com/r/${subreddit}/about.json`
|
||||||
let url = ''
|
if(config.use_reddit_oauth) {
|
||||||
if(config.use_reddit_oauth)
|
|
||||||
url = `https://oauth.reddit.com/r/${subreddit}/about`
|
url = `https://oauth.reddit.com/r/${subreddit}/about`
|
||||||
else
|
}
|
||||||
url = `https://reddit.com/r/${subreddit}/about.json`
|
|
||||||
fetch(encodeURI(url), redditApiGETHeaders())
|
fetch(encodeURI(url), redditApiGETHeaders())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if(result.status === 200) {
|
if(result.status === 200) {
|
||||||
result.json()
|
result.json()
|
||||||
.then(json => {
|
.then(json => {
|
||||||
|
json.moderators = []
|
||||||
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
|
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
console.error('Error setting the sidebar key to redis.', error)
|
console.error('Error setting the sidebar key to redis.', error)
|
||||||
return res.render('index', { json: null, user_preferences: req.cookies })
|
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||||
} else {
|
} else {
|
||||||
console.log('Fetched the sidebar from reddit API.');
|
console.log('Fetched the sidebar from reddit API.')
|
||||||
(async () => {
|
let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`
|
||||||
let obj = {
|
if(config.use_reddit_oauth) {
|
||||||
title: json.data.title,
|
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`
|
||||||
public_description_html: json.data.public_description_html,
|
|
||||||
active_user_count: json.data.active_user_count,
|
|
||||||
subscribers: json.data.subscribers,
|
|
||||||
created_utc: json.data.created_utc,
|
|
||||||
over18: json.data.over18,
|
|
||||||
description_html: json.data.description_html
|
|
||||||
}
|
}
|
||||||
resolve(obj)
|
fetch(encodeURI(moderators_url), redditApiGETHeaders())
|
||||||
})()
|
.then(mod_result => {
|
||||||
|
if(mod_result.status === 200) {
|
||||||
|
mod_result.json()
|
||||||
|
.then(mod_json => {
|
||||||
|
json.moderators = mod_json
|
||||||
|
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
|
||||||
|
if(error) {
|
||||||
|
console.error('Error setting the sidebar with moderators key to redis.', error)
|
||||||
|
return res.render('index', { json: null, user_preferences: req.cookies })
|
||||||
|
} else {
|
||||||
|
console.log('Fetched the moderators from reddit API.')
|
||||||
|
resolve(returnRelevantKeys(json))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} – ${mod_result.statusText}`)
|
||||||
|
console.error(config.reddit_api_error_text)
|
||||||
|
resolve(returnRelevantKeys(json))
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('Error fetching moderators.', error)
|
||||||
|
resolve(returnRelevantKeys(json))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -64,9 +84,6 @@ module.exports = function() {
|
|||||||
console.error('Error fetching the sidebar.', error)
|
console.error('Error fetching the sidebar.', error)
|
||||||
resolve(null)
|
resolve(null)
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -1149,6 +1149,17 @@ footer a {
|
|||||||
.subreddit-listing li {
|
.subreddit-listing li {
|
||||||
margin: 15px 0;
|
margin: 15px 0;
|
||||||
}
|
}
|
||||||
|
#sidebar .mod-list {
|
||||||
|
float: left;
|
||||||
|
width: auto;
|
||||||
|
margin-top: 25px;
|
||||||
|
}
|
||||||
|
#sidebar .mod-list ul {
|
||||||
|
padding: 7px 0px 0px 20px;
|
||||||
|
}
|
||||||
|
#sidebar .mod-list li a {
|
||||||
|
float: left;
|
||||||
|
}
|
||||||
#sidebar .content .description {
|
#sidebar .content .description {
|
||||||
margin-top: 38px;
|
margin-top: 38px;
|
||||||
}
|
}
|
||||||
|
@ -219,6 +219,17 @@ html
|
|||||||
!= unescape(subreddit_about.public_description_html, user_preferences)
|
!= unescape(subreddit_about.public_description_html, user_preferences)
|
||||||
.description
|
.description
|
||||||
!= unescape(subreddit_about.description_html, user_preferences)
|
!= unescape(subreddit_about.description_html, user_preferences)
|
||||||
|
if subreddit_about.moderators
|
||||||
|
if subreddit_about.moderators.kind === 'UserList'
|
||||||
|
if subreddit_about.moderators.data.children.length > 0
|
||||||
|
.mod-list
|
||||||
|
h4 Moderators
|
||||||
|
ul
|
||||||
|
each moderator in subreddit_about.moderators.data.children
|
||||||
|
li
|
||||||
|
a(href="/u/"+ moderator.name +"")
|
||||||
|
p(title=""+ moderator.mod_permissions.join(', ') +"") #{moderator.name}
|
||||||
|
span.flair #{moderator.author_flair_text}
|
||||||
else
|
else
|
||||||
if subreddit.includes('+')
|
if subreddit.includes('+')
|
||||||
.content
|
.content
|
||||||
|
Loading…
x
Reference in New Issue
Block a user