refactor some methods that will be used

pull/1/head
ansuz 5 years ago
parent b585dd998d
commit 75f1f8c40b

@ -278,6 +278,24 @@ var getMetadata = function (Env, channel, cb) {
}); });
}; };
// E_NO_OWNERS
var hasOwners = function (metadata) {
return Boolean(metadata && Array.isArray(metadata.owners));
};
var hasPendingOwners = function (metadata) {
return Boolean(metadata && Array.isArray(metadata.pending_owners));
};
// INSUFFICIENT_PERMISSIONS
var isOwner = function (metadata, unsafeKey) {
return metadata.owners.indexOf(unsafeKey) !== -1;
};
var isPendingOwner = function (metadata, unsafeKey) {
return metadata.pending_owners.indexOf(unsafeKey) !== -1;
};
/* setMetadata /* setMetadata
- write a new line to the metadata log if a valid command is provided - write a new line to the metadata log if a valid command is provided
- data is an object: { - data is an object: {
@ -300,16 +318,16 @@ var setMetadata = function (Env, data, unsafeKey, cb) {
cb(err); cb(err);
return void next(); return void next();
} }
if (!(metadata && Array.isArray(metadata.owners))) { if (!hasOwners(metadata)) {
cb('E_NO_OWNERS'); cb('E_NO_OWNERS');
return void next(); return void next();
} }
// Confirm that the channel is owned by the user in question // Confirm that the channel is owned by the user in question
// or the user is accepting a pending ownerhsip offer // or the user is accepting a pending ownership offer
if (metadata.pending_owners && Array.isArray(metadata.pending_owners) && if (hasPendingOwners(metadata) &&
metadata.pending_owners.indexOf(unsafeKey) !== -1 && isPendingOwner(metadata, unsafeKey) &&
metadata.owners.indexOf(unsafeKey) === -1) { !isOwner(metadata, unsafeKey)) {
// If you are a pending owner, make sure you can only add yourelf as an owner // If you are a pending owner, make sure you can only add yourelf as an owner
if ((command !== 'ADD_OWNERS' && command !== 'RM_PENDING_OWNERS') if ((command !== 'ADD_OWNERS' && command !== 'RM_PENDING_OWNERS')
@ -319,8 +337,8 @@ var setMetadata = function (Env, data, unsafeKey, cb) {
cb('INSUFFICIENT_PERMISSIONS'); cb('INSUFFICIENT_PERMISSIONS');
return void next(); return void next();
} }
// XXX wacky fallthrough is hard to read
} else if (metadata.owners.indexOf(unsafeKey) === -1) { } else if (!isOwner(metadata, unsafeKey)) {
cb('INSUFFICIENT_PERMISSIONS'); cb('INSUFFICIENT_PERMISSIONS');
return void next(); return void next();
} }
@ -817,12 +835,11 @@ var clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
getMetadata(Env, channelId, function (err, metadata) { getMetadata(Env, channelId, function (err, metadata) {
if (err) { return void cb(err); } if (err) { return void cb(err); }
if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); } if (!hasOwners(metadata)) { return void cb('E_NO_OWNERS'); }
// Confirm that the channel is owned by the user in question // Confirm that the channel is owned by the user in question
if (metadata.owners.indexOf(unsafeKey) === -1) { if (!isOwner(metadata, unsafeKey)) {
return void cb('INSUFFICIENT_PERMISSIONS'); return void cb('INSUFFICIENT_PERMISSIONS');
} }
// FIXME COLDSTORAGE
return void Env.msgStore.clearChannel(channelId, function (e) { return void Env.msgStore.clearChannel(channelId, function (e) {
cb(e); cb(e);
}); });
@ -905,8 +922,8 @@ var removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
getMetadata(Env, channelId, function (err, metadata) { getMetadata(Env, channelId, function (err, metadata) {
if (err) { return void cb(err); } if (err) { return void cb(err); }
if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); } if (!hasOwners(metadata)) { return void cb('E_NO_OWNERS'); }
if (metadata.owners.indexOf(unsafeKey) === -1) { if (!isOwner(metadata, unsafeKey)) {
return void cb('INSUFFICIENT_PERMISSIONS'); return void cb('INSUFFICIENT_PERMISSIONS');
} }
// if the admin has configured data retention... // if the admin has configured data retention...

Loading…
Cancel
Save