Move contact migrate code from message.js to contact.js

This commit is contained in:
Scott Nonnenberg 2018-05-08 09:48:47 -07:00
parent 47adbe4358
commit 420f81ebcd
4 changed files with 480 additions and 469 deletions

121
js/modules/types/contact.js Normal file
View file

@ -0,0 +1,121 @@
const { omit, compact, map } = require('lodash');
exports.parseAndWriteContactAvatar = upgradeAttachment => async (
contact,
context = {}
) => {
const { message } = context;
const { avatar } = contact;
const contactWithUpdatedAvatar =
avatar && avatar.avatar
? Object.assign({}, contact, {
avatar: Object.assign({}, avatar, {
avatar: await upgradeAttachment(avatar.avatar, context),
}),
})
: omit(contact, ['avatar']);
// eliminates empty numbers, emails, and addresses; adds type if not provided
const contactWithCleanedElements = parseContact(contactWithUpdatedAvatar);
// We'll log if the contact is invalid, leave everything as-is
validateContact(contactWithCleanedElements, {
messageId: idForLogging(message),
});
return contactWithCleanedElements;
};
function parseContact(contact) {
return Object.assign(
{},
omit(contact, ['avatar', 'number', 'email', 'address']),
cleanAvatar(contact.avatar),
addArrayKey('number', compact(map(contact.number, cleanBasicItem))),
addArrayKey('email', compact(map(contact.email, cleanBasicItem))),
addArrayKey('address', compact(map(contact.address, cleanAddress)))
);
}
function idForLogging(message) {
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
}
function validateContact(contact, options = {}) {
const { messageId } = options;
const { name, number, email, address, organization } = contact;
if ((!name || !name.displayName) && !organization) {
console.log(
`Message ${messageId}: Contact had neither 'displayName' nor 'organization'`
);
return false;
}
if (
(!number || !number.length) &&
(!email || !email.length) &&
(!address || !address.length)
) {
console.log(
`Message ${messageId}: Contact had no included numbers, email or addresses`
);
return false;
}
return true;
}
function cleanBasicItem(item) {
if (!item.value) {
return null;
}
return Object.assign({}, item, {
type: item.type || 1,
});
}
function cleanAddress(address) {
if (!address) {
return null;
}
if (
!address.street &&
!address.pobox &&
!address.neighborhood &&
!address.city &&
!address.region &&
!address.postcode &&
!address.country
) {
return null;
}
return Object.assign({}, address, {
type: address.type || 1,
});
}
function cleanAvatar(avatar) {
if (!avatar) {
return null;
}
return {
avatar: Object.assign({}, avatar, {
isProfile: avatar.isProfile || false,
}),
};
}
function addArrayKey(key, array) {
if (!array || !array.length) {
return null;
}
return {
[key]: array,
};
}

View file

@ -1,5 +1,6 @@
const { isFunction, isString, omit, compact, map } = require('lodash');
const { isFunction, isString, omit } = require('lodash');
const Contact = require('./contact');
const Attachment = require('./attachment');
const Errors = require('./errors');
const SchemaVersion = require('./schema_version');
@ -210,126 +211,6 @@ exports._mapQuotedAttachments = upgradeAttachment => async (
});
};
function validateContact(contact, options = {}) {
const { messageId } = options;
const { name, number, email, address, organization } = contact;
if ((!name || !name.displayName) && !organization) {
console.log(
`Message ${messageId}: Contact had neither 'displayName' nor 'organization'`
);
return false;
}
if (
(!number || !number.length) &&
(!email || !email.length) &&
(!address || !address.length)
) {
console.log(
`Message ${messageId}: Contact had no included numbers, email or addresses`
);
return false;
}
return true;
}
function cleanContact(contact) {
function cleanBasicItem(item) {
if (!item.value) {
return null;
}
return Object.assign({}, item, {
type: item.type || 1,
});
}
function cleanAddress(address) {
if (!address) {
return null;
}
if (
!address.street &&
!address.pobox &&
!address.neighborhood &&
!address.city &&
!address.region &&
!address.postcode &&
!address.country
) {
return null;
}
return Object.assign({}, address, {
type: address.type || 1,
});
}
function cleanAvatar(avatar) {
if (!avatar) {
return null;
}
return {
avatar: Object.assign({}, avatar, {
isProfile: avatar.isProfile || false,
}),
};
}
function addArrayKey(key, array) {
if (!array || !array.length) {
return null;
}
return {
[key]: array,
};
}
return Object.assign(
{},
omit(contact, ['avatar', 'number', 'email', 'address']),
cleanAvatar(contact.avatar),
addArrayKey('number', compact(map(contact.number, cleanBasicItem))),
addArrayKey('email', compact(map(contact.email, cleanBasicItem))),
addArrayKey('address', compact(map(contact.address, cleanAddress)))
);
}
function idForLogging(message) {
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
}
exports._cleanAndWriteContactAvatar = upgradeAttachment => async (
contact,
context = {}
) => {
const { message } = context;
const { avatar } = contact;
const contactWithUpdatedAvatar =
avatar && avatar.avatar
? Object.assign({}, contact, {
avatar: Object.assign({}, avatar, {
avatar: await upgradeAttachment(avatar.avatar, context),
}),
})
: omit(contact, ['avatar']);
// eliminates empty numbers, emails, and addresses; adds type if not provided
const contactWithCleanedElements = cleanContact(contactWithUpdatedAvatar);
// We'll log if the contact is invalid, leave everything as-is
validateContact(contactWithCleanedElements, {
messageId: idForLogging(message),
});
return contactWithCleanedElements;
};
const toVersion0 = async message => exports.initializeSchemaVersion(message);
const toVersion1 = exports._withSchemaVersion(
@ -353,7 +234,7 @@ const toVersion5 = exports._withSchemaVersion(5, initializeAttachmentMetadata);
const toVersion6 = exports._withSchemaVersion(
6,
exports._mapContact(
exports._cleanAndWriteContactAvatar(Attachment.migrateDataToFileSystem)
Contact.parseAndWriteContactAvatar(Attachment.migrateDataToFileSystem)
)
);

View file

@ -0,0 +1,356 @@
const { assert } = require('chai');
const sinon = require('sinon');
const Contact = require('../../../js/modules/types/contact');
const {
stringToArrayBuffer,
} = require('../../../js/modules/string_to_array_buffer');
describe('Contact', () => {
describe('parseAndWriteContactAvatar', () => {
const NUMBER = '+12025550099';
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 message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, message.contact[0]);
});
it('removes contact avatar if it has no sub-avatar', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
avatar: {
isProfile: true,
},
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('writes avatar to disk', async () => {
const upgradeAttachment = async () => {
return {
path: 'abc/abcdefg',
};
};
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
email: [
{
type: 2,
value: 'someone@somewhere.com',
},
],
address: [
{
type: 1,
street: '5 Somewhere Ave.',
},
],
avatar: {
otherKey: 'otherValue',
avatar: {
contentType: 'image/png',
data: stringToArrayBuffer('Its easy if you try'),
},
},
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
email: [
{
type: 2,
value: 'someone@somewhere.com',
},
],
address: [
{
type: 1,
street: '5 Somewhere Ave.',
},
],
avatar: {
otherKey: 'otherValue',
isProfile: false,
avatar: {
path: 'abc/abcdefg',
},
},
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('removes number element if it ends up with no value', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
},
],
email: [
{
value: 'someone@somewhere.com',
},
],
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
email: [
{
type: 1,
value: 'someone@somewhere.com',
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('drops address if it has no real values', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
value: NUMBER,
},
],
address: [
{
type: 1,
},
],
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
number: [
{
value: NUMBER,
type: 1,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('logs if contact has no name.displayName or organization', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
source: NUMBER,
sourceDevice: '1',
sent_at: 1232132,
contact: [
{
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const expected = {
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('removes invalid elements then logs if no values remain in contact', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
source: NUMBER,
sourceDevice: '1',
sent_at: 1232132,
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
},
],
email: [
{
type: 1,
},
],
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('handles a contact with just organization', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Contact.parseAndWriteContactAvatar(
upgradeAttachment
);
const message = {
contact: [
{
organization: 'Somewhere Consulting',
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, message.contact[0]);
});
});
});

View file

@ -630,351 +630,4 @@ describe('Message', () => {
assert.deepEqual(result, expected);
});
});
describe('_cleanAndWriteContactAvatar', () => {
const NUMBER = '+12025550099';
it('handles message with no avatar in contact', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, message.contact[0]);
});
it('removes contact avatar if it has no sub-avatar', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
avatar: {
isProfile: true,
},
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('writes avatar to disk', async () => {
const upgradeAttachment = async () => {
return {
path: 'abc/abcdefg',
};
};
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
email: [
{
type: 2,
value: 'someone@somewhere.com',
},
],
address: [
{
type: 1,
street: '5 Somewhere Ave.',
},
],
avatar: {
otherKey: 'otherValue',
avatar: {
contentType: 'image/png',
data: stringToArrayBuffer('Its easy if you try'),
},
},
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
value: NUMBER,
},
],
email: [
{
type: 2,
value: 'someone@somewhere.com',
},
],
address: [
{
type: 1,
street: '5 Somewhere Ave.',
},
],
avatar: {
otherKey: 'otherValue',
isProfile: false,
avatar: {
path: 'abc/abcdefg',
},
},
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('removes number element if it ends up with no value', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
},
],
email: [
{
value: 'someone@somewhere.com',
},
],
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
email: [
{
type: 1,
value: 'someone@somewhere.com',
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('drops address if it has no real values', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
value: NUMBER,
},
],
address: [
{
type: 1,
},
],
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
number: [
{
value: NUMBER,
type: 1,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('logs if contact has no name.displayName or organization', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
source: NUMBER,
sourceDevice: '1',
sent_at: 1232132,
contact: [
{
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const expected = {
name: {
name: 'Someone',
},
number: [
{
type: 1,
value: NUMBER,
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('removes invalid elements then logs if no values remain in contact', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
body: 'hey there!',
source: NUMBER,
sourceDevice: '1',
sent_at: 1232132,
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
type: 1,
},
],
email: [
{
type: 1,
},
],
},
],
};
const expected = {
name: {
displayName: 'Someone Somewhere',
},
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, expected);
});
it('handles a contact with just organization', async () => {
const upgradeAttachment = sinon
.stub()
.throws(new Error("Shouldn't be called"));
const upgradeVersion = Message._cleanAndWriteContactAvatar(
upgradeAttachment
);
const message = {
contact: [
{
organization: 'Somewhere Consulting',
number: [
{
type: 1,
value: NUMBER,
},
],
},
],
};
const result = await upgradeVersion(message.contact[0], { message });
assert.deepEqual(result, message.contact[0]);
});
});
});