-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrelloClient.js
240 lines (202 loc) · 8.28 KB
/
trelloClient.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* TODO: Better error management, we should be able to tell the client exactly what went wrong insteal of
* vague messages such as "unauthorized access" or "strange error"
*/
function TrelloClient () {
this.apiKey = null;
this.apiSecret = null;
this.clientToken = null; // Required to see non public boards
this.username = null;
this.openBoards = [];
this.currentLists = [];
this.currentCards = [];
}
// If the user is logged into Trello, we can get his API key and secret for him
// The callback takes an err argument
TrelloClient.prototype.getApiCredentials = function (cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://trello.com/1/appKey/generate" }).done(function (data) {
try {
var html = $($.parseHTML(data))
, apiKey = html.find("input#key").val()
, apiSecret = html.find("input#secret").val()
;
if (!apiKey || !apiSecret) {
return callback("Couldn't get API key and secret from the returned HTML");
} else {
self.apiKey = apiKey;
self.apiSecret = apiSecret;
return callback(null);
}
} catch (e) {
return callback(e);
}
}).fail(function () {
return callback("Unauthorized access");
});
};
// Simulates the user requesting then accepting a client token for this application
// The callback takes only one err argument
TrelloClient.prototype.getClientToken = function(cb) {
var self = this
, callback = cb || function() {};
if (!this.apiKey) { return callback("Can't request token without an API key"); }
// TODO: check the scope is right
$.ajax({ url: "https://trello.com/1/authorize?key=" + self.apiKey + "&name=TrelloCapture&expiration=never&response_type=token&scope=read,write" }).done(function(data) {
try {
var html = $($.parseHTML(data))
, approve = "Allow"
, requestKey = html.find("input[name='requestKey']").val()
, signature = html.find("input[name='signature']").val()
;
} catch(e) { return callback(e); }
$.ajax({ type: 'POST'
, url: 'https://trello.com/1/token/approve'
, data: { approve: approve, requestKey: requestKey, signature: signature }
}).done(function(data) {
try {
var html = $($.parseHTML(data))
, token = $(html[5]).html().replace(/ /g, '').replace(/\r\n/g, '').replace(/\n/g, '').replace(/\r/g, '')
;
} catch (e) { return callback(e); }
if (token && token.length === 64) {
self.clientToken = token;
return callback(null);
} else {
// Should probably loop on the html array to check whether its structure was changed
return callback("Couldn't get valid token");
}
}).fail(function () {
return callback("Trello refused to give a token");
});
}).fail(function () {
return callback("Unauthorized access");
});
};
// Simulates going to the frontpage and scrape the username
// If this.username is not populated, it means user is not logged in
// Callback takes one err argument
TrelloClient.prototype.getLoggedUsername = function (cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://trello.com/1/Members/me" }).done(function (data) {
self.username = data.username;
return callback(null);
}).fail(function () {
return callback("Unauthorized access");
});
};
TrelloClient.prototype.getAllBoards = function (cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://api.trello.com/1/members/" + this.username + "/boards?key=" + this.apiKey + "&token=" + this.clientToken }).done(function(data) {
self.openBoards = _.filter(data, function(board) { return board.closed === false; });
return callback(null);
}).fail(function() {
return callback("Unauthorized access");
});
};
TrelloClient.prototype.getAllLabelsNames = function(boardId, cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://api.trello.com/1/boards/" + boardId + "/labelNames?key=" + this.apiKey + "&token=" + this.clientToken }).done(function(data) {
self.currentLabels = data;
return callback(null);
}).fail(function() {
return callback("Unauthorized access");
});
}
TrelloClient.prototype.getAllCurrentLists = function (boardId, cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://api.trello.com/1/boards/" + boardId + "/lists?key=" + this.apiKey + "&token=" + this.clientToken }).done(function(data) {
self.currentLists = _.filter(data, function (list) { return list.closed === false; });
return callback(null);
}).fail(function() {
return callback("Unauthorized access");
});
};
TrelloClient.prototype.getAllCurrentCards = function (listId, cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://api.trello.com/1/lists/" + listId + "/cards?key=" + this.apiKey + "&token=" + this.clientToken }).done(function(data) {
self.currentCards = _.filter(data, function (card) { return card.closed === false; });
return callback(null);
}).fail(function() {
return callback("Unauthorized access");
});
};
// Callback signature: err, createdCardId
TrelloClient.prototype.createCardAtBottomOfCurrentList = function (listId, cardName, cardDesc, _labels, cb) {
var self = this
, callback = cb || function() {}
, labels = _labels.length > 0 ? '&labels=' + _labels.join(',') : ''
;
$.ajax({ url: "https://api.trello.com/1/lists/" + listId + "/cards?key=" + this.apiKey + "&token=" + this.clientToken + labels
, type: 'POST'
, data: { name: cardName, desc: cardDesc, pos: 'top' }
}).done(function(data) {
return callback(null, data.id);
}).fail(function() {
return callback("Unauthorized access");
});
};
// Callback signature: err, createdCardId
TrelloClient.prototype.putCardOnTopOfList = function (cardId, cb) {
var self = this
, callback = cb || function() {}
;
$.ajax({ url: "https://api.trello.com/1/cards/" + cardId + "?key=" + this.apiKey + "&token=" + this.clientToken
, type: 'PUT'
, data: { pos: 'top' }
}).done(function(data) {
return callback(null, data.id);
}).fail(function() {
return callback("Unauthorized access");
});
};
// TODO: a bit scrappy since it is dependent on the title Trello sends us. A better way would be to use the url or an API call
// Callback signature: err, boolean indicating whether login was successful or not
TrelloClient.prototype.logUserIn = function(email, password, cb) {
var self = this
, callback = cb || function() {};
$.ajax({ url: "https://trello.com/authenticate"
, type: 'POST'
, data: { user: email, password: password, returnUrl: '/' }
}).done(function(data, textStatus, jqXHR) {
if (jqXHR.responseText.match(/<title>Log In[^<]*<\/title>/)) { // Trello still wants us to login, so login was not successful
return callback(null, false);
} else {
return callback(null, true);
}
}).fail(function(jqXHR) {
return callback("Strange error");
});
};
// Take as input a base64-encoded image (for example given by the tabs API screenshot)
// And send attach it to a Trello card
TrelloClient.prototype.attachBase64ImageToCard = function(cardId, imageData, progressHandler, endHandler) {
var imageDataElements = imageData.split(',')
, mimeType = imageDataElements[0].split(':')[1].split(';')[0]
, imageB64Data = imageDataElements[1]
, byteString = atob(imageB64Data)
, length = byteString.length
, ab = new ArrayBuffer(length)
, ua = new Uint8Array(ab)
, blob, formData, request, i
;
for (i = 0; i < length; i++) {
ua[i] = byteString.charCodeAt(i);
}
blob = new Blob([ab], { type: mimeType });
formData = new FormData();
formData.append("key", this.apiKey);
formData.append("token", this.clientToken);
formData.append("file", blob, "trello-capture-screenshot.jpg"); // TODO slugify title or beginning of title
request = new XMLHttpRequest();
request.upload.addEventListener("progress", progressHandler || function () {});
request.onload = endHandler || function () {};
request.open("POST", "https://api.trello.com/1/cards/" + cardId + "/attachments");
request.send(formData);
}