Contact sharing: protos and data pipeline
As of this commit: 82b76ccf37
This commit is contained in:
parent
b6a585a646
commit
3ea3e4e256
7 changed files with 868 additions and 27 deletions
|
@ -283,6 +283,7 @@ describe('Backup', () => {
|
|||
|
||||
const OUR_NUMBER = '+12025550000';
|
||||
const CONTACT_ONE_NUMBER = '+12025550001';
|
||||
const CONTACT_TWO_NUMBER = '+12025550002';
|
||||
|
||||
async function wrappedLoadAttachment(attachment) {
|
||||
return _.omit(await loadAttachmentData(attachment), ['path']);
|
||||
|
@ -356,18 +357,31 @@ describe('Backup', () => {
|
|||
return wrappedLoadAttachment(thumbnail);
|
||||
});
|
||||
|
||||
const promises = (message.attachments || []).map(attachment =>
|
||||
wrappedLoadAttachment(attachment)
|
||||
);
|
||||
|
||||
return Object.assign({}, await loadThumbnails(message), {
|
||||
attachments: await Promise.all(promises),
|
||||
contact: await Promise.all(
|
||||
(message.contact || []).map(async contact => {
|
||||
return contact && contact.avatar && contact.avatar.avatar
|
||||
? Object.assign({}, contact, {
|
||||
avatar: Object.assign({}, contact.avatar, {
|
||||
avatar: await wrappedLoadAttachment(
|
||||
contact.avatar.avatar
|
||||
),
|
||||
}),
|
||||
})
|
||||
: contact;
|
||||
})
|
||||
),
|
||||
attachments: await Promise.all(
|
||||
(message.attachments || []).map(attachment =>
|
||||
wrappedLoadAttachment(attachment)
|
||||
)
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
let backupDir;
|
||||
try {
|
||||
const ATTACHMENT_COUNT = 2;
|
||||
const ATTACHMENT_COUNT = 3;
|
||||
const MESSAGE_COUNT = 1;
|
||||
const CONVERSATION_COUNT = 1;
|
||||
|
||||
|
@ -473,6 +487,59 @@ describe('Backup', () => {
|
|||
},
|
||||
],
|
||||
},
|
||||
contact: [
|
||||
{
|
||||
name: {
|
||||
displayName: 'Someone Somewhere',
|
||||
},
|
||||
number: [
|
||||
{
|
||||
value: CONTACT_TWO_NUMBER,
|
||||
type: 1,
|
||||
},
|
||||
],
|
||||
avatar: {
|
||||
isProfile: false,
|
||||
avatar: {
|
||||
contentType: 'image/png',
|
||||
data: new Uint8Array([
|
||||
3,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
]).buffer,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
console.log('Backup test: Clear all data');
|
||||
|
@ -494,7 +561,7 @@ describe('Backup', () => {
|
|||
profileAvatar: {
|
||||
contentType: 'image/jpeg',
|
||||
data: new Uint8Array([
|
||||
3,
|
||||
4,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
|
@ -530,7 +597,7 @@ describe('Backup', () => {
|
|||
size: 64,
|
||||
},
|
||||
profileKey: new Uint8Array([
|
||||
4,
|
||||
5,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
|
|
|
@ -63,6 +63,7 @@ describe('Message', () => {
|
|||
path: 'ab/abcdefghi',
|
||||
},
|
||||
],
|
||||
contact: [],
|
||||
};
|
||||
|
||||
const writeExistingAttachmentData = attachment => {
|
||||
|
@ -108,6 +109,56 @@ describe('Message', () => {
|
|||
},
|
||||
],
|
||||
},
|
||||
contact: [],
|
||||
};
|
||||
|
||||
const writeExistingAttachmentData = attachment => {
|
||||
assert.equal(attachment.path, 'ab/abcdefghi');
|
||||
assert.deepEqual(
|
||||
attachment.data,
|
||||
stringToArrayBuffer('It’s easy if you try')
|
||||
);
|
||||
};
|
||||
|
||||
const actual = await Message.createAttachmentDataWriter(
|
||||
writeExistingAttachmentData
|
||||
)(input);
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should process contact avatars', async () => {
|
||||
const input = {
|
||||
body: 'Imagine there is no heaven…',
|
||||
schemaVersion: 4,
|
||||
attachments: [],
|
||||
contact: [
|
||||
{
|
||||
name: 'john',
|
||||
avatar: {
|
||||
isProfile: false,
|
||||
avatar: {
|
||||
path: 'ab/abcdefghi',
|
||||
data: stringToArrayBuffer('It’s easy if you try'),
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const expected = {
|
||||
body: 'Imagine there is no heaven…',
|
||||
schemaVersion: 4,
|
||||
attachments: [],
|
||||
contact: [
|
||||
{
|
||||
name: 'john',
|
||||
avatar: {
|
||||
isProfile: false,
|
||||
avatar: {
|
||||
path: 'ab/abcdefghi',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const writeExistingAttachmentData = attachment => {
|
||||
|
@ -212,6 +263,7 @@ describe('Message', () => {
|
|||
hasVisualMediaAttachments: undefined,
|
||||
hasFileAttachments: 1,
|
||||
schemaVersion: Message.CURRENT_SCHEMA_VERSION,
|
||||
contact: [],
|
||||
};
|
||||
|
||||
const expectedAttachmentData = stringToArrayBuffer(
|
||||
|
@ -458,7 +510,7 @@ describe('Message', () => {
|
|||
assert.deepEqual(result, message);
|
||||
});
|
||||
|
||||
it('eliminates thumbnails with no data fielkd', async () => {
|
||||
it('eliminates thumbnails with no data field', async () => {
|
||||
const upgradeAttachment = sinon
|
||||
.stub()
|
||||
.throws(new Error("Shouldn't be called"));
|
||||
|
@ -531,4 +583,398 @@ describe('Message', () => {
|
|||
assert.deepEqual(result, expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_mapContact', () => {
|
||||
it('handles message with no contact field', async () => {
|
||||
const upgradeContact = sinon
|
||||
.stub()
|
||||
.throws(new Error("Shouldn't be called"));
|
||||
const upgradeVersion = Message._mapContact(upgradeContact);
|
||||
|
||||
const message = {
|
||||
body: 'hey there!',
|
||||
};
|
||||
const expected = {
|
||||
body: 'hey there!',
|
||||
contact: [],
|
||||
};
|
||||
const result = await upgradeVersion(message);
|
||||
assert.deepEqual(result, expected);
|
||||
});
|
||||
|
||||
it('handles one contact', async () => {
|
||||
const upgradeContact = contact => Promise.resolve(contact);
|
||||
const upgradeVersion = Message._mapContact(upgradeContact);
|
||||
|
||||
const message = {
|
||||
body: 'hey there!',
|
||||
contact: [
|
||||
{
|
||||
name: {
|
||||
displayName: 'Someone somewhere',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const expected = {
|
||||
body: 'hey there!',
|
||||
contact: [
|
||||
{
|
||||
name: {
|
||||
displayName: 'Someone somewhere',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const result = await upgradeVersion(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('It’s 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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue