The Tweet ID JavaScript Rounded Off — JavaScript Bug Hunt

Modelled on Twitter's move to Snowflake ids in 2010. Tweet ids became 64-bit integers that soon grew past 2^53, the largest integer a JavaScript number…

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Easy
  • Concepts: Parsing, Identifiers
  • Modelled on: Twitter · 2010
  • Visible tests: an early, small id renders; a snowflake id keeps every digit
  • Reward: 50 XP for a complete fix

Briefing

Modelled on Twitter's move to Snowflake ids in 2010. Tweet ids became 64-bit integers that soon grew past 2^53, the largest integer a JavaScript number holds exactly. A client that ran the API's JSON through JSON.parse got a different, rounded id, so links and replies pointed at the wrong tweet or none. Twitter's answer was to add string copies of every id — id_str, in_reply_to_status_id_str — and tell JavaScript clients to use those.

This reconstruction's timeline view model still reads the numeric fields.

Fix toViewModel so every id comes from its string field.

Bug report

BUG-IDSTR · Priority: High · Reported by: web client team

toViewModel(rawJson) returns { id, text, permalink, replyTo }:

  • id is the tweet's id_str, exactly as sent
  • permalink is "/status/" + id
  • replyTo is in_reply_to_status_id_str, or null when that is null/absent
  • a payload without a digits-only id_str string is malformed: throw

Observed: tweet 1234567890123456789 renders a permalink to /status/1234567890123456800, which is somebody else's tweet (or a 404).

Logs

[timeline] GET /status/1234567890123456800 -> 404
[timeline] reply target 1498765432109876543 not found in thread

The code as shipped

src/timeline/tweets.js (editable)

// Turns one raw API payload (a JSON string) into the view model the
// timeline renders.
exports.toViewModel = function (rawJson) {
  var t = JSON.parse(rawJson);
  var id = String(t.id);
  var replyTo = t.in_reply_to_status_id == null ? null : String(t.in_reply_to_status_id);
  return {
    id: id,
    text: t.text,
    permalink: "/status/" + id,
    replyTo: replyTo
  };
};

Read-only context: src/timeline/NUMBERS.js.

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More JavaScript bug hunts.