remove regionCode and PhoneNumber.ts
parent
2b92386399
commit
02fb5783a4
@ -1,155 +0,0 @@
|
|||||||
const { omit, compact, map } = require('lodash');
|
|
||||||
|
|
||||||
const { toLogFormat } = require('./errors');
|
|
||||||
const { SignalService } = require('../../../ts/protobuf');
|
|
||||||
const { parse: parsePhoneNumber } = require('../../../ts/types/PhoneNumber');
|
|
||||||
|
|
||||||
const DEFAULT_PHONE_TYPE = SignalService.DataMessage.Contact.Phone.Type.HOME;
|
|
||||||
const DEFAULT_EMAIL_TYPE = SignalService.DataMessage.Contact.Email.Type.HOME;
|
|
||||||
const DEFAULT_ADDRESS_TYPE =
|
|
||||||
SignalService.DataMessage.Contact.PostalAddress.Type.HOME;
|
|
||||||
|
|
||||||
exports.parseAndWriteAvatar = upgradeAttachment => async (
|
|
||||||
contact,
|
|
||||||
context = {}
|
|
||||||
) => {
|
|
||||||
const { message, regionCode, logger } = context;
|
|
||||||
const { avatar } = contact;
|
|
||||||
|
|
||||||
// This is to ensure that an omit() call doesn't pull in prototype props/methods
|
|
||||||
const contactShallowCopy = Object.assign({}, contact);
|
|
||||||
|
|
||||||
const contactWithUpdatedAvatar =
|
|
||||||
avatar && avatar.avatar
|
|
||||||
? Object.assign({}, contactShallowCopy, {
|
|
||||||
avatar: Object.assign({}, avatar, {
|
|
||||||
avatar: await upgradeAttachment(avatar.avatar, context),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
: omit(contactShallowCopy, ['avatar']);
|
|
||||||
|
|
||||||
// eliminates empty numbers, emails, and addresses; adds type if not provided
|
|
||||||
const parsedContact = parseContact(contactWithUpdatedAvatar, { regionCode });
|
|
||||||
|
|
||||||
const error = exports._validate(parsedContact, {
|
|
||||||
messageId: idForLogging(message),
|
|
||||||
});
|
|
||||||
if (error) {
|
|
||||||
logger.error(
|
|
||||||
'Contact.parseAndWriteAvatar: contact was malformed.',
|
|
||||||
toLogFormat(error)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsedContact;
|
|
||||||
};
|
|
||||||
|
|
||||||
function parseContact(contact, options = {}) {
|
|
||||||
const { regionCode } = options;
|
|
||||||
|
|
||||||
const boundParsePhone = phoneNumber =>
|
|
||||||
parsePhoneItem(phoneNumber, { regionCode });
|
|
||||||
|
|
||||||
return Object.assign(
|
|
||||||
{},
|
|
||||||
omit(contact, ['avatar', 'number', 'email', 'address']),
|
|
||||||
parseAvatar(contact.avatar),
|
|
||||||
createArrayKey('number', compact(map(contact.number, boundParsePhone))),
|
|
||||||
createArrayKey('email', compact(map(contact.email, parseEmailItem))),
|
|
||||||
createArrayKey('address', compact(map(contact.address, parseAddress)))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function idForLogging(message) {
|
|
||||||
return `${message.source}.${message.sourceDevice} ${message.sent_at}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports._validate = (contact, options = {}) => {
|
|
||||||
const { messageId } = options;
|
|
||||||
const { name, number, email, address, organization } = contact;
|
|
||||||
|
|
||||||
if ((!name || !name.displayName) && !organization) {
|
|
||||||
return new Error(
|
|
||||||
`Message ${messageId}: Contact had neither 'displayName' nor 'organization'`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
(!number || !number.length) &&
|
|
||||||
(!email || !email.length) &&
|
|
||||||
(!address || !address.length)
|
|
||||||
) {
|
|
||||||
return new Error(
|
|
||||||
`Message ${messageId}: Contact had no included numbers, email or addresses`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
function parsePhoneItem(item, options = {}) {
|
|
||||||
const { regionCode } = options;
|
|
||||||
|
|
||||||
if (!item.value) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Object.assign({}, item, {
|
|
||||||
type: item.type || DEFAULT_PHONE_TYPE,
|
|
||||||
value: parsePhoneNumber(item.value, { regionCode }),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseEmailItem(item) {
|
|
||||||
if (!item.value) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Object.assign({}, item, {
|
|
||||||
type: item.type || DEFAULT_EMAIL_TYPE,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseAddress(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 || DEFAULT_ADDRESS_TYPE,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseAvatar(avatar) {
|
|
||||||
if (!avatar) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
avatar: Object.assign({}, avatar, {
|
|
||||||
isProfile: avatar.isProfile || false,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createArrayKey(key, array) {
|
|
||||||
if (!array || !array.length) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
[key]: array,
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,189 +0,0 @@
|
|||||||
import { assert } from 'chai';
|
|
||||||
|
|
||||||
import { contactSelector, getName } from '../../types/Contact';
|
|
||||||
|
|
||||||
describe('Contact', () => {
|
|
||||||
describe('getName', () => {
|
|
||||||
it('returns displayName if provided', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
};
|
|
||||||
const expected = 'displayName';
|
|
||||||
const actual = getName(contact);
|
|
||||||
assert.strictEqual(actual, expected);
|
|
||||||
});
|
|
||||||
it('returns organization if no displayName', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
};
|
|
||||||
const expected = 'Somewhere, Inc.';
|
|
||||||
const actual = getName(contact);
|
|
||||||
assert.strictEqual(actual, expected);
|
|
||||||
});
|
|
||||||
it('returns givenName + familyName if no displayName or organization', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const expected = 'givenName familyName';
|
|
||||||
const actual = getName(contact);
|
|
||||||
assert.strictEqual(actual, expected);
|
|
||||||
});
|
|
||||||
it('returns just givenName', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
givenName: 'givenName',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const expected = 'givenName';
|
|
||||||
const actual = getName(contact);
|
|
||||||
assert.strictEqual(actual, expected);
|
|
||||||
});
|
|
||||||
it('returns just familyName', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const expected = 'familyName';
|
|
||||||
const actual = getName(contact);
|
|
||||||
assert.strictEqual(actual, expected);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('contactSelector', () => {
|
|
||||||
const regionCode = '1';
|
|
||||||
const getAbsoluteAttachmentPath = (path: string) => `absolute:${path}`;
|
|
||||||
const onSendMessage = () => null;
|
|
||||||
const onClick = () => null;
|
|
||||||
|
|
||||||
it('eliminates avatar if it has had an attachment download error', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
avatar: {
|
|
||||||
isProfile: true,
|
|
||||||
avatar: {
|
|
||||||
error: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const expected = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
avatar: undefined,
|
|
||||||
onSendMessage,
|
|
||||||
onClick,
|
|
||||||
number: undefined,
|
|
||||||
};
|
|
||||||
const actual = contactSelector(contact, {
|
|
||||||
regionCode,
|
|
||||||
getAbsoluteAttachmentPath,
|
|
||||||
onSendMessage,
|
|
||||||
onClick,
|
|
||||||
});
|
|
||||||
assert.deepEqual(actual, expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not calculate absolute path if avatar is pending', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
avatar: {
|
|
||||||
isProfile: true,
|
|
||||||
avatar: {
|
|
||||||
pending: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const expected = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
avatar: {
|
|
||||||
isProfile: true,
|
|
||||||
avatar: {
|
|
||||||
pending: true,
|
|
||||||
path: undefined,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
onSendMessage,
|
|
||||||
onClick,
|
|
||||||
number: undefined,
|
|
||||||
};
|
|
||||||
const actual = contactSelector(contact, {
|
|
||||||
regionCode,
|
|
||||||
getAbsoluteAttachmentPath,
|
|
||||||
onSendMessage,
|
|
||||||
onClick,
|
|
||||||
});
|
|
||||||
assert.deepEqual(actual, expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('calculates absolute path', () => {
|
|
||||||
const contact = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
avatar: {
|
|
||||||
isProfile: true,
|
|
||||||
avatar: {
|
|
||||||
path: 'somewhere',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const expected = {
|
|
||||||
name: {
|
|
||||||
displayName: 'displayName',
|
|
||||||
givenName: 'givenName',
|
|
||||||
familyName: 'familyName',
|
|
||||||
},
|
|
||||||
organization: 'Somewhere, Inc.',
|
|
||||||
avatar: {
|
|
||||||
isProfile: true,
|
|
||||||
avatar: {
|
|
||||||
path: 'absolute:somewhere',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
onSendMessage,
|
|
||||||
onClick,
|
|
||||||
number: undefined,
|
|
||||||
};
|
|
||||||
const actual = contactSelector(contact, {
|
|
||||||
regionCode,
|
|
||||||
getAbsoluteAttachmentPath,
|
|
||||||
onSendMessage,
|
|
||||||
onClick,
|
|
||||||
});
|
|
||||||
assert.deepEqual(actual, expected);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,65 +0,0 @@
|
|||||||
import { LocalizerType } from './Util';
|
|
||||||
|
|
||||||
export function format(
|
|
||||||
phoneNumber: string,
|
|
||||||
_options: {
|
|
||||||
ourRegionCode: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
return phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parse(
|
|
||||||
phoneNumber: string,
|
|
||||||
_options: {
|
|
||||||
regionCode: string;
|
|
||||||
}
|
|
||||||
): string {
|
|
||||||
return phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function normalize(
|
|
||||||
phoneNumber: string,
|
|
||||||
_options: { regionCode: string }
|
|
||||||
): string | undefined {
|
|
||||||
try {
|
|
||||||
if (isValidNumber(phoneNumber)) {
|
|
||||||
return phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
} catch (error) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function validate(number: string) {
|
|
||||||
// Check if it's hex
|
|
||||||
const isHex = number.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/);
|
|
||||||
if (!isHex) {
|
|
||||||
return 'invalidSessionId';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the pubkey length is 33 and leading with 05 or of length 32
|
|
||||||
const len = number.length;
|
|
||||||
if ((len !== 33 * 2 || !/^05/.test(number)) && len !== 32 * 2) {
|
|
||||||
return 'invalidPubkeyFormat';
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidNumber(number: string) {
|
|
||||||
const error = validate(number);
|
|
||||||
|
|
||||||
return !error;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function validateNumber(
|
|
||||||
number: string,
|
|
||||||
i18n: LocalizerType = window.i18n
|
|
||||||
) {
|
|
||||||
const error = validate(number);
|
|
||||||
|
|
||||||
return error && i18n(error);
|
|
||||||
}
|
|
Loading…
Reference in New Issue