Documentation and refactoring
This commit is contained in:
parent
7cc55ade30
commit
03e9a0d5ac
46
gomuks.go
46
gomuks.go
@ -22,7 +22,6 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"maunium.net/go/gomatrix"
|
||||
"maunium.net/go/gomuks/config"
|
||||
"maunium.net/go/gomuks/interface"
|
||||
"maunium.net/go/gomuks/matrix"
|
||||
@ -31,6 +30,7 @@ import (
|
||||
"maunium.net/go/tview"
|
||||
)
|
||||
|
||||
// Gomuks is the wrapper for everything.
|
||||
type Gomuks struct {
|
||||
app *tview.Application
|
||||
ui *ui.GomuksUI
|
||||
@ -41,6 +41,8 @@ type Gomuks struct {
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
// NewGomuks creates a new Gomuks instance with everything initialized,
|
||||
// but does not start it.
|
||||
func NewGomuks(enableDebug bool) *Gomuks {
|
||||
configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks")
|
||||
gmx := &Gomuks{
|
||||
@ -76,16 +78,7 @@ func NewGomuks(enableDebug bool) *Gomuks {
|
||||
return gmx
|
||||
}
|
||||
|
||||
func (gmx *Gomuks) Stop() {
|
||||
gmx.debug.Print("Disconnecting from Matrix...")
|
||||
gmx.matrix.Stop()
|
||||
gmx.debug.Print("Cleaning up UI...")
|
||||
gmx.app.Stop()
|
||||
gmx.stop <- true
|
||||
gmx.Save()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Save saves the active session and message history.
|
||||
func (gmx *Gomuks) Save() {
|
||||
if gmx.config.Session != nil {
|
||||
gmx.debug.Print("Saving session...")
|
||||
@ -95,6 +88,8 @@ func (gmx *Gomuks) Save() {
|
||||
gmx.ui.MainView().SaveAllHistory()
|
||||
}
|
||||
|
||||
// StartAutosave calls Save() every minute until it receives a stop signal
|
||||
// on the Gomuks.stop channel.
|
||||
func (gmx *Gomuks) StartAutosave() {
|
||||
defer gmx.Recover()
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
@ -110,6 +105,21 @@ func (gmx *Gomuks) StartAutosave() {
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the Matrix syncer, the tview app and the autosave goroutine,
|
||||
// then saves everything and calls os.Exit(0).
|
||||
func (gmx *Gomuks) Stop() {
|
||||
gmx.debug.Print("Disconnecting from Matrix...")
|
||||
gmx.matrix.Stop()
|
||||
gmx.debug.Print("Cleaning up UI...")
|
||||
gmx.app.Stop()
|
||||
gmx.stop <- true
|
||||
gmx.Save()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Recover recovers a panic, closes the tcell screen and either re-panics or
|
||||
// shows an user-friendly message about the panic depending on whether or not
|
||||
// the debug mode is enabled.
|
||||
func (gmx *Gomuks) Recover() {
|
||||
if p := recover(); p != nil {
|
||||
if gmx.App().GetScreen() != nil {
|
||||
@ -123,6 +133,10 @@ func (gmx *Gomuks) Recover() {
|
||||
}
|
||||
}
|
||||
|
||||
// Start opens a goroutine for the autosave loop and starts the tview app.
|
||||
//
|
||||
// If the tview app returns an error, it will be passed into panic(), which
|
||||
// will be recovered as specified in Recover().
|
||||
func (gmx *Gomuks) Start() {
|
||||
defer gmx.Recover()
|
||||
go gmx.StartAutosave()
|
||||
@ -131,22 +145,22 @@ func (gmx *Gomuks) Start() {
|
||||
}
|
||||
}
|
||||
|
||||
func (gmx *Gomuks) Matrix() *gomatrix.Client {
|
||||
return gmx.matrix.Client()
|
||||
}
|
||||
|
||||
func (gmx *Gomuks) MatrixContainer() ifc.MatrixContainer {
|
||||
// Matrix returns the MatrixContainer instance.
|
||||
func (gmx *Gomuks) Matrix() ifc.MatrixContainer {
|
||||
return gmx.matrix
|
||||
}
|
||||
|
||||
// App returns the tview Application instance.
|
||||
func (gmx *Gomuks) App() *tview.Application {
|
||||
return gmx.app
|
||||
}
|
||||
|
||||
// Config returns the Gomuks config instance.
|
||||
func (gmx *Gomuks) Config() *config.Config {
|
||||
return gmx.config
|
||||
}
|
||||
|
||||
// UI returns the Gomuks UI instance.
|
||||
func (gmx *Gomuks) UI() ifc.GomuksUI {
|
||||
return gmx.ui
|
||||
}
|
||||
|
@ -17,14 +17,13 @@
|
||||
package ifc
|
||||
|
||||
import (
|
||||
"maunium.net/go/gomatrix"
|
||||
"maunium.net/go/gomuks/config"
|
||||
"maunium.net/go/tview"
|
||||
)
|
||||
|
||||
// Gomuks is the wrapper for everything.
|
||||
type Gomuks interface {
|
||||
Matrix() *gomatrix.Client
|
||||
MatrixContainer() MatrixContainer
|
||||
Matrix() MatrixContainer
|
||||
App() *tview.Application
|
||||
UI() GomuksUI
|
||||
Config() *config.Config
|
||||
|
@ -263,9 +263,10 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
|
||||
if membership == prevMembership {
|
||||
return
|
||||
}
|
||||
if membership == "join" {
|
||||
switch membership {
|
||||
case "join":
|
||||
c.ui.MainView().AddRoom(evt.RoomID)
|
||||
} else if membership == "leave" {
|
||||
case "leave":
|
||||
c.ui.MainView().RemoveRoom(evt.RoomID)
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,14 @@ type rawPushRuleset struct {
|
||||
Underride PushRuleArray `json:"underride"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON parses JSON into this PushRuleset.
|
||||
//
|
||||
// For override, sender and underride push rule arrays, the type is added
|
||||
// to each PushRule and the array is used as-is.
|
||||
//
|
||||
// For room and sender push rule arrays, the type is added to each PushRule
|
||||
// and the array is converted to a map with the rule ID as the key and the
|
||||
// PushRule as the value.
|
||||
func (rs *PushRuleset) UnmarshalJSON(raw []byte) (err error) {
|
||||
data := rawPushRuleset{}
|
||||
err = json.Unmarshal(raw, &data)
|
||||
@ -54,6 +62,7 @@ func (rs *PushRuleset) UnmarshalJSON(raw []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalJSON is the reverse of UnmarshalJSON()
|
||||
func (rs *PushRuleset) MarshalJSON() ([]byte, error) {
|
||||
data := rawPushRuleset{
|
||||
Override: rs.Override,
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"maunium.net/go/gomatrix"
|
||||
)
|
||||
|
||||
// Membership is an enum specifying the membership state of a room member.
|
||||
type Membership string
|
||||
|
||||
// The allowed membership states as specified in spec section 10.5.5.
|
||||
|
@ -34,17 +34,22 @@ type Room struct {
|
||||
SessionUserID string
|
||||
// MXID -> Member cache calculated from membership events.
|
||||
memberCache map[string]*Member
|
||||
// The first non-SessionUserID member in the room. Calculated at the same time as memberCache.
|
||||
// The first non-SessionUserID member in the room. Calculated at
|
||||
// the same time as memberCache.
|
||||
firstMemberCache string
|
||||
// The name of the room. Calculated from the state event name, canonical_alias or alias or the member cache.
|
||||
// The name of the room. Calculated from the state event name,
|
||||
// canonical_alias or alias or the member cache.
|
||||
nameCache string
|
||||
// The topic of the room. Directly fetched from the m.room.topic state event.
|
||||
topicCache string
|
||||
|
||||
// fetchHistoryLock is used to make sure multiple goroutines don't fetch history for this room at the same time.
|
||||
// fetchHistoryLock is used to make sure multiple goroutines don't fetch
|
||||
// history for this room at the same time.
|
||||
fetchHistoryLock *sync.Mutex
|
||||
}
|
||||
|
||||
// LockHistory locks the history fetching mutex.
|
||||
// If the mutex is nil, it will be created.
|
||||
func (room *Room) LockHistory() {
|
||||
if room.fetchHistoryLock == nil {
|
||||
room.fetchHistoryLock = &sync.Mutex{}
|
||||
@ -52,6 +57,8 @@ func (room *Room) LockHistory() {
|
||||
room.fetchHistoryLock.Lock()
|
||||
}
|
||||
|
||||
// UnlockHistory unlocks the history fetching mutex.
|
||||
// If the mutex is nil, this does nothing.
|
||||
func (room *Room) UnlockHistory() {
|
||||
if room.fetchHistoryLock != nil {
|
||||
room.fetchHistoryLock.Unlock()
|
||||
|
@ -49,7 +49,7 @@ func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
|
||||
if len(since) == 0 {
|
||||
return
|
||||
}
|
||||
// gdebug.Print("Processing sync response", since, res)
|
||||
// debug.Print("Processing sync response", since, res)
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
|
@ -44,7 +44,7 @@ func (ui *GomuksUI) NewLoginView() tview.Primitive {
|
||||
username: widget.NewAdvancedInputField(),
|
||||
password: widget.NewAdvancedInputField(),
|
||||
|
||||
matrix: ui.gmx.MatrixContainer(),
|
||||
matrix: ui.gmx.Matrix(),
|
||||
config: ui.gmx.Config(),
|
||||
parent: ui,
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func (ui *GomuksUI) NewMainView() tview.Primitive {
|
||||
roomView: tview.NewPages(),
|
||||
rooms: make(map[string]*widget.RoomView),
|
||||
|
||||
matrix: ui.gmx.MatrixContainer(),
|
||||
matrix: ui.gmx.Matrix(),
|
||||
gmx: ui.gmx,
|
||||
config: ui.gmx.Config(),
|
||||
parent: ui,
|
||||
|
Loading…
Reference in New Issue
Block a user