SendProfileEvent #6

Merged
dan merged 2 commits from add_profile_form into trunk 2021-03-04 02:06:02 +00:00
3 changed files with 58 additions and 24 deletions

45
lib.go
View File

@ -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"
@ -111,12 +111,45 @@ 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())
}
// GetAppBusEvent blocks until an event
func GetAppBusEvent() string {
e := appBusQueue.Next()
@ -192,7 +225,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,
@ -204,7 +238,6 @@ func GetProfiles() string {
return string(jsonBytes)
}
type Contact struct {
Name string `json:"name"`
Onion string `json:"onion"`

View File

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