-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongoDB.js
483 lines (462 loc) · 16.6 KB
/
mongoDB.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import 'dotenv/config'
import mongoose from 'mongoose';
import fetch from 'node-fetch';
let host = APP_MONGO_HOST;
console.log("CONNECTING TO", host)
mongoose.connect(host, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
const ContentCategorySchema = new mongoose.Schema({
code: {type: String, required: true},
display: {type: String, required: true},
videoOnly: {type: Boolean, default: false}
});
const CommunitySchema = new mongoose.Schema({
name: {type: String, required: false},
code: {type: String, required: true},
owner: {type: String, required: false},
description: {type: String},
image: {type: String},
coverImage: {type: String},
open: {type: Boolean},
hive: {type: String}
});
const HiveCommunitySchema = new mongoose.Schema({
name: {type: String},
title: {type: String}
})
const CommunityMemberSchema = new mongoose.Schema({
communityName: {type: String, required: true},
username: {type: String, required: true},
isCreator: {type: Boolean, default: false},
isAdmin: {type: Boolean, default: false},
isOwner: {type: Boolean, default: false}
});
const ContentCreatorSchema = new mongoose.Schema({
username: {type: String, required: true},
banned: {type: Boolean, required: true, default: false},
banReason: String,
livestreamEnabled: {type: Boolean, required: true, default: false},
canUpload: {type: Boolean, required: true, default: true},
canProxyUpvote: {type: Boolean, required: true, default: false},
upvoteDay: {type: Number},
isCitizenJournalist: {type: Boolean, required: false, default: false},
CJPromotion: {type: Number},
limit: {type: Number, required: false, default: 0},
hidden: {type: Boolean, required: true, default: false},
joined: {type: Date, required: true, default: Date.now()},
score: {type: Number, required: true, default: 0},
postWarning: {type: Boolean, default: false},
askWitnessVote: {type: Boolean, default: true, required: true},
badges: {
type: [String],
required: true,
default: []
},
lastPayment: {type: mongoose.Types.ObjectId, default: null},
warningPending: {type: Boolean, default: false},
warningText: {type: String},
upvoteEligible: {type: Boolean, default: true},
awaitingVerification: {type: Boolean, default: false, required: true},
verificationEvidence: {type: String, default: null},
verified: {type: Boolean, default: false},
verificationRequired: {type: Boolean, default: false},
profile_image: String,
cover_image: String,
autoFillTitle: {type: Boolean, default: false, required: true},
reducedUpvote: {type: Boolean, default: false},
ipfsBeta: {type: Boolean, default: false}
});
const DonationSchema = new mongoose.Schema({
username: {type: String},
address: {type: String},
ticker: {type: String}
})
const DonationAccountTypesSchema = new mongoose.Schema({
ticker: {type: String},
img: {type: String}
})
const LiveStreamKeySchema = new mongoose.Schema({
username: {type: String, required: true},
key: {type: String, required: true, unique: true},
lastUpdate: {type: Date, required: true, default: Date.now()}
})
const ContentCreatorPaymentSchema = new mongoose.Schema({
username: {type: String, required: true},
timestamp: {type: Date, required: true, default: Date.now()},
amount: {type: Number, required: true},
forwarded: {type: Boolean, required: true, default: false},
currency: {
type: String,
required: true,
enum: ["STEEM", "SBD"],
default: "STEEM"
},
type: {
type: String,
required: true,
enum: [
"signup",
"monthly",
"annually"
],
default: "monthly"
},
status: {
type: String,
required: true,
enum: [
"pending",
"completed",
"refunded"
],
default: "pending"
},
amount_refunded: {type: Number, required: true, default: 0},
memo: {type: String, required: true, unique: true}
});
const VideoSchema = new mongoose.Schema({
filename: {type: String, required: false},
updateSteem: {type: Boolean, default: false},
lowRc: {type: Boolean, default: false, required: true},
originalFilename: {type: String, required: true},
thumbnail: String,
title: String,
score_boost: Number,
needsBlockchainUpdate: {type: Boolean, default: false, required: true},
tags: String,
description: String,
status: {
type: String,
enum: ["uploaded", "encoding", "saving", "published", "deleted", "encoding_failed", "encoding_queued", "encoding_halted_time", "encoding_queued_vod", "scheduled", "encoding_ipfs", "encoding_preparing", "publish_manual", "self_deleted", "beneficiary_check_failed"],
default: 'uploaded',
required: true
},
encoding_price_steem: {type: String, required: true, default: "0.000"},
paid: {type: Boolean, default: false, required: true}, //only ever true when there was a manual payment!
encodingProgress: {type: Number, required: true, default: 0},
raw_description: String,
size: {type: Number, required: true},
permlink: {type: String, required: true},
duration: {type: Number, required: true},
created: {type: Date, required: true, default: Date.now()},
published: Date,
pipeline: String,
encoding: {
"360": {type: Boolean, default: false, required: true},
"480": {type: Boolean, default: false, required: true},
"720": {type: Boolean, default: false, required: true},
"1080": {type: Boolean, default: false, required: true}
},
owner: {type: String, required: true},
is3CJContent: {type: Boolean, required: false, default: false},
isVOD: {type: Boolean, required: false, default: false},
isNsfwContent: {type: Boolean, default: false},
declineRewards: {type: Boolean, default: false},
rewardPowerup: {type: Boolean, default: false},
language: {type: String, required: false, default: "en"},
category: {type: String, required: false, default: "general"},
firstUpload: {type: Boolean, default: false},
community: {type: String, default: null},
indexed: {type: Boolean, default: false},
views: {type: Number, default: 0},
hive: {type: String, default: 'hive-181335'},
upvoteEligible: {type: Boolean, default: true},
publish_type: {type: String, default: "publish", enum: ["publish", "schedule"], required: true},
publish_data: {type: Date},
beneficiaries: {type: String, default: '[]'},
userId: String,
votePercent: {type: Number, default: 1},
reducedUpvote: {type: Boolean, default: false},
donations: {type: Boolean, default: false},
postToHiveBlog: {type: Boolean, default: false},
tags_v2: [String],
upload_type: {type: String},
job_id: {type: String},
video_v2: {type: String},
local_filename: {type: String},
fromMobile: {type: Boolean, default: false},
isReel: {type: Boolean, default: false},
app: {type: String},
width: {type: Number, default: null, required: false},
height: {type: Number, default: null, required: false},
isAudio: {type: Boolean, default: false},
jsonMetaDataAppName: {type: String, default: null, required: false},
});
const SubscriptionSchema = new mongoose.Schema({
userId: {type: String, required: true},
channel: {type: String, required: true},
followed_since: {type: Date, required: true, default: Date.now()},
notifications: {type: Boolean, required: true, default: true}
});
const BlogSchema = new mongoose.Schema({
channel: {type: String, required: true},
permlink: String,
status: {
type: String,
enum: ["draft", "published", "deleted"],
required: true,
default: "draft"
},
title: {
type: String
},
body: {
type: String,
},
category: {
type: String,
enum: [
"social",
"announcement",
"general",
"gaming",
"politics",
"other"
],
required: true,
default: "general"
},
created_at: {
type: Date,
required: true,
default: Date.now()
},
thumbnail: {
type: String,
required: false,
default: null
}
});
const ViewSchema = new mongoose.Schema({
author: {type: String, required: true},
permlink: {type: String, required: true},
userIP: {type: String, required: true},
userAgent: {type: String},
timestamp: {type: Date, required: true, default: new Date()}
});
const BalanceSchema = new mongoose.Schema({
userId: {type: String, required: true, unique: true},
balance: {type: Number, required: true, default: 0},
created: Date
});
const TransactionSchema = new mongoose.Schema({
from: {type: String, required: true},
to: {type: String, required: true},
permlink: String,
amount: {type: Number, required: true, default: 0},
memo: String,
timestamp: {
type: Date, required: true, default: new Date
},
type: {
type: String,
enum: ["issue", "transfer", "credit", "payout"],
required: true
}
});
const LiveStreamSchema = new mongoose.Schema({
channel: {type: String, required: true, unique: true},
streamkey: {type: String, required: true, unique: true},
title: {type: String, required: true},
poster: {type: String, required: true, default: 'https://v--03-eu-west.3speakcontent.online/static/live.png'},
offline: {type: String, required: true, default: 'https://v--03-eu-west.3speakcontent.online/static/offline.jpg'},
tier: {
type: Number,
required: true,
enum: [1, 2, 3],
default: 1
},
is247: {type: Boolean, required: true, default: false}
});
const LanguageSchema = new mongoose.Schema({
code: {type: String, required: true},
language: {type: String, required: true}
});
const GuideLinesAcceptSchema = new mongoose.Schema({
username: {type: String, required: true},
timestamp: {type: Date, required: true, default: Date.now()}
});
const LiveViewSchema = new mongoose.Schema({
channel: {type: String, required: true},
userIP: {type: String, required: true},
userAgent: {type: String},
timestamp: {type: Date, required: true, default: new Date()}
});
const InboxVerificationSchema = new mongoose.Schema({
spkUser: {type: String},
username: {type: String},
verifyId: {type: String},
platform: {type: String},
sent: {type: Boolean, required: true, default: false}
});
const UserSchema = {
user_id: {type: String, required: true, unique: true},
banned: {type: Boolean, required: true, default: false},
self_deleted: {type: Boolean, required: false, default: false},
email: {type: String, required: true, unique: true},
last_identity: mongoose.ObjectId,
display_name: String //fallback for non blockchain user
}
const MobileUserSchema = {
user_id: {type: String, required: true, unique: true},
network: {type: String, required: true, default: 'hive'},
banned: {type: Boolean, required: true, default: false},
self_deleted: {type: Boolean, required: false, default: false},
}
const MobileUserPushTokenSchema = {
user_id: {type: String, required: true},
token: {type: String, required: true},
network: {type: String, required: true, default: 'hive'},
}
const MobileUserNotifySchema = {
user_id: {type: String, required: true},
video_id: {type: String, required: true},
notified: {type: Boolean, required: true, default: false},
}
const HiveAccountSchema = {
account: {type: String, required: true},
user_id: {type: mongoose.ObjectId, required: true},
}
const HiveAccountChallengeSchema = {
account: {type: String, required: true},
user_id: {type: String, required: true},
challenge: {type: String, required: true, unique: true},
key: {type: String, required: true, default: "posting", enum: ["posting", "active"]}
}
const ChatBotTokenSchema = {
account: {type: String, required: true, unique: true},
token: {type: String, required: true}
}
const VideoBoostSchema = new mongoose.Schema({
user_id: {type: String},
permlink: String,
order_id: {type: mongoose.ObjectId},
boost: Number
});
const PodcastSchema = new mongoose.Schema({
podcast_title: String,
podcast_owner: String,
podcast_description: String,
podcast_image: String,
podcast_categories: Array,
podcast_languages: Array
});
const VideoBoost = mongoose.model("VideoBoost", VideoBoostSchema);
const ChatBotToken = mongoose.model("ChatBotToken", ChatBotTokenSchema);
const HiveAccountChallenge = mongoose.model("HiveAccountChallenge", HiveAccountChallengeSchema);
const HiveAccount = mongoose.model("HiveAccount", HiveAccountSchema);
const User = mongoose.model("User", UserSchema);
const MobileUser = mongoose.model("MobileUser", MobileUserSchema);
const MobileUserPushToken = mongoose.model("MobileUserPushToken", MobileUserPushTokenSchema);
const MobileUserNotify = mongoose.model("MobileUserNotify", MobileUserNotifySchema);
const LiveStreamKey = mongoose.model("LiveStreamKey", LiveStreamKeySchema);
const Balance = mongoose.model("Balance", BalanceSchema);
const Transaction = mongoose.model("Transaction", TransactionSchema);
const Community = mongoose.model("Community", CommunitySchema);
const HiveCommunity = mongoose.model("HiveCommunity", HiveCommunitySchema);
const CommunityMember = mongoose.model("CommunityMember", CommunityMemberSchema);
const ContentCategory = mongoose.model("ContentCategory", ContentCategorySchema);
const ContentCreator = mongoose.model("ContentCreator", ContentCreatorSchema);
const ContentCreatorPayment = mongoose.model("ContentCreatorPayment", ContentCreatorPaymentSchema);
const Video = mongoose.model("Video", VideoSchema);
const Blog = mongoose.model("Blog", BlogSchema);
const Subscription = mongoose.model("Subscription", SubscriptionSchema);
const View = mongoose.model('View', ViewSchema);
const Language = mongoose.model("Language", LanguageSchema);
const Livestream = mongoose.model("Livestream", LiveStreamSchema);
const GuideLinesAccept = mongoose.model("GuideLinesAccept", GuideLinesAcceptSchema);
const LiveView = mongoose.model('LiveView', LiveViewSchema);
const InboxVerification = mongoose.model('InboxVerification', InboxVerificationSchema);
const Donation = mongoose.model('Donation', DonationSchema);
const DonationAccountTypes = mongoose.model('DonationAccountTypes', DonationAccountTypesSchema)
const Podcast = mongoose.model('Podcast', PodcastSchema)
async function updateBalance(user) {
const tx = await Transaction.aggregate([
{
$match: {
$or: [{
to: user
},
{from: user}
]
}
},
{
$sort: {
timestamp: -1
}
}
]);
let amount = 0;
for (let i in tx) {
switch (tx[i].type) {
case 'issue':
if (user !== "oauth2|Steemconnect|threespeak") {
amount += parseFloat(tx[i].amount)
}
break;
case 'credit':
amount += parseFloat(tx[i].amount)
break;
case 'payout':
amount -= parseFloat(tx[i].amount)
break;
case 'transfer':
if (tx[i].to === user) {
amount += parseFloat(tx[i].amount)
} else {
amount -= parseFloat(tx[i].amount)
}
break;
}
}
let balance = await Balance.findOne({userId: user});
if (balance === null) {
let balance = new Balance();
balance.userId = user;
balance.balance = amount;
balance.created = new Date();
await balance.save();
} else {
await Balance.updateOne({userId: user}, {balance: amount});
}
console.log("> Update Balance for", user, "to", amount);
return amount;
}
async function getIdentities(user_id) {
return HiveAccount.find({user_id})
}
async function isUsernameAvailable(username) {
return true;
}
export default {
GuideLinesAccept,
Balance,
Transaction,
View,
Community,
HiveCommunity,
CommunityMember,
ContentCategory,
ContentCreator,
ContentCreatorPayment,
Subscription,
Livestream,
Language,
Video,
LiveView,
Blog,
InboxVerification,
User,
MobileUser,
MobileUserPushToken,
MobileUserNotify,
HiveAccount,
HiveAccountChallenge,
updateBalance,
getIdentities,
ChatBotToken,
VideoBoost,
Donation,
DonationAccountTypes,
Podcast,
};