Oddbean new post about | logout
 Last update of the day. Some folks asked for a version of my crappy script that preserves the latest relay list, and someone also suggested adding a “license” to it. So here you go. Thanks, nostr:nprofile1qqs8h057dewjchkpp2etmjkvd9chzr7j09m5ra4jsvq4ls60usya73qpz4mhxue69uhkummnw3ezummcw3ezuer9wchsz9thwden5te0wfjkccte9ejxzmt4wvhxjme0qy88wumn8ghj7mn0wvhxcmmv9ue77sja, for the idea.

```javascript
// Remove all outdated kind 10002 events
// SPDX-License-Identifier: Unlicense
// SPDX-FileCopyrightText: 2024 Anthony Accioly <anthony@accioly.social>

import WebSocket from 'ws'
import { Relay, useWebSocketImplementation } from 'nostr-tools/relay'
import { finalizeEvent, getPublicKey, sortEvents } from 'nostr-tools/pure'
import * as nip19 from 'nostr-tools/nip19'

useWebSocketImplementation(WebSocket);

const relay = await Relay.connect('ws://192.168.1.1:4869');
const sk = nip19.decode('nsec1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa').data
const pk = getPublicKey(sk)

console.log(`connected to ${relay.url}`)

const kind10002Events = []

relay.subscribe([
  {
    kinds: [10002],
    authors: [pk],
  },
], {
  onevent(event) {
    kind10002Events.push(event)
  },
  async oneose() {
    const eventCount = kind10002Events.length
    console.log('Got', eventCount, 'kind 10002 events')
    try {
      await removeAllOutdatedKind10002Events();
    } catch (error) {
      console.error('Error removing outdated events:', error);
    }
    relay.close()
  }
})

async function removeAllOutdatedKind10002Events() {
  if (kind10002Events.length === 0) {
    console.log('No kind 10002 events to remove')
    return
  }

  const [firstEvent, ...outdatedEvents] = sortEvents(kind10002Events);
  console.log('Most recent kind 10002 event:', firstEvent);

  if (outdatedEvents.length === 0) {
    console.log('No outdated kind 10002 events to remove')
    return
  }

  const deleteEvent = finalizeEvent({
    kind: 5,
    created_at: Math.floor(Date.now() / 1000),
    // Event tags with id and kind
    tags: outdatedEvents.flatMap(e => [['e', e.id], ['k', '10002']]),
    content: 'Deleting outdated kind 10002 events',
  }, sk)
  console.log('Delete event:', deleteEvent)
  try {
    await relay.publish(deleteEvent);
    console.log('Deleted', outdatedEvents.length, 'kind 10002 events');
  } catch (error) {
    console.error('Error publishing delete event:', error);
  }
}
```

Haven doesn’t honour delete requests at the moment. One way to work around this is to delete the Outbox database and re-import your notes from well-behaved relays. Once you have a single kind 10002 event in your Outbox relay, avoid changing your relay settings (easier said than done, but it’s the only workaround I’ve found until either Amethyst or Haven/Khatru is updated).

nostr:nevent1qqs8pj23nwav4e352086d7eawq4w28lqus7yp4l6ey8vvj7uvux97tgpypmhxue69uhksctkv4hzuctrvd5k7mre9eek7cmfv9kz76twvfhhsq3qa6we08n7zsv2na689whc9hykpq4q6sj3kaauk9c2dm8vj0adlajqxpqqqqqqzv39a9a

#devnostr #kind10002writeLoop #amethyst #haven #citrine