room.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package gomatrix
  2. // Room represents a single Matrix room.
  3. type Room struct {
  4. ID string
  5. State map[EventType]map[string]*Event
  6. }
  7. // UpdateState updates the room's current state with the given Event. This will clobber events based
  8. // on the type/state_key combination.
  9. func (room Room) UpdateState(event *Event) {
  10. _, exists := room.State[event.Type]
  11. if !exists {
  12. room.State[event.Type] = make(map[string]*Event)
  13. }
  14. room.State[event.Type][*event.StateKey] = event
  15. }
  16. // GetStateEvent returns the state event for the given type/state_key combo, or nil.
  17. func (room Room) GetStateEvent(eventType EventType, stateKey string) *Event {
  18. stateEventMap, _ := room.State[eventType]
  19. event, _ := stateEventMap[stateKey]
  20. return event
  21. }
  22. // GetMembershipState returns the membership state of the given user ID in this room. If there is
  23. // no entry for this member, 'leave' is returned for consistency with left users.
  24. func (room Room) GetMembershipState(userID string) string {
  25. state := "leave"
  26. event := room.GetStateEvent(StateMember, userID)
  27. if event != nil {
  28. state = event.Content.Membership
  29. }
  30. return state
  31. }
  32. // NewRoom creates a new Room with the given ID
  33. func NewRoom(roomID string) *Room {
  34. // Init the State map and return a pointer to the Room
  35. return &Room{
  36. ID: roomID,
  37. State: make(map[EventType]map[string]*Event),
  38. }
  39. }