Skip to content

Commit 282014f

Browse files
slavabobikSlava Bobik
andauthored
feat(message): add MessageImmediate option for setting pending=false (#320)
* added message pending options * added WithPendion option --------- Co-authored-by: Slava Bobik <[email protected]>
1 parent 26ce6d0 commit 282014f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

message.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type messageRequest struct {
123123
Message messageRequestMessage `json:"message"`
124124
SkipPush bool `json:"skip_push,omitempty"`
125125
SkipEnrichURL bool `json:"skip_enrich_url,omitempty"`
126-
Pending bool `json:"pending,omitempty"`
126+
Pending *bool `json:"pending,omitempty"`
127127
IsPendingMessage bool `json:"is_pending_message,omitempty"`
128128
PendingMessageMetadata map[string]string `json:"pending_message_metadata,omitempty"`
129129
KeepChannelHidden bool `json:"keep_channel_hidden,omitempty"`
@@ -225,10 +225,18 @@ func MessageSkipEnrichURL(r *messageRequest) {
225225
}
226226
}
227227

228-
// MessagePending is a flag that makes this a pending message.
228+
// MessagePending sets a flag that marks this message as pending.
229+
// Deprecated: Use WithPending(true) instead.
229230
func MessagePending(r *messageRequest) {
230-
if r != nil {
231-
r.Pending = true
231+
WithPending(true)(r)
232+
}
233+
234+
// WithPending returns an option that sets the Pending flag to the specified value.
235+
func WithPending(pending bool) SendMessageOption {
236+
return func(r *messageRequest) {
237+
if r != nil {
238+
r.Pending = &pending
239+
}
232240
}
233241
}
234242

message_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ func TestClient_SendMessage_Pending(t *testing.T) {
6969
require.NoError(t, err)
7070
}
7171

72+
func TestClient_SendMessage_WithPendingFalse(t *testing.T) {
73+
c := initClient(t)
74+
user := randomUser(t, c)
75+
76+
ctx := context.Background()
77+
78+
ch := initChannel(t, c, user.ID)
79+
resp1, err := c.CreateChannel(ctx, ch.Type, ch.ID, user.ID, nil)
80+
require.NoError(t, err)
81+
82+
msg := &Message{Text: "message with WithPending(false) - non-pending message"}
83+
messageResp, err := resp1.Channel.SendMessage(ctx, msg, user.ID, WithPending(false))
84+
require.NoError(t, err)
85+
86+
// Get the message to verify it's not in pending state
87+
gotMsg, err := c.GetMessage(ctx, messageResp.Message.ID)
88+
require.NoError(t, err)
89+
90+
// No need to commit the message as it's already in non-pending state
91+
// The message should be immediately available without requiring a commit
92+
require.NotNil(t, gotMsg.Message)
93+
require.Equal(t, msg.Text, gotMsg.Message.Text)
94+
}
95+
7296
func TestClient_SendMessage_SkipEnrichURL(t *testing.T) {
7397
c := initClient(t)
7498
user := randomUser(t, c)

0 commit comments

Comments
 (0)