This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
muter.gs
161 lines (135 loc) · 4.2 KB
/
muter.gs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function muter() {
var FETCH_OPTIONS, Message, QUERY, Thread, UNSUB_HEADER, UNSUB_URL_PREFIX, UNSUB_URL_REGEX, i, id, len, ref;
QUERY = "is:mute AND ( from:\"notifications@github.com\" OR from:\"noreply@github.com\" )";
UNSUB_HEADER = "List-Unsubscribe";
UNSUB_URL_PREFIX = "https://github.com/notifications/unsubscribe";
UNSUB_URL_REGEX = new RegExp("<(" + UNSUB_URL_PREFIX + "/.*?)>");
FETCH_OPTIONS = {
muteHttpExceptions: true
};
Thread = (function() {
Thread.all = {};
Thread.ids = [];
Thread.loadFromSearch = function(query) {
var i, len, results, t, threads;
threads = GmailApp.search(query);
GmailApp.getMessagesForThreads(threads);
results = [];
for (i = 0, len = threads.length; i < len; i++) {
t = threads[i];
results.push(new Thread(t));
}
return results;
};
function Thread(_thread) {
var m;
this._thread = _thread;
this.id = this._thread.getId();
Thread.all[this.id] = this;
Thread.ids.push(this.id);
this.messages = (function() {
var i, len, ref, results;
ref = this._thread.getMessages() || [];
results = [];
for (i = 0, len = ref.length; i < len; i++) {
m = ref[i];
results.push(new Message(m));
}
return results;
}).call(this);
this.subject = this._thread.getFirstMessageSubject();
}
Thread.prototype.unsubscribeAndUnmute = function() {
return !!(this.unmute() && this.unsubscribe());
};
Thread.prototype.unsubscribe = function() {
var res, url;
if (url = this.unsubUrl()) {
if (res = UrlFetchApp.fetch(url, FETCH_OPTIONS)) {
if (res.getResponseCode() === 200) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return true;
}
};
Thread.prototype.unsubUrl = function() {
if (this.messages.length > 0) {
return this.messages[0].unsubUrl();
}
};
Thread.prototype.unmute = function() {
this.moveToInbox();
this.moveToArchive();
return true;
};
Thread.prototype.moveToInbox = function() {
return this._thread.moveToInbox();
};
Thread.prototype.moveToArchive = function() {
return this._thread.moveToArchive();
};
return Thread;
})();
Message = (function() {
function Message(_message) {
this._message = _message;
}
Message.prototype.unsubUrl = function() {
var i, len, match, raw, ref, value;
if (raw = this.headers()[UNSUB_HEADER]) {
ref = raw.split(", ");
for (i = 0, len = ref.length; i < len; i++) {
value = ref[i];
if (match = value.match(UNSUB_URL_REGEX)) {
return match[1];
}
}
}
};
Message.prototype.headers = function() {
var i, key, len, line, match, parts, ref, ref1, value;
if (this._headers == null) {
this._headers = {};
parts = this._message.getRawContent().split("\r\n\r\n", 2);
ref = parts[0].split("\r\n");
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
if (match = line.match(/^\s+(.*)/)) {
value += " " + match[1];
} else {
if ((typeof key !== "undefined" && key !== null) && (typeof value !== "undefined" && value !== null)) {
this.setHeader(this._headers, key, value);
}
ref1 = line.split(": ", 2), key = ref1[0], value = ref1[1];
}
}
if ((key != null) && (value != null)) {
this.setHeader(this._headers, key, value);
}
}
return this._headers;
};
Message.prototype.setHeader = function(headers, key, value) {
if (Array.isArray(headers[key])) {
return headers[key].push(value);
} else if (headers[key] != null) {
return headers[key] = [headers[key], value];
} else {
return headers[key] = value;
}
};
return Message;
})();
Thread.loadFromSearch(QUERY);
ref = Thread.ids;
for (i = 0, len = ref.length; i < len; i++) {
id = ref[i];
Thread.all[id].unsubscribeAndUnmute();
}
}