|
|
@@ -1,98 +0,0 @@
|
|
1
|
|
-import { RPCExporter, SubscriptionResponse, ErrorResponse, SuccessResponse } from "rpclibrary";
|
|
2
|
|
-import { FrontworkAdmin } from "../Admin/Admin";
|
|
3
|
|
-import { TableDefinitionExporter } from "../Types/Interfaces";
|
|
4
|
|
-import { getLogger } from 'frontblock-generic/Types';
|
|
5
|
|
-
|
|
6
|
|
-import * as uuid from 'uuid/v4'
|
|
7
|
|
-import { TableDefiniton } from "../Types/Types";
|
|
8
|
|
-
|
|
9
|
|
-export type NotificationSeverity = 'Info' | 'Important' | 'Error'
|
|
10
|
|
-
|
|
11
|
|
-export type Notification = {
|
|
12
|
|
- ID?:number,
|
|
13
|
|
- severity: NotificationSeverity,
|
|
14
|
|
- topic: string,
|
|
15
|
|
- message:string,
|
|
16
|
|
- time?: number
|
|
17
|
|
-}
|
|
18
|
|
-
|
|
19
|
|
-export type EventbusIfc = {
|
|
20
|
|
- Eventbus: {
|
|
21
|
|
- getNotificationLog: () => Promise<Notification[]>
|
|
22
|
|
- pushNotification: (notification:Notification) => Promise<void>
|
|
23
|
|
- subscribeNotifications: (callback:Function) => Promise<SubscriptionResponse | ErrorResponse>
|
|
24
|
|
- }
|
|
25
|
|
-}
|
|
26
|
|
-
|
|
27
|
|
-const logger = getLogger("Eventbus", 'debug')
|
|
28
|
|
-
|
|
29
|
|
-export class FrontworkEventBus
|
|
30
|
|
-implements RPCExporter<EventbusIfc, "Eventbus">, TableDefinitionExporter {
|
|
31
|
|
- name = "Eventbus" as "Eventbus"
|
|
32
|
|
- private subscriptions : { [uid in string]:Function } = {}
|
|
33
|
|
-
|
|
34
|
|
- constructor(private admin: FrontworkAdmin){
|
|
35
|
|
- }
|
|
36
|
|
-
|
|
37
|
|
- async subscribeNotifications(callback) : Promise<SubscriptionResponse>{
|
|
38
|
|
- const uid = uuid()
|
|
39
|
|
- this.subscriptions[uid] = callback
|
|
40
|
|
- return { result: 'Success', uuid: uid }
|
|
41
|
|
- }
|
|
42
|
|
-
|
|
43
|
|
- async getNotificationLog() : Promise<Notification[]>{
|
|
44
|
|
- try{
|
|
45
|
|
- return await this.admin.knex.select('*').from('notifications')
|
|
46
|
|
- }catch(e){
|
|
47
|
|
- logger.error(e)
|
|
48
|
|
- throw e
|
|
49
|
|
- }
|
|
50
|
|
- }
|
|
51
|
|
-
|
|
52
|
|
- async unsubscribeNotifications(uid:string) : Promise<SuccessResponse | ErrorResponse>{
|
|
53
|
|
- if(!this.subscriptions[uid]) return { result: 'Error', message: "Unknown subscription" }
|
|
54
|
|
- delete this.subscriptions[uid]
|
|
55
|
|
- return { result: 'Success' }
|
|
56
|
|
- }
|
|
57
|
|
-
|
|
58
|
|
- async pushNotification(notification: Notification){
|
|
59
|
|
- if(!notification.time) notification.time = Date.now()
|
|
60
|
|
- logger.debug("inserting into notifications", notification)
|
|
61
|
|
- try{
|
|
62
|
|
- await this.admin.knex('notifications').insert(notification)
|
|
63
|
|
- }catch(e){
|
|
64
|
|
- logger.error(e)
|
|
65
|
|
- throw e
|
|
66
|
|
- }
|
|
67
|
|
-
|
|
68
|
|
- Object.values(this.subscriptions).forEach(callback => {
|
|
69
|
|
- callback(notification)
|
|
70
|
|
- })
|
|
71
|
|
- }
|
|
72
|
|
-
|
|
73
|
|
- exportRPCs(){
|
|
74
|
|
- return [{
|
|
75
|
|
- name: 'getNotificationLog' as 'getNotificationLog',
|
|
76
|
|
- call: this.getNotificationLog
|
|
77
|
|
- },{
|
|
78
|
|
- name: 'pushNotification' as 'pushNotification',
|
|
79
|
|
- call: this.pushNotification
|
|
80
|
|
- },{
|
|
81
|
|
- name: 'subscribeNotificaitons' as 'subscribeNotifications',
|
|
82
|
|
- hook: async (callback: Function) => await this.subscribeNotifications(callback)
|
|
83
|
|
- }]
|
|
84
|
|
- }
|
|
85
|
|
-
|
|
86
|
|
- getTableDefinitions(): TableDefiniton[]{
|
|
87
|
|
- return [{
|
|
88
|
|
- name: 'notifications',
|
|
89
|
|
- tableBuilder: (table) => {
|
|
90
|
|
- table.increments('ID').primary();
|
|
91
|
|
- table.string('severity');
|
|
92
|
|
- table.string('topic');
|
|
93
|
|
- table.string('message');
|
|
94
|
|
- table.timestamp('time');
|
|
95
|
|
- }
|
|
96
|
|
- }]
|
|
97
|
|
- }
|
|
98
|
|
-}
|