-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (64 loc) · 1.95 KB
/
index.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
const notifier = require('node-notifier');
const stripAnsi = require('strip-ansi');
let notifications = [];
let notifierString;
exports.middleware = store => next => action => {
if ('SESSION_ADD_DATA' === action.type) {
const {data} = action;
registerConfigs();
if (isNotifierString(data)) {
// load notification configs
let instanceOverrides = findNotifierString(data);
if (instanceOverrides) {
instanceOverrides.message = stripAnsi(data).replace(/\r?\n|\r|%/g, '');
if (instanceOverrides.reply) {
notifier.notify(instanceOverrides, responseHandler(next, action, store));
} else {
notifier.notify(instanceOverrides);
}
}
}
next(action);
} else {
next(action);
}
};
function sendSessionData(uid, data, escaped) {
return (dispatch, getState) => {
dispatch({
type: 'SESSION_USER_DATA',
data,
effect() {
// TODO
// If no uid is passed, data is sent to the active session.
const targetUid = uid || getState().sessions.activeUid;
window.rpc.emit('data', {uid: targetUid, data, escaped});
}
});
};
}
function isNotifierString(data) {
return new RegExp('(' + notifierString + ')').test(data);
}
function findNotifierString(data) {
return notifications.find(n => {
return new RegExp('(' + n.test + ')').test(data);
});
}
const responseHandler = (next, action, store) => (error, response, metadata) => {
if (metadata && metadata.activationValue) {
sendSessionData(action.uid, `${metadata.activationValue}\r`)(store.dispatch, store.getState);
}
};
function registerConfigs() {
const notifierConfigs = window.config.getConfig().hyperNotifier;
if (notifications.length > 0 && !notifierString) {
return;
}
if (notifierConfigs.notifications) {
notifications = notifierConfigs.notifications;
}
if (!notifierString) {
notifierString = notifications.map(n => n.test).join(')|(');
}
}