This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
forked from jontcarroll/trellomirror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
refbot.coffee
168 lines (149 loc) · 7.13 KB
/
refbot.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
settings = require './settings'
Promise = require 'bluebird'
Redis = require 'then-redis'
NodeTrello = require 'node-trello'
url = require 'url'
moment = require 'moment'
express = require 'express'
bodyParser = require 'body-parser'
zipObject = require 'lodash.zipobject'
values = require 'lodash.values'
trello = Promise.promisifyAll new NodeTrello settings.TRELLO_API_KEY, settings.TRELLO_BOT_TOKEN
rurl = url.parse settings.REDIS_URL
redis = Redis.createClient
host: rurl.hostname
port: rurl.port
password: rurl.auth.split(':')[1]
this.addRefbotHooks = (app) ->
app.post '/webhooks/tracked-board', (request, response) ->
payload = request.body
action = payload.action
data = action.data
console.log 'card ' + payload.model.shortUrl + ': ' + payload.action.type
# console.log JSON.stringify payload.action, null, 2
response.send 'ok'
if action.memberCreator.id == settings.TRELLO_BOT_ID # @refbot
return
if action.memberCreator.id == '557976153d63ef846e16a992' # @cardsync
return
try
commentDate = moment(action.date).format('MMM D, YYYY')
refText = """
> :paperclip: [#{action.memberCreator.username}](https://trello.com/#{action.memberCreator.username}) referenced this card from #{if action.type.indexOf('omment') != -1 then 'a comment at ' else if action.type.indexOf('heck') != -1 then 'a checkItem from' else 'the description of'} https://trello.com/c/#{data.card.shortLink} on #{commentDate}[.](http://websitesfortrello.com/)
"""
notHere = (match) -> match not in [data.card.id, data.card.shortLink]
catch e
return
handle = switch action.type
when 'commentCard'
matches = (require './match')(data.text).filter(notHere)
Promise.resolve().then(->
Promise.all matches.map((m) -> trello.postAsync "/1/cards/#{m}/actions/comments", text: refText)
).then((comments) ->
if comments.length
# add newly created ids to redis
redis.hmset 'comment:' + action.id, zipObject(matches, comments.map((c) -> c.id))
)
when 'updateComment'
newmatches = (require './match')(data.action.text).filter(notHere)
oldmatches = (require './match')(data.old.text).filter(notHere)
removedmatches = oldmatches.filter((x) -> x not in newmatches)
addedmatches = newmatches.filter((x) -> x not in oldmatches)
Promise.resolve().then(->
redis.hgetall 'comment:' + data.action.id
).then((targets) ->
removedids = removedmatches.map((m) -> targets[m])
# remove old and create new
Promise.all [
Promise.all addedmatches.map((m) -> trello.postAsync "/1/cards/#{m}/actions/comments", text: refText)
redis.hdel 'comment:' + data.action.id, removedmatches if removedmatches.length
Promise.all removedids.map((id) -> trello.delAsync("/1/actions/#{id}").catch(console.log.bind console))
]
).spread((addedcomments) ->
if addedcomments.length
# add newly created ids to redis
addedids = addedcomments.map((c) -> c.id)
redis.hmset 'comment:' + data.action.id, zipObject(addedmatches, addedids)
)
when 'deleteComment'
Promise.resolve().then(->
redis.hgetall 'comment:' + data.action.id
).then((targets) ->
# just remove everything
Promise.all [
Promise.all (values targets).map((id) -> trello.delAsync("/1/actions/#{id}").catch(console.log.bind console))
redis.del 'comment:' + data.action.id
]
)
when 'updateCard'
if 'desc' of data.old
newmatches = (require './match')(data.card.desc).filter(notHere)
oldmatches = (require './match')(data.old.desc).filter(notHere)
removedmatches = oldmatches.filter((x) -> x not in newmatches)
addedmatches = newmatches.filter((x) -> x not in oldmatches)
Promise.resolve().then(->
redis.hgetall 'desc:' + data.card.id
).then((targets) ->
removedids = removedmatches.map((m) -> targets[m])
# remove old and create new
Promise.all [
Promise.all addedmatches.map((m) -> trello.postAsync "/1/cards/#{m}/actions/comments", text: refText)
redis.hdel 'desc:' + data.card.id, removedmatches if removedmatches.length
Promise.all removedids.map((id) -> trello.delAsync("/1/actions/#{id}").catch(console.log.bind console))
]
).spread((addedcomments) ->
if addedcomments.length
# add newly created ids to redis
addedids = addedcomments.map((c) -> c.id)
redis.hmset 'desc:' + data.card.id, zipObject(addedmatches, addedids)
)
else
Promise.resolve(null)
when 'createCheckItem'
matches = (require './match')(data.checkItem.name).filter(notHere)
Promise.resolve().then(->
Promise.all matches.map((m) -> trello.postAsync "/1/cards/#{m}/actions/comments", text: refText)
).then((comments) ->
if comments.length
# add newly created ids to redis
redis.hmset 'checkItem:' + data.checkItem.id, zipObject(matches, comments.map((c) -> c.id))
)
when 'updateCheckItem'
if 'name' of data.old
newmatches = (require './match')(data.checkItem.name).filter(notHere)
oldmatches = (require './match')(data.old.name).filter(notHere)
removedmatches = oldmatches.filter((x) -> x not in newmatches)
addedmatches = newmatches.filter((x) -> x not in oldmatches)
Promise.resolve().then(->
redis.hgetall 'checkItem:' + data.checkItem.id
).then((targets) ->
removedids = removedmatches.map((m) -> targets[m])
# remove old and create new
Promise.all [
Promise.all addedmatches.map((m) -> trello.postAsync "/1/cards/#{m}/actions/comments", text: refText)
redis.hdel 'checkItem:' + data.checkItem.id, removedmatches if removedmatches.length
Promise.all removedids.map((id) -> trello.delAsync("/1/actions/#{id}").catch(console.log.bind console))
]
).spread((addedcomments) ->
if addedcomments.length
# add newly created ids to redis
addedids = addedcomments.map((c) -> c.id)
redis.hmset 'checkItem:' + data.checkItem.id, zipObject(addedmatches, addedids)
)
else
Promise.resolve(null)
when 'deleteCheckItem'
Promise.resolve().then(->
redis.hgetall 'checkItem:' + data.checkItem.id
).then((targets) ->
# just remove everything
Promise.all [
Promise.all (values targets).map((id) -> trello.delAsync("/1/actions/#{id}").catch(console.log.bind console))
redis.del 'checkItem:' + data.checkItem.id
]
)
else Promise.resolve(null)
handle.then(console.log.bind console).catch(console.log.bind console)
# port = process.env.PORT or 5000
# app.listen port, '0.0.0.0', ->
# console.log 'running at 0.0.0.0:' + port