1package gomatrix 2 3// Room represents a single Matrix room. 4type Room struct { 5 ID string 6 State map[string]map[string]*Event 7} 8 9// PublicRoom represents the information about a public room obtainable from the room directory 10type PublicRoom struct { 11 CanonicalAlias string `json:"canonical_alias"` 12 Name string `json:"name"` 13 WorldReadable bool `json:"world_readable"` 14 Topic string `json:"topic"` 15 NumJoinedMembers int `json:"num_joined_members"` 16 AvatarURL string `json:"avatar_url"` 17 RoomID string `json:"room_id"` 18 GuestCanJoin bool `json:"guest_can_join"` 19 Aliases []string `json:"aliases"` 20} 21 22// UpdateState updates the room's current state with the given Event. This will clobber events based 23// on the type/state_key combination. 24func (room Room) UpdateState(event *Event) { 25 _, exists := room.State[event.Type] 26 if !exists { 27 room.State[event.Type] = make(map[string]*Event) 28 } 29 room.State[event.Type][*event.StateKey] = event 30} 31 32// GetStateEvent returns the state event for the given type/state_key combo, or nil. 33func (room Room) GetStateEvent(eventType string, stateKey string) *Event { 34 stateEventMap := room.State[eventType] 35 event := stateEventMap[stateKey] 36 return event 37} 38 39// GetMembershipState returns the membership state of the given user ID in this room. If there is 40// no entry for this member, 'leave' is returned for consistency with left users. 41func (room Room) GetMembershipState(userID string) string { 42 state := "leave" 43 event := room.GetStateEvent("m.room.member", userID) 44 if event != nil { 45 membershipState, found := event.Content["membership"] 46 if found { 47 mState, isString := membershipState.(string) 48 if isString { 49 state = mState 50 } 51 } 52 } 53 return state 54} 55 56// NewRoom creates a new Room with the given ID 57func NewRoom(roomID string) *Room { 58 // Init the State map and return a pointer to the Room 59 return &Room{ 60 ID: roomID, 61 State: make(map[string]map[string]*Event), 62 } 63} 64