From 0c963172ffc6efa7da930e73227ae3570d876a7b Mon Sep 17 00:00:00 2001 From: erinn Date: Wed, 7 Apr 2021 22:06:21 -0700 Subject: [PATCH 1/2] restoring sendmessage yay --- lib.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/lib.go b/lib.go index 78c5e72..ce77298 100644 --- a/lib.go +++ b/lib.go @@ -322,6 +322,44 @@ func ContactEvents() string { } } +//export c_AcceptContact +func c_AcceptContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) { + AcceptContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen)) +} + +func AcceptContact(profile, handle string) { + err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthApproved) + if err != nil { + log.Errorf("error accepting contact: %s", err.Error()) + } +} + +//export c_BlockContact +func c_BlockContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) { + BlockContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen)) +} + +func BlockContact(profile, handle string) { + err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthBlocked) + if err != nil { + log.Errorf("error blocking contact: %s", err.Error()) + } +} + +//export c_DebugResetContact +func c_DebugResetContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) { + DebugResetContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen)) +} + +func DebugResetContact(profile, handle string) { + err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthUnknown) + if err != nil { + log.Errorf("error resetting contact: %s", err.Error()) + } else { + log.Infof("reset contact! %s -> %s", profile, handle) + } +} + //export c_NumMessages func c_NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int) (n int) { profile := C.GoStringN(profile_ptr, profile_len) @@ -363,5 +401,17 @@ func GetMessages(profile, handle string, start, end int) string { return string(bytes) } +//export c_SendMessage +func c_SendMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, msg_ptr *C.char, msg_len C.int) { + profile := C.GoStringN(profile_ptr, profile_len) + handle := C.GoStringN(handle_ptr, handle_len) + msg := C.GoStringN(msg_ptr, msg_len) + SendMessage(profile, handle, msg) +} + +func SendMessage(profile, handle, msg string) { + application.GetPeer(profile).SendMessageToPeer(handle, msg) +} + // Leave as is, needed by ffi func main() {} From 6e1cb20d668a3bffd80b1c250cf704fdc6ecc238 Mon Sep 17 00:00:00 2001 From: erinn Date: Fri, 9 Apr 2021 19:31:05 -0700 Subject: [PATCH 2/2] mo wirin --- go.mod | 2 +- lib.go | 30 ++++++++++++++++++++++-------- utils/eventHandler.go | 8 +------- utils/manager.go | 1 + 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index ee7bd02..afc1ffb 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,4 @@ require ( cwtch.im/cwtch v0.6.3 git.openprivacy.ca/openprivacy/connectivity v1.3.3 git.openprivacy.ca/openprivacy/log v1.0.2 -) \ No newline at end of file +) diff --git a/lib.go b/lib.go index ce77298..53fc0c0 100644 --- a/lib.go +++ b/lib.go @@ -293,7 +293,6 @@ func c_SelectProfile(onion_ptr *C.char, onion_len C.int) *C.char { } func SelectProfile(onion string) string { - log.Infof("Select Profile: %v", onion) contactEventsQueue = event.NewQueue() application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue) return "" @@ -329,7 +328,14 @@ func c_AcceptContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, ha func AcceptContact(profile, handle string) { err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthApproved) - if err != nil { + if err == nil { + eventHandler.Push(event.NewEvent(event.PeerStateChange, map[event.Field]string{ + //application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{ + "ProfileOnion": profile, + event.RemotePeer: handle, + "authorization": string(model.AuthApproved), + })) + } else { log.Errorf("error accepting contact: %s", err.Error()) } } @@ -341,7 +347,13 @@ func c_BlockContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, han func BlockContact(profile, handle string) { err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthBlocked) - if err != nil { + if err == nil { + application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{ + "ProfileOnion": profile, + event.RemotePeer: handle, + "authorization": string(model.AuthBlocked), + })) + } else { log.Errorf("error blocking contact: %s", err.Error()) } } @@ -353,10 +365,14 @@ func c_DebugResetContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char func DebugResetContact(profile, handle string) { err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthUnknown) - if err != nil { - log.Errorf("error resetting contact: %s", err.Error()) + if err == nil { + application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{ + "ProfileOnion": profile, + event.RemotePeer: handle, + "authorization": string(model.AuthUnknown), + })) } else { - log.Infof("reset contact! %s -> %s", profile, handle) + log.Errorf("error resetting contact: %s", err.Error()) } } @@ -369,7 +385,6 @@ func c_NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, h func NumMessages(profile, handle string) (n int) { n = len(application.GetPeer(profile).GetContact(handle).Timeline.Messages) - //log.Infof("NumMessagse(%s, %s) = %d", profile, handle, n) return } @@ -384,7 +399,6 @@ func c_GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, ha func GetMessage(profile, handle string, message_index int) string { message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index] bytes, _ := json.Marshal(message) - log.Infof("GetMessage(%s, %s, %d) = %s", profile, handle, message_index, string(bytes)) return string(bytes) } diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 05a0a53..2746d53 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -192,14 +192,8 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { contact := peer.GetContact(ev.Event.Data[event.RemotePeer]) if cxnState == connections.AUTHENTICATED && contact == nil { - // Contact does not exist, change event to NewPeer peer.AddContact(ev.Event.Data[event.RemotePeer], ev.Event.Data[event.RemotePeer], model.AuthUnknown) - contact = peer.GetContact(ev.Event.Data[event.RemotePeer]) - ev.Event.EventType = event.PeerCreated - err := EnrichNewPeer(ev.Event.Data[event.RemotePeer], ph, ev) - if err != nil { - return "" - } + return "" } if contact != nil { diff --git a/utils/manager.go b/utils/manager.go index 68c2c52..7c78275 100644 --- a/utils/manager.go +++ b/utils/manager.go @@ -264,6 +264,7 @@ func EnrichNewPeer(handle string, ph *PeerHelper, ev *EventProfileEnvelope) erro if contact != nil { lastRead := ph.InitLastReadTime(contact.Onion) ev.Event.Data["unread"] = strconv.Itoa(ph.CountUnread(contact.Timeline.GetMessages(), lastRead)) + ev.Event.Data["numMessages"] = strconv.Itoa(contact.Timeline.Len()) ev.Event.Data["picture"] = ph.GetProfilePic(handle) ev.Event.Data["nick"] = ph.GetNick(handle)