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
InitClient() error
Initialized() bool
Login(user, password string) error
Start()
Stop()
Login(user, password string) error
SendMessage(roomID, msgtype, message string) (string, error)
SendMarkdownMessage(roomID, msgtype, message string) (string, error)
SendTyping(roomID string, typing bool)
JoinRoom(roomID string) (*rooms.Room, error)
LeaveRoom(roomID string) error
GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error)
GetRoom(roomID string) *rooms.Room
Download(mxcURL string) ([]byte, string, string, error)
GetCachePath(homeserver, fileID string) string
}

View File

@ -25,21 +25,13 @@ import (
"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 GomuksUI interface {
Render()
SetView(name View)
OnLogin()
OnLogout()
MainView() MainView
LoginView() LoginView
Init()
Start() error
@ -62,9 +54,6 @@ type MainView interface {
NotifyMessage(room *rooms.Room, message Message, should pushrules.PushActionArrayShould)
}
type LoginView interface {
}
type MessageDirection int
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.
func (c *Container) UpdatePushRules() {
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.
func (c *Container) OnLogout() {
c.ui.OnLogout()
c.Stop()
c.ui.SetView(ifc.ViewLogin)
}
// OnLogin initializes the syncer and updates the room list.
func (c *Container) OnLogin() {
c.ui.OnLogin()
c.client.Store = c.config.Session
debug.Print("Initializing syncer")
@ -191,7 +188,6 @@ func (c *Container) OnLogin() {
func (c *Container) Start() {
defer debug.Recover()
c.ui.SetView(ifc.ViewMain)
c.OnLogin()
if c.client == nil {
@ -349,7 +345,7 @@ func (c *Container) SendMessage(roomID, msgtype, text string) (string, error) {
return resp.EventID, nil
}
func (c *Container) RenderMarkdown(text string) string {
func (c *Container) renderMarkdown(text string) string {
parser := blackfriday.New(
blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
blackfriday.Tables |
@ -377,10 +373,14 @@ func (c *Container) RenderMarkdown(text string) string {
var mentionRegex = 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) {
defer debug.Recover()
html := c.RenderMarkdown(text)
html := c.renderMarkdown(text)
if html == text {
return c.SendMessage(roomID, msgtype, text)
}
@ -474,6 +474,9 @@ func (c *Container) GetRoom(roomID string) *rooms.Room {
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) {
parts := mxcRegex.FindStringSubmatch(mxcURL)
if parts == nil || len(parts) != 3 {
@ -519,6 +522,8 @@ func (c *Container) download(hs, id, cacheFile string) (data []byte, err error)
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 {
dir := filepath.Join(c.config.MediaDir, homeserver)

View File

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