server/bugfix: fix panic on invited rooms; expose v2 server url to client

- Expose `/client/server.json` so clients know the CS API base endpoint for things like media requests
  (and in future sending events, etc)
- Tidy up a few comments.
This commit is contained in:
Kegan Dougal 2022-02-24 14:14:59 +00:00
parent f79eb957de
commit 8a677347f8
8 changed files with 60 additions and 26 deletions

View File

@ -1,5 +1,7 @@
// This file contains code to render the developer tools overlay (bandwidth, list visualisations, etc)
// You don't need to read this file to understand sliding sync.
/*
* This file contains code to render the developer tools overlay (bandwidth, list visualisations, etc)
* You don't need to read this file to understand sliding sync.
*/
/**
* Set the bandwidth values on the devtools display.

View File

@ -1,4 +1,7 @@
// This file contains the entry point for the client, as well as DOM interactions.
/*
* This file contains the entry point for the client, as well as DOM interactions.
*/
import {
SlidingList,
SlidingSyncConnection,
@ -9,6 +12,7 @@ import {
import * as render from "./render.js";
import * as devtools from "./devtools.js";
let syncv2ServerUrl;
let slidingSync;
let syncConnection = new SlidingSyncConnection();
let activeLists = [
@ -202,14 +206,6 @@ const renderRoomContent = (roomId, refresh) => {
if (roomId !== slidingSync.roomSubscription) {
return;
}
const container = document.getElementById("messages");
if (refresh) {
document.getElementById("selectedroomname").textContent = "";
// wipe all message entries
while (container.hasChildNodes()) {
container.removeChild(container.firstChild);
}
}
let room = rooms.roomIdToRoom[slidingSync.roomSubscription];
if (!room) {
console.error(
@ -218,6 +214,14 @@ const renderRoomContent = (roomId, refresh) => {
);
return;
}
const container = document.getElementById("messages");
if (refresh) {
document.getElementById("selectedroomname").textContent = "";
// wipe all message entries
while (container.hasChildNodes()) {
container.removeChild(container.firstChild);
}
}
document.getElementById("selectedroomname").textContent =
room.name || room.room_id;
if (room.avatar) {
@ -485,11 +489,18 @@ const mxcToUrl = (mxc) => {
if (!path) {
return;
}
// TODO: we should really use the proxy HS not matrix.org
return `https://matrix-client.matrix.org/_matrix/media/r0/thumbnail/${path}?width=64&height=64&method=crop`;
return `${syncv2ServerUrl}/_matrix/media/r0/thumbnail/${path}?width=64&height=64&method=crop`;
};
window.addEventListener("load", (event) => {
window.addEventListener("load", async (event) => {
const v2ServerResp = await fetch("./server.json");
const syncv2ServerJson = await v2ServerResp.json();
if (!syncv2ServerJson || !syncv2ServerJson.server) {
console.error("failed to fetch v2 server url: ", v2ServerResp);
return;
}
syncv2ServerUrl = syncv2ServerJson.server.replace(/\/$/, ""); // remove trailing /
const container = document.getElementById("roomlistcontainer");
activeLists.forEach((list) => {
const roomList = document.createElement("div");

View File

@ -1,6 +1,8 @@
// This file contains code to map data structures to actual HTML elements.
// It is mostly functional and boring and it does not include any sliding sync specific data.
// In other words, if you want to learn about sliding sync, this isn't the file to look at.
/*
* This file contains code to map data structures to actual HTML elements.
* It is mostly functional and boring and it does not include any sliding sync specific data.
* In other words, if you want to learn about sliding sync, this isn't the file to look at.
*/
const membershipChangeText = (ev) => {
const prevContent = (ev.unsigned || {}).prev_content || {};

View File

@ -1,4 +1,9 @@
// This file contains the main sliding sync code.
/*
* This file contains the main sliding sync code.
* It defines the core sliding sync types and contains code for handling list deltas.
* It doesn't concern itself with storing room data or updating the UI in any way, deferring
* to room/lifecycle callbacks for that.
*/
import * as devtools from "./devtools.js";

View File

@ -39,5 +39,5 @@ func main() {
if err != nil {
panic(err)
}
syncv3.RunSyncV3Server(h, *flagBindAddr)
syncv3.RunSyncV3Server(h, *flagBindAddr, *flagDestinationServer)
}

View File

@ -95,11 +95,11 @@ func (s *Storage) MetadataForAllRooms() (map[string]internal.RoomMetadata, error
defer rows.Close()
result := make(map[string]internal.RoomMetadata)
for rows.Next() {
var hi internal.RoomMetadata
if err := rows.Scan(&hi.RoomID, &hi.JoinCount); err != nil {
var metadata internal.RoomMetadata
if err := rows.Scan(&metadata.RoomID, &metadata.JoinCount); err != nil {
return nil, err
}
result[hi.RoomID] = hi
result[metadata.RoomID] = metadata
}
// Select the invited member counts using the same style of query
rows, err = s.accumulator.db.Query(`
@ -119,9 +119,9 @@ func (s *Storage) MetadataForAllRooms() (map[string]internal.RoomMetadata, error
if err := rows.Scan(&roomID, &inviteCount); err != nil {
return nil, err
}
hi := result[roomID]
hi.InviteCount = inviteCount
result[roomID] = hi
metadata := result[roomID]
metadata.InviteCount = inviteCount
result[roomID] = metadata
}
// work out latest timestamps
@ -132,6 +132,9 @@ func (s *Storage) MetadataForAllRooms() (map[string]internal.RoomMetadata, error
for _, ev := range events {
metadata := result[ev.RoomID]
metadata.LastMessageTimestamp = gjson.ParseBytes(ev.JSON).Get("origin_server_ts").Uint()
// it's possible the latest event is a brand new room not caught by the first SELECT for joined
// rooms e.g when you're invited to a room so we need to make sure to se the metadata again here
metadata.RoomID = ev.RoomID
result[ev.RoomID] = metadata
}

View File

@ -152,6 +152,8 @@ func (c *GlobalCache) Startup(roomIDToMetadata map[string]internal.RoomMetadata)
for roomID := range roomIDToMetadata {
metadata := roomIDToMetadata[roomID]
fmt.Printf("Room: %s - %s - %s \n", roomID, metadata.NameEvent, gomatrixserverlib.Timestamp(metadata.LastMessageTimestamp).Time())
internal.Assert("room ID is set", metadata.RoomID != "")
internal.Assert("last message timestamp exists", metadata.LastMessageTimestamp > 1)
c.roomIDToMetadata[roomID] = &metadata
}
return nil

11
v3.go
View File

@ -44,11 +44,20 @@ func allowCORS(next http.Handler) http.HandlerFunc {
}
// RunSyncV3Server is the main entry point to the server
func RunSyncV3Server(h http.Handler, bindAddr string) {
func RunSyncV3Server(h http.Handler, bindAddr, destV2Server string) {
// HTTP path routing
r := mux.NewRouter()
r.Handle("/_matrix/client/v3/sync", allowCORS(h))
r.Handle("/_matrix/client/unstable/org.matrix.msc3575/sync", allowCORS(h))
serverJSON, _ := json.Marshal(struct {
Server string `json:"server"`
}{destV2Server})
r.Handle("/client/server.json", allowCORS(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(200)
rw.Write(serverJSON)
})))
r.PathPrefix("/client/").HandlerFunc(
allowCORS(
http.StripPrefix("/client/", http.FileServer(http.Dir("./client"))),