2017-06-19 18:37:35 +02:00
|
|
|
const db = require('../models');
|
2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-12-07 21:23:21 +01:00
|
|
|
const { returnPaginatedChannelViewData } = require('../helpers/channelPagination.js');
|
2017-09-08 00:31:32 +02:00
|
|
|
|
2017-10-18 00:09:43 +02:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2017-12-06 02:08:34 +01:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
2017-11-27 19:22:47 +01:00
|
|
|
const NO_FILE = 'NO_FILE';
|
2017-08-02 22:16:39 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-12-05 19:18:49 +01:00
|
|
|
getClaimId (channelName, channelClaimId, name, claimId) {
|
2017-11-21 21:53:43 +01:00
|
|
|
if (channelName) {
|
2017-12-05 19:18:49 +01:00
|
|
|
return module.exports.getClaimIdByChannel(channelName, channelClaimId, name);
|
2017-11-21 21:53:43 +01:00
|
|
|
} else {
|
|
|
|
return module.exports.getClaimIdByClaim(name, claimId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getClaimIdByClaim (claimName, claimId) {
|
|
|
|
logger.debug(`getClaimIdByClaim(${claimName}, ${claimId})`);
|
2017-08-02 03:58:13 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-06 02:08:34 +01:00
|
|
|
db.Claim.getLongClaimId(claimName, claimId)
|
|
|
|
.then(longClaimId => {
|
|
|
|
if (!longClaimId) {
|
|
|
|
resolve(NO_CLAIM);
|
|
|
|
}
|
|
|
|
resolve(longClaimId);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-12-05 19:18:49 +01:00
|
|
|
getClaimIdByChannel (channelName, channelClaimId, claimName) {
|
|
|
|
logger.debug(`getClaimIdByChannel(${channelName}, ${channelClaimId}, ${claimName})`);
|
2017-08-23 00:50:20 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-05 19:18:49 +01:00
|
|
|
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel id
|
2017-12-06 02:08:34 +01:00
|
|
|
.then(longChannelId => {
|
|
|
|
if (!longChannelId) {
|
|
|
|
return [null, null];
|
2017-10-18 00:50:35 +02:00
|
|
|
}
|
2017-12-06 02:08:34 +01:00
|
|
|
return Promise.all([longChannelId, db.Claim.getClaimIdByLongChannelId(longChannelId, claimName)]); // 2. get the long claim id
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-12-06 02:08:34 +01:00
|
|
|
.then(([longChannelId, longClaimId]) => {
|
|
|
|
if (!longChannelId) {
|
|
|
|
return resolve(NO_CHANNEL);
|
|
|
|
}
|
|
|
|
if (!longClaimId) {
|
|
|
|
return resolve(NO_CLAIM);
|
|
|
|
}
|
|
|
|
resolve(longClaimId);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-24 01:01:28 +02:00
|
|
|
});
|
|
|
|
},
|
2017-12-07 21:23:21 +01:00
|
|
|
getChannelViewData (channelName, channelClaimId, query) {
|
2017-08-24 01:01:28 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-06 20:53:31 +01:00
|
|
|
// 1. get the long channel Id (make sure channel exists)
|
|
|
|
db.Certificate.getLongChannelId(channelName, channelClaimId)
|
|
|
|
.then(longChannelClaimId => {
|
2017-12-06 02:08:34 +01:00
|
|
|
if (!longChannelClaimId) {
|
|
|
|
return [null, null, null];
|
2017-10-18 00:09:43 +02:00
|
|
|
}
|
2017-12-06 20:53:31 +01:00
|
|
|
// 2. get the short ID and all claims for that channel
|
2017-12-06 02:08:34 +01:00
|
|
|
return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName), db.Claim.getAllChannelClaims(longChannelClaimId)]);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-12-06 20:53:31 +01:00
|
|
|
.then(([longChannelClaimId, shortChannelClaimId, channelClaimsArray]) => {
|
2017-12-06 02:08:34 +01:00
|
|
|
if (!longChannelClaimId) {
|
|
|
|
return resolve(NO_CHANNEL);
|
2017-10-18 00:09:43 +02:00
|
|
|
}
|
2017-12-07 21:23:21 +01:00
|
|
|
// 3. format the data for the view, including pagination
|
|
|
|
let paginatedChannelViewData = returnPaginatedChannelViewData(channelName, longChannelClaimId, shortChannelClaimId, channelClaimsArray, query);
|
|
|
|
// 4. return all the channel information and contents
|
|
|
|
resolve(paginatedChannelViewData);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-23 00:50:20 +02:00
|
|
|
});
|
|
|
|
},
|
2017-11-21 21:53:43 +01:00
|
|
|
getLocalFileRecord (claimId, name) {
|
|
|
|
return db.File.findOne({where: {claimId, name}})
|
|
|
|
.then(file => {
|
|
|
|
if (!file) {
|
2017-11-27 19:22:47 +01:00
|
|
|
return NO_FILE;
|
2017-11-21 21:53:43 +01:00
|
|
|
}
|
|
|
|
return file.dataValues;
|
|
|
|
});
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|