updating ui to use eventbus

This commit is contained in:
erinn 2019-01-21 13:17:51 -08:00
parent 61bad4775a
commit 2e908a7560
7 changed files with 84 additions and 121 deletions

View File

@ -3,6 +3,7 @@ package characters
import (
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"time"
)
@ -14,8 +15,12 @@ func GroupPoller(getContact func(string) *gobjects.Contact, updateContact func(s
groups := the.Peer.GetGroups()
for i := range groups {
group := the.Peer.GetGroup(groups[i])
getContact(group.GroupID).Status = int(servers[group.GroupServer])
updateContact(group.GroupID)
if group != nil {
getContact(group.GroupID).Status = int(servers[group.GroupServer])
updateContact(group.GroupID)
} else {
log.Errorf("grouppoller found a group that is nil :/")
}
}
}
}

View File

@ -0,0 +1,41 @@
package characters
import (
"cwtch.im/cwtch/event"
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"time"
)
func IncomingListener(callback func(*gobjects.Message)) {
q := event.NewEventQueue(1000)
the.CwtchApp.EventBus().Subscribe(event.NewMessageFromPeer, q.EventChannel)
the.CwtchApp.EventBus().Subscribe(event.NewMessageFromGroup, q.EventChannel)
for {
e := q.Next()
log.Debugf("got event %s", e.EventType)
switch e.EventType {
case event.NewMessageFromPeer:
callback(&gobjects.Message{
Handle: e.Data["Onion"],
From: e.Data["Onion"],
Message: e.Data["Data"],
Image: cwutil.RandomProfileImage(e.Data["Onion"]),
Timestamp: time.Now(),
})
case event.NewMessageFromGroup:
log.Debugf("NewMessageFromGroup!")
callback(&gobjects.Message{
Handle: e.Data["GroupID"],
From: e.Data["Onion"],
Message: e.Data["Data"],
Image: cwutil.RandomGroupImage(e.Data["GroupID"]),
Timestamp: time.Now(),
})
}
}
}

View File

@ -3,7 +3,6 @@ package characters
import (
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
)
@ -26,14 +25,9 @@ func PostmanPat(messages chan gobjects.Letter) {
func andHisBlackAndWhiteCat(incomingMessages chan gobjects.Letter) {
for {
m := <-incomingMessages
connection := the.Peer.PeerWithOnion(m.To)
connection.WaitTilAuthenticated()
connection.DoOnChannel("im.ricochet.chat", channels.Outbound, func(channel *channels.Channel) {
chatchannel, ok := channel.Handler.(*channels.ChatChannel)
if ok {
log.Debugln("Sending packet")
the.AcknowledgementIDs[chatchannel.SendMessage(m.Message)] = m.MID
}
})
the.Peer.PeerWithOnion(m.To)
log.Debugf("sending message!")
id := the.Peer.SendMessageToPeer(m.To, m.Message)
the.AcknowledgementIDs[id] = m.MID
}
}

View File

@ -1,60 +0,0 @@
package cwtchthings
import (
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/application"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
"time"
)
type ChatChannelListener struct {
rai *application.ApplicationInstance
ra *application.RicochetApplication
addMessage func(*gobjects.Message)
acknowledged func(uint)
}
func (this *ChatChannelListener) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication, addMessage func(*gobjects.Message), acknowledged func(uint)) {
this.rai = rai
this.ra = ra
this.addMessage = addMessage
this.acknowledged = acknowledged
}
// We always want bidirectional chat channels
func (this *ChatChannelListener) OpenInbound() {
outboutChatChannel := this.rai.Connection.Channel("im.ricochet.chat", channels.Outbound)
if outboutChatChannel == nil {
this.rai.Connection.Do(func() error {
this.rai.Connection.RequestOpenChannel("im.ricochet.chat",
&channels.ChatChannel{
Handler: this,
})
return nil
})
}
}
func (this *ChatChannelListener) ChatMessage(messageID uint32, when time.Time, message string) bool {
this.addMessage(&gobjects.Message{
this.rai.RemoteHostname,
this.rai.RemoteHostname,
"",
message,
cwutil.RandomProfileImage(this.rai.RemoteHostname),
false,
int(messageID),
when,
})
go func() { // TODO: this is probably no longer necessary. check later
time.Sleep(time.Second)
the.CwtchApp.SaveProfile(the.Peer)
}()
return true
}
func (this *ChatChannelListener) ChatMessageAck(messageID uint32, accepted bool) {
this.acknowledged(the.AcknowledgementIDs[messageID])
}

View File

@ -201,11 +201,19 @@ func (this *GrandCentralDispatcher) importString(str string) {
}
group := the.Peer.GetGroup(groupID)
log.Debugf("group id: %s", groupID)
if group == nil {
log.Debugf("group IS nil. bad!")
} else {
log.Debugf("group is NOT nil. good!")
}
the.Peer.JoinServer(group.GroupServer)
the.CwtchApp.SaveProfile(the.Peer)
this.UIState.AddContact(&gobjects.Contact{
groupID[:12],
groupID,
groupID[:12],
cwutil.RandomGroupImage(groupID),
group.GroupServer,
0,
@ -263,13 +271,10 @@ func (this *GrandCentralDispatcher) importString(str string) {
}
this.UIState.AddContact(&gobjects.Contact{
onion,
name,
cwutil.RandomProfileImage(onion),
"",
0,
0,
true,
Handle: onion,
DisplayName: name,
Image: cwutil.RandomProfileImage(onion),
Trusted: true,
})
}
@ -290,13 +295,11 @@ func (this *GrandCentralDispatcher) createGroup(server, groupName string) {
}
this.UIState.AddContact(&gobjects.Contact{
groupID,
groupName,
cwutil.RandomGroupImage(groupID),
server,
0,
0,
true,
Handle: groupID,
DisplayName: groupName,
Image: cwutil.RandomGroupImage(groupID),
Server: server,
Trusted: true,
})
group := the.Peer.GetGroup(groupID)

View File

@ -8,4 +8,4 @@ import (
var CwtchApp app.Application
var Peer libPeer.CwtchPeer
var CwtchDir string
var AcknowledgementIDs map[uint32]uint
var AcknowledgementIDs map[string]uint

46
main.go
View File

@ -4,14 +4,11 @@ import (
libapp "cwtch.im/cwtch/app"
"cwtch.im/cwtch/model"
"cwtch.im/ui/go/characters"
"cwtch.im/ui/go/cwtchthings"
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/gothings"
"cwtch.im/ui/go/the"
"fmt"
"git.openprivacy.ca/openprivacy/libricochet-go/application"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"github.com/therecipe/qt/core"
@ -29,10 +26,12 @@ func init() {
}
func main() {
log.SetLevel(log.LevelDebug)
// our globals
gcd := gothings.NewGrandCentralDispatcher(nil)
gcd.UIState = gothings.NewUIState(gcd)
the.AcknowledgementIDs = make(map[uint32]uint)
the.AcknowledgementIDs = make(map[string]uint)
gcd.OutgoingMessages = make(chan gobjects.Letter, 1000)
//TODO: put theme stuff somewhere better
@ -57,6 +56,7 @@ func main() {
// these are long-lived pollers/listeners for incoming messages and status changes
loadCwtchData(gcd)
go characters.IncomingListener(gcd.UIState.AddMessage)
go characters.PostmanPat(gcd.OutgoingMessages)
go characters.TorStatusPoller(gcd.TorStatus)
go characters.PresencePoller(gcd.UIState.GetContact, gcd.UIState.AddContact, gcd.UIState.UpdateContact)
@ -132,20 +132,6 @@ func loadCwtchData(gcd *gothings.GrandCentralDispatcher) {
}
gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion))
aif := application.ApplicationInstanceFactory{}
aif.Init()
app := new(application.RicochetApplication)
aif.AddHandler("im.ricochet.chat", func(rai *application.ApplicationInstance) func() channels.Handler {
ccl := new(cwtchthings.ChatChannelListener)
ccl.Init(rai, app, gcd.UIState.AddMessage, gcd.Acknowledged)
return func() channels.Handler {
chat := new(channels.ChatChannel)
chat.Handler = ccl
return chat
}
})
the.Peer.SetApplicationInstanceFactory(aif)
the.CwtchApp.LaunchPeers()
contacts := the.Peer.GetContacts()
@ -153,13 +139,10 @@ func loadCwtchData(gcd *gothings.GrandCentralDispatcher) {
contact, _ := the.Peer.GetProfile().GetContact(contacts[i])
displayName, _ := contact.GetAttribute("name")
gcd.UIState.AddContact(&gobjects.Contact{
contacts[i],
displayName,
cwutil.RandomProfileImage(contacts[i]),
"",
0,
0,
contact.Trusted,
Handle: contacts[i],
DisplayName: displayName,
Image: cwutil.RandomProfileImage(contacts[i]),
Trusted: contact.Trusted,
})
}
@ -168,20 +151,17 @@ func loadCwtchData(gcd *gothings.GrandCentralDispatcher) {
log.Infof("adding saved group %v", groups[i])
group := the.Peer.GetGroup(groups[i])
group.NewMessage = make(chan model.Message)
go characters.CwtchListener(gcd.UIState.AddMessage, groups[i], group.NewMessage)
the.Peer.JoinServer(group.GroupServer)
nick, exists := group.GetAttribute("nick")
if !exists {
nick = group.GroupID[:12]
}
gcd.UIState.AddContact(&gobjects.Contact{
group.GroupID,
nick,
cwutil.RandomGroupImage(group.GroupID),
group.GroupServer,
0,
0,
group.Accepted,
Handle: group.GroupID,
DisplayName: nick,
Image: cwutil.RandomGroupImage(group.GroupID),
Server: group.GroupServer,
Trusted: group.Accepted,
})
}
}