aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-05-22 17:23:54 +0300
committerTulir Asokan <tulir@maunium.net>2018-05-22 17:23:54 +0300
commitcce79ab7d8481a06054166049876c8a2cbb3418f (patch)
treef796cfc0d3633563d38cc9809fd0c21c524a4dec /matrix
parent09703c6b9c343feca8ee5db263d25f174171339d (diff)
Clean up code
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go98
1 files changed, 41 insertions, 57 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go
index c41975e..264cff8 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -254,6 +254,47 @@ func (c *Container) HandleMessage(source EventSource, evt *gomatrix.Event) {
}
}
+// HandleMembership is the event handler for the m.room.member state event.
+func (c *Container) HandleMembership(source EventSource, evt *gomatrix.Event) {
+ isLeave := source&EventSourceLeave != 0
+ isTimeline := source&EventSourceTimeline != 0
+ isNonTimelineLeave := isLeave && !isTimeline
+ if !c.config.AuthCache.InitialSyncDone && isNonTimelineLeave {
+ return
+ } else if evt.StateKey != nil && *evt.StateKey == c.config.UserID {
+ c.processOwnMembershipChange(evt)
+ } else if !isTimeline && (!c.config.AuthCache.InitialSyncDone || isLeave) {
+ // We don't care about other users' membership events in the initial sync or chats we've left.
+ return
+ }
+
+ c.HandleMessage(source, evt)
+}
+
+func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
+ membership, _ := evt.Content["membership"].(string)
+ prevMembership := "leave"
+ if evt.Unsigned.PrevContent != nil {
+ prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
+ }
+ debug.Printf("Processing own membership change: %s->%s in %s", prevMembership, membership, evt.RoomID)
+ if membership == prevMembership {
+ return
+ }
+ room := c.GetRoom(evt.RoomID)
+ switch membership {
+ case "join":
+ c.ui.MainView().AddRoom(room)
+ room.HasLeft = false
+ case "leave":
+ c.ui.MainView().RemoveRoom(room)
+ room.HasLeft = true
+ case "invite":
+ // TODO handle
+ debug.Printf("%s invited the user to %s", evt.Sender, evt.RoomID)
+ }
+}
+
func (c *Container) parseReadReceipt(evt *gomatrix.Event) (largestTimestampEvent string) {
var largestTimestamp int64
for eventID, rawContent := range evt.Content {
@@ -368,63 +409,6 @@ func (c *Container) HandleTag(source EventSource, evt *gomatrix.Event) {
mainView.UpdateTags(room)
}
-func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
- membership, _ := evt.Content["membership"].(string)
- prevMembership := "leave"
- if evt.Unsigned.PrevContent != nil {
- prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
- }
- debug.Printf("Processing own membership change: %s->%s in %s", prevMembership, membership, evt.RoomID)
- if membership == prevMembership {
- return
- }
- room := c.GetRoom(evt.RoomID)
- switch membership {
- case "join":
- c.ui.MainView().AddRoom(room)
- room.HasLeft = false
- case "leave":
- c.ui.MainView().RemoveRoom(room)
- room.HasLeft = true
- case "invite":
- // TODO handle
- debug.Printf("%s invited the user to %s", evt.Sender, evt.RoomID)
- }
-}
-
-// HandleMembership is the event handler for the m.room.member state event.
-func (c *Container) HandleMembership(source EventSource, evt *gomatrix.Event) {
- isLeave := source&EventSourceLeave != 0
- isTimeline := source&EventSourceTimeline != 0
- isNonTimelineLeave := isLeave && !isTimeline
- if !c.config.AuthCache.InitialSyncDone && isNonTimelineLeave {
- return
- } else if evt.StateKey != nil && *evt.StateKey == c.config.UserID {
- c.processOwnMembershipChange(evt)
- } else if !isTimeline && (!c.config.AuthCache.InitialSyncDone || isLeave) {
- // We don't care about other users' membership events in the initial sync or chats we've left.
- return
- }
-
- mainView := c.ui.MainView()
- roomView := mainView.GetRoom(evt.RoomID)
- if roomView == nil {
- return
- }
-
- message := mainView.ParseEvent(roomView, evt)
- if message != nil {
- roomView.AddMessage(message, ifc.AppendMessage)
- roomView.MxRoom().LastReceivedMessage = message.Timestamp()
- // We don't want notifications at startup.
- if c.syncer.FirstSyncDone {
- pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should()
- mainView.NotifyMessage(roomView.MxRoom(), message, pushRules)
- c.ui.Render()
- }
- }
-}
-
// HandleTyping is the event handler for the m.typing event.
func (c *Container) HandleTyping(source EventSource, evt *gomatrix.Event) {
users := evt.Content["user_ids"].([]interface{})