From 30fbbef6b7c1c7015268bfc214a81a2b7901ce67 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 22 May 2019 11:18:53 -0700 Subject: [PATCH] Refactor to match cwtch refactor - presense changes are events, minor library API changes --- go/characters/grouppoller.go | 29 -------------- go/characters/incominglistener.go | 66 ++++++++++++++++++++++--------- go/characters/presencepoller.go | 24 +++-------- go/gothings/gcd.go | 16 ++++---- go/gothings/uistate.go | 5 +-- go/the/globals.go | 2 + main.go | 12 ++---- 7 files changed, 67 insertions(+), 87 deletions(-) delete mode 100644 go/characters/grouppoller.go diff --git a/go/characters/grouppoller.go b/go/characters/grouppoller.go deleted file mode 100644 index 0bb3a960..00000000 --- a/go/characters/grouppoller.go +++ /dev/null @@ -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 :/") - } - } - } -} diff --git a/go/characters/incominglistener.go b/go/characters/incominglistener.go index 506e068f..a239758f 100644 --- a/go/characters/incominglistener.go +++ b/go/characters/incominglistener.go @@ -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 :/") + } + } } } } diff --git a/go/characters/presencepoller.go b/go/characters/presencepoller.go index ef011eb5..62fedaf5 100644 --- a/go/characters/presencepoller.go +++ b/go/characters/presencepoller.go @@ -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) } diff --git a/go/gothings/gcd.go b/go/gothings/gcd.go index d95175d4..5de6210f 100644 --- a/go/gothings/gcd.go +++ b/go/gothings/gcd.go @@ -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, })) diff --git a/go/gothings/uistate.go b/go/gothings/uistate.go index f4ccc3c1..4d7c3831 100644 --- a/go/gothings/uistate.go +++ b/go/gothings/uistate.go @@ -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) } } diff --git a/go/the/globals.go b/go/the/globals.go index 06b40a69..72171dc2 100644 --- a/go/the/globals.go +++ b/go/the/globals.go @@ -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 diff --git a/main.go b/main.go index 191cef62..a2caea21 100644 --- a/main.go +++ b/main.go @@ -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) - } } } }