Leave import community data

This commit is contained in:
Andrea Maria Piana 2020-12-17 15:36:09 +01:00
parent 5d929c3584
commit a0d5f66c8f
6 changed files with 85 additions and 65 deletions

View File

@ -1 +1 @@
0.66.2
0.67.0

View File

@ -130,7 +130,7 @@ func _1605007189_identity_imagesDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "1605007189_identity_images.down.sql", size: 29, mode: os.FileMode(0644), modTime: time.Unix(1608211507, 0)}
info := bindataFileInfo{name: "1605007189_identity_images.down.sql", size: 29, mode: os.FileMode(0644), modTime: time.Unix(1608278926, 0)}
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x2f, 0xcf, 0xa7, 0xae, 0xd5, 0x4f, 0xcd, 0x14, 0x63, 0x9, 0xbe, 0x39, 0x49, 0x18, 0x96, 0xb2, 0xa3, 0x8, 0x7d, 0x41, 0xdb, 0x50, 0x5d, 0xf5, 0x4d, 0xa2, 0xd, 0x8f, 0x57, 0x79, 0x77, 0x67}}
return a, nil
}
@ -150,7 +150,7 @@ func _1605007189_identity_imagesUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "1605007189_identity_images.up.sql", size: 268, mode: os.FileMode(0644), modTime: time.Unix(1608211507, 0)}
info := bindataFileInfo{name: "1605007189_identity_images.up.sql", size: 268, mode: os.FileMode(0644), modTime: time.Unix(1608278926, 0)}
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x50, 0xb6, 0xc1, 0x5c, 0x76, 0x72, 0x6b, 0x22, 0x34, 0xdc, 0x96, 0xdc, 0x2b, 0xfd, 0x2d, 0xbe, 0xcc, 0x1e, 0xd4, 0x5, 0x93, 0xd, 0xc2, 0x51, 0xf3, 0x1a, 0xef, 0x2b, 0x26, 0xa4, 0xeb, 0x65}}
return a, nil
}
@ -170,7 +170,7 @@ func _1606224181_drop_photo_path_from_accountsDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "1606224181_drop_photo_path_from_accounts.down.sql", size: 892, mode: os.FileMode(0644), modTime: time.Unix(1608211507, 0)}
info := bindataFileInfo{name: "1606224181_drop_photo_path_from_accounts.down.sql", size: 892, mode: os.FileMode(0644), modTime: time.Unix(1608278926, 0)}
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x90, 0x24, 0x17, 0x7, 0x80, 0x93, 0x6f, 0x8d, 0x5d, 0xaa, 0x8c, 0x79, 0x15, 0x5d, 0xb3, 0x19, 0xd7, 0xd8, 0x39, 0xf9, 0x3a, 0x63, 0x8f, 0x81, 0x15, 0xb6, 0xd6, 0x9a, 0x37, 0xa8, 0x8e, 0x9b}}
return a, nil
}
@ -190,7 +190,7 @@ func _1606224181_drop_photo_path_from_accountsUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "1606224181_drop_photo_path_from_accounts.up.sql", size: 866, mode: os.FileMode(0644), modTime: time.Unix(1608211507, 0)}
info := bindataFileInfo{name: "1606224181_drop_photo_path_from_accounts.up.sql", size: 866, mode: os.FileMode(0644), modTime: time.Unix(1608278926, 0)}
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xff, 0x4c, 0x97, 0xee, 0xef, 0x82, 0xb8, 0x6c, 0x71, 0xbb, 0x50, 0x7b, 0xe6, 0xd9, 0x22, 0x31, 0x7c, 0x1a, 0xfe, 0x91, 0x28, 0xf6, 0x6, 0x36, 0xe, 0xb1, 0xf1, 0xc8, 0x25, 0xac, 0x7e, 0xd6}}
return a, nil
}

View File

@ -99,92 +99,103 @@ func (m *Manager) CreateCommunity(description *protobuf.CommunityDescription) (*
Joined: true,
CommunityDescription: description,
}
org, err := New(config)
community, err := New(config)
if err != nil {
return nil, err
}
err = m.persistence.SaveCommunity(org)
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, err
}
m.publish(&Subscription{Community: org})
m.publish(&Subscription{Community: community})
return org, nil
return community, nil
}
func (m *Manager) ExportCommunity(idString string) (*ecdsa.PrivateKey, error) {
org, err := m.GetByIDString(idString)
community, err := m.GetByIDString(idString)
if err != nil {
return nil, err
}
if org.config.PrivateKey == nil {
if community.config.PrivateKey == nil {
return nil, errors.New("not an admin")
}
return org.config.PrivateKey, nil
return community.config.PrivateKey, nil
}
func (m *Manager) ImportCommunity(key *ecdsa.PrivateKey) (*Community, error) {
description := &protobuf.CommunityDescription{
Permissions: &protobuf.CommunityPermissions{},
}
communityID := crypto.CompressPubkey(&key.PublicKey)
config := Config{
ID: &key.PublicKey,
PrivateKey: key,
Logger: m.logger,
Joined: true,
CommunityDescription: description,
}
org, err := New(config)
community, err := m.persistence.GetByID(communityID)
if err != nil {
return nil, err
}
err = m.persistence.SaveCommunity(org)
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)
if err != nil {
return nil, err
}
return org, nil
return community, nil
}
func (m *Manager) CreateChat(idString string, chat *protobuf.CommunityChat) (*Community, *CommunityChanges, error) {
org, err := m.GetByIDString(idString)
community, err := m.GetByIDString(idString)
if err != nil {
return nil, nil, err
}
if org == nil {
if community == nil {
return nil, nil, ErrOrgNotFound
}
chatID := uuid.New().String()
changes, err := org.CreateChat(chatID, chat)
changes, err := community.CreateChat(chatID, chat)
if err != nil {
return nil, nil, err
}
err = m.persistence.SaveCommunity(org)
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, nil, err
}
// Advertise changes
m.publish(&Subscription{Community: org})
m.publish(&Subscription{Community: community})
return org, changes, nil
return community, changes, nil
}
func (m *Manager) HandleCommunityDescriptionMessage(signer *ecdsa.PublicKey, description *protobuf.CommunityDescription, payload []byte) (*Community, error) {
id := crypto.CompressPubkey(signer)
org, err := m.persistence.GetByID(id)
community, err := m.persistence.GetByID(id)
if err != nil {
return nil, err
}
if org == nil {
if community == nil {
config := Config{
CommunityDescription: description,
Logger: m.logger,
@ -192,36 +203,36 @@ func (m *Manager) HandleCommunityDescriptionMessage(signer *ecdsa.PublicKey, des
ID: signer,
}
org, err = New(config)
community, err = New(config)
if err != nil {
return nil, err
}
}
_, err = org.HandleCommunityDescription(signer, description, payload)
_, err = community.HandleCommunityDescription(signer, description, payload)
if err != nil {
return nil, err
}
err = m.persistence.SaveCommunity(org)
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, err
}
return org, nil
return community, nil
}
func (m *Manager) HandleCommunityInvitation(signer *ecdsa.PublicKey, invitation *protobuf.CommunityInvitation, payload []byte) (*Community, error) {
m.logger.Debug("Handling wrapped community description message")
org, err := m.HandleWrappedCommunityDescriptionMessage(payload)
community, err := m.HandleWrappedCommunityDescriptionMessage(payload)
if err != nil {
return nil, err
}
// Save grant
return org, nil
return community, nil
}
func (m *Manager) HandleWrappedCommunityDescriptionMessage(payload []byte) (*Community, error) {
@ -251,59 +262,59 @@ func (m *Manager) HandleWrappedCommunityDescriptionMessage(payload []byte) (*Com
}
func (m *Manager) JoinCommunity(idString string) (*Community, error) {
org, err := m.GetByIDString(idString)
community, err := m.GetByIDString(idString)
if err != nil {
return nil, err
}
if org == nil {
if community == nil {
return nil, ErrOrgNotFound
}
org.Join()
err = m.persistence.SaveCommunity(org)
community.Join()
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, err
}
return org, nil
return community, nil
}
func (m *Manager) LeaveCommunity(idString string) (*Community, error) {
org, err := m.GetByIDString(idString)
community, err := m.GetByIDString(idString)
if err != nil {
return nil, err
}
if org == nil {
if community == nil {
return nil, ErrOrgNotFound
}
org.Leave()
err = m.persistence.SaveCommunity(org)
community.Leave()
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, err
}
return org, nil
return community, nil
}
func (m *Manager) InviteUserToCommunity(idString string, pk *ecdsa.PublicKey) (*Community, error) {
org, err := m.GetByIDString(idString)
community, err := m.GetByIDString(idString)
if err != nil {
return nil, err
}
if org == nil {
if community == nil {
return nil, ErrOrgNotFound
}
invitation, err := org.InviteUserToOrg(pk)
invitation, err := community.InviteUserToOrg(pk)
if err != nil {
return nil, err
}
err = m.persistence.SaveCommunity(org)
err = m.persistence.SaveCommunity(community)
if err != nil {
return nil, err
}
m.publish(&Subscription{Community: org, Invitation: invitation})
m.publish(&Subscription{Community: community, Invitation: invitation})
return org, nil
return community, nil
}
func (m *Manager) GetByIDString(idString string) (*Community, error) {
@ -315,12 +326,12 @@ func (m *Manager) GetByIDString(idString string) (*Community, error) {
}
func (m *Manager) CanPost(pk *ecdsa.PublicKey, orgIDString, chatID string, grant []byte) (bool, error) {
org, err := m.GetByIDString(orgIDString)
community, err := m.GetByIDString(orgIDString)
if err != nil {
return false, err
}
if org == nil {
if community == nil {
return false, nil
}
return org.CanPost(pk, chatID, grant)
return community.CanPost(pk, chatID, grant)
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -317,27 +317,33 @@ func (api *PublicAPI) SetInstallationMetadata(installationID string, data *multi
return api.service.messenger.SetInstallationMetadata(installationID, data)
}
// Communities returns a list of communities that are stored
func (api *PublicAPI) Communities(parent context.Context) ([]*communities.Community, error) {
return api.service.messenger.Communities()
}
// JoinedCommunities returns a list of communities that the user has joined
func (api *PublicAPI) JoinedCommunities(parent context.Context) ([]*communities.Community, error) {
return api.service.messenger.JoinedCommunities()
}
// JoinCommunity joins a community with the given ID
func (api *PublicAPI) JoinCommunity(parent context.Context, communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.JoinCommunity(communityID)
}
// LeaveCommunity leaves a commuity with the given ID
func (api *PublicAPI) LeaveCommunity(parent context.Context, communityID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.LeaveCommunity(communityID)
}
// CreateCommunity creates a new community with the provided description
func (api *PublicAPI) CreateCommunity(description *protobuf.CommunityDescription) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateCommunity(description)
}
// ExportCommunity exports the private key of the community with given ID
func (api *PublicAPI) ExportCommunity(id string) (string, error) {
key, err := api.service.messenger.ExportCommunity(id)
if err != nil {
@ -346,6 +352,7 @@ func (api *PublicAPI) ExportCommunity(id string) (string, error) {
return types.EncodeHex(crypto.FromECDSA(key)), nil
}
// ImportCommunity imports a community with the given private key in hex
func (api *PublicAPI) ImportCommunity(hexPrivateKey string) (*protocol.MessengerResponse, error) {
// Strip the 0x from the beginning
privateKey, err := crypto.HexToECDSA(hexPrivateKey[2:])
@ -356,10 +363,12 @@ func (api *PublicAPI) ImportCommunity(hexPrivateKey string) (*protocol.Messenger
}
// CreateCommunityChat creates a community chat in the given community
func (api *PublicAPI) CreateCommunityChat(orgID string, c *protobuf.CommunityChat) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateCommunityChat(orgID, c)
}
// InviteUserToCommunity invites the used with pk to the community with ID
func (api *PublicAPI) InviteUserToCommunity(orgID, userPublicKey string) (*protocol.MessengerResponse, error) {
return api.service.messenger.InviteUserToCommunity(orgID, userPublicKey)
}