summary refs log tree commit diff
path: root/vendor/maunium.net/go/mautrix/room.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/maunium.net/go/mautrix/room.go')
-rw-r--r--vendor/maunium.net/go/mautrix/room.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/vendor/maunium.net/go/mautrix/room.go b/vendor/maunium.net/go/mautrix/room.go
new file mode 100644
index 0000000..c3ddb7e
--- /dev/null
+++ b/vendor/maunium.net/go/mautrix/room.go
@@ -0,0 +1,54 @@
+package mautrix
+
+import (
+	"maunium.net/go/mautrix/event"
+	"maunium.net/go/mautrix/id"
+)
+
+type RoomStateMap = map[event.Type]map[string]*event.Event
+
+// Room represents a single Matrix room.
+type Room struct {
+	ID    id.RoomID
+	State RoomStateMap
+}
+
+// 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(evt *event.Event) {
+	_, exists := room.State[evt.Type]
+	if !exists {
+		room.State[evt.Type] = make(map[string]*event.Event)
+	}
+	room.State[evt.Type][*evt.StateKey] = evt
+}
+
+// GetStateEvent returns the state event for the given type/state_key combo, or nil.
+func (room Room) GetStateEvent(eventType event.Type, stateKey string) *event.Event {
+	stateEventMap, _ := room.State[eventType]
+	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.
+func (room Room) GetMembershipState(userID id.UserID) event.Membership {
+	state := event.MembershipLeave
+	evt := room.GetStateEvent(event.StateMember, string(userID))
+	if evt != nil {
+		membership, ok := evt.Content.Raw["membership"].(string)
+		if ok {
+			state = event.Membership(membership)
+		}
+	}
+	return state
+}
+
+// NewRoom creates a new Room with the given ID
+func NewRoom(roomID id.RoomID) *Room {
+	// Init the State map and return a pointer to the Room
+	return &Room{
+		ID:    roomID,
+		State: make(RoomStateMap),
+	}
+}