mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-09 15:13:12 -06:00
Update migrations to set "correct" history visibility
This commit is contained in:
parent
56d8a2371f
commit
503afceccb
|
|
@ -16,9 +16,11 @@ package deltas
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
func LoadAddHistoryVisibilityColumn(m *sqlutil.Migrations) {
|
||||
|
|
@ -35,6 +37,41 @@ func UpAddHistoryVisibilityColumn(tx *sql.Tx) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||
}
|
||||
|
||||
// get the current room history visibility
|
||||
rows, err := tx.Query(`SELECT DISTINCT room_id, headered_event_json FROM syncapi_current_room_state
|
||||
WHERE type = 'm.room.history_visibility' AND state_key = '';
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to query current room state: %w", err)
|
||||
}
|
||||
var eventBytes []byte
|
||||
var roomID string
|
||||
var event gomatrixserverlib.HeaderedEvent
|
||||
var hisVis gomatrixserverlib.HistoryVisibility
|
||||
historyVisibilities := make(map[string]gomatrixserverlib.HistoryVisibility)
|
||||
for rows.Next() {
|
||||
if err = rows.Scan(&roomID, &eventBytes); err != nil {
|
||||
return fmt.Errorf("failed to scan row: %w", err)
|
||||
}
|
||||
if err = json.Unmarshal(eventBytes, &event); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal event: %w", err)
|
||||
}
|
||||
historyVisibilities[roomID] = gomatrixserverlib.HistoryVisibilityShared
|
||||
if hisVis, err = event.HistoryVisibility(); err == nil {
|
||||
historyVisibilities[roomID] = hisVis
|
||||
}
|
||||
}
|
||||
|
||||
// update the history visibility
|
||||
for roomID, hisVis = range historyVisibilities {
|
||||
_, err = tx.Exec(`UPDATE syncapi_output_room_events SET history_visibility = $1
|
||||
WHERE type IN ('m.room.message', 'm.room.encrypted') AND room_id = $2`, hisVis, roomID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update history visibility: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,9 +16,11 @@ package deltas
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
func LoadAddHistoryVisibilityColumn(m *sqlutil.Migrations) {
|
||||
|
|
@ -51,6 +53,41 @@ func UpAddHistoryVisibilityColumn(tx *sql.Tx) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||
}
|
||||
|
||||
// get the current room history visibility
|
||||
rows, err := tx.Query(`SELECT DISTINCT room_id, headered_event_json FROM syncapi_current_room_state
|
||||
WHERE type = 'm.room.history_visibility' AND state_key = '';
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to query current room state: %w", err)
|
||||
}
|
||||
var eventBytes []byte
|
||||
var roomID string
|
||||
var event gomatrixserverlib.HeaderedEvent
|
||||
var hisVis gomatrixserverlib.HistoryVisibility
|
||||
historyVisibilities := make(map[string]gomatrixserverlib.HistoryVisibility)
|
||||
for rows.Next() {
|
||||
if err = rows.Scan(&roomID, &eventBytes); err != nil {
|
||||
return fmt.Errorf("failed to scan row: %w", err)
|
||||
}
|
||||
if err = json.Unmarshal(eventBytes, &event); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal event: %w", err)
|
||||
}
|
||||
historyVisibilities[roomID] = gomatrixserverlib.HistoryVisibilityShared
|
||||
if hisVis, err = event.HistoryVisibility(); err == nil {
|
||||
historyVisibilities[roomID] = hisVis
|
||||
}
|
||||
}
|
||||
|
||||
// update the history visibility
|
||||
for roomID, hisVis = range historyVisibilities {
|
||||
_, err = tx.Exec(`UPDATE syncapi_output_room_events SET history_visibility = $1
|
||||
WHERE type IN ('m.room.message', 'm.room.encrypted') AND room_id = $2`, hisVis, roomID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update history visibility: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue