Refactoring and documentation

This commit is contained in:
Tulir Asokan 2018-03-23 23:39:17 +02:00
parent 997948ccad
commit 38364646a7
3 changed files with 40 additions and 36 deletions

View File

@ -26,6 +26,7 @@ import (
"maunium.net/go/gomuks/ui/debug" "maunium.net/go/gomuks/ui/debug"
) )
// Config contains the main config of gomuks.
type Config struct { type Config struct {
UserID string `yaml:"mxid"` UserID string `yaml:"mxid"`
HS string `yaml:"homeserver"` HS string `yaml:"homeserver"`
@ -35,6 +36,7 @@ type Config struct {
Session *Session `yaml:"-"` Session *Session `yaml:"-"`
} }
// NewConfig creates a config that loads data from the given directory.
func NewConfig(dir string) *Config { func NewConfig(dir string) *Config {
return &Config{ return &Config{
Dir: dir, Dir: dir,
@ -42,6 +44,7 @@ func NewConfig(dir string) *Config {
} }
} }
// Clear clears the session cache and removes all history.
func (config *Config) Clear() { func (config *Config) Clear() {
if config.Session != nil { if config.Session != nil {
config.Session.Clear() config.Session.Clear()
@ -49,6 +52,7 @@ func (config *Config) Clear() {
os.RemoveAll(config.HistoryDir) os.RemoveAll(config.HistoryDir)
} }
// Load loads the config from config.yaml in the directory given to the config struct.
func (config *Config) Load() { func (config *Config) Load() {
os.MkdirAll(config.Dir, 0700) os.MkdirAll(config.Dir, 0700)
os.MkdirAll(config.HistoryDir, 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() { func (config *Config) Save() {
os.MkdirAll(config.Dir, 0700) os.MkdirAll(config.Dir, 0700)
data, err := yaml.Marshal(&config) data, err := yaml.Marshal(&config)

View File

@ -26,6 +26,7 @@ import (
"maunium.net/go/gomatrix" "maunium.net/go/gomatrix"
"maunium.net/go/gomuks/config" "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 // 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() { defer func() {
if r := recover(); r != nil { 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.processSyncEvents(nil, res.Presence.Events, false, false)
s.notifyListeners(event) s.processSyncEvents(nil, res.AccountData.Events, false, false)
}
for _, event := range res.AccountData.Events {
s.notifyListeners(event)
}
for roomID, roomData := range res.Rooms.Join { for roomID, roomData := range res.Rooms.Join {
room := s.Session.GetRoom(roomID) room := s.Session.GetRoom(roomID)
for _, event := range roomData.State.Events { s.processSyncEvents(room, roomData.State.Events, true, false)
event.RoomID = roomID s.processSyncEvents(room, roomData.Timeline.Events, false, false)
room.UpdateState(event) s.processSyncEvents(room, roomData.Ephemeral.Events, false, false)
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)
}
if len(room.PrevBatch) == 0 { if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch room.PrevBatch = roomData.Timeline.PrevBatch
} }
} }
for roomID, roomData := range res.Rooms.Invite { for roomID, roomData := range res.Rooms.Invite {
room := s.Session.GetRoom(roomID) room := s.Session.GetRoom(roomID)
for _, event := range roomData.State.Events { s.processSyncEvents(room, roomData.State.Events, true, false)
event.RoomID = roomID
room.UpdateState(event)
s.notifyListeners(event)
}
} }
for roomID, roomData := range res.Rooms.Leave { for roomID, roomData := range res.Rooms.Leave {
room := s.Session.GetRoom(roomID) room := s.Session.GetRoom(roomID)
for _, event := range roomData.Timeline.Events { s.processSyncEvents(room, roomData.Timeline.Events, true, true)
if event.StateKey != nil {
event.RoomID = roomID
room.UpdateState(event)
s.notifyListeners(event)
}
}
if len(room.PrevBatch) == 0 { if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch room.PrevBatch = roomData.Timeline.PrevBatch
} }
} }
return 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. // OnEventType allows callers to be notified when there are new events for the given event type.
// There are no duplicate checks. // There are no duplicate checks.
func (s *GomuksSyncer) OnEventType(eventType string, callback gomatrix.OnEventListener) { func (s *GomuksSyncer) OnEventType(eventType string, callback gomatrix.OnEventListener) {

View File

@ -325,10 +325,10 @@ func (view *MessageView) Draw(screen tcell.Screen) {
} }
totalHeight := float64(len(view.textBuffer)) 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)))) scrollBarHeight := int(math.Ceil(float64(height) / (totalHeight / float64(height))))
// height - ceil(scrollOffset) / totalHeight * 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))) scrollBarPos := height - int(math.Ceil(float64(view.ScrollOffset)/totalHeight*float64(height)))
var prevMeta types.MessageMeta var prevMeta types.MessageMeta
firstLine := true firstLine := true
@ -349,7 +349,7 @@ func (view *MessageView) Draw(screen tcell.Screen) {
} else if line == height-1 && view.ScrollOffset == 0 { } else if line == height-1 && view.ScrollOffset == 0 {
// At bottom of message history // At bottom of message history
borderChar = '┴' borderChar = '┴'
} else if line >= scrollBarPos && line < scrollBarPos + scrollBarHeight { } else if line >= scrollBarPos && line < scrollBarPos+scrollBarHeight {
// Scroll bar // Scroll bar
borderChar = '║' borderChar = '║'
borderStyle = borderStyle.Foreground(tcell.ColorGreen) borderStyle = borderStyle.Foreground(tcell.ColorGreen)