aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-03-23 23:39:17 +0200
committerTulir Asokan <tulir@maunium.net>2018-03-23 23:39:17 +0200
commit38364646a73a5e666f2db6a34025e2a5a0e3999a (patch)
treec84911c6e8530f55c2fcfc06da587b10bec63547
parent997948ccad0ddd8e7ed2e87cbef1df10e14a3354 (diff)
Refactoring and documentation
-rw-r--r--config/config.go5
-rw-r--r--matrix/sync.go63
-rw-r--r--ui/widget/message-view.go8
3 files changed, 40 insertions, 36 deletions
diff --git a/config/config.go b/config/config.go
index 5c1d87c..4ad6793 100644
--- a/config/config.go
+++ b/config/config.go
@@ -26,6 +26,7 @@ import (
"maunium.net/go/gomuks/ui/debug"
)
+// Config contains the main config of gomuks.
type Config struct {
UserID string `yaml:"mxid"`
HS string `yaml:"homeserver"`
@@ -35,6 +36,7 @@ type Config struct {
Session *Session `yaml:"-"`
}
+// NewConfig creates a config that loads data from the given directory.
func NewConfig(dir string) *Config {
return &Config{
Dir: dir,
@@ -42,6 +44,7 @@ func NewConfig(dir string) *Config {
}
}
+// Clear clears the session cache and removes all history.
func (config *Config) Clear() {
if config.Session != nil {
config.Session.Clear()
@@ -49,6 +52,7 @@ func (config *Config) Clear() {
os.RemoveAll(config.HistoryDir)
}
+// Load loads the config from config.yaml in the directory given to the config struct.
func (config *Config) Load() {
os.MkdirAll(config.Dir, 0700)
os.MkdirAll(config.HistoryDir, 0700)
@@ -70,6 +74,7 @@ func (config *Config) Load() {
}
}
+// Save saves this config to config.yaml in the directory given to the config struct.
func (config *Config) Save() {
os.MkdirAll(config.Dir, 0700)
data, err := yaml.Marshal(&config)
diff --git a/matrix/sync.go b/matrix/sync.go
index f78359c..a21d982 100644
--- a/matrix/sync.go
+++ b/matrix/sync.go
@@ -26,6 +26,7 @@ import (
"maunium.net/go/gomatrix"
"maunium.net/go/gomuks/config"
+ "maunium.net/go/gomuks/matrix/rooms"
)
// GomuksSyncer is the default syncing implementation. You can either write your own syncer, or selectively
@@ -53,61 +54,59 @@ func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
defer func() {
if r := recover(); r != nil {
- err = fmt.Errorf("ProcessResponse panicked! userID=%s since=%s panic=%s\n%s", s.Session.UserID, since, r, debug.Stack())
+ err = fmt.Errorf("ProcessResponse for %s since %s panicked: %s\n%s", s.Session.UserID, since, r, debug.Stack())
}
}()
- for _, event := range res.Presence.Events {
- s.notifyListeners(event)
- }
- for _, event := range res.AccountData.Events {
- s.notifyListeners(event)
- }
+ s.processSyncEvents(nil, res.Presence.Events, false, false)
+ s.processSyncEvents(nil, res.AccountData.Events, false, false)
+
for roomID, roomData := range res.Rooms.Join {
room := s.Session.GetRoom(roomID)
- for _, event := range roomData.State.Events {
- event.RoomID = roomID
- room.UpdateState(event)
- s.notifyListeners(event)
- }
- for _, event := range roomData.Timeline.Events {
- event.RoomID = roomID
- s.notifyListeners(event)
- }
- for _, event := range roomData.Ephemeral.Events {
- event.RoomID = roomID
- s.notifyListeners(event)
- }
+ s.processSyncEvents(room, roomData.State.Events, true, false)
+ s.processSyncEvents(room, roomData.Timeline.Events, false, false)
+ s.processSyncEvents(room, roomData.Ephemeral.Events, false, false)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
}
}
+
for roomID, roomData := range res.Rooms.Invite {
room := s.Session.GetRoom(roomID)
- for _, event := range roomData.State.Events {
- event.RoomID = roomID
- room.UpdateState(event)
- s.notifyListeners(event)
- }
+ s.processSyncEvents(room, roomData.State.Events, true, false)
}
+
for roomID, roomData := range res.Rooms.Leave {
room := s.Session.GetRoom(roomID)
- for _, event := range roomData.Timeline.Events {
- if event.StateKey != nil {
- event.RoomID = roomID
- room.UpdateState(event)
- s.notifyListeners(event)
- }
- }
+ s.processSyncEvents(room, roomData.Timeline.Events, true, true)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
}
}
+
return
}
+func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*gomatrix.Event, isState bool, checkStateKey bool) {
+ for _, event := range events {
+ if !checkStateKey || event.StateKey != nil {
+ s.processSyncEvent(room, event, isState)
+ }
+ }
+}
+
+func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, event *gomatrix.Event, isState bool) {
+ if room != nil {
+ event.RoomID = room.ID
+ }
+ if isState {
+ room.UpdateState(event)
+ }
+ s.notifyListeners(event)
+}
+
// OnEventType allows callers to be notified when there are new events for the given event type.
// There are no duplicate checks.
func (s *GomuksSyncer) OnEventType(eventType string, callback gomatrix.OnEventListener) {
diff --git a/ui/widget/message-view.go b/ui/widget/message-view.go
index e482b25..a3a7942 100644
--- a/ui/widget/message-view.go
+++ b/ui/widget/message-view.go
@@ -325,10 +325,10 @@ func (view *MessageView) Draw(screen tcell.Screen) {
}
totalHeight := float64(len(view.textBuffer))
- // ceil(height / (totalHeight / height))
+ // The height of the scrollbar: ceil(height / (totalHeight / height))
scrollBarHeight := int(math.Ceil(float64(height) / (totalHeight / float64(height))))
- // height - ceil(scrollOffset) / totalHeight * height
- scrollBarPos := height - int(math.Ceil(float64(view.ScrollOffset) / totalHeight * float64(height)))
+ // The position of the scrollbar from the bottom: height - ceil(scrollOffset) / totalHeight * height
+ scrollBarPos := height - int(math.Ceil(float64(view.ScrollOffset)/totalHeight*float64(height)))
var prevMeta types.MessageMeta
firstLine := true
@@ -349,7 +349,7 @@ func (view *MessageView) Draw(screen tcell.Screen) {
} else if line == height-1 && view.ScrollOffset == 0 {
// At bottom of message history
borderChar = '┴'
- } else if line >= scrollBarPos && line < scrollBarPos + scrollBarHeight {
+ } else if line >= scrollBarPos && line < scrollBarPos+scrollBarHeight {
// Scroll bar
borderChar = '║'
borderStyle = borderStyle.Foreground(tcell.ColorGreen)