-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdelivery.ts
221 lines (193 loc) · 7.33 KB
/
delivery.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import cors from 'cors';
import express from 'express';
import { IDatabase } from './persistance/getDatabase';
import { WithLocals } from './types';
import { auth } from './utils';
import { schema, getMessages, Acknoledgment } from '@dm3-org/dm3-lib-delivery';
import { validateSchema } from '@dm3-org/dm3-lib-shared';
import { getConversationId } from '@dm3-org/dm3-lib-storage';
const syncAcknoledgmentParamsSchema = {
type: 'object',
properties: {
ensName: { type: 'string' },
last_message_pull: { type: 'string' },
},
required: ['ensName', 'last_message_pull'],
additionalProperties: false,
};
const syncAcknoledgmentBodySchema = {
type: 'object',
properties: {
acknoledgments: {
type: 'array',
items: schema.Acknoledgment,
},
},
required: ['acknoledgments'],
additionalProperties: false,
};
export default () => {
const router = express.Router();
//TODO remove
router.use(cors());
router.param('ensName', auth);
router.get(
'/messages/:ensName/contact/:contactEnsName',
//@ts-ignore
async (req: express.Request & { app: WithLocals }, res, next) => {
try {
const idEnsName = await req.app.locals.db.getIdEnsName(
req.params.ensName,
);
const idContactEnsName = await req.app.locals.db.getIdEnsName(
req.params.contactEnsName,
);
const newMessages = await getMessages(
req.app.locals.db.getMessages,
req.app.locals.keys.encryption,
idEnsName,
idContactEnsName,
);
res.json(newMessages);
} catch (e) {
next(e);
}
},
);
/**
* Retrieves incoming messages for a specific ENS name.
*
* @route GET /messages/incoming/:ensName
* @param {express.Request & { app: WithLocals }} req - The request object, including the ENS name as a parameter
* @param {express.Response} res - The response object.
* @param {express.NextFunction} next - The next middleware function.
* @returns {Promise<void>} - A promise that resolves with the incoming messages as a JSON response.
* @throws {Error} - If an error occurs while retrieving the incoming messages.
*
* @example
* GET /messages/incoming/example.ens
*/
router.get(
'/messages/incoming/:ensName',
//@ts-ignore
async (req: express.Request & { app: WithLocals }, res, next) => {
try {
const incomingMessages =
await req.app.locals.db.getIncomingMessages(
req.params.ensName,
//Fetch the last 10 messages per conversation
//If we decide to add pagination for that endpoint we can pass this value as a param
10,
);
res.json(incomingMessages);
} catch (e) {
next(e);
}
},
);
router.post('/messages/:ensName/pending', async (req, res, next) => {
try {
const db: IDatabase = req.app.locals.db;
const account = await db.getIdEnsName(req.params.ensName);
const pending = await db.getPending(account);
await db.deletePending(account);
res.json(pending);
} catch (e) {
next(e);
}
});
//TODO remove after storage refactoring
router.post(
'/messages/:ensName/syncAcknoledgment/:last_message_pull',
async (req, res, next) => {
const hasValidParams = validateSchema(
syncAcknoledgmentParamsSchema,
req.params,
);
const hasValidBody = validateSchema(
syncAcknoledgmentBodySchema,
req.body,
);
// eslint-disable-next-line max-len
//Express transform number inputs into strings. So we have to check if a string used as last_message_pull can be converted to a number later on.
const isLastMessagePullNumber = !isNaN(
Number.parseInt(req.params.last_message_pull),
);
if (!hasValidParams || !isLastMessagePullNumber || !hasValidBody) {
return res.send(400);
}
try {
const ensName = await req.app.locals.db.getIdEnsName(
req.params.ensName,
);
await Promise.all(
req.body.acknoledgments.map(async (ack: Acknoledgment) => {
const contactEnsName =
await await req.app.locals.db.getIdEnsName(
ack.contactAddress,
);
const conversationId = getConversationId(
ensName,
contactEnsName,
);
const db: IDatabase = req.app.locals.db;
await db.syncAcknowledge(
conversationId,
Number.parseInt(req.params.last_message_pull),
);
}),
);
res.json();
} catch (e) {
next(e);
}
},
);
router.post(
'/messages/:ensName/syncAcknowledgment/:last_message_pull',
async (req, res, next) => {
const hasValidParams = validateSchema(
syncAcknoledgmentParamsSchema,
req.params,
);
const hasValidBody = validateSchema(
syncAcknoledgmentBodySchema,
req.body,
);
// eslint-disable-next-line max-len
//Express transform number inputs into strings. So we have to check if a string used as last_message_pull can be converted to a number later on.
const isLastMessagePullNumber = !isNaN(
Number.parseInt(req.params.last_message_pull),
);
if (!hasValidParams || !isLastMessagePullNumber || !hasValidBody) {
return res.send(400);
}
try {
const ensName = await req.app.locals.db.getIdEnsName(
req.params.ensName,
);
await Promise.all(
req.body.acknoledgments.map(async (ack: Acknoledgment) => {
const contactEnsName =
await await req.app.locals.db.getIdEnsName(
ack.contactAddress,
);
const conversationId = getConversationId(
ensName,
contactEnsName,
);
const db: IDatabase = req.app.locals.db;
await db.syncAcknowledge(
conversationId,
Number.parseInt(req.params.last_message_pull),
);
}),
);
res.json();
} catch (e) {
next(e);
}
},
);
return router;
};