|
@ -8,6 +8,8 @@ const DTP_COMPONENT_NAME = 'home'; |
|
|
|
|
|
|
|
|
const express = require('express'); |
|
|
const express = require('express'); |
|
|
|
|
|
|
|
|
|
|
|
const moment = require('moment'); |
|
|
|
|
|
|
|
|
const { SiteController, SiteError } = require('../../lib/site-lib'); |
|
|
const { SiteController, SiteError } = require('../../lib/site-lib'); |
|
|
|
|
|
|
|
|
class HomeController extends SiteController { |
|
|
class HomeController extends SiteController { |
|
@ -30,6 +32,8 @@ class HomeController extends SiteController { |
|
|
return next(); |
|
|
return next(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
router.get('/:username/embed', this.getPublicProfileEmbed.bind(this)); |
|
|
|
|
|
router.get('/:username/json', this.getPublicProfileJSON.bind(this)); |
|
|
router.get('/:username', |
|
|
router.get('/:username', |
|
|
limiterService.create(limiterService.config.home.getPublicProfile), |
|
|
limiterService.create(limiterService.config.home.getPublicProfile), |
|
|
this.getPublicProfile.bind(this), |
|
|
this.getPublicProfile.bind(this), |
|
@ -53,6 +57,86 @@ class HomeController extends SiteController { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getPublicProfileEmbed (req, res, next) { |
|
|
|
|
|
const { link: linkService, user: userService } = this.dtp.services; |
|
|
|
|
|
try { |
|
|
|
|
|
this.log.debug('profile request', { url: req.url }); |
|
|
|
|
|
if (!res.locals.userProfile) { |
|
|
|
|
|
return next(new SiteError(404, 'Member profile not found')); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res.locals.currentView = 'profile-embed'; |
|
|
|
|
|
|
|
|
|
|
|
res.locals.pageTitle = `${res.locals.userProfile.displayName || res.locals.userProfile.username} | ${this.dtp.config.site.name}`; |
|
|
|
|
|
res.locals.pageDescription = res.locals.userProfile.bio || this.dtp.config.site.description; |
|
|
|
|
|
|
|
|
|
|
|
res.locals.pagination = this.getPaginationParameters(req, 20); |
|
|
|
|
|
res.locals.links = await linkService.getForUser(res.locals.userProfile, res.locals.pagination); |
|
|
|
|
|
|
|
|
|
|
|
await userService.recordProfileView(res.locals.userProfile, req); |
|
|
|
|
|
|
|
|
|
|
|
res.render('profile/embed'); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
this.log.error('failed to display landing page', { error }); |
|
|
|
|
|
return next(error); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getPublicProfileJSON (req, res) { |
|
|
|
|
|
const { link: linkService, user: userService } = this.dtp.services; |
|
|
|
|
|
try { |
|
|
|
|
|
this.log.debug('profile request', { url: req.url }); |
|
|
|
|
|
if (!res.locals.userProfile) { |
|
|
|
|
|
throw new SiteError(404, 'Member profile not found'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res.locals.currentView = 'profile-embed'; |
|
|
|
|
|
|
|
|
|
|
|
res.locals.pageTitle = `${res.locals.userProfile.displayName || res.locals.userProfile.username} | ${this.dtp.config.site.name}`; |
|
|
|
|
|
res.locals.pageDescription = res.locals.userProfile.bio || this.dtp.config.site.description; |
|
|
|
|
|
|
|
|
|
|
|
res.locals.pagination = this.getPaginationParameters(req, 20); |
|
|
|
|
|
res.locals.links = await linkService.getForUser(res.locals.userProfile, res.locals.pagination); |
|
|
|
|
|
|
|
|
|
|
|
await userService.recordProfileView(res.locals.userProfile, req); |
|
|
|
|
|
|
|
|
|
|
|
if (res.locals.userProfile.picture) { |
|
|
|
|
|
if (res.locals.userProfile.picture.large) { |
|
|
|
|
|
res.locals.userProfile.picture.large = this.makeSiteLink(`/image/${res.locals.userProfile.picture.large._id}`); |
|
|
|
|
|
} |
|
|
|
|
|
if (res.locals.userProfile.picture.small) { |
|
|
|
|
|
res.locals.userProfile.picture.small = this.makeSiteLink(`/image/${res.locals.userProfile.picture.small._id}`); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (res.locals.userProfile.header) { |
|
|
|
|
|
res.locals.userProfile.header = this.makeSiteLink(`/image/${res.locals.userProfile.header._id}`); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete res.locals.userProfile._id; |
|
|
|
|
|
res.locals.userProfile.createdText = moment(res.locals.userProfile.created).fromNow(); |
|
|
|
|
|
|
|
|
|
|
|
res.locals.links.forEach((link) => { |
|
|
|
|
|
link.href = this.makeSiteLink(`/link/${link._id}/visit`); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
res.status(200).json({ |
|
|
|
|
|
success: true, |
|
|
|
|
|
user: res.locals.userProfile, |
|
|
|
|
|
links: res.locals.links, |
|
|
|
|
|
}); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
this.log.error('failed to display landing page', { error }); |
|
|
|
|
|
return res.status(error.statusCode || 500).json({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
message: error.message, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
makeSiteLink (url) { |
|
|
|
|
|
return `https://${this.dtp.config.site.domain}${url}`; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
async getPublicProfile (req, res, next) { |
|
|
async getPublicProfile (req, res, next) { |
|
|
const { link: linkService, user: userService } = this.dtp.services; |
|
|
const { link: linkService, user: userService } = this.dtp.services; |
|
|
try { |
|
|
try { |
|
|