Matrix-Nachrichtenabruf auf /rooms/{roomId}/messages umstellen
This commit is contained in:
@@ -48,8 +48,12 @@ func newMatrixClientFromEnv() *MatrixClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *MatrixClient) FetchRecentMessages(ctx context.Context) ([]MatrixMessage, error) {
|
func (m *MatrixClient) FetchRecentMessages(ctx context.Context) ([]MatrixMessage, error) {
|
||||||
filter := fmt.Sprintf(`{"room":{"rooms":["%s"],"timeline":{"limit":%d}}}`, m.RoomID, m.Limit)
|
endpoint := fmt.Sprintf(
|
||||||
endpoint := m.HomeserverURL + "/_matrix/client/v3/sync?timeout=0&filter=" + url.QueryEscape(filter)
|
"%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)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||||
if err != 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))
|
return nil, errors.New("matrix sync failed: " + strconv.Itoa(resp.StatusCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
var syncResp struct {
|
var messagesResp struct {
|
||||||
Rooms struct {
|
Chunk []struct {
|
||||||
Join map[string]struct {
|
Type string `json:"type"`
|
||||||
Timeline struct {
|
Sender string `json:"sender"`
|
||||||
Events []struct {
|
OriginServerTS int64 `json:"origin_server_ts"`
|
||||||
Type string `json:"type"`
|
Content struct {
|
||||||
Sender string `json:"sender"`
|
MsgType string `json:"msgtype"`
|
||||||
OriginServerTS int64 `json:"origin_server_ts"`
|
Body string `json:"body"`
|
||||||
Content struct {
|
} `json:"content"`
|
||||||
MsgType string `json:"msgtype"`
|
} `json:"chunk"`
|
||||||
Body string `json:"body"`
|
|
||||||
} `json:"content"`
|
|
||||||
} `json:"events"`
|
|
||||||
} `json:"timeline"`
|
|
||||||
} `json:"join"`
|
|
||||||
} `json:"rooms"`
|
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&syncResp); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&messagesResp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(messagesResp.Chunk) == 0 {
|
||||||
room, ok := syncResp.Rooms.Join[m.RoomID]
|
|
||||||
if !ok {
|
|
||||||
return []MatrixMessage{}, nil
|
return []MatrixMessage{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
out := make([]MatrixMessage, 0, len(room.Timeline.Events))
|
out := make([]MatrixMessage, 0, len(messagesResp.Chunk))
|
||||||
for _, ev := range room.Timeline.Events {
|
for i := len(messagesResp.Chunk) - 1; i >= 0; i-- {
|
||||||
if ev.Type != "m.room.message" {
|
ev := messagesResp.Chunk[i]
|
||||||
continue
|
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
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user