Use protobuf enum values to eliminate magic numbers
This commit is contained in:
parent
adbe989949
commit
cda326ca45
1 changed files with 21 additions and 5 deletions
|
@ -1,6 +1,12 @@
|
||||||
const { omit, compact, map } = require('lodash');
|
const { omit, compact, map } = require('lodash');
|
||||||
|
|
||||||
const { toLogFormat } = require('./errors');
|
const { toLogFormat } = require('./errors');
|
||||||
|
const { SignalService } = require('../../../ts/protobuf');
|
||||||
|
|
||||||
|
const DEFAULT_PHONE_TYPE = SignalService.DataMessage.Contact.Phone.Type.HOME;
|
||||||
|
const DEFAULT_EMAIL_TYPE = SignalService.DataMessage.Contact.Email.Type.HOME;
|
||||||
|
const DEFAULT_ADDRESS_TYPE =
|
||||||
|
SignalService.DataMessage.Contact.PostalAddress.Type.HOME;
|
||||||
|
|
||||||
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
||||||
contact,
|
contact,
|
||||||
|
@ -38,8 +44,8 @@ function parseContact(contact) {
|
||||||
{},
|
{},
|
||||||
omit(contact, ['avatar', 'number', 'email', 'address']),
|
omit(contact, ['avatar', 'number', 'email', 'address']),
|
||||||
parseAvatar(contact.avatar),
|
parseAvatar(contact.avatar),
|
||||||
createArrayKey('number', compact(map(contact.number, cleanBasicItem))),
|
createArrayKey('number', compact(map(contact.number, cleanPhoneItem))),
|
||||||
createArrayKey('email', compact(map(contact.email, cleanBasicItem))),
|
createArrayKey('email', compact(map(contact.email, cleanEmailItem))),
|
||||||
createArrayKey('address', compact(map(contact.address, cleanAddress)))
|
createArrayKey('address', compact(map(contact.address, cleanAddress)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -71,13 +77,23 @@ exports._validateContact = (contact, options = {}) => {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function cleanBasicItem(item) {
|
function cleanPhoneItem(item) {
|
||||||
if (!item.value) {
|
if (!item.value) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.assign({}, item, {
|
return Object.assign({}, item, {
|
||||||
type: item.type || 1,
|
type: item.type || DEFAULT_PHONE_TYPE,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanEmailItem(item) {
|
||||||
|
if (!item.value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign({}, item, {
|
||||||
|
type: item.type || DEFAULT_EMAIL_TYPE,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +115,7 @@ function cleanAddress(address) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.assign({}, address, {
|
return Object.assign({}, address, {
|
||||||
type: address.type || 1,
|
type: address.type || DEFAULT_ADDRESS_TYPE,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue