Skip to content

Commit

Permalink
Improve lint and comments (#83)
Browse files Browse the repository at this point in the history
* explicit checks
* better adaptation of golint rules
* improve config and bump version
  • Loading branch information
ferhatelmas authored Jun 25, 2020
1 parent 9f75b67 commit b26b5b3
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 86 deletions.
60 changes: 50 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
# all available settings of specific linters
linters-settings:
gofmt:
simplify: true
goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: github.com/GetStream/stream-chat-go
errcheck:
check-type-assertions: false
check-blank: false

gocritic:
disabled-checks:
- unnamedResult
- whyNoLint
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
settings:
hugeParam:
sizeThreshold: 364
rangeValCopy:
sizeThreshold: 364
skipTestFuncs: true
govet:
enable-all: true
disable:
- shadow
run:
skip-dirs:
- hack
skip-files:
- ".*_easyjson\\.go"
# include test files
tests: true

linters:
enable-all: true
disable:
- gomnd
- stylecheck
- wsl
- whitespace
disable-all: true
enable:
- asciicheck
- bodyclose
- deadcode
- dogsled
- dupl
- errcheck
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- prealloc
- staticcheck
- structcheck
- unconvert
- unparam
- unused
- varcheck
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type appResponse struct {
App *AppConfig `json:"app"`
}

// GetAppConfig returns app settings
// GetAppConfig returns app settings.
func (c *Client) GetAppConfig() (*AppConfig, error) {
var resp appResponse

Expand Down
52 changes: 25 additions & 27 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (q queryResponse) updateChannel(ch *Channel) {
}
}

// query makes request to channel api and updates channel internal state
// query makes request to channel api and updates channel internal state.
func (ch *Channel) query(options, data map[string]interface{}) (err error) {
payload := map[string]interface{}{
"state": true,
Expand Down Expand Up @@ -160,14 +160,14 @@ func (ch *Channel) Delete() error {
return ch.client.makeRequest(http.MethodDelete, p, nil, nil, nil)
}

// Truncate removes all messages from the channel
// Truncate removes all messages from the channel.
func (ch *Channel) Truncate() error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "truncate")

return ch.client.makeRequest(http.MethodPost, p, nil, nil, nil)
}

// AddMembers adds members with given user IDs to the channel
// AddMembers adds members with given user IDs to the channel.
func (ch *Channel) AddMembers(userIDs []string, message *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
Expand All @@ -186,7 +186,7 @@ func (ch *Channel) AddMembers(userIDs []string, message *Message) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// RemoveMembers deletes members with given IDs from the channel
// RemoveMembers deletes members with given IDs from the channel.
func (ch *Channel) RemoveMembers(userIDs []string, message *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
Expand All @@ -213,17 +213,17 @@ func (ch *Channel) RemoveMembers(userIDs []string, message *Message) error {
return nil
}

// AddModerators adds moderators with given IDs to the channel
// AddModerators adds moderators with given IDs to the channel.
func (ch *Channel) AddModerators(userIDs ...string) error {
return ch.addModerators(userIDs, nil)
}

// AddModerators adds moderators with given IDs to the channel and produce system message
// AddModerators adds moderators with given IDs to the channel and produce system message.
func (ch *Channel) AddModeratorsWithMessage(userIDs []string, msg *Message) error {
return ch.addModerators(userIDs, msg)
}

// AddModerators adds moderators with given IDs to the channel
// AddModerators adds moderators with given IDs to the channel.
func (ch *Channel) addModerators(userIDs []string, msg *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
Expand All @@ -242,17 +242,17 @@ func (ch *Channel) addModerators(userIDs []string, msg *Message) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// InviteMembers invites users with given IDs to the channel
// InviteMembers invites users with given IDs to the channel.
func (ch *Channel) InviteMembers(userIDs ...string) error {
return ch.inviteMembers(userIDs, nil)
}

// InviteMembers invites users with given IDs to the channel and produce system message
// InviteMembers invites users with given IDs to the channel and produce system message.
func (ch *Channel) InviteMembersWithMessage(userIDs []string, msg *Message) error {
return ch.inviteMembers(userIDs, msg)
}

// InviteMembers invites users with given IDs to the channel
// InviteMembers invites users with given IDs to the channel.
func (ch *Channel) inviteMembers(userIDs []string, msg *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
Expand All @@ -271,17 +271,17 @@ func (ch *Channel) inviteMembers(userIDs []string, msg *Message) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// DemoteModerators moderators with given IDs from the channel
// DemoteModerators moderators with given IDs from the channel.
func (ch *Channel) DemoteModerators(userIDs ...string) error {
return ch.demoteModerators(userIDs, nil)
}

// DemoteModerators moderators with given IDs from the channel and produce system message
// DemoteModerators moderators with given IDs from the channel and produce system message.
func (ch *Channel) DemoteModeratorsWithMessage(userIDs []string, msg *Message) error {
return ch.demoteModerators(userIDs, msg)
}

// DemoteModerators moderators with given IDs from the channel
// DemoteModerators moderators with given IDs from the channel.
func (ch *Channel) demoteModerators(userIDs []string, msg *Message) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
Expand All @@ -300,8 +300,8 @@ func (ch *Channel) demoteModerators(userIDs []string, msg *Message) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// MarkRead send the mark read event for user with given ID, only works if the `read_events` setting is enabled
// options: additional data, ie {"messageID": last_messageID}
// MarkRead send the mark read event for user with given ID, only works if the `read_events` setting is enabled
// options: additional data, ie {"messageID": last_messageID}
func (ch *Channel) MarkRead(userID string, options map[string]interface{}) error {
switch {
case userID == "":
Expand Down Expand Up @@ -336,7 +336,7 @@ func (ch *Channel) BanUser(targetID, userID string, options map[string]interface
return ch.client.BanUser(targetID, userID, options)
}

// UnBanUser removes the ban for target user ID on this channel
// UnBanUser removes the ban for target user ID on this channel.
func (ch *Channel) UnBanUser(targetID string, options map[string]string) error {
switch {
case targetID == "":
Expand All @@ -351,7 +351,7 @@ func (ch *Channel) UnBanUser(targetID string, options map[string]string) error {
return ch.client.UnBanUser(targetID, options)
}

// Query fills channel info without state (messages, members, reads)
// Query fills channel info without state (messages, members, reads).
func (ch *Channel) Query(data map[string]interface{}) error {
options := map[string]interface{}{
"watch": false,
Expand All @@ -362,7 +362,7 @@ func (ch *Channel) Query(data map[string]interface{}) error {
return ch.query(options, data)
}

// Show makes channel visible for userID
// Show makes channel visible for userID.
func (ch *Channel) Show(userID string) error {
data := map[string]interface{}{
"user_id": userID,
Expand All @@ -373,12 +373,12 @@ func (ch *Channel) Show(userID string) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// Hide makes channel hidden for userID
// Hide makes channel hidden for userID.
func (ch *Channel) Hide(userID string) error {
return ch.hide(userID, false)
}

// HideWithHistoryClear clear marks channel as hidden and remove all messages for user
// HideWithHistoryClear clear marks channel as hidden and remove all messages for user.
func (ch *Channel) HideWithHistoryClear(userID string) error {
return ch.hide(userID, true)
}
Expand All @@ -394,7 +394,7 @@ func (ch *Channel) hide(userID string, clearHistory bool) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// CreateChannel creates new channel of given type and id or returns already created one
// CreateChannel creates new channel of given type and id or returns already created one.
func (c *Client) CreateChannel(chanType, chanID, userID string, data map[string]interface{}) (*Channel, error) {
_, membersPresent := data["members"]

Expand Down Expand Up @@ -441,21 +441,21 @@ type SendFileRequest struct {
ContentType string
}

// SendFile sends file to the channel. Returns file url or error
// SendFile sends file to the channel. Returns file url or error.
func (ch *Channel) SendFile(request SendFileRequest) (string, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "file")

return ch.client.sendFile(p, request)
}

// SendFile sends image to the channel. Returns file url or error
// SendFile sends image to the channel. Returns file url or error.
func (ch *Channel) SendImage(request SendFileRequest) (string, error) {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "image")

return ch.client.sendFile(p, request)
}

// DeleteFile removes uploaded file
// DeleteFile removes uploaded file.
func (ch *Channel) DeleteFile(location string) error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "file")

Expand All @@ -465,7 +465,7 @@ func (ch *Channel) DeleteFile(location string) error {
return ch.client.makeRequest(http.MethodDelete, p, params, nil, nil)
}

// DeleteImage removes uploaded image
// DeleteImage removes uploaded image.
func (ch *Channel) DeleteImage(location string) error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "image")

Expand Down Expand Up @@ -548,8 +548,6 @@ func (ch *Channel) Unmute(userID string) error {
return ch.client.makeRequest(http.MethodPost, "moderation/unmute/channel", nil, data, nil)
}

//nolint: godox
// todo: cleanup this
func (ch *Channel) refresh() error {
options := map[string]interface{}{
"watch": false,
Expand Down
5 changes: 2 additions & 3 deletions channel_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package stream_chat //nolint: golint

// ChannelConfig is the configuration for a channel
// ChannelConfig is the configuration for a channel.
type ChannelConfig struct {
Name string `json:"name"`

Expand All @@ -25,8 +25,7 @@ type ChannelConfig struct {
ModBehavior modBehaviour `json:"automod_behavior"`
}

// DefaultChannelConfig is the default channel configuration
// nolint: gochecknoglobals
// DefaultChannelConfig is the default channel configuration.
var DefaultChannelConfig = ChannelConfig{
Automod: AutoModDisabled,
ModBehavior: ModBehaviourFlag,
Expand Down
8 changes: 4 additions & 4 deletions channel_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (ct *ChannelType) toRequest() channelTypeRequest {
return req
}

// NewChannelType returns initialized ChannelType with default values
// NewChannelType returns initialized ChannelType with default values.
func NewChannelType(name string) *ChannelType {
ct := &ChannelType{ChannelConfig: DefaultChannelConfig}
ct.Name = name
Expand All @@ -82,7 +82,7 @@ type channelTypeResponse struct {
ChannelTypes map[string]*ChannelType `json:"channel_types"`
}

// CreateChannelType adds new channel type
// CreateChannelType adds new channel type.
func (c *Client) CreateChannelType(chType *ChannelType) (*ChannelType, error) {
if chType == nil {
return nil, errors.New("channel type is nil")
Expand All @@ -105,7 +105,7 @@ func (c *Client) CreateChannelType(chType *ChannelType) (*ChannelType, error) {
return resp.ChannelType, nil
}

// GetChannelType returns information about channel type
// GetChannelType returns information about channel type.
func (c *Client) GetChannelType(chanType string) (*ChannelType, error) {
if chanType == "" {
return nil, errors.New("channel type is empty")
Expand All @@ -120,7 +120,7 @@ func (c *Client) GetChannelType(chanType string) (*ChannelType, error) {
return &ct, err
}

// ListChannelTypes returns all channel types
// ListChannelTypes returns all channel types.
func (c *Client) ListChannelTypes() (map[string]*ChannelType, error) {
var resp channelTypeResponse

Expand Down
11 changes: 5 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (c *Client) createToken(claims jwt.Claims) (string, error) {
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(c.apiSecret)
}

// VerifyWebhook validates if hmac signature is correct for message body
// VerifyWebhook validates if hmac signature is correct for message body.
func (c *Client) VerifyWebhook(body, signature []byte) (valid bool) {
mac := hmac.New(crypto.SHA256.New, c.apiSecret)
_, _ = mac.Write(body)
Expand All @@ -169,20 +169,19 @@ type sendFileResponse struct {
File string `json:"file"`
}

//nolint:gochecknoglobals
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")

func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}

// this adds possible to set content type
// this makes possible to set content type.
type multipartForm struct {
*multipart.Writer
}

// CreateFormFile is a convenience wrapper around CreatePart. It creates
// a new form-data header with the provided field name, file name and content type
// a new form-data header with the provided field name, file name and content type.
func (form *multipartForm) CreateFormFile(fieldName, filename, contentType string) (io.Writer, error) {
h := make(textproto.MIMEHeader)

Expand Down Expand Up @@ -274,7 +273,7 @@ func (c *Client) sendFile(link string, opts SendFileRequest) (string, error) {
return resp.File, err
}

// NewClient creates new stream chat api client
// NewClient creates new stream chat api client.
func NewClient(apiKey string, apiSecret []byte) (*Client, error) {
switch {
case apiKey == "":
Expand Down Expand Up @@ -304,7 +303,7 @@ func NewClient(apiKey string, apiSecret []byte) (*Client, error) {

// Channel prepares a Channel object for future API calls. This does not in and
// of itself call the API.
func (c *Client) Channel(channelType string, channelID string) *Channel {
func (c *Client) Channel(channelType, channelID string) *Channel {
return &Channel{
client: c,

Expand Down
Loading

0 comments on commit b26b5b3

Please sign in to comment.