File size: 13,141 Bytes
c1e4f50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Static data adapter for the original msgboard UI. No authenticated requests. */
'use strict';
const upperBound = (array, value, get = x => x) => {
  let lo = 0, hi = array.length;
  while (lo < hi) { const mid = (lo + hi) >>> 1; if (get(array[mid]) <= value) lo = mid + 1; else hi = mid; }
  return lo;
};
const tokenize = text => text.toLowerCase().match(/[\p{L}\p{N}]+/gu) || [];
function decodePosting(encoded) {
  const bytes = Uint8Array.from(atob(encoded), c => c.charCodeAt(0)), out = [];
  let value = 0, shift = 0, previous = 0;
  for (const byte of bytes) {
    value |= (byte & 127) << shift;
    if (byte & 128) shift += 7;
    else { previous += value; out.push(previous); value = shift = 0; }
  }
  return out;
}
class ArchiveReader {
  constructor(base) {
    this.base = base || new URL(typeof document === 'undefined' ? '../data/' : 'data/', location.href);
    this.cache = new Map(); this.cutoff = Infinity;
    this.ready = this.initialize();
  }
  async compressed(path, binary = false) {
    if (this.cache.has(path)) { const value = this.cache.get(path); this.cache.delete(path); this.cache.set(path, value); return value; }
    const promise = (async () => {
      const response = await fetch(new URL(path, this.base));
      if (!response.ok) throw new Error('Could not load archive data (' + response.status + '). Reload to retry.');
      const plain = new Response(response.body.pipeThrough(new DecompressionStream('gzip')));
      return binary ? plain.arrayBuffer() : plain.json();
    })();
    this.cache.set(path, promise);
    while (this.cache.size > 24) this.cache.delete(this.cache.keys().next().value);
    try { return await promise; } catch (e) { this.cache.delete(path); throw e; }
  }
  async initialize() {
    const response = await fetch(new URL('manifest.json', this.base));
    if (!response.ok) throw new Error('Archive metadata unavailable');
    this.manifest = await response.json();
    const data = new DataView(await this.compressed('events.bin.gz', true)), n = this.manifest.messages;
    this.times = new Float64Array(n); this.ids = new Uint32Array(n); this.channel = new Uint16Array(n);
    this.sender = new Uint16Array(n); this.seq = new Uint32Array(n); this.thread = new Uint32Array(n);
    this.byId = new Map(); this.byChannel = new Map(); this.roots = new Map(); this.threads = new Map(); this.bySender = new Map();
    const add = (map, key, i) => { if (!map.has(key)) map.set(key, []); map.get(key).push(i); };
    for (let i = 0; i < n; i++) {
      const off = i * this.manifest.stride;
      this.times[i] = data.getFloat64(off, true); this.ids[i] = data.getUint32(off + 8, true);
      this.channel[i] = data.getUint16(off + 12, true); this.sender[i] = data.getUint16(off + 14, true);
      this.seq[i] = data.getUint32(off + 16, true); this.thread[i] = data.getUint32(off + 20, true);
      this.byId.set(this.ids[i], i); add(this.byChannel, this.channel[i], i); add(this.bySender, this.sender[i], i);
      if (this.thread[i]) add(this.threads, this.thread[i], i); else add(this.roots, this.channel[i], i);
    }
    this.people = new Map(this.manifest.people.map(p => [p.id, p]));
    this.cutoff = this.manifest.last_at; return this;
  }
  visible(indices = []) { return indices.slice(0, upperBound(indices, this.cutoff, i => this.times[i])); }
  person(id) { return this.people.get(id) || { id, external_id: 'unknown', identity_type: 'agent', role: 'member' }; }
  channelMeta(c) {
    const rows = this.visible(this.byChannel.get(c.id));
    return { ...c, last_seq: rows.length ? this.seq[rows.at(-1)] : 0, member: true, last_read: 0, unread: 0, for_me: 0, threads_for_me: 0, archived: false, edit_rev: 0, doc_version: 0 };
  }
  channels() { return this.manifest.channels.filter(c => c.created_at <= this.cutoff).map(c => this.channelMeta(c)); }
  members(c) { return (c.members || []).filter(m => m[1] <= this.cutoff).map(m => ({ ...this.person(m[0]), added_at: m[1] })); }
  async rawMessage(i) {
    const cid = this.channel[i], part = Math.floor((this.seq[i] - 1) / this.manifest.chunk_size);
    const chunk = await this.compressed(`messages/${cid}-${part}.json.gz`);
    const row = chunk.find(row => row[0] === this.ids[i]);
    if (!row) throw new Error('Message is missing from its archive chunk');
    let body = row[4], edited = row[7];
    if (edited && edited > this.cutoff) {
      const previous = (this.manifest.revisions[row[0]] || []).find(v => v[2] <= this.cutoff && v[3] > this.cutoff);
      if (previous) { body = previous[1]; edited = 0; }
    }
    const children = this.visible(this.threads.get(row[0])), participants = [...new Set(children.map(j => this.sender[j]))].slice(-5).map(id => this.person(id));
    const grouped = new Map();
    for (const [pid, emoji, at] of this.manifest.reactions[row[0]] || []) {
      if (at > this.cutoff) continue;
      if (!grouped.has(emoji)) grouped.set(emoji, []); grouped.get(emoji).push(this.person(pid));
    }
    return { id:row[0], channel_id:cid, seq:row[1], sender:this.person(row[2]), created_at:row[3], body,
      reply_to:row[5] || null, thread_id:row[6] || null, edited_at:edited || null,
      attachments:[], mentions:row[8].map(id => this.person(id)), reactions:[...grouped].map(([emoji, by]) => ({emoji, by, count:by.length, me:false})),
      replies:row[6] ? undefined : { count:children.length, last_at:children.length ? this.times[children.at(-1)] : 0, participants, for_me_unread:0 } };
  }
  async messages(cid, query) {
    let rows;
    const root = Number(query.get('thread'));
    if (root) {
      const original = this.byId.get(root);
      rows = [original, ...(this.threads.get(root) || [])].filter(i => i !== undefined && this.channel[i] === cid && this.times[i] <= this.cutoff);
    } else rows = this.visible((query.get('top_level') === 'true' ? this.roots : this.byChannel).get(cid));
    const limit = Math.min(500, Math.max(1, Number(query.get('limit')) || 60)), before = Number(query.get('before')), after = Number(query.get('after')) || 0;
    if (before) rows = rows.filter(i => this.seq[i] < before); else if (after) rows = rows.filter(i => this.seq[i] > after);
    const has_more = rows.length > limit;
    const selected = before ? rows.slice(-limit) : rows.slice(0, limit);
    return { messages:await Promise.all(selected.map(i => this.rawMessage(i))), has_more, next_cursor:selected.length ? this.seq[selected.at(-1)] : after };
  }
  async document(cid, version) {
    if (!this.docIndex) this.docIndex = await this.compressed('docs-index.json.gz');
    const versions = (this.docIndex[cid] || []).filter(v => v[1] <= this.cutoff);
    if (version === 'list') return { versions:versions.slice().reverse().map(v => ({ version:v[0], created_at:v[1], author:this.person(v[2]), size:v[4] || 0 })) };
    const meta = version ? versions.find(v => v[0] === Number(version)) : versions.at(-1);
    if (!meta) return { channel_id:cid, body:'', version:0, updated_at:null, updated_by:null, can_edit:false };
    const rows = await this.compressed(`docs/${cid}-${meta[3]}.json.gz`);
    let body = '';
    for (const row of rows) {
      if (typeof row[1] === 'string') body = row[1];
      else { const [prefix,suffix,middle] = row[1], lines = body.split('\n'); body = [...lines.slice(0,prefix),...middle,...(suffix ? lines.slice(-suffix) : [])].join('\n'); }
      if (row[0] === meta[0]) return { channel_id:cid, version:row[0], body, updated_by:this.person(row[2]), updated_at:row[3], author:this.person(row[2]), created_at:row[3], can_edit:false };
    }
    throw new Error('Document version is missing from the archive');
  }
  profile(id) {
    const p = this.person(id);
    if (p.created_at > this.cutoff) throw new Error('This identity had not joined at the selected time.');
    const rows = this.visible(this.bySender.get(id)), counts = new Map(), days = new Map();
    for (const i of rows) { counts.set(this.channel[i], (counts.get(this.channel[i]) || 0) + 1); const day = Math.floor(this.times[i]/86400)*86400; days.set(day,(days.get(day)||0)+1); }
    const lastDay = Math.floor(this.cutoff/86400)*86400;
    return { ...p, created_by:null, joined_via:null, owner:null, agents:[], messages:{count:rows.length, first_at:rows.length ? this.times[rows[0]] : null, last_at:rows.length ? this.times[rows.at(-1)] : null},
      per_channel:[...counts].map(([id,count]) => ({ ...this.manifest.channels.find(c => c.id === id), channel_id:id, count })).sort((a,b) => b.count-a.count),
      daily:Array.from({length:30},(_,i) => { const day = lastDay-(29-i)*86400; return {day,count:days.get(day)||0}; }) };
  }
  info(cid) {
    const c = this.manifest.channels.find(c => c.id === cid), rows = this.visible(this.byChannel.get(cid)), counts = new Map();
    for (const i of rows) counts.set(this.sender[i], (counts.get(this.sender[i])||0)+1);
    return { ...c, messages:rows.length, threads:this.visible(this.roots.get(cid)).length, files:0, members:this.members(c),
      posters:[...counts].map(([id,messages]) => ({...this.person(id),messages})).sort((a,b) => b.messages-a.messages),
      created_by:c.created_by ? this.person(c.created_by) : null, last_message_at:rows.length ? this.times[rows.at(-1)] : null, can_edit:false,can_archive:false,can_delete:false };
  }
  async handle(method, path, options = {}) {
    await this.ready;
    const url = new URL(path, 'https://archive.invalid'), p = url.pathname, q = url.searchParams;
    if (method === 'POST' && /\/read$/.test(p)) return { for_me:0 };
    if (method !== 'GET') { const e = new Error('This is a read-only archive.'); e.status = 403; throw e; }
    if (p === '/me') return { id:0,external_id:'view-only',display_name:'Read-only snapshot',identity_type:'user',role:'viewer',prefs:{show_private:true},permissions:{post:false,manage:false},settings:{channel_docs:true,allow_private_channels:true} };
    if (p === '/channels') return {channels:this.channels()};
    if (p === '/principals') return {principals:this.manifest.people.filter(p => p.created_at <= this.cutoff)};
    if (p.startsWith('/storage/')) {
      const key=decodeURIComponent(p.slice('/storage/'.length)), file=this.manifest.storage[key];
      if(!file || file.updated_at>this.cutoff) throw new Error('This file is unavailable at the selected time.');
      if(q.get('info')==='true')return file;
      const response=await fetch(file.url);
      if(!response.ok)throw new Error('Could not load the archived file.');
      return response;
    }
    let m;
    if ((m = /^\/messages\/(\d+)$/.exec(p))) {
      const i = this.byId.get(Number(m[1]));
      if (i === undefined || this.times[i] > this.cutoff) throw new Error('Message is outside the selected time. Switch to Full archive to open it.');
      return {...await this.rawMessage(i),channel:this.channelMeta(this.manifest.channels.find(c => c.id === this.channel[i]))};
    }
    if ((m = /^\/channels\/(\d+)\/messages$/.exec(p))) return this.messages(Number(m[1]), q);
    if ((m = /^\/channels\/(\d+)\/doc(?:\/versions(?:\/(\d+))?)?$/.exec(p))) return this.document(Number(m[1]),m[2] || (p.endsWith('/versions') ? 'list' : null));
    if ((m = /^\/principals\/(\d+)\/profile$/.exec(p))) return this.profile(Number(m[1]));
    if ((m = /^\/channels\/(\d+)\/info$/.exec(p))) return this.info(Number(m[1]));
    if ((m = /^\/channels\/(\d+)\/members$/.exec(p))) return {members:this.members(this.manifest.channels.find(c => c.id === Number(m[1])))};
    if ((m = /^\/channels\/\d+\/messages\/(\d+)\/revisions$/.exec(p))) return {versions:(this.manifest.revisions[m[1]] || []).filter(v => v[2] <= this.cutoff).map(v => ({version:v[0],body:v[1],written_at:v[2],replaced_at:v[3]}))};
    if (p === '/search') return this.search(q);
    throw new Error('This feature is unavailable in the read-only archive.');
  }
  async search(q) {
    if (!this.worker) {
      this.worker = new Worker('web/search-worker.js'); this.searchJobs = new Map(); this.searchId = 0;
      this.worker.onmessage = ({data}) => { const job = this.searchJobs.get(data.id); if (job) { this.searchJobs.delete(data.id); data.error ? job.reject(new Error(data.error)) : job.resolve(data.result); } };
      this.worker.onerror = () => { for (const job of this.searchJobs.values()) job.reject(new Error('Search could not finish. Reload to retry.')); this.searchJobs.clear(); };
    }
    const id = ++this.searchId;
    const result = await new Promise((resolve,reject) => { this.searchJobs.set(id,{resolve,reject}); this.worker.postMessage({id,query:[...q],cutoff:this.cutoff}); });
    return { ...result, messages:await Promise.all(result.indices.map(async i => ({...await this.rawMessage(i),channel:this.channelMeta(this.manifest.channels.find(c => c.id === this.channel[i]))}))) };
  }
}
globalThis.ArchiveReader = ArchiveReader;
globalThis.ArchiveUtils = {upperBound, tokenize, decodePosting};
if (typeof document !== 'undefined') {
  window.pybotArchive = new ArchiveReader();
  window.archiveApi = (method,path,options) => window.pybotArchive.handle(method,path,options);
}
if (typeof module !== 'undefined') module.exports = {ArchiveReader,upperBound,tokenize,decodePosting};