add link sharing
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
const Out = _flitter.model('v1:Out')
|
||||
const Project = _flitter.model('v1:Project')
|
||||
const Invite = _flitter.model('v1:Invite')
|
||||
class v1 {
|
||||
|
||||
/*
|
||||
@@ -66,6 +67,53 @@ class v1 {
|
||||
success: true
|
||||
})
|
||||
}
|
||||
|
||||
async invite_show(req, res, next){
|
||||
const invite = await Invite.findById(req.params.id)
|
||||
if ( !invite ) _flitter.error(res, 404, {reason: 'Invitation not found with the specified URL.'})
|
||||
|
||||
if ( invite.used ) _flitter.error(res, 401, {reason: 'This invitation link has been used or has expired.'})
|
||||
|
||||
const project = await Project.findById(invite.project_id)
|
||||
if ( !project ) _flitter.error(res, 404, {reason: 'This project no longer exists.'})
|
||||
|
||||
const user = await _flitter.model('User').findOne({uuid: invite.by_user_id})
|
||||
if ( !user ) _flitter.error(res, 500, {reason: 'This user no longer exists. Sorry.'})
|
||||
|
||||
return _flitter.view(res, 'dash_v1:accept', {invite, project, user})
|
||||
}
|
||||
|
||||
async invite_accept(req, res, next){
|
||||
const invite = await Invite.findById(req.params.id)
|
||||
if ( !invite ) _flitter.error(res, 404, {reason: 'Invitation not found with the specified URL.'})
|
||||
|
||||
if ( invite.used ) _flitter.error(res, 401, {reason: 'This invitation link has been used or has expired.'})
|
||||
|
||||
const project = await Project.findById(invite.project_id)
|
||||
if ( !project ) _flitter.error(res, 404, {reason: 'This project no longer exists.'})
|
||||
|
||||
const user = await _flitter.model('User').findOne({uuid: invite.by_user_id})
|
||||
if ( !user ) _flitter.error(res, 500, {reason: 'This user no longer exists. Sorry.'})
|
||||
|
||||
// if we're signed in
|
||||
if ( req.session.auth && req.session.auth.user ){
|
||||
if ( project.user_id !== req.session.auth.uuid && !project.shared_user_ids.includes(req.session.auth.uuid) ){
|
||||
project.shared_user_ids.push(req.session.auth.uuid)
|
||||
await project.save()
|
||||
}
|
||||
return res.redirect('/dash/v1')
|
||||
}
|
||||
else {
|
||||
req.session.invite = true
|
||||
req.session.invite_data = {
|
||||
invite: invite.id,
|
||||
project: project.id,
|
||||
user: user.id,
|
||||
}
|
||||
|
||||
return res.redirect('/dash/v1')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = v1
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
const Project = _flitter.model('v1:Project')
|
||||
const Out = _flitter.model('v1:Out')
|
||||
const Invite = _flitter.model('v1:Invite')
|
||||
class v1 {
|
||||
|
||||
/*
|
||||
@@ -130,7 +131,7 @@ class v1 {
|
||||
}
|
||||
|
||||
// TODO permission access check
|
||||
return _flitter.view(res, 'dash_v1:out', {user: req.session.auth.user, out, prettyd:pretty, show_back: true, title: out.brief, title_small: true });
|
||||
return _flitter.view(res, 'dash_v1:out', {project, user: req.session.auth.user, out, prettyd:pretty, show_back: true, title: out.brief, title_small: true });
|
||||
}
|
||||
|
||||
async out_delete(req, res, next){
|
||||
@@ -264,6 +265,49 @@ class v1 {
|
||||
|
||||
return res.redirect('/dash/v1')
|
||||
}
|
||||
|
||||
async project_share_invite(req, res, next){
|
||||
const project = await Project.findById(req.params.id)
|
||||
if ( !project ) return _flitter.error(res, 404, {reason: 'Project not found with the specified ID.'})
|
||||
|
||||
if ( !project.user_id === req.session.auth.uuid ) return _flitter.error(res, 401, {reason: 'You do not have permission to edit this project.'})
|
||||
|
||||
let share_data = {
|
||||
project_id: project.id,
|
||||
by_user_id: req.session.auth.uuid,
|
||||
created_on: Date.now()
|
||||
}
|
||||
|
||||
const share = new Invite(share_data)
|
||||
await share.save()
|
||||
|
||||
return _flitter.view(res, 'dash_v1:invite', {share, project, title: 'Generate Invite Link', show_back: true})
|
||||
}
|
||||
|
||||
async accept_invite(req, res, next){
|
||||
if ( !req.session.invite ) return res.redirect('/dash/v1')
|
||||
|
||||
const invite = await Invite.findById(req.session.invite_data.invite)
|
||||
if ( !invite ) return _flitter.error(res, 404, {reason: 'This invitation is no longer valid. Sorry.'})
|
||||
|
||||
const project = await Project.findById(req.session.invite_data.project)
|
||||
if ( !project ) return _flitter.error(res, 404, {reason: 'This project no longer exists.'})
|
||||
|
||||
const user = await _flitter.model('User').findById(req.session.invite_data.user)
|
||||
if ( !user ) return _flitter.error(res, 404, {reason: 'This user no longer exists. Sorry.'})
|
||||
|
||||
if ( !project.shared_user_ids.includes(req.session.auth.uuid) && !(project.user_id === req.session.auth.uuid) ){
|
||||
project.shared_user_ids.push(req.session.auth.uuid)
|
||||
await project.save()
|
||||
}
|
||||
|
||||
invite.used = true
|
||||
await invite.save()
|
||||
|
||||
req.session.invite = false
|
||||
req.session.invite_data = false
|
||||
return res.redirect('/dash/v1/project/view/'+project.id)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = v1
|
||||
|
||||
Reference in New Issue
Block a user