-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.ts
88 lines (83 loc) · 3.08 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2020 Privacy Research, LLC
/* eslint-disable @typescript-eslint/no-unused-vars */
import { APIGatewayProxyHandler } from 'aws-lambda'
import 'source-map-support/register'
import { FullKeyBundle, registerKeyBundle, getPublicPreKeyBundle } from './key-table'
import { getMessagesAfter, storeMessage } from './message-table'
export const register: APIGatewayProxyHandler = async (event, _context) => {
const { address } = event.pathParameters
const bundle = JSON.parse(event.body) as FullKeyBundle
const registered = (await registerKeyBundle(address, bundle)) === address
return {
statusCode: registered ? 200 : 409,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
},
body: JSON.stringify(
{
message: registered ? 'OK' : 'Username not available',
},
null,
2
),
}
}
export const prekeyBundle: APIGatewayProxyHandler = async (event, _context) => {
const { address } = event.pathParameters
const bundle = await getPublicPreKeyBundle(address)
if (bundle) {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
},
body: JSON.stringify(bundle, null, 2),
}
}
return {
statusCode: 404,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
},
body: '',
}
}
export const messages: APIGatewayProxyHandler = async (event, _context) => {
const { address } = event.pathParameters
const { after } = event.queryStringParameters
const messages = await getMessagesAfter(address, parseInt(after))
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
},
body: JSON.stringify(messages, null, 2),
}
}
export const send: APIGatewayProxyHandler = async (event, _context) => {
const { address } = event.pathParameters
const message = event.body
const item = await storeMessage(address, message)
return {
statusCode: 201,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
},
body: JSON.stringify(item, null, 2),
}
}