From 8040385681a3f22e4dd565568e3d8030021ac72e Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 8 Feb 2022 12:43:57 -0800 Subject: [PATCH 1/4] Fixup Logic for Custom Profile Image Downloads (restrict to Accepted contacts), stronger defaults --- utils/eventHandler.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 32cf941..6fb0664 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -3,6 +3,7 @@ package utils import ( "encoding/json" "fmt" + "os" "strconv" "cwtch.im/cwtch/app" @@ -113,7 +114,8 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { // If the user has chosen to block unknown profiles // then explicitly configure the protocol engine to do so.. - if ReadGlobalSettings().BlockUnknownConnections { + settings := ReadGlobalSettings() + if settings.BlockUnknownConnections { profile.BlockUnknownConnections() } else { // For completeness @@ -181,7 +183,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { if conversationInfo.IsGroup() { cpicPath = RandomGroupImage(conversationInfo.Handle) } else { - cpicPath = GetProfileImage(profile, conversationInfo) + cpicPath = GetProfileImage(profile, conversationInfo, settings.DownloadPath) } // Resolve Save History Setting @@ -255,12 +257,16 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { return string(json) } -func GetProfileImage(profile peer.CwtchPeer, conversationInfo *model.Conversation) string { +func GetProfileImage(profile peer.CwtchPeer, conversationInfo *model.Conversation, basepath string) string { fileKey, err := profile.GetConversationAttribute(conversationInfo.ID, attr.PublicScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(constants.CustomProfileImageKey))) if err == nil { if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { - image, _ := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.path", fileKey)) - return image + fp, _ := filesharing.GenerateDownloadPath(basepath, fileKey, true) + // check if the file exists...if it does then set the path... + if _, err := os.Stat(fp); err == nil { + image, _ := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.path", fileKey)) + return image + } } } return RandomProfileImage(conversationInfo.Handle) @@ -284,7 +290,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { if ci != nil && err == nil { ev.Event.Data[event.ConversationID] = strconv.Itoa(ci.ID) profile.SetConversationAttribute(ci.ID, attr.LocalScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(constants2.Archived)), event.False) - ev.Event.Data[constants2.Picture] = GetProfileImage(profile, ci) + ev.Event.Data[constants2.Picture] = GetProfileImage(profile, ci, ReadGlobalSettings().DownloadPath) } else { // TODO This Conversation May Not Exist Yet...But we are not in charge of creating it... log.Errorf("todo wait for contact to be added before processing this event...") @@ -318,7 +324,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { ev.Event.Data["Nick"] = ev.Event.Data["RemotePeer"] } } - ev.Event.Data[constants2.Picture] = GetProfileImage(profile, ci) + ev.Event.Data[constants2.Picture] = GetProfileImage(profile, ci, ReadGlobalSettings().DownloadPath) } conversationID, _ := strconv.Atoi(ev.Event.Data[event.ConversationID]) @@ -449,18 +455,23 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { fileKey := val fsf, err := filesharing.FunctionalityGate(settings.Experiments) imagePreviewsEnabled := settings.Experiments["filesharing-images"] - if err == nil && imagePreviewsEnabled { + if err == nil && imagePreviewsEnabled && conversation.Accepted { + if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { + basepath := settings.DownloadPath + fp, _ := filesharing.GenerateDownloadPath(basepath, fileKey, true) + if _, err := os.Stat(fp); err == nil { + // file is marked as completed downloaded and exists... + return "" + } + } basepath := settings.DownloadPath fp, mp := filesharing.GenerateDownloadPath(basepath, fileKey, true) log.Debugf("Downloading Profile Image %v %v %v", fp, mp, fileKey) ev.Event.Data[event.FilePath] = fp - if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { - ev.Event.Data[event.FileDownloadFinished] = constants.True - } else { - ev.Event.Data[event.FileDownloadFinished] = constants.False - fsf.DownloadFile(profile, conversation.ID, fp, mp, val, constants.ImagePreviewMaxSizeInBytes) - } + ev.Event.Data[event.FileDownloadFinished] = constants.False + fsf.DownloadFile(profile, conversation.ID, fp, mp, val, constants.ImagePreviewMaxSizeInBytes) } else { + // if image previews are disabled then ignore this event... return "" } } From 08774268a813952543f08c364e793cc4f4a889be Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 8 Feb 2022 12:59:33 -0800 Subject: [PATCH 2/4] simplify profile image download if case --- utils/eventHandler.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 6fb0664..fa204ae 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -456,16 +456,17 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { fsf, err := filesharing.FunctionalityGate(settings.Experiments) imagePreviewsEnabled := settings.Experiments["filesharing-images"] if err == nil && imagePreviewsEnabled && conversation.Accepted { + + basepath := settings.DownloadPath + fp, mp := filesharing.GenerateDownloadPath(basepath, fileKey, true) + if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { - basepath := settings.DownloadPath - fp, _ := filesharing.GenerateDownloadPath(basepath, fileKey, true) if _, err := os.Stat(fp); err == nil { // file is marked as completed downloaded and exists... return "" } } - basepath := settings.DownloadPath - fp, mp := filesharing.GenerateDownloadPath(basepath, fileKey, true) + log.Debugf("Downloading Profile Image %v %v %v", fp, mp, fileKey) ev.Event.Data[event.FilePath] = fp ev.Event.Data[event.FileDownloadFinished] = constants.False From 50b7a43466e1676db24b848ed7b6a66a32011de1 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 8 Feb 2022 13:09:04 -0800 Subject: [PATCH 3/4] Check complete status against file existence --- utils/eventHandler.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index fa204ae..981533f 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -460,16 +460,18 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { basepath := settings.DownloadPath fp, mp := filesharing.GenerateDownloadPath(basepath, fileKey, true) - if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { + if value, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists && value == event.True { if _, err := os.Stat(fp); err == nil { // file is marked as completed downloaded and exists... return "" + } else { + // the user probably deleted the file, mark completed as false... + profile.SetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey), event.False) } } log.Debugf("Downloading Profile Image %v %v %v", fp, mp, fileKey) ev.Event.Data[event.FilePath] = fp - ev.Event.Data[event.FileDownloadFinished] = constants.False fsf.DownloadFile(profile, conversation.ID, fp, mp, val, constants.ImagePreviewMaxSizeInBytes) } else { // if image previews are disabled then ignore this event... From db8e43cb0548201a279c7bb9992de032c93dd473 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 8 Feb 2022 13:13:33 -0800 Subject: [PATCH 4/4] fixup filekey.complete checks to check the actual value instead of just existence --- lib.go | 2 +- utils/eventHandler.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib.go b/lib.go index 8823381..73ff576 100644 --- a/lib.go +++ b/lib.go @@ -775,7 +775,7 @@ func c_CheckDownloadStatus(profilePtr *C.char, profileLen C.int, fileKeyPtr *C.c func CheckDownloadStatus(profileOnion, fileKey string) { profile := application.GetPeer(profileOnion) path, _ := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.path", fileKey)) - if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { + if value, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists && value == event.True { eventHandler.Push(event.NewEvent(event.FileDownloaded, map[event.Field]string{ ProfileOnion: profileOnion, event.FileKey: fileKey, diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 981533f..1e80136 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -260,7 +260,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { func GetProfileImage(profile peer.CwtchPeer, conversationInfo *model.Conversation, basepath string) string { fileKey, err := profile.GetConversationAttribute(conversationInfo.ID, attr.PublicScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(constants.CustomProfileImageKey))) if err == nil { - if _, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists { + if value, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey)); exists && value == event.True { fp, _ := filesharing.GenerateDownloadPath(basepath, fileKey, true) // check if the file exists...if it does then set the path... if _, err := os.Stat(fp); err == nil {