Remove 'Contact' from 'Contact.*' properties, clean->parse

This commit is contained in:
Scott Nonnenberg 2018-05-08 13:12:11 -07:00
parent cda326ca45
commit 26be658892
3 changed files with 24 additions and 38 deletions

View File

@ -8,7 +8,7 @@ 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.parseAndWriteAvatar = upgradeAttachment => async (
contact,
context = {}
) => {
@ -24,19 +24,19 @@ exports.parseAndWriteContactAvatar = upgradeAttachment => async (
: omit(contact, ['avatar']);
// eliminates empty numbers, emails, and addresses; adds type if not provided
const contactWithCleanedElements = parseContact(contactWithUpdatedAvatar);
const parsedContact = parseContact(contactWithUpdatedAvatar);
const error = exports._validateContact(contactWithCleanedElements, {
const error = exports._validate(parsedContact, {
messageId: idForLogging(message),
});
if (error) {
console.log(
'Contact.parseAndWriteContactAvatar: contact was malformed.',
'Contact.parseAndWriteAvatar: contact was malformed.',
toLogFormat(error)
);
}
return contactWithCleanedElements;
return parsedContact;
};
function parseContact(contact) {
@ -44,9 +44,9 @@ function parseContact(contact) {
{},
omit(contact, ['avatar', 'number', 'email', 'address']),
parseAvatar(contact.avatar),
createArrayKey('number', compact(map(contact.number, cleanPhoneItem))),
createArrayKey('email', compact(map(contact.email, cleanEmailItem))),
createArrayKey('address', compact(map(contact.address, cleanAddress)))
createArrayKey('number', compact(map(contact.number, parsePhoneItem))),
createArrayKey('email', compact(map(contact.email, parseEmailItem))),
createArrayKey('address', compact(map(contact.address, parseAddress)))
);
}
@ -54,7 +54,7 @@ function idForLogging(message) {
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
}
exports._validateContact = (contact, options = {}) => {
exports._validate = (contact, options = {}) => {
const { messageId } = options;
const { name, number, email, address, organization } = contact;
@ -77,7 +77,7 @@ exports._validateContact = (contact, options = {}) => {
return null;
};
function cleanPhoneItem(item) {
function parsePhoneItem(item) {
if (!item.value) {
return null;
}
@ -87,7 +87,7 @@ function cleanPhoneItem(item) {
});
}
function cleanEmailItem(item) {
function parseEmailItem(item) {
if (!item.value) {
return null;
}
@ -97,7 +97,7 @@ function cleanEmailItem(item) {
});
}
function cleanAddress(address) {
function parseAddress(address) {
if (!address) {
return null;
}

View File

@ -234,7 +234,7 @@ const toVersion5 = exports._withSchemaVersion(5, initializeAttachmentMetadata);
const toVersion6 = exports._withSchemaVersion(
6,
exports._mapContact(
Contact.parseAndWriteContactAvatar(Attachment.migrateDataToFileSystem)
Contact.parseAndWriteAvatar(Attachment.migrateDataToFileSystem)
)
);

View File

@ -9,14 +9,12 @@ const {
describe('Contact', () => {
const NUMBER = '+12025550099';
describe('parseAndWriteContactAvatar', () => {
describe('parseAndWriteAvatar', () => {
it('handles message with no avatar in contact', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
body: 'hey there!',
@ -42,9 +40,7 @@ describe('Contact', () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
body: 'hey there!',
@ -86,9 +82,7 @@ describe('Contact', () => {
path: 'abc/abcdefg',
};
};
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
body: 'hey there!',
@ -164,9 +158,7 @@ describe('Contact', () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
body: 'hey there!',
@ -207,9 +199,7 @@ describe('Contact', () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
body: 'hey there!',
@ -250,9 +240,7 @@ describe('Contact', () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
body: 'hey there!',
@ -290,9 +278,7 @@ describe('Contact', () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
const message = {
contact: [
@ -312,7 +298,7 @@ describe('Contact', () => {
});
});
describe('_validateContact', () => {
describe('_validate', () => {
it('returns error if contact has no name.displayName or organization', () => {
const messageId = 'the-message-id';
const contact = {
@ -329,7 +315,7 @@ describe('Contact', () => {
const expected =
"Message the-message-id: Contact had neither 'displayName' nor 'organization'";
const result = Contact._validateContact(contact, { messageId });
const result = Contact._validate(contact, { messageId });
assert.deepEqual(result.message, expected);
});
@ -345,7 +331,7 @@ describe('Contact', () => {
const expected =
'Message the-message-id: Contact had no included numbers, email or addresses';
const result = Contact._validateContact(contact, { messageId });
const result = Contact._validate(contact, { messageId });
assert.deepEqual(result.message, expected);
});
});