Fix room list updating when joining/leaving

This commit is contained in:
Tulir Asokan 2018-04-30 23:09:14 +03:00
parent 9ad3a95ba6
commit c600ce68a2
5 changed files with 25 additions and 34 deletions

View File

@ -31,7 +31,7 @@ type MatrixContainer interface {
SendMessage(roomID, msgtype, message string) (string, error) SendMessage(roomID, msgtype, message string) (string, error)
SendMarkdownMessage(roomID, msgtype, message string) (string, error) SendMarkdownMessage(roomID, msgtype, message string) (string, error)
SendTyping(roomID string, typing bool) SendTyping(roomID string, typing bool)
JoinRoom(roomID string) error JoinRoom(roomID string) (*rooms.Room, error)
LeaveRoom(roomID string) error LeaveRoom(roomID string) error
GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error)
GetRoom(roomID string) *rooms.Room GetRoom(roomID string) *rooms.Room

View File

@ -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) { func (c *Container) HandleMembership(source EventSource, evt *gomatrix.Event) {
if !c.config.Session.InitialSyncDone && source == EventSourceLeave { if !c.config.Session.InitialSyncDone && source == EventSourceLeave {
return return
@ -418,9 +418,9 @@ func (c *Container) SendTyping(roomID string, typing bool) {
} }
// JoinRoom makes the current user try to join the given room. // 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 { if len(roomID) == 0 {
return fmt.Errorf("invalid room ID") return nil, fmt.Errorf("invalid room ID")
} }
server := "" server := ""
@ -428,12 +428,15 @@ func (c *Container) JoinRoom(roomID string) error {
server = roomID[strings.Index(roomID, ":")+1:] server = roomID[strings.Index(roomID, ":")+1:]
} }
_, err := c.client.JoinRoom(roomID, server, nil) resp, err := c.client.JoinRoom(roomID, server, nil)
if err != 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. // LeaveRoom makes the current user leave the given room.
@ -447,20 +450,11 @@ func (c *Container) LeaveRoom(roomID string) error {
return err return err
} }
room := c.GetRoom(roomID)
room.HasLeft = true
return nil 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. // GetHistory fetches room history.
func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) { func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) {
resp, err := c.client.Messages(roomID, prevBatch, "", 'b', limit) 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. // 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 { func (c *Container) GetRoom(roomID string) *rooms.Room {
room := c.config.Session.GetRoom(roomID) return 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
} }
var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)") var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)")

View File

@ -150,7 +150,7 @@ func (s *GomuksSyncer) OnFailedSync(res *gomatrix.RespSync, err error) (time.Dur
func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage { func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage {
return json.RawMessage(`{ return json.RawMessage(`{
"room": { "room": {
"include_leave": true, "include_leave": false,
"state": { "state": {
"types": [ "types": [
"m.room.member", "m.room.member",

View File

@ -137,5 +137,5 @@ func ParseMembershipEvent(room *rooms.Room, evt *gomatrix.Event) messages.UIMess
} }
ts := unixToTime(evt.Timestamp) 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)
} }

View File

@ -152,13 +152,21 @@ func (view *MainView) HandleCommand(roomView *RoomView, command string, args []s
case "/panic": case "/panic":
panic("This is a test panic.") panic("This is a test panic.")
case "/part", "/leave": 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": case "/join":
if len(args) == 0 { if len(args) == 0 {
roomView.AddServiceMessage("Usage: /join <room>") roomView.AddServiceMessage("Usage: /join <room>")
break 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: default:
roomView.AddServiceMessage("Unknown command.") roomView.AddServiceMessage("Unknown command.")
} }