mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 01:13:10 -06:00
Improve the joined members counter computation
This commit is contained in:
parent
fe47ed6051
commit
8ce6334353
|
|
@ -93,31 +93,37 @@ func (d *PublicRoomsServerDatabase) GetPublicRooms(offset int64, limit int16, fi
|
||||||
// UpdateRoomFromEvents iterate over a slice of state events and call
|
// UpdateRoomFromEvents iterate over a slice of state events and call
|
||||||
// UpdateRoomFromEvent on each of them to update the database representation of
|
// UpdateRoomFromEvent on each of them to update the database representation of
|
||||||
// the rooms updated by each event.
|
// the rooms updated by each event.
|
||||||
// The slice of events to remove is used to process membership events.
|
// The slice of events to remove is used to update the number of joined members
|
||||||
|
// for the room in the database.
|
||||||
// If the update triggered by one of the events failed, aborts the process and
|
// If the update triggered by one of the events failed, aborts the process and
|
||||||
// returns an error.
|
// returns an error.
|
||||||
func (d *PublicRoomsServerDatabase) UpdateRoomFromEvents(
|
func (d *PublicRoomsServerDatabase) UpdateRoomFromEvents(
|
||||||
eventsToAdd []gomatrixserverlib.Event, eventsToRemove []gomatrixserverlib.Event,
|
eventsToAdd []gomatrixserverlib.Event, eventsToRemove []gomatrixserverlib.Event,
|
||||||
) error {
|
) error {
|
||||||
for _, event := range eventsToAdd {
|
for _, event := range eventsToAdd {
|
||||||
if err := d.UpdateRoomFromEvent(event, eventsToRemove); err != nil {
|
if err := d.UpdateRoomFromEvent(event); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, event := range eventsToRemove {
|
||||||
|
if event.Type() == "m.room.member" {
|
||||||
|
if err := d.updateNumJoinedUsers(event, true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRoomFromEvent updates the database representation of a room from a Matrix event, by
|
// UpdateRoomFromEvent updates the database representation of a room from a Matrix event, by
|
||||||
// checking the event's type to know which attribute to change and using the event's content
|
// checking the event's type to know which attribute to change and using the event's content
|
||||||
// to define the new value of the attribute.
|
// to define the new value of the attribute.
|
||||||
// The slice of events to remove is used to process membership events.
|
|
||||||
// If the event doesn't match with any property used to compute the public room directory,
|
// If the event doesn't match with any property used to compute the public room directory,
|
||||||
// does nothing.
|
// does nothing.
|
||||||
// If something went wrong during the process, returns an error.
|
// If something went wrong during the process, returns an error.
|
||||||
func (d *PublicRoomsServerDatabase) UpdateRoomFromEvent(
|
func (d *PublicRoomsServerDatabase) UpdateRoomFromEvent(event gomatrixserverlib.Event) error {
|
||||||
event gomatrixserverlib.Event, eventsToRemove []gomatrixserverlib.Event,
|
|
||||||
) error {
|
|
||||||
roomID := event.RoomID()
|
roomID := event.RoomID()
|
||||||
|
|
||||||
// Process the event according to its type
|
// Process the event according to its type
|
||||||
|
|
@ -125,7 +131,7 @@ func (d *PublicRoomsServerDatabase) UpdateRoomFromEvent(
|
||||||
case "m.room.create":
|
case "m.room.create":
|
||||||
return d.statements.insertNewRoom(roomID)
|
return d.statements.insertNewRoom(roomID)
|
||||||
case "m.room.member":
|
case "m.room.member":
|
||||||
return d.updateNumJoinedUsers(event, eventsToRemove, roomID)
|
return d.updateNumJoinedUsers(event, false)
|
||||||
case "m.room.aliases":
|
case "m.room.aliases":
|
||||||
return d.updateRoomAliases(event, roomID)
|
return d.updateRoomAliases(event, roomID)
|
||||||
case "m.room.canonical_alias":
|
case "m.room.canonical_alias":
|
||||||
|
|
@ -167,34 +173,27 @@ func (d *PublicRoomsServerDatabase) UpdateRoomFromEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateNumJoinedUsers updates the number of joined user in the database representation
|
// updateNumJoinedUsers updates the number of joined user in the database representation
|
||||||
// of the room identified by a given room ID, using a given Matrix event.
|
// of the room identified by a given room ID, using a given "m.room.member" Matrix event.
|
||||||
// If the event's membership is "join", increments the value if the event isn't an update,
|
// If the membership property of the event isn't "join", ignores it and returs nil.
|
||||||
// if not, decrements it.
|
// If the remove parameter is set to false, increments the joined members counter in the
|
||||||
|
// database, if set to truem decrements it.
|
||||||
// Returns an error if the update failed.
|
// Returns an error if the update failed.
|
||||||
func (d *PublicRoomsServerDatabase) updateNumJoinedUsers(
|
func (d *PublicRoomsServerDatabase) updateNumJoinedUsers(
|
||||||
membershipEvent gomatrixserverlib.Event, eventsToRemove []gomatrixserverlib.Event, roomID string,
|
membershipEvent gomatrixserverlib.Event, remove bool,
|
||||||
) error {
|
) error {
|
||||||
membership, err := membershipEvent.Membership()
|
membership, err := membershipEvent.Membership()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if membership == "join" {
|
if membership != "join" {
|
||||||
for _, event := range eventsToRemove {
|
|
||||||
if event.Type() == "m.room.member" {
|
|
||||||
// This is an update for an user who already joined the room, most
|
|
||||||
// likely triggered by a profile update. We don't increment the
|
|
||||||
// number of joined member in this case because we already counted
|
|
||||||
// this user.
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if remove {
|
||||||
|
return d.statements.decrementJoinedMembersInRoom(membershipEvent.RoomID())
|
||||||
}
|
}
|
||||||
return d.statements.incrementJoinedMembersInRoom(roomID)
|
return d.statements.incrementJoinedMembersInRoom(membershipEvent.RoomID())
|
||||||
} else if membership == "leave" || membership == "ban" {
|
|
||||||
return d.statements.decrementJoinedMembersInRoom(roomID)
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateStringAttribute updates a given string attribute in the database
|
// updateStringAttribute updates a given string attribute in the database
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue