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()) + } + } + } + } +}