add support for Gotify push notifications
This commit is contained in:
@@ -26,9 +26,101 @@ class ProfileController extends Controller {
|
||||
uid: user.uid,
|
||||
tagline: user.tagline,
|
||||
user_id: user.id,
|
||||
...(user.notify_config ? { notify_config: await user.notify_config.to_api() } : {})
|
||||
})
|
||||
}
|
||||
|
||||
async fetch_notify(req, res, next) {
|
||||
const User = this.models.get('auth:User')
|
||||
|
||||
let user
|
||||
if ( req.params.user_id === 'me' ) user = req.user
|
||||
else { // If not me, verify that user can modify profile
|
||||
if ( !req.user.can(`profile:update:${req.params.user_id}`) )
|
||||
return res.status(401).api()
|
||||
|
||||
user = await User.findById(req.params.user_id)
|
||||
}
|
||||
|
||||
if ( !user )
|
||||
return res.status(404)
|
||||
.message(req.T('api.user_not_found'))
|
||||
.api()
|
||||
|
||||
if ( user.notify_config ) {
|
||||
return res.api({
|
||||
has_config: true,
|
||||
config: await user.notify_config.to_api(),
|
||||
})
|
||||
} else {
|
||||
return res.api({
|
||||
has_config: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async test_notify(req, res, next) {
|
||||
const User = this.models.get('auth:User')
|
||||
|
||||
let user
|
||||
if ( req.params.user_id === 'me' ) user = req.user
|
||||
else { // If not me, verify that user can modify profile
|
||||
if ( !req.user.can(`profile:update:${req.params.user_id}`) )
|
||||
return res.status(401).api()
|
||||
|
||||
user = await User.findById(req.params.user_id)
|
||||
}
|
||||
|
||||
if ( !user )
|
||||
return res.status(404)
|
||||
.message(req.T('api.user_not_found'))
|
||||
.api()
|
||||
|
||||
if ( user.notify_config && user.notify_config.active ) {
|
||||
await user.notify_config.log({
|
||||
message: req.T('profile.test_notification'),
|
||||
})
|
||||
}
|
||||
|
||||
return res.api()
|
||||
}
|
||||
|
||||
async update_notify(req, res, next) {
|
||||
const User = this.models.get('auth:User')
|
||||
const NotifyConfig = this.models.get('system:NotifyConfig')
|
||||
|
||||
let user
|
||||
if ( req.params.user_id === 'me' ) user = req.user
|
||||
else { // If not me, verify that user can modify profile
|
||||
if ( !req.user.can(`profile:update:${req.params.user_id}`) )
|
||||
return res.status(401).api()
|
||||
|
||||
user = await User.findById(req.params.user_id)
|
||||
}
|
||||
|
||||
if ( !user )
|
||||
return res.status(404)
|
||||
.message(req.T('api.user_not_found'))
|
||||
.api()
|
||||
|
||||
if ( !user.notify_config ) {
|
||||
user.notify_config = new NotifyConfig({
|
||||
user_id: user.id,
|
||||
gateway_url: String(req.body.gateway_url),
|
||||
application_key: String(req.body.app_key),
|
||||
created_on: new Date,
|
||||
active: String(req.body.app_key) && String(req.body.gateway_url),
|
||||
}, user)
|
||||
} else {
|
||||
user.notify_config.application_key = String(req.body.app_key)
|
||||
user.notify_config.gateway_url = String(req.body.gateway_url)
|
||||
user.notify_config.active = String(req.body.app_key) && String(req.body.gateway_url)
|
||||
}
|
||||
|
||||
await user.save()
|
||||
return res.api()
|
||||
}
|
||||
|
||||
async update(req, res, next) {
|
||||
const User = this.models.get('auth:User')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user