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

View File

@ -20,4 +20,4 @@ const StateSelectedProfileTime = "state-selected-profile-time"
// Settings // Settings
const BlockUnknownPeersSetting = "blockunknownpeers" const BlockUnknownPeersSetting = "blockunknownpeers"
const LocaleSetting = "locale" const LocaleSetting = "locale"
const ZoomSetting = "zoom" const ZoomSetting = "zoom"

75
lib.go
View File

@ -5,10 +5,10 @@ import "C"
import ( import (
"crypto/rand" "crypto/rand"
"cwtch.im/cwtch/app" "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/app/plugins"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/peer"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -104,19 +104,52 @@ func c_ACNEvents() *C.char {
func ACNEvents() string { func ACNEvents() string {
select { select {
case myevent := <- acnQueue.OutChan(): case myevent := <-acnQueue.OutChan():
return fmt.Sprintf("%v", myevent) return fmt.Sprintf("%v", myevent)
default: default:
return "" return ""
} }
} }
//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 //export c_GetAppBusEvent
func c_GetAppBusEvent() *C.char { func c_GetAppBusEvent() *C.char {
return C.CString(GetAppBusEvent()) return C.CString(GetAppBusEvent())
} }
// GetAppBusEvent blocks until an event // GetAppBusEvent blocks until an event
func GetAppBusEvent() string { func GetAppBusEvent() string {
e := appBusQueue.Next() e := appBusQueue.Next()
@ -173,13 +206,13 @@ func c_GetProfileRepaintEvent() int8 {
} }
func GetProfileRepaintEvent() bool { func GetProfileRepaintEvent() bool {
<- acnQueue.OutChan() <-acnQueue.OutChan()
return true return true
} }
type Profile struct { type Profile struct {
Name string `json:"name"` Name string `json:"name"`
Onion string `json:"onion"` Onion string `json:"onion"`
ImagePath string `json:"imagePath"` ImagePath string `json:"imagePath"`
} }
@ -192,22 +225,22 @@ func GetProfiles() string {
peerList := application.ListPeers() peerList := application.ListPeers()
profiles := make([]Profile, len(peerList)) profiles := make([]Profile, len(peerList))
i := 0 i := 0
for onion, name := range peerList { for onion := range peerList {
name, _ := application.GetPeer(onion).GetAttribute(attr.GetPublicScope(constants.Name))
profiles[i] = Profile{ profiles[i] = Profile{
Name: name, Name: name,
Onion: onion, Onion: onion,
ImagePath: "", ImagePath: "",
} }
i += 1 i += 1
} }
jsonBytes,_ := json.Marshal(profiles) jsonBytes, _ := json.Marshal(profiles)
return string(jsonBytes) return string(jsonBytes)
} }
type Contact struct { type Contact struct {
Name string `json:"name"` Name string `json:"name"`
Onion string `json:"onion"` Onion string `json:"onion"`
Status string `json:"status"` Status string `json:"status"`
} }
@ -224,13 +257,13 @@ func GetContacts(onion string) string {
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue) application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
var contacts []Contact var contacts []Contact
for _,contact := range mypeer.GetContacts() { for _, contact := range mypeer.GetContacts() {
contactInfo := mypeer.GetContact(contact) contactInfo := mypeer.GetContact(contact)
log.Infof("contactInfo %v", contactInfo) log.Infof("contactInfo %v", contactInfo)
contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State}) contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State})
} }
bytes,_ := json.Marshal(contacts) bytes, _ := json.Marshal(contacts)
return string(bytes) return string(bytes)
} }
@ -271,7 +304,7 @@ func c_ContactEvents() *C.char {
func ContactEvents() string { func ContactEvents() string {
select { select {
case myevent := <- contactEventsQueue.OutChan(): case myevent := <-contactEventsQueue.OutChan():
return fmt.Sprintf("%v", myevent) return fmt.Sprintf("%v", myevent)
default: default:
return "" return ""
@ -301,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 // Deprecate - 2021.01.14 - not used
func GetMessage(profile, handle string, message_index int) string { func GetMessage(profile, handle string, message_index int) string {
message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index] 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)) log.Infof("GetMessage(%s, %s, %d) = %s", profile, handle, message_index, string(bytes))
return string(bytes) return string(bytes)
} }
@ -315,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 { func GetMessages(profile, handle string, start, end int) string {
messages := application.GetPeer(profile).GetContact(handle).Timeline.Messages[start:end] messages := application.GetPeer(profile).GetContact(handle).Timeline.Messages[start:end]
bytes,_ := json.Marshal(messages) bytes, _ := json.Marshal(messages)
return string(bytes) return string(bytes)
} }
// Leave as is, needed by ffi // Leave as is, needed by ffi
func main() {} func main() {}

View File

@ -18,7 +18,7 @@ func NewPeerHelper(profile peer.CwtchPeer) *PeerHelper {
return &PeerHelper{profile} return &PeerHelper{profile}
} }
func (p *PeerHelper) IsGroup( id string) bool { func (p *PeerHelper) IsGroup(id string) bool {
return len(id) == 32 && !p.IsServer(id) return len(id) == 32 && !p.IsServer(id)
} }
@ -170,6 +170,7 @@ func (p *PeerHelper) CountUnread(messages []model.Message, lastRead time.Time) i
} }
return count return count
} }
/* /*
// AddProfile adds a new profile to the UI // AddProfile adds a new profile to the UI
func AddProfile(gcd *GrandCentralDispatcher, handle string) { func AddProfile(gcd *GrandCentralDispatcher, handle string) {
@ -385,4 +386,4 @@ func (this *manager) ChangePasswordResponse(error bool) {
func (this *manager) UpdateNetworkStatus(online bool) { func (this *manager) UpdateNetworkStatus(online bool) {
this.gcd.UpdateProfileNetworkStatus(this.profile, online) this.gcd.UpdateProfileNetworkStatus(this.profile, online)
} }
*/ */