Skip to content

Commit 2ee8147

Browse files
authored
refactor: share reaction item decoding (#1700)
Share one private reaction-item response type and conversion method between `reactions.get` and `reactions.list`. Remove the duplicated JSON structure and message/file/comment conversion switch, reducing the code by 25 lines. Preserve returned items, reaction details, list ordering, and pagination. No public API changes.
1 parent b80941d commit 2ee8147

1 file changed

Lines changed: 8 additions & 33 deletions

File tree

‎reactions.go‎

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func NewGetReactionsParameters() GetReactionsParameters {
3333
}
3434

3535
type getReactionsResponseFull struct {
36+
reactedItemResponse
37+
SlackResponse
38+
}
39+
40+
type reactedItemResponse struct {
3641
Type string
3742
Channel string `json:"channel,omitempty"` // channel is at the root level for message types
3843
M struct {
@@ -46,10 +51,9 @@ type getReactionsResponseFull struct {
4651
*Comment
4752
Reactions []ItemReaction
4853
} `json:"comment"`
49-
SlackResponse
5054
}
5155

52-
func (res getReactionsResponseFull) extractReactedItem() ReactedItem {
56+
func (res reactedItemResponse) extractReactedItem() ReactedItem {
5357
item := ReactedItem{}
5458
item.Type = res.Type
5559

@@ -93,44 +97,15 @@ func NewListReactionsParameters() ListReactionsParameters {
9397
}
9498

9599
type listReactionsResponseFull struct {
96-
Items []struct {
97-
Type string
98-
Channel string
99-
M struct {
100-
*Message
101-
} `json:"message"`
102-
F struct {
103-
*File
104-
Reactions []ItemReaction
105-
} `json:"file"`
106-
FC struct {
107-
*Comment
108-
Reactions []ItemReaction
109-
} `json:"comment"`
110-
}
100+
Items []reactedItemResponse
111101
SlackResponse
112102
ResponseMetadata `json:"response_metadata"`
113103
}
114104

115105
func (res listReactionsResponseFull) extractReactedItems() []ReactedItem {
116106
items := make([]ReactedItem, len(res.Items))
117107
for i, input := range res.Items {
118-
item := ReactedItem{}
119-
item.Type = input.Type
120-
switch input.Type {
121-
case "message":
122-
item.Channel = input.Channel
123-
item.Message = input.M.Message
124-
item.Reactions = input.M.Reactions
125-
case "file":
126-
item.File = input.F.File
127-
item.Reactions = input.F.Reactions
128-
case "file_comment":
129-
item.File = input.F.File
130-
item.Comment = input.FC.Comment
131-
item.Reactions = input.FC.Reactions
132-
}
133-
items[i] = item
108+
items[i] = input.extractReactedItem()
134109
}
135110
return items
136111
}

0 commit comments

Comments
 (0)