maubot/vendor/maunium.net/go/gomatrix/room.go

45 lines
1.3 KiB
Go
Raw Normal View History

2018-07-06 13:08:50 +00:00
package gomatrix
// Room represents a single Matrix room.
type Room struct {
ID string
2018-09-19 22:28:37 +00:00
State map[EventType]map[string]*Event
2018-07-06 13:08:50 +00:00
}
// UpdateState updates the room's current state with the given Event. This will clobber events based
// on the type/state_key combination.
func (room Room) UpdateState(event *Event) {
_, exists := room.State[event.Type]
if !exists {
room.State[event.Type] = make(map[string]*Event)
}
room.State[event.Type][*event.StateKey] = event
}
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
2018-09-19 22:28:37 +00:00
func (room Room) GetStateEvent(eventType EventType, stateKey string) *Event {
2018-07-06 13:08:50 +00:00
stateEventMap, _ := room.State[eventType]
event, _ := stateEventMap[stateKey]
return event
}
// 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.
2018-09-19 22:28:37 +00:00
func (room Room) GetMembershipState(userID string) Membership {
state := MembershipLeave
event := room.GetStateEvent(StateMember, userID)
2018-07-06 13:08:50 +00:00
if event != nil {
2018-09-19 22:28:37 +00:00
state = event.Content.Membership
2018-07-06 13:08:50 +00:00
}
return state
}
// NewRoom creates a new Room with the given ID
func NewRoom(roomID string) *Room {
// Init the State map and return a pointer to the Room
return &Room{
ID: roomID,
2018-09-19 22:28:37 +00:00
State: make(map[EventType]map[string]*Event),
2018-07-06 13:08:50 +00:00
}
}