image previews sarah comments
continuous-integration/drone/pr Build is pending Details

This commit is contained in:
erinn 2021-12-18 17:16:21 -08:00
parent 8b38a8b159
commit f4ff802460
3 changed files with 42 additions and 48 deletions

2
go.mod
View File

@ -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

17
lib.go
View File

@ -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

View File

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