-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhey.js
55 lines (48 loc) · 1.16 KB
/
hey.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
const {admin} = require('./firebase-admin');
const AGGR_TIME = 60;
const BARK_TEXT = "Woof!";
const context = {
ref: null,
text: "",
};
const EventEmitter = require('events');
const emitter = new EventEmitter();
var sendNotification = async function() {
if (context.ref) {
context.text = context.text + " " + BARK_TEXT;
return context.ref.set({
text: context.text,
updateTime: admin.firestore.FieldValue.serverTimestamp()
}, {
merge: true
})
.then(() => {
console.log("updated hey!");
})
.catch((err) => {
console.error("hey update error", err);
})
}
else {
context.text = BARK_TEXT;
await admin.firestore().collection('hubs/emma/messages').add({
text: context.text,
createTime: admin.firestore.FieldValue.serverTimestamp(),
}).then((ref) => {
context.ref = ref;
console.log("sent hey!");
})
.catch((err) => {
console.error("hey error", err);
})
setTimeout(() => {
context.ref = null;
context.text = "";
emitter.emit('reset');
}, AGGR_TIME * 1000);
}
};
module.exports = {
send: sendNotification,
on: emitter.on,
};