contacts rewire #9

Merged
dan merged 7 commits from contacts into trunk 2021-03-16 20:49:12 +00:00
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 cwtch.im/cwtch v0.5.1
git.openprivacy.ca/openprivacy/connectivity v1.3.3 git.openprivacy.ca/openprivacy/connectivity v1.3.3
git.openprivacy.ca/openprivacy/log v1.0.2 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-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 h1:kgfVkAEEQXXQ0qc6dH7n6y37NAYmTFmz0YRwrRjgxKw=
golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= 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.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.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= 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) return string(jsonBytes)
} }
type Contact struct {
Name string `json:"name"`
Onion string `json:"onion"`
Status string `json:"status"`
}
//export c_GetContacts //export c_GetContacts
func c_GetContacts(onion_ptr *C.char, onion_len C.int) *C.char { func c_GetContacts(onion_ptr *C.char, onion_len C.int) *C.char {
return C.CString(GetContacts(C.GoStringN(onion_ptr, onion_len))) return C.CString(GetContacts(C.GoStringN(onion_ptr, onion_len)))
@ -247,11 +241,11 @@ func GetContacts(onion string) string {
contactEventsQueue = event.NewQueue() contactEventsQueue = event.NewQueue()
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue) application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
var contacts []Contact var contacts []utils.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, utils.Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State})
} }
bytes, _ := json.Marshal(contacts) 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"`
erinn marked this conversation as resolved
Review

might want to squeeze "Sate" in for use with "unknown, approved and blocked"

might want to squeeze "Sate" in for use with "unknown, approved and blocked"
Review

gonna leave this until we're ready to wire it in

gonna leave this until we're ready to wire it in
Picture string `json:"picture"`
}

View File

@ -8,12 +8,13 @@ import (
"cwtch.im/cwtch/protocol/connections" "cwtch.im/cwtch/protocol/connections"
"encoding/json" "encoding/json"
"git.openprivacy.ca/flutter/libcwtch-go/constants" "git.openprivacy.ca/flutter/libcwtch-go/constants"
"git.openprivacy.ca/openprivacy/log"
) )
import "cwtch.im/cwtch/event" import "cwtch.im/cwtch/event"
type EventProfileEnvelope struct { type EventProfileEnvelope struct {
event event.Event Event event.Event
profile string Profile string
} }
type EventHandler struct { type EventHandler struct {
@ -88,19 +89,42 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
e.Data[constants.Name] = nick e.Data[constants.Name] = nick
e.Data[constants.Picture] = picPath e.Data[constants.Picture] = picPath
e.Data["Online"] = online 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) json, _ := json.Marshal(e)
return string(json) 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 { func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
peer := eh.app.GetPeer(ev.profile) peer := eh.app.GetPeer(ev.Profile)
ph := NewPeerHelper(peer) ph := NewPeerHelper(peer)
switch ev.event.EventType { switch ev.Event.EventType {
/*case event.NetworkStatus: /*case event.NetworkStatus:
online, _ := peer.GetAttribute(attr.GetLocalScope(constants.PeerOnline)) online, _ := peer.GetAttribute(attr.GetLocalScope(constants.PeerOnline))
@ -131,7 +155,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
case event.PeerAcknowledgement: case event.PeerAcknowledgement:
// No enrichement required // 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 case event.NewMessageFromGroup: //event.TimestampReceived, event.TimestampSent, event.Data, event.GroupID, event.RemotePeer
ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampSent]) ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampSent])
@ -145,7 +169,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
} }
*/ */
case event.PeerCreated: case event.PeerCreated:
handle := ev.event.Data[event.RemotePeer] handle := ev.Event.Data[event.RemotePeer]
err := EnrichNewPeer(handle, ph, ev) err := EnrichNewPeer(handle, ph, ev)
if err != nil { if err != nil {
return "" 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]) uiManager.AddSendMessageError(e.Data[event.RemotePeer], e.Data[event.EventID], e.Data[event.Error])
*/ */
case event.PeerStateChange: case event.PeerStateChange:
cxnState := connections.ConnectionStateToType[ev.event.Data[event.ConnectionState]] cxnState := connections.ConnectionStateToType[ev.Event.Data[event.ConnectionState]]
contact := peer.GetContact(ev.event.Data[event.RemotePeer]) contact := peer.GetContact(ev.Event.Data[event.RemotePeer])
if cxnState == connections.AUTHENTICATED && contact == nil { if cxnState == connections.AUTHENTICATED && contact == nil {
// Contact does not exist, change event to NewPeer // Contact does not exist, change event to NewPeer
peer.AddContact(ev.event.Data[event.RemotePeer], ev.event.Data[event.RemotePeer], model.AuthUnknown) peer.AddContact(ev.Event.Data[event.RemotePeer], ev.Event.Data[event.RemotePeer], model.AuthUnknown)
contact = peer.GetContact(ev.event.Data[event.RemotePeer]) contact = peer.GetContact(ev.Event.Data[event.RemotePeer])
ev.event.EventType = event.PeerCreated ev.Event.EventType = event.PeerCreated
err := EnrichNewPeer(ev.event.Data[event.RemotePeer], ph, ev) err := EnrichNewPeer(ev.Event.Data[event.RemotePeer], ph, ev)
if err != nil { if err != nil {
return "" return ""
} }
@ -176,8 +200,8 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
//uiManager.UpdateContactStatus(contact.Onion, int(cxnState), false) //uiManager.UpdateContactStatus(contact.Onion, int(cxnState), false)
if cxnState == connections.AUTHENTICATED { if cxnState == connections.AUTHENTICATED {
// if known and authed, get vars // 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.Name)
peer.SendGetValToPeer(ev.event.Data[event.RemotePeer], attr.PublicScope, constants.Picture) 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 // TODO: graceful shutdown, via an injected event of special QUIT type exiting loop/go routine
for { for {
e := q.Next() e := q.Next()
ev := EventProfileEnvelope{event: *e, profile: onion} ev := EventProfileEnvelope{Event: *e, Profile: onion}
eh.profileEvents <- ev eh.profileEvents <- ev
} }
} }

View File

@ -249,28 +249,28 @@ func EnrichNewPeer(handle string, ph *PeerHelper, ev *EventProfileEnvelope) erro
group := ph.peer.GetGroup(handle) group := ph.peer.GetGroup(handle)
if group != nil { if group != nil {
lastRead := ph.InitLastReadTime(group.GroupID) lastRead := ph.InitLastReadTime(group.GroupID)
ev.event.Data["unread"] = strconv.Itoa(ph.CountUnread(group.Timeline.GetMessages(), lastRead)) ev.Event.Data["unread"] = strconv.Itoa(ph.CountUnread(group.Timeline.GetMessages(), lastRead))
ev.event.Data["picture"] = ph.GetProfilePic(handle) ev.Event.Data["picture"] = ph.GetProfilePic(handle)
ev.event.Data["nick"] = ph.GetNick(handle) ev.Event.Data["nick"] = ph.GetNick(handle)
ev.event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[group.State])) ev.Event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[group.State]))
ev.event.Data["authorization"] = string(model.AuthApproved) ev.Event.Data["authorization"] = string(model.AuthApproved)
ev.event.Data["loading"] = "false" ev.Event.Data["loading"] = "false"
ev.event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&group.Timeline)) ev.Event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&group.Timeline))
} }
} else if ph.IsPeer(handle) { } else if ph.IsPeer(handle) {
contact := ph.peer.GetContact(handle) contact := ph.peer.GetContact(handle)
if contact != nil { if contact != nil {
lastRead := ph.InitLastReadTime(contact.Onion) lastRead := ph.InitLastReadTime(contact.Onion)
ev.event.Data["unread"] = strconv.Itoa(ph.CountUnread(contact.Timeline.GetMessages(), lastRead)) ev.Event.Data["unread"] = strconv.Itoa(ph.CountUnread(contact.Timeline.GetMessages(), lastRead))
ev.event.Data["picture"] = ph.GetProfilePic(handle) ev.Event.Data["picture"] = ph.GetProfilePic(handle)
ev.event.Data["nick"] = ph.GetNick(handle) ev.Event.Data["nick"] = ph.GetNick(handle)
ev.event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[contact.State])) ev.Event.Data["status"] = strconv.Itoa(int(connections.ConnectionStateToType[contact.State]))
ev.event.Data["authorization"] = string(contact.Authorization) ev.Event.Data["authorization"] = string(contact.Authorization)
ev.event.Data["loading"] = "false" ev.Event.Data["loading"] = "false"
ev.event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&contact.Timeline)) ev.Event.Data["lastMsgTime"] = strconv.Itoa(getLastMessageTime(&contact.Timeline))
} }
} else { } else {
// could be a server? // could be a server?