Skip to content

Commit

Permalink
Mute and Unmute community
Browse files Browse the repository at this point in the history
* mute and unmute all community chats when community mute status changes

* unmute community when atleast one channel is unmuted

* fix: save community, extend the function to save muted state and mute duration
  • Loading branch information
jo-mut authored Jul 19, 2023
1 parent 1f379ae commit cf2d72b
Show file tree
Hide file tree
Showing 22 changed files with 699 additions and 348 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.162.4
0.162.5
162 changes: 81 additions & 81 deletions appdatabase/migrations/bindata.go

Large diffs are not rendered by default.

108 changes: 54 additions & 54 deletions appdatabase/migrationsprevnodecfg/bindata.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions mailserver/migrations/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions multiaccounts/migrations/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions protocol/activity_center_persistence_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package protocol

import (
"context"
"strconv"
"testing"
"time"
Expand All @@ -9,6 +10,8 @@ import (

"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
)

func currentMilliseconds() uint64 {
Expand Down Expand Up @@ -210,6 +213,98 @@ func TestDeleteActivityCenterNotificationsForMessage(t *testing.T) {
require.NoError(t, err)
}

func (s *MessengerActivityCenterMessageSuite) TestMuteCommunityActivityCenterNotifications() {

description := &requests.CreateCommunity{
Membership: protobuf.CommunityPermissions_NO_MEMBERSHIP,
Name: "status",
Color: "#ffffff",
Description: "status community description",
}

alice := s.m
bob := s.newMessenger()
_, err := bob.Start()
s.Require().NoError(err)

// Create an community chat
response, err := bob.CreateCommunity(description, true)
s.Require().NoError(err)
s.Require().Len(response.Communities(), 1)

community := response.Communities()[0]
s.Require().NotNil(community)

chat := CreateOneToOneChat(common.PubkeyToHex(&alice.identity.PublicKey), &alice.identity.PublicKey, bob.transport)

// bob sends a community message
inputMessage := &common.Message{}
inputMessage.ChatId = chat.ID
inputMessage.Text = "some text"
inputMessage.CommunityID = community.IDString()

err = bob.SaveChat(chat)
s.Require().NoError(err)
_, err = bob.SendChatMessage(context.Background(), inputMessage)
s.Require().NoError(err)

_, err = WaitOnMessengerResponse(
alice,
func(r *MessengerResponse) bool { return len(r.Communities()) == 1 },
"no messages",
)

s.Require().NoError(err)

// Alice joins the community
response, err = alice.JoinCommunity(context.Background(), community.ID(), true)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1)
s.Require().True(response.Communities()[0].Joined())
s.Require().Len(response.Chats(), 1)

defaultCommunityChatID := response.Chats()[0].ID

// Bob mutes the community
time, err := bob.MuteAllCommunityChats(&requests.MuteCommunity{
CommunityID: community.ID(),
MutedType: MuteTillUnmuted,
})
s.Require().NoError(err)
s.Require().NotNil(time)

bobCommunity, err := bob.GetCommunityByID(community.ID())
s.Require().NoError(err)
s.Require().True(bobCommunity.Muted())

// alice sends a community message
inputMessage = &common.Message{}
inputMessage.ChatId = defaultCommunityChatID
inputMessage.Text = "Good news, @" + common.EveryoneMentionTag + " !"
inputMessage.CommunityID = community.IDString()

response, err = alice.SendChatMessage(context.Background(), inputMessage)
s.Require().NoError(err)

s.Require().Len(response.Messages(), 1)

s.Require().True(response.Messages()[0].Mentioned)

response, err = WaitOnMessengerResponse(
bob,
func(r *MessengerResponse) bool { return len(r.Messages()) == 1 },
"no messages",
)

s.Require().NoError(err)

s.Require().Len(response.Messages(), 1)

s.Require().True(response.Messages()[0].Mentioned)
s.Require().Len(response.ActivityCenterNotifications(), 0)
}

func TestAcceptActivityCenterNotificationsForInvitesFromUser(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions protocol/anonmetrics/migrations/migrations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions protocol/communities/community_categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ func (o *Community) ChatsByCategoryID(categoryID string) []string {
return chatIDs
}

func (o *Community) CommunityChatsIDs() []string {
o.mutex.Lock()
defer o.mutex.Unlock()
var chatIDs []string
if o.config == nil || o.config.CommunityDescription == nil {
return chatIDs
}

for chatID := range o.config.CommunityDescription.Chats {
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()
Expand Down
7 changes: 5 additions & 2 deletions protocol/communities/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1426,10 +1426,13 @@ func (m *Manager) UpdateClockInRequestToJoin(id types.HexBytes, clock uint64) er
return m.persistence.UpdateClockInRequestToJoin(id, clock)
}

func (m *Manager) SetMuted(id types.HexBytes, muted bool, mutedTill time.Time) error {
return m.persistence.SetMuted(id, muted, mutedTill)
func (m *Manager) SetMuted(id types.HexBytes, muted bool) error {
return m.persistence.SetMuted(id, muted)
}

func (m *Manager) MuteCommunityTill(communityID []byte, muteTill time.Time) error {
return m.persistence.MuteCommunityTill(communityID, muteTill)
}
func (m *Manager) CancelRequestToJoin(request *requests.CancelRequestToJoinCommunity) (*RequestToJoin, *Community, error) {
dbRequest, err := m.persistence.GetRequestToJoin(request.ID)
if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions protocol/communities/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ func (p *Persistence) SaveCommunity(community *Community) error {
}

_, err = p.db.Exec(`
INSERT INTO communities_communities (id, private_key, description, joined, spectated, verified) VALUES (?, ?, ?, ?, ?, ?);`,
id, crypto.FromECDSA(privateKey), description, community.config.Joined, community.config.Spectated, community.config.Verified)

INSERT INTO communities_communities (id, private_key, description, joined, spectated, verified, muted, muted_till) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
id, crypto.FromECDSA(privateKey), description, community.config.Joined, community.config.Spectated, community.config.Verified, community.config.Muted, community.config.MuteTill)
return err
}

Expand Down Expand Up @@ -764,9 +763,14 @@ func (p *Persistence) UpdateClockInRequestToJoin(id []byte, clock uint64) error
return err
}

func (p *Persistence) SetMuted(communityID []byte, muted bool, mutedTill time.Time) error {
func (p *Persistence) SetMuted(communityID []byte, muted bool) error {
_, err := p.db.Exec(`UPDATE communities_communities SET muted = ? WHERE id = ?`, muted, communityID)
return err
}

func (p *Persistence) MuteCommunityTill(communityID []byte, mutedTill time.Time) error {
mutedTillFormatted := mutedTill.Format(time.RFC3339)
_, err := p.db.Exec(`UPDATE communities_communities SET muted = ?, muted_till = ? WHERE id = ?`, muted, mutedTillFormatted, communityID)
_, err := p.db.Exec(`UPDATE communities_communities SET muted_till = ? WHERE id = ?`, mutedTillFormatted, communityID)
return err
}

Expand Down
4 changes: 4 additions & 0 deletions protocol/communities/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (s *PersistenceSuite) TestSaveCommunity() {
Joined: true,
Spectated: true,
Verified: true,
Muted: true,
MuteTill: time.Time{},
CommunityDescription: &protobuf.CommunityDescription{},
},
}
Expand All @@ -72,6 +74,8 @@ func (s *PersistenceSuite) TestSaveCommunity() {
s.Equal(true, communities[1].Joined())
s.Equal(true, communities[1].Spectated())
s.Equal(true, communities[1].Verified())
s.Equal(true, communities[1].Muted())
s.Equal(time.Time{}, communities[1].MuteTill())
}

func (s *PersistenceSuite) TestShouldHandleSyncCommunity() {
Expand Down
Loading

0 comments on commit cf2d72b

Please sign in to comment.