ricochet-go/ricochet-cli/contact.go

98 lines
2.1 KiB
Go
Raw Normal View History

package main
import (
"errors"
"fmt"
2016-10-17 04:26:35 +00:00
"github.com/ricochet-im/ricochet-go/rpc"
)
type ContactList struct {
Client *Client
Contacts map[int32]*Contact
}
func NewContactList(client *Client) *ContactList {
return &ContactList{
Client: client,
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(cl.Client, 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(cl.Client, 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
Conversation *Conversation
}
func initContact(client *Client, data *ricochet.Contact) *Contact {
c := &Contact{
Data: data,
}
c.Conversation = &Conversation{
Client: client,
Contact: c,
}
return c
}
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,
}
}