From 40be8420edabde4c3c9950d4f0bfe4200efaae8f Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 21 Aug 2017 15:55:39 +0100 Subject: [PATCH] Remove the functions and attributes used before the new query API --- .../clientapi/auth/authtypes/membership.go | 7 +- .../auth/storage/accounts/membership_table.go | 66 ++----------------- .../auth/storage/accounts/storage.go | 40 ++--------- 3 files changed, 15 insertions(+), 98 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/membership.go b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/membership.go index f00bd24da..ad5312db6 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/membership.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/membership.go @@ -17,8 +17,7 @@ package authtypes // Membership represents the relationship between a user and a room they're a // member of type Membership struct { - Localpart string - RoomID string - EventID string - StillInRoom bool + Localpart string + RoomID string + EventID string } diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/membership_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/membership_table.go index 5a7878706..4219e4dab 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/membership_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/membership_table.go @@ -30,11 +30,6 @@ CREATE TABLE IF NOT EXISTS account_memberships ( room_id TEXT NOT NULL, -- The ID of the join membership event event_id TEXT NOT NULL, - -- Is the user still in the room? We use a boolean instead of just deleting - -- the row to know if the user has been in the room at some point - -- If set to false, event_id would be the event ID of the leave/kick/ban - -- membership event - still_in_room BOOLEAN NOT NULL, -- A user can only be member of a room once PRIMARY KEY (localpart, room_id) @@ -45,34 +40,24 @@ CREATE UNIQUE INDEX IF NOT EXISTS account_membership_event_id ON account_members ` const insertMembershipSQL = ` - INSERT INTO account_memberships(localpart, room_id, event_id, still_in_room) - VALUES ($1, $2, $3, $4) - ON CONFLICT (localpart, room_id) DO - UPDATE SET event_id = EXCLUDED.event_id, still_in_room = EXCLUDED.still_in_room + INSERT INTO account_memberships(localpart, room_id, event_id) VALUES ($1, $2, $3) + ON CONFLICT (localpart, room_id) DO UPDATE SET event_id = EXCLUDED.event_id ` -const selectMembershipSQL = "" + - "SELECT event_id, still_in_room from account_memberships WHERE localpart = $1 AND room_id = $2" - const selectMembershipsByLocalpartSQL = "" + - "SELECT room_id, event_id FROM account_memberships WHERE localpart = $1 AND still_in_room = true" - -const selectMembershipsByRoomIDSQL = "" + - "SELECT localpart, event_id FROM account_memberships WHERE room_id = $1 AND still_in_room = true" + "SELECT * FROM account_memberships WHERE localpart = $1" const deleteMembershipsByEventIDsSQL = "" + "DELETE FROM account_memberships WHERE event_id = ANY($1)" const updateMembershipByEventIDSQL = "" + - "UPDATE account_memberships SET event_id = $2, still_in_room = $3 WHERE event_id = $1" + "UPDATE account_memberships SET event_id = $2 WHERE event_id = $1" type membershipStatements struct { deleteMembershipsByEventIDsStmt *sql.Stmt insertMembershipStmt *sql.Stmt - selectMembershipStmt *sql.Stmt selectMembershipByEventIDStmt *sql.Stmt selectMembershipsByLocalpartStmt *sql.Stmt - selectMembershipsByRoomIDStmt *sql.Stmt updateMembershipByEventIDStmt *sql.Stmt } @@ -87,23 +72,17 @@ func (s *membershipStatements) prepare(db *sql.DB) (err error) { if s.insertMembershipStmt, err = db.Prepare(insertMembershipSQL); err != nil { return } - if s.selectMembershipStmt, err = db.Prepare(selectMembershipSQL); err != nil { - return - } if s.selectMembershipsByLocalpartStmt, err = db.Prepare(selectMembershipsByLocalpartSQL); err != nil { return } - if s.selectMembershipsByRoomIDStmt, err = db.Prepare(selectMembershipsByRoomIDSQL); err != nil { - return - } if s.updateMembershipByEventIDStmt, err = db.Prepare(updateMembershipByEventIDSQL); err != nil { return } return } -func (s *membershipStatements) insertMembership(localpart string, roomID string, eventID string, stillInRoom bool, txn *sql.Tx) (err error) { - _, err = txn.Stmt(s.insertMembershipStmt).Exec(localpart, roomID, eventID, stillInRoom) +func (s *membershipStatements) insertMembership(localpart string, roomID string, eventID string, txn *sql.Tx) (err error) { + _, err = txn.Stmt(s.insertMembershipStmt).Exec(localpart, roomID, eventID) return } @@ -112,18 +91,6 @@ func (s *membershipStatements) deleteMembershipsByEventIDs(eventIDs []string, tx return } -func (s *membershipStatements) selectMembership(localpart string, roomID string) (*authtypes.Membership, error) { - m := authtypes.Membership{ - Localpart: localpart, - RoomID: roomID, - } - err := s.selectMembershipStmt.QueryRow(localpart, roomID).Scan(&m.EventID, &m.StillInRoom) - if err == sql.ErrNoRows { - return nil, nil - } - return &m, err -} - func (s *membershipStatements) selectMembershipsByLocalpart(localpart string) (memberships []authtypes.Membership, err error) { rows, err := s.selectMembershipsByLocalpartStmt.Query(localpart) if err != nil { @@ -145,27 +112,6 @@ func (s *membershipStatements) selectMembershipsByLocalpart(localpart string) (m return } -func (s *membershipStatements) selectMembershipsByRoomID(roomID string) (memberships []authtypes.Membership, err error) { - rows, err := s.selectMembershipsByRoomIDStmt.Query(roomID) - if err != nil { - return - } - - memberships = []authtypes.Membership{} - - defer rows.Close() - for rows.Next() { - var m authtypes.Membership - m.RoomID = roomID - if err := rows.Scan(&m.Localpart, &m.EventID); err != nil { - return nil, err - } - memberships = append(memberships, m) - } - - return -} - func (s *membershipStatements) updateMembershipByEventID(oldEventID string, newEventID string) (err error) { _, err = s.updateMembershipByEventIDStmt.Exec(oldEventID, newEventID) return diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index 030fae25e..76b9a4dd9 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -125,8 +125,8 @@ func (d *Database) SetPartitionOffset(topic string, partition int32, offset int6 // is still in the room. // If a membership already exists between the user and the room, or of the // insert fails, returns the SQL error -func (d *Database) SaveMembership(localpart string, roomID string, eventID string, stillInRoom bool, txn *sql.Tx) error { - return d.memberships.insertMembership(localpart, roomID, eventID, stillInRoom, txn) +func (d *Database) SaveMembership(localpart string, roomID string, eventID string, txn *sql.Tx) error { + return d.memberships.insertMembership(localpart, roomID, eventID, txn) } // removeMembershipsByEventIDs removes the memberships of which the `join` membership @@ -136,9 +136,9 @@ func (d *Database) removeMembershipsByEventIDs(eventIDs []string, txn *sql.Tx) e return d.memberships.deleteMembershipsByEventIDs(eventIDs, txn) } -// UpdateMemberships saves the membership events included in a given state events -// array, and removes those which ID is included in a given array of events IDs. -// All of the process is run in a transaction, which commits only once/if every +// UpdateMemberships adds the "join" membership events included in a given state +// events array, and removes those which ID is included in a given array of events +// IDs. All of the process is run in a transaction, which commits only once/if every // insertion and deletion has been successfully processed. // Returns a SQL error if there was an issue with any part of the process func (d *Database) UpdateMemberships(eventsToAdd []gomatrixserverlib.Event, idsToRemove []string) error { @@ -157,13 +157,6 @@ func (d *Database) UpdateMemberships(eventsToAdd []gomatrixserverlib.Event, idsT }) } -// GetMembership returns the membership for the given localpart and room ID -// If no membership match the given localpart, returns nil -// If there was an issue during the retrieval, returns the SQL error -func (d *Database) GetMembership(localpart string, roomID string) (*authtypes.Membership, error) { - return d.memberships.selectMembership(localpart, roomID) -} - // GetMembershipsByLocalpart returns an array containing the memberships for all // the rooms a user matching a given localpart is a member of // If no membership match the given localpart, returns an empty array @@ -172,21 +165,6 @@ func (d *Database) GetMembershipsByLocalpart(localpart string) (memberships []au return d.memberships.selectMembershipsByLocalpart(localpart) } -// GetMembershipsByRoomID returns an array containing the memberships for all -// the users that are member of a given room -// If no membership match the given localpart, returns an empty array -// If there was an issue during the retrieval, returns the SQL error -func (d *Database) GetMembershipsByRoomID(roomID string) (memberships []authtypes.Membership, err error) { - return d.memberships.selectMembershipsByRoomID(roomID) -} - -// UpdateMembership update the "join" membership event ID of a membership. -// This is useful in case of membership upgrade (e.g. profile update) -// If there was an issue during the update, returns the SQL error -func (d *Database) UpdateMembership(oldEventID string, newEventID string) error { - return d.memberships.updateMembershipByEventID(oldEventID, newEventID) -} - // newMembership will save a new membership in the database, with a flag on whether // the user is still in the room. This flag is set to true if the given state // event is a "join" membership event and false if the event is a "leave" or "ban" @@ -215,13 +193,7 @@ func (d *Database) newMembership(ev gomatrixserverlib.Event, txn *sql.Tx) error // Only "join" membership events can be considered as new memberships if membership == "join" { - if err := d.SaveMembership(localpart, roomID, eventID, true, txn); err != nil { - return err - } - } else if membership == "leave" || membership == "ban" { - // If the user left the room, save the new event ID and mark them as - // not in the room anymore - if err := d.SaveMembership(localpart, roomID, eventID, false, txn); err != nil { + if err := d.SaveMembership(localpart, roomID, eventID, txn); err != nil { return err } }