From 7dfdb33c52771e9915855b131e131f4dca9d6f5e Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 3 Mar 2021 17:25:24 -0800 Subject: [PATCH 1/2] SendProfileEvent --- lib.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/lib.go b/lib.go index a67953b..dd3b352 100644 --- a/lib.go +++ b/lib.go @@ -111,6 +111,43 @@ func ACNEvents() string { } } +//export c_SendProfileEvent +// A generic method for Rebroadcasting Profile Events from a UI +func c_SendProfileEvent(onion_ptr *C.char, onion_len C.int, json_ptr *C.char, json_len C.int) { + onion:= C.GoStringN(onion_ptr, onion_len) + eventJson:= C.GoStringN(json_ptr, json_len) + SendProfileEvent(onion, eventJson) +} + +// SendProfileEvent is a generic method for Rebroadcasting Profile Events from a UI +func SendProfileEvent(onion string, eventJson string) { + // Convert the Event Json back to a typed Event Struct, this will make the + // rest of the logic nicer. + var new_event event.Event + json.Unmarshal([]byte(eventJson), &new_event) + log.Infof("Event: %v %v", onion, new_event) + + + // Get the correct Peer + peer := application.GetPeer(onion) + if peer == nil { + return + } + + // We need to update the local cache + // Ideally I think this would be pushed back into Cwtch + switch new_event.EventType { + case event.SetAttribute: { + peer.SetAttribute(new_event.Data[event.Key], new_event.Data[event.Data]) + } + default: { + // rebroadcast catch all + log.Infof("Received Event %v for %v but no libCwtch handler found, relaying the event directly", new_event, onion) + application.GetEventBus(onion).Publish(new_event) + } + } +} + //export c_GetAppBusEvent func c_GetAppBusEvent() *C.char { return C.CString(GetAppBusEvent()) @@ -192,7 +229,8 @@ func GetProfiles() string { peerList := application.ListPeers() profiles := make([]Profile, len(peerList)) i := 0 - for onion, name := range peerList { + for onion,_ := range peerList { + name,_ := application.GetPeer(onion).GetAttribute(attr.GetPublicScope(constants.Name)) profiles[i] = Profile{ Name: name, Onion: onion, -- 2.25.1 From b78d8d8832121ac6ae5f5e2fde265468137c8716 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 3 Mar 2021 18:05:22 -0800 Subject: [PATCH 2/2] Format --- constants/attributes.go | 2 +- lib.go | 53 +++++++++++++++++++---------------------- utils/manager.go | 5 ++-- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/constants/attributes.go b/constants/attributes.go index 4026122..f8b1c98 100644 --- a/constants/attributes.go +++ b/constants/attributes.go @@ -20,4 +20,4 @@ const StateSelectedProfileTime = "state-selected-profile-time" // Settings const BlockUnknownPeersSetting = "blockunknownpeers" const LocaleSetting = "locale" -const ZoomSetting = "zoom" \ No newline at end of file +const ZoomSetting = "zoom" diff --git a/lib.go b/lib.go index dd3b352..c4c305d 100644 --- a/lib.go +++ b/lib.go @@ -5,10 +5,10 @@ import "C" import ( "crypto/rand" "cwtch.im/cwtch/app" - "cwtch.im/cwtch/event" - "cwtch.im/cwtch/peer" - "cwtch.im/cwtch/model/attr" "cwtch.im/cwtch/app/plugins" + "cwtch.im/cwtch/event" + "cwtch.im/cwtch/model/attr" + "cwtch.im/cwtch/peer" "encoding/json" "fmt" @@ -104,7 +104,7 @@ func c_ACNEvents() *C.char { func ACNEvents() string { select { - case myevent := <- acnQueue.OutChan(): + case myevent := <-acnQueue.OutChan(): return fmt.Sprintf("%v", myevent) default: return "" @@ -114,8 +114,8 @@ func ACNEvents() string { //export c_SendProfileEvent // A generic method for Rebroadcasting Profile Events from a UI func c_SendProfileEvent(onion_ptr *C.char, onion_len C.int, json_ptr *C.char, json_len C.int) { - onion:= C.GoStringN(onion_ptr, onion_len) - eventJson:= C.GoStringN(json_ptr, json_len) + onion := C.GoStringN(onion_ptr, onion_len) + eventJson := C.GoStringN(json_ptr, json_len) SendProfileEvent(onion, eventJson) } @@ -127,7 +127,6 @@ func SendProfileEvent(onion string, eventJson string) { json.Unmarshal([]byte(eventJson), &new_event) log.Infof("Event: %v %v", onion, new_event) - // Get the correct Peer peer := application.GetPeer(onion) if peer == nil { @@ -137,15 +136,13 @@ func SendProfileEvent(onion string, eventJson string) { // We need to update the local cache // Ideally I think this would be pushed back into Cwtch switch new_event.EventType { - case event.SetAttribute: { + case event.SetAttribute: peer.SetAttribute(new_event.Data[event.Key], new_event.Data[event.Data]) - } - default: { + default: // rebroadcast catch all log.Infof("Received Event %v for %v but no libCwtch handler found, relaying the event directly", new_event, onion) application.GetEventBus(onion).Publish(new_event) } - } } //export c_GetAppBusEvent @@ -153,7 +150,6 @@ func c_GetAppBusEvent() *C.char { return C.CString(GetAppBusEvent()) } - // GetAppBusEvent blocks until an event func GetAppBusEvent() string { e := appBusQueue.Next() @@ -210,13 +206,13 @@ func c_GetProfileRepaintEvent() int8 { } func GetProfileRepaintEvent() bool { - <- acnQueue.OutChan() + <-acnQueue.OutChan() return true } type Profile struct { - Name string `json:"name"` - Onion string `json:"onion"` + Name string `json:"name"` + Onion string `json:"onion"` ImagePath string `json:"imagePath"` } @@ -229,23 +225,22 @@ func GetProfiles() string { peerList := application.ListPeers() profiles := make([]Profile, len(peerList)) i := 0 - for onion,_ := range peerList { - name,_ := application.GetPeer(onion).GetAttribute(attr.GetPublicScope(constants.Name)) + for onion := range peerList { + name, _ := application.GetPeer(onion).GetAttribute(attr.GetPublicScope(constants.Name)) profiles[i] = Profile{ - Name: name, - Onion: onion, + Name: name, + Onion: onion, ImagePath: "", } i += 1 } - jsonBytes,_ := json.Marshal(profiles) + jsonBytes, _ := json.Marshal(profiles) return string(jsonBytes) } - type Contact struct { - Name string `json:"name"` - Onion string `json:"onion"` + Name string `json:"name"` + Onion string `json:"onion"` Status string `json:"status"` } @@ -262,13 +257,13 @@ func GetContacts(onion string) string { application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue) var contacts []Contact - for _,contact := range mypeer.GetContacts() { + for _, contact := range mypeer.GetContacts() { contactInfo := mypeer.GetContact(contact) log.Infof("contactInfo %v", contactInfo) contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State}) } - bytes,_ := json.Marshal(contacts) + bytes, _ := json.Marshal(contacts) return string(bytes) } @@ -309,7 +304,7 @@ func c_ContactEvents() *C.char { func ContactEvents() string { select { - case myevent := <- contactEventsQueue.OutChan(): + case myevent := <-contactEventsQueue.OutChan(): return fmt.Sprintf("%v", myevent) default: return "" @@ -339,7 +334,7 @@ func c_GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, ha // Deprecate - 2021.01.14 - not used func GetMessage(profile, handle string, message_index int) string { message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index] - bytes,_ := json.Marshal(message) + bytes, _ := json.Marshal(message) log.Infof("GetMessage(%s, %s, %d) = %s", profile, handle, message_index, string(bytes)) return string(bytes) } @@ -353,9 +348,9 @@ func c_GetMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, h func GetMessages(profile, handle string, start, end int) string { messages := application.GetPeer(profile).GetContact(handle).Timeline.Messages[start:end] - bytes,_ := json.Marshal(messages) + bytes, _ := json.Marshal(messages) return string(bytes) } // Leave as is, needed by ffi -func main() {} \ No newline at end of file +func main() {} diff --git a/utils/manager.go b/utils/manager.go index fd9fcae..22ffb4e 100644 --- a/utils/manager.go +++ b/utils/manager.go @@ -18,7 +18,7 @@ func NewPeerHelper(profile peer.CwtchPeer) *PeerHelper { return &PeerHelper{profile} } -func (p *PeerHelper) IsGroup( id string) bool { +func (p *PeerHelper) IsGroup(id string) bool { return len(id) == 32 && !p.IsServer(id) } @@ -170,6 +170,7 @@ func (p *PeerHelper) CountUnread(messages []model.Message, lastRead time.Time) i } return count } + /* // AddProfile adds a new profile to the UI func AddProfile(gcd *GrandCentralDispatcher, handle string) { @@ -385,4 +386,4 @@ func (this *manager) ChangePasswordResponse(error bool) { func (this *manager) UpdateNetworkStatus(online bool) { this.gcd.UpdateProfileNetworkStatus(this.profile, online) } -*/ \ No newline at end of file +*/ -- 2.25.1