aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-04-24 22:58:30 +0300
committerGitHub <noreply@github.com>2018-04-24 22:58:30 +0300
commit7026ed99a3ef4071a35e74d2d5f9026f6848be92 (patch)
tree33332880db2cbdaeabaeb4e1ddbf94a3f15686a3 /matrix
parent49cd74c548e3baf259fce9270af3ecca5bdb625e (diff)
parentd7d654e2ec0a1d001f936b1575a3e6af3973a874 (diff)
Merge pull request #33 from tulir/initial-sync
Use initial sync data instead of fetching room list, state, history, etc manually
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go57
-rw-r--r--matrix/rooms/room.go10
-rw-r--r--matrix/sync.go31
3 files changed, 56 insertions, 42 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go
index d58ce20..2e97ef4 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -129,6 +129,7 @@ func (c *Container) Login(user, password string) error {
// Stop stops the Matrix syncer.
func (c *Container) Stop() {
if c.running {
+ debug.Print("Stopping Matrix container...")
c.stop <- true
c.client.StopSync()
}
@@ -157,22 +158,6 @@ func (c *Container) PushRules() *pushrules.PushRuleset {
return c.config.Session.PushRules
}
-// UpdateRoomList fetches the list of rooms the user has joined and sends them to the UI.
-func (c *Container) UpdateRoomList() {
- resp, err := c.client.JoinedRooms()
- if err != nil {
- respErr, _ := err.(gomatrix.HTTPError).WrappedError.(gomatrix.RespError)
- if respErr.ErrCode == "M_UNKNOWN_TOKEN" {
- c.OnLogout()
- return
- }
- debug.Print("Error fetching room list:", err)
- return
- }
-
- c.ui.MainView().SetRooms(resp.JoinedRooms)
-}
-
// OnLogout stops the syncer and moves the UI back to the login view.
func (c *Container) OnLogout() {
c.Stop()
@@ -183,15 +168,23 @@ func (c *Container) OnLogout() {
func (c *Container) OnLogin() {
c.client.Store = c.config.Session
+ debug.Print("Initializing syncer")
c.syncer = NewGomuksSyncer(c.config.Session)
c.syncer.OnEventType("m.room.message", c.HandleMessage)
c.syncer.OnEventType("m.room.member", c.HandleMembership)
c.syncer.OnEventType("m.typing", c.HandleTyping)
c.syncer.OnEventType("m.push_rules", c.HandlePushRules)
c.syncer.OnEventType("m.tag", c.HandleTag)
+ c.syncer.InitDoneCallback = func() {
+ c.config.Session.InitialSyncDone = true
+ c.ui.Render()
+ }
c.client.Syncer = c.syncer
- c.UpdateRoomList()
+ debug.Print("Setting existing rooms")
+ c.ui.MainView().SetRooms(c.config.Session.Rooms)
+
+ debug.Print("OnLogin() done.")
}
// Start moves the UI to the main view, calls OnLogin() and runs the syncer forever until stopped with Stop()
@@ -226,19 +219,23 @@ func (c *Container) Start() {
// HandleMessage is the event handler for the m.room.message timeline event.
func (c *Container) HandleMessage(evt *gomatrix.Event) {
mainView := c.ui.MainView()
+
roomView := mainView.GetRoom(evt.RoomID)
if roomView == nil {
+ debug.Printf("Failed to handle event %v: No room view found.", evt)
return
}
message := mainView.ParseEvent(roomView, evt)
if message != nil {
+ roomView.AddMessage(message, ifc.AppendMessage)
if c.syncer.FirstSyncDone {
pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should()
mainView.NotifyMessage(roomView.MxRoom(), message, pushRules)
+ c.ui.Render()
}
- roomView.AddMessage(message, ifc.AppendMessage)
- c.ui.Render()
+ } else {
+ debug.Printf("Parsing event %v failed (ParseEvent() returned nil).", evt)
}
}
@@ -279,6 +276,7 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
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
}
@@ -287,6 +285,9 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
c.ui.MainView().AddRoom(evt.RoomID)
case "leave":
c.ui.MainView().RemoveRoom(evt.RoomID)
+ case "invite":
+ // TODO handle
+ debug.Printf("%s invited the user to %s", evt.Sender, evt.RoomID)
}
}
@@ -296,6 +297,11 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) {
c.processOwnMembershipChange(evt)
}
+ if !c.config.Session.InitialSyncDone /*&& evt.Timestamp < time.Now().Add(-1*time.Hour).Unix()*/ {
+ // We don't care about other users' membership events in the initial sync.
+ return
+ }
+
mainView := c.ui.MainView()
roomView := mainView.GetRoom(evt.RoomID)
if roomView == nil {
@@ -305,16 +311,17 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) {
message := mainView.ParseEvent(roomView, evt)
if message != nil {
// TODO this shouldn't be necessary
- roomView.MxRoom().UpdateState(evt)
+ //roomView.MxRoom().UpdateState(evt)
// TODO This should probably also be in a different place
- roomView.UpdateUserList()
+ //roomView.UpdateUserList()
+ roomView.AddMessage(message, ifc.AppendMessage)
+ // 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()
}
- roomView.AddMessage(message, ifc.AppendMessage)
- c.ui.Render()
}
}
@@ -471,12 +478,12 @@ func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.
func (c *Container) GetRoom(roomID string) *rooms.Room {
room := c.config.Session.GetRoom(roomID)
if room != nil && len(room.State) == 0 {
- events := c.getState(room.ID)
+ /*events := c.getState(room.ID)
if events != nil {
for _, event := range events {
room.UpdateState(event)
}
- }
+ }*/
}
return room
}
diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go
index 44a386b..95349c3 100644
--- a/matrix/rooms/room.go
+++ b/matrix/rooms/room.go
@@ -23,6 +23,7 @@ import (
"time"
"maunium.net/go/gomatrix"
+ "maunium.net/go/gomuks/debug"
)
type RoomNameSource int
@@ -148,6 +149,15 @@ func (room *Room) UpdateState(event *gomatrix.Event) {
case "m.room.topic":
room.topicCache = ""
}
+
+ stateKey := ""
+ if event.StateKey != nil {
+ stateKey = *event.StateKey
+ }
+ if event.Type != "m.room.member" {
+ debug.Printf("Updating state %s#%s for %s", event.Type, stateKey, room.ID)
+ }
+
if event.StateKey == nil {
room.State[event.Type][""] = event
} else {
diff --git a/matrix/sync.go b/matrix/sync.go
index f3966cb..8ad7ad5 100644
--- a/matrix/sync.go
+++ b/matrix/sync.go
@@ -20,8 +20,6 @@ package matrix
import (
"encoding/json"
- "fmt"
- "runtime/debug"
"time"
"maunium.net/go/gomatrix"
@@ -37,9 +35,10 @@ type SyncerSession interface {
// replace parts of this default syncer (e.g. the ProcessResponse method). The default syncer uses the observer
// pattern to notify callers about incoming events. See GomuksSyncer.OnEventType for more information.
type GomuksSyncer struct {
- Session SyncerSession
- listeners map[string][]gomatrix.OnEventListener // event type to listeners array
- FirstSyncDone bool
+ Session SyncerSession
+ listeners map[string][]gomatrix.OnEventListener // event type to listeners array
+ FirstSyncDone bool
+ InitDoneCallback func()
}
// NewGomuksSyncer returns an instantiated GomuksSyncer
@@ -53,17 +52,6 @@ func NewGomuksSyncer(session SyncerSession) *GomuksSyncer {
// ProcessResponse processes a Matrix sync response.
func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (err error) {
- if len(since) == 0 {
- return
- }
- // debug.Print("Processing sync response", since, res)
-
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("ProcessResponse for %s since %s panicked: %s\n%s", s.Session.GetUserID(), since, r, debug.Stack())
- }
- }()
-
s.processSyncEvents(nil, res.Presence.Events, false, false)
s.processSyncEvents(nil, res.AccountData.Events, false, false)
@@ -93,6 +81,9 @@ func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
}
}
+ if since == "" && s.InitDoneCallback != nil {
+ s.InitDoneCallback()
+ }
s.FirstSyncDone = true
return
@@ -147,7 +138,13 @@ func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage {
"room": {
"include_leave": true,
"state": {
- "types": ["m.room.member"]
+ "types": [
+ "m.room.member",
+ "m.room.name",
+ "m.room.topic",
+ "m.room.canonical_alias",
+ "m.room.aliases"
+ ]
},
"timeline": {
"types": ["m.room.message"],