Merge branch 'tapir' of cwtch.im/ui into master

This commit is contained in:
Dan Ballard 2019-08-01 15:32:17 -07:00 committed by Gogs
commit 732d8ab671
7 changed files with 78 additions and 67 deletions

View File

@ -75,9 +75,6 @@ func AppEventListener(gcd *gothings.GrandCentralDispatcher, subscribed chan bool
go PresencePoller(&gcd.UIState) go PresencePoller(&gcd.UIState)
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))
if e.Data[event.Status] != "running" {
the.CwtchApp.LaunchPeers()
}
contacts := the.Peer.GetContacts() contacts := the.Peer.GetContacts()
for i := range contacts { for i := range contacts {
@ -117,6 +114,10 @@ func AppEventListener(gcd *gothings.GrandCentralDispatcher, subscribed chan bool
} }
} }
if e.Data[event.Status] != "running" {
the.CwtchApp.LaunchPeers()
}
// load ui preferences // load ui preferences
gcd.RequestSettings() gcd.RequestSettings()
locale, exists := the.Peer.GetProfile().GetAttribute("settings.locale") locale, exists := the.Peer.GetProfile().GetAttribute("settings.locale")

View File

@ -14,6 +14,7 @@ import (
func IncomingListener(uiState *gothings.InterfaceState, subscribed chan bool) { func IncomingListener(uiState *gothings.InterfaceState, subscribed chan bool) {
q := event.NewEventQueue(1000) q := event.NewEventQueue(1000)
the.EventBus.Subscribe(event.NewMessageFromPeer, q.EventChannel) the.EventBus.Subscribe(event.NewMessageFromPeer, q.EventChannel)
the.EventBus.Subscribe(event.PeerAcknowledgement, q.EventChannel)
the.EventBus.Subscribe(event.NewMessageFromGroup, q.EventChannel) the.EventBus.Subscribe(event.NewMessageFromGroup, q.EventChannel)
the.EventBus.Subscribe(event.NewGroupInvite, q.EventChannel) the.EventBus.Subscribe(event.NewGroupInvite, q.EventChannel)
the.EventBus.Subscribe(event.SendMessageToGroupError, q.EventChannel) the.EventBus.Subscribe(event.SendMessageToGroupError, q.EventChannel)
@ -38,10 +39,18 @@ func IncomingListener(uiState *gothings.InterfaceState, subscribed chan bool) {
if the.Peer.GetContact(e.Data[event.RemotePeer]) == nil { if the.Peer.GetContact(e.Data[event.RemotePeer]) == nil {
the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], 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" { case event.PeerAcknowledgement:
the.Peer.SendMessageToPeer(e.Data[event.RemotePeer], "ack") ackI, ok := the.AcknowledgementIDs.Load(e.Data[event.EventID])
if ok {
ack := ackI.(*the.AckId)
if ack.Peer == e.Data[event.RemotePeer] {
ack.Ack = true
uiState.Acknowledge(e.Data[event.EventID])
continue
}
} }
log.Debugf("Received Ack ID for unknown message or peer: %v", e)
case event.NewMessageFromGroup: //event.TimestampReceived, event.TimestampSent, event.Data, event.GroupID, event.RemotePeer case event.NewMessageFromGroup: //event.TimestampReceived, event.TimestampSent, event.Data, event.GroupID, event.RemotePeer
var name string var name string
var exists bool var exists bool
@ -75,18 +84,27 @@ func IncomingListener(uiState *gothings.InterfaceState, subscribed chan bool) {
uiState.AddSendMessageError(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.PeerStateChange: case event.PeerStateChange:
cxnState := connections.ConnectionStateToType[e.Data[event.ConnectionState]] cxnState := connections.ConnectionStateToType[e.Data[event.ConnectionState]]
_, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]
if !exists {
// Contact does not exist, we will add them but we won't know who they are until they are authenticated
// So if we get any other state from an unknown contact we do nothing
// (the next exists check will fail)
if cxnState == connections.AUTHENTICATED {
the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], false)
}
}
if contact, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]; exists { if contact, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]; exists {
uiContact := uiState.GetContact(contact.Onion) uiContact := uiState.GetContact(contact.Onion)
if uiContact != nil && uiContact.Status != int(cxnState) { if uiContact != nil && uiContact.Status != int(cxnState) {
uiContact.Status = int(cxnState) uiContact.Status = int(cxnState)
uiState.UpdateContact(contact.Onion) uiState.UpdateContact(contact.Onion)
} }
} else {
the.Peer.PeerWithOnion(contact.Onion) // Stub reconnection-handler simply attempts reconnection
c2 := uiState.GetContact(contact.Onion) if cxnState == connections.DISCONNECTED {
if c2 != nil && c2.Status != -2 { the.Peer.PeerWithOnion(contact.Onion)
c2.Status = -2
uiState.UpdateContact(contact.Onion)
} }
} }
case event.ServerStateChange: case event.ServerStateChange:

View File

@ -3,14 +3,14 @@ package gobjects
import "time" import "time"
type Message struct { type Message struct {
Handle string Handle string
From string From string
DisplayName string DisplayName string
Message string Message string
Image string Image string
FromMe bool FromMe bool
MessageID string MessageID string
Timestamp time.Time Timestamp time.Time
Acknowledged bool Acknowledged bool
Error bool Error bool
} }

View File

@ -127,7 +127,6 @@ func (this *GrandCentralDispatcher) sendMessage(message string, mID string) {
} }
to := this.CurrentOpenConversation() to := this.CurrentOpenConversation()
the.Peer.PeerWithOnion(to)
mID = the.Peer.SendMessageToPeer(to, message) mID = the.Peer.SendMessageToPeer(to, message)
this.UIState.AddMessage(&gobjects.Message{ this.UIState.AddMessage(&gobjects.Message{
@ -146,7 +145,8 @@ func (this *GrandCentralDispatcher) sendMessage(message string, mID string) {
ackID := new(the.AckId) ackID := new(the.AckId)
ackID.ID = mID ackID.ID = mID
ackID.Ack = false ackID.Ack = false
the.AcknowledgementIDs[to] = append(the.AcknowledgementIDs[to], ackID) ackID.Peer = to
the.AcknowledgementIDs.Store(mID, ackID)
} }
} }
@ -237,6 +237,13 @@ func (this *GrandCentralDispatcher) loadMessagesPaneHelper(handle string) {
from = "me" from = "me"
} }
ackI, ok := the.AcknowledgementIDs.Load(messages[i].MessageID)
acked := false
if ok {
ack := ackI.(*the.AckId)
acked = ack.Ack
}
this.AppendMessage( this.AppendMessage(
messages[i].Handle, messages[i].Handle,
from, from,
@ -246,14 +253,10 @@ func (this *GrandCentralDispatcher) loadMessagesPaneHelper(handle string) {
messages[i].MessageID, messages[i].MessageID,
messages[i].FromMe, messages[i].FromMe,
messages[i].Timestamp.Format(constants.TIME_FORMAT), messages[i].Timestamp.Format(constants.TIME_FORMAT),
false, acked,
messages[i].Error, messages[i].Error,
) )
for _, id := range the.AcknowledgementIDs[messages[i].Handle] {
if id.ID == messages[i].MessageID && id.Ack && !id.Error {
this.Acknowledged(id.ID)
}
}
} }
} }

View File

@ -22,6 +22,10 @@ func NewUIState(gcd *GrandCentralDispatcher) (uis InterfaceState) {
return return
} }
func (this *InterfaceState) Acknowledge(mID string) {
this.parentGcd.Acknowledged(mID)
}
func (this *InterfaceState) AddContact(c *gobjects.Contact) { func (this *InterfaceState) AddContact(c *gobjects.Contact) {
if len(c.Handle) == 32 { // ADD GROUP if len(c.Handle) == 32 { // ADD GROUP
if _, found := this.contacts.Load(c.Handle); !found { if _, found := this.contacts.Load(c.Handle); !found {
@ -64,8 +68,6 @@ func (this *InterfaceState) GetContact(handle string) *gobjects.Contact {
group.Accepted, group.Accepted,
false, false,
}) })
go the.Peer.JoinServer(group.GroupServer)
} else { } else {
log.Errorf("Attempting to add non existent group to ui %v", handle) log.Errorf("Attempting to add non existent group to ui %v", handle)
} }
@ -101,12 +103,12 @@ func (this *InterfaceState) GetContact(handle string) *gobjects.Contact {
} }
func (this *InterfaceState) AddSendMessageError(peer string, signature string, err string) { func (this *InterfaceState) AddSendMessageError(peer string, signature string, err string) {
acklist := the.AcknowledgementIDs[peer] ackI, ok := the.AcknowledgementIDs.Load(signature)
for _, ack := range acklist { if ok {
if ack.ID == signature { ack := ackI.(*the.AckId)
ack.Error = true ack.Error = true
}
} }
messages, _ := this.messages.Load(peer) messages, _ := this.messages.Load(peer)
messageList, _ := messages.([]*gobjects.Message) messageList, _ := messages.([]*gobjects.Message)
@ -132,37 +134,23 @@ func (this *InterfaceState) AddMessage(m *gobjects.Message) {
// Ack message sent to group // Ack message sent to group
this.parentGcd.Acknowledged(m.MessageID) this.parentGcd.Acknowledged(m.MessageID)
// Ack personal messages messages, _ := this.messages.Load(m.Handle)
// TODO: unify this with the above signature based approach messageList, _ := messages.([]*gobjects.Message)
if m.Message == "ack" { this.messages.Store(m.Handle, append(messageList, m))
// If an ack, swallow the message and ack from the list.
acklist := the.AcknowledgementIDs[m.From] // If we have this group loaded already
for _, ack := range acklist { if this.parentGcd.CurrentOpenConversation() == m.Handle {
if ack.Error == false { // If the message is not from the user then add it, otherwise, just acknowledge.
ack.Ack = true if !m.FromMe || !m.Acknowledged {
this.parentGcd.Acknowledged(ack.ID) this.parentGcd.AppendMessage(m.Handle, m.From, m.DisplayName, m.Message, m.Image, m.MessageID, m.FromMe, m.Timestamp.Format(constants.TIME_FORMAT), m.Acknowledged, m.Error)
} } else {
this.parentGcd.Acknowledged(m.MessageID)
} }
} else { } else {
c := this.GetContact(m.Handle)
messages, _ := this.messages.Load(m.Handle) if c != nil {
messageList, _ := messages.([]*gobjects.Message) c.Badge++
this.messages.Store(m.Handle, append(messageList, m)) this.UpdateContact(c.Handle)
// If we have this group loaded already
if this.parentGcd.CurrentOpenConversation() == m.Handle {
// If the message is not from the user then add it, otherwise, just acknowledge.
if !m.FromMe || !m.Acknowledged {
this.parentGcd.AppendMessage(m.Handle, m.From, m.DisplayName, m.Message, m.Image, m.MessageID, m.FromMe, m.Timestamp.Format(constants.TIME_FORMAT), m.Acknowledged, m.Error)
} else {
this.parentGcd.Acknowledged(m.MessageID)
}
} else {
c := this.GetContact(m.Handle)
if c != nil {
c.Badge++
this.UpdateContact(c.Handle)
}
} }
} }

View File

@ -5,6 +5,7 @@ import (
"cwtch.im/cwtch/event" "cwtch.im/cwtch/event"
libPeer "cwtch.im/cwtch/peer" libPeer "cwtch.im/cwtch/peer"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity" "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"sync"
) )
// Terrible, to be replaced when proper profile/password management comes in ~ 0.2 // Terrible, to be replaced when proper profile/password management comes in ~ 0.2
@ -21,8 +22,9 @@ var IPCBridge event.IPCBridge
type AckId struct { type AckId struct {
ID string ID string
Peer string
Ack bool Ack bool
Error bool Error bool
} }
var AcknowledgementIDs map[string][]*AckId var AcknowledgementIDs sync.Map

View File

@ -113,7 +113,6 @@ func mainUi(flagLocal bool, flagClientUI bool) {
gcd.SetBuildDate("now") gcd.SetBuildDate("now")
} }
gcd.UIState = gothings.NewUIState(gcd) gcd.UIState = gothings.NewUIState(gcd)
the.AcknowledgementIDs = make(map[string][]*the.AckId)
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