Search returns primary device when looking up secondary pub key

pull/501/head
sachaaaaa 6 years ago
parent f9df221638
commit d3c3184405

@ -75,6 +75,8 @@ module.exports = {
createOrUpdatePairingAuthorisation, createOrUpdatePairingAuthorisation,
getAuthorisationForPubKey, getAuthorisationForPubKey,
getSecondaryDevicesFor, getSecondaryDevicesFor,
getPrimaryDeviceFor,
getPairedDevicesFor,
createOrUpdateItem, createOrUpdateItem,
getItemById, getItemById,
@ -1284,6 +1286,43 @@ async function getSecondaryDevicesFor(primaryDevicePubKey) {
return map(rows, row => row.secondaryDevicePubKey); return map(rows, row => row.secondaryDevicePubKey);
} }
async function getPrimaryDeviceFor(secondaryDevicePubKey) {
const row = await db.get(
`SELECT primaryDevicePubKey FROM ${PAIRING_AUTHORISATIONS_TABLE} WHERE secondaryDevicePubKey = $secondaryDevicePubKey AND isGranted = 1;`,
{
$secondaryDevicePubKey: secondaryDevicePubKey,
}
);
if (!row) {
return null;
}
return row.primaryDevicePubKey;
}
// Return all the paired pubkeys for a specific pubkey (excluded),
// irrespective of their Primary or Secondary status.
async function getPairedDevicesFor(pubKey) {
let results = [];
// get primary pubkey (only works if the pubkey is a secondary pubkey)
const primaryPubKey = await getPrimaryDeviceFor(pubKey);
if (primaryPubKey) {
results.push(primaryPubKey);
}
// get secondary pubkeys (only works if the pubkey is a primary pubkey)
const secondaryPubKeys = await getSecondaryDevicesFor(
primaryPubKey || pubKey
);
results = results.concat(secondaryPubKeys);
// ensure the input pubkey is not in the results
results = results.filter(x => x !== pubKey);
return results;
}
const ITEMS_TABLE = 'items'; const ITEMS_TABLE = 'items';
async function createOrUpdateItem(data) { async function createOrUpdateItem(data) {
return createOrUpdate(ITEMS_TABLE, data); return createOrUpdate(ITEMS_TABLE, data);

@ -1,2 +1,3 @@
export function searchMessages(query: string): Promise<Array<any>>; export function searchMessages(query: string): Promise<Array<any>>;
export function searchConversations(query: string): Promise<Array<any>>; export function searchConversations(query: string): Promise<Array<any>>;
export function getPrimaryDeviceFor(pubKey: string): Promise<string | null>;

@ -93,6 +93,8 @@ module.exports = {
getGrantAuthorisationForPubKey, getGrantAuthorisationForPubKey,
getAuthorisationForPubKey, getAuthorisationForPubKey,
getSecondaryDevicesFor, getSecondaryDevicesFor,
getPrimaryDeviceFor,
getPairedDevicesFor,
createOrUpdateItem, createOrUpdateItem,
getItemById, getItemById,
@ -627,8 +629,16 @@ async function getAuthorisationForPubKey(pubKey) {
}; };
} }
function getSecondaryDevicesFor(primareyDevicePubKey) { function getSecondaryDevicesFor(primaryDevicePubKey) {
return channels.getSecondaryDevicesFor(primareyDevicePubKey); return channels.getSecondaryDevicesFor(primaryDevicePubKey);
}
function getPrimaryDeviceFor(secondaryDevicePubKey) {
return channels.getPrimaryDeviceFor(secondaryDevicePubKey);
}
function getPairedDevicesFor(pubKey) {
return channels.getPairedDevicesFor(pubKey);
} }
// Items // Items

@ -13,6 +13,7 @@ import { ContactName } from './conversation/ContactName';
import { cleanSearchTerm } from '../util/cleanSearchTerm'; import { cleanSearchTerm } from '../util/cleanSearchTerm';
import { LocalizerType } from '../types/Util'; import { LocalizerType } from '../types/Util';
import { SearchOptions } from '../types/Search';
import { clipboard } from 'electron'; import { clipboard } from 'electron';
import { validateNumber } from '../types/PhoneNumber'; import { validateNumber } from '../types/PhoneNumber';
@ -37,18 +38,11 @@ export interface Props {
verified: boolean; verified: boolean;
profileName?: string; profileName?: string;
avatarPath?: string; avatarPath?: string;
isSecondaryDevice?: boolean; isSecondaryDevice: boolean;
i18n: LocalizerType; i18n: LocalizerType;
updateSearchTerm: (searchTerm: string) => void; updateSearchTerm: (searchTerm: string) => void;
search: ( search: (query: string, options: SearchOptions) => void;
query: string,
options: {
regionCode: string;
ourNumber: string;
noteToSelf: string;
}
) => void;
clearSearch: () => void; clearSearch: () => void;
onClick?: () => void; onClick?: () => void;
@ -110,12 +104,20 @@ export class MainHeader extends React.Component<Props, any> {
} }
public search() { public search() {
const { searchTerm, search, i18n, ourNumber, regionCode } = this.props; const {
searchTerm,
search,
i18n,
ourNumber,
regionCode,
isSecondaryDevice,
} = this.props;
if (search) { if (search) {
search(searchTerm, { search(searchTerm, {
noteToSelf: i18n('noteToSelf').toLowerCase(), noteToSelf: i18n('noteToSelf').toLowerCase(),
ourNumber, ourNumber,
regionCode, regionCode,
isSecondaryDevice,
}); });
} }
} }

@ -1,10 +1,12 @@
import { omit, reject } from 'lodash'; import { omit, reject } from 'lodash';
import { normalize } from '../../types/PhoneNumber'; import { normalize } from '../../types/PhoneNumber';
import { SearchOptions } from '../../types/Search';
import { trigger } from '../../shims/events'; import { trigger } from '../../shims/events';
// import { getMessageModel } from '../../shims/Whisper'; // import { getMessageModel } from '../../shims/Whisper';
// import { cleanSearchTerm } from '../../util/cleanSearchTerm'; // import { cleanSearchTerm } from '../../util/cleanSearchTerm';
import { import {
getPrimaryDeviceFor,
searchConversations /*, searchMessages */, searchConversations /*, searchMessages */,
} from '../../../js/modules/data'; } from '../../../js/modules/data';
import { makeLookup } from '../../util/makeLookup'; import { makeLookup } from '../../util/makeLookup';
@ -81,7 +83,7 @@ export const actions = {
function search( function search(
query: string, query: string,
options: { regionCode: string; ourNumber: string; noteToSelf: string } options: SearchOptions
): SearchResultsKickoffActionType { ): SearchResultsKickoffActionType {
return { return {
type: 'SEARCH_RESULTS', type: 'SEARCH_RESULTS',
@ -91,16 +93,12 @@ function search(
async function doSearch( async function doSearch(
query: string, query: string,
options: { options: SearchOptions
regionCode: string;
ourNumber: string;
noteToSelf: string;
}
): Promise<SearchResultsPayloadType> { ): Promise<SearchResultsPayloadType> {
const { regionCode, ourNumber, noteToSelf } = options; const { regionCode } = options;
const [discussions /*, messages */] = await Promise.all([ const [discussions /*, messages */] = await Promise.all([
queryConversationsAndContacts(query, { ourNumber, noteToSelf }), queryConversationsAndContacts(query, options),
// queryMessages(query), // queryMessages(query),
]); ]);
const { conversations, contacts } = discussions; const { conversations, contacts } = discussions;
@ -170,23 +168,46 @@ function startNewConversation(
async function queryConversationsAndContacts( async function queryConversationsAndContacts(
providedQuery: string, providedQuery: string,
options: { ourNumber: string; noteToSelf: string } options: SearchOptions
) { ) {
const { ourNumber, noteToSelf } = options; const { ourNumber, noteToSelf, isSecondaryDevice } = options;
const query = providedQuery.replace(/[+-.()]*/g, ''); const query = providedQuery.replace(/[+-.()]*/g, '');
const searchResults: Array<ConversationType> = await searchConversations( const searchResults: Array<ConversationType> = await searchConversations(
query query
); );
const ourPrimaryDevice = isSecondaryDevice
? await getPrimaryDeviceFor(ourNumber)
: ourNumber;
const resultPrimaryDevices: Array<string | null> = await Promise.all(
searchResults.map(
async conversation =>
conversation.id === ourPrimaryDevice
? Promise.resolve(ourPrimaryDevice)
: getPrimaryDeviceFor(conversation.id)
)
);
// Split into two groups - active conversations and items just from address book // Split into two groups - active conversations and items just from address book
let conversations: Array<string> = []; let conversations: Array<string> = [];
let contacts: Array<string> = []; let contacts: Array<string> = [];
const max = searchResults.length; const max = searchResults.length;
for (let i = 0; i < max; i += 1) { for (let i = 0; i < max; i += 1) {
const conversation = searchResults[i]; const conversation = searchResults[i];
const primaryDevice = resultPrimaryDevices[i];
if (conversation.type === 'direct' && !Boolean(conversation.lastMessage)) {
if (primaryDevice) {
if (isSecondaryDevice && primaryDevice === ourPrimaryDevice) {
conversations.push(ourNumber);
} else {
conversations.push(primaryDevice);
}
} else if (
conversation.type === 'direct' &&
!Boolean(conversation.lastMessage)
) {
contacts.push(conversation.id); contacts.push(conversation.id);
} else { } else {
conversations.push(conversation.id); conversations.push(conversation.id);

@ -0,0 +1,6 @@
export type SearchOptions = {
regionCode: string;
ourNumber: string;
noteToSelf: string;
isSecondaryDevice: boolean;
};
Loading…
Cancel
Save