-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbob.js
183 lines (161 loc) · 4.56 KB
/
bob.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
const mongoose = require('mongoose');
const request = require('request');
// Define and compile Bob Schema
const bobSchema = mongoose.Schema({
data: {},
startDate: { type: Date, default: Date.now() },
endDate: { type: Date, default: Date.now() + 604800 }, // One week from now
description: String,
flavor: String,
tags: [],
votes: { type: Number, default: 1 },
flag: { type: Number, default: 0 }, // 0: OK, 1: Flagged, 2: Mod OK, 3: Mod Remove
mediaReady: Boolean
});
const BobModel = mongoose.model('Bob', bobSchema);
// Bob functions
/**
Create and save a new bob in the db
@param {Object[]} bobData
*/
function saveBob(bobData) {
// TODO: use mediaStatus, or remove this code
// let mediaStatus = "";
// if (bobData.data.Link) {
// mediaStatus = false;
// } else {
// console.log("Warning: Saving bob without Link");
// mediaStatus = true;
// }
const newBob = new BobModel({
data: bobData.data,
startDate: bobData.startDate,
endDate: bobData.endDate,
description: bobData.description,
flavor: bobData.flavor,
tags: bobData.tags,
mediaReady: true
});
return newBob.save(function (err) {
checkMediaStatus(bobData.data.Link);
if (err) console.log("Bob save error:", err);
});
}
/**
* Updates the status of a mediaReady
* @param {String} url
*/
function checkMediaStatus(url) {
request.head(url, function (err, res, _body) {
if (err){ console.log(err); }
if (res.statusCode === 200) {
setMediaStatus(url, true);
} else {
setMediaStatus(url, false);
}
});
}
/**
Gets all bobs
@param {Object[]} [filter] - Optional mongoose filter
*/
function getBobs(filter) {
return BobModel.find(filter).and({flag: [0, 2]}).sort('-startDate').lean();
}
/**
Gets the first matching bob
@param {Object[]} [filter] - Optional mongoose filter
*/
function getOneBob(filter) {
return BobModel.findOne(filter).and({flag: [0, 2]});
}
/**
Gets all active bobs - First maxBobs that aren't flagged
@param {Object[]} [filter] - Optional mongoose filter
@param {Object[]} [maxBobs=20] - Number of bobs to return
*/
function getActiveBobs(filter, maxBobs = 20) {
// Get first maxBobs bobs that aren't flagged. Can flesh out with fancier algorithms in the future
let query = BobModel.find(filter).lean();
query.and({flag: [0, 2]}); // If not flagged
query.and({mediaReady: true}); // Only bobs with media ready
query.sort('-startDate'); // Sort newest to oldest
query.limit(maxBobs); // First n bobs
return query;
}
/**
Gets all flagged bobs
@param {Object[]} [filter] - Optional mongoose filter
*/
function getFlaggedBobs(filter) {
return BobModel.find(filter).and({ flag: 1 }).sort('-startDate').lean();
}
/**
Update an existing bob in the db. Checks for _id.
Allows editing: data, tags, startDate, endDate
@param {Object[]} bobData
*/
function updateBob(bobData) {
return BobModel.update(
{ _id: bobData._id },
{
// Do not allow updating flavor
data: bobData.data,
tags: bobData.tags,
startDate: bobData.startDate,
endDate: bobData.endDate
}
);
}
/**
Removes a bob from the db
@param {Object[]} bobId - ObjectId of bob to delete
*/
function deleteBob(bobId) {
return BobModel.remove(
{ _id: bobId}
);
}
/**
Upvotes a bob
@param {Object[]} bobId - ObjectId of bob to upvote
*/
function upvoteBob(bobId) {
return BobModel.findOneAndUpdate(
{ _id: bobId },
{
$inc: { votes: 1} // Increment votes by 1
}
).and({flag: [0, 2]}).lean();
}
/**
Flags a bob for moderation
@param {Object[]} bobId - ObjectId of bob to flag
*/
function flagBob(bobId) {
return BobModel.findOneAndUpdate({ _id: bobId }, { flag: 1 }).and({flag: [0, 2]}).lean();
}
/**
Sets a bob's mediaReady status
@param {String} mediaURL - URL that is ready
@param {Boolean} [status=true] - status of media to set
*/
function setMediaStatus(mediaURL, status=true) {
console.log("looking for bob with..:" + mediaURL);
const data = BobModel.findOneAndUpdate({ data : { Link: mediaURL }}, { mediaReady: status }).lean();
console.log("Setting : " + data);
return data;
}
let Bob = {};
Bob.model = BobModel;
Bob.saveBob = saveBob;
Bob.getBobs = getBobs;
Bob.getOneBob = getOneBob;
Bob.getActiveBobs = getActiveBobs;
Bob.getFlaggedBobs = getFlaggedBobs;
Bob.updateBob = updateBob;
Bob.deleteBob = deleteBob;
Bob.upvoteBob = upvoteBob;
Bob.flagBob = flagBob;
Bob.setMediaStatus = setMediaStatus;
module.exports = Bob;