From c600ce68a2684fdeebfbe9580c6ff833257450ed Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 30 Apr 2018 23:09:14 +0300 Subject: [PATCH] Fix room list updating when joining/leaving --- interface/matrix.go | 2 +- matrix/matrix.go | 41 +++++++++++------------------------- matrix/sync.go | 2 +- ui/messages/parser/parser.go | 2 +- ui/view-main.go | 12 +++++++++-- 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/interface/matrix.go b/interface/matrix.go index b53adca..95122cc 100644 --- a/interface/matrix.go +++ b/interface/matrix.go @@ -31,7 +31,7 @@ type MatrixContainer interface { SendMessage(roomID, msgtype, message string) (string, error) SendMarkdownMessage(roomID, msgtype, message string) (string, error) SendTyping(roomID string, typing bool) - JoinRoom(roomID string) error + JoinRoom(roomID string) (*rooms.Room, error) LeaveRoom(roomID string) error GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) GetRoom(roomID string) *rooms.Room diff --git a/matrix/matrix.go b/matrix/matrix.go index 655e272..2b0832b 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -294,7 +294,7 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) { } } -// HandleMembership is the event handler for the m.room.membership state event. +// HandleMembership is the event handler for the m.room.member state event. func (c *Container) HandleMembership(source EventSource, evt *gomatrix.Event) { if !c.config.Session.InitialSyncDone && source == EventSourceLeave { return @@ -418,9 +418,9 @@ func (c *Container) SendTyping(roomID string, typing bool) { } // JoinRoom makes the current user try to join the given room. -func (c *Container) JoinRoom(roomID string) error { +func (c *Container) JoinRoom(roomID string) (*rooms.Room, error) { if len(roomID) == 0 { - return fmt.Errorf("invalid room ID") + return nil, fmt.Errorf("invalid room ID") } server := "" @@ -428,12 +428,15 @@ func (c *Container) JoinRoom(roomID string) error { server = roomID[strings.Index(roomID, ":")+1:] } - _, err := c.client.JoinRoom(roomID, server, nil) + resp, err := c.client.JoinRoom(roomID, server, nil) if err != nil { - return err + return nil, err } - return nil + room := c.GetRoom(resp.RoomID) + room.HasLeft = false + + return room, nil } // LeaveRoom makes the current user leave the given room. @@ -447,20 +450,11 @@ func (c *Container) LeaveRoom(roomID string) error { return err } + room := c.GetRoom(roomID) + room.HasLeft = true return nil } -// getState requests the state of the given room. -func (c *Container) getState(roomID string) []*gomatrix.Event { - content := make([]*gomatrix.Event, 0) - err := c.client.StateEvent(roomID, "", "", &content) - if err != nil { - debug.Print("Error getting state of", roomID, err) - return nil - } - return content -} - // GetHistory fetches room history. func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) { resp, err := c.client.Messages(roomID, prevBatch, "", 'b', limit) @@ -471,19 +465,8 @@ func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix. } // GetRoom gets the room instance stored in the session. -// -// If the room doesn't have any state events stored, its state will be updated. 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) - if events != nil { - for _, event := range events { - room.UpdateState(event) - } - }*/ - } - return room + return c.config.Session.GetRoom(roomID) } var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)") diff --git a/matrix/sync.go b/matrix/sync.go index e2d38df..9488705 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -150,7 +150,7 @@ func (s *GomuksSyncer) OnFailedSync(res *gomatrix.RespSync, err error) (time.Dur func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage { return json.RawMessage(`{ "room": { - "include_leave": true, + "include_leave": false, "state": { "types": [ "m.room.member", diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go index eef91a2..090732d 100644 --- a/ui/messages/parser/parser.go +++ b/ui/messages/parser/parser.go @@ -137,5 +137,5 @@ func ParseMembershipEvent(room *rooms.Room, evt *gomatrix.Event) messages.UIMess } ts := unixToTime(evt.Timestamp) - return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.membership", text, ts) + return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.member", text, ts) } diff --git a/ui/view-main.go b/ui/view-main.go index c5f1304..34bccc7 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -152,13 +152,21 @@ func (view *MainView) HandleCommand(roomView *RoomView, command string, args []s case "/panic": panic("This is a test panic.") case "/part", "/leave": - debug.Print("Leave room result:", view.matrix.LeaveRoom(roomView.Room.ID)) + err := view.matrix.LeaveRoom(roomView.Room.ID) + debug.Print("Leave room error:", err) + if err == nil { + view.RemoveRoom(roomView.Room.ID) + } case "/join": if len(args) == 0 { roomView.AddServiceMessage("Usage: /join ") break } - debug.Print("Join room result:", view.matrix.JoinRoom(args[0])) + room, err := view.matrix.JoinRoom(args[0]) + debug.Print("Join room error:", err) + if err == nil { + view.AddRoom(room.ID) + } default: roomView.AddServiceMessage("Unknown command.") }