mautrix-go/room.go

55 lines
1.5 KiB
Go
Raw Permalink Normal View History

2018-10-02 04:31:42 -07:00
package mautrix
2020-04-16 06:51:32 -07:00
import (
2020-04-16 07:29:47 -07:00
"maunium.net/go/mautrix/event"
2020-04-16 06:51:32 -07:00
"maunium.net/go/mautrix/id"
)
2021-09-30 06:05:56 -07:00
type RoomStateMap = map[event.Type]map[string]*event.Event
// Room represents a single Matrix room.
type Room struct {
2020-04-16 06:51:32 -07:00
ID id.RoomID
2021-09-30 06:05:56 -07:00
State RoomStateMap
}
// UpdateState updates the room's current state with the given Event. This will clobber events based
// on the type/state_key combination.
2020-04-16 09:10:51 -07:00
func (room Room) UpdateState(evt *event.Event) {
_, exists := room.State[evt.Type]
if !exists {
2020-04-16 09:10:51 -07:00
room.State[evt.Type] = make(map[string]*event.Event)
}
2020-04-16 09:10:51 -07:00
room.State[evt.Type][*evt.StateKey] = evt
}
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
2020-04-16 07:29:47 -07:00
func (room Room) GetStateEvent(eventType event.Type, stateKey string) *event.Event {
stateEventMap, _ := room.State[eventType]
2020-04-16 09:10:51 -07:00
evt, _ := stateEventMap[stateKey]
return evt
}
// GetMembershipState returns the membership state of the given user ID in this room. If there is
// no entry for this member, 'leave' is returned for consistency with left users.
2020-04-16 07:29:47 -07:00
func (room Room) GetMembershipState(userID id.UserID) event.Membership {
state := event.MembershipLeave
2020-04-16 09:10:51 -07:00
evt := room.GetStateEvent(event.StateMember, string(userID))
if evt != nil {
2020-04-18 17:23:27 -07:00
membership, ok := evt.Content.Raw["membership"].(string)
if ok {
state = event.Membership(membership)
}
}
return state
}
// NewRoom creates a new Room with the given ID
2020-04-16 06:51:32 -07:00
func NewRoom(roomID id.RoomID) *Room {
// Init the State map and return a pointer to the Room
return &Room{
ID: roomID,
2021-09-30 06:05:56 -07:00
State: make(RoomStateMap),
}
}