Skip to content

Commit

Permalink
feat: added support for MemberPartialUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
totalimmersion committed Dec 4, 2024
1 parent 60b5fab commit 3692d12
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
60 changes: 54 additions & 6 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,48 @@ type ChannelMember struct {
User *User `json:"user,omitempty"`
IsModerator bool `json:"is_moderator,omitempty"`

Invited bool `json:"invited,omitempty"`
InviteAcceptedAt *time.Time `json:"invite_accepted_at,omitempty"`
InviteRejectedAt *time.Time `json:"invite_rejected_at,omitempty"`
Role string `json:"role,omitempty"`
ArchivedAt *time.Time `json:"archived_at,omitempty"`
PinnedAt *time.Time `json:"pinned_at,omitempty"`
Invited bool `json:"invited,omitempty"`
InviteAcceptedAt *time.Time `json:"invite_accepted_at,omitempty"`
InviteRejectedAt *time.Time `json:"invite_rejected_at,omitempty"`
Status string `json:"status,omitempty"`
Role string `json:"role,omitempty"`
ChannelRole string `json:"channel_role"`
Banned bool `json:"banned"`
BanExpires *time.Time `json:"ban_expires,omitempty"`
ShadowBanned bool `json:"shadow_banned"`
ArchivedAt *time.Time `json:"archived_at,omitempty"`
PinnedAt *time.Time `json:"pinned_at,omitempty"`
NotificationsMuted bool `json:"notifications_muted"`

ExtraData map[string]interface{} `json:"-"`

CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

type channelMemberForJSON ChannelMember

// UnmarshalJSON implements json.Unmarshaler.
func (m *ChannelMember) UnmarshalJSON(data []byte) error {
var m2 channelMemberForJSON
if err := json.Unmarshal(data, &m2); err != nil {
return err
}
*m = ChannelMember(m2)

if err := json.Unmarshal(data, &m.ExtraData); err != nil {
return err
}

removeFromMap(m.ExtraData, *m)
return nil
}

// MarshalJSON implements json.Marshaler.
func (m ChannelMember) MarshalJSON() ([]byte, error) {
return addToMapAndMarshal(m.ExtraData, channelMemberForJSON(m))
}

type Channel struct {
ID string `json:"id"`
Type string `json:"type"`
Expand Down Expand Up @@ -863,6 +894,7 @@ type ChannelMemberResponse struct {
Response
}

// Pin pins the channel for the user.
func (ch *Channel) Pin(ctx context.Context, userID string) (*ChannelMemberResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
Expand All @@ -881,6 +913,7 @@ func (ch *Channel) Pin(ctx context.Context, userID string) (*ChannelMemberRespon
return resp, err
}

// Unpin unpins the channel for the user.
func (ch *Channel) Unpin(ctx context.Context, userID string) (*ChannelMemberResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
Expand All @@ -899,6 +932,7 @@ func (ch *Channel) Unpin(ctx context.Context, userID string) (*ChannelMemberResp
return resp, err
}

// Archive archives the channel for the user.
func (ch *Channel) Archive(ctx context.Context, userID string) (*ChannelMemberResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
Expand All @@ -917,6 +951,7 @@ func (ch *Channel) Archive(ctx context.Context, userID string) (*ChannelMemberRe
return resp, err
}

// Unarchive unarchives the channel for the user.
func (ch *Channel) Unarchive(ctx context.Context, userID string) (*ChannelMemberResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
Expand All @@ -934,3 +969,16 @@ func (ch *Channel) Unarchive(ctx context.Context, userID string) (*ChannelMember
err := ch.client.makeRequest(ctx, http.MethodPatch, p, nil, data, resp)
return resp, err
}

// PartialUpdateMember set and unset specific fields when it is necessary to retain additional custom data fields on the object. AKA a patch style update.
func (ch *Channel) PartialUpdateMember(ctx context.Context, userID string, update PartialUpdate) (*ChannelMemberResponse, error) {
if userID == "" {
return nil, errors.New("user ID must be not empty")
}

p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "member", url.PathEscape(userID))

resp := &ChannelMemberResponse{}
err := ch.client.makeRequest(ctx, http.MethodPatch, p, nil, update, resp)
return resp, err
}
35 changes: 35 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,41 @@ func TestChannel_PartialUpdate(t *testing.T) {
require.Nil(t, ch.ExtraData["age"])
}

func TestChannel_MemberPartialUpdate(t *testing.T) {
c := initClient(t)
users := randomUsers(t, c, 5)
ctx := context.Background()

members := make([]string, 0, len(users))
for i := range users {
members = append(members, users[i].ID)
}

req := &ChannelRequest{Members: members}
resp, err := c.CreateChannel(ctx, "team", randomString(12), randomUser(t, c).ID, req)
require.NoError(t, err)

ch := resp.Channel
member, err := ch.PartialUpdateMember(ctx, members[0], PartialUpdate{
Set: map[string]interface{}{
"color": "red",
},
Unset: []string{"age"},
})
require.NoError(t, err)
require.Equal(t, "red", member.ChannelMember.ExtraData["color"])

member, err = ch.PartialUpdateMember(ctx, members[0], PartialUpdate{
Set: map[string]interface{}{
"age": "18",
},
Unset: []string{"color"},
})
require.NoError(t, err)
require.Equal(t, "18", member.ChannelMember.ExtraData["age"])
require.Nil(t, member.ChannelMember.ExtraData["color"])
}

func TestChannel_SendFile(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
Expand Down

0 comments on commit 3692d12

Please sign in to comment.