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"
)
// 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)

View File

@ -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) {

View File

@ -325,9 +325,9 @@ 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
// 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