From 94e1575c30db4bdaa522a946cfe92a4b3412ec9a Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:53 +0200 Subject: [PATCH] Dealing with users without profile. --- src/curate/user.js | 26 +++++++++++++++++--------- src/interpolate.js | 13 +++++++++---- src/save/profileDetails.js | 8 +++++--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/curate/user.js b/src/curate/user.js index bb62afe..c9a1ff6 100644 --- a/src/curate/user.js +++ b/src/curate/user.js @@ -3,22 +3,30 @@ const path = require('path'); function curateUser(user) { - return { + const curatedUser = { id: user.id, name: user.name, created: new Date(user.created_utc * 1000), + image: user.icon_img, gold: user.is_gold, verified: user.verified, verifiedEmail: user.has_verified_email, - profile: { - id: user.subreddit.display_name.name, - title: user.subreddit.display_name.title, - image: user.subreddit.display_name.icon_img, - banner: user.subreddit.display_name.banner_img, - description: user.subreddit.display_name.public_description, - over18: user.subreddit.display_name.over_18 - } }; + + if(user.subreddit) { + Object.assign(curatedUser, { + profile: { + id: user.subreddit.display_name.name, + title: user.subreddit.display_name.title, + image: user.subreddit.display_name.icon_img, + banner: user.subreddit.display_name.banner_img, + description: user.subreddit.display_name.public_description, + over18: user.subreddit.display_name.over_18 + } + }); + } + + return curatedUser; }; module.exports = curateUser; diff --git a/src/interpolate.js b/src/interpolate.js index 7ac9c83..cfa746a 100644 --- a/src/interpolate.js +++ b/src/interpolate.js @@ -29,11 +29,16 @@ function interpolate(pattern, user, post, item) { $userVerified: user.verified ? config.library.booleans.verified : '', $userVerifiedEmail: user.verifiedEmail ? config.library.booleans.verifiedEmail : '', $userGold: user.gold ? config.library.booleans.gold : '', - $profileId: user.profile.id, - $profileTitle: user.profile.title, - $profileDescription: user.profile.description, - $profileOver18: user.profile.over18 ? config.library.booleans.over18 : '' }); + + if(user.profile) { + Object.assign(vars, { + $profileId: user.profile.id, + $profileTitle: user.profile.title, + $profileDescription: user.profile.description, + $profileOver18: user.profile.over18 ? config.library.booleans.over18 : '' + }); + } } if(post) { diff --git a/src/save/profileDetails.js b/src/save/profileDetails.js index aee5c99..f159849 100644 --- a/src/save/profileDetails.js +++ b/src/save/profileDetails.js @@ -9,18 +9,20 @@ const save = require('./save.js'); function saveProfileDetails(user) { if(config.library.profile.image) { + const image = user.profile ? user.profile.image : user.image; + const filepath = interpolate(config.library.profile.image, user, null, { // pass profile image as item to interpolate extension variable - url: user.profile.image + url: image }); - fetchItem(user.profile.image).then(stream => save(filepath, stream)).catch(error => { + fetchItem(image).then(stream => save(filepath, stream)).catch(error => { console.log('\x1b[33m%s\x1b[0m', `Could not save profile image for '${user.name}': ${error}`); }); } if(config.library.profile.description) { - if(user.profile.description) { + if(user.profile && user.profile.description) { const filepath = interpolate(config.library.profile.description, user); const stream = textToStream(user.profile.description);