Everything is no longer broken

This commit is contained in:
Tulir Asokan 2018-04-24 16:51:40 +03:00
parent fcd9a932cb
commit e64df67ec3
10 changed files with 77 additions and 40 deletions

View File

@ -65,6 +65,7 @@ func (s *Session) Clear() {
s.PushRules = nil s.PushRules = nil
s.NextBatch = "" s.NextBatch = ""
s.FilterID = "" s.FilterID = ""
s.InitialSyncDone = false
s.Save() s.Save()
} }

View File

@ -18,7 +18,9 @@ package main
import ( import (
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"syscall"
"time" "time"
"maunium.net/go/gomuks/config" "maunium.net/go/gomuks/config"
@ -104,6 +106,13 @@ func (gmx *Gomuks) Stop() {
func (gmx *Gomuks) Start() { func (gmx *Gomuks) Start() {
_ = gmx.matrix.InitClient() _ = gmx.matrix.InitClient()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
gmx.Stop()
}()
go gmx.StartAutosave() go gmx.StartAutosave()
if err := gmx.ui.Start(); err != nil { if err := gmx.ui.Start(); err != nil {
panic(err) panic(err)

View File

@ -51,7 +51,7 @@ type MainView interface {
GetRoom(roomID string) RoomView GetRoom(roomID string) RoomView
AddRoom(roomID string) AddRoom(roomID string)
RemoveRoom(roomID string) RemoveRoom(roomID string)
SetRooms(roomIDs []string) SetRooms(rooms map[string]*rooms.Room)
SaveAllHistory() SaveAllHistory()
UpdateTags(room *rooms.Room, newTags []rooms.RoomTag) UpdateTags(room *rooms.Room, newTags []rooms.RoomTag)

View File

@ -129,6 +129,7 @@ func (c *Container) Login(user, password string) error {
// Stop stops the Matrix syncer. // Stop stops the Matrix syncer.
func (c *Container) Stop() { func (c *Container) Stop() {
if c.running { if c.running {
debug.Print("Stopping Matrix container...")
c.stop <- true c.stop <- true
c.client.StopSync() c.client.StopSync()
} }
@ -157,22 +158,6 @@ func (c *Container) PushRules() *pushrules.PushRuleset {
return c.config.Session.PushRules 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. // OnLogout stops the syncer and moves the UI back to the login view.
func (c *Container) OnLogout() { func (c *Container) OnLogout() {
c.Stop() c.Stop()
@ -183,6 +168,7 @@ func (c *Container) OnLogout() {
func (c *Container) OnLogin() { func (c *Container) OnLogin() {
c.client.Store = c.config.Session c.client.Store = c.config.Session
debug.Print("Initializing syncer")
c.syncer = NewGomuksSyncer(c.config.Session) c.syncer = NewGomuksSyncer(c.config.Session)
c.syncer.OnEventType("m.room.message", c.HandleMessage) c.syncer.OnEventType("m.room.message", c.HandleMessage)
c.syncer.OnEventType("m.room.member", c.HandleMembership) c.syncer.OnEventType("m.room.member", c.HandleMembership)
@ -191,7 +177,10 @@ func (c *Container) OnLogin() {
c.syncer.OnEventType("m.tag", c.HandleTag) c.syncer.OnEventType("m.tag", c.HandleTag)
c.client.Syncer = c.syncer 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() // Start moves the UI to the main view, calls OnLogin() and runs the syncer forever until stopped with Stop()
@ -226,20 +215,25 @@ func (c *Container) Start() {
// HandleMessage is the event handler for the m.room.message timeline event. // HandleMessage is the event handler for the m.room.message timeline event.
func (c *Container) HandleMessage(evt *gomatrix.Event) { func (c *Container) HandleMessage(evt *gomatrix.Event) {
mainView := c.ui.MainView() mainView := c.ui.MainView()
roomView := mainView.GetRoom(evt.RoomID) roomView := mainView.GetRoom(evt.RoomID)
if roomView == nil { if roomView == nil {
debug.Printf("Failed to handle event %v: No room view found.", evt)
return return
} }
message := mainView.ParseEvent(roomView, evt) message := mainView.ParseEvent(roomView, evt)
if message != nil { if message != nil {
debug.Print("Adding message", message.ID(), c.syncer.FirstSyncDone, c.config.Session.InitialSyncDone)
roomView.AddMessage(message, ifc.AppendMessage)
if c.syncer.FirstSyncDone { if c.syncer.FirstSyncDone {
pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should()
mainView.NotifyMessage(roomView.MxRoom(), message, pushRules) mainView.NotifyMessage(roomView.MxRoom(), message, pushRules)
}
roomView.AddMessage(message, ifc.AppendMessage)
c.ui.Render() c.ui.Render()
} }
} else {
debug.Printf("Parsing event %v failed (ParseEvent() returned nil).", evt)
}
} }
// HandlePushRules is the event handler for the m.push_rules account data event. // HandlePushRules is the event handler for the m.push_rules account data event.
@ -279,6 +273,7 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
if evt.Unsigned.PrevContent != nil { if evt.Unsigned.PrevContent != nil {
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string) prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
} }
debug.Printf("Processing own membership change: %s->%s in %s", membership, prevMembership, evt.RoomID)
if membership == prevMembership { if membership == prevMembership {
return return
} }
@ -287,6 +282,9 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
c.ui.MainView().AddRoom(evt.RoomID) c.ui.MainView().AddRoom(evt.RoomID)
case "leave": case "leave":
c.ui.MainView().RemoveRoom(evt.RoomID) c.ui.MainView().RemoveRoom(evt.RoomID)
case "invite":
// TODO handle
debug.Printf("%s invited the user to %s", evt.Sender, evt.RoomID)
} }
} }
@ -296,7 +294,8 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) {
c.processOwnMembershipChange(evt) c.processOwnMembershipChange(evt)
} }
if !c.config.Session.InitialSyncDone && evt.Timestamp < time.Now().Add(-1*time.Hour).Unix() { 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 return
} }
@ -308,18 +307,20 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) {
message := mainView.ParseEvent(roomView, evt) message := mainView.ParseEvent(roomView, evt)
if message != nil { if message != nil {
debug.Print("Adding membership event", message.ID(), c.syncer.FirstSyncDone, c.config.Session.InitialSyncDone)
// TODO this shouldn't be necessary // TODO this shouldn't be necessary
roomView.MxRoom().UpdateState(evt) //roomView.MxRoom().UpdateState(evt)
// TODO This should probably also be in a different place // 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 { if c.syncer.FirstSyncDone {
pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should()
mainView.NotifyMessage(roomView.MxRoom(), message, pushRules) mainView.NotifyMessage(roomView.MxRoom(), message, pushRules)
}
roomView.AddMessage(message, ifc.AppendMessage)
c.ui.Render() c.ui.Render()
} }
}
} }
// HandleTyping is the event handler for the m.typing event. // HandleTyping is the event handler for the m.typing event.
@ -475,12 +476,12 @@ func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.
func (c *Container) GetRoom(roomID string) *rooms.Room { func (c *Container) GetRoom(roomID string) *rooms.Room {
room := c.config.Session.GetRoom(roomID) room := c.config.Session.GetRoom(roomID)
if room != nil && len(room.State) == 0 { if room != nil && len(room.State) == 0 {
events := c.getState(room.ID) /*events := c.getState(room.ID)
if events != nil { if events != nil {
for _, event := range events { for _, event := range events {
room.UpdateState(event) room.UpdateState(event)
} }
} }*/
} }
return room return room
} }

View File

@ -23,6 +23,7 @@ import (
"time" "time"
"maunium.net/go/gomatrix" "maunium.net/go/gomatrix"
"maunium.net/go/gomuks/debug"
) )
type RoomNameSource int type RoomNameSource int
@ -148,6 +149,15 @@ func (room *Room) UpdateState(event *gomatrix.Event) {
case "m.room.topic": case "m.room.topic":
room.topicCache = "" room.topicCache = ""
} }
stateKey := ""
if event.StateKey != nil {
stateKey = *event.StateKey
}
if event.Type != "m.room.member" {
debug.Printf("[ROOM] Updating state %s#%s for %s", event.Type, stateKey, room.ID)
}
if event.StateKey == nil { if event.StateKey == nil {
room.State[event.Type][""] = event room.State[event.Type][""] = event
} else { } else {

View File

@ -23,7 +23,6 @@ import (
"time" "time"
"maunium.net/go/gomatrix" "maunium.net/go/gomatrix"
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
) )
@ -53,8 +52,6 @@ func NewGomuksSyncer(session SyncerSession) *GomuksSyncer {
// ProcessResponse processes a Matrix sync response. // ProcessResponse processes a Matrix sync response.
func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (err error) { func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (err error) {
debug.Print("Processing sync response", since, res)
s.processSyncEvents(nil, res.Presence.Events, false, false) s.processSyncEvents(nil, res.Presence.Events, false, false)
s.processSyncEvents(nil, res.AccountData.Events, false, false) s.processSyncEvents(nil, res.AccountData.Events, false, false)
@ -141,7 +138,13 @@ func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage {
"room": { "room": {
"include_leave": true, "include_leave": true,
"state": { "state": {
"types": ["m.room.member"] "types": [
"m.room.member",
"m.room.name",
"m.room.topic",
"m.room.canonical_alias",
"m.room.aliases"
]
}, },
"timeline": { "timeline": {
"types": ["m.room.message"], "types": ["m.room.message"],

View File

@ -183,7 +183,8 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction ifc.Messag
} else if oldMsg != nil { } else if oldMsg != nil {
view.replaceBuffer(oldMsg, message) view.replaceBuffer(oldMsg, message)
} else { } else {
view.replaceBuffer(message, message) debug.Print("Unexpected AddMessage() call: Direction is not append or prepend, but message is new.")
debug.PrintStack()
} }
view.messageIDs[message.ID()] = message view.messageIDs[message.ID()] = message
@ -232,7 +233,7 @@ func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages
} }
if start == -1 { if start == -1 {
debug.Print("Called replaceBuffer() with message that was not in the buffer:", original) debug.Print("Called replaceBuffer() with message that was not in the buffer:", original.ID())
view.appendBuffer(new) view.appendBuffer(new)
return return
} }

View File

@ -73,7 +73,13 @@ func (msg *BaseTextMessage) calculateBufferWithText(text tstring.TString, width
matches := boundaryPattern.FindAllStringIndex(extract.String(), -1) matches := boundaryPattern.FindAllStringIndex(extract.String(), -1)
if len(matches) > 0 { if len(matches) > 0 {
extract = extract[:matches[len(matches)-1][1]] match := matches[len(matches)-1]
if len(match) > 1 {
until := match[1]
if until < len(extract) {
extract = extract[:until]
}
}
} }
} }
msg.buffer = append(msg.buffer, extract) msg.buffer = append(msg.buffer, extract)

View File

@ -210,7 +210,7 @@ func (list *RoomList) Clear() {
func (list *RoomList) SetSelected(tag string, room *rooms.Room) { func (list *RoomList) SetSelected(tag string, room *rooms.Room) {
list.selected = room list.selected = room
list.selectedTag = "" list.selectedTag = tag
} }
func (list *RoomList) HasSelected() bool { func (list *RoomList) HasSelected() bool {
@ -264,7 +264,9 @@ func (list *RoomList) Previous() (string, *rooms.Room) {
items := list.items[list.selectedTag] items := list.items[list.selectedTag]
index := list.indexInTag(list.selectedTag, list.selected) index := list.indexInTag(list.selectedTag, list.selected)
if index == len(items)-1 { if index == -1 {
return list.First()
} else if index == len(items)-1 {
tagIndex := list.IndexTag(list.selectedTag) tagIndex := list.IndexTag(list.selectedTag)
tagIndex++ tagIndex++
for ; tagIndex < len(list.tags); tagIndex++ { for ; tagIndex < len(list.tags); tagIndex++ {
@ -288,7 +290,9 @@ func (list *RoomList) Next() (string, *rooms.Room) {
items := list.items[list.selectedTag] items := list.items[list.selectedTag]
index := list.indexInTag(list.selectedTag, list.selected) index := list.indexInTag(list.selectedTag, list.selected)
if index == 0 { if index == -1 {
return list.Last()
} else if index == 0 {
tagIndex := list.IndexTag(list.selectedTag) tagIndex := list.IndexTag(list.selectedTag)
tagIndex-- tagIndex--
for ; tagIndex >= 0; tagIndex-- { for ; tagIndex >= 0; tagIndex-- {
@ -332,6 +336,8 @@ func (list *RoomList) Get(n int) (string, *rooms.Room) {
// Tag items // Tag items
n -= len(items) n -= len(items)
// Tag footer
n--
} }
return "", nil return "", nil
} }
@ -420,5 +426,6 @@ func (list *RoomList) Draw(screen tcell.Screen) {
break break
} }
} }
y++
} }
} }

View File

@ -336,12 +336,11 @@ func (view *MainView) RemoveRoom(roomID string) {
view.parent.Render() view.parent.Render()
} }
func (view *MainView) SetRooms(roomIDs []string) { func (view *MainView) SetRooms(rooms map[string]*rooms.Room) {
view.roomList.Clear() view.roomList.Clear()
view.roomView.Clear() view.roomView.Clear()
view.rooms = make(map[string]*RoomView) view.rooms = make(map[string]*RoomView)
for _, roomID := range roomIDs { for _, room := range rooms {
room := view.matrix.GetRoom(roomID)
view.roomList.Add(room) view.roomList.Add(room)
view.addRoomPage(room) view.addRoomPage(room)
} }