diff --git a/lib.go b/lib.go index 2032a18..a990be9 100644 --- a/lib.go +++ b/lib.go @@ -13,6 +13,7 @@ import ( contact "git.openprivacy.ca/flutter/libcwtch-go/features/contacts" "git.openprivacy.ca/flutter/libcwtch-go/features/groups" "git.openprivacy.ca/openprivacy/connectivity" + "runtime" "strings" "encoding/json" @@ -54,15 +55,25 @@ type ChatMessage struct { } //export c_StartCwtch -func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) { +func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) int8 { dir := C.GoStringN(dir_c, len) tor := C.GoStringN(tor_c, torLen) - StartCwtch(dir, tor) + return StartCwtch(dir, tor) } -func StartCwtch(appDir string, torPath string) { +func StartCwtch(appDir string, torPath string) int8 { log.SetLevel(log.LevelInfo) + if runtime.GOOS == "android" { + fh, err := os.Open(torPath) + if err != nil { + log.Errorf("%v", err) + log.Errorf("failed to stat tor, skipping StartCwtch(). potentially normal if the app was reinstalled or the device was restarted; this workorder should get canceled soon") + return 1 + } + _ = fh.Close() + } + // Exclude Tapir wire Messages (We need a TRACE level) log.ExcludeFromPattern("service.go") @@ -126,6 +137,21 @@ func StartCwtch(appDir string, torPath string) { // Send global settings to the UI... application.GetPrimaryBus().Publish(event.NewEvent(utils.UpdateGlobalSettings, map[event.Field]string{event.Data: string(settingsJson)})) log.Infof("libcwtch-go application launched") + return 0 +} + +//export c_ReconnectCwtchForeground +func c_ReconnectCwtchForeground() { + ReconnectCwtchForeground() +} + +// Like StartCwtch, but StartCwtch has already been called so we don't need to restart Tor etc (probably) +// Do need to re-send initial state tho, eg profiles that are already loaded +func ReconnectCwtchForeground() { + peerList := application.ListPeers() + for onion := range peerList { + eventHandler.Push(event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: onion, event.Created: event.False})) + } } //export c_ACNEvents @@ -507,8 +533,10 @@ func GetMessage(profileOnion, handle string, message_index int) string { } } } else { - message.Message = profile.GetContact(handle).Timeline.Messages[message_index] - message.ContactImage = ph.GetProfilePic(handle) + if message_index < len(profile.GetContact(handle).Timeline.Messages) { + message.Message = profile.GetContact(handle).Timeline.Messages[message_index] + message.ContactImage = ph.GetProfilePic(handle) + } } bytes, _ := json.Marshal(message) return string(bytes) diff --git a/utils/errorACN.go b/utils/errorACN.go index 951aec2..a39464b 100644 --- a/utils/errorACN.go +++ b/utils/errorACN.go @@ -3,6 +3,7 @@ package utils import ( "errors" "git.openprivacy.ca/openprivacy/connectivity" + "git.openprivacy.ca/openprivacy/log" "net" ) @@ -26,6 +27,7 @@ func (e ErrorACN) SetStatusCallback(callback func(int, string)) { func (e ErrorACN) Restart() { // does nothing + log.Errorf("Called Restart() on ErrorACN, which does nothing") } func (e ErrorACN) Open(hostname string) (net.Conn, string, error) { diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 2351fa8..7709f66 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -222,21 +222,9 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { }*/ case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data - // I Don't think we need to enrich - - // TODO: deprecate and move to dart - /*func (p *PeerHelper) updateLastReadTime(id string) { - lastRead, _ := time.Now().MarshalText() - if p.IsGroup(id) { - p.peer.SetGroupAttribute(id, attr.GetLocalScope(constants.LastRead), string(lastRead)) - } else { - p.peer.SetContactAttribute(id, attr.GetLocalScope(constants.LastRead), string(lastRead)) - } - }*/ - - // legacy - //ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampReceived]) - + // only needs contact nickname and picture, for displaying on popup notifications + ev.Event.Data["Nick"] = ph.GetNick(ev.Event.Data["RemotePeer"]) + ev.Event.Data["Picture"] = ph.GetProfilePic(ev.Event.Data["RemotePeer"]) case event.PeerAcknowledgement: // No enrichement required //Acknowledge(ev.Event.Data[event.RemotePeer], ev.Event.Data[event.EventID])