-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.ts
More file actions
60 lines (47 loc) · 1.61 KB
/
notifier.ts
File metadata and controls
60 lines (47 loc) · 1.61 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
import settings from "./settings"
import { GmailID, GmailThread } from "./gmail-classes"
import { exec, execSync } from "child_process"
let knownThreadIds: { [id: string]: boolean } = {}
let activeNotifications: { [id: string]: boolean } = {}
interface NotifyOptions {
message: string
title: string
subtitle?: string
id?: string
activate?: string
}
function notify(options: NotifyOptions) {
let command = `echo "${options.message}" | terminal-notifier -title "\\${options.title}"`
if (options.activate) {
command += ` -activate ${options.activate}`
}
if (options.id) {
command += ` -group ${options.id}`
}
exec(command)
}
function removeNotification(options: { id: string }) {
execSync(`terminal-notifier -remove "${options.id}"`)
}
function notifyThread(thread: GmailThread) {
activeNotifications[thread.id] = true
notify({ title: thread.subject, message: `From: ${thread.from}`, id: idForThread(thread) })
}
function idForThread(thread: GmailThread) {
return `inbox-cli-${thread.id}`
}
export default {
processNewThreads(oldThreads: GmailThread[], newThreads: GmailThread[]) {
knownThreadIds = oldThreads.reduce((memo, t) => ({ ...memo, [t.id]: true }), {})
const threadsThatJustAppeared = newThreads.filter(t => !knownThreadIds[t.id])
if (settings.load().showNotifications && threadsThatJustAppeared.length > 0) {
threadsThatJustAppeared.forEach(t => notifyThread(t))
}
},
hideNotification(thread: GmailThread) {
if (activeNotifications[thread.id]) {
removeNotification({ id: idForThread(thread) })
activeNotifications[thread.id] = false
}
}
}