From 9a96f949185da6025c2c27d8b19ca28b5e75c6b7 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Tue, 15 Aug 2017 17:24:32 +0100 Subject: [PATCH] Support visibility update and retrieval --- .../storage/public_rooms_table.go | 13 +++++++++++++ .../dendrite/publicroomsapi/storage/storage.go | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go b/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go index 805ee39fd..26671b759 100644 --- a/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go +++ b/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go @@ -64,6 +64,10 @@ const selectPublicRoomswithLimitSQL = "" + " ORDER BY joined_members DESC" + " OFFSET $1 LIMIT $2" +const selectRoomVisibilitySQL = "" + + "SELECT visibility FROM publicroomsapi_public_rooms" + + " WHERE room_id = $1" + const insertNewRoomSQL = "" + "INSERT INTO publicroomsapi_public_rooms(room_id)" + " VALUES ($1)" @@ -87,6 +91,7 @@ type publicRoomsStatements struct { countPublicRoomsStmt *sql.Stmt selectPublicRoomsStmt *sql.Stmt selectPublicRoomswithLimitStmt *sql.Stmt + selectRoomVisibilityStmt *sql.Stmt insertNewRoomStmt *sql.Stmt incrementJoinedMembersInRoomStmt *sql.Stmt decrementJoinedMembersInRoomStmt *sql.Stmt @@ -107,6 +112,9 @@ func (s *publicRoomsStatements) prepare(db *sql.DB) (err error) { if s.selectPublicRoomswithLimitStmt, err = db.Prepare(selectPublicRoomswithLimitSQL); err != nil { return } + if s.selectRoomVisibilityStmt, err = db.Prepare(selectRoomVisibilitySQL); err != nil { + return + } if s.insertNewRoomStmt, err = db.Prepare(insertNewRoomSQL); err != nil { return } @@ -165,6 +173,11 @@ func (s *publicRoomsStatements) selectPublicRooms(offset int64, limit int16) ([] return rooms, nil } +func (s *publicRoomsStatements) selectRoomVisibility(roomID string) (v bool, err error) { + err = s.selectRoomVisibilityStmt.QueryRow(roomID).Scan(&v) + return +} + func (s *publicRoomsStatements) insertNewRoom(roomID string) error { _, err := s.insertNewRoomStmt.Exec(roomID) return err diff --git a/src/github.com/matrix-org/dendrite/publicroomsapi/storage/storage.go b/src/github.com/matrix-org/dendrite/publicroomsapi/storage/storage.go index ef27d79d1..d693bc6b8 100644 --- a/src/github.com/matrix-org/dendrite/publicroomsapi/storage/storage.go +++ b/src/github.com/matrix-org/dendrite/publicroomsapi/storage/storage.go @@ -24,7 +24,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" ) -// PublicRoomsServerDatabase represents a public rooms server database +// PublicRoomsServerDatabase represents a public rooms server database. type PublicRoomsServerDatabase struct { db *sql.DB partitions common.PartitionOffsetStatements @@ -33,7 +33,7 @@ type PublicRoomsServerDatabase struct { type attributeValue interface{} -// NewPublicRoomsServerDatabase creates a new public rooms server database +// NewPublicRoomsServerDatabase creates a new public rooms server database. func NewPublicRoomsServerDatabase(dataSourceName string) (*PublicRoomsServerDatabase, error) { var db *sql.DB var err error @@ -51,6 +51,20 @@ func NewPublicRoomsServerDatabase(dataSourceName string) (*PublicRoomsServerData return &PublicRoomsServerDatabase{db, partitions, statements}, nil } +// GetRoomVisibility returns the room visibility as a boolean: true if the room +// is publicly visible, false if not. +// Returns an error if the retrieval failed. +func (d *PublicRoomsServerDatabase) GetRoomVisibility(roomID string) (bool, error) { + return d.statements.selectRoomVisibility(roomID) +} + +// SetRoomVisibility updates the visibility attribute of a room. This attribute +// must be set to true if the room is publicly visible, false if not. +// Returns an error if the update failed. +func (d *PublicRoomsServerDatabase) SetRoomVisibility(visible bool, roomID string) error { + return d.statements.updateRoomAttribute("visibility", visible, roomID) +} + // CountPublicRooms returns the number of room set as publicly visible on the server. // Returns an error if the retrieval failed. func (d *PublicRoomsServerDatabase) CountPublicRooms() (int64, error) {