Support visibility update and retrieval

This commit is contained in:
Brendan Abolivier 2017-08-15 17:24:32 +01:00
parent 4ff7c33e05
commit 9a96f94918
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
2 changed files with 29 additions and 2 deletions

View file

@ -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

View file

@ -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) {