Browse Source

more progress towards /user/:username

develop
Rob Colbert 2 years ago
parent
commit
e83e37394d
  1. 25
      app/controllers/user.js
  2. 50
      app/services/user.js

25
app/controllers/user.js

@ -60,8 +60,9 @@ class UserController extends SiteController {
return next();
}
router.param('userId', this.populateUser.bind(this));
router.param('coreUserId', this.populateCoreUser.bind(this));
router.param('username', this.populateUsername.bind(this));
router.param('userId', this.populateUserId.bind(this));
router.param('coreUserId', this.populateCoreUserId.bind(this));
router.post(
'/core/:coreUserId/settings',
@ -131,7 +132,7 @@ class UserController extends SiteController {
this.getUserSettingsView.bind(this),
);
router.get(
'/:userId',
'/:username',
limiterService.createMiddleware(limiterService.config.user.getUserProfile),
authRequired,
otpMiddleware,
@ -147,7 +148,21 @@ class UserController extends SiteController {
);
}
async populateUser (req, res, next, userId) {
async populateUsername (req, res, next, username) {
const { user: userService } = this.dtp.services;
try {
res.locals.userProfile = await userService.getPublicProfile('User', username);
if (!res.locals.userProfile) {
throw new SiteError(404, 'Member not found');
}
return next();
} catch (error) {
this.log.error('failed to populate username with public profile', { username, error });
return next(error);
}
}
async populateUserId (req, res, next, userId) {
const { user: userService } = this.dtp.services;
try {
userId = mongoose.Types.ObjectId(userId);
@ -163,7 +178,7 @@ class UserController extends SiteController {
}
}
async populateCoreUser (req, res, next, coreUserId) {
async populateCoreUserId (req, res, next, coreUserId) {
const { coreNode: coreNodeService } = this.dtp.services;
try {
coreUserId = mongoose.Types.ObjectId(coreUserId);

50
app/services/user.js

@ -459,7 +459,7 @@ class UserService extends SiteService {
return user;
}
async getPublicProfile (username) {
async getPublicProfile (type, username) {
if (!username || (typeof username !== 'string')) {
throw new SiteError(406, 'Invalid username');
}
@ -469,28 +469,32 @@ class UserService extends SiteService {
throw new SiteError(406, 'Invalid username');
}
/**
* Try to resolve the user as a CoreUser
*/
let user = await CoreUser
.findOne({ username_lc: username })
.select('_id created username username_lc displayName bio picture header core')
.populate(this.populateUser)
.lean();
if (user) {
user.type = 'CoreUser';
} else {
/*
* Try to resolve the user as a local User
*/
user = await User
.findOne({ username_lc: username })
.select('_id created username username_lc displayName bio picture header')
.populate(this.populateUser)
.lean();
if (user) {
user.type = 'User';
}
let user;
switch (type) {
case 'CoreUser':
user = await CoreUser
.findOne({ username_lc: username })
.select('_id created username username_lc displayName bio picture header core')
.populate(this.populateUser)
.lean();
if (user) {
user.type = 'CoreUser';
}
break;
case 'User':
user = await User
.findOne({ username_lc: username })
.select('_id created username username_lc displayName bio picture header')
.populate(this.populateUser)
.lean();
if (user) {
user.type = 'User';
}
break;
default:
throw new SiteError(400, 'Invalid user account type');
}
return user;

Loading…
Cancel
Save