cli: Better tracking of contact list state
This commit is contained in:
parent
70af42e39e
commit
c089cf3e34
24
cli/cli.go
24
cli/cli.go
|
@ -54,16 +54,10 @@ func main() {
|
||||||
words := strings.SplitN(line.Line, " ", 1)
|
words := strings.SplitN(line.Line, " ", 1)
|
||||||
|
|
||||||
if id, err := strconv.Atoi(words[0]); err == nil {
|
if id, err := strconv.Atoi(words[0]); err == nil {
|
||||||
found := false
|
contact := c.Contacts.ById(int32(id))
|
||||||
for _, contact := range c.Contacts {
|
if contact != nil {
|
||||||
if int(contact.Id) == id {
|
|
||||||
c.SetCurrentContact(contact)
|
c.SetCurrentContact(contact)
|
||||||
found = true
|
} else {
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
fmt.Printf("no contact %d\n", id)
|
fmt.Printf("no contact %d\n", id)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
@ -76,8 +70,8 @@ func main() {
|
||||||
_, err := c.Backend.SendMessage(context.Background(), &rpc.Message{
|
_, err := c.Backend.SendMessage(context.Background(), &rpc.Message{
|
||||||
Sender: &rpc.Entity{IsSelf: true},
|
Sender: &rpc.Entity{IsSelf: true},
|
||||||
Recipient: &rpc.Entity{
|
Recipient: &rpc.Entity{
|
||||||
ContactId: c.CurrentContact.Id,
|
ContactId: c.CurrentContact.Data.Id,
|
||||||
Address: c.CurrentContact.Address,
|
Address: c.CurrentContact.Data.Address,
|
||||||
},
|
},
|
||||||
Text: line.Line,
|
Text: line.Line,
|
||||||
})
|
})
|
||||||
|
@ -116,9 +110,9 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "contacts":
|
case "contacts":
|
||||||
byStatus := make(map[rpc.Contact_Status][]*rpc.Contact)
|
byStatus := make(map[rpc.Contact_Status][]*Contact)
|
||||||
for _, contact := range c.Contacts {
|
for _, contact := range c.Contacts.Contacts {
|
||||||
byStatus[contact.Status] = append(byStatus[contact.Status], contact)
|
byStatus[contact.Data.Status] = append(byStatus[contact.Data.Status], contact)
|
||||||
}
|
}
|
||||||
|
|
||||||
order := []rpc.Contact_Status{rpc.Contact_ONLINE, rpc.Contact_UNKNOWN, rpc.Contact_OFFLINE, rpc.Contact_REQUEST, rpc.Contact_REJECTED}
|
order := []rpc.Contact_Status{rpc.Contact_ONLINE, rpc.Contact_UNKNOWN, rpc.Contact_OFFLINE, rpc.Contact_REQUEST, rpc.Contact_REJECTED}
|
||||||
|
@ -129,7 +123,7 @@ func main() {
|
||||||
}
|
}
|
||||||
fmt.Printf(". %s\n", strings.ToLower(status.String()))
|
fmt.Printf(". %s\n", strings.ToLower(status.String()))
|
||||||
for _, contact := range contacts {
|
for _, contact := range contacts {
|
||||||
fmt.Printf("... [%d] %s\n", contact.Id, contact.Nickname)
|
fmt.Printf("... [%d] %s\n", contact.Data.Id, contact.Data.Nickname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,13 +18,14 @@ type Client struct {
|
||||||
|
|
||||||
// XXX threadsafety
|
// XXX threadsafety
|
||||||
NetworkStatus ricochet.NetworkStatus
|
NetworkStatus ricochet.NetworkStatus
|
||||||
Contacts []*ricochet.Contact
|
Contacts *ContactList
|
||||||
|
CurrentContact *Contact
|
||||||
CurrentContact *ricochet.Contact
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX need to handle backend connection loss/reconnection..
|
// XXX need to handle backend connection loss/reconnection..
|
||||||
func (c *Client) Initialize() error {
|
func (c *Client) Initialize() error {
|
||||||
|
c.Contacts = NewContactList()
|
||||||
|
|
||||||
// Query server status and version
|
// Query server status and version
|
||||||
status, err := c.Backend.GetServerStatus(context.Background(), &ricochet.ServerStatusRequest{
|
status, err := c.Backend.GetServerStatus(context.Background(), &ricochet.ServerStatusRequest{
|
||||||
RpcVersion: 1,
|
RpcVersion: 1,
|
||||||
|
@ -53,14 +54,14 @@ func (c *Client) Initialize() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetCurrentContact(contact *ricochet.Contact) {
|
func (c *Client) SetCurrentContact(contact *Contact) {
|
||||||
c.CurrentContact = contact
|
c.CurrentContact = contact
|
||||||
if c.CurrentContact != nil {
|
if c.CurrentContact != nil {
|
||||||
config := *c.Input.Config
|
config := *c.Input.Config
|
||||||
config.Prompt = fmt.Sprintf("%s > ", c.CurrentContact.Nickname)
|
config.Prompt = fmt.Sprintf("%s > ", c.CurrentContact.Data.Nickname)
|
||||||
config.UniqueEditLine = true
|
config.UniqueEditLine = true
|
||||||
c.Input.SetConfig(&config)
|
c.Input.SetConfig(&config)
|
||||||
fmt.Printf("--- %s (%s) ---\n", c.CurrentContact.Nickname, strings.ToLower(c.CurrentContact.Status.String()))
|
fmt.Printf("--- %s (%s) ---\n", c.CurrentContact.Data.Nickname, strings.ToLower(c.CurrentContact.Data.Status.String()))
|
||||||
} else {
|
} else {
|
||||||
config := *c.Input.Config
|
config := *c.Input.Config
|
||||||
config.Prompt = "> "
|
config.Prompt = "> "
|
||||||
|
@ -118,7 +119,7 @@ func (c *Client) monitorContacts() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if contact := event.GetContact(); contact != nil {
|
if contact := event.GetContact(); contact != nil {
|
||||||
c.Contacts = append(c.Contacts, contact)
|
c.Contacts.Populate(contact)
|
||||||
} else if request := event.GetRequest(); request != nil {
|
} else if request := event.GetRequest(); request != nil {
|
||||||
// XXX handle requests
|
// XXX handle requests
|
||||||
log.Printf("XXX contact requests not supported")
|
log.Printf("XXX contact requests not supported")
|
||||||
|
@ -127,7 +128,7 @@ func (c *Client) monitorContacts() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Loaded %d contacts", len(c.Contacts))
|
log.Printf("Loaded %d contacts", len(c.Contacts.Contacts))
|
||||||
|
|
||||||
for {
|
for {
|
||||||
event, err := stream.Recv()
|
event, err := stream.Recv()
|
||||||
|
@ -137,66 +138,39 @@ func (c *Client) monitorContacts() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
contact := event.GetContact()
|
data := event.GetContact()
|
||||||
|
|
||||||
switch event.Type {
|
switch event.Type {
|
||||||
case ricochet.ContactEvent_ADD:
|
case ricochet.ContactEvent_ADD:
|
||||||
if contact == nil {
|
if data == nil {
|
||||||
log.Printf("Ignoring contact add event with null contact")
|
log.Printf("Ignoring contact add event with null data")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.Contacts = append(c.Contacts, contact)
|
|
||||||
log.Printf("new contact: %v", contact)
|
c.Contacts.Added(data)
|
||||||
|
|
||||||
case ricochet.ContactEvent_UPDATE:
|
case ricochet.ContactEvent_UPDATE:
|
||||||
if contact == nil {
|
if data == nil {
|
||||||
log.Printf("Ignoring contact update event with null contact")
|
log.Printf("Ignoring contact update event with null data")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var found bool
|
contact := c.Contacts.ByIdAndAddress(data.Id, data.Address)
|
||||||
for i, match := range c.Contacts {
|
if contact == nil {
|
||||||
if match.Id == contact.Id && match.Address == contact.Address {
|
log.Printf("Ignoring contact update event for unknown contact: %v", data)
|
||||||
contacts := append(c.Contacts[0:i], contact)
|
|
||||||
contacts = append(contacts, c.Contacts[i+1:]...)
|
|
||||||
c.Contacts = contacts
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
log.Printf("Ignoring contact update event for unknown contact: %v", contact)
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("updated contact: %v", contact)
|
contact.Updated(data)
|
||||||
}
|
|
||||||
|
|
||||||
if c.CurrentContact != nil && c.CurrentContact.Id == contact.Id {
|
|
||||||
c.SetCurrentContact(contact)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case ricochet.ContactEvent_DELETE:
|
case ricochet.ContactEvent_DELETE:
|
||||||
if contact == nil {
|
if data == nil {
|
||||||
log.Printf("Ignoring contact delete event with null contact")
|
log.Printf("Ignoring contact delete event with null data")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var found bool
|
contact, _ := c.Contacts.Deleted(data)
|
||||||
for i, match := range c.Contacts {
|
|
||||||
if match.Id == contact.Id && match.Address == contact.Address {
|
|
||||||
c.Contacts = append(c.Contacts[0:i], c.Contacts[i+1:]...)
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
if c.CurrentContact == contact {
|
||||||
log.Printf("Ignoring contact delete event for unknown contact: %v", contact)
|
|
||||||
} else {
|
|
||||||
log.Printf("deleted contact: %v", contact)
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.CurrentContact != nil && c.CurrentContact.Id == contact.Id {
|
|
||||||
c.SetCurrentContact(nil)
|
c.SetCurrentContact(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,22 +210,14 @@ func (c *Client) monitorConversations() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var remoteContact *ricochet.Contact
|
|
||||||
var remoteEntity *ricochet.Entity
|
var remoteEntity *ricochet.Entity
|
||||||
|
|
||||||
if !message.Sender.IsSelf {
|
if !message.Sender.IsSelf {
|
||||||
remoteEntity = message.Sender
|
remoteEntity = message.Sender
|
||||||
} else {
|
} else {
|
||||||
remoteEntity = message.Recipient
|
remoteEntity = message.Recipient
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, contact := range c.Contacts {
|
remoteContact := c.Contacts.ByIdAndAddress(remoteEntity.ContactId, remoteEntity.Address)
|
||||||
if remoteEntity.ContactId == contact.Id && remoteEntity.Address == contact.Address {
|
|
||||||
remoteContact = contact
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if remoteContact == nil {
|
if remoteContact == nil {
|
||||||
log.Printf("Ignoring conversation event with unknown contact: %v", event)
|
log.Printf("Ignoring conversation event with unknown contact: %v", event)
|
||||||
continue
|
continue
|
||||||
|
@ -260,12 +226,12 @@ func (c *Client) monitorConversations() {
|
||||||
if remoteContact == c.CurrentContact {
|
if remoteContact == c.CurrentContact {
|
||||||
// XXX so unsafe
|
// XXX so unsafe
|
||||||
if message.Sender.IsSelf {
|
if message.Sender.IsSelf {
|
||||||
fmt.Fprintf(c.Input.Stdout(), "\r%s > %s\n", remoteContact.Nickname, message.Text)
|
fmt.Fprintf(c.Input.Stdout(), "\r%s > %s\n", remoteContact.Data.Nickname, message.Text)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(c.Input.Stdout(), "\r%s < %s\n", remoteContact.Nickname, message.Text)
|
fmt.Fprintf(c.Input.Stdout(), "\r%s < %s\n", remoteContact.Data.Nickname, message.Text)
|
||||||
}
|
}
|
||||||
} else if !message.Sender.IsSelf {
|
} else if !message.Sender.IsSelf {
|
||||||
fmt.Fprintf(c.Input.Stdout(), "\r---- %s < %s\n", remoteContact.Nickname, message.Text)
|
fmt.Fprintf(c.Input.Stdout(), "\r---- %s < %s\n", remoteContact.Data.Nickname, message.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !message.Sender.IsSelf {
|
if !message.Sender.IsSelf {
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/special/notricochet/rpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ContactList struct {
|
||||||
|
Contacts map[int32]*Contact
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewContactList() *ContactList {
|
||||||
|
return &ContactList{
|
||||||
|
Contacts: make(map[int32]*Contact),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *ContactList) Populate(data *ricochet.Contact) error {
|
||||||
|
if cl.Contacts[data.Id] != nil {
|
||||||
|
return fmt.Errorf("Duplicate contact ID %d in populate", data.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
cl.Contacts[data.Id] = initContact(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *ContactList) Added(data *ricochet.Contact) (*Contact, error) {
|
||||||
|
if cl.Contacts[data.Id] != nil {
|
||||||
|
return nil, fmt.Errorf("Duplicate contact ID %d in add", data.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
contact := initContact(data)
|
||||||
|
cl.Contacts[data.Id] = contact
|
||||||
|
return contact, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *ContactList) Deleted(data *ricochet.Contact) (*Contact, error) {
|
||||||
|
contact := cl.Contacts[data.Id]
|
||||||
|
if contact == nil {
|
||||||
|
return nil, fmt.Errorf("Contact ID %d does not exist in delete", data.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if contact.Data.Address != data.Address {
|
||||||
|
return nil, fmt.Errorf("Contact ID %d does not match address in delete (expected %s, received %s)", data.Id, contact.Data.Address, data.Address)
|
||||||
|
}
|
||||||
|
|
||||||
|
contact.Deleted()
|
||||||
|
delete(cl.Contacts, data.Id)
|
||||||
|
return contact, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *ContactList) ById(id int32) *Contact {
|
||||||
|
return cl.Contacts[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *ContactList) ByIdAndAddress(id int32, address string) *Contact {
|
||||||
|
contact := cl.Contacts[id]
|
||||||
|
if contact != nil && contact.Data.Address == address {
|
||||||
|
return contact
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Contact struct {
|
||||||
|
Data *ricochet.Contact
|
||||||
|
}
|
||||||
|
|
||||||
|
func initContact(data *ricochet.Contact) *Contact {
|
||||||
|
return &Contact{
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Contact) Updated(newData *ricochet.Contact) error {
|
||||||
|
if newData.Id != c.Data.Id || newData.Address != c.Data.Address {
|
||||||
|
return errors.New("Contact ID and address are immutable")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Data = newData
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Contact) Deleted() {
|
||||||
|
c.Data = &ricochet.Contact{
|
||||||
|
Id: c.Data.Id,
|
||||||
|
Address: c.Data.Address,
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue