Skip to content

Commit 4744ee8

Browse files
committed
Add TestPinMessageInCommunityChat, adjust pin permissions
Also check pin post permission a bit earlier for the sender, with canPost() in sendPinMessage(), even though there is a later check down the line in dispatchMessage(). Fixes #4138
1 parent 74396b4 commit 4744ee8

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

protocol/communities_messenger_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,82 @@ func (s *MessengerCommunitiesSuite) TestPostToCommunityChat() {
498498
s.Require().True(found)
499499
}
500500

501+
func (s *MessengerCommunitiesSuite) TestPinMessageInCommunityChat() {
502+
ctx := context.Background()
503+
504+
// Create a community
505+
description := &requests.CreateCommunity{
506+
Membership: protobuf.CommunityPermissions_AUTO_ACCEPT,
507+
Name: "status",
508+
Color: "#ffffff",
509+
Description: "status community description",
510+
PinMessageAllMembersEnabled: true,
511+
}
512+
513+
response, err := s.owner.CreateCommunity(description, true)
514+
s.Require().NoError(err)
515+
s.Require().NotNil(response)
516+
s.Require().Len(response.Communities(), 1)
517+
518+
community := response.Communities()[0]
519+
s.Require().NotNil(community)
520+
s.Require().Equal(community.AllowsAllMembersToPinMessage(), true)
521+
522+
// Create a community chat
523+
orgChat := &protobuf.CommunityChat{
524+
Permissions: &protobuf.CommunityPermissions{
525+
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
526+
},
527+
Identity: &protobuf.ChatIdentity{
528+
DisplayName: "status-core",
529+
Emoji: "😎",
530+
Description: "status-core community chat",
531+
},
532+
}
533+
response, err = s.owner.CreateCommunityChat(community.ID(), orgChat)
534+
s.Require().NoError(err)
535+
s.Require().NotNil(response)
536+
s.Require().Len(response.Communities(), 1)
537+
s.Require().Len(response.Chats(), 1)
538+
chat := response.Chats()[0]
539+
s.Require().NotNil(chat)
540+
541+
s.advertiseCommunityTo(community, s.owner, s.bob)
542+
s.joinCommunity(community, s.owner, s.bob)
543+
544+
inputMessage := common.NewMessage()
545+
inputMessage.ChatId = chat.ID
546+
inputMessage.ContentType = protobuf.ChatMessage_TEXT_PLAIN
547+
inputMessage.Text = "message to be pinned"
548+
549+
sendResponse, err := s.bob.SendChatMessage(ctx, inputMessage)
550+
s.Require().NoError(err)
551+
s.Require().Len(sendResponse.Messages(), 1)
552+
553+
// bob should be able to pin the message
554+
pinMessage := common.NewPinMessage()
555+
pinMessage.ChatId = chat.ID
556+
pinMessage.MessageId = inputMessage.ID
557+
pinMessage.Pinned = true
558+
sendResponse, err = s.bob.SendPinMessage(ctx, pinMessage)
559+
s.Require().NoError(err)
560+
s.Require().Len(sendResponse.PinMessages(), 1)
561+
562+
// alice does not fully join the community,
563+
// so she should not be able to send the pin message
564+
s.advertiseCommunityTo(community, s.owner, s.alice)
565+
response, err = s.alice.SpectateCommunity(community.ID())
566+
s.Require().NotNil(response)
567+
s.Require().NoError(err)
568+
failedPinMessage := common.NewPinMessage()
569+
failedPinMessage.ChatId = chat.ID
570+
failedPinMessage.MessageId = inputMessage.ID
571+
failedPinMessage.Pinned = true
572+
sendResponse, err = s.alice.SendPinMessage(ctx, failedPinMessage)
573+
s.Require().Nil(sendResponse)
574+
s.Require().Error(err, "can't pin message")
575+
}
576+
501577
func (s *MessengerCommunitiesSuite) TestImportCommunity() {
502578
ctx := context.Background()
503579

protocol/messenger_pin_messages.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ func (m *Messenger) sendPinMessage(ctx context.Context, message *common.PinMessa
3636
if err != nil {
3737
return nil, err
3838
}
39+
3940
hasPermission := community.IsPrivilegedMember(&m.identity.PublicKey)
4041
pinMessageAllowed := community.AllowsAllMembersToPinMessage()
42+
canPost, err := community.CanPost(&m.identity.PublicKey, chat.CommunityChatID(), nil)
43+
if err != nil {
44+
return nil, err
45+
}
4146

42-
if !pinMessageAllowed && !hasPermission {
43-
return nil, errors.New("member can't pin message")
47+
if !canPost && !pinMessageAllowed && !hasPermission {
48+
return nil, errors.New("can't pin message")
4449
}
4550
}
4651

0 commit comments

Comments
 (0)