Skip to content

Commit

Permalink
feat: sending system messages (#248)
Browse files Browse the repository at this point in the history
* test: added a test for system messages

* test: updated test

* test: updating test

* feat: ability to send system message

* test: updated test

* test: fixing test

* test: fix test

* fix: test
  • Loading branch information
totalimmersion authored Mar 30, 2023
1 parent a74a643 commit 468c56e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
24 changes: 22 additions & 2 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ func TestChannel_GetReplies(t *testing.T) {

msg = resp.Message

reply := &Message{Text: "test reply", ParentID: msg.ID, Type: MessageTypeReply}
_, err = ch.SendMessage(ctx, reply, randomUser(t, c).ID)
reply := &Message{Text: "test reply", ParentID: msg.ID}
resp, err = ch.SendMessage(ctx, reply, randomUser(t, c).ID)
require.NoError(t, err, "send reply")
require.Equal(t, MessageTypeReply, resp.Message.Type, "message type is reply")

repliesResp, err := ch.GetReplies(ctx, msg.ID, nil)
require.NoError(t, err, "get replies")
Expand Down Expand Up @@ -361,6 +362,25 @@ func TestChannel_SendMessage(t *testing.T) {
assert.True(t, msg2.Silent, "message silent flag is set")
}

func TestChannel_SendSystemMessage(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
user := randomUser(t, c)
msg := &Message{
Text: "test message",
Type: MessageTypeSystem,
}

resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")

// check that message was updated
msg = resp.Message
assert.NotEmpty(t, msg.ID, "message has ID")
assert.Equal(t, MessageTypeSystem, msg.Type, "message type is system")
}

func TestChannel_Truncate(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
Expand Down
23 changes: 12 additions & 11 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (m *Message) toRequest() messageRequest {

req.Message = messageRequestMessage{
Text: m.Text,
Type: m.Type,
Attachments: m.Attachments,
User: messageRequestUser{ID: m.User.ID},
ExtraData: m.ExtraData,
Expand Down Expand Up @@ -121,17 +122,17 @@ type messageRequest struct {
}

type messageRequestMessage struct {
Text string `json:"text"`
Attachments []*Attachment `json:"attachments"`
User messageRequestUser `json:"user"`
MentionedUsers []string `json:"mentioned_users"`
ParentID string `json:"parent_id"`
ShowInChannel bool `json:"show_in_channel"`
Silent bool `json:"silent"`
QuotedMessageID string `json:"quoted_message_id"`
Pinned bool `json:"pinned"`

ExtraData map[string]interface{} `json:"-"`
Text string `json:"text"`
Type MessageType `json:"type" validate:"omitempty,oneof=system"`
Attachments []*Attachment `json:"attachments"`
User messageRequestUser `json:"user"`
MentionedUsers []string `json:"mentioned_users"`
ParentID string `json:"parent_id"`
ShowInChannel bool `json:"show_in_channel"`
Silent bool `json:"silent"`
QuotedMessageID string `json:"quoted_message_id"`
Pinned bool `json:"pinned"`
ExtraData map[string]interface{} `json:"-"`
}

type messageRequestForJSON messageRequestMessage
Expand Down

0 comments on commit 468c56e

Please sign in to comment.