From 1b0333d46868beecd870364b37817900bfd462a7 Mon Sep 17 00:00:00 2001 From: erinn Date: Tue, 14 Dec 2021 13:23:32 -0800 Subject: [PATCH 1/5] wip image previews --- utils/eventHandler.go | 41 ++++++++++++++++++++++++++++++++++++++--- utils/settings.go | 11 ++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 99921a8..a3cf1bd 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -1,20 +1,24 @@ package utils import ( + "encoding/json" + "strconv" + "strings" + "cwtch.im/cwtch/app" "cwtch.im/cwtch/app/plugins" "cwtch.im/cwtch/model" "cwtch.im/cwtch/model/attr" "cwtch.im/cwtch/model/constants" "cwtch.im/cwtch/protocol/connections" - "encoding/json" constants2 "git.openprivacy.ca/cwtch.im/libcwtch-go/constants" "git.openprivacy.ca/cwtch.im/libcwtch-go/features/groups" "git.openprivacy.ca/cwtch.im/libcwtch-go/features/servers" "git.openprivacy.ca/openprivacy/log" - "strconv" + + "cwtch.im/cwtch/event" + "cwtch.im/cwtch/functionality/filesharing" ) -import "cwtch.im/cwtch/event" type EventProfileEnvelope struct { Event event.Event @@ -262,6 +266,37 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { ev.Event.Data["Nick"] = ph.GetNick(ev.Event.Data["RemotePeer"]) ev.Event.Data["Picture"] = ph.GetProfilePic(ev.Event.Data["RemotePeer"]) peer.SetContactAttribute(ev.Event.Data["RemotePeer"], attr.GetLocalScope(constants2.Archived), event.False) + + fh, err := filesharing.PreviewFunctionalityGate(ReadGlobalSettings().Experiments) + if err == nil { + var cm model.MessageWrapper + err := json.Unmarshal([]byte(ev.Event.Data[event.Data]), &cm) + if err == nil && cm.Overlay == model.OverlayFileSharing { + var fm filesharing.OverlayMessage + log.Debugf("parsing: %v", cm.Data) + err = json.Unmarshal([]byte(cm.Data), &fm) + if err == nil { + lname := strings.ToLower(fm.Name) + if strings.HasSuffix(lname, "jpg") || strings.HasSuffix(lname, "jpeg") || strings.HasSuffix(lname, "png") || strings.HasSuffix(lname, "gif") || strings.HasSuffix(lname, "webp") || strings.HasSuffix(lname, "bmp") { + settings := ReadGlobalSettings() + basepath := settings.DownloadPath + //todo: security + fp, mp := filesharing.GenerateDownloadPath(basepath, fm.Name) + log.Debugf("NMFP: autodownloading file!!!") + log.Debugf("%v | %v | %v | %v | %v | %v", basepath, fm.Name, fp, mp, fm.FileKey(), fm) + fh.DownloadFile(peer, ev.Event.Data[event.RemotePeer], fp, mp, fm.FileKey()) + } else { + log.Debugf("NMFP: suffix not jpg/jpeg/png") + } + } else { + log.Debugf("NMFP: not a file overlay message") + } + } else { + log.Debugf("NMFP: not a message wrapper") + } + } else { + log.Debugf("NMFP: file experiment is off") + } case event.NewMessageFromGroup: // only needs contact nickname and picture, for displaying on popup notifications ev.Event.Data["Nick"] = ph.GetNick(ev.Event.Data[event.GroupID]) diff --git a/utils/settings.go b/utils/settings.go index c28379f..6fd7c5a 100644 --- a/utils/settings.go +++ b/utils/settings.go @@ -1,15 +1,17 @@ package utils import ( - "cwtch.im/cwtch/event" - "cwtch.im/cwtch/storage/v1" "sync" + "cwtch.im/cwtch/event" + "cwtch.im/cwtch/storage/v1" + "encoding/json" - "git.openprivacy.ca/openprivacy/log" "io/ioutil" "os" "path" + + "git.openprivacy.ca/openprivacy/log" ) const ( @@ -36,6 +38,7 @@ type GlobalSettings struct { FirstTime bool UIColumnModePortrait string UIColumnModeLandscape string + DownloadPath string } var DefaultGlobalSettings = GlobalSettings{ @@ -50,6 +53,7 @@ var DefaultGlobalSettings = GlobalSettings{ StreamerMode: false, UIColumnModePortrait: "DualpaneMode.Single", UIColumnModeLandscape: "DualpaneMode.CopyPortrait", + DownloadPath: "", } func InitGlobalSettingsFile(directory string, password string) error { @@ -111,6 +115,7 @@ func WriteGlobalSettings(globalSettings GlobalSettings) { lock.Lock() defer lock.Unlock() bytes, _ := json.Marshal(globalSettings) + log.Debugf("writing settings: %v", string(bytes)) // override first time setting globalSettings.FirstTime = true err := GlobalSettingsFile.Write(bytes) From d59d54ab6adb5315c99aa13acfa9217a34cc5e61 Mon Sep 17 00:00:00 2001 From: erinn Date: Thu, 16 Dec 2021 16:43:23 -0800 Subject: [PATCH 2/5] image previews --- utils/eventHandler.go | 53 +++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 1103e79..ab0b858 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -3,7 +3,6 @@ package utils import ( "encoding/json" "strconv" - "strings" "cwtch.im/cwtch/app" "cwtch.im/cwtch/app/plugins" @@ -15,14 +14,11 @@ import ( "git.openprivacy.ca/cwtch.im/libcwtch-go/features/groups" "git.openprivacy.ca/cwtch.im/libcwtch-go/features/servers" "git.openprivacy.ca/openprivacy/log" -<<<<<<< HEAD + + "time" "cwtch.im/cwtch/event" "cwtch.im/cwtch/functionality/filesharing" -======= - "strconv" - "time" ->>>>>>> aa102bd169a9f4d71b18d9c16c590f61ba827ab3 ) type EventProfileEnvelope struct { @@ -280,31 +276,21 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { err := json.Unmarshal([]byte(ev.Event.Data[event.Data]), &cm) if err == nil && cm.Overlay == model.OverlayFileSharing { var fm filesharing.OverlayMessage - log.Debugf("parsing: %v", cm.Data) err = json.Unmarshal([]byte(cm.Data), &fm) if err == nil { - lname := strings.ToLower(fm.Name) - if strings.HasSuffix(lname, "jpg") || strings.HasSuffix(lname, "jpeg") || strings.HasSuffix(lname, "png") || strings.HasSuffix(lname, "gif") || strings.HasSuffix(lname, "webp") || strings.HasSuffix(lname, "bmp") { + if fm.ShouldAutoDL() { settings := ReadGlobalSettings() basepath := settings.DownloadPath - //todo: security fp, mp := filesharing.GenerateDownloadPath(basepath, fm.Name) - log.Debugf("NMFP: autodownloading file!!!") - log.Debugf("%v | %v | %v | %v | %v | %v", basepath, fm.Name, fp, mp, fm.FileKey(), fm) - fh.DownloadFile(peer, ev.Event.Data[event.RemotePeer], fp, mp, fm.FileKey()) - } else { - log.Debugf("NMFP: suffix not jpg/jpeg/png") + log.Debugf("autodownloading file!") + ev.Event.Data["Auto"] = "true" + mID, _ := strconv.Atoi(ev.Event.Data["Index"]) + profile.UpdateMessageAttribute(ci.ID, 0, mID, "file-downloaded", "true") + fh.DownloadFile(profile, ci.ID, fp, mp, fm.FileKey()) } - } else { - log.Debugf("NMFP: not a file overlay message") } - } else { - log.Debugf("NMFP: not a message wrapper") } - } else { - log.Debugf("NMFP: file experiment is off") } - case event.NewMessageFromGroup: // only needs contact nickname and picture, for displaying on popup notifications ci, err := profile.FetchConversationInfo(ev.Event.Data["RemotePeer"]) @@ -321,6 +307,29 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { ev.Event.Data["Picture"] = RandomProfileImage(ev.Event.Data[event.RemotePeer]) conversationID, _ := strconv.Atoi(ev.Event.Data[event.ConversationID]) profile.SetConversationAttribute(conversationID, attr.LocalScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(constants2.Archived)), event.False) + + // note careful use of ci.ID versus conversationID + fh, err := filesharing.PreviewFunctionalityGate(ReadGlobalSettings().Experiments) + if err == nil && ci != nil && ci.Accepted { + var cm model.MessageWrapper + err := json.Unmarshal([]byte(ev.Event.Data[event.Data]), &cm) + if err == nil && cm.Overlay == model.OverlayFileSharing { + var fm filesharing.OverlayMessage + err = json.Unmarshal([]byte(cm.Data), &fm) + if err == nil { + if fm.ShouldAutoDL() { + settings := ReadGlobalSettings() + basepath := settings.DownloadPath + fp, mp := filesharing.GenerateDownloadPath(basepath, fm.Name) + log.Debugf("autodownloading file!") + ev.Event.Data["Auto"] = "true" + mID, _ := strconv.Atoi(ev.Event.Data["Index"]) + profile.UpdateMessageAttribute(conversationID, 0, mID, "file-downloaded", "true") + fh.DownloadFile(profile, ci.ID, fp, mp, fm.FileKey()) + } + } + } + } case event.PeerAcknowledgement: ci, err := profile.FetchConversationInfo(ev.Event.Data["RemotePeer"]) if ci != nil && err == nil { From eb33f1112700ae315668f9d4459e6ea221ec27e8 Mon Sep 17 00:00:00 2001 From: erinn Date: Thu, 16 Dec 2021 16:50:21 -0800 Subject: [PATCH 3/5] message previews - remove debug --- utils/settings.go | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/settings.go b/utils/settings.go index 3d3dddd..cf4ee70 100644 --- a/utils/settings.go +++ b/utils/settings.go @@ -116,7 +116,6 @@ func WriteGlobalSettings(globalSettings GlobalSettings) { lock.Lock() defer lock.Unlock() bytes, _ := json.Marshal(globalSettings) - log.Debugf("writing settings: %v", string(bytes)) // override first time setting globalSettings.FirstTime = true err := GlobalSettingsFile.Write(bytes) From f4ff8024606b2d370c22adfb15d09a70256c7e69 Mon Sep 17 00:00:00 2001 From: erinn Date: Sat, 18 Dec 2021 17:16:21 -0800 Subject: [PATCH 4/5] image previews sarah comments --- go.mod | 2 ++ lib.go | 17 ++++++----- utils/eventHandler.go | 71 ++++++++++++++++++------------------------- 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/go.mod b/go.mod index b5e5651..ab40980 100644 --- a/go.mod +++ b/go.mod @@ -10,3 +10,5 @@ require ( github.com/mutecomm/go-sqlcipher/v4 v4.4.2 golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect ) + +replace cwtch.im/cwtch => /home/erinn/Android/Goprojects/cwtch diff --git a/lib.go b/lib.go index 3eb5a77..138c172 100644 --- a/lib.go +++ b/lib.go @@ -8,17 +8,20 @@ import "C" import ( "crypto/rand" - constants2 "cwtch.im/cwtch/model/constants" "encoding/json" "fmt" + + constants2 "cwtch.im/cwtch/model/constants" + // Import SQL Cipher - _ "github.com/mutecomm/go-sqlcipher/v4" "os/user" "runtime" "strconv" "strings" "unsafe" + _ "github.com/mutecomm/go-sqlcipher/v4" + "cwtch.im/cwtch/app" "cwtch.im/cwtch/event" "cwtch.im/cwtch/functionality/filesharing" @@ -92,11 +95,11 @@ func StartCwtch(appDir string, torPath string) int { if runtime.GOOS == "android" { log.SetUseColor(false) } - log.SetLevel(log.LevelInfo) - if logLevel := os.Getenv("LOG_LEVEL"); strings.ToLower(logLevel) == "debug" { - log.SetLevel(log.LevelDebug) - } - + // log.SetLevel(log.LevelInfo) + // if logLevel := os.Getenv("LOG_LEVEL"); strings.ToLower(logLevel) == "debug" { + // log.SetLevel(log.LevelDebug) + // } + log.SetLevel(log.LevelDebug) log.Infof("StartCwtch(...)") // Quick hack check that we're being called with the correct params // On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker diff --git a/utils/eventHandler.go b/utils/eventHandler.go index bedabf9..7316544 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -9,6 +9,7 @@ import ( "cwtch.im/cwtch/model" "cwtch.im/cwtch/model/attr" "cwtch.im/cwtch/model/constants" + "cwtch.im/cwtch/peer" "cwtch.im/cwtch/protocol/connections" constants2 "git.openprivacy.ca/cwtch.im/libcwtch-go/constants" "git.openprivacy.ca/cwtch.im/libcwtch-go/features/groups" @@ -268,26 +269,8 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { } ev.Event.Data["Picture"] = RandomProfileImage(ev.Event.Data["RemotePeer"]) - fh, err := filesharing.PreviewFunctionalityGate(ReadGlobalSettings().Experiments) - if err == nil && ci.Accepted { - var cm model.MessageWrapper - err := json.Unmarshal([]byte(ev.Event.Data[event.Data]), &cm) - if err == nil && cm.Overlay == model.OverlayFileSharing { - var fm filesharing.OverlayMessage - err = json.Unmarshal([]byte(cm.Data), &fm) - if err == nil { - if fm.ShouldAutoDL() { - settings := ReadGlobalSettings() - basepath := settings.DownloadPath - fp, mp := filesharing.GenerateDownloadPath(basepath, fm.Name) - log.Debugf("autodownloading file!") - ev.Event.Data["Auto"] = "true" - mID, _ := strconv.Atoi(ev.Event.Data["Index"]) - profile.UpdateMessageAttribute(ci.ID, 0, mID, "file-downloaded", "true") - fh.DownloadFile(profile, ci.ID, fp, mp, fm.FileKey()) - } - } - } + if ci.Accepted { + handleImagePreviews(profile, &ev.Event, ci.ID, ci.ID) } case event.NewMessageFromGroup: // only needs contact nickname and picture, for displaying on popup notifications @@ -306,27 +289,8 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { conversationID, _ := strconv.Atoi(ev.Event.Data[event.ConversationID]) profile.SetConversationAttribute(conversationID, attr.LocalScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(constants2.Archived)), event.False) - // note careful use of ci.ID versus conversationID - fh, err := filesharing.PreviewFunctionalityGate(ReadGlobalSettings().Experiments) - if err == nil && ci != nil && ci.Accepted { - var cm model.MessageWrapper - err := json.Unmarshal([]byte(ev.Event.Data[event.Data]), &cm) - if err == nil && cm.Overlay == model.OverlayFileSharing { - var fm filesharing.OverlayMessage - err = json.Unmarshal([]byte(cm.Data), &fm) - if err == nil { - if fm.ShouldAutoDL() { - settings := ReadGlobalSettings() - basepath := settings.DownloadPath - fp, mp := filesharing.GenerateDownloadPath(basepath, fm.Name) - log.Debugf("autodownloading file!") - ev.Event.Data["Auto"] = "true" - mID, _ := strconv.Atoi(ev.Event.Data["Index"]) - profile.UpdateMessageAttribute(conversationID, 0, mID, "file-downloaded", "true") - fh.DownloadFile(profile, ci.ID, fp, mp, fm.FileKey()) - } - } - } + if ci != nil && ci.Accepted { + handleImagePreviews(profile, &ev.Event, conversationID, ci.ID) } case event.PeerAcknowledgement: ci, err := profile.FetchConversationInfo(ev.Event.Data["RemotePeer"]) @@ -501,3 +465,28 @@ func getLastMessageTime(conversationMessages []model.ConversationMessage) int { } return int(time.Unix()) } + +// handleImagePreviews checks settings and, if appropriate, auto-downloads any images +func handleImagePreviews(profile peer.CwtchPeer, ev *event.Event, conversationID, senderID int) { + settings := ReadGlobalSettings() + fh, err := filesharing.PreviewFunctionalityGate(settings.Experiments) + if err == nil { + var cm model.MessageWrapper + err := json.Unmarshal([]byte(ev.Data[event.Data]), &cm) + if err == nil && cm.Overlay == model.OverlayFileSharing { + var fm filesharing.OverlayMessage + err = json.Unmarshal([]byte(cm.Data), &fm) + if err == nil { + if fm.ShouldAutoDL() { + basepath := settings.DownloadPath + fp, mp := filesharing.GenerateDownloadPath(basepath, fm.Name) + log.Debugf("autodownloading file!") + ev.Data["Auto"] = constants.True + mID, _ := strconv.Atoi(ev.Data["Index"]) + profile.UpdateMessageAttribute(conversationID, 0, mID, constants.AttrDownloaded, constants.True) + fh.DownloadFile(profile, senderID, fp, mp, fm.FileKey()) + } + } + } + } +} From 11bbc3aa782fee932ffcdc97d378ec3bf09d09d7 Mon Sep 17 00:00:00 2001 From: erinn Date: Sat, 18 Dec 2021 17:20:08 -0800 Subject: [PATCH 5/5] bump cwtch and fix miscommits --- go.mod | 4 +--- go.sum | 1 + lib.go | 9 ++++----- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index ab40980..8b7e273 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,10 @@ module git.openprivacy.ca/cwtch.im/libcwtch-go go 1.15 require ( - cwtch.im/cwtch v0.14.5 + cwtch.im/cwtch v0.14.6 git.openprivacy.ca/cwtch.im/server v1.4.1 git.openprivacy.ca/openprivacy/connectivity v1.5.0 git.openprivacy.ca/openprivacy/log v1.0.3 github.com/mutecomm/go-sqlcipher/v4 v4.4.2 golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect ) - -replace cwtch.im/cwtch => /home/erinn/Android/Goprojects/cwtch diff --git a/go.sum b/go.sum index 02918ee..fa5ea4f 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,7 @@ cwtch.im/cwtch v0.14.4 h1:QbQVzrAqczAG5tGazQvRVfN7MyQPWVN5Fwv7MzCXZs4= cwtch.im/cwtch v0.14.4/go.mod h1:/fLuoYLY/7JHw6RojFojpd245CiOcU24QpWqzh9FRDI= cwtch.im/cwtch v0.14.5 h1:GY86C8UgqmuGh7ATR4C14hndc+c7PwPI8WH3l2iwnaQ= cwtch.im/cwtch v0.14.5/go.mod h1:/fLuoYLY/7JHw6RojFojpd245CiOcU24QpWqzh9FRDI= +cwtch.im/cwtch v0.14.6/go.mod h1:/fLuoYLY/7JHw6RojFojpd245CiOcU24QpWqzh9FRDI= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= diff --git a/lib.go b/lib.go index 138c172..26d1a0e 100644 --- a/lib.go +++ b/lib.go @@ -95,11 +95,10 @@ func StartCwtch(appDir string, torPath string) int { if runtime.GOOS == "android" { log.SetUseColor(false) } - // log.SetLevel(log.LevelInfo) - // if logLevel := os.Getenv("LOG_LEVEL"); strings.ToLower(logLevel) == "debug" { - // log.SetLevel(log.LevelDebug) - // } - log.SetLevel(log.LevelDebug) + log.SetLevel(log.LevelInfo) + if logLevel := os.Getenv("LOG_LEVEL"); strings.ToLower(logLevel) == "debug" { + log.SetLevel(log.LevelDebug) + } log.Infof("StartCwtch(...)") // Quick hack check that we're being called with the correct params // On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker