mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 09:23:09 -06:00
Merge account data retrieval functions
This commit is contained in:
parent
90ff5da293
commit
620d11ea21
|
|
@ -41,19 +41,15 @@ const insertAccountDataSQL = `
|
||||||
ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = EXCLUDED.content
|
ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = EXCLUDED.content
|
||||||
`
|
`
|
||||||
|
|
||||||
const selectGlobalAccountDataSQL = "" +
|
const selectAccountDataSQL = "" +
|
||||||
"SELECT type, content FROM account_data WHERE localpart = $1 AND room_id = ''"
|
|
||||||
|
|
||||||
const selectRoomAccountDataSQL = "" +
|
|
||||||
"SELECT type, content FROM account_data WHERE localpart = $1 AND room_id = $2"
|
"SELECT type, content FROM account_data WHERE localpart = $1 AND room_id = $2"
|
||||||
|
|
||||||
const deleteAccountDataSQL = "" +
|
const deleteAccountDataSQL = "" +
|
||||||
"DELETE FROM account_data WHERE localpart = $1 AND room_id = $2 AND type = $3"
|
"DELETE FROM account_data WHERE localpart = $1 AND room_id = $2 AND type = $3"
|
||||||
|
|
||||||
type accountDataStatements struct {
|
type accountDataStatements struct {
|
||||||
insertAccountDataStmt *sql.Stmt
|
insertAccountDataStmt *sql.Stmt
|
||||||
selectGlobalAccountDataStmt *sql.Stmt
|
selectAccountDataStmt *sql.Stmt
|
||||||
selectRoomAccountDataStmt *sql.Stmt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
|
func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
|
@ -64,10 +60,7 @@ func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
|
||||||
if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil {
|
if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if s.selectGlobalAccountDataStmt, err = db.Prepare(selectGlobalAccountDataSQL); err != nil {
|
if s.selectAccountDataStmt, err = db.Prepare(selectAccountDataSQL); err != nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.selectRoomAccountDataStmt, err = db.Prepare(selectRoomAccountDataSQL); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -78,36 +71,10 @@ func (s *accountDataStatements) insertAccountData(localpart string, roomID strin
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *accountDataStatements) selectGlobalAccountData(localpart string) ([]gomatrixserverlib.ClientEvent, error) {
|
func (s *accountDataStatements) selectAccountData(localpart string, roomID string) ([]gomatrixserverlib.ClientEvent, error) {
|
||||||
events := []gomatrixserverlib.ClientEvent{}
|
events := []gomatrixserverlib.ClientEvent{}
|
||||||
|
|
||||||
rows, err := s.selectGlobalAccountDataStmt.Query(localpart)
|
rows, err := s.selectAccountDataStmt.Query(localpart, roomID)
|
||||||
if err != nil {
|
|
||||||
return events, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var dataType string
|
|
||||||
var content []byte
|
|
||||||
|
|
||||||
if err := rows.Scan(&dataType, &content); err != nil && err != sql.ErrNoRows {
|
|
||||||
return events, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ac := gomatrixserverlib.ClientEvent{
|
|
||||||
Type: dataType,
|
|
||||||
Content: content,
|
|
||||||
}
|
|
||||||
events = append(events, ac)
|
|
||||||
}
|
|
||||||
|
|
||||||
return events, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *accountDataStatements) selectRoomAccountData(localpart string, roomID string) ([]gomatrixserverlib.ClientEvent, error) {
|
|
||||||
events := []gomatrixserverlib.ClientEvent{}
|
|
||||||
|
|
||||||
rows, err := s.selectRoomAccountDataStmt.Query(localpart, roomID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return events, err
|
return events, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,10 +206,7 @@ func (d *Database) SaveAccountData(localpart string, roomID string, dataType str
|
||||||
// If no account data could be found, returns an empty arrays
|
// If no account data could be found, returns an empty arrays
|
||||||
// Returns an error if there was an issue with the retrieval
|
// Returns an error if there was an issue with the retrieval
|
||||||
func (d *Database) GetAccountData(localpart string, roomID string) ([]gomatrixserverlib.ClientEvent, error) {
|
func (d *Database) GetAccountData(localpart string, roomID string) ([]gomatrixserverlib.ClientEvent, error) {
|
||||||
if len(roomID) > 0 {
|
return d.accountDatas.selectAccountData(localpart, roomID)
|
||||||
return d.accountDatas.selectRoomAccountData(localpart, roomID)
|
|
||||||
}
|
|
||||||
return d.accountDatas.selectGlobalAccountData(localpart)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hashPassword(plaintext string) (hash string, err error) {
|
func hashPassword(plaintext string) (hash string, err error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue