add feature: export and import preferences as json file

This commit is contained in:
teddit 2021-03-19 20:40:24 +01:00
parent 41c62b37a7
commit caf8901e47
3 changed files with 62 additions and 3 deletions

View File

@ -1640,12 +1640,19 @@ module.exports = (app, redis, fetch, RedditAPI) => {
app.post('/export_prefs', (req, res, next) => { app.post('/export_prefs', (req, res, next) => {
let export_saved = req.body.export_saved let export_saved = req.body.export_saved
let export_data = req.cookies let export_data = req.cookies
let export_to_file = req.body.export_to_file
if(export_saved !== 'on') { if(export_saved !== 'on') {
if(req.cookies.saved) if(req.cookies.saved)
delete export_data.saved delete export_data.saved
} }
if(export_to_file === 'on') {
res.setHeader('Content-disposition', 'attachment; filename=teddit_prefs.json')
res.setHeader('Content-type', 'application/json')
res.send(req.cookies)
}
let r = `${(Math.random().toString(36)+'00000000000000000').slice(2, 10+2).toUpperCase()}` let r = `${(Math.random().toString(36)+'00000000000000000').slice(2, 10+2).toUpperCase()}`
let key = `prefs_key:${r}` let key = `prefs_key:${r}`
redis.set(key, JSON.stringify(req.cookies), (error) => { redis.set(key, JSON.stringify(req.cookies), (error) => {
@ -1657,6 +1664,29 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} }
}) })
}) })
app.post('/import_prefs', (req, res, next) => {
let body = ''
req.on('data', chunk => {
body += chunk.toString()
})
req.on('end', () => {
body = body.toString()
let json = body.split('Content-Type: application/json')[1].trim().split('--')[0]
try {
let prefs = JSON.parse(json)
resetPreferences(res)
for(var setting in prefs) {
if(prefs.hasOwnProperty(setting)) {
res.cookie(setting, prefs[setting], { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true })
}
}
return res.redirect('/preferences')
} catch(e) {
console.error(`Error importing preferences from a JSON file. Please report this error on https://codeberg.org/teddit/teddit.`, e)
}
})
})
app.post('/r/:subreddit/comments/:id/:snippet', (req, res, next) => { app.post('/r/:subreddit/comments/:id/:snippet', (req, res, next) => {
/* morechildren route */ /* morechildren route */
@ -1736,5 +1766,16 @@ module.exports = (app, redis, fetch, RedditAPI) => {
}) })
} }
}) })
function resetPreferences(res) {
res.clearCookie('theme')
res.clearCookie('flairs')
res.clearCookie('nsfw_enabled')
res.clearCookie('highlight_controversial')
res.clearCookie('post_media_max_height')
res.clearCookie('collapse_child_comments')
res.clearCookie('show_upvoted_percentage')
res.clearCookie('subbed_subreddits')
}
} }

View File

@ -150,12 +150,15 @@ form .setting {
margin: 10px 0; margin: 10px 0;
width: 100%; width: 100%;
} }
#export-form { .export-import-form {
margin: 60px 0px 0px 0px; margin: 60px 0px 0px 0px;
} }
#export-form input { .export-import-form input {
margin: 10px 0px 10px 0px; margin: 10px 0px 10px 0px;
} }
.export-import-form:last-child {
margin: 25px 0px 15px 0px;
}
.container .content small.notice { .container .content small.notice {
padding-top: 20px; padding-top: 20px;
padding-bottom: 5px; padding-bottom: 5px;

View File

@ -93,7 +93,7 @@ html
small(class="notice") Preferences are stored client-side using cookies without any personal information. small(class="notice") Preferences are stored client-side using cookies without any personal information.
input(type="submit", value="Save preferences") input(type="submit", value="Save preferences")
a(href="/resetprefs", class="btn") Reset preferences a(href="/resetprefs", class="btn") Reset preferences
form(action="/export_prefs", method="POST", id="export-form") form(action="/export_prefs", method="POST", class="export-import-form")
if preferences_key if preferences_key
details(open) details(open)
summary summary
@ -118,8 +118,23 @@ html
.setting .setting
small By exporting your preferences you can transfer your subscribed subreddits and preferences to another device. Or you could create a bookmark if you tend to delete your cookies frequently. small By exporting your preferences you can transfer your subscribed subreddits and preferences to another device. Or you could create a bookmark if you tend to delete your cookies frequently.
br br
small If you are exporting to a file, please save your preferences first!
br
label(for="export_saved") Export saved posts: label(for="export_saved") Export saved posts:
input(type="checkbox", name="export_saved", id="export_saved") input(type="checkbox", name="export_saved", id="export_saved")
br br
label(for="export_to_file") Export preferences to a JSON file:
input(type="checkbox", name="export_to_file", id="export_to_file")
br
input(type="submit", value="Export preferences") input(type="submit", value="Export preferences")
form(action="/import_prefs", method="POST", class="export-import-form", enctype="multipart/form-data")
details
summary
span Import JSON preferences file
.setting
small All your current preferences and saved posts will be reseted and the settings from the JSON file will be used instead.
br
input(type="file", name="file", id="file")
br
input(type="submit", value="Import preferences")
include includes/footer.pug include includes/footer.pug