diff --git a/roomserver/storage/postgres/event_json_table.go b/roomserver/storage/postgres/event_json_table.go index 7454ca0794..312fcaa378 100644 --- a/roomserver/storage/postgres/event_json_table.go +++ b/roomserver/storage/postgres/event_json_table.go @@ -54,63 +54,61 @@ const bulkSelectEventJSONSQL = "" + " WHERE event_nid = ANY($1)" + " ORDER BY event_nid ASC" -const selectRoomsUnderSpaceSQL = "" + - "WITH room_ids AS (" + - " SELECT DISTINCT" + - " (REGEXP_MATCHES(event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " FROM roomserver_event_json" + - " WHERE event_json LIKE '%\"state_key\":\"$1\"%'" + - " AND event_json LIKE '%\"type\":\"m.space.parent\"%'" + - ")," + - "dm_rooms AS (" + - " SELECT" + - " ARRAY_AGG(DISTINCT r.room_id) AS dm_array" + - " FROM roomserver_event_json e" + - " CROSS JOIN LATERAL (" + - " SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " ) AS r" + - " WHERE e.event_json LIKE '%\"is_direct\":true%'" + - " AND r.room_id = ANY (" + - " SELECT room_id FROM room_ids" + - " )" + - ")," + - "operation_rooms AS (" + - " SELECT" + - " ARRAY_AGG(DISTINCT r.room_id) AS operation_array" + - " FROM roomserver_event_json e" + - " CROSS JOIN LATERAL (" + - " SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " ) AS r" + - " WHERE e.event_json LIKE '%\"type\":\"connect.operation\"%'" + - " AND r.room_id = ANY (" + - " SELECT room_id FROM room_ids" + - " )" + - ")," + - "team_rooms AS (" + - " SELECT" + - " ARRAY_AGG(DISTINCT r.room_id) AS team_array" + - " FROM roomserver_event_json e" + - " CROSS JOIN LATERAL (" + - " SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " ) AS r" + - " WHERE r.room_id = ANY (" + - " SELECT room_id FROM room_ids" + - " )" + - " AND r.room_id NOT IN (" + - " SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms" + - " )" + - " AND r.room_id NOT IN (" + - " SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms" + - " )" + - ")" + - "SELECT" + - " dm_rooms.dm_array," + - " operation_rooms.operation_array," + - " team_rooms.team_array" + - "FROM" + - " dm_rooms," + - " operation_rooms," + - " team_rooms;" +const selectRoomsUnderSpaceSQL = ` + WITH room_ids AS ( + SELECT DISTINCT + (REGEXP_MATCHES(event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + FROM roomserver_event_json + WHERE event_json LIKE '%"state_key":"$1"%' + AND event_json LIKE '%"type":"m.space.parent"%' + ), + dm_rooms AS ( + SELECT + ARRAY_AGG(DISTINCT r.room_id) AS dm_array + FROM roomserver_event_json e + CROSS JOIN LATERAL ( + SELECT (REGEXP_MATCHES(e.event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + ) AS r + WHERE e.event_json LIKE '%"is_direct":true%' + AND r.room_id = ANY ( + SELECT room_id FROM room_ids + ) + ), + operation_rooms AS ( + SELECT + ARRAY_AGG(DISTINCT r.room_id) AS operation_array + FROM roomserver_event_json e + CROSS JOIN LATERAL ( + SELECT (REGEXP_MATCHES(e.event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + ) AS r + WHERE e.event_json LIKE '%"type":"connect.operation"%' + AND r.room_id = ANY ( + SELECT room_id FROM room_ids + ) + ), + team_rooms AS ( + SELECT + ARRAY_AGG(DISTINCT r.room_id) AS team_array + FROM roomserver_event_json e + CROSS JOIN LATERAL ( + SELECT (REGEXP_MATCHES(e.event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + ) AS r + WHERE r.room_id = ANY ( + SELECT room_id FROM room_ids) + AND r.room_id NOT IN ( + SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms) + AND r.room_id NOT IN ( + SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms) + ) + SELECT + dm_rooms.dm_array, + operation_rooms.operation_array, + team_rooms.team_array + FROM + dm_rooms, + operation_rooms, + team_rooms +` type eventJSONStatements struct { insertEventJSONStmt *sql.Stmt diff --git a/roomserver/storage/sqlite3/event_json_table.go b/roomserver/storage/sqlite3/event_json_table.go index 42c2179b1c..0ecf53ca21 100644 --- a/roomserver/storage/sqlite3/event_json_table.go +++ b/roomserver/storage/sqlite3/event_json_table.go @@ -46,63 +46,61 @@ const bulkSelectEventJSONSQL = ` ORDER BY event_nid ASC ` -const selectRoomsUnderSpaceSQL = "" + - "WITH room_ids AS (" + - " SELECT DISTINCT" + - " (REGEXP_MATCHES(event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " FROM roomserver_event_json" + - " WHERE event_json LIKE '%\"state_key\":\"$1\"%'" + - " AND event_json LIKE '%\"type\":\"m.space.parent\"%'" + - ")," + - "dm_rooms AS (" + - " SELECT" + - " ARRAY_AGG(DISTINCT r.room_id) AS dm_array" + - " FROM roomserver_event_json e" + - " CROSS JOIN LATERAL (" + - " SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " ) AS r" + - " WHERE e.event_json LIKE '%\"is_direct\":true%'" + - " AND r.room_id = ANY (" + - " SELECT room_id FROM room_ids" + - " )" + - ")," + - "operation_rooms AS (" + - " SELECT" + - " ARRAY_AGG(DISTINCT r.room_id) AS operation_array" + - " FROM roomserver_event_json e" + - " CROSS JOIN LATERAL (" + - " SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " ) AS r" + - " WHERE e.event_json LIKE '%\"type\":\"connect.operation\"%'" + - " AND r.room_id = ANY (" + - " SELECT room_id FROM room_ids" + - " )" + - ")," + - "team_rooms AS (" + - " SELECT" + - " ARRAY_AGG(DISTINCT r.room_id) AS team_array" + - " FROM roomserver_event_json e" + - " CROSS JOIN LATERAL (" + - " SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" + - " ) AS r" + - " WHERE r.room_id = ANY (" + - " SELECT room_id FROM room_ids" + - " )" + - " AND r.room_id NOT IN (" + - " SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms" + - " )" + - " AND r.room_id NOT IN (" + - " SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms" + - " )" + - ")" + - "SELECT" + - " dm_rooms.dm_array," + - " operation_rooms.operation_array," + - " team_rooms.team_array" + - "FROM" + - " dm_rooms," + - " operation_rooms," + - " team_rooms;" +const selectRoomsUnderSpaceSQL = ` + WITH room_ids AS ( + SELECT DISTINCT + (REGEXP_MATCHES(event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + FROM roomserver_event_json + WHERE event_json LIKE '%"state_key":"$1"%' + AND event_json LIKE '%"type":"m.space.parent"%' + ), + dm_rooms AS ( + SELECT + ARRAY_AGG(DISTINCT r.room_id) AS dm_array + FROM roomserver_event_json e + CROSS JOIN LATERAL ( + SELECT (REGEXP_MATCHES(e.event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + ) AS r + WHERE e.event_json LIKE '%"is_direct":true%' + AND r.room_id = ANY ( + SELECT room_id FROM room_ids + ) + ), + operation_rooms AS ( + SELECT + ARRAY_AGG(DISTINCT r.room_id) AS operation_array + FROM roomserver_event_json e + CROSS JOIN LATERAL ( + SELECT (REGEXP_MATCHES(e.event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + ) AS r + WHERE e.event_json LIKE '%"type":"connect.operation"%' + AND r.room_id = ANY ( + SELECT room_id FROM room_ids + ) + ), + team_rooms AS ( + SELECT + ARRAY_AGG(DISTINCT r.room_id) AS team_array + FROM roomserver_event_json e + CROSS JOIN LATERAL ( + SELECT (REGEXP_MATCHES(e.event_json, '"room_id":"([^"]+)"'))[1]::text AS room_id + ) AS r + WHERE r.room_id = ANY ( + SELECT room_id FROM room_ids) + AND r.room_id NOT IN ( + SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms) + AND r.room_id NOT IN ( + SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms) + ) + SELECT + dm_rooms.dm_array, + operation_rooms.operation_array, + team_rooms.team_array + FROM + dm_rooms, + operation_rooms, + team_rooms +` type eventJSONStatements struct { db *sql.DB