feat: Add mute category methods (#2736)

This commit is contained in:
Boris Melnik 2022-07-06 19:16:19 +03:00 committed by GitHub
parent 76e2d843a6
commit 3e65e36fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 135 additions and 0 deletions

View File

@ -6,6 +6,22 @@ import (
"github.com/status-im/status-go/protocol/protobuf"
)
func (o *Community) ChatsByCategoryID(categoryID string) []string {
o.mutex.Lock()
defer o.mutex.Unlock()
var chatIDs []string
if o == nil || o.config == nil || o.config.CommunityDescription == nil {
return chatIDs
}
for chatID, chat := range o.config.CommunityDescription.Chats {
if chat.CategoryId == categoryID {
chatIDs = append(chatIDs, chatID)
}
}
return chatIDs
}
func (o *Community) CreateCategory(categoryID string, categoryName string, chatIDs []string) (*CommunityChanges, error) {
o.mutex.Lock()
defer o.mutex.Unlock()

View File

@ -1758,3 +1758,94 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunity_Leave() {
aoCom := mr.Communities()[0]
s.Equal(aCom, aoCom)
}
func (s *MessengerCommunitiesSuite) TestSetMutePropertyOnChatsByCategory() {
// Create a community
createCommunityReq := &requests.CreateCommunity{
Membership: protobuf.CommunityPermissions_ON_REQUEST,
Name: "new community",
Color: "#000000",
Description: "new community description",
}
mr, err := s.alice.CreateCommunity(createCommunityReq)
s.Require().NoError(err, "s.alice.CreateCommunity")
var newCommunity *communities.Community
for _, com := range mr.Communities() {
if com.Name() == createCommunityReq.Name {
newCommunity = com
}
}
s.Require().NotNil(newCommunity)
orgChat1 := &protobuf.CommunityChat{
Permissions: &protobuf.CommunityPermissions{
Access: protobuf.CommunityPermissions_NO_MEMBERSHIP,
},
Identity: &protobuf.ChatIdentity{
DisplayName: "status-core",
Emoji: "😎",
Description: "status-core community chat",
},
}
orgChat2 := &protobuf.CommunityChat{
Permissions: &protobuf.CommunityPermissions{
Access: protobuf.CommunityPermissions_NO_MEMBERSHIP,
},
Identity: &protobuf.ChatIdentity{
DisplayName: "status-core2",
Emoji: "😎",
Description: "status-core community chat2",
},
}
mr, err = s.alice.CreateCommunityChat(newCommunity.ID(), orgChat1)
s.Require().NoError(err)
s.Require().NotNil(mr)
s.Require().Len(mr.Communities(), 1)
s.Require().Len(mr.Chats(), 1)
mr, err = s.alice.CreateCommunityChat(newCommunity.ID(), orgChat2)
s.Require().NoError(err)
s.Require().NotNil(mr)
s.Require().Len(mr.Communities(), 1)
s.Require().Len(mr.Chats(), 1)
var chatIds []string
for k := range newCommunity.Chats() {
chatIds = append(chatIds, k)
}
category := &requests.CreateCommunityCategory{
CommunityID: newCommunity.ID(),
CategoryName: "category-name",
ChatIDs: chatIds,
}
mr, err = s.alice.CreateCommunityCategory(category)
s.Require().NoError(err)
s.Require().NotNil(mr)
s.Require().Len(mr.Communities(), 1)
s.Require().Len(mr.Communities()[0].Categories(), 1)
var categoryID string
for k := range mr.Communities()[0].Categories() {
categoryID = k
}
err = s.alice.SetMutePropertyOnChatsByCategory(newCommunity.IDString(), categoryID, true)
s.Require().NoError(err)
for _, chat := range s.alice.Chats() {
if chat.CategoryID == categoryID {
s.Require().True(chat.Muted)
}
}
err = s.alice.SetMutePropertyOnChatsByCategory(newCommunity.IDString(), categoryID, false)
s.Require().NoError(err)
for _, chat := range s.alice.Chats() {
s.Require().False(chat.Muted)
}
}

View File

@ -349,6 +349,26 @@ func (m *Messenger) SetMuted(communityID types.HexBytes, muted bool) error {
return m.communitiesManager.SetMuted(communityID, muted)
}
func (m *Messenger) SetMutePropertyOnChatsByCategory(communityID string, categoryID string, muted bool) error {
community, err := m.communitiesManager.GetByIDString(communityID)
if err != nil {
return err
}
for _, chatID := range community.ChatsByCategoryID(categoryID) {
if muted {
err = m.MuteChat(communityID + chatID)
} else {
err = m.UnmuteChat(communityID + chatID)
}
if err != nil {
return err
}
}
return nil
}
func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommunity) (*MessengerResponse, error) {
logger := m.logger.Named("RequestToJoinCommunity")
if err := request.Validate(); err != nil {

View File

@ -287,6 +287,14 @@ func (api *PublicAPI) DeleteChat(parent context.Context, chatID string) error {
return api.service.messenger.DeleteChat(chatID)
}
func (api *PublicAPI) MuteCommunityCategory(communityID string, categoryID string) error {
return api.service.messenger.SetMutePropertyOnChatsByCategory(communityID, categoryID, true)
}
func (api *PublicAPI) UnmuteCommunityCategory(communityID string, categoryID string) error {
return api.service.messenger.SetMutePropertyOnChatsByCategory(communityID, categoryID, false)
}
func (api *PublicAPI) MuteChat(parent context.Context, chatID string) error {
return api.service.messenger.MuteChat(chatID)
}