-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
91 lines (75 loc) · 2.15 KB
/
storage.js
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
const HyperDB = require('hyperdb')
const def = require('./spec/hyperdb/index.js');
const { DAY_LIMIT, ONE_DAY } = require('./constants.js');
const db = HyperDB.rocks('./db/messages.db', def)
/**
* Verify limit of this sender.
* @param {string} pubKey - The public key of the sender.
* @returns {Promise<boolean>}
*/
async function limit(pubKey) {
const endTime = Date.now()
const startTime = endTime - ONE_DAY;
const queryStream = db.find('@messages/messages', {
eq: { pub: pubKey },
gte: { timestamp: startTime },
lt: { timestamp: endTime }
})
const messages = await queryStream.toArray()
return messages.length > DAY_LIMIT
}
/**
* Add a new message to the database.
* @param {string} message - The message object.
* @returns {Promise<void>}
*/
async function save(message) {
const cipher = message.cipher
const timestamp = message.timestamp
const hash = message.hash
const pub = message.pub
const signature = message.signature
await db.insert('@messages/messages', {
cipher,
timestamp,
pub,
hash,
signature
});
await db.flush();
}
/**
* Delete a message from the database.
* @param {number} timestamp - The timestamp of the message to delete.
* @returns {Promise<void>}
*/
async function remove(timestamp) {
await db.delete('@messages/messages', { timestamp });
await db.flush();
console.log('Message deleted with timestamp:', timestamp);
}
/**
* Load all messages from the database.
* @returns {Promise<Array>} - List of all messages.
*/
async function load() {
await delete_old()
const queryStream = db.find('@messages/messages', {});
const messages = await queryStream.toArray();
console.log('Loaded messages:', messages.length);
return messages;
}
/**
* Deletes all older messages from the database.
* @returns {Promise<void>}
*/
async function delete_old() {
const older = Date.now() - ONE_DAY;
const stream = db.find('@messages/messages', { lt: { timestamp: older } });
const old = await stream.toArray();
for (const msg of old) {
await db.delete('@messages/messages', { timestamp: msg.timestamp });
}
await db.flush();
}
module.exports={load, remove, save, limit}