-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (119 loc) · 3.14 KB
/
Copy pathindex.js
File metadata and controls
147 lines (119 loc) · 3.14 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
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
const fs = require('node:fs');
// Definition de DB
class DB {
constructor() {
this.messages = {};
}
/**
* Default Channel Template
*/
static DEFAULT =(
channel,
time = new Date()
) => {
// Get time
let time_of_day = time.toTimeString().split(' ')[0];
let date = time.toDateString();
return {
messages:[{
username:'Marvin',
message:
`\x1b[90mThis chat room \x1b[32m${channel}\x1b[90m `+
`was created at ${time_of_day} on ${date}\x1b[0m`,
userTag:'BOT'
}]
}
}
static int32ToBuffer(buffer) {
if (
buffer instanceof Buffer ||
typeof buffer == "string"
) return buffer;
let byte_size = buffer[buffer.length-1]
let ans = new Uint8Array(byte_size * buffer.length + 1);
// Set the last byte as int byte size indicator
ans[ans.length-1] = 0xff - byte_size;
// Set the Uint8Array buffer
for (let i = 0; i < buffer.length-1; i++) {
for (let j = 0; j < byte_size; j++) {
// Calculate byte size shift
let shift = -8 * j + ((byte_size - 1) * 8);
// Mask only 1 byte N times, MSB first
// Set the value in buffer
ans[i+j] = (buffer[i]&(0xff << shift) << shift);
}
}
return Buffer.from(ans);
}
/**
* Load the local server file if it exists
*/
loadIfExists(channel) {
// If channel's message history does exist in Heap,
// return it.
if (this.messages[channel] !== undefined) return {
messages: this.messages[channel]
};
// If no message history for a channel is found,
// return a channel template.
if (
!fs.existsSync(`./data/${channel}.json`)
) {
// default bot greeting for new channels
return DB.DEFAULT(channel);
}
// Read from a channel file
let data = JSON.parse(
fs.readFileSync(`./data/${channel}.json`, 'utf-8')
);
// Convert to Uint8Arrays to buffers, ignore strings
data.messages = data.messages.map(x => {
if (typeof x.message == 'string') return x;
x.message = Buffer.from(x.message.data);
return x;
});
// read from file
return data;
}
/**
* Load the local server file
*/
load(channel) {
const data = this.loadIfExists(channel).messages;
data.map(msg => {
msg.message = DB.int32ToBuffer(msg.message);
return msg;
})
this.messages[channel] = data;
}
/**
* get all messages in DB
* @returns obj message list
*/
getMessages(channel) {
return [...this.messages[channel]];
}
/**
* Save to a local server file
*/
save(channel) {
fs.writeFileSync(
`./data/${channel}.json`,
JSON.stringify({ messages:this.messages[channel] }),
'utf-8'
);
}
/**
* add a message to DB
* @param {Message} msg - the message to add to the DB
*/
addMessage(channel, msg) {
if (this.messages[channel] === undefined) {
console.log('Message was sent to an channel which is not in heap ');
return;
}
// Add a message
this.messages[channel].push(msg);
}
}
module.exports = DB;