From e60d9f4e6455bf7496a35e9244ea9ad544ebe4f5 Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Fri, 3 Mar 2023 19:25:06 +0000 Subject: [PATCH] feat: add ability to delete/restore channels when deactivate/reactivate users (#244) --- user.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/user.go b/user.go index 166e75dc..55a3b74c 100644 --- a/user.go +++ b/user.go @@ -251,6 +251,7 @@ func (c *Client) ExportUser(ctx context.Context, targetID string) (*ExportUserRe type deactivateUserOptions struct { MarkMessagesDeleted bool `json:"mark_messages_deleted"` + MarkChannelsDeleted bool `json:"mark_channels_deleted"` CreatedByID string `json:"created_by_id"` } @@ -262,6 +263,12 @@ func DeactivateUserWithMarkMessagesDeleted() func(*deactivateUserOptions) { } } +func DeactivateUserWithMarkChannelsDeleted() func(*deactivateUserOptions) { + return func(opt *deactivateUserOptions) { + opt.MarkChannelsDeleted = true + } +} + func DeactivateUserWithCreatedBy(userID string) func(*deactivateUserOptions) { return func(opt *deactivateUserOptions) { opt.CreatedByID = userID @@ -286,8 +293,34 @@ func (c *Client) DeactivateUser(ctx context.Context, targetID string, options .. return &resp, err } +type deactivateUsersOptions struct { + UserIDs []string `json:"user_ids"` + deactivateUserOptions +} + +// DeactivateUsers deactivates the users with the given target user IDs. +func (c *Client) DeactivateUsers(ctx context.Context, targetIDs []string, options ...DeactivateUserOptions) (*Response, error) { + if len(targetIDs) == 0 { + return nil, errors.New("target IDs is empty") + } + + opts := &deactivateUsersOptions{ + UserIDs: targetIDs, + } + for _, fn := range options { + fn(&opts.deactivateUserOptions) + } + + p := path.Join("users", "deactivate") + + var resp Response + err := c.makeRequest(ctx, http.MethodPost, p, nil, opts, &resp) + return &resp, err +} + type reactivateUserOptions struct { RestoreMessages bool `json:"restore_messages"` + RestoreChannels bool `json:"restore_channels"` Name string `json:"name"` CreatedByID string `json:"created_by_id"` } @@ -300,6 +333,12 @@ func ReactivateUserWithRestoreMessages() func(*reactivateUserOptions) { } } +func ReactivateUserWithRestoreChannels() func(*reactivateUserOptions) { + return func(opt *reactivateUserOptions) { + opt.RestoreChannels = true + } +} + func ReactivateUserWithCreatedBy(userID string) func(*reactivateUserOptions) { return func(opt *reactivateUserOptions) { opt.CreatedByID = userID @@ -330,6 +369,31 @@ func (c *Client) ReactivateUser(ctx context.Context, targetID string, options .. return &resp, err } +type reactivateUsersOptions struct { + UserIDs []string `json:"user_ids"` + reactivateUserOptions +} + +// ReactivateUsers reactivates deactivated users with the given target user IDs. +func (c *Client) ReactivateUsers(ctx context.Context, targetIDs []string, options ...ReactivateUserOptions) (*Response, error) { + if len(targetIDs) == 0 { + return nil, errors.New("target IDs is empty") + } + + opts := &reactivateUsersOptions{ + UserIDs: targetIDs, + } + for _, fn := range options { + fn(&opts.reactivateUserOptions) + } + + p := path.Join("users", "reactivate") + + var resp Response + err := c.makeRequest(ctx, http.MethodPost, p, nil, opts, &resp) + return &resp, err +} + type deleteUserOptions struct { MarkMessagesDeleted string HardDelete string