-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sending email notifications via POST /notification (#3)
Also - give tests more time to run in .mocharc.yml - refactor body schemata and update apidocs
- Loading branch information
Showing
12 changed files
with
388 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
extension: [ts] | ||
spec: src/test/*.spec.ts | ||
require: ts-node/register | ||
timeout: 10000 |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { DefaultContext, Middleware } from 'koa' | ||
import { emailSender } from '../config' | ||
import { sendMail } from '../services/mailerService' | ||
import { getVerifiedEmails } from './status' | ||
|
||
export type GoodBody = { | ||
'@context': 'https://www.w3.org/ns/activitystreams' | ||
type: 'Create' | ||
actor: { type: 'Person'; id: string; name?: string } | ||
object: { type: 'Note'; id: string; content: string } | ||
target: { type: 'Person'; id: string; name?: string } | ||
} | ||
|
||
export const notification: Middleware< | ||
{ user: string; client: string | undefined }, | ||
DefaultContext & { request: { body: GoodBody } } | ||
> = async ctx => { | ||
const body: GoodBody = ctx.request.body | ||
|
||
if (ctx.state.user !== body.actor.id) | ||
return ctx.throw(403, "You can't send notification as somebody else") | ||
|
||
// find email address | ||
const emails = await getVerifiedEmails(body.target.id) | ||
|
||
if (emails.length === 0) | ||
return ctx.throw( | ||
404, | ||
"Receiving person doesn't have available email address", | ||
) | ||
|
||
for (const email of emails) { | ||
await sendMail({ | ||
from: emailSender, | ||
to: email, | ||
subject: 'You have a new message from sleepy.bike!', // TODO generalize | ||
html: body.object.content, | ||
text: body.object.content, | ||
}) | ||
} | ||
ctx.response.status = 202 | ||
ctx.response.body = 'Accepted' | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.