-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (126 loc) · 3.67 KB
/
index.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
const BOARD_NAME = 'FamilySearch Research Tasks';
const BOARD_DESCRIPTION = 'Track your research tasks in Trello. Used by the FamilySearch Custom Trello Tab.';
setup();
function setup() {
// When the login is clicked we initiate interactive login meaning we use the popup
$('#login').click(function(){
login(true);
});
// Noninteractive login to check if an auth token already exists in storage
login(false);
}
/**
* Initiate the auth sequence and handle the result
*
* @param {Boolean=} interactive Whether to use the popup or just try to authenticate with existing tokens stored locally.
*/
async function login(interactive = true) {
const loggedIn = await trelloAuth(interactive);
if(!loggedIn) {
$('#login').show();
} else {
begin();
}
}
/**
* Initiate Authentication with Trello.
*
* @param {Boolean=} interactive Whether to use the popup or just try to authenticate with existing tokens stored locally.
* @return {Promise<boolean>} resolves to true (authenticated) or false (not authenticated)
*/
function trelloAuth(interactive = true){
return new Promise((resolve, reject) => {
Trello.authorize({
type: 'popup',
name: 'FamilySearch Trello Tab',
scope: {
read: 'true',
write: 'true'
},
interactive,
expiration: 'never',
success: () => resolve(true),
error: () => resolve(false)
});
});
}
/**
* Promisify the Trello.rest() method provided by the client.js Trello library
*
* @param {String} method
* @param {String} url
* @param {Object=} params
* @return {Promise} resolves to response object
*/
function trelloRequest(method, url, params = {}) {
return new Promise((resolve, reject) => {
Trello.rest(method, url, params, resolve, reject);
});
}
async function begin() {
$('#login').hide();
document.write('<h2>Authenticated</h2>');
const boardId = await getBoardId();
const listId = await getListId(boardId, getFSPersonId());
console.log(listId);
}
function getFSPersonId() {
const params = (new URL(document.location.href)).searchParams;
return params.get('pid');
}
/**
* Get the ID of the Trello list for this person, or create a new one
*
* @param {String} boardId Trello board ID
* @param {String} pid FamilySearch person ID
*/
async function getListId(boardId, pid) {
const nameRegex = new RegExp(`${pid}$`);
// Get all lists for this board
const existingList = await trelloRequest('GET', `/board/${boardId}/lists`, {
filter: 'open'
}).then((lists) => {
return lists.find(l => nameRegex.test(l.name));
});
if(existingList) {
return existingList.id;
}
// Create a new board for this person
else {
const response = await trelloRequest('POST', `/board/${boardId}/lists`, {
// TODO: add person's name to the board name
name: pid,
pos: 'bottom'
});
return response.id;
}
}
/**
* Get the ID of the FamilySearch board or create a new board and return its ID
*/
async function getBoardId() {
// Get a list of open boards
const existingFamilySearchBoard = await trelloRequest('GET', '/members/me/boards', {
filter: 'open'
})
// Filter to just personal boards
.then((boards) => {
return boards.filter(b => b.idOrganization === null);
})
// Choose a FamilySearch board if one exists
.then((boards) => {
return boards.find(b => b.name === BOARD_NAME);
});
if(existingFamilySearchBoard) {
return existingFamilySearchBoard.id;
}
// Create a board if one doesn't exist
else {
const response = await trelloRequest('POST', '/boards', {
name: BOARD_NAME,
desc: BOARD_DESCRIPTION,
defaultLists: false
});
return response.id;
}
}