-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add /share/file-by-username endpoint
- Loading branch information
1 parent
1859668
commit 5d214c7
Showing
5 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const express = require('express'); | ||
const { Endpoint } = require('../util/expressutil'); | ||
|
||
const validator = require('validator'); | ||
const APIError = require('../api/APIError'); | ||
const { get_user } = require('../helpers'); | ||
const { Context } = require('../util/context'); | ||
const auth2 = require('../middleware/auth2'); | ||
const config = require('../config'); | ||
|
||
const { PermissionUtil } = require('../services/auth/PermissionService'); | ||
|
||
const uuidv4 = require('uuid').v4; | ||
|
||
const router = express.Router(); | ||
router.use(auth2); | ||
|
||
Endpoint({ | ||
route: '/file-by-username', | ||
methods: ['POST'], | ||
handler: async (req, res) => { | ||
const svc_token = req.services.get('token'); | ||
const svc_email = req.services.get('email'); | ||
const svc_permission = req.services.get('permission'); | ||
|
||
console.log('which actor exists?', | ||
req.actor, | ||
Context.get('actor')) | ||
const actor = Context.get('actor'); | ||
|
||
const username = req.body.username; | ||
if ( ! (typeof username === 'string') ) { | ||
throw APIError.create('field_invalid', null, { | ||
key: 'username', | ||
expected: 'string', | ||
got: typeof username, | ||
}); | ||
} | ||
|
||
let path = req.body.path; | ||
if ( ! (typeof path === 'string') ) { | ||
throw APIError.create('field_invalid', null, { | ||
key: 'path', | ||
expected: 'string', | ||
got: typeof path, | ||
}); | ||
} | ||
|
||
const access_level = req.body.access_level || 'write'; | ||
if ( ! ['read','write'].includes(access_level) ) { | ||
throw APIError.create('field_invalid', null, { | ||
key: 'access_level', | ||
expected: '"read" or "write"', | ||
}); | ||
} | ||
|
||
const recipient = await get_user({ username }); | ||
if ( ! recipient ) { | ||
throw APIError.create('user_does_not_exist', null, { | ||
username | ||
}); | ||
} | ||
|
||
await svc_permission.grant_user_user_permission( | ||
actor, username, | ||
PermissionUtil.join('fs', path, access_level), | ||
{}, {}, | ||
); | ||
|
||
if ( path.startsWith('/') ) path = path.slice(1); | ||
const link = `${config.origin}/show/${path}`; | ||
|
||
const email_values = { | ||
link, | ||
susername: req.user.username, | ||
...(recipient ? { | ||
rusername: recipient.username, | ||
} : {}), | ||
}; | ||
|
||
const email_tmpl = recipient ? | ||
'share_existing_user' : 'share_new_user'; | ||
|
||
await svc_email.send_email({ email: recipient.email }, email_tmpl, email_values); | ||
|
||
res.send({}); | ||
}, | ||
}).attach(router); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,17 @@ If this was not you, please contact [email protected] immediately. | |
<p>Sincerely,</p> | ||
<p>Puter</p> | ||
` | ||
} | ||
}, | ||
// TODO: revise email contents | ||
'share_existing_user': { | ||
subject: 'Puter share from {{susername}}', | ||
html: ` | ||
<p>Hi there {{rusername}},</p> | ||
<p>{{link}}</p> | ||
<p>Sincerely,</p> | ||
<p>Puter</p> | ||
` | ||
}, | ||
} | ||
|
||
class Emailservice extends BaseService { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters