Compare commits
14 commits
6499cd4ff6
...
79aa423ebb
Author | SHA1 | Date | |
---|---|---|---|
79aa423ebb | |||
91ac7a6b3e | |||
aa12cd68e6 | |||
4382928a93 | |||
2f5955b043 | |||
36f204624f | |||
16de7edd19 | |||
0113024be6 | |||
1b97351ca0 | |||
dce4fa6303 | |||
2ff43ea801 | |||
61cc4a19f3 | |||
0c3c06bc0a | |||
6f67ddbce5 |
4 changed files with 113 additions and 47 deletions
|
@ -5,6 +5,10 @@ import {Anchor} from $to_relative "/js/Anchor.js"
|
||||||
import * as lsm from $to_relative "/js/lsm.js"
|
import * as lsm from $to_relative "/js/lsm.js"
|
||||||
import {resolveMxc} from $to_relative "/js/functions.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"})
|
const dateFormatter = Intl.DateTimeFormat("default", {hour: "numeric", minute: "numeric", day: "numeric", month: "short", year: "numeric"})
|
||||||
|
|
||||||
let sentIndex = 0
|
let sentIndex = 0
|
||||||
|
@ -13,10 +17,10 @@ function getTxnId() {
|
||||||
return Date.now() + (sentIndex++)
|
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 (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)
|
let mid = Math.floor((max + min) / 2)
|
||||||
// success condition
|
// success condition
|
||||||
if (list[mid] && list[mid].data.event_id === event.data.event_id) return {success: true, i: mid}
|
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") {
|
} else if (this.data.type === "m.room.member") {
|
||||||
if (this.data.content.membership === "join") {
|
if (this.data.content.membership === "join") {
|
||||||
this.child(ejs("i").text("joined the room"))
|
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"))
|
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") {
|
} else if (this.data.type === "m.room.encrypted") {
|
||||||
this.child(ejs("i").text("Carbon does not yet support encrypted messages."))
|
this.child(ejs("i").text("Carbon does not yet support encrypted messages."))
|
||||||
|
@ -178,10 +186,21 @@ class ReactiveTimeline extends ElemJS {
|
||||||
}
|
}
|
||||||
|
|
||||||
addEvent(event) {
|
addEvent(event) {
|
||||||
|
// if (debug) console.log("running search", this.list, event)
|
||||||
|
// if (debug) debugger;
|
||||||
const search = eventSearch(this.list, event)
|
const search = eventSearch(this.list, event)
|
||||||
// console.log(search, this.list.map(l => l.data.sender), event.data)
|
// 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])
|
if (!search.success) {
|
||||||
else this.tryAddGroups(event, [search.i])
|
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) {
|
tryAddGroups(event, indices) {
|
||||||
|
@ -189,6 +208,10 @@ class ReactiveTimeline extends ElemJS {
|
||||||
if (!this.list[i]) {
|
if (!this.list[i]) {
|
||||||
// if (printed++ < 100) console.log("tryadd success, created group")
|
// if (printed++ < 100) console.log("tryadd success, created group")
|
||||||
const group = new EventGroup(this, [event])
|
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.list.splice(i, 0, group)
|
||||||
this.childAt(i, group)
|
this.childAt(i, group)
|
||||||
event.setGroup(group)
|
event.setGroup(group)
|
||||||
|
@ -234,6 +257,8 @@ class Timeline extends Subscribable {
|
||||||
this.reactiveTimeline = new ReactiveTimeline(this.id, [])
|
this.reactiveTimeline = new ReactiveTimeline(this.id, [])
|
||||||
this.latest = 0
|
this.latest = 0
|
||||||
this.pending = new Set()
|
this.pending = new Set()
|
||||||
|
this.pendingEdits = []
|
||||||
|
this.from = null
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStateEvents(events) {
|
updateStateEvents(events) {
|
||||||
|
@ -282,17 +307,8 @@ class Timeline extends Subscribable {
|
||||||
}
|
}
|
||||||
// handle edits
|
// handle edits
|
||||||
if (eventData.type === "m.room.message" && eventData.content["m.relates_to"] && eventData.content["m.relates_to"].rel_type === "m.replace") {
|
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
|
this.pendingEdits.push(eventData)
|
||||||
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
|
continue
|
||||||
} else {
|
|
||||||
// uhhhhhhh
|
|
||||||
console.error(`want to replace event ${replaces} with ${eventData.id} but replaced event not found`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// add new event
|
// add new event
|
||||||
const event = new Event(eventData)
|
const event = new Event(eventData)
|
||||||
|
@ -300,6 +316,19 @@ class Timeline extends Subscribable {
|
||||||
this.reactiveTimeline.addEvent(event)
|
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")
|
this.broadcast("afterChange")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,6 +342,25 @@ class Timeline extends Subscribable {
|
||||||
return this.reactiveTimeline
|
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) {
|
send(body) {
|
||||||
const tx = getTxnId()
|
const tx = getTxnId()
|
||||||
const id = `pending$${tx}`
|
const id = `pending$${tx}`
|
||||||
|
|
|
@ -33,5 +33,6 @@ function fixHeight() {
|
||||||
|
|
||||||
function send(body) {
|
function send(body) {
|
||||||
if (!store.activeRoom.exists()) return
|
if (!store.activeRoom.exists()) return
|
||||||
|
if (!body.trim().length) return
|
||||||
return store.activeRoom.value().timeline.send(body)
|
return store.activeRoom.value().timeline.send(body)
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,40 +77,16 @@ class Form extends ElemJS {
|
||||||
if (!username.isValid()) return this.cancel("Username is not valid.")
|
if (!username.isValid()) return this.cancel("Username is not valid.")
|
||||||
|
|
||||||
// Resolve homeserver address
|
// Resolve homeserver address
|
||||||
let currentAddress = homeserver.value
|
let domain
|
||||||
let ok = false
|
|
||||||
while (!ok) {
|
|
||||||
if (!currentAddress.match(/^https?:\/\//)) {
|
|
||||||
console.warn(`${currentAddress} doesn't specify the protocol, assuming https`)
|
|
||||||
currentAddress = "https://" + currentAddress
|
|
||||||
}
|
|
||||||
currentAddress = currentAddress.replace(/\/*$/, "")
|
|
||||||
this.status(`Looking up homeserver... trying ${currentAddress}`)
|
|
||||||
try {
|
try {
|
||||||
// check if we found the actual matrix server
|
domain = await this.findHomeserver(homeserver.value)
|
||||||
try {
|
|
||||||
const versions = await fetch(`${currentAddress}/_matrix/client/versions`).then(res => res.json())
|
|
||||||
if (Array.isArray(versions.versions)) {
|
|
||||||
ok = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} catch (e) {}
|
|
||||||
// find the next matrix server in the chain
|
|
||||||
const root = await fetch(`${currentAddress}/.well-known/matrix/client`).then(res => res.json())
|
|
||||||
let nextAddress = root["m.homeserver"].base_url
|
|
||||||
nextAddress = nextAddress.replace(/\/*$/, "")
|
|
||||||
if (currentAddress === nextAddress) {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
currentAddress = nextAddress
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
return this.cancel(`Failed to look up server ${currentAddress}`)
|
return this.cancel(e.message)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request access token
|
// Request access token
|
||||||
this.status("Logging in...")
|
this.status("Logging in...")
|
||||||
const root = await fetch(`${currentAddress}/_matrix/client/r0/login`, {
|
const root = await fetch(`${domain}/_matrix/client/r0/login`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
type: "m.login.password",
|
type: "m.login.password",
|
||||||
|
@ -130,12 +106,52 @@ class Form extends ElemJS {
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.setItem("mx_user_id", root.user_id)
|
localStorage.setItem("mx_user_id", root.user_id)
|
||||||
localStorage.setItem("domain", currentAddress)
|
localStorage.setItem("domain", domain)
|
||||||
localStorage.setItem("access_token", root.access_token)
|
localStorage.setItem("access_token", root.access_token)
|
||||||
|
|
||||||
location.assign("../")
|
location.assign("../")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findHomeserver(address, maxDepth = 5) {
|
||||||
|
|
||||||
|
//Protects from servers sending us on a redirect loop
|
||||||
|
maxDepth--
|
||||||
|
if (maxDepth <= 0) throw new Error(`Failed to look up homeserver, maximum search depth reached`)
|
||||||
|
|
||||||
|
//Normalise the address
|
||||||
|
if (!address.match(/^https?:\/\//)) {
|
||||||
|
console.warn(`${address} doesn't specify the protocol, assuming https`)
|
||||||
|
address = "https://" + address
|
||||||
|
}
|
||||||
|
address = address.replace(/\/*$/, "")
|
||||||
|
|
||||||
|
this.status(`Looking up homeserver... trying ${address}`)
|
||||||
|
|
||||||
|
// Check if we found the actual matrix server
|
||||||
|
try {
|
||||||
|
const versionsReq = await fetch(`${address}/_matrix/client/versions`)
|
||||||
|
if (versionsReq.ok) {
|
||||||
|
const versions = await versionsReq.json()
|
||||||
|
if (Array.isArray(versions.versions)) return address
|
||||||
|
}
|
||||||
|
} catch(e) {}
|
||||||
|
|
||||||
|
// Find the next matrix server in the chain
|
||||||
|
const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => {
|
||||||
|
console.error(e)
|
||||||
|
throw new Error(`Failed to look up server ${address}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
let nextAddress = root["m.homeserver"].base_url
|
||||||
|
nextAddress = nextAddress.replace(/\/*$/, "")
|
||||||
|
|
||||||
|
if (address === nextAddress) {
|
||||||
|
throw new Error(`Failed to look up server ${address}, /.well-known/matrix/client found a redirect loop`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.findHomeserver(nextAddress, maxDepth)
|
||||||
|
}
|
||||||
|
|
||||||
status(message) {
|
status(message) {
|
||||||
feedback.setLoading(true)
|
feedback.setLoading(true)
|
||||||
feedback.message(message)
|
feedback.message(message)
|
||||||
|
|
|
@ -53,6 +53,7 @@ function manageSync(root) {
|
||||||
}
|
}
|
||||||
const room = store.rooms.get(id).value()
|
const room = store.rooms.get(id).value()
|
||||||
const timeline = room.timeline
|
const timeline = room.timeline
|
||||||
|
if (!timeline.from) timeline.from = data.timeline.prev_batch
|
||||||
if (data.timeline.events.length) newEvents = true
|
if (data.timeline.events.length) newEvents = true
|
||||||
timeline.updateStateEvents(data.state.events)
|
timeline.updateStateEvents(data.state.events)
|
||||||
timeline.updateEvents(data.timeline.events)
|
timeline.updateEvents(data.timeline.events)
|
||||||
|
|
Loading…
Reference in a new issue