From e18c8c77aef6a5ac36b2a9c22daa8f3fb8efb7cb Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 22 Oct 2020 20:56:27 +1300 Subject: [PATCH 1/3] Fallback to room name "empty room" --- src/js/room-picker.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/js/room-picker.js b/src/js/room-picker.js index d2e79fa..5a5340c 100644 --- a/src/js/room-picker.js +++ b/src/js/room-picker.js @@ -106,8 +106,12 @@ class Room extends ElemJS { } // if the room has no alias, use the names of its members ("heroes") const users = this.data.summary["m.heroes"] - const usernames = users.map(u => (u.match(/^@([^:]+):/) || [])[1] || u) - return usernames.join(", ") + if (users && users.length) { + const usernames = users.map(u => (u.match(/^@([^:]+):/) || [])[1] || u) + return usernames.join(", ") + } + // the room is empty + return "Empty room" } getIcon() { From 36f204624f3aebc221813c220ca8e82af8556647 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 22 Oct 2020 23:02:50 +1300 Subject: [PATCH 2/3] Refuse to send empty messages --- src/js/chat-input.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/js/chat-input.js b/src/js/chat-input.js index de0eec7..9b703c1 100644 --- a/src/js/chat-input.js +++ b/src/js/chat-input.js @@ -33,5 +33,6 @@ function fixHeight() { function send(body) { if (!store.activeRoom.exists()) return + if (!body.trim().length) return return store.activeRoom.value().timeline.send(body) } From 2f5955b0431b9ca3a87accdf1ee0722c028a8107 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 22 Oct 2020 23:03:26 +1300 Subject: [PATCH 3/3] Be able to load past room state (no UI yet) --- src/js/Timeline.js | 80 ++++++++++++++++++++++++++++++++++++--------- src/js/sync/sync.js | 1 + 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/js/Timeline.js b/src/js/Timeline.js index 3e2a0b6..f4e6d09 100644 --- a/src/js/Timeline.js +++ b/src/js/Timeline.js @@ -5,6 +5,10 @@ import {Anchor} from $to_relative "/js/Anchor.js" import * as lsm from $to_relative "/js/lsm.js" import {resolveMxc} from $to_relative "/js/functions.js" +let debug = false + +const NO_MAX = Symbol("NO_MAX") + const dateFormatter = Intl.DateTimeFormat("default", {hour: "numeric", minute: "numeric", day: "numeric", month: "short", year: "numeric"}) let sentIndex = 0 @@ -13,10 +17,10 @@ function getTxnId() { return Date.now() + (sentIndex++) } -function eventSearch(list, event, min = 0, max = -1) { +function eventSearch(list, event, min = 0, max = NO_MAX) { if (list.length === 0) return {success: false, i: 0} - if (max === -1) max = list.length - 1 + if (max === NO_MAX) max = list.length - 1 let mid = Math.floor((max + min) / 2) // success condition if (list[mid] && list[mid].data.event_id === event.data.event_id) return {success: true, i: mid} @@ -78,8 +82,12 @@ class Event extends ElemJS { } else if (this.data.type === "m.room.member") { if (this.data.content.membership === "join") { this.child(ejs("i").text("joined the room")) - } else { + } else if (this.data.content.membership === "invite") { + this.child(ejs("i").text(`invited ${this.data.content.displayname} to the room`)) + } else if (this.data.content.membership === "leave") { this.child(ejs("i").text("left the room")) + } else { + this.child(ejs("i").text("unknown membership event")) } } else if (this.data.type === "m.room.encrypted") { this.child(ejs("i").text("Carbon does not yet support encrypted messages.")) @@ -178,10 +186,21 @@ class ReactiveTimeline extends ElemJS { } addEvent(event) { + // if (debug) console.log("running search", this.list, event) + // if (debug) debugger; const search = eventSearch(this.list, event) // console.log(search, this.list.map(l => l.data.sender), event.data) - if (!search.success && search.i >= 1) this.tryAddGroups(event, [search.i-1, search.i]) - else this.tryAddGroups(event, [search.i]) + if (!search.success) { + if (search.i >= 1) { + // add at end + this.tryAddGroups(event, [search.i-1, search.i]) + } else { + // add at start + this.tryAddGroups(event, [0, -1]) + } + } else { + this.tryAddGroups(event, [search.i]) + } } tryAddGroups(event, indices) { @@ -189,6 +208,10 @@ class ReactiveTimeline extends ElemJS { if (!this.list[i]) { // if (printed++ < 100) console.log("tryadd success, created group") const group = new EventGroup(this, [event]) + if (i === -1) { + // here, -1 means at the start, before the first group + i = 0 // jank but it does the trick + } this.list.splice(i, 0, group) this.childAt(i, group) event.setGroup(group) @@ -234,6 +257,8 @@ class Timeline extends Subscribable { this.reactiveTimeline = new ReactiveTimeline(this.id, []) this.latest = 0 this.pending = new Set() + this.pendingEdits = [] + this.from = null } updateStateEvents(events) { @@ -282,17 +307,8 @@ class Timeline extends Subscribable { } // handle edits if (eventData.type === "m.room.message" && eventData.content["m.relates_to"] && eventData.content["m.relates_to"].rel_type === "m.replace") { - const replaces = eventData.content["m.relates_to"].event_id - if (this.map.has(replaces)) { - const event = this.map.get(replaces) - event.data.content = eventData.content["m.new_content"] - event.setEdited(eventData.origin_server_ts) - event.update(event.data) - continue - } else { - // uhhhhhhh - console.error(`want to replace event ${replaces} with ${eventData.id} but replaced event not found`) - } + this.pendingEdits.push(eventData) + continue } // add new event const event = new Event(eventData) @@ -300,6 +316,19 @@ class Timeline extends Subscribable { this.reactiveTimeline.addEvent(event) } } + // apply edits + this.pendingEdits = this.pendingEdits.filter(eventData => { + const replaces = eventData.content["m.relates_to"].event_id + if (this.map.has(replaces)) { + const event = this.map.get(replaces) + event.data.content = eventData.content["m.new_content"] + event.setEdited(eventData.origin_server_ts) + event.update(event.data) + return false // handled; remove from list + } else { + return true // we don't have the event it edits yet; keep in list + } + }) this.broadcast("afterChange") } @@ -313,6 +342,25 @@ class Timeline extends Subscribable { return this.reactiveTimeline } + async loadScrollback() { + debug = true + if (!this.from) throw new Error("Can't load scrollback, no from token") + const url = new URL(`${lsm.get("domain")}/_matrix/client/r0/rooms/${this.id}/messages`) + url.searchParams.set("access_token", lsm.get("access_token")) + url.searchParams.set("from", this.from) + url.searchParams.set("dir", "b") + url.searchParams.set("limit", 10) + const filter = { + lazy_load_members: true + } + url.searchParams.set("filter", JSON.stringify(filter)) + const root = await fetch(url.toString()).then(res => res.json()) + this.from = root.end + console.log(this.updateEvents, root.chunk) + if (root.state) this.updateStateEvents(root.state) + this.updateEvents(root.chunk) + } + send(body) { const tx = getTxnId() const id = `pending$${tx}` diff --git a/src/js/sync/sync.js b/src/js/sync/sync.js index f338e4d..195af6e 100644 --- a/src/js/sync/sync.js +++ b/src/js/sync/sync.js @@ -53,6 +53,7 @@ function manageSync(root) { } const room = store.rooms.get(id).value() const timeline = room.timeline + if (!timeline.from) timeline.from = data.timeline.prev_batch if (data.timeline.events.length) newEvents = true timeline.updateStateEvents(data.state.events) timeline.updateEvents(data.timeline.events)