From eda2b575f06e72040ebf82d24a7ec1ac84b7948c Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 9 Apr 2018 23:45:54 +0300 Subject: Refactor UI to use interfaces everywhere --- matrix/matrix.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'matrix') diff --git a/matrix/matrix.go b/matrix/matrix.go index 7391ca0..6fff6d8 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -22,13 +22,14 @@ import ( "strings" "time" - "maunium.net/go/gomatrix" - "maunium.net/go/gomuks/config" - "maunium.net/go/gomuks/interface" - "maunium.net/go/gomuks/matrix/pushrules" - "maunium.net/go/gomuks/matrix/rooms" - "maunium.net/go/gomuks/ui/debug" - "maunium.net/go/gomuks/ui/widget" + +"maunium.net/go/gomatrix" +"maunium.net/go/gomuks/config" +"maunium.net/go/gomuks/debug" +"maunium.net/go/gomuks/interface" +"maunium.net/go/gomuks/matrix/pushrules" +"maunium.net/go/gomuks/matrix/rooms" + ) // Container is a wrapper for a gomatrix Client and some other stuff. @@ -224,10 +225,10 @@ func (c *Container) HandleMessage(evt *gomatrix.Event) { message := mainView.ProcessMessageEvent(roomView, evt) if message != nil { if c.syncer.FirstSyncDone { - pushRules := c.PushRules().GetActions(roomView.Room, evt).Should() - mainView.NotifyMessage(roomView.Room, message, pushRules) + pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() + mainView.NotifyMessage(roomView.MxRoom(), message, pushRules) } - roomView.AddMessage(message, widget.AppendMessage) + roomView.AddMessage(message, ifc.AppendMessage) c.ui.Render() } } @@ -255,8 +256,7 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) { if evt.Unsigned.PrevContent != nil { prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string) } - const Hour = 1 * 60 * 60 * 1000 - if membership == prevMembership || evt.Unsigned.Age > Hour { + if membership == prevMembership { return } switch membership { @@ -282,15 +282,15 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) { message := mainView.ProcessMembershipEvent(roomView, evt) if message != nil { // TODO this shouldn't be necessary - roomView.Room.UpdateState(evt) + roomView.MxRoom().UpdateState(evt) // TODO This should probably also be in a different place roomView.UpdateUserList() if c.syncer.FirstSyncDone { - pushRules := c.PushRules().GetActions(roomView.Room, evt).Should() - mainView.NotifyMessage(roomView.Room, message, pushRules) + pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() + mainView.NotifyMessage(roomView.MxRoom(), message, pushRules) } - roomView.AddMessage(message, widget.AppendMessage) + roomView.AddMessage(message, ifc.AppendMessage) c.ui.Render() } } -- cgit v1.2.3 From 8270bc0322ac262f4b48c92d5fad25cf9634f1bb Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 10 Apr 2018 19:31:28 +0300 Subject: Move event parsing to ui/messages and add image displaying --- matrix/matrix.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 10 deletions(-) (limited to 'matrix') diff --git a/matrix/matrix.go b/matrix/matrix.go index 6fff6d8..29419b0 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -17,19 +17,25 @@ package matrix import ( + "bytes" "encoding/json" "fmt" + "io" + "io/ioutil" + "net/url" + "os" + "path" + "path/filepath" + "regexp" "strings" "time" - -"maunium.net/go/gomatrix" -"maunium.net/go/gomuks/config" -"maunium.net/go/gomuks/debug" -"maunium.net/go/gomuks/interface" -"maunium.net/go/gomuks/matrix/pushrules" -"maunium.net/go/gomuks/matrix/rooms" - + "maunium.net/go/gomatrix" + "maunium.net/go/gomuks/config" + "maunium.net/go/gomuks/debug" + "maunium.net/go/gomuks/interface" + "maunium.net/go/gomuks/matrix/pushrules" + "maunium.net/go/gomuks/matrix/rooms" ) // Container is a wrapper for a gomatrix Client and some other stuff. @@ -222,7 +228,7 @@ func (c *Container) HandleMessage(evt *gomatrix.Event) { return } - message := mainView.ProcessMessageEvent(roomView, evt) + message := mainView.ParseEvent(roomView, evt) if message != nil { if c.syncer.FirstSyncDone { pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() @@ -279,7 +285,7 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) { return } - message := mainView.ProcessMembershipEvent(roomView, evt) + message := mainView.ParseEvent(roomView, evt) if message != nil { // TODO this shouldn't be necessary roomView.MxRoom().UpdateState(evt) @@ -403,3 +409,53 @@ func (c *Container) GetRoom(roomID string) *rooms.Room { } return room } + +var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)") + +func (c *Container) Download(mxcURL string) ([]byte, error) { + parts := mxcRegex.FindStringSubmatch(mxcURL) + if parts == nil || len(parts) != 3 { + debug.Print(parts) + return nil, fmt.Errorf("invalid matrix content URL") + } + hs := parts[1] + id := parts[2] + + cacheFile := c.getCachePath(hs, id) + if _, err := os.Stat(cacheFile); err != nil { + data, err := ioutil.ReadFile(cacheFile) + if err == nil { + return data, nil + } + } + + dlURL, _ := url.Parse(c.client.HomeserverURL.String()) + dlURL.Path = path.Join(dlURL.Path, "/_matrix/media/v1/download", hs, id) + + resp, err := c.client.Client.Get(dlURL.String()) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var buf bytes.Buffer + _, err = io.Copy(&buf, resp.Body) + if err != nil { + return nil, err + } + + data := buf.Bytes() + + err = ioutil.WriteFile(cacheFile, data, 0600) + return data, err +} +func (c *Container) getCachePath(homeserver, fileID string) string { + dir := filepath.Join(c.config.MediaDir, homeserver) + + err := os.MkdirAll(dir, 0700) + if err != nil { + return "" + } + + return filepath.Join(dir, fileID) +} -- cgit v1.2.3 From ff7ee333a1028850337aa332532cbc23c7bdde14 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 11 Apr 2018 17:57:15 +0300 Subject: Rename UIString to TString, move ansimage to lib/ and switch to tcell fork --- matrix/matrix.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'matrix') diff --git a/matrix/matrix.go b/matrix/matrix.go index 29419b0..b4d5775 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -22,6 +22,7 @@ import ( "fmt" "io" "io/ioutil" + "net/http" "net/url" "os" "path" @@ -412,42 +413,44 @@ func (c *Container) GetRoom(roomID string) *rooms.Room { var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)") -func (c *Container) Download(mxcURL string) ([]byte, error) { +func (c *Container) Download(mxcURL string) (data []byte, fullID string, err error) { parts := mxcRegex.FindStringSubmatch(mxcURL) if parts == nil || len(parts) != 3 { - debug.Print(parts) - return nil, fmt.Errorf("invalid matrix content URL") + err = fmt.Errorf("invalid matrix content URL") + return } hs := parts[1] id := parts[2] + fullID = fmt.Sprintf("%s/%s", hs, id) cacheFile := c.getCachePath(hs, id) - if _, err := os.Stat(cacheFile); err != nil { - data, err := ioutil.ReadFile(cacheFile) + if _, err = os.Stat(cacheFile); err != nil { + data, err = ioutil.ReadFile(cacheFile) if err == nil { - return data, nil + return } } dlURL, _ := url.Parse(c.client.HomeserverURL.String()) dlURL.Path = path.Join(dlURL.Path, "/_matrix/media/v1/download", hs, id) - resp, err := c.client.Client.Get(dlURL.String()) + var resp *http.Response + resp, err = c.client.Client.Get(dlURL.String()) if err != nil { - return nil, err + return } defer resp.Body.Close() var buf bytes.Buffer _, err = io.Copy(&buf, resp.Body) if err != nil { - return nil, err + return } - data := buf.Bytes() + data = buf.Bytes() err = ioutil.WriteFile(cacheFile, data, 0600) - return data, err + return } func (c *Container) getCachePath(homeserver, fileID string) string { dir := filepath.Join(c.config.MediaDir, homeserver) -- cgit v1.2.3 From 92a2428865dfde80af6f9ffba0f59567b63230b6 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 11 Apr 2018 19:20:40 +0300 Subject: Allow clicking images and load images from cache --- matrix/matrix.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'matrix') diff --git a/matrix/matrix.go b/matrix/matrix.go index b4d5775..48bb57f 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -413,17 +413,16 @@ func (c *Container) GetRoom(roomID string) *rooms.Room { var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)") -func (c *Container) Download(mxcURL string) (data []byte, fullID string, err error) { +func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err error) { parts := mxcRegex.FindStringSubmatch(mxcURL) if parts == nil || len(parts) != 3 { err = fmt.Errorf("invalid matrix content URL") return } - hs := parts[1] - id := parts[2] - fullID = fmt.Sprintf("%s/%s", hs, id) + hs = parts[1] + id = parts[2] - cacheFile := c.getCachePath(hs, id) + cacheFile := c.GetCachePath(hs, id) if _, err = os.Stat(cacheFile); err != nil { data, err = ioutil.ReadFile(cacheFile) if err == nil { @@ -452,7 +451,8 @@ func (c *Container) Download(mxcURL string) (data []byte, fullID string, err err err = ioutil.WriteFile(cacheFile, data, 0600) return } -func (c *Container) getCachePath(homeserver, fileID string) string { + +func (c *Container) GetCachePath(homeserver, fileID string) string { dir := filepath.Join(c.config.MediaDir, homeserver) err := os.MkdirAll(dir, 0700) -- cgit v1.2.3 From e7bf5bd59fc0a43172b6ab5b338e1d60bd4b3bbb Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 14 Apr 2018 00:34:25 +0300 Subject: Add basic HTML rendering (ref #16) --- matrix/rooms/room.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'matrix') diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go index 7dd2af4..2e12a28 100644 --- a/matrix/rooms/room.go +++ b/matrix/rooms/room.go @@ -94,11 +94,7 @@ func (room *Room) UpdateState(event *gomatrix.Event) { room.memberCache = nil room.firstMemberCache = "" fallthrough - case "m.room.name": - fallthrough - case "m.room.canonical_alias": - fallthrough - case "m.room.alias": + case "m.room.name", "m.room.canonical_alias", "m.room.alias": room.nameCache = "" case "m.room.topic": room.topicCache = "" -- cgit v1.2.3