Merge branch 'cwtchRefactor2' of dan/ui into master

This commit is contained in:
erinn 2019-05-22 13:23:03 -07:00 committed by Gogs
commit 007e485e76
7 changed files with 67 additions and 87 deletions

View File

@ -1,29 +0,0 @@
package characters
import (
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"time"
)
func GroupPoller(getContact func(string) *gobjects.Contact, updateContact func(string)) {
for {
time.Sleep(time.Second * 4)
servers := the.Peer.GetServers()
groups := the.Peer.GetGroups()
for i := range groups {
group := the.Peer.GetGroup(groups[i])
if group != nil && group.GroupID != "" {
deleted,_ := group.GetAttribute("deleted")
if deleted != "deleted" {
getContact(group.GroupID).Status = int(servers[group.GroupServer])
updateContact(group.GroupID)
}
} else {
log.Errorf("grouppoller found a group that is nil :/")
}
}
}
}

View File

@ -2,24 +2,26 @@ package characters
import (
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/protocol/connections"
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/gothings"
"cwtch.im/ui/go/the"
"encoding/base32"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"strings"
"time"
)
func IncomingListener(callback func(*gobjects.Message), groupErrorCallback func(string, string, string), serverStateCallback func(string, bool)) {
func IncomingListener(uiState *gothings.InterfaceState) {
q := event.NewEventQueue(1000)
the.CwtchApp.EventBus().Subscribe(event.NewMessageFromPeer, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.NewMessageFromGroup, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.NewGroupInvite, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.SendMessageToGroupError, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.SendMessageToPeerError, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.JoinServer, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.FinishedFetch, q.EventChannel)
the.EventBus.Subscribe(event.NewMessageFromPeer, q.EventChannel)
the.EventBus.Subscribe(event.NewMessageFromGroup, q.EventChannel)
the.EventBus.Subscribe(event.NewGroupInvite, q.EventChannel)
the.EventBus.Subscribe(event.SendMessageToGroupError, q.EventChannel)
the.EventBus.Subscribe(event.SendMessageToPeerError, q.EventChannel)
the.EventBus.Subscribe(event.JoinServer, q.EventChannel)
the.EventBus.Subscribe(event.FinishedFetch, q.EventChannel)
the.EventBus.Subscribe(event.ServerStateChange, q.EventChannel)
the.EventBus.Subscribe(event.PeerStateChange, q.EventChannel)
for {
e := q.Next()
@ -27,7 +29,7 @@ func IncomingListener(callback func(*gobjects.Message), groupErrorCallback func(
switch e.EventType {
case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data
ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampReceived])
callback(&gobjects.Message{
uiState.AddMessage(&gobjects.Message{
Handle: e.Data[event.RemotePeer],
From: e.Data[event.RemotePeer],
Message: e.Data[event.Data],
@ -35,8 +37,7 @@ func IncomingListener(callback func(*gobjects.Message), groupErrorCallback func(
Timestamp: ts,
})
if the.Peer.GetContact(e.Data[event.RemotePeer]) == nil {
decodedPub, _ := base32.StdEncoding.DecodeString(strings.ToUpper(e.Data[event.RemotePeer]))
the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], decodedPub, false)
the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], false)
}
the.Peer.PeerWithOnion(e.Data[event.RemotePeer])
if e.Data[event.Data] != "ack" {
@ -56,7 +57,7 @@ func IncomingListener(callback func(*gobjects.Message), groupErrorCallback func(
}
ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampSent])
callback(&gobjects.Message{
uiState.AddMessage(&gobjects.Message{
MessageID: e.Data[event.Signature],
Handle: e.Data[event.GroupID],
From: e.Data[event.RemotePeer],
@ -70,13 +71,42 @@ func IncomingListener(callback func(*gobjects.Message), groupErrorCallback func(
case event.NewGroupInvite:
log.Debugf("got a group invite!")
case event.SendMessageToGroupError:
groupErrorCallback(e.Data[event.GroupServer], e.Data[event.Signature], e.Data[event.Error])
uiState.AddSendMessageError(e.Data[event.GroupServer], e.Data[event.Signature], e.Data[event.Error])
case event.SendMessageToPeerError:
groupErrorCallback(e.Data[event.RemotePeer], e.Data[event.Signature], e.Data[event.Error])
uiState.AddSendMessageError(e.Data[event.RemotePeer], e.Data[event.Signature], e.Data[event.Error])
case event.JoinServer:
serverStateCallback(e.Data[event.GroupServer], true)
uiState.UpdateServerStatus(e.Data[event.GroupServer], true)
case event.FinishedFetch:
serverStateCallback(e.Data[event.GroupServer], false)
uiState.UpdateServerStatus(e.Data[event.GroupServer], false)
case event.PeerStateChange:
cxnState := connections.ConnectionStateType[e.Data[event.ConnectionState]]
if contact, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]; exists {
uiContact := uiState.GetContact(contact.Onion)
if uiContact != nil && uiContact.Status != int(cxnState) {
uiContact.Status = int(cxnState)
uiState.UpdateContact(contact.Onion)
}
} else {
the.Peer.PeerWithOnion(contact.Onion)
c2 := uiState.GetContact(contact.Onion)
if c2 != nil && c2.Status != -2 {
c2.Status = -2
uiState.UpdateContact(contact.Onion)
}
}
case event.ServerStateChange:
serverOnion := e.Data[event.GroupServer]
state := connections.ConnectionStateType[e.Data[event.ConnectionState]]
groups := the.Peer.GetGroups()
for _, groupID := range groups {
group := the.Peer.GetGroup(groupID)
if group != nil && group.GroupServer == serverOnion {
uiState.GetContact(group.GroupID).Status = int(state)
uiState.UpdateContact(group.GroupID)
} else {
log.Errorf("grouppoller found a group that is nil :/")
}
}
}
}
}

View File

@ -4,23 +4,24 @@ import (
"cwtch.im/cwtch/event"
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/gothings"
"cwtch.im/ui/go/the"
"time"
)
func PresencePoller(getContact func(string) *gobjects.Contact, addContact func(contact *gobjects.Contact), updateContact func(string)) { // TODO: make this subscribe-able in ricochet
func PresencePoller(uiState *gothings.InterfaceState) {
time.Sleep(time.Second * 4)
for {
contacts := the.Peer.GetContacts()
for i := range contacts {
ct := getContact(contacts[i])
ct := uiState.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")
if deleted != "deleted" {
addContact(&gobjects.Contact{
uiState.AddContact(&gobjects.Contact{
toc.Onion,
toc.Name,
cwutil.RandomProfileImage(toc.Onion),
@ -31,7 +32,7 @@ func PresencePoller(getContact func(string) *gobjects.Contact, addContact func(c
false,
})
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
event.RemotePeer: contacts[i],
event.Key: "name",
event.Data: c.Name,
@ -39,21 +40,6 @@ func PresencePoller(getContact func(string) *gobjects.Contact, addContact func(c
}
}
cxnState, found := the.Peer.GetPeers()[contacts[i]]
if !found {
the.Peer.PeerWithOnion(contacts[i])
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)
}

View File

@ -270,7 +270,7 @@ func (this *GrandCentralDispatcher) saveSettings(zoom, locale string) {
return
}
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{
event.Key: "settings.zoom",
event.Data: zoom,
}))
@ -302,7 +302,7 @@ func (this *GrandCentralDispatcher) savePeerSettings(onion, nick string) {
}
contact.SetAttribute("nick", nick)
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
event.RemotePeer: onion,
event.Key: "nick",
event.Data: nick,
@ -346,7 +346,7 @@ func (this *GrandCentralDispatcher) saveGroupSettings(groupID, nick string) {
}
group.SetAttribute("nick", nick)
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
event.GroupID: groupID,
event.Key: "nick",
event.Data: nick,
@ -443,7 +443,7 @@ func (this *GrandCentralDispatcher) popup(str string) {
func (this *GrandCentralDispatcher) updateNick(nick string) {
the.Peer.GetProfile().Name = nick
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetProfileName, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetProfileName, map[event.Field]string{
event.ProfileName: nick,
}))
}
@ -465,7 +465,7 @@ func (this *GrandCentralDispatcher) createGroup(server, groupName string) {
group := the.Peer.GetGroup(groupID)
group.SetAttribute("nick", groupName)
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
event.GroupID: groupID,
event.Key: "nick",
event.Data: groupName,
@ -482,7 +482,7 @@ func (this *GrandCentralDispatcher) inviteToGroup(onion, groupID string) {
}
func (this *GrandCentralDispatcher) leaveGroup(groupID string) {
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
event.GroupID: groupID,
event.Key: "deleted",
event.Data: "deleted",
@ -501,7 +501,7 @@ func (this *GrandCentralDispatcher) setAttribute(onion, key, value string) {
pp, _ := the.Peer.GetProfile().GetContact(onion)
if pp != nil {
pp.SetAttribute(key, value)
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
event.RemotePeer: onion,
event.Key: key,
event.Data: value,
@ -513,7 +513,7 @@ func (this *GrandCentralDispatcher) setAttribute(onion, key, value string) {
func (this *GrandCentralDispatcher) setLocale(locale string) {
this.SetLocale_helper(locale)
the.CwtchApp.EventBus().Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{
the.EventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{
event.Key: "settings.locale",
event.Data: locale,
}))

View File

@ -5,10 +5,8 @@ import (
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
"encoding/base32"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"runtime/debug"
"strings"
"sync"
"time"
)
@ -41,8 +39,7 @@ func (this *InterfaceState) AddContact(c *gobjects.Contact) {
this.contacts[c.Handle] = c
this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, false)
if the.Peer.GetContact(c.Handle) == nil {
decodedPub, _ := base32.StdEncoding.DecodeString(strings.ToUpper(c.Handle))
the.Peer.AddContact(c.DisplayName, c.Handle, decodedPub, c.Trusted)
the.Peer.AddContact(c.DisplayName, c.Handle, c.Trusted)
go the.Peer.PeerWithOnion(c.Handle)
}
}

View File

@ -2,11 +2,13 @@ package the
import (
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/event"
libPeer "cwtch.im/cwtch/peer"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
)
var CwtchApp app.Application
var EventBus *event.Manager
var ACN connectivity.ACN
var Peer libPeer.CwtchPeer
var CwtchDir string

12
main.go
View File

@ -167,10 +167,9 @@ func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
// these are long-lived pollers/listeners for incoming messages and status changes
loadCwtchData(gcd, the.ACN)
go characters.IncomingListener(gcd.UIState.AddMessage, gcd.UIState.AddSendMessageError, gcd.UIState.UpdateServerStatus)
go characters.IncomingListener(&gcd.UIState)
go characters.TorStatusPoller(gcd.TorStatus, the.ACN)
go characters.PresencePoller(gcd.UIState.GetContact, gcd.UIState.AddContact, gcd.UIState.UpdateContact)
go characters.GroupPoller(gcd.UIState.GetContact, gcd.UIState.UpdateContact)
go characters.PresencePoller(&gcd.UIState)
// load ui preferences
gcd.RequestSettings()
@ -212,6 +211,7 @@ func loadCwtchData(gcd *gothings.GrandCentralDispatcher, acn connectivity.ACN) {
} else {
the.Peer = the.CwtchApp.PrimaryIdentity()
}
the.EventBus = the.CwtchApp.GetEventBus(the.Peer.GetProfile().Onion)
gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion))
the.CwtchApp.LaunchPeers()
@ -251,12 +251,6 @@ func loadCwtchData(gcd *gothings.GrandCentralDispatcher, acn connectivity.ACN) {
Trusted: group.Accepted,
Loading: true,
})
// Only send a join server packet if we haven't joined this server yet...
_, connecting := the.Peer.GetServers()[group.GroupServer]
if group.Accepted && !connecting {
the.Peer.JoinServer(group.GroupServer)
}
}
}
}