-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMute GitHub Thread.js
More file actions
88 lines (73 loc) · 1.94 KB
/
Mute GitHub Thread.js
File metadata and controls
88 lines (73 loc) · 1.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function header(message) {
return headers(message).find(unsubscribe)
}
function unsubscribe(header) {
return header.name().toLowerCase() === 'list-unsubscribe'
}
function replyTo(header) {
return header.name().toLowerCase() === 'in-reply-to'
}
function compact(value) {
return value !== undefined
}
function urls(header) {
return header.content().split(',')
}
function clean(url) {
return url.trim().replace(/[<>]/g, '')
}
function http(url) {
return /^http/.test(url)
}
function app() {
const app = Application.currentApplication()
app.includeStandardAdditions = true
return app
}
function curl(url) {
return 'curl ' + url + ' &> /dev/null &'
}
function mute(commands) {
if (commands.length) {
app().doShellScript(commands.join("\n"))
}
}
function announce(count) {
const label = (count === 1) ? 'conversation' : 'conversations'
const text = count + ' ' + label + ' muted.'
app().displayNotification(text, {withTitle: 'Thread Muted'})
}
function groupBy(values, fn) {
return values.reduce(function(groups, value) {
const key = fn(value)
groups[key] = groups[key] || []
groups[key].push(value)
return groups
}, {})
}
function headers(message) {
return message.allHeaders().trim().split("\n").map(function(line) {
const [all, key, value] = line.trim().match(/([\w-]+):(.*)/)
const name = () => key.trim()
const content = () => value.trim()
return {name, content}
})
}
function thread(message) {
const reply = headers(message).find(replyTo)
return reply ? clean(reply.content()) : message.messageId()
}
function run(input, parameters) {
const app = Application('Mail')
const conversations = groupBy(app.selection(), thread)
const messages = Object.keys(conversations).map(key => conversations[key][0])
const commands = messages
.map(header)
.filter(compact)
.flatMap(urls)
.map(clean)
.filter(http)
.map(curl)
mute(commands)
announce(commands.length)
}