-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail-later.coffee
169 lines (132 loc) · 3.57 KB
/
gmail-later.coffee
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
162
163
164
165
166
167
168
169
_l = -> Logger.log.apply(Logger, arguments)
D = new Date()
NOW = D.getTime()
TODAY = new Date(D.getFullYear(), D.getMonth(), D.getDate()).getTime()
`
function setup() {
InputLabel.root().create()
InputLabel.createAll();
ReminderLabel.root().create()
}
function process() {
InputLabel.processAll();
ReminderLabel.processAll();
}
`
class Label
LABELS = {}
constructor: ({@name, @instance})->
create: ->
_l "create #{@name}"
@instance ||= @get() || GmailApp.createLabel(@name)
delete: ->
_l "removing #{@name}"
@get()?.deleteLabel()
get: ->
@instance ||= GmailApp.getUserLabelByName(@name)
threads: ->
@get()?.getThreads()
isEmpty: ->
! @threads().length
moveTo: (to, {remove}={})->
_l " #{@name} -> #{to.name}"
to.create().addToThreads @threads()
@get().removeFromThreads @threads()
@delete() if remove
moveToInbox: ({remove}={})->
_l " #{@name} -> Inbox"
GmailApp.moveThreadsToInbox @threads()
@get().removeFromThreads @threads()
@delete() if remove
# Class methods
@byName: (name, instance)->
LABELS[name] ||= new @ {name, instance}
@root: ->
@byName(@ROOT)
# this is not an instance method on Label, because then it would always
# create instances of Label. This way it creates instances of @, which can be a derived class
@at: (name)->
@byName "#{@root().name}/#{name}"
@forEach: (method)->
for l in @all()
l[method]()
@childrenOf: (parent)->
@byName(l.getName(), l) for l in GmailApp.getUserLabels() when l.getName().slice(0, parent.name.length + 1) == "#{parent.name}/"
H = 3600 * 1000
D = 24 * H
W = 7 * D
M = 30 * D
Y = 365 * D
pad = (n)->
if n < 10
"0#{n}"
else
n
formatTime = (t)->
y = t.getFullYear()
m = pad t.getMonth() + 1
d = pad t.getDate()
_h = pad t.getHours()
_m = pad t.getMinutes()
"#{y}-#{m}-#{d} #{_h}:#{_m}"
humanName = (ts)->
"#{formatTime new Date(ts)} - #{ts}"
class InputLabel extends Label
@ROOT = '#later'
due_at: ->
throw "no match" unless match = /^(?:.*\/)?(\d+)([hdwmy])$/.exec(@name)
[_, n, x] = match
switch x
when "h" then NOW + n * H
when "d" then TODAY + n * D
when "w" then TODAY + n * W
when "m" then TODAY + n * M
when "y" then TODAY + n * Y
reminder: ->
ReminderLabel.at humanName @due_at()
process: ->
return if @isEmpty()
@moveTo @reminder()
# Class methods
IN = [
"1h", "2h", "3h", "4h", "8h", "10h", "12h",
"1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "10d",
"1w", "2w", "3w", "4w",
"1m", "2m", "3m", "4m", "5m", "6m", "7m", "8m", "9m", "10m", "11m",
"1y"
]
@all: ->
@in ||=
@at(n) for n in IN
@createAll: -> @forEach 'create'
@processAll: -> @forEach 'process'
class ReminderLabel extends Label
@ROOT = '#reminders'
parent: ->
unless match = /^(.*\/)/.exec(@name)
_l "no parent match: #{@name}"
return
match[1]
due_at: ->
unless match = /^.*\/(?:[0-9: -]* - )([0-9]+)$/.exec(@name)
_l "no match: #{@name}"
return
parseInt match[1], 10
pastDue: ->
(n = @due_at()) && (n < NOW)
# Class methods
@processAll: ->
_l "processing at #{humanName NOW}"
for l in @childrenOf(@root())
_l l.name
if l.pastDue()
l.moveToInbox(remove: true)
# @renameAll: ->
# for l in @childrenOf(@root())
# _l l.name
# hname = humanName l.due_at()
#
# unless hname == l.name
# _l " -> #{hname}"
# to = @at hname
# l.moveTo to, remove: true