Merge pull request 'contacts rewire' (#9) from contacts into trunk

Reviewed-on: #9
This commit is contained in:
Dan Ballard 2021-03-16 13:49:11 -07:00
commit 2359d8a2e4
6 changed files with 67 additions and 39 deletions

2
go.mod
View File

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

2
go.sum
View File

@ -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=

10
lib.go
View File

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

8
utils/contacts.go Normal file
View File

@ -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"`
}

View File

@ -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
}
}

View File

@ -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?