2020-11-18 09:16:51 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
persistence *Persistence
|
|
|
|
subscriptions []chan *Subscription
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager(db *sql.DB, logger *zap.Logger) (*Manager, error) {
|
|
|
|
var err error
|
|
|
|
if logger == nil {
|
|
|
|
if logger, err = zap.NewDevelopment(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a logger")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Manager{
|
|
|
|
logger: logger,
|
|
|
|
persistence: &Persistence{
|
|
|
|
logger: logger,
|
|
|
|
db: db,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subscription struct {
|
|
|
|
Community *Community
|
|
|
|
Invitation *protobuf.CommunityInvitation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Subscribe() chan *Subscription {
|
|
|
|
subscription := make(chan *Subscription, 100)
|
|
|
|
m.subscriptions = append(m.subscriptions, subscription)
|
|
|
|
return subscription
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Stop() error {
|
|
|
|
for _, c := range m.subscriptions {
|
|
|
|
close(c)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) publish(subscription *Subscription) {
|
|
|
|
for _, s := range m.subscriptions {
|
|
|
|
select {
|
|
|
|
case s <- subscription:
|
|
|
|
default:
|
|
|
|
m.logger.Warn("subscription channel full, dropping message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) All() ([]*Community, error) {
|
|
|
|
return m.persistence.AllCommunities()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Joined() ([]*Community, error) {
|
|
|
|
return m.persistence.JoinedCommunities()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Created() ([]*Community, error) {
|
|
|
|
return m.persistence.CreatedCommunities()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCommunity takes a description, generates an ID for it, saves it and return it
|
|
|
|
func (m *Manager) CreateCommunity(description *protobuf.CommunityDescription) (*Community, error) {
|
|
|
|
err := ValidateCommunityDescription(description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
description.Clock = 1
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
ID: &key.PublicKey,
|
|
|
|
PrivateKey: key,
|
|
|
|
Logger: m.logger,
|
|
|
|
Joined: true,
|
|
|
|
CommunityDescription: description,
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := New(config)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
m.publish(&Subscription{Community: community})
|
2020-11-18 09:16:51 +00:00
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) ExportCommunity(idString string) (*ecdsa.PrivateKey, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.GetByIDString(idString)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
if community.config.PrivateKey == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, errors.New("not an admin")
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community.config.PrivateKey, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) ImportCommunity(key *ecdsa.PrivateKey) (*Community, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
communityID := crypto.CompressPubkey(&key.PublicKey)
|
2020-11-18 09:16:51 +00:00
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.persistence.GetByID(communityID)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
|
|
|
description := &protobuf.CommunityDescription{
|
|
|
|
Permissions: &protobuf.CommunityPermissions{},
|
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
ID: &key.PublicKey,
|
|
|
|
PrivateKey: key,
|
|
|
|
Logger: m.logger,
|
|
|
|
Joined: true,
|
|
|
|
CommunityDescription: description,
|
|
|
|
}
|
|
|
|
community, err = New(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
community.config.PrivateKey = key
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) CreateChat(idString string, chat *protobuf.CommunityChat) (*Community, *CommunityChanges, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.GetByIDString(idString)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, nil, ErrOrgNotFound
|
|
|
|
}
|
|
|
|
chatID := uuid.New().String()
|
2020-12-17 14:36:09 +00:00
|
|
|
changes, err := community.CreateChat(chatID, chat)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advertise changes
|
2020-12-17 14:36:09 +00:00
|
|
|
m.publish(&Subscription{Community: community})
|
2020-11-18 09:16:51 +00:00
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, changes, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) HandleCommunityDescriptionMessage(signer *ecdsa.PublicKey, description *protobuf.CommunityDescription, payload []byte) (*Community, error) {
|
|
|
|
id := crypto.CompressPubkey(signer)
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.persistence.GetByID(id)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
config := Config{
|
|
|
|
CommunityDescription: description,
|
|
|
|
Logger: m.logger,
|
|
|
|
MarshaledCommunityDescription: payload,
|
|
|
|
ID: signer,
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err = New(config)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
_, err = community.HandleCommunityDescription(signer, description, payload)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 11:00:13 +00:00
|
|
|
// TODO: Finish implementing this
|
2020-11-18 09:16:51 +00:00
|
|
|
func (m *Manager) HandleCommunityInvitation(signer *ecdsa.PublicKey, invitation *protobuf.CommunityInvitation, payload []byte) (*Community, error) {
|
|
|
|
m.logger.Debug("Handling wrapped community description message")
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.HandleWrappedCommunityDescriptionMessage(payload)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save grant
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) HandleWrappedCommunityDescriptionMessage(payload []byte) (*Community, error) {
|
|
|
|
m.logger.Debug("Handling wrapped community description message")
|
|
|
|
|
|
|
|
applicationMetadataMessage := &protobuf.ApplicationMetadataMessage{}
|
|
|
|
err := proto.Unmarshal(payload, applicationMetadataMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if applicationMetadataMessage.Type != protobuf.ApplicationMetadataMessage_COMMUNITY_DESCRIPTION {
|
|
|
|
return nil, ErrInvalidMessage
|
|
|
|
}
|
|
|
|
signer, err := applicationMetadataMessage.RecoverKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
description := &protobuf.CommunityDescription{}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(applicationMetadataMessage.Payload, description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.HandleCommunityDescriptionMessage(signer, description, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) JoinCommunity(idString string) (*Community, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.GetByIDString(idString)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, ErrOrgNotFound
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
community.Join()
|
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) LeaveCommunity(idString string) (*Community, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.GetByIDString(idString)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, ErrOrgNotFound
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
community.Leave()
|
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) InviteUserToCommunity(idString string, pk *ecdsa.PublicKey) (*Community, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.GetByIDString(idString)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
invitation, err := community.InviteUserToOrg(pk)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
err = m.persistence.SaveCommunity(community)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
m.publish(&Subscription{Community: community, Invitation: invitation})
|
2020-11-18 09:16:51 +00:00
|
|
|
|
2020-12-17 14:36:09 +00:00
|
|
|
return community, nil
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:10:52 +00:00
|
|
|
func (m *Manager) RemoveUserFromCommunity(idString string, pk *ecdsa.PublicKey) (*Community, error) {
|
|
|
|
community, err := m.GetByIDString(idString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if community == nil {
|
|
|
|
return nil, ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = community.RemoveUserFromOrg(pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.SaveCommunity(community)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.publish(&Subscription{Community: community})
|
|
|
|
|
|
|
|
return community, nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func (m *Manager) GetByIDString(idString string) (*Community, error) {
|
|
|
|
id, err := types.DecodeHex(idString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m.persistence.GetByID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) CanPost(pk *ecdsa.PublicKey, orgIDString, chatID string, grant []byte) (bool, error) {
|
2020-12-17 14:36:09 +00:00
|
|
|
community, err := m.GetByIDString(orgIDString)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
if community == nil {
|
2020-11-18 09:16:51 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2020-12-17 14:36:09 +00:00
|
|
|
return community.CanPost(pk, chatID, grant)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|