2018-11-22 00:01:17 +00:00
|
|
|
package gothings
|
|
|
|
|
|
|
|
import (
|
2018-11-28 22:14:02 +00:00
|
|
|
"cwtch.im/ui/go/constants"
|
2019-02-05 21:09:38 +00:00
|
|
|
"cwtch.im/ui/go/cwutil"
|
2018-11-28 22:14:02 +00:00
|
|
|
"cwtch.im/ui/go/gobjects"
|
|
|
|
"cwtch.im/ui/go/the"
|
2018-11-22 00:01:17 +00:00
|
|
|
"encoding/base32"
|
2018-12-05 16:05:39 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
2019-02-04 23:00:12 +00:00
|
|
|
"runtime/debug"
|
2018-11-28 20:34:49 +00:00
|
|
|
"strings"
|
2018-11-22 00:01:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InterfaceState struct {
|
|
|
|
parentGcd *GrandCentralDispatcher
|
2018-11-28 20:34:49 +00:00
|
|
|
contacts map[string]*gobjects.Contact
|
|
|
|
messages map[string][]*gobjects.Message
|
2018-11-22 00:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUIState(gcd *GrandCentralDispatcher) (uis InterfaceState) {
|
|
|
|
uis = InterfaceState{gcd, make(map[string]*gobjects.Contact), make(map[string][]*gobjects.Message)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *InterfaceState) AddContact(c *gobjects.Contact) {
|
|
|
|
if len(c.Handle) == 32 { // ADD GROUP
|
|
|
|
if _, found := this.contacts[c.Handle]; !found {
|
|
|
|
this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted)
|
2019-02-13 03:08:23 +00:00
|
|
|
this.contacts[c.Handle] = c
|
2018-11-22 00:01:17 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if len(c.Handle) != 56 {
|
2018-12-05 16:05:39 +00:00
|
|
|
log.Errorf("sorry, unable to handle AddContact(%v)", c.Handle)
|
2019-02-04 23:00:12 +00:00
|
|
|
debug.PrintStack()
|
2018-11-22 00:01:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := this.contacts[c.Handle]; !found {
|
|
|
|
this.contacts[c.Handle] = c
|
|
|
|
this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted)
|
2019-02-13 03:08:23 +00:00
|
|
|
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)
|
|
|
|
go the.Peer.PeerWithOnion(c.Handle)
|
|
|
|
}
|
2018-11-22 00:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *InterfaceState) GetContact(handle string) *gobjects.Contact {
|
2019-02-05 21:09:38 +00:00
|
|
|
if _, found := this.contacts[handle]; !found {
|
2019-02-13 03:08:23 +00:00
|
|
|
if len(handle) == 32 {
|
|
|
|
group := the.Peer.GetGroup(handle)
|
2019-02-13 07:09:39 +00:00
|
|
|
if group != nil {
|
|
|
|
this.AddContact(&gobjects.Contact{
|
|
|
|
handle,
|
|
|
|
handle,
|
|
|
|
cwutil.RandomGroupImage(handle),
|
2019-02-14 02:42:13 +00:00
|
|
|
group.GroupServer,
|
2019-02-13 07:09:39 +00:00
|
|
|
0,
|
|
|
|
0,
|
2019-02-14 02:42:13 +00:00
|
|
|
group.Accepted,
|
2019-02-13 07:09:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
go the.Peer.JoinServer(group.GroupServer)
|
|
|
|
} else {
|
|
|
|
log.Errorf("Attempting to add non existent group to ui %v", handle)
|
|
|
|
}
|
2019-02-13 03:08:23 +00:00
|
|
|
} else {
|
2019-02-13 07:09:39 +00:00
|
|
|
contact := the.Peer.GetContact(handle)
|
|
|
|
if contact != nil && handle != contact.Onion {
|
|
|
|
this.AddContact(&gobjects.Contact{
|
|
|
|
handle,
|
|
|
|
handle,
|
|
|
|
cwutil.RandomProfileImage(handle),
|
|
|
|
"",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
})
|
|
|
|
} else if contact == nil {
|
2019-02-14 02:42:13 +00:00
|
|
|
//log.Errorf("Attempting to add non existent contact to ui %v", handle)
|
2019-02-13 07:09:39 +00:00
|
|
|
}
|
2019-02-05 21:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 00:01:17 +00:00
|
|
|
return this.contacts[handle]
|
|
|
|
}
|
|
|
|
|
2019-02-20 21:45:42 +00:00
|
|
|
func (this *InterfaceState) AddGroupError(signature string, err string) {
|
|
|
|
this.parentGcd.GroupSendError(signature, err)
|
|
|
|
}
|
|
|
|
|
2018-11-22 00:01:17 +00:00
|
|
|
func (this *InterfaceState) AddMessage(m *gobjects.Message) {
|
2019-02-13 07:09:39 +00:00
|
|
|
this.GetContact(m.From)
|
2018-11-22 00:01:17 +00:00
|
|
|
|
2019-02-13 07:09:39 +00:00
|
|
|
_, found := this.messages[m.Handle]
|
2018-11-22 00:01:17 +00:00
|
|
|
if !found {
|
|
|
|
this.messages[m.Handle] = make([]*gobjects.Message, 0)
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:07:12 +00:00
|
|
|
// Ack message sent to group
|
|
|
|
this.parentGcd.Acknowledged(m.MessageID)
|
|
|
|
|
|
|
|
// Ack personal messages
|
|
|
|
// TODO: unify this with the above signature based approach
|
2019-02-04 22:22:58 +00:00
|
|
|
if m.Message == "ack" {
|
|
|
|
// If an ack, swallow the message and ack from the list.
|
|
|
|
acklist := the.AcknowledgementIDs[m.From]
|
|
|
|
for _,ack := range acklist {
|
2019-02-11 21:05:01 +00:00
|
|
|
ack.Ack = true
|
|
|
|
this.parentGcd.Acknowledged(ack.ID)
|
2018-11-22 00:01:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 22:22:58 +00:00
|
|
|
this.messages[m.Handle] = append(this.messages[m.Handle], m)
|
|
|
|
|
2019-02-20 20:07:12 +00:00
|
|
|
// If we have this group loaded already
|
2019-02-04 22:22:58 +00:00
|
|
|
if this.parentGcd.CurrentOpenConversation() == m.Handle {
|
2019-02-20 20:07:12 +00:00
|
|
|
// 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)
|
|
|
|
} else {
|
|
|
|
this.parentGcd.Acknowledged(m.MessageID)
|
|
|
|
}
|
2019-02-04 22:22:58 +00:00
|
|
|
} else {
|
|
|
|
c := this.GetContact(m.Handle)
|
2019-02-13 07:09:39 +00:00
|
|
|
if c != nil {
|
|
|
|
c.Badge++
|
|
|
|
this.UpdateContact(c.Handle)
|
|
|
|
}
|
2019-02-04 22:22:58 +00:00
|
|
|
}
|
2018-11-22 00:01:17 +00:00
|
|
|
}
|
2019-02-04 22:22:58 +00:00
|
|
|
|
2018-11-22 00:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *InterfaceState) GetMessages(handle string) []*gobjects.Message {
|
|
|
|
_, found := this.messages[handle]
|
|
|
|
if !found {
|
|
|
|
this.messages[handle] = make([]*gobjects.Message, 0)
|
|
|
|
}
|
|
|
|
return this.messages[handle]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *InterfaceState) UpdateContact(handle string) {
|
|
|
|
c, found := this.contacts[handle]
|
|
|
|
if found {
|
|
|
|
this.parentGcd.UpdateContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted)
|
|
|
|
}
|
2018-11-28 20:34:49 +00:00
|
|
|
}
|
2019-02-13 07:09:39 +00:00
|
|
|
|
|
|
|
func (this *InterfaceState) UpdateContactAttribute(handle, key, value string) {
|
|
|
|
this.parentGcd.UpdateContactAttribute(handle, key, value)
|
|
|
|
}
|