Refactoring

This commit is contained in:
Tulir Asokan 2018-05-01 19:17:57 +03:00
parent 986c84b768
commit b49416ed80
4 changed files with 41 additions and 30 deletions

View File

@ -25,16 +25,21 @@ type MatrixContainer interface {
Client() *gomatrix.Client Client() *gomatrix.Client
InitClient() error InitClient() error
Initialized() bool Initialized() bool
Login(user, password string) error
Start() Start()
Stop() Stop()
Login(user, password string) error
SendMessage(roomID, msgtype, message string) (string, error) SendMessage(roomID, msgtype, message string) (string, error)
SendMarkdownMessage(roomID, msgtype, message string) (string, error) SendMarkdownMessage(roomID, msgtype, message string) (string, error)
SendTyping(roomID string, typing bool) SendTyping(roomID string, typing bool)
JoinRoom(roomID string) (*rooms.Room, error) JoinRoom(roomID string) (*rooms.Room, error)
LeaveRoom(roomID string) error LeaveRoom(roomID string) error
GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error)
GetRoom(roomID string) *rooms.Room GetRoom(roomID string) *rooms.Room
Download(mxcURL string) ([]byte, string, string, error) Download(mxcURL string) ([]byte, string, string, error)
GetCachePath(homeserver, fileID string) string GetCachePath(homeserver, fileID string) string
} }

View File

@ -25,21 +25,13 @@ import (
"maunium.net/go/tcell" "maunium.net/go/tcell"
) )
type View string
// Allowed views in GomuksUI
const (
ViewLogin View = "login"
ViewMain View = "main"
)
type UIProvider func(gmx Gomuks) GomuksUI type UIProvider func(gmx Gomuks) GomuksUI
type GomuksUI interface { type GomuksUI interface {
Render() Render()
SetView(name View) OnLogin()
OnLogout()
MainView() MainView MainView() MainView
LoginView() LoginView
Init() Init()
Start() error Start() error
@ -62,9 +54,6 @@ type MainView interface {
NotifyMessage(room *rooms.Room, message Message, should pushrules.PushActionArrayShould) NotifyMessage(room *rooms.Room, message Message, should pushrules.PushActionArrayShould)
} }
type LoginView interface {
}
type MessageDirection int type MessageDirection int
const ( const (

View File

@ -135,11 +135,6 @@ func (c *Container) Stop() {
} }
} }
// Client returns the underlying gomatrix client object.
func (c *Container) Client() *gomatrix.Client {
return c.client
}
// UpdatePushRules fetches the push notification rules from the server and stores them in the current Session object. // UpdatePushRules fetches the push notification rules from the server and stores them in the current Session object.
func (c *Container) UpdatePushRules() { func (c *Container) UpdatePushRules() {
debug.Print("Updating push rules...") debug.Print("Updating push rules...")
@ -160,12 +155,14 @@ func (c *Container) PushRules() *pushrules.PushRuleset {
// 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.ui.OnLogout()
c.Stop() c.Stop()
c.ui.SetView(ifc.ViewLogin)
} }
// OnLogin initializes the syncer and updates the room list. // OnLogin initializes the syncer and updates the room list.
func (c *Container) OnLogin() { func (c *Container) OnLogin() {
c.ui.OnLogin()
c.client.Store = c.config.Session c.client.Store = c.config.Session
debug.Print("Initializing syncer") debug.Print("Initializing syncer")
@ -191,7 +188,6 @@ func (c *Container) OnLogin() {
func (c *Container) Start() { func (c *Container) Start() {
defer debug.Recover() defer debug.Recover()
c.ui.SetView(ifc.ViewMain)
c.OnLogin() c.OnLogin()
if c.client == nil { if c.client == nil {
@ -349,7 +345,7 @@ func (c *Container) SendMessage(roomID, msgtype, text string) (string, error) {
return resp.EventID, nil return resp.EventID, nil
} }
func (c *Container) RenderMarkdown(text string) string { func (c *Container) renderMarkdown(text string) string {
parser := blackfriday.New( parser := blackfriday.New(
blackfriday.WithExtensions(blackfriday.NoIntraEmphasis | blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
blackfriday.Tables | blackfriday.Tables |
@ -377,10 +373,14 @@ func (c *Container) RenderMarkdown(text string) string {
var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)") var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)")
var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)") var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)")
// SendMarkdownMessage sends a message with the given text to the given room.
//
// If the given text contains markdown formatting symbols, it will be rendered into HTML before sending.
// Otherwise, it will be sent as plain text.
func (c *Container) SendMarkdownMessage(roomID, msgtype, text string) (string, error) { func (c *Container) SendMarkdownMessage(roomID, msgtype, text string) (string, error) {
defer debug.Recover() defer debug.Recover()
html := c.RenderMarkdown(text) html := c.renderMarkdown(text)
if html == text { if html == text {
return c.SendMessage(roomID, msgtype, text) return c.SendMessage(roomID, msgtype, text)
} }
@ -474,6 +474,9 @@ func (c *Container) GetRoom(roomID string) *rooms.Room {
var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)") var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)")
// Download fetches the given Matrix content (mxc) URL and returns the data, homeserver, file ID and potential errors.
//
// The file will be either read from the media cache (if found) or downloaded from the server.
func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err error) { func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err error) {
parts := mxcRegex.FindStringSubmatch(mxcURL) parts := mxcRegex.FindStringSubmatch(mxcURL)
if parts == nil || len(parts) != 3 { if parts == nil || len(parts) != 3 {
@ -519,6 +522,8 @@ func (c *Container) download(hs, id, cacheFile string) (data []byte, err error)
return return
} }
// GetCachePath gets the path to the cached version of the given homeserver:fileID combination.
// The file may or may not exist, use Download() to ensure it has been cached.
func (c *Container) GetCachePath(homeserver, fileID string) string { func (c *Container) GetCachePath(homeserver, fileID string) string {
dir := filepath.Join(c.config.MediaDir, homeserver) dir := filepath.Join(c.config.MediaDir, homeserver)

View File

@ -22,6 +22,14 @@ import (
"maunium.net/go/tview" "maunium.net/go/tview"
) )
type View string
// Allowed views in GomuksUI
const (
ViewLogin View = "login"
ViewMain View = "main"
)
type GomuksUI struct { type GomuksUI struct {
gmx ifc.Gomuks gmx ifc.Gomuks
app *tview.Application app *tview.Application
@ -68,20 +76,24 @@ func (ui *GomuksUI) Render() {
ui.app.Draw() ui.app.Draw()
} }
func (ui *GomuksUI) SetView(name ifc.View) { func (ui *GomuksUI) OnLogin() {
ui.SetView(ViewMain)
}
func (ui *GomuksUI) OnLogout() {
ui.SetView(ViewLogin)
}
func (ui *GomuksUI) SetView(name View) {
ui.views.SwitchToPage(string(name)) ui.views.SwitchToPage(string(name))
} }
func (ui *GomuksUI) InitViews() tview.Primitive { func (ui *GomuksUI) InitViews() tview.Primitive {
ui.views.AddPage(string(ifc.ViewLogin), ui.NewLoginView(), true, true) ui.views.AddPage(string(ViewLogin), ui.NewLoginView(), true, true)
ui.views.AddPage(string(ifc.ViewMain), ui.NewMainView(), true, false) ui.views.AddPage(string(ViewMain), ui.NewMainView(), true, false)
return ui.views return ui.views
} }
func (ui *GomuksUI) MainView() ifc.MainView { func (ui *GomuksUI) MainView() ifc.MainView {
return ui.mainView return ui.mainView
} }
func (ui *GomuksUI) LoginView() ifc.LoginView {
return ui.loginView
}