Skip to content

Commit

Permalink
feat: adding pending property to message (#251)
Browse files Browse the repository at this point in the history
* feat: adding pending property to message

* feat: added commitMessage endpoint

* feat: added MarkMessagesPending to channel config

* fix: code comment
  • Loading branch information
vishalnarkhede committed Jul 3, 2023
1 parent 27834f3 commit b70d16c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
35 changes: 26 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ type AppSettings struct {
Policies map[string][]Policy `json:"policies"`
Grants map[string][]string `json:"grants,omitempty"`

MultiTenantEnabled *bool `json:"multi_tenant_enabled,omitempty"`
AsyncURLEnrichEnabled *bool `json:"async_url_enrich_enabled,omitempty"`
AutoTranslationEnabled *bool `json:"auto_translation_enabled,omitempty"`
RemindersInterval int `json:"reminders_interval,omitempty"`
UserSearchDisallowedRoles []string `json:"user_search_disallowed_roles,omitempty"`
EnforceUniqueUsernames *string `json:"enforce_unique_usernames,omitempty"`
ChannelHideMembersOnly *bool `json:"channel_hide_members_only,omitempty"`
MultiTenantEnabled *bool `json:"multi_tenant_enabled,omitempty"`
AsyncURLEnrichEnabled *bool `json:"async_url_enrich_enabled,omitempty"`
AutoTranslationEnabled *bool `json:"auto_translation_enabled,omitempty"`
RemindersInterval int `json:"reminders_interval,omitempty"`
UserSearchDisallowedRoles []string `json:"user_search_disallowed_roles,omitempty"`
EnforceUniqueUsernames *string `json:"enforce_unique_usernames,omitempty"`
ChannelHideMembersOnly *bool `json:"channel_hide_members_only,omitempty"`
AsyncModerationConfig *AsyncModerationConfiguration `json:"async_moderation_config,omitempty"`
}

func (a *AppSettings) SetDisableAuth(b bool) *AppSettings {
Expand Down Expand Up @@ -85,10 +86,25 @@ func (a *AppSettings) SetGrants(g map[string][]string) *AppSettings {
return a
}

func (a *AppSettings) SetAsyncModerationConfig(c AsyncModerationConfiguration) *AppSettings {
a.AsyncModerationConfig = &c
return a
}

func NewAppSettings() *AppSettings {
return &AppSettings{}
}

type AsyncModerationCallback struct {
Mode string `json:"mode"`
ServerUrl string `json:"server_url"`
}

type AsyncModerationConfiguration struct {
Callback *AsyncModerationCallback `json:"callback,omitempty"`
Timeout int `json:"timeout_ms,omitempty"`
}

type FileUploadConfig struct {
AllowedFileExtensions []string `json:"allowed_file_extensions,omitempty"`
BlockedFileExtensions []string `json:"blocked_file_extensions,omitempty"`
Expand Down Expand Up @@ -180,8 +196,9 @@ func (c *Client) GetAppSettings(ctx context.Context) (*AppResponse, error) {

// UpdateAppSettings makes request to update app settings
// Example of usage:
// settings := NewAppSettings().SetDisableAuth(true)
// err := client.UpdateAppSettings(settings)
//
// settings := NewAppSettings().SetDisableAuth(true)
// err := client.UpdateAppSettings(settings)
func (c *Client) UpdateAppSettings(ctx context.Context, settings *AppSettings) (*Response, error) {
var resp Response
err := c.makeRequest(ctx, http.MethodPatch, "app", nil, settings, &resp)
Expand Down
19 changes: 19 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ func TestClient_UpdateAppSettings(t *testing.T) {
require.NoError(t, err)
}

func TestClient_CheckAsyncModeConfig(t *testing.T) {
c := initClient(t)
ctx := context.Background()

settings := NewAppSettings().
SetAsyncModerationConfig(
AsyncModerationConfiguration{
Callback: &AsyncModerationCallback{
Mode: "CALLBACK_MODE_REST",
ServerUrl: "https://example.com/gosdk",
},
Timeout: 10000,
},
)

_, err := c.UpdateAppSettings(ctx, settings)
require.NoError(t, err)
}

func TestClient_UpdateAppSettingsClearing(t *testing.T) {
c := initClient(t)
ctx := context.Background()
Expand Down
7 changes: 4 additions & 3 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ type Channel struct {
MemberCount int `json:"member_count"`
Members []*ChannelMember `json:"members"`

Messages []*Message `json:"messages"`
PinnedMessages []*Message `json:"pinned_messages"`
Read []*ChannelRead `json:"read"`
Messages []*Message `json:"messages"`
PinnedMessages []*Message `json:"pinned_messages"`
PendingMessages []*Message `json:"pending_messages"`
Read []*ChannelRead `json:"read"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Expand Down
5 changes: 3 additions & 2 deletions channel_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type ChannelConfig struct {
CustomEvents bool `json:"custom_events"`

// number of days to keep messages, must be MessageRetentionForever or numeric string
MessageRetention string `json:"message_retention"`
MaxMessageLength int `json:"max_message_length"`
MessageRetention string `json:"message_retention"`
MaxMessageLength int `json:"max_message_length"`
MarkMessagesPending bool `json:"mark_messages_pending"`

Automod modType `json:"automod"` // disabled, simple or AI
ModBehavior modBehaviour `json:"automod_behavior"`
Expand Down
3 changes: 3 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ func TestChannel_PartialUpdate(t *testing.T) {
_, err = ch.PartialUpdate(ctx, PartialUpdate{
Set: map[string]interface{}{
"color": "red",
"config_override": map[string]interface{}{
"typing_events": false,
},
},
Unset: []string{"age"},
})
Expand Down
16 changes: 16 additions & 0 deletions channel_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ func TestClient_ListChannelTypes(t *testing.T) {
assert.Contains(t, resp.ChannelTypes, ct.Name)
}

func TestClient_UpdateChannelTypeMarkMessagesPending(t *testing.T) {
c := initClient(t)
ct := prepareChannelType(t, c)
ctx := context.Background()

// default is off
require.False(t, ct.MarkMessagesPending)

_, err := c.UpdateChannelType(ctx, ct.Name, map[string]interface{}{"mark_messages_pending": true})
require.NoError(t, err)

resp, err := c.GetChannelType(ctx, ct.Name)
require.NoError(t, err)
require.True(t, resp.ChannelType.MarkMessagesPending)
}

func TestClient_UpdateChannelTypePushNotifications(t *testing.T) {
c := initClient(t)
ct := prepareChannelType(t, c)
Expand Down
15 changes: 14 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type messageRequest struct {
Message messageRequestMessage `json:"message"`
SkipPush bool `json:"skip_push,omitempty"`
SkipEnrichURL bool `json:"skip_enrich_url,omitempty"`
Pending bool `json:"pending,omitempty"`
IsPendingMessage bool `json:"is_pending_message,omitempty"`
PendingMessageMetadata map[string]string `json:"pending_message_metadata,omitempty"`
}
Expand Down Expand Up @@ -220,7 +221,7 @@ func MessageSkipEnrichURL(r *messageRequest) {
// MessagePending is a flag that makes this a pending message
func MessagePending(r *messageRequest) {
if r != nil {
r.IsPendingMessage = true
r.Pending = true
}
}

Expand Down Expand Up @@ -364,6 +365,18 @@ func (c *Client) UnPinMessage(ctx context.Context, msgID, userID string) (*Messa
return c.PartialUpdateMessage(ctx, msgID, &request)
}

func (c *Client) CommitMessage(ctx context.Context, msgID string) (*Response, error) {
if msgID == "" {
return nil, errors.New("message ID must be not empty")
}

p := path.Join("messages", url.PathEscape(msgID), "commit")
var resp Response
err := c.makeRequest(ctx, http.MethodPost, p, nil, nil, &resp)
return &resp, err

}

// DeleteMessage soft deletes the message with given msgID.
func (c *Client) DeleteMessage(ctx context.Context, msgID string) (*Response, error) {
return c.deleteMessage(ctx, msgID, false)
Expand Down
3 changes: 3 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func TestClient_SendMessage_Pending(t *testing.T) {
gotMsg, err := c.GetMessage(ctx, messageResp.Message.ID)
require.NoError(t, err)
require.Equal(t, metadata, gotMsg.PendingMessageMetadata)

_, err = c.CommitMessage(ctx, messageResp.Message.ID)
require.NoError(t, err)
}

func TestClient_SendMessage_SkipEnrichURL(t *testing.T) {
Expand Down

0 comments on commit b70d16c

Please sign in to comment.