Nostr rest api field mapping gap

From divinevideo/divine-mobile · View interactive version

Nostr rest api field mapping gap is an AI agent skill from the GitHub repository divinevideo/divine-mobile. Fix features broken by REST API responses that flatten Nostr events, losing tag data. Use when: (1) A feature works for WebSocket-loaded events but not REST API-loaded ones, (2) A model field (like sha256, textTrackRef) is null for REST API data but set for WebSocket data, (3) REST API returns denor

How to use this skill

Skill file

Nostr REST API Field Mapping Gap

Problem

Nostr clients that use both REST APIs and WebSocket connections to load events can

have features that work inconsistently. Features that depend on Nostr tag values

(like the x tag for sha256, or custom tags for text tracks) work when events are

loaded via WebSocket (which provides the full raw event with all tags), but break

when the same events are loaded via REST API (which returns flattened/denormalized

JSON without the raw tags array).

Context / Trigger Conditions

doesn't include raw tags

Root Cause

REST APIs that index Nostr events typically:

  1. Extract commonly-used tag values into dedicated columns (d_tag, title, thumbnail, etc.)
  2. Do NOT return the full raw tags array in their response
  3. May have semantically equivalent fields under different names

For example, in a Blossom-based video system:

Solution

Step 1: Identify the mapping gap

Compare what the REST API returns vs what the model needs:

# Fetch a sample REST API response
curl -s "https://your-api.example.com/api/videos?limit=1" | jq '.[0] | keys'

Check which model fields are null after parsing:

Step 2: Add fallback mappings

In the model's fromJson(), add fallback logic AFTER the primary parsing:

// Primary: try direct field and tags
var sha256 = eventData['sha256']?.toString() ?? json['sha256']?.toString();

// Parse from tags if available
if (eventData['tags'] is List) {
  // ... tag parsing for 'x' tag ...
}

// Normalize
if (sha256 != null && sha256.isEmpty) sha256 = null;

// FALLBACK: Use semantically equivalent field from REST API
// For Blossom uploads, d_tag IS the content hash (64 hex chars)
if (sha256 == null && dTag.length == 64 && _isHex(dTag)) {
  sha256 = dTag;
}

Step 3: Validate the fallback is safe

Ensure the fallback only triggers when appropriate:

Step 4: Add tests

test('falls back to d_tag as sha256 when d_tag is 64-char hex', () {
  final json = {
    'd_tag': 'a04b70820ef370e90aae19d23e46b1482d3af0e7c9d994d1594a1384a62d3972',
    // No sha256 field, no tags array (REST API response)
    ...otherFields,
  };
  final model = Model.fromJson(json);
  expect(model.sha256, equals(json['d_tag']));
});

test('does not use d_tag as sha256 when d_tag is not a hex hash', () {
  final json = {'d_tag': 'my-video-slug', ...otherFields};
  final model = Model.fromJson(json);
  expect(model.sha256, isNull);
});

test('does not override explicit sha256 with d_tag', () {
  final json = {
    'd_tag': 'a04b708...', // 64 hex
    'sha256': 'explicit-value',
    ...otherFields,
  };
  final model = Model.fromJson(json);
  expect(model.sha256, equals('explicit-value'));
});

Verification

  1. Load the app and navigate to a feed that uses the REST API (e.g., trending/discovery)
  2. Verify the feature works (e.g., CC button shows AND subtitles display when toggled)
  3. Also verify WebSocket-loaded events still work (e.g., home feed from followed users)
  4. Check that non-hash d_tags (classic imports, custom slugs) don't get false positives

Debugging Approach

When a feature works inconsistently across events:

  1. Identify data source: Is the broken event from REST API or WebSocket?
  2. Compare raw data: Fetch the same event from both sources, diff the fields
  3. Trace the pipeline: API response -> fromJson() -> model -> getter -> UI condition
  4. Check conditional guards: Look for if (model.hasX) or if (field != null) in UI
  5. Log at the model level: Add temporary logging in fromJson() to see what's null

Notes

is forward-compatible (explicit fields take priority over fallbacks)

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.