ui/go/characters/presencepoller.go

60 lines
1.5 KiB
Go
Raw Normal View History

2018-11-22 00:01:17 +00:00
package characters
import (
2019-01-21 21:41:47 +00:00
"cwtch.im/cwtch/event"
2018-11-28 22:14:02 +00:00
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
2018-11-22 00:01:17 +00:00
"time"
)
func PresencePoller(getContact func(string) *gobjects.Contact, addContact func(contact *gobjects.Contact), updateContact func(string)) { // TODO: make this subscribe-able in ricochet
time.Sleep(time.Second * 4)
for {
contacts := the.Peer.GetContacts()
for i := range contacts {
ct := getContact(contacts[i])
if ct == nil { // new contact has attempted to connect with us, treat it as an invite
toc := the.Peer.GetContact(contacts[i])
c, _ := the.Peer.GetProfile().GetContact(contacts[i])
deleted,_ := c.GetAttribute("deleted")
2019-01-28 19:57:44 +00:00
if deleted != "deleted" {
addContact(&gobjects.Contact{
toc.Onion,
toc.Name,
cwutil.RandomProfileImage(toc.Onion),
"",
0,
0,
c.Trusted,
})
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
event.RemotePeer: contacts[i],
event.Key: "name",
event.Data: c.Name,
}))
}
2018-11-22 00:01:17 +00:00
}
cxnState, found := the.Peer.GetPeers()[contacts[i]]
if !found {
2019-02-14 19:30:31 +00:00
the.Peer.PeerWithOnion(contacts[i])
2018-11-22 00:01:17 +00:00
c2 := getContact(contacts[i])
if c2 != nil && c2.Status != -2 {
c2.Status = -2
updateContact(contacts[i])
}
} else {
c2 := getContact(contacts[i])
if c2 != nil && c2.Status != int(cxnState) {
c2.Status = int(cxnState)
updateContact(contacts[i])
}
}
}
time.Sleep(time.Second * 4)
}
2018-11-28 22:14:02 +00:00
}