2017-04-11 21:55:36 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
|
|
|
|
const response = require('../response')
|
|
|
|
const models = require('../models')
|
|
|
|
const logger = require('../logger')
|
2018-04-12 11:14:42 +00:00
|
|
|
const {generateAvatar} = require('../letter-avatars')
|
2017-04-11 21:55:36 +00:00
|
|
|
|
|
|
|
const UserRouter = module.exports = Router()
|
|
|
|
|
|
|
|
// get me info
|
|
|
|
UserRouter.get('/me', function (req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (!user) { return response.errorNotFound(res) }
|
|
|
|
var profile = models.User.getProfile(user)
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: req.user.id,
|
|
|
|
name: profile.name,
|
|
|
|
photo: profile.photo
|
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('read me failed: ' + err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.send({
|
|
|
|
status: 'forbidden'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-04-12 11:14:42 +00:00
|
|
|
|
|
|
|
UserRouter.get('/user/:username/avatar.svg', function (req, res, next) {
|
|
|
|
res.setHeader('Content-Type', 'image/svg+xml')
|
|
|
|
res.setHeader('Cache-Control', 'public, max-age=86400')
|
|
|
|
res.send(generateAvatar(req.params.username))
|
|
|
|
})
|