Skip to content

Commit

Permalink
refactored code to use request.MuteCommunity struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-mut committed Jun 20, 2023
1 parent d2035ec commit 3abc86e
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 139 deletions.
4 changes: 2 additions & 2 deletions appdatabase/migrations/bindata.go

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

2 changes: 1 addition & 1 deletion protocol/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
MuteFor1Week
MuteTillUnmuted
MuteTill1Min
Unmuted
UnMuted
)

const pkStringLength = 68
Expand Down
8 changes: 6 additions & 2 deletions protocol/communities/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,12 @@ 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) {
Expand Down
62 changes: 8 additions & 54 deletions protocol/communities/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/golang/protobuf/proto"
"go.uber.org/zap"
Expand All @@ -27,11 +28,7 @@ var ErrOldRequestToJoin = errors.New("old request to join")
var ErrOldRequestToLeave = errors.New("old request to leave")

const OR = " OR "
<<<<<<< HEAD
const communitiesBaseQuery = `SELECT c.id, c.private_key, c.description,c.joined,c.spectated,c.verified,c.muted,c.muted_till,r.clock FROM communities_communities c LEFT JOIN communities_requests_to_join r ON c.id = r.community_id AND r.public_key = ?`
=======
const communitiesBaseQuery = `SELECT c.id, c.private_key, c.description,c.joined,c.spectated,c.verified,c.muted, c.muted_till, r.clock FROM communities_communities c LEFT JOIN communities_requests_to_join r ON c.id = r.community_id AND r.public_key = ?`
>>>>>>> d6856fdf3 (added mute till to community)

func (p *Persistence) SaveCommunity(community *Community) error {
id := community.ID()
Expand Down Expand Up @@ -109,22 +106,12 @@ func (p *Persistence) queryCommunities(memberIdentity *ecdsa.PublicKey, query st
var joined, spectated, verified, muted bool
var muteTill sql.NullTime
var requestedToJoinAt sql.NullInt64
<<<<<<< HEAD
err := rows.Scan(&publicKeyBytes, &privateKeyBytes, &descriptionBytes, &joined, &spectated, &verified, &muted, &muteTill, &requestedToJoinAt)
=======
var mutedTill sql.NullTime

err := rows.Scan(&publicKeyBytes, &privateKeyBytes, &descriptionBytes, &joined, &spectated, &verified, &muted, &mutedTill, &requestedToJoinAt)
>>>>>>> d6856fdf3 (added mute till to community)
if err != nil {
return nil, err
}

<<<<<<< HEAD
org, err := unmarshalCommunityFromDB(memberIdentity, publicKeyBytes, privateKeyBytes, descriptionBytes, joined, spectated, verified, muted, muteTill.Time, uint64(requestedToJoinAt.Int64), p.logger)
=======
org, err := unmarshalCommunityFromDB(memberIdentity, publicKeyBytes, privateKeyBytes, descriptionBytes, joined, spectated, verified, muted, mutedTill, uint64(requestedToJoinAt.Int64), p.logger)
>>>>>>> d6856fdf3 (added mute till to community)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,33 +153,21 @@ func (p *Persistence) rowsToCommunities(memberIdentity *ecdsa.PublicKey, rows *s
// Community specific fields
var publicKeyBytes, privateKeyBytes, descriptionBytes []byte
var joined, spectated, verified, muted bool
<<<<<<< HEAD
var muteTill sql.NullTime
=======
var mutedTill sql.NullTime
>>>>>>> d6856fdf3 (added mute till to community)

// Request to join specific fields
var rtjID, rtjCommunityID []byte
var rtjPublicKey, rtjENSName, rtjChatID sql.NullString
var rtjClock, rtjState sql.NullInt64

err = rows.Scan(
<<<<<<< HEAD
&publicKeyBytes, &privateKeyBytes, &descriptionBytes, &joined, &spectated, &verified, &muted, &muteTill,
=======
&publicKeyBytes, &privateKeyBytes, &descriptionBytes, &joined, &spectated, &verified, &muted, &mutedTill,
>>>>>>> d6856fdf3 (added mute till to community)
&rtjID, &rtjPublicKey, &rtjClock, &rtjENSName, &rtjChatID, &rtjCommunityID, &rtjState)
if err != nil {
return nil, err
}

<<<<<<< HEAD
comm, err = unmarshalCommunityFromDB(memberIdentity, publicKeyBytes, privateKeyBytes, descriptionBytes, joined, spectated, verified, muted, muteTill.Time, uint64(rtjClock.Int64), p.logger)
=======
comm, err = unmarshalCommunityFromDB(memberIdentity, publicKeyBytes, privateKeyBytes, descriptionBytes, joined, spectated, verified, muted, mutedTill, uint64(rtjClock.Int64), p.logger)
>>>>>>> d6856fdf3 (added mute till to community)
if err != nil {
return nil, err
}
Expand All @@ -209,11 +184,7 @@ func (p *Persistence) rowsToCommunities(memberIdentity *ecdsa.PublicKey, rows *s

func (p *Persistence) JoinedAndPendingCommunitiesWithRequests(memberIdentity *ecdsa.PublicKey) (comms []*Community, err error) {
query := `SELECT
<<<<<<< HEAD
c.id, c.private_key, c.description, c.joined, c.spectated, c.verified, c.muted, c.muted_till,
=======
c.id, c.private_key, c.description, c.joined, c.spectated, c.verified, c.muted, c.muted_till
>>>>>>> d6856fdf3 (added mute till to community)
r.id, r.public_key, r.clock, r.ens_name, r.chat_id, r.community_id, r.state
FROM communities_communities c
LEFT JOIN communities_requests_to_join r ON c.id = r.community_id AND r.public_key = ?
Expand All @@ -229,11 +200,7 @@ WHERE c.Joined OR r.state = ?`

func (p *Persistence) DeletedCommunities(memberIdentity *ecdsa.PublicKey) (comms []*Community, err error) {
query := `SELECT
<<<<<<< HEAD
c.id, c.private_key, c.description, c.joined, c.spectated, c.verified, c.muted, c.muted_till,
=======
c.id, c.private_key, c.description, c.joined, c.spectated, c.verified, c.muted, c.muted_till
>>>>>>> d6856fdf3 (added mute till to community)
r.id, r.public_key, r.clock, r.ens_name, r.chat_id, r.community_id, r.state
FROM communities_communities c
LEFT JOIN communities_requests_to_join r ON c.id = r.community_id AND r.public_key = ?
Expand All @@ -258,35 +225,21 @@ func (p *Persistence) GetByID(memberIdentity *ecdsa.PublicKey, id []byte) (*Comm
var spectated bool
var verified bool
var muted bool
<<<<<<< HEAD
var muteTill sql.NullTime
var requestedToJoinAt sql.NullInt64

err := p.db.QueryRow(communitiesBaseQuery+` WHERE c.id = ?`, common.PubkeyToHex(memberIdentity), id).Scan(&publicKeyBytes, &privateKeyBytes, &descriptionBytes, &joined, &spectated, &verified, &muted, &muteTill, &requestedToJoinAt)
=======
var mutedTill sql.NullTime
var requestedToJoinAt sql.NullInt64

err := p.db.QueryRow(communitiesBaseQuery+` WHERE c.id = ?`, common.PubkeyToHex(memberIdentity), id).Scan(&publicKeyBytes, &privateKeyBytes, &descriptionBytes, &joined, &spectated, &verified, &muted, &mutedTill, &requestedToJoinAt)
>>>>>>> d6856fdf3 (added mute till to community)

if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}

<<<<<<< HEAD
return unmarshalCommunityFromDB(memberIdentity, publicKeyBytes, privateKeyBytes, descriptionBytes, joined, spectated, verified, muted, muteTill.Time, uint64(requestedToJoinAt.Int64), p.logger)
}

func unmarshalCommunityFromDB(memberIdentity *ecdsa.PublicKey, publicKeyBytes, privateKeyBytes, descriptionBytes []byte, joined, spectated, verified, muted bool, muteTill time.Time, requestedToJoinAt uint64, logger *zap.Logger) (*Community, error) {
=======
return unmarshalCommunityFromDB(memberIdentity, publicKeyBytes, privateKeyBytes, descriptionBytes, joined, spectated, verified, muted, mutedTill, uint64(requestedToJoinAt.Int64), p.logger)
}

func unmarshalCommunityFromDB(memberIdentity *ecdsa.PublicKey, publicKeyBytes, privateKeyBytes, descriptionBytes []byte, joined, spectated, verified, muted bool, mutedTill sql.NullTime, requestedToJoinAt uint64, logger *zap.Logger) (*Community, error) {
>>>>>>> d6856fdf3 (added mute till to community)

var privateKey *ecdsa.PrivateKey
var err error
Expand Down Expand Up @@ -329,11 +282,7 @@ func unmarshalCommunityFromDB(memberIdentity *ecdsa.PublicKey, publicKeyBytes, p
ID: id,
Verified: verified,
Muted: muted,
<<<<<<< HEAD
MuteTill: muteTill,
=======
MuteTill: mutedTill.Time,
>>>>>>> d6856fdf3 (added mute till to community)
RequestedToJoinAt: requestedToJoinAt,
Joined: joined,
Spectated: spectated,
Expand Down Expand Up @@ -607,9 +556,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
17 changes: 13 additions & 4 deletions protocol/communities_messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3315,7 +3315,7 @@ func (s *MessengerCommunitiesSuite) TestSetMutePropertyOnChatsByCategory() {
err = s.alice.SetMutePropertyOnChatsByCategory(&requests.MuteCategory{
CommunityID: newCommunity.IDString(),
CategoryID: categoryID,
MutedType: Unmuted,
MutedType: UnMuted,
}, false)
s.Require().NoError(err)

Expand Down Expand Up @@ -3345,9 +3345,12 @@ func (s *MessengerCommunitiesSuite) TestCheckCommunitiesToUnmute() {

currTime, _ := time.Parse(time.RFC3339, time.Now().Add(-time.Hour).Format(time.RFC3339))

err = s.alice.communitiesManager.SetMuted(newCommunity.ID(), true, currTime)
err = s.alice.communitiesManager.SetMuted(newCommunity.ID(), true)
s.Require().NoError(err, "SetMuted to community")

err = s.alice.communitiesManager.MuteCommunityTill(newCommunity.ID(), currTime)
s.Require().NoError(err, "SetMuteTill to community")

response := &MessengerResponse{}

err = s.alice.CheckCommunitiesToUnmute(response)
Expand Down Expand Up @@ -3412,7 +3415,10 @@ func (s *MessengerCommunitiesSuite) TestMuteAllCommunityChats() {
muteDuration, err := s.alice.MuteDuration(MuteFor15Min)
s.Require().NoError(err)

err = s.alice.MuteAllCommunityChats(newCommunity.IDString(), MuteFor15Min)
err = s.alice.MuteAllCommunityChats(&requests.MuteCommunity{
CommunityID: newCommunity.ID(),
MutedType: MuteFor15Min,
})
s.Require().NoError(err)

aliceCommunity, err := s.alice.GetCommunityByID(newCommunity.ID())
Expand All @@ -3432,7 +3438,10 @@ func (s *MessengerCommunitiesSuite) TestMuteAllCommunityChats() {
s.Require().NoError(err)
s.Require().False(chat.Muted)

err = s.alice.UnMuteAllCommunityChats(newCommunity.IDString())
err = s.alice.UnMuteAllCommunityChats(&requests.MuteCommunity{
CommunityID: newCommunity.ID(),
MutedType: MuteTillUnmuted,
})
s.Require().NoError(err)

for _, chat := range s.alice.Chats() {
Expand Down
5 changes: 4 additions & 1 deletion protocol/messenger_activity_center_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ func (s *MessengerActivityCenterMessageSuite) TestMuteCommunityActivityCenterNot
defaultCommunityChatID := response.Chats()[0].ID

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

bobCommunity, err := bob.GetCommunityByID(community.ID())
Expand Down
76 changes: 39 additions & 37 deletions protocol/messenger_communities.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,17 @@ func (m *Messenger) CheckCommunitiesToUnmute(response *MessengerResponse) error
communityMuteTill, _ := time.Parse(time.RFC3339, community.MuteTill().Format(time.RFC3339))
currTime, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
if currTime.After(communityMuteTill) && !communityMuteTill.Equal(time.Time{}) && community.Muted() {
err := m.communitiesManager.SetMuted(community.ID(), false, time.Time{})
err := m.communitiesManager.SetMuted(community.ID(), false)
if err != nil {
m.logger.Info("CheckCommunitiesToUnmute err", zap.Any("Couldn't unmute community", err))
break
}

err = m.MuteCommunityTill(community.ID(), time.Time{})
if err != nil {
m.logger.Info("MuteCommunityTill err", zap.Any("Could not set mute community till", err))
break
}
response.AddCommunity(community)
}
}
Expand Down Expand Up @@ -595,37 +601,6 @@ func (m *Messenger) SpectateCommunity(communityID types.HexBytes) (*MessengerRes
return response, nil
}

func (m *Messenger) SetMuted(request *requests.MuteCommunity) error {
if err := request.Validate(); err != nil {
return err
}
if request.MutedType == Unmuted {
return m.communitiesManager.SetMuted(request.CommunityID, false, time.Time{})
}
var MuteTill time.Time

switch request.MutedType {
case MuteTill1Min:
MuteTill = time.Now().Add(MuteFor1MinDuration)
case MuteFor15Min:
MuteTill = time.Now().Add(MuteFor15MinsDuration)
case MuteFor1Hr:
MuteTill = time.Now().Add(MuteFor1HrsDuration)
case MuteFor8Hr:
MuteTill = time.Now().Add(MuteFor8HrsDuration)
case MuteFor1Week:
MuteTill = time.Now().Add(MuteFor1WeekDuration)
default:
MuteTill = time.Time{}
}
muteTillTimeRemoveMs, err := time.Parse(time.RFC3339, MuteTill.Format(time.RFC3339))

if err != nil {
return err
}
return m.communitiesManager.SetMuted(request.CommunityID, true, muteTillTimeRemoveMs)
}

func (m *Messenger) MuteDuration(mutedType requests.MutingVariation) (time.Time, error) {
var MuteTill time.Time

Expand All @@ -652,12 +627,37 @@ func (m *Messenger) MuteDuration(mutedType requests.MutingVariation) (time.Time,
return muteTillTimeRemoveMs, nil
}

func (m *Messenger) MuteAllCommunityChats(communityID string, mutedType requests.MutingVariation) error {
return m.UpdateMuteCommunityStatus(communityID, true, mutedType)
func (m *Messenger) SetMuted(request *requests.MuteCommunity) error {
if err := request.Validate(); err != nil {
return err
}
if request.MutedType == UnMuted {
return m.communitiesManager.SetMuted(request.CommunityID, false)
}

return m.communitiesManager.SetMuted(request.CommunityID, true)
}

func (m *Messenger) UnMuteAllCommunityChats(communityID string) error {
return m.UpdateMuteCommunityStatus(communityID, false, 0)
func (m *Messenger) MuteCommunityTill(communityID []byte, muteTill time.Time) error {
return m.communitiesManager.MuteCommunityTill(communityID, muteTill)
}

func (m *Messenger) MuteAllCommunityChats(request *requests.MuteCommunity) error {
err := m.SetMuted(request)
if err != nil {
return err
}

return m.UpdateMuteCommunityStatus(request.CommunityID.String(), true, request.MutedType)
}

func (m *Messenger) UnMuteAllCommunityChats(request *requests.MuteCommunity) error {
err := m.SetMuted(request)
if err != nil {
return err
}

return m.UpdateMuteCommunityStatus(request.CommunityID.String(), false, UnMuted)
}

func (m *Messenger) UpdateMuteCommunityStatus(communityID string, muted bool, mutedType requests.MutingVariation) error {
Expand All @@ -666,11 +666,13 @@ func (m *Messenger) UpdateMuteCommunityStatus(communityID string, muted bool, mu
return err
}

err = m.SetMuted(community.ID(), muted)
muteTill, err := m.MuteDuration(mutedType)
if err != nil {
return err
}

err = m.MuteCommunityTill(community.ID(), muteTill)

for _, chatID := range community.CommunityChatsIDs() {
if muted {
_, err := m.MuteChat(&requests.MuteChat{ChatID: communityID + chatID, MutedType: mutedType})
Expand Down
Loading

0 comments on commit 3abc86e

Please sign in to comment.