2017-10-31 12:15:49 +08:00
|
|
|
const utils = require('./utils');
|
2017-11-07 08:23:35 +08:00
|
|
|
const log = require('./log');
|
2017-11-17 10:50:00 +08:00
|
|
|
const sql = require('./sql');
|
2017-10-31 12:15:49 +08:00
|
|
|
|
2017-12-24 02:55:13 +08:00
|
|
|
async function saveSourceId(sourceId) {
|
2017-12-17 09:48:34 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.insert("source_ids", {
|
|
|
|
source_id: sourceId,
|
|
|
|
date_created: utils.nowDate()
|
2017-11-17 11:18:25 +08:00
|
|
|
});
|
2017-12-17 09:48:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await refreshSourceIds();
|
2017-12-24 02:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createSourceId() {
|
|
|
|
const sourceId = utils.randomString(12);
|
|
|
|
|
|
|
|
log.info("Generated sourceId=" + sourceId);
|
|
|
|
return sourceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateSourceId() {
|
|
|
|
const sourceId = createSourceId();
|
|
|
|
|
|
|
|
await saveSourceId(sourceId);
|
2017-12-17 09:48:34 +08:00
|
|
|
|
|
|
|
return sourceId;
|
|
|
|
}
|
2017-11-17 10:50:00 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
async function refreshSourceIds() {
|
2017-12-24 00:02:38 +08:00
|
|
|
allSourceIds = await sql.getFirstColumn("SELECT source_id FROM source_ids ORDER BY date_created DESC");
|
2017-12-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let allSourceIds = [];
|
|
|
|
|
2017-11-17 10:50:00 +08:00
|
|
|
function isLocalSourceId(srcId) {
|
|
|
|
return allSourceIds.includes(srcId);
|
|
|
|
}
|
|
|
|
|
2017-12-24 02:55:13 +08:00
|
|
|
const currentSourceId = createSourceId();
|
|
|
|
|
|
|
|
// this will also refresh source IDs
|
|
|
|
sql.dbReady.then(() => saveSourceId(currentSourceId));
|
2017-12-17 10:23:35 +08:00
|
|
|
|
2017-12-24 02:55:13 +08:00
|
|
|
function getCurrentSourceId() {
|
|
|
|
return currentSourceId;
|
2017-12-17 10:23:35 +08:00
|
|
|
}
|
2017-12-17 09:48:34 +08:00
|
|
|
|
2017-11-17 10:50:00 +08:00
|
|
|
module.exports = {
|
2017-12-17 09:48:34 +08:00
|
|
|
generateSourceId,
|
2017-12-17 10:23:35 +08:00
|
|
|
getCurrentSourceId,
|
2017-11-17 10:50:00 +08:00
|
|
|
isLocalSourceId
|
|
|
|
};
|