Move contact migrate code from message.js to contact.js
This commit is contained in:
parent
47adbe4358
commit
420f81ebcd
4 changed files with 480 additions and 469 deletions
121
js/modules/types/contact.js
Normal file
121
js/modules/types/contact.js
Normal 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,
|
||||
};
|
||||
}
|
|
@ -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)
|
||||
)
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue