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 ( import (
"cwtch.im/ui/go/gobjects" "cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the" "cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"time" "time"
) )
@ -14,8 +15,12 @@ func GroupPoller(getContact func(string) *gobjects.Contact, updateContact func(s
groups := the.Peer.GetGroups() groups := the.Peer.GetGroups()
for i := range groups { for i := range groups {
group := the.Peer.GetGroup(groups[i]) group := the.Peer.GetGroup(groups[i])
getContact(group.GroupID).Status = int(servers[group.GroupServer]) if group != nil {
updateContact(group.GroupID) 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 ( import (
"cwtch.im/ui/go/gobjects" "cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/the" "cwtch.im/ui/go/the"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
"git.openprivacy.ca/openprivacy/libricochet-go/log" "git.openprivacy.ca/openprivacy/libricochet-go/log"
) )
@ -26,14 +25,9 @@ func PostmanPat(messages chan gobjects.Letter) {
func andHisBlackAndWhiteCat(incomingMessages chan gobjects.Letter) { func andHisBlackAndWhiteCat(incomingMessages chan gobjects.Letter) {
for { for {
m := <-incomingMessages m := <-incomingMessages
connection := the.Peer.PeerWithOnion(m.To) the.Peer.PeerWithOnion(m.To)
connection.WaitTilAuthenticated() log.Debugf("sending message!")
connection.DoOnChannel("im.ricochet.chat", channels.Outbound, func(channel *channels.Channel) { id := the.Peer.SendMessageToPeer(m.To, m.Message)
chatchannel, ok := channel.Handler.(*channels.ChatChannel) the.AcknowledgementIDs[id] = m.MID
if ok {
log.Debugln("Sending packet")
the.AcknowledgementIDs[chatchannel.SendMessage(m.Message)] = 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) 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.Peer.JoinServer(group.GroupServer)
the.CwtchApp.SaveProfile(the.Peer) the.CwtchApp.SaveProfile(the.Peer)
this.UIState.AddContact(&gobjects.Contact{ this.UIState.AddContact(&gobjects.Contact{
groupID[:12],
groupID, groupID,
groupID[:12],
cwutil.RandomGroupImage(groupID), cwutil.RandomGroupImage(groupID),
group.GroupServer, group.GroupServer,
0, 0,
@ -263,13 +271,10 @@ func (this *GrandCentralDispatcher) importString(str string) {
} }
this.UIState.AddContact(&gobjects.Contact{ this.UIState.AddContact(&gobjects.Contact{
onion, Handle: onion,
name, DisplayName: name,
cwutil.RandomProfileImage(onion), Image: cwutil.RandomProfileImage(onion),
"", Trusted: true,
0,
0,
true,
}) })
} }
@ -290,13 +295,11 @@ func (this *GrandCentralDispatcher) createGroup(server, groupName string) {
} }
this.UIState.AddContact(&gobjects.Contact{ this.UIState.AddContact(&gobjects.Contact{
groupID, Handle: groupID,
groupName, DisplayName: groupName,
cwutil.RandomGroupImage(groupID), Image: cwutil.RandomGroupImage(groupID),
server, Server: server,
0, Trusted: true,
0,
true,
}) })
group := the.Peer.GetGroup(groupID) group := the.Peer.GetGroup(groupID)

View File

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