2016-07-01 03:18:55 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-08-30 02:46:41 +00:00
|
|
|
"fmt"
|
2016-10-17 04:26:35 +00:00
|
|
|
"github.com/ricochet-im/ricochet-go/core/utils"
|
|
|
|
"github.com/ricochet-im/ricochet-go/rpc"
|
2016-08-30 02:46:41 +00:00
|
|
|
"strconv"
|
2016-09-02 16:51:24 +00:00
|
|
|
"sync"
|
2016-07-01 03:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContactList struct {
|
2016-09-02 16:51:24 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
events *utils.Publisher
|
|
|
|
|
2016-10-16 21:50:59 +00:00
|
|
|
contacts map[int]*Contact
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func LoadContactList(core *Ricochet) (*ContactList, error) {
|
2016-09-02 16:51:24 +00:00
|
|
|
list := &ContactList{
|
|
|
|
events: utils.CreatePublisher(),
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
config := core.Config.OpenRead()
|
2016-08-30 02:46:41 +00:00
|
|
|
defer config.Close()
|
|
|
|
|
|
|
|
list.contacts = make(map[int]*Contact, len(config.Contacts))
|
|
|
|
for idStr, data := range config.Contacts {
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid contact id '%s'", idStr)
|
|
|
|
}
|
|
|
|
if _, exists := list.contacts[id]; exists {
|
|
|
|
return nil, fmt.Errorf("Duplicate contact id '%d'", id)
|
|
|
|
}
|
|
|
|
|
2016-09-20 01:53:21 +00:00
|
|
|
contact, err := ContactFromConfig(core, id, data, list.events)
|
2016-08-30 02:46:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list.contacts[id] = contact
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Requests aren't implemented
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
func (this *ContactList) EventMonitor() utils.Subscribable {
|
|
|
|
return this.events
|
|
|
|
}
|
|
|
|
|
2016-07-01 03:18:55 +00:00
|
|
|
func (this *ContactList) Contacts() []*Contact {
|
2016-09-02 16:51:24 +00:00
|
|
|
this.mutex.RLock()
|
|
|
|
defer this.mutex.RUnlock()
|
2016-07-01 03:18:55 +00:00
|
|
|
re := make([]*Contact, 0, len(this.contacts))
|
|
|
|
for _, contact := range this.contacts {
|
|
|
|
re = append(re, contact)
|
|
|
|
}
|
|
|
|
return re
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ContactList) ContactById(id int) *Contact {
|
2016-09-02 16:51:24 +00:00
|
|
|
this.mutex.RLock()
|
|
|
|
defer this.mutex.RUnlock()
|
2016-07-01 03:18:55 +00:00
|
|
|
return this.contacts[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ContactList) ContactByAddress(address string) *Contact {
|
2016-09-02 16:51:24 +00:00
|
|
|
this.mutex.RLock()
|
|
|
|
defer this.mutex.RUnlock()
|
2016-07-01 03:18:55 +00:00
|
|
|
for _, contact := range this.contacts {
|
2016-08-30 02:46:41 +00:00
|
|
|
if contact.Address() == address {
|
2016-07-01 03:18:55 +00:00
|
|
|
return contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
func (this *ContactList) AddContact(address string, name string) (*Contact, error) {
|
2016-07-01 03:18:55 +00:00
|
|
|
return nil, errors.New("Not implemented")
|
|
|
|
}
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
func (this *ContactList) RemoveContact(contact *Contact) error {
|
|
|
|
this.mutex.Lock()
|
|
|
|
defer this.mutex.Unlock()
|
|
|
|
|
|
|
|
if this.contacts[contact.Id()] != contact {
|
|
|
|
return errors.New("Not in contact list")
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Not persisting in config
|
|
|
|
// XXX This will have to do some things to the contact itself
|
|
|
|
// eventually too, such as killing connections and other resources.
|
|
|
|
delete(this.contacts, contact.Id())
|
|
|
|
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_DELETE,
|
|
|
|
Subject: &ricochet.ContactEvent_Contact{
|
|
|
|
Contact: &ricochet.Contact{
|
|
|
|
Id: int32(contact.Id()),
|
|
|
|
Address: contact.Address(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.events.Publish(event)
|
|
|
|
|
|
|
|
return nil
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|