add modifications to contacts history change log

This commit is contained in:
=
2023-03-09 16:43:27 +00:00
parent 3086593e27
commit 486c0be546
2 changed files with 62 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import {
generateContactsChanges, generateHistoryTable, generateMetadataChanges, generateRelayChanges,
Kind10002Event, Kind3Event,
Kind10002Event, Kind10002Tag, Kind3Event,
} from './LoadHistory';
import { MetadataFlex } from './LoadMetadataPage';
import SampleEvents from './SampleEvents';
@@ -87,6 +87,7 @@ describe('generateMetadataChanges', () => {
});
describe('generateContactsChanges', () => {
const shortContactPubkey = (a: Kind10002Tag) => `<mark>${(a[1]).substring(0, 10)}...</mark>`;
test('the oldest event list all the contacts as a single change', () => {
const r = generateContactsChanges([
{ ...SampleEvents.kind3 } as Kind3Event,
@@ -111,6 +112,41 @@ describe('generateContactsChanges', () => {
]);
expect(r[0].changes).toEqual(['<div class="removed">removed <mark>carol</mark></div>']);
});
describe('when a contact\'s petname changes, the change is listed in the changes array', () => {
test('is removed', () => {
const s = JSON.parse(JSON.stringify(SampleEvents.kind3));
delete s.tags[2][3];
const r = generateContactsChanges([
s,
{ ...SampleEvents.kind3 },
]);
expect(r[0].changes).toEqual([
`removed petname for ${shortContactPubkey(s.tags[2])}, previously <mark>carol</mark>`,
]);
});
test('is added', () => {
const s = JSON.parse(JSON.stringify(SampleEvents.kind3));
delete s.tags[2][3];
const r = generateContactsChanges([
{ ...SampleEvents.kind3 },
s,
]);
expect(r[0].changes).toEqual([
'added petname for <mark>carol</mark>',
]);
});
test('is modified', () => {
const s = JSON.parse(JSON.stringify(SampleEvents.kind3));
s.tags[2][3] = 'caroline';
const r = generateContactsChanges([
s,
{ ...SampleEvents.kind3 },
]);
expect(r[0].changes).toEqual([
'modified petname for <mark>caroline</mark>, previously <mark>carol</mark>',
]);
});
});
test('when a contact is added and another removed, both events are listed in the changes array', () => {
const s = JSON.parse(JSON.stringify(SampleEvents.kind3));
delete s.tags[2];

View File

@@ -90,8 +90,18 @@ export const generateContactsChanges = (
// list adds
const added = current.filter((c) => !next.some((n) => sameContact(c, n)));
if (added.length > 0) changes.push(`<div class="added">added ${added.map(getPetname).join(', ')}</div>`);
// TODO: list modified
// current.map((c) => JSON.stringify(c))
// list modifications
const modified = current.filter(
(c) => next.filter((n) => n[1] === c[1]).some((n) => c[3] !== n[3]),
);
modified.forEach((r) => {
const nv = next.find((n) => n[1] === r[1]);
if (!nv) return null;
if (!r[3] && !nv[3]) return null;
if (r[3] && !nv[3]) return changes.push(`added petname for ${getPetname(r)}`);
if (!r[3] && nv[3]) return changes.push(`removed petname for ${getPetname(r)}, previously ${getPetname(nv)}`);
return changes.push(`modified petname for ${getPetname(r)}, previously ${getPetname(nv)}`);
});
// list deletes
const removed = next.filter((c) => !current.some((n) => sameContact(c, n)));
if (removed.length > 0) changes.push(`<div class="removed">removed ${removed.map(getPetname).join(', ')}</div>`);
@@ -143,21 +153,19 @@ export const generateRelayChanges = (
const modified = current.filter(
(c) => next.filter((n) => n[1] === c[1]).some((n) => c[2] !== n[2]),
);
if (modified.length > 0) {
modified.forEach((r) => {
const nv = next.find((n) => n[1] === r[1]);
let s: string;
if (!r[2]) {
if (!!nv && nv[2] === 'read') s = '<mark class="added">write</mark> as well as read';
else s = '<mark class="added">read</mark> as well as write';
} else if (r[2] === 'read') {
if (!nv) s = 'only read and no longer <mark class="removed">write</mark>';
else s = '<mark class="added">read</mark> instead of <mark class="removed">write</mark>';
} else if (!nv) s = 'only write and no longer <mark class="removed">read</mark>';
else s = '<mark class="added">write</mark> instead of <mark class="removed">read</mark>';
changes.push(`<div class="modified">modified <mark>${r[1]}</mark> to ${s}</div>`);
});
}
modified.forEach((r) => {
const nv = next.find((n) => n[1] === r[1]);
let s: string;
if (!r[2]) {
if (!!nv && nv[2] === 'read') s = '<mark class="added">write</mark> as well as read';
else s = '<mark class="added">read</mark> as well as write';
} else if (r[2] === 'read') {
if (!nv) s = 'only read and no longer <mark class="removed">write</mark>';
else s = '<mark class="added">read</mark> instead of <mark class="removed">write</mark>';
} else if (!nv) s = 'only write and no longer <mark class="removed">read</mark>';
else s = '<mark class="added">write</mark> instead of <mark class="removed">read</mark>';
changes.push(`<div class="modified">modified <mark>${r[1]}</mark> to ${s}</div>`);
});
// list deletes
const removed = next.filter((c) => !current.some((n) => n[1] === c[1]));
if (removed.length > 0) {