From 18cbc27ec334eebf3f4c23afdac936b8fd14afa5 Mon Sep 17 00:00:00 2001 From: Kai Date: Tue, 17 Feb 2026 16:22:32 +0100 Subject: [PATCH] Matrix-Nachrichtenabruf auf /rooms/{roomId}/messages umstellen --- cmd/server/matrix.go | 82 +++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/cmd/server/matrix.go b/cmd/server/matrix.go index 8457748..c460685 100644 --- a/cmd/server/matrix.go +++ b/cmd/server/matrix.go @@ -48,8 +48,12 @@ func newMatrixClientFromEnv() *MatrixClient { } func (m *MatrixClient) FetchRecentMessages(ctx context.Context) ([]MatrixMessage, error) { - filter := fmt.Sprintf(`{"room":{"rooms":["%s"],"timeline":{"limit":%d}}}`, m.RoomID, m.Limit) - endpoint := m.HomeserverURL + "/_matrix/client/v3/sync?timeout=0&filter=" + url.QueryEscape(filter) + endpoint := fmt.Sprintf( + "%s/_matrix/client/v3/rooms/%s/messages?dir=b&limit=%d", + m.HomeserverURL, + url.PathEscape(m.RoomID), + m.Limit, + ) req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) if err != nil { @@ -66,50 +70,50 @@ func (m *MatrixClient) FetchRecentMessages(ctx context.Context) ([]MatrixMessage return nil, errors.New("matrix sync failed: " + strconv.Itoa(resp.StatusCode)) } - var syncResp struct { - Rooms struct { - Join map[string]struct { - Timeline struct { - Events []struct { - Type string `json:"type"` - Sender string `json:"sender"` - OriginServerTS int64 `json:"origin_server_ts"` - Content struct { - MsgType string `json:"msgtype"` - Body string `json:"body"` - } `json:"content"` - } `json:"events"` - } `json:"timeline"` - } `json:"join"` - } `json:"rooms"` + var messagesResp struct { + Chunk []struct { + Type string `json:"type"` + Sender string `json:"sender"` + OriginServerTS int64 `json:"origin_server_ts"` + Content struct { + MsgType string `json:"msgtype"` + Body string `json:"body"` + } `json:"content"` + } `json:"chunk"` } - if err := json.NewDecoder(resp.Body).Decode(&syncResp); err != nil { + if err := json.NewDecoder(resp.Body).Decode(&messagesResp); err != nil { return nil, err } - - room, ok := syncResp.Rooms.Join[m.RoomID] - if !ok { + if len(messagesResp.Chunk) == 0 { return []MatrixMessage{}, nil } - out := make([]MatrixMessage, 0, len(room.Timeline.Events)) - for _, ev := range room.Timeline.Events { - if ev.Type != "m.room.message" { - continue + out := make([]MatrixMessage, 0, len(messagesResp.Chunk)) + for i := len(messagesResp.Chunk) - 1; i >= 0; i-- { + ev := messagesResp.Chunk[i] + switch ev.Type { + case "m.room.message": + if ev.Content.MsgType != "m.text" && ev.Content.MsgType != "m.notice" { + continue + } + body := strings.TrimSpace(ev.Content.Body) + if body == "" { + continue + } + ts := time.UnixMilli(ev.OriginServerTS).Local().Format("02.01.2006 15:04") + out = append(out, MatrixMessage{ + Sender: ev.Sender, + Body: body, + Timestamp: ts, + }) + case "m.room.encrypted": + ts := time.UnixMilli(ev.OriginServerTS).Local().Format("02.01.2006 15:04") + out = append(out, MatrixMessage{ + Sender: ev.Sender, + Body: "[Verschlüsselte Nachricht]", + Timestamp: ts, + }) } - if ev.Content.MsgType != "m.text" && ev.Content.MsgType != "m.notice" { - continue - } - body := strings.TrimSpace(ev.Content.Body) - if body == "" { - continue - } - ts := time.UnixMilli(ev.OriginServerTS).Local().Format("02.01.2006 15:04") - out = append(out, MatrixMessage{ - Sender: ev.Sender, - Body: body, - Timestamp: ts, - }) } return out, nil }