feat_: poc to use single content-topic for all community chats

This commit is contained in:
Prem Chaitanya Prathi 2024-09-26 16:38:12 +05:30
parent a84f78f8aa
commit 4f10919946
No known key found for this signature in database
4 changed files with 36 additions and 24 deletions

View File

@ -1888,7 +1888,7 @@ func (m *Messenger) InitFilters() error {
} }
} }
filtersToInit = append(filtersToInit, transport.FiltersToInitialize{ChatID: chat.ID, PubsubTopic: community.PubsubTopic()}) filtersToInit = append(filtersToInit, transport.FiltersToInitialize{ChatID: chat.ID, PubsubTopic: community.PubsubTopic(), ContentTopicID: community.MemberUpdateChannelID()})
case ChatTypeOneToOne: case ChatTypeOneToOne:
pk, err := chat.PublicKey() pk, err := chat.PublicKey()
if err != nil { if err != nil {
@ -2208,7 +2208,9 @@ func (m *Messenger) dispatchMessage(ctx context.Context, rawMessage common.RawMe
} }
isEncrypted := isCommunityEncrypted || isChannelEncrypted isEncrypted := isCommunityEncrypted || isChannelEncrypted
if !isEncrypted { if !isEncrypted {
id, err = m.sender.SendPublic(ctx, chat.ID, rawMessage) //over-riding chatname for community messages to use memberUpdatesChannelID
//TODO: Need to update the SendPublic method to actually take-in an override id to fetch the key
id, err = m.sender.SendPublic(ctx, community.MemberUpdateChannelID(), rawMessage)
if err != nil { if err != nil {
return rawMessage, err return rawMessage, err
} }

View File

@ -951,7 +951,7 @@ func (m *Messenger) initCommunityChats(community *communities.Community) ([]*Cha
chats := CreateCommunityChats(community, m.getTimesource()) chats := CreateCommunityChats(community, m.getTimesource())
for _, chat := range chats { for _, chat := range chats {
publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: chat.ID, PubsubTopic: community.PubsubTopic()}) publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: chat.ID, PubsubTopic: community.PubsubTopic(), ContentTopicID: community.MemberUpdateChannelID()})
} }
@ -2394,7 +2394,7 @@ func (m *Messenger) CreateCommunityChat(communityID types.HexBytes, c *protobuf.
for chatID, chat := range changes.ChatsAdded { for chatID, chat := range changes.ChatsAdded {
c := CreateCommunityChat(changes.Community.IDString(), chatID, chat, m.getTimesource()) c := CreateCommunityChat(changes.Community.IDString(), chatID, chat, m.getTimesource())
chats = append(chats, c) chats = append(chats, c)
publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: c.ID, PubsubTopic: changes.Community.PubsubTopic()}) publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: c.ID, PubsubTopic: changes.Community.PubsubTopic(), ContentTopicID: changes.Community.MemberUpdateChannelID()})
response.AddChat(c) response.AddChat(c)
} }
@ -2489,10 +2489,10 @@ func (m *Messenger) DefaultFilters(o *communities.Community) []transport.Filters
communityPubsubTopic := o.PubsubTopic() communityPubsubTopic := o.PubsubTopic()
filters := []transport.FiltersToInitialize{ filters := []transport.FiltersToInitialize{
{ChatID: cID, PubsubTopic: communityPubsubTopic}, {ChatID: cID, PubsubTopic: communityPubsubTopic, ContentTopicID: o.MemberUpdateChannelID()},
{ChatID: updatesChannelID, PubsubTopic: communityPubsubTopic}, {ChatID: updatesChannelID, PubsubTopic: communityPubsubTopic, ContentTopicID: o.MemberUpdateChannelID()},
{ChatID: mlChannelID, PubsubTopic: communityPubsubTopic}, {ChatID: mlChannelID, PubsubTopic: communityPubsubTopic, ContentTopicID: o.MemberUpdateChannelID()},
{ChatID: memberUpdateChannelID, PubsubTopic: communityPubsubTopic}, {ChatID: memberUpdateChannelID, PubsubTopic: communityPubsubTopic, ContentTopicID: o.MemberUpdateChannelID()},
{ChatID: uncompressedPubKey, PubsubTopic: shard.DefaultNonProtectedPubsubTopic()}, {ChatID: uncompressedPubKey, PubsubTopic: shard.DefaultNonProtectedPubsubTopic()},
} }
@ -3406,8 +3406,9 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi
state.Response.AddChat(chat) state.Response.AddChat(chat)
publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{
ChatID: chat.ID, ChatID: chat.ID,
PubsubTopic: community.PubsubTopic(), PubsubTopic: community.PubsubTopic(),
ContentTopicID: community.MemberUpdateChannelID(),
}) })
// Update name, currently is the only field is mutable // Update name, currently is the only field is mutable
} else if oldChat.Name != chat.Name || } else if oldChat.Name != chat.Name ||

View File

@ -99,7 +99,7 @@ func (f *FiltersManager) Init(
// Add public, one-to-one and negotiated filters. // Add public, one-to-one and negotiated filters.
for _, fi := range filtersToInit { for _, fi := range filtersToInit {
_, err := f.LoadPublic(fi.ChatID, fi.PubsubTopic) _, err := f.LoadPublic(fi.ChatID, fi.PubsubTopic, fi.ContentTopicID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -123,15 +123,16 @@ func (f *FiltersManager) Init(
} }
type FiltersToInitialize struct { type FiltersToInitialize struct {
ChatID string ChatID string
PubsubTopic string PubsubTopic string
ContentTopicID string //litte hacky but this is used to override content-topic in filtersManager.
} }
func (f *FiltersManager) InitPublicFilters(publicFiltersToInit []FiltersToInitialize) ([]*Filter, error) { func (f *FiltersManager) InitPublicFilters(publicFiltersToInit []FiltersToInitialize) ([]*Filter, error) {
var filters []*Filter var filters []*Filter
// Add public, one-to-one and negotiated filters. // Add public, one-to-one and negotiated filters.
for _, pf := range publicFiltersToInit { for _, pf := range publicFiltersToInit {
f, err := f.LoadPublic(pf.ChatID, pf.PubsubTopic) f, err := f.LoadPublic(pf.ChatID, pf.PubsubTopic, pf.ContentTopicID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -455,7 +456,7 @@ func (f *FiltersManager) LoadNegotiated(secret types.NegotiatedSecret) (*Filter,
} }
keyString := hex.EncodeToString(secret.Key) keyString := hex.EncodeToString(secret.Key)
filter, err := f.addSymmetric(keyString, "") filter, err := f.addSymmetric(keyString, "", "")
if err != nil { if err != nil {
f.logger.Debug("could not register negotiated topic", zap.Error(err)) f.logger.Debug("could not register negotiated topic", zap.Error(err))
return nil, err return nil, err
@ -534,7 +535,7 @@ func (f *FiltersManager) PersonalTopicFilter() *Filter {
} }
// LoadPublic adds a filter for a public chat. // LoadPublic adds a filter for a public chat.
func (f *FiltersManager) LoadPublic(chatID string, pubsubTopic string) (*Filter, error) { func (f *FiltersManager) LoadPublic(chatID string, pubsubTopic string, contentTopicID string) (*Filter, error) {
f.mutex.Lock() f.mutex.Lock()
defer f.mutex.Unlock() defer f.mutex.Unlock()
@ -553,7 +554,7 @@ func (f *FiltersManager) LoadPublic(chatID string, pubsubTopic string) (*Filter,
return chat, nil return chat, nil
} }
filterAndTopic, err := f.addSymmetric(chatID, pubsubTopic) filterAndTopic, err := f.addSymmetric(chatID, pubsubTopic, contentTopicID)
if err != nil { if err != nil {
f.logger.Debug("could not register public chat topic", zap.String("chatID", chatID), zap.Error(err)) f.logger.Debug("could not register public chat topic", zap.String("chatID", chatID), zap.Error(err))
return nil, err return nil, err
@ -592,7 +593,7 @@ func (f *FiltersManager) LoadContactCode(pubKey *ecdsa.PublicKey) (*Filter, erro
return f.filters[chatID], nil return f.filters[chatID], nil
} }
contactCodeFilter, err := f.addSymmetric(chatID, "") contactCodeFilter, err := f.addSymmetric(chatID, "", "")
if err != nil { if err != nil {
f.logger.Debug("could not register contact code topic", zap.String("chatID", chatID), zap.Error(err)) f.logger.Debug("could not register contact code topic", zap.String("chatID", chatID), zap.Error(err))
return nil, err return nil, err
@ -615,7 +616,7 @@ func (f *FiltersManager) LoadContactCode(pubKey *ecdsa.PublicKey) (*Filter, erro
} }
// addSymmetric adds a symmetric key filter // addSymmetric adds a symmetric key filter
func (f *FiltersManager) addSymmetric(chatID string, pubsubTopic string) (*RawFilter, error) { func (f *FiltersManager) addSymmetric(chatID string, pubsubTopic string, contentTopicID string) (*RawFilter, error) {
var symKeyID string var symKeyID string
var err error var err error
@ -644,6 +645,12 @@ func (f *FiltersManager) addSymmetric(chatID string, pubsubTopic string) (*RawFi
} }
} }
if contentTopicID != "" {
//override with single contentTopic for all community chats
topic = ToTopic(contentTopicID)
topics = append(topics, topic)
}
id, err := f.service.Subscribe(&types.SubscriptionOptions{ id, err := f.service.Subscribe(&types.SubscriptionOptions{
SymKeyID: symKeyID, SymKeyID: symKeyID,
PoW: minPow, PoW: minPow,

View File

@ -190,7 +190,7 @@ func (t *Transport) ProcessNegotiatedSecret(secret types.NegotiatedSecret) (*Fil
} }
func (t *Transport) JoinPublic(chatID string) (*Filter, error) { func (t *Transport) JoinPublic(chatID string) (*Filter, error) {
return t.filters.LoadPublic(chatID, "") return t.filters.LoadPublic(chatID, "", "")
} }
func (t *Transport) LeavePublic(chatID string) error { func (t *Transport) LeavePublic(chatID string) error {
@ -279,12 +279,12 @@ func (t *Transport) SendPublic(ctx context.Context, newMessage *types.NewMessage
if err := t.addSig(newMessage); err != nil { if err := t.addSig(newMessage); err != nil {
return nil, err return nil, err
} }
//passing empty communityID as the filter should have already been loaded.
filter, err := t.filters.LoadPublic(chatName, newMessage.PubsubTopic) //TODO: is there a scenario where filter is not loaded until a message is sent?
filter, err := t.filters.LoadPublic(chatName, newMessage.PubsubTopic, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
newMessage.SymKeyID = filter.SymKeyID newMessage.SymKeyID = filter.SymKeyID
newMessage.Topic = filter.ContentTopic newMessage.Topic = filter.ContentTopic
newMessage.PubsubTopic = filter.PubsubTopic newMessage.PubsubTopic = filter.PubsubTopic
@ -361,7 +361,9 @@ func (t *Transport) SendCommunityMessage(ctx context.Context, newMessage *types.
} }
// We load the filter to make sure we can post on it // We load the filter to make sure we can post on it
filter, err := t.filters.LoadPublic(PubkeyToHex(publicKey)[2:], newMessage.PubsubTopic) //passing empty communityID as the filter should have already been loaded.
//TODO: is there a scenario where filter is not loaded until a message is sent?
filter, err := t.filters.LoadPublic(PubkeyToHex(publicKey)[2:], newMessage.PubsubTopic, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }