forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.ts
More file actions
65 lines (61 loc) · 1.73 KB
/
sync.ts
File metadata and controls
65 lines (61 loc) · 1.73 KB
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
import { zero, zeroClean } from '@logux/actions'
import type { BaseServer } from '@logux/server'
import { SUBPROTOCOL } from '@slowreader/api'
import { and, eq, gt } from 'drizzle-orm'
import { actions, db } from '../db/index.ts'
export default (server: BaseServer): void => {
server.type(zero, {
access() {
return true
},
async process(ctx, action, meta) {
await db.insert(actions).values({
added: await server.log.store.getLastAdded(),
compressed: action.z,
encrypted: Buffer.from(action.d, 'base64'),
id: meta.id,
iv: Buffer.from(action.iv, 'base64'),
subprotocol: meta.subprotocol ?? SUBPROTOCOL,
time: meta.time,
userId: ctx.userId
})
},
resend(ctx) {
return { user: ctx.userId }
}
})
server.type(zeroClean, {
async access(ctx, action) {
let deleting = await db.query.actions.findFirst({
where: eq(actions.id, action.id)
})
return deleting?.userId === ctx.userId
},
async process(ctx, action) {
await db.delete(actions).where(eq(actions.id, action.id))
},
resend(ctx) {
return { user: ctx.userId }
}
})
server.sendOnConnect(async (ctx, lastSynced) => {
let list = await db.query.actions.findMany({
where: and(eq(actions.userId, ctx.userId), gt(actions.added, lastSynced))
})
return list.map(column => {
return [
zero({
d: Buffer.from(column.encrypted).toString('base64'),
iv: Buffer.from(column.iv).toString('base64'),
z: column.compressed
}),
{
added: column.added,
id: column.id,
subprotocol: column.subprotocol,
time: column.time
}
]
})
})
}