fix: code review

This commit is contained in:
Richard Ramos 2022-02-10 11:15:27 -04:00
parent 1bffd2e64d
commit 41d523f205
6 changed files with 158 additions and 63 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/status-im/status-go/nodecfg"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/ext"
"github.com/status-im/status-go/services/personal"
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/signal"
@ -1144,7 +1145,7 @@ func (b *GethStatusBackend) GetActiveAccount() (*multiaccounts.Account, error) {
return b.account, nil
}
func (b *GethStatusBackend) injectAccountsIntoServices() error {
func (b *GethStatusBackend) injectAccountsIntoWakuService(w types.WakuKeyManager, st *ext.Service) error {
chatAccount, err := b.accountManager.SelectedChatAccount()
if err != nil {
return err
@ -1157,17 +1158,13 @@ func (b *GethStatusBackend) injectAccountsIntoServices() error {
return err
}
wakuService := b.statusNode.WakuService()
if wakuService != nil {
if err := wakuService.DeleteKeyPairs(); err != nil { // err is not possible; method return value is incorrect
if err := w.DeleteKeyPairs(); err != nil { // err is not possible; method return value is incorrect
return err
}
b.selectedAccountKeyID, err = wakuService.AddKeyPair(identity)
b.selectedAccountKeyID, err = w.AddKeyPair(identity)
if err != nil {
return ErrWakuIdentityInjectionFailure
}
st := b.statusNode.WakuExtService()
if st != nil {
if err := st.InitProtocol(b.statusNode.GethNode().Config().Name, identity, b.appDB, b.multiaccountsDB, acc, logutils.ZapLogger()); err != nil {
@ -1179,24 +1176,30 @@ func (b *GethStatusBackend) injectAccountsIntoServices() error {
messenger := st.Messenger()
// Init public status api
b.statusNode.StatusPublicService().Init(messenger)
// Init chat service
b.statusNode.ChatService().Init(messenger)
}
return nil
}
wakuV2Service := b.statusNode.WakuV2Service()
func (b *GethStatusBackend) injectAccountsIntoServices() error {
if b.statusNode.WakuService() != nil {
return b.injectAccountsIntoWakuService(b.statusNode.WakuService(), func() *ext.Service {
if b.statusNode.WakuExtService() == nil {
return nil
}
return b.statusNode.WakuExtService().Service
}())
}
if wakuV2Service != nil {
if err := wakuV2Service.DeleteKeyPairs(); err != nil { // err is not possible; method return value is incorrect
return err
}
b.selectedAccountKeyID, err = wakuV2Service.AddKeyPair(identity)
if err != nil {
return ErrWakuIdentityInjectionFailure
}
st := b.statusNode.WakuV2ExtService()
if err := st.InitProtocol(b.statusNode.GethNode().Config().Name, identity, b.appDB, b.multiaccountsDB, acc, logutils.ZapLogger()); err != nil {
return err
if b.statusNode.WakuV2Service() != nil {
return b.injectAccountsIntoWakuService(b.statusNode.WakuV2Service(), func() *ext.Service {
if b.statusNode.WakuV2ExtService() == nil {
return nil
}
return b.statusNode.WakuV2ExtService().Service
}())
}
return nil

View File

@ -45,6 +45,21 @@ func (u *ConnStatusSubscription) Unsubscribe() {
u.active = false
}
type WakuKeyManager interface {
// GetPrivateKey retrieves the private key of the specified identity.
GetPrivateKey(id string) (*ecdsa.PrivateKey, error)
// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
AddKeyPair(key *ecdsa.PrivateKey) (string, error)
// DeleteKeyPair deletes the key with the specified ID if it exists.
DeleteKeyPair(keyID string) bool
// DeleteKeyPairs deletes all the keys
DeleteKeyPairs() error
AddSymKeyDirect(key []byte) (string, error)
AddSymKeyFromPassword(password string) (string, error)
DeleteSymKey(id string) bool
GetSymKey(id string) ([]byte, error)
}
// Whisper represents a dark communication interface through the Ethereum
// network, using its very own P2P communication layer.
type Waku interface {

View File

@ -32,6 +32,7 @@ import (
accountssvc "github.com/status-im/status-go/services/accounts"
appmetricsservice "github.com/status-im/status-go/services/appmetrics"
"github.com/status-im/status-go/services/browsers"
"github.com/status-im/status-go/services/chat"
"github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/services/gif"
localnotifications "github.com/status-im/status-go/services/local-notifications"
@ -115,6 +116,7 @@ type StatusNode struct {
ensSrvc *ens.Service
gifSrvc *gif.Service
stickersSrvc *stickers.Service
chatSrvc *chat.Service
}
// New makes new instance of StatusNode.

View File

@ -24,6 +24,7 @@ import (
accountssvc "github.com/status-im/status-go/services/accounts"
appmetricsservice "github.com/status-im/status-go/services/appmetrics"
"github.com/status-im/status-go/services/browsers"
"github.com/status-im/status-go/services/chat"
"github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/services/ext"
"github.com/status-im/status-go/services/gif"
@ -75,6 +76,7 @@ func (b *StatusNode) initServices(config *params.NodeConfig) error {
services = appendIf(config.MailserversConfig.Enabled, services, b.mailserversService())
services = appendIf(config.Web3ProviderConfig.Enabled, services, b.providerService())
services = append(services, b.gifService())
services = append(services, b.ChatService())
if config.WakuConfig.Enabled {
wakuService, err := b.wakuService(&config.WakuConfig, &config.ClusterConfig)
@ -386,6 +388,13 @@ func (b *StatusNode) gifService() *gif.Service {
return b.gifSrvc
}
func (b *StatusNode) ChatService() *chat.Service {
if b.chatSrvc == nil {
b.chatSrvc = chat.NewService(b.appDB)
}
return b.chatSrvc
}
func (b *StatusNode) permissionsService() *permissions.Service {
if b.permissionsSrvc == nil {
b.permissionsSrvc = permissions.NewService(permissions.NewDB(b.appDB))

View File

@ -1,4 +1,4 @@
package ext
package chat
import (
"context"
@ -24,12 +24,12 @@ type ChannelGroupType string
const Personal ChannelGroupType = "personal"
const Community ChannelGroupType = "community"
type ChatPinnedMessages struct {
type PinnedMessages struct {
Cursor string
PinnedMessages []*common.PinnedMessage
}
type ChatMember struct {
type Member struct {
// Community Roles
Roles []protobuf.CommunityMember_Roles `json:"roles,omitempty"`
// Admin indicates if the member is an admin of the group chat
@ -53,7 +53,7 @@ type Chat struct {
UnviewedMessagesCount uint `json:"unviewedMessagesCount"`
UnviewedMentionsCount uint `json:"unviewedMentionsCount"`
LastMessage *common.Message `json:"lastMessage"`
Members map[string]ChatMember `json:"members,omitempty"`
Members map[string]Member `json:"members,omitempty"`
MembershipUpdates []v1protocol.MembershipUpdateEvent `json:"membershipUpdateEvents"`
Alias string `json:"alias,omitempty"`
Identicon string `json:"identicon"`
@ -69,7 +69,7 @@ type Chat struct {
SyncedTo uint32 `json:"syncedTo,omitempty"`
SyncedFrom uint32 `json:"syncedFrom,omitempty"`
Highlight bool `json:"highlight,omitempty"`
PinnedMessages *ChatPinnedMessages `json:"pinnedMessages,omitempty"`
PinnedMessages *PinnedMessages `json:"pinnedMessages,omitempty"`
CanPost bool `json:"canPost"`
}
@ -83,15 +83,25 @@ type ChannelGroup struct {
EnsName string `json:"ensName"`
}
func (api *PublicAPI) GetChats(parent context.Context) (map[string]ChannelGroup, error) {
joinedCommunities, err := api.service.messenger.JoinedCommunities()
func NewAPI(service *Service) *API {
return &API{
s: service,
}
}
type API struct {
s *Service
}
func (api *API) GetChats(parent context.Context) (map[string]ChannelGroup, error) {
joinedCommunities, err := api.s.messenger.JoinedCommunities()
if err != nil {
return nil, err
}
channels := api.service.messenger.Chats()
channels := api.s.messenger.Chats()
pubKey, err := api.service.accountsDB.GetPublicKey()
pubKey, err := api.s.accountsDB.GetPublicKey()
if err != nil {
return nil, err
}
@ -113,7 +123,7 @@ func (api *PublicAPI) GetChats(parent context.Context) (map[string]ChannelGroup,
continue
}
pinnedMessages, cursor, err := api.service.messenger.PinnedMessageByChatID(chat.ID, "", -1)
pinnedMessages, cursor, err := api.s.messenger.PinnedMessageByChatID(chat.ID, "", -1)
if err != nil {
return nil, err
}
@ -149,7 +159,7 @@ func (api *PublicAPI) GetChats(parent context.Context) (map[string]ChannelGroup,
for _, chat := range channels {
if chat.CommunityID == community.IDString() {
pinnedMessages, cursor, err := api.service.messenger.PinnedMessageByChatID(chat.ID, "", -1)
pinnedMessages, cursor, err := api.s.messenger.PinnedMessageByChatID(chat.ID, "", -1)
if err != nil {
return nil, err
}
@ -169,23 +179,27 @@ func (api *PublicAPI) GetChats(parent context.Context) (map[string]ChannelGroup,
return result, nil
}
func (api *PublicAPI) GetChat(parent context.Context, communityID types.HexBytes, chatID string) (*Chat, error) {
func (api *API) GetChat(parent context.Context, communityID types.HexBytes, chatID string) (*Chat, error) {
fullChatID := chatID
pubKey, err := api.s.accountsDB.GetPublicKey()
if err != nil {
return nil, err
}
if string(communityID.Bytes()) == pubKey { // Obtaining chats from personal
communityID = []byte{}
}
if len(communityID) != 0 {
fullChatID = string(communityID.Bytes()) + chatID
}
messengerChat := api.service.messenger.Chat(fullChatID)
messengerChat := api.s.messenger.Chat(fullChatID)
if messengerChat == nil {
return nil, ErrChatNotFound
}
pubKey, err := api.service.accountsDB.GetPublicKey()
if err != nil {
return nil, err
}
var community *communities.Community
if messengerChat.CommunityID != "" {
communityID, err := hexutil.Decode(messengerChat.CommunityID)
@ -193,13 +207,13 @@ func (api *PublicAPI) GetChat(parent context.Context, communityID types.HexBytes
return nil, err
}
community, err = api.service.messenger.GetCommunityByID(communityID)
community, err = api.s.messenger.GetCommunityByID(communityID)
if err != nil {
return nil, err
}
}
pinnedMessages, cursor, err := api.service.messenger.PinnedMessageByChatID(messengerChat.ID, "", -1)
pinnedMessages, cursor, err := api.s.messenger.PinnedMessageByChatID(messengerChat.ID, "", -1)
if err != nil {
return nil, err
}
@ -228,7 +242,7 @@ func toAPIChat(protocolChat *protocol.Chat, community *communities.Community, pu
UnviewedMessagesCount: protocolChat.UnviewedMessagesCount,
UnviewedMentionsCount: protocolChat.UnviewedMentionsCount,
LastMessage: protocolChat.LastMessage,
Members: make(map[string]ChatMember),
Members: make(map[string]Member),
MembershipUpdates: protocolChat.MembershipUpdates,
Alias: protocolChat.Alias,
Identicon: protocolChat.Identicon,
@ -249,7 +263,7 @@ func toAPIChat(protocolChat *protocol.Chat, community *communities.Community, pu
}
if len(pinnedMessages) != 0 {
chat.PinnedMessages = &ChatPinnedMessages{
chat.PinnedMessages = &PinnedMessages{
Cursor: cursor,
PinnedMessages: pinnedMessages,
}
@ -268,7 +282,7 @@ func toAPIChat(protocolChat *protocol.Chat, community *communities.Community, pu
func (chat *Chat) setChatMembers(sourceChat *protocol.Chat, community *communities.Community, userPubKey string) {
if sourceChat.ChatType == protocol.ChatTypePrivateGroupChat && len(sourceChat.Members) > 0 {
for _, m := range sourceChat.Members {
chat.Members[m.ID] = ChatMember{
chat.Members[m.ID] = Member{
Admin: m.Admin,
Joined: m.Joined,
}
@ -277,10 +291,10 @@ func (chat *Chat) setChatMembers(sourceChat *protocol.Chat, community *communiti
}
if sourceChat.ChatType == protocol.ChatTypeOneToOne {
chat.Members[sourceChat.ID] = ChatMember{
chat.Members[sourceChat.ID] = Member{
Joined: true,
}
chat.Members[userPubKey] = ChatMember{
chat.Members[userPubKey] = Member{
Joined: true,
}
return
@ -289,12 +303,12 @@ func (chat *Chat) setChatMembers(sourceChat *protocol.Chat, community *communiti
if community != nil {
for pubKey, m := range community.Description().Members {
if pubKey == userPubKey {
chat.Members[pubKey] = ChatMember{
chat.Members[pubKey] = Member{
Roles: m.Roles,
Joined: true,
}
} else {
chat.Members[pubKey] = ChatMember{
chat.Members[pubKey] = Member{
Roles: m.Roles,
Joined: community.Joined(),
}
@ -305,6 +319,10 @@ func (chat *Chat) setChatMembers(sourceChat *protocol.Chat, community *communiti
}
func (chat *Chat) populateCommunityFields(community *communities.Community) error {
if community == nil {
return nil
}
commChat, exists := community.Chats()[chat.ID]
if !exists {
return ErrChatNotFound

48
services/chat/service.go Normal file
View File

@ -0,0 +1,48 @@
package chat
import (
"database/sql"
"github.com/ethereum/go-ethereum/p2p"
gethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol"
)
func NewService(appDB *sql.DB) *Service {
return &Service{
accountsDB: accounts.NewDB(appDB),
}
}
type Service struct {
accountsDB *accounts.Database
messenger *protocol.Messenger
}
func (s *Service) Init(messenger *protocol.Messenger) {
s.messenger = messenger
}
func (s *Service) Start() error {
return nil
}
func (s *Service) Stop() error {
return nil
}
func (s *Service) APIs() []gethrpc.API {
return []gethrpc.API{
{
Namespace: "chat",
Version: "0.1.0",
Service: NewAPI(s),
},
}
}
func (s *Service) Protocols() []p2p.Protocol {
return nil
}