|
@@ -1,40 +1,64 @@
|
|
|
|
|
+import fs from 'fs';
|
|
|
|
|
+import path from 'path';
|
|
|
|
|
+import util from 'util';
|
|
|
|
|
+
|
|
|
import { ControllerBase } from './lib/controller-base.class';
|
|
import { ControllerBase } from './lib/controller-base.class';
|
|
|
import { ControllerPool } from './lib/controller-pool.interface';
|
|
import { ControllerPool } from './lib/controller-pool.interface';
|
|
|
|
|
|
|
|
export interface ChatMessage {
|
|
export interface ChatMessage {
|
|
|
|
|
+ id?: string;
|
|
|
time: Date;
|
|
time: Date;
|
|
|
author: String;
|
|
author: String;
|
|
|
text: string;
|
|
text: string;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** Example implementation of a data controller.
|
|
|
|
|
|
|
+/** Example file system based implementation of a data controller.
|
|
|
*
|
|
*
|
|
|
* Could also be a database client implementation of any kind to store and receive data.
|
|
* Could also be a database client implementation of any kind to store and receive data.
|
|
|
*/
|
|
*/
|
|
|
export class ChatController extends ControllerBase {
|
|
export class ChatController extends ControllerBase {
|
|
|
- private _messages: ChatMessage[] = [];
|
|
|
|
|
|
|
+ private _messages?: ChatMessage[];
|
|
|
|
|
|
|
|
constructor(ctrlPool: ControllerPool) {
|
|
constructor(ctrlPool: ControllerPool) {
|
|
|
super(ctrlPool);
|
|
super(ctrlPool);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public addMessage(author: string, text: string) {
|
|
|
|
|
|
|
+ public async addMessage(author: string, text: string) {
|
|
|
|
|
+ const id = new Date().getTime().toFixed(0);
|
|
|
const msg: ChatMessage = {
|
|
const msg: ChatMessage = {
|
|
|
author,
|
|
author,
|
|
|
text,
|
|
text,
|
|
|
time: new Date()
|
|
time: new Date()
|
|
|
};
|
|
};
|
|
|
- this._messages.push(msg);
|
|
|
|
|
|
|
+ await util.promisify(fs.writeFile)(path.resolve(process.env.CHATDATA_DIR, id), JSON.stringify(msg, null, 2));
|
|
|
|
|
+ msg.id = id;
|
|
|
|
|
+ const messages = await this.getMessages();
|
|
|
|
|
+ messages.push(msg);
|
|
|
return msg;
|
|
return msg;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public getMessages() {
|
|
|
|
|
|
|
+ public async getMessages() {
|
|
|
|
|
+ if (!this._messages) {
|
|
|
|
|
+ const dirEntries = await util.promisify(fs.readdir)(process.env.CHATDATA_DIR);
|
|
|
|
|
+ const messages = [];
|
|
|
|
|
+ for (const entry of dirEntries) {
|
|
|
|
|
+ const file = path.resolve(process.env.CHATDATA_DIR, entry);
|
|
|
|
|
+ const json = await util.promisify(fs.readFile)(file, { encoding: 'utf-8' });
|
|
|
|
|
+ const message = JSON.parse(json) as ChatMessage;
|
|
|
|
|
+ message.id = entry;
|
|
|
|
|
+ messages.push(message);
|
|
|
|
|
+ }
|
|
|
|
|
+ this._messages = messages;
|
|
|
|
|
+ }
|
|
|
return this._messages;
|
|
return this._messages;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public deleteMessage(idx: number) {
|
|
|
|
|
- if (this._messages.length > idx) {
|
|
|
|
|
- this._messages.splice(idx, 1);
|
|
|
|
|
|
|
+ public async deleteMessage(id: string) {
|
|
|
|
|
+ const messages = await this.getMessages();
|
|
|
|
|
+ const idx = messages.findIndex(msg => msg.id === id);
|
|
|
|
|
+ if (idx >= 0) {
|
|
|
|
|
+ messages.splice(idx, 1);
|
|
|
|
|
+ await util.promisify(fs.unlink)(path.resolve(process.env.CHATDATA_DIR, id));
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
return false;
|
|
return false;
|