-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinfinite_timeline.js
More file actions
295 lines (285 loc) · 8.14 KB
/
infinite_timeline.js
File metadata and controls
295 lines (285 loc) · 8.14 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright 2018 Zane Littrell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const Login = require("./login");
/*
* Controller class for the infinite timeline.
*/
class InfiniteTimeline {
/*
* Renders an index page with the current selected stories.
*/
static index(req, dynamoDb, callback) {
const params = {
TableName: process.env.TIME_TABLE,
IndexName: "gsiSelectedTimeline",
KeyConditionExpression: "#sel = :selVal",
ExpressionAttributeNames: {
"#sel": "selected"
},
ExpressionAttributeValues: {
":selVal": "x"
},
ScanIndexForward: true // Sort by week in ascending order
};
dynamoDb.query(params, function(err, dat) {
if (err) {
console.error(err);
callback("render", "error", { error: err });
} else {
callback("render", "infinite_timeline/index", { story: dat.Items });
}
});
}
/*
* Renders a form to create a new submission for the infinite timeline.
*/
static new_story(req, dynamoDb, callback) {
callback("render", "infinite_timeline/new");
}
/*
* Creates a new infinite timeline submission in the database. Redirects
* to the infinite timline index on successful creation.
*/
static create(req, dynamoDb, callback) {
InfiniteTimeline._getWeek(dynamoDb)
.then(data => {
const params = {
TableName: process.env.TIME_TABLE,
Item: {
id: Date.now(),
content: req.body.content,
week: parseInt(data.Item.value, 10)
}
};
return dynamoDb.put(params).promise();
})
.then(() => {
callback("redirect", "/infinite_timeline");
})
.catch(err => {
console.error(err);
callback("render", "error", { error: err });
});
}
/*
* Renders a page to select a submission for the given week. Selected
* submissions will be displayed on the index page.
* NOTE:
* User must be logged in.
*/
static edit(req, dynamoDb, callback) {
if (Login.authenticate(req)) {
let first = InfiniteTimeline._getWeek(dynamoDb);
const params = {
TableName: process.env.TIME_TABLE
};
let second = dynamoDb.scan(params).promise();
Promise.all([first, second])
.then(([w, data]) => {
let week;
// If given a number week use that
if (req.query.week && !isNaN(req.query.week)) {
week = req.query.week;
// Otherwise use the current week from the database
} else {
week = w.Item.value;
}
// Filter all timeline entries to the correct week
let timeline = data.Items.filter(x => {
return parseInt(x.week) === parseInt(week);
});
callback("render", "infinite_timeline/edit", {
story: timeline,
week: week
});
})
.catch(error => {
console.error(error);
callback("render", "error", { error: error });
});
} else {
callback("redirect", "/login");
}
}
/*
* Updates which stories are selected in the database. After successfully
* updating, the user is redirected to the timeline index.
* NOTE:
* User must be logged in.
*/
static update(req, dynamoDb, callback) {
if (Login.authenticate(req)) {
dynamoDb
.scan({ TableName: process.env.TIME_TABLE })
.promise()
.then(data => {
// Get the stories from the appropriate week
let stories = data.Items.filter(x => {
return parseInt(x.week) === parseInt(req.body.week);
});
// Mark the selected story as selected and all others as unselected
const story = parseInt(req.body.story, 10);
let prom = stories.map(x => {
return InfiniteTimeline._selectStory(
x.id,
x.id === story,
dynamoDb
);
});
return Promise.all(prom);
})
.then(_ => {
callback("redirect", "/infinite_timeline");
})
.catch(error => {
console.error(error);
callback("render", "error", { error: error });
});
} else {
callback("redirect", "/login");
}
}
/*
* Renders a form to change the week value for submissions.
* NOTE:
* User must be logged in.
*/
static changeWeek(req, dynamoDb, callback) {
if (Login.authenticate(req)) {
InfiniteTimeline._getWeek(dynamoDb)
.then(w => {
callback("render", "infinite_timeline/week", { week: w.Item.value });
})
.catch(error => {
console.error(error);
callback("render", "error", { error: error });
});
} else {
callback("redirect", "/login");
}
}
/*
* Updates the week value in the database. Upon successfully updating, the
* user is redirected to the timeline index.
* NOTE:
* User must be logged in.
*/
static setWeek(req, dynamoDb, callback) {
if (Login.authenticate(req)) {
const params = {
TableName: process.env.GLOBAL_TABLE,
Key: {
key: "TimelineWeek"
},
UpdateExpression: "Set #value = :val",
ExpressionAttributeNames: {
"#value": "value"
},
ExpressionAttributeValues: {
":val": parseInt(req.body.week, 10)
}
};
dynamoDb.update(params, function(error, _data) {
if (error) {
console.error(error);
callback("render", "error", { error: error });
} else {
callback("redirect", "/infinite_timeline");
}
});
} else {
callback("redirect", "/login");
}
}
/*
* Remove unselected storiees for the database. Redirect to the timeline
* index on success.
* NOTE:
* User must be logged in.
*/
static clean(req, dynamoDb, callback) {
if (Login.authenticate(req)) {
dynamoDb
.scan({ TableName: process.env.TIME_TABLE })
.promise()
.then(data => {
let stories = data.Items.filter(x => !x.selected);
stories = stories.map(x => {
const params = {
TableName: process.env.TIME_TABLE,
Key: {
id: x.id
}
};
return dynamoDb.delete(params).promise();
});
return Promise.all(stories);
})
.then(() => {
callback("redirect", "/infinite_timeline");
})
.catch(error => {
console.error(error);
callback("render", "error", { error: error });
});
} else {
callback("redirect", "/login");
}
}
/*
* Marks a story as selected in the database if the selected param is true.
* Returns a promise.
*/
static _selectStory(id, selected, dynamoDb) {
let params;
if (selected) {
params = {
// Mark the story as selected
TableName: process.env.TIME_TABLE,
Key: {
id: id
},
UpdateExpression: "SET selected = :val",
ExpressionAttributeValues: {
":val": "x"
}
};
} else {
params = {
// Unselect the story
TableName: process.env.TIME_TABLE,
Key: {
id: id
},
UpdateExpression: "REMOVE selected"
};
}
return dynamoDb.update(params).promise();
}
/*
* Gets the current week from the database. Returns a promise.
*/
static _getWeek(dynamoDb) {
const params = {
TableName: process.env.GLOBAL_TABLE,
Key: {
key: "TimelineWeek"
}
};
return dynamoDb.get(params).promise();
}
}
module.exports = InfiniteTimeline;