2018-04-29 20:33:27 +00:00
|
|
|
const map = require('lodash/map');
|
2018-04-27 15:13:18 +00:00
|
|
|
const retry = require('async-retry');
|
2018-04-28 07:24:53 +00:00
|
|
|
const generate = require('nanoid/generate');
|
2018-04-27 15:13:18 +00:00
|
|
|
const markdown = require('markdown').markdown;
|
2018-05-02 21:01:29 +00:00
|
|
|
const async = require('async');
|
2018-05-02 18:07:09 +00:00
|
|
|
const ExpirationUnits = require('../expirationunits');
|
2018-03-22 15:45:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Code
|
|
|
|
{
|
|
|
|
constructor(values)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.id = values.id || values._id || null;
|
|
|
|
self.userId = values.userId || null;
|
|
|
|
self.created = values.created || new Date();
|
2018-08-16 13:18:02 +00:00
|
|
|
self.firstUpload = values.firstUpload || null;
|
2018-03-22 15:45:11 +00:00
|
|
|
self.expiration = values.expiration || null;
|
2018-05-02 18:07:09 +00:00
|
|
|
self.expirationDate = values.expirationDate || null;
|
2018-04-27 15:13:18 +00:00
|
|
|
self.description = values.description || null;
|
|
|
|
self.message = values.message || null;
|
|
|
|
self.messageHTML = values.messageHTML || null;
|
2018-05-13 07:33:10 +00:00
|
|
|
self.history = values.history || [{
|
|
|
|
userId: self.userId,
|
|
|
|
date: self.created
|
|
|
|
}];
|
2018-03-22 15:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CodeRepository
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
constructor(store)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
self.store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
init()
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
2018-04-27 15:13:18 +00:00
|
|
|
resolve();
|
2018-03-21 15:51:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
async insert(code)
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return await retry(async bail =>
|
|
|
|
{
|
2018-04-28 07:24:53 +00:00
|
|
|
var codeId = generate(config.code.alphabet, config.code.length);
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-05-02 18:07:09 +00:00
|
|
|
if ((await self.get(codeId)) !== null)
|
2018-04-27 15:13:18 +00:00
|
|
|
throw new Error('Code ' + codeId + ' already exists');
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-05-13 07:33:10 +00:00
|
|
|
let now = new Date();
|
|
|
|
|
2018-03-21 15:51:46 +00:00
|
|
|
self.store.insert({
|
2018-04-27 15:13:18 +00:00
|
|
|
_id: codeId,
|
|
|
|
userId: code.userId,
|
2018-05-13 07:33:10 +00:00
|
|
|
created: code.created || now,
|
2018-08-16 13:18:02 +00:00
|
|
|
firstUpload: code.firstUpload,
|
2018-04-27 15:13:18 +00:00
|
|
|
expiration: code.expiration,
|
2018-08-16 13:18:02 +00:00
|
|
|
expirationDate: code.expirationDate,
|
2018-04-27 15:13:18 +00:00
|
|
|
description: code.description,
|
|
|
|
message: code.message,
|
2018-05-13 07:33:10 +00:00
|
|
|
messageHTML: self.getMessageHTML(code.message),
|
|
|
|
history: [
|
|
|
|
{
|
|
|
|
userId: code.userId,
|
|
|
|
date: code.created || now
|
|
|
|
}
|
|
|
|
]
|
2018-03-21 15:51:46 +00:00
|
|
|
})
|
|
|
|
|
2018-04-27 15:13:18 +00:00
|
|
|
return codeId;
|
2018-03-21 15:51:46 +00:00
|
|
|
}, {
|
|
|
|
retries: 100,
|
|
|
|
minTimeout: 0,
|
|
|
|
maxTimeout: 0
|
|
|
|
});
|
|
|
|
}
|
2018-03-22 15:45:11 +00:00
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
update(code)
|
2018-04-27 15:13:18 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.update({ _id: code.id }, { $set: {
|
|
|
|
expiration: code.expiration,
|
2018-08-16 13:18:02 +00:00
|
|
|
firstUpload: code.firstUpload,
|
|
|
|
expirationDate: code.firstUpload != null && code.expiration != null ? ExpirationUnits.apply(code.expiration, code.firstUpload) : null,
|
2018-04-27 15:13:18 +00:00
|
|
|
description: code.description,
|
|
|
|
message: code.message,
|
|
|
|
messageHTML: self.getMessageHTML(code.message)
|
|
|
|
}},
|
|
|
|
(err, numAffected) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numAffected == 0)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-16 13:18:02 +00:00
|
|
|
getUploadExpirationDate(codeId)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.findOne({ _id: codeId }, (err, doc) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc !== null)
|
|
|
|
{
|
|
|
|
if (doc.firstUpload == null)
|
|
|
|
{
|
|
|
|
let now = new Date();
|
|
|
|
let expirationDate = doc.expiration != null ? ExpirationUnits.apply(doc.expiration, now) : null;
|
|
|
|
|
|
|
|
self.store.update({ _id: codeId }, { $set: {
|
|
|
|
firstUpload: now,
|
|
|
|
expirationDate: expirationDate
|
|
|
|
}},
|
|
|
|
(err, numAffected) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numAffected == 0)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(expirationDate);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resolve(doc.expirationDate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-13 07:33:10 +00:00
|
|
|
move(codeId, userId)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.update({ _id: codeId }, {
|
|
|
|
$set: {
|
|
|
|
userId: userId
|
|
|
|
},
|
|
|
|
$push: {
|
|
|
|
history: {
|
|
|
|
userId: userId,
|
|
|
|
date: new Date()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err, numAffected) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numAffected == 0)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
list(userId)
|
2018-03-22 15:45:11 +00:00
|
|
|
{
|
2018-04-26 12:08:08 +00:00
|
|
|
var self = this;
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
2018-04-26 12:08:08 +00:00
|
|
|
self.store.find(userId != null ? { userId: userId } : {}, (err, docs) =>
|
2018-03-22 15:45:11 +00:00
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-29 20:33:27 +00:00
|
|
|
resolve(map(docs, (doc) => new Code(doc)));
|
2018-03-22 15:45:11 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-04-27 15:13:18 +00:00
|
|
|
|
|
|
|
|
2018-04-28 07:24:53 +00:00
|
|
|
getDescriptions()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.find({}, (err, docs) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var descriptions = {};
|
|
|
|
docs.forEach((dbCode) =>
|
|
|
|
{
|
|
|
|
if (dbCode.description)
|
|
|
|
descriptions[dbCode._id] = dbCode.description;
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(descriptions);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
get(codeId)
|
2018-04-27 15:13:18 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.findOne({ _id: codeId }, (err, doc) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(doc !== null ? new Code(doc) : null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getMessageHTML(message)
|
|
|
|
{
|
|
|
|
return message !== null ? markdown.toHTML(message) : null;
|
|
|
|
}
|
2018-04-28 07:51:58 +00:00
|
|
|
|
|
|
|
|
2018-05-13 07:33:10 +00:00
|
|
|
delete(codeId)
|
2018-04-28 07:51:58 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
2018-05-13 07:33:10 +00:00
|
|
|
self.store.remove({ _id: codeId }, (err, numRemoved) =>
|
2018-04-28 07:51:58 +00:00
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-05-02 21:01:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
deleteExpired()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
let now = new Date();
|
|
|
|
|
|
|
|
self.store.find({ $where: function() { return this.expirationDate !== null && this.expirationDate < now }}, (err, docs) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async.eachOfSeries(docs,
|
|
|
|
async (doc) =>
|
|
|
|
{
|
|
|
|
console.log('Expired code: ' + doc._id);
|
|
|
|
await self.delete(doc._id);
|
|
|
|
},
|
|
|
|
(err) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
|
|
|
else
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-03-21 15:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
module.exports = {
|
|
|
|
Code,
|
|
|
|
CodeRepository
|
|
|
|
}
|