From 76ac861c08b51c3220368a23627bdc6cfe00200f Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 8 Apr 2023 21:16:43 +0000 Subject: [PATCH] improve display of contact names --- src/LoadContactsPage.ts | 2 +- src/LoadHistory.test.ts | 5 +++++ src/LoadHistory.ts | 4 ++-- src/fetchEvents.ts | 5 ++++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/LoadContactsPage.ts b/src/LoadContactsPage.ts index 132bf9a..918d8b1 100644 --- a/src/LoadContactsPage.ts +++ b/src/LoadContactsPage.ts @@ -55,7 +55,7 @@ const generateContactDetails = (pubkey:string):string => {
${m && !!m.picture ? ` ` : ''}
- ${m.name ? m.name : '[unknown name]'} + ${getContactName(pubkey)} ${m.nip05 ? `${m.nip05} ` : ''} ${otherspetname && otherspetname !== m.name ? `
popular petname: ${otherspetname}
` : ''}
${m.about ? m.about : ''}
diff --git a/src/LoadHistory.test.ts b/src/LoadHistory.test.ts index 68197c4..0c2623e 100644 --- a/src/LoadHistory.test.ts +++ b/src/LoadHistory.test.ts @@ -4,6 +4,7 @@ import { } from './LoadHistory'; import { MetadataFlex } from './LoadMetadataPage'; import SampleEvents from './SampleEvents'; +import * as LocalStorage from './LocalStorage'; const weekago = Math.round((Date.now() / 1000) - (60 * 60 * 24 * 7.1)); const monthsago = Math.round((Date.now() / 1000) - (60 * 60 * 24 * 7 * 5)); @@ -118,6 +119,8 @@ describe('generateContactsChanges', () => { test('is removed', () => { const s = JSON.parse(JSON.stringify(SampleEvents.kind3)); delete s.tags[2][3]; + jest.spyOn(LocalStorage, 'localStorageGetItem') + .mockImplementation((): string | null => JSON.stringify([s])); const r = generateContactsChanges([ s, { ...SampleEvents.kind3 }, @@ -128,6 +131,8 @@ describe('generateContactsChanges', () => { }); test('is added', () => { const s = JSON.parse(JSON.stringify(SampleEvents.kind3)); + jest.spyOn(LocalStorage, 'localStorageGetItem') + .mockImplementation((): string | null => JSON.stringify([{ ...SampleEvents.kind3 }])); delete s.tags[2][3]; const r = generateContactsChanges([ { ...SampleEvents.kind3 }, diff --git a/src/LoadHistory.ts b/src/LoadHistory.ts index 765f500..c5d6611 100644 --- a/src/LoadHistory.ts +++ b/src/LoadHistory.ts @@ -1,6 +1,6 @@ import * as timeago from 'timeago.js'; import { Event } from 'nostr-tools'; -import { fetchCachedMyProfileEventHistory, submitUnsignedEvent } from './fetchEvents'; +import { fetchCachedMyProfileEventHistory, getContactName, submitUnsignedEvent } from './fetchEvents'; export type VersionChange = { ago:number; @@ -73,7 +73,7 @@ const sameContact = ( const getPetname = (a:['p', string, string, string]):string => { if (a[3] && a[3].length > 0) return `${a[3]}`; - return `${(a[1]).substring(0, 10)}...`; + return `${getContactName(a[1])}`; }; export const generateContactsChanges = ( diff --git a/src/fetchEvents.ts b/src/fetchEvents.ts index 9847f7c..9c78e35 100644 --- a/src/fetchEvents.ts +++ b/src/fetchEvents.ts @@ -275,8 +275,11 @@ export const getContactName = (pubkey: string):string => { // their kind 0 name if (UserProfileEvents[pubkey]) { if (UserProfileEvents[pubkey][0]) { - const { name } = JSON.parse(UserProfileEvents[pubkey][0].content); + // eslint-disable-next-line @typescript-eslint/naming-convention + const { name, display_name } = JSON.parse(UserProfileEvents[pubkey][0].content); if (name) return name; + // name isn't present for Jack Dorsey and Vitor from Amethyst in Apr 2023. + if (display_name) return display_name; } } }