-
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 service and share-by-email to /share
- Loading branch information
1 parent
fc4ae19
commit db5990a
Showing
10 changed files
with
196 additions
and
13 deletions.
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
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
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 |
---|---|---|
|
@@ -141,6 +141,10 @@ If this was not you, please contact [email protected] immediately. | |
<p>Puter</p> | ||
` | ||
}, | ||
'share_by_email': { | ||
subject: 'share by email', | ||
html: `testing: {{link}}` | ||
}, | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { whatis } = require("../util/langutil"); | ||
const { Actor, UserActorType } = require("./auth/Actor"); | ||
const BaseService = require("./BaseService"); | ||
const { DB_WRITE } = require("./database/consts"); | ||
|
||
class ShareService extends BaseService { | ||
static MODULES = { | ||
uuidv4: require('uuid').v4, | ||
validator: require('validator'), | ||
}; | ||
|
||
async _init () { | ||
this.db = await this.services.get('database').get(DB_WRITE, 'share'); | ||
} | ||
|
||
async create_share ({ | ||
issuer, | ||
email, | ||
data, | ||
}) { | ||
const require = this.require; | ||
const validator = require('validator'); | ||
|
||
// track: type check | ||
if ( typeof email !== 'string' ) { | ||
throw new Error('email must be a string'); | ||
} | ||
// track: type check | ||
if ( whatis(data) !== 'object' ) { | ||
throw new Error('data must be an object'); | ||
} | ||
|
||
// track: adapt | ||
issuer = Actor.adapt(issuer); | ||
// track: type check | ||
if ( ! (issuer instanceof Actor) ) { | ||
throw new Error('expected issuer to be Actor'); | ||
} | ||
|
||
// track: actor type | ||
if ( ! (issuer.type instanceof UserActorType) ) { | ||
throw new Error('only users are allowed to create shares'); | ||
} | ||
|
||
if ( ! validator.isEmail(email) ) { | ||
throw new Error('invalid email'); | ||
} | ||
|
||
const uuid = this.modules.uuidv4(); | ||
|
||
await this.db.write( | ||
'INSERT INTO `share` ' + | ||
'(`uid`, `issuer_user_id`, `recipient_email`, `data`) ' + | ||
'VALUES (?, ?, ?, ?)', | ||
[uuid, issuer.type.user.id, email, JSON.stringify(data)] | ||
); | ||
|
||
return uuid; | ||
} | ||
} | ||
|
||
module.exports = { | ||
ShareService, | ||
}; | ||
|
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
11 changes: 11 additions & 0 deletions
11
packages/backend/src/services/database/sqlite_setup/0014_share.sql
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,11 @@ | ||
CREATE TABLE `share` ( | ||
"id" INTEGER PRIMARY KEY AUTOINCREMENT, | ||
"uid" TEXT NOT NULL UNIQUE, | ||
"issuer_user_id" INTEGER NOT NULL, | ||
"recipient_email" TEXT NOT NULL, | ||
"data" JSON DEFAULT NULL, | ||
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
FOREIGN KEY ("issuer_user_id") REFERENCES "user" ("id") | ||
ON DELETE CASCADE ON UPDATE CASCADE | ||
); |