diff --git a/go.mod b/go.mod index aa25554..e110966 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,5 @@ require ( cwtch.im/cwtch v0.5.1 git.openprivacy.ca/openprivacy/connectivity v1.3.3 git.openprivacy.ca/openprivacy/log v1.0.2 - golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f // indirect + golang.org/x/mobile v0.0.0-20210208171126-f462b3930c8f // indirect ) diff --git a/go.sum b/go.sum index 47a1420..31f74f0 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPI golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f h1:kgfVkAEEQXXQ0qc6dH7n6y37NAYmTFmz0YRwrRjgxKw= golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= +golang.org/x/mobile v0.0.0-20210208171126-f462b3930c8f h1:aEcjdTsycgPqO/caTgnxfR9xwWOltP/21vtJyFztEy0= +golang.org/x/mobile v0.0.0-20210208171126-f462b3930c8f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= diff --git a/lib.go b/lib.go index 36beaf9..364cb0a 100644 --- a/lib.go +++ b/lib.go @@ -229,12 +229,6 @@ func GetProfiles() string { return string(jsonBytes) } -type Contact struct { - Name string `json:"name"` - Onion string `json:"onion"` - Status string `json:"status"` -} - //export c_GetContacts func c_GetContacts(onion_ptr *C.char, onion_len C.int) *C.char { return C.CString(GetContacts(C.GoStringN(onion_ptr, onion_len))) @@ -247,11 +241,11 @@ func GetContacts(onion string) string { contactEventsQueue = event.NewQueue() application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue) - var contacts []Contact + var contacts []utils.Contact 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}) + contacts = append(contacts, utils.Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State}) } bytes, _ := json.Marshal(contacts) diff --git a/utils/contacts.go b/utils/contacts.go new file mode 100644 index 0000000..9e88cfd --- /dev/null +++ b/utils/contacts.go @@ -0,0 +1,8 @@ +package utils + +type Contact struct { + Name string `json:"name"` + Onion string `json:"onion"` + Status string `json:"status"` + Picture string `json:"picture"` +} diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 056eb0e..b3cd71d 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -8,12 +8,13 @@ import ( "cwtch.im/cwtch/protocol/connections" "encoding/json" "git.openprivacy.ca/flutter/libcwtch-go/constants" + "git.openprivacy.ca/openprivacy/log" ) import "cwtch.im/cwtch/event" type EventProfileEnvelope struct { - event event.Event - profile string + Event event.Event + Profile string } type EventHandler struct { @@ -88,19 +89,42 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { e.Data[constants.Name] = nick e.Data[constants.Picture] = picPath e.Data["Online"] = online + + var contacts []Contact + for _, contact := range profile.GetContacts() { + if profile.GetContact(contact).IsServer() { + continue + } + + cpicVal, ok := profile.GetContactAttribute(contact, attr.GetPeerScope(constants.Picture)) + if !ok { + cpicVal = ImageToString(NewImage(RandomProfileImage(contact), TypeImageDistro)) + } + cpic, err := StringToImage(cpicVal) + if err != nil { + cpic = NewImage(RandomProfileImage(contact), TypeImageDistro) + } + cpicPath := GetPicturePath(cpic) + contactInfo := profile.GetContact(contact) + contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State, Picture: cpicPath,}) + } + + bytes, _ := json.Marshal(contacts) + e.Data["ContactsJson"] = string(bytes) + log.Infof("contactsJson %v", e.Data["ContactsJson"]) } json, _ := json.Marshal(e) return string(json) } -// handleProfileEvent enriches profile events so they are usable with out further data fetches +// handleProfileEvent enriches Profile events so they are usable with out further data fetches func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { - peer := eh.app.GetPeer(ev.profile) + peer := eh.app.GetPeer(ev.Profile) ph := NewPeerHelper(peer) - switch ev.event.EventType { + switch ev.Event.EventType { /*case event.NetworkStatus: online, _ := peer.GetAttribute(attr.GetLocalScope(constants.PeerOnline)) @@ -131,7 +155,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { case event.PeerAcknowledgement: // No enrichement required - //Acknowledge(ev.event.Data[event.RemotePeer], ev.event.Data[event.EventID]) + //Acknowledge(ev.Event.Data[event.RemotePeer], ev.Event.Data[event.EventID]) /* case event.NewMessageFromGroup: //event.TimestampReceived, event.TimestampSent, event.Data, event.GroupID, event.RemotePeer ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampSent]) @@ -145,7 +169,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { } */ case event.PeerCreated: - handle := ev.event.Data[event.RemotePeer] + handle := ev.Event.Data[event.RemotePeer] err := EnrichNewPeer(handle, ph, ev) if err != nil { return "" @@ -157,15 +181,15 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { uiManager.AddSendMessageError(e.Data[event.RemotePeer], e.Data[event.EventID], e.Data[event.Error]) */ case event.PeerStateChange: - cxnState := connections.ConnectionStateToType[ev.event.Data[event.ConnectionState]] - contact := peer.GetContact(ev.event.Data[event.RemotePeer]) + cxnState := connections.ConnectionStateToType[ev.Event.Data[event.ConnectionState]] + 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) + 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 "" } @@ -176,8 +200,8 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { //uiManager.UpdateContactStatus(contact.Onion, int(cxnState), false) if cxnState == connections.AUTHENTICATED { // if known and authed, get vars - peer.SendGetValToPeer(ev.event.Data[event.RemotePeer], attr.PublicScope, constants.Name) - peer.SendGetValToPeer(ev.event.Data[event.RemotePeer], attr.PublicScope, constants.Picture) + peer.SendGetValToPeer(ev.Event.Data[event.RemotePeer], attr.PublicScope, constants.Name) + peer.SendGetValToPeer(ev.Event.Data[event.RemotePeer], attr.PublicScope, constants.Picture) } } @@ -253,7 +277,7 @@ func (eh *EventHandler) forwardProfileMessages(onion string, q event.Queue) { // TODO: graceful shutdown, via an injected event of special QUIT type exiting loop/go routine for { e := q.Next() - ev := EventProfileEnvelope{event: *e, profile: onion} + ev := EventProfileEnvelope{Event: *e, Profile: onion} eh.profileEvents <- ev } } diff --git a/utils/manager.go b/utils/manager.go index 3db2a74..7fec70d 100644 --- a/utils/manager.go +++ b/utils/manager.go @@ -249,28 +249,28 @@ func EnrichNewPeer(handle string, ph *PeerHelper, ev *EventProfileEnvelope) erro group := ph.peer.GetGroup(handle) if group != nil { lastRead := ph.InitLastReadTime(group.GroupID) - ev.event.Data["unread"] = strconv.Itoa(ph.CountUnread(group.Timeline.GetMessages(), lastRead)) - ev.event.Data["picture"] = ph.GetProfilePic(handle) + ev.Event.Data["unread"] = strconv.Itoa(ph.CountUnread(group.Timeline.GetMessages(), lastRead)) + ev.Event.Data["picture"] = ph.GetProfilePic(handle) - ev.event.Data["nick"] = ph.GetNick(handle) - ev.event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[group.State])) - ev.event.Data["authorization"] = string(model.AuthApproved) - ev.event.Data["loading"] = "false" - ev.event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&group.Timeline)) + ev.Event.Data["nick"] = ph.GetNick(handle) + ev.Event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[group.State])) + ev.Event.Data["authorization"] = string(model.AuthApproved) + ev.Event.Data["loading"] = "false" + ev.Event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&group.Timeline)) } } else if ph.IsPeer(handle) { contact := ph.peer.GetContact(handle) if contact != nil { lastRead := ph.InitLastReadTime(contact.Onion) - ev.event.Data["unread"] = strconv.Itoa(ph.CountUnread(contact.Timeline.GetMessages(), lastRead)) - ev.event.Data["picture"] = ph.GetProfilePic(handle) + ev.Event.Data["unread"] = strconv.Itoa(ph.CountUnread(contact.Timeline.GetMessages(), lastRead)) + ev.Event.Data["picture"] = ph.GetProfilePic(handle) - ev.event.Data["nick"] = ph.GetNick(handle) - ev.event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[contact.State])) + ev.Event.Data["nick"] = ph.GetNick(handle) + ev.Event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[contact.State])) - ev.event.Data["authorization"] = string(contact.Authorization) - ev.event.Data["loading"] = "false" - ev.event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&contact.Timeline)) + ev.Event.Data["authorization"] = string(contact.Authorization) + ev.Event.Data["loading"] = "false" + ev.Event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&contact.Timeline)) } } else { // could be a server?