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