Remove 'Contact' from 'Contact.*' properties, clean->parse
This commit is contained in:
parent
cda326ca45
commit
26be658892
3 changed files with 24 additions and 38 deletions
|
@ -8,7 +8,7 @@ const DEFAULT_EMAIL_TYPE = SignalService.DataMessage.Contact.Email.Type.HOME;
|
||||||
const DEFAULT_ADDRESS_TYPE =
|
const DEFAULT_ADDRESS_TYPE =
|
||||||
SignalService.DataMessage.Contact.PostalAddress.Type.HOME;
|
SignalService.DataMessage.Contact.PostalAddress.Type.HOME;
|
||||||
|
|
||||||
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
exports.parseAndWriteAvatar = upgradeAttachment => async (
|
||||||
contact,
|
contact,
|
||||||
context = {}
|
context = {}
|
||||||
) => {
|
) => {
|
||||||
|
@ -24,19 +24,19 @@ exports.parseAndWriteContactAvatar = upgradeAttachment => async (
|
||||||
: omit(contact, ['avatar']);
|
: omit(contact, ['avatar']);
|
||||||
|
|
||||||
// eliminates empty numbers, emails, and addresses; adds type if not provided
|
// 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),
|
messageId: idForLogging(message),
|
||||||
});
|
});
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log(
|
console.log(
|
||||||
'Contact.parseAndWriteContactAvatar: contact was malformed.',
|
'Contact.parseAndWriteAvatar: contact was malformed.',
|
||||||
toLogFormat(error)
|
toLogFormat(error)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return contactWithCleanedElements;
|
return parsedContact;
|
||||||
};
|
};
|
||||||
|
|
||||||
function parseContact(contact) {
|
function parseContact(contact) {
|
||||||
|
@ -44,9 +44,9 @@ 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, cleanPhoneItem))),
|
createArrayKey('number', compact(map(contact.number, parsePhoneItem))),
|
||||||
createArrayKey('email', compact(map(contact.email, cleanEmailItem))),
|
createArrayKey('email', compact(map(contact.email, parseEmailItem))),
|
||||||
createArrayKey('address', compact(map(contact.address, cleanAddress)))
|
createArrayKey('address', compact(map(contact.address, parseAddress)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ function idForLogging(message) {
|
||||||
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
|
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports._validateContact = (contact, options = {}) => {
|
exports._validate = (contact, options = {}) => {
|
||||||
const { messageId } = options;
|
const { messageId } = options;
|
||||||
const { name, number, email, address, organization } = contact;
|
const { name, number, email, address, organization } = contact;
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ exports._validateContact = (contact, options = {}) => {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function cleanPhoneItem(item) {
|
function parsePhoneItem(item) {
|
||||||
if (!item.value) {
|
if (!item.value) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ function cleanPhoneItem(item) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanEmailItem(item) {
|
function parseEmailItem(item) {
|
||||||
if (!item.value) {
|
if (!item.value) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -97,7 +97,7 @@ function cleanEmailItem(item) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanAddress(address) {
|
function parseAddress(address) {
|
||||||
if (!address) {
|
if (!address) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -234,7 +234,7 @@ const toVersion5 = exports._withSchemaVersion(5, initializeAttachmentMetadata);
|
||||||
const toVersion6 = exports._withSchemaVersion(
|
const toVersion6 = exports._withSchemaVersion(
|
||||||
6,
|
6,
|
||||||
exports._mapContact(
|
exports._mapContact(
|
||||||
Contact.parseAndWriteContactAvatar(Attachment.migrateDataToFileSystem)
|
Contact.parseAndWriteAvatar(Attachment.migrateDataToFileSystem)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -9,14 +9,12 @@ const {
|
||||||
describe('Contact', () => {
|
describe('Contact', () => {
|
||||||
const NUMBER = '+12025550099';
|
const NUMBER = '+12025550099';
|
||||||
|
|
||||||
describe('parseAndWriteContactAvatar', () => {
|
describe('parseAndWriteAvatar', () => {
|
||||||
it('handles message with no avatar in contact', async () => {
|
it('handles message with no avatar in contact', async () => {
|
||||||
const upgradeAttachment = sinon
|
const upgradeAttachment = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.throws(new Error("Shouldn't be called"));
|
.throws(new Error("Shouldn't be called"));
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
body: 'hey there!',
|
body: 'hey there!',
|
||||||
|
@ -42,9 +40,7 @@ describe('Contact', () => {
|
||||||
const upgradeAttachment = sinon
|
const upgradeAttachment = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.throws(new Error("Shouldn't be called"));
|
.throws(new Error("Shouldn't be called"));
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
body: 'hey there!',
|
body: 'hey there!',
|
||||||
|
@ -86,9 +82,7 @@ describe('Contact', () => {
|
||||||
path: 'abc/abcdefg',
|
path: 'abc/abcdefg',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
body: 'hey there!',
|
body: 'hey there!',
|
||||||
|
@ -164,9 +158,7 @@ describe('Contact', () => {
|
||||||
const upgradeAttachment = sinon
|
const upgradeAttachment = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.throws(new Error("Shouldn't be called"));
|
.throws(new Error("Shouldn't be called"));
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
body: 'hey there!',
|
body: 'hey there!',
|
||||||
|
@ -207,9 +199,7 @@ describe('Contact', () => {
|
||||||
const upgradeAttachment = sinon
|
const upgradeAttachment = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.throws(new Error("Shouldn't be called"));
|
.throws(new Error("Shouldn't be called"));
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
body: 'hey there!',
|
body: 'hey there!',
|
||||||
|
@ -250,9 +240,7 @@ describe('Contact', () => {
|
||||||
const upgradeAttachment = sinon
|
const upgradeAttachment = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.throws(new Error("Shouldn't be called"));
|
.throws(new Error("Shouldn't be called"));
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
body: 'hey there!',
|
body: 'hey there!',
|
||||||
|
@ -290,9 +278,7 @@ describe('Contact', () => {
|
||||||
const upgradeAttachment = sinon
|
const upgradeAttachment = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.throws(new Error("Shouldn't be called"));
|
.throws(new Error("Shouldn't be called"));
|
||||||
const upgradeVersion = Contact.parseAndWriteContactAvatar(
|
const upgradeVersion = Contact.parseAndWriteAvatar(upgradeAttachment);
|
||||||
upgradeAttachment
|
|
||||||
);
|
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
contact: [
|
contact: [
|
||||||
|
@ -312,7 +298,7 @@ describe('Contact', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_validateContact', () => {
|
describe('_validate', () => {
|
||||||
it('returns error if contact has no name.displayName or organization', () => {
|
it('returns error if contact has no name.displayName or organization', () => {
|
||||||
const messageId = 'the-message-id';
|
const messageId = 'the-message-id';
|
||||||
const contact = {
|
const contact = {
|
||||||
|
@ -329,7 +315,7 @@ describe('Contact', () => {
|
||||||
const expected =
|
const expected =
|
||||||
"Message the-message-id: Contact had neither 'displayName' nor 'organization'";
|
"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);
|
assert.deepEqual(result.message, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -345,7 +331,7 @@ describe('Contact', () => {
|
||||||
const expected =
|
const expected =
|
||||||
'Message the-message-id: Contact had no included numbers, email or addresses';
|
'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);
|
assert.deepEqual(result.message, expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue