Skip to content

Commit

Permalink
Add comments (still no nearly enough)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlopes committed Jan 27, 2015
1 parent 1335acb commit 55d471b
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 30 deletions.
26 changes: 22 additions & 4 deletions channels.go
Expand Up @@ -16,18 +16,21 @@ type channelResponseFull struct {
SlackResponse
}

// ChannelTopic contains information about the channel topic
type ChannelTopic struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet JSONTime `json:"last_set"`
}

// ChannelPurpose contains information about the channel purpose
type ChannelPurpose struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet JSONTime `json:"last_set"`
}

// Channel contains information about the channel
type Channel struct {
Id string `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -59,6 +62,7 @@ func channelRequest(path string, values url.Values, debug bool) (*channelRespons
return response, nil
}

// ArchiveChannel archives the given channel
func (api *Slack) ArchiveChannel(channelId string) error {
values := url.Values{
"token": {api.config.token},
Expand All @@ -71,6 +75,7 @@ func (api *Slack) ArchiveChannel(channelId string) error {
return nil
}

// UnarchiveChannel unarchives the given channel
func (api *Slack) UnarchiveChannel(channelId string) error {
values := url.Values{
"token": {api.config.token},
Expand All @@ -83,6 +88,7 @@ func (api *Slack) UnarchiveChannel(channelId string) error {
return nil
}

// CreateChannel creates a channel with the given name and returns a *Channel
func (api *Slack) CreateChannel(channel string) (*Channel, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -95,6 +101,7 @@ func (api *Slack) CreateChannel(channel string) (*Channel, error) {
return &response.Channel, nil
}

// GetChannelHistory retrieves the channel history
func (api *Slack) GetChannelHistory(channelId string, params HistoryParameters) (*History, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -117,6 +124,7 @@ func (api *Slack) GetChannelHistory(channelId string, params HistoryParameters)
return &channelHistory, nil
}

// GetChannelInfo retrieves the given channel
func (api *Slack) GetChannelInfo(channelId string) (*Channel, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -129,6 +137,7 @@ func (api *Slack) GetChannelInfo(channelId string) (*Channel, error) {
return &response.Channel, nil
}

// InviteUserToChannel invites a user to a given channel and returns a *Channel
func (api *Slack) InviteUserToChannel(channelId, userId string) (*Channel, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -142,6 +151,7 @@ func (api *Slack) InviteUserToChannel(channelId, userId string) (*Channel, error
return &response.Channel, nil
}

// JoinChannel joins the currently authenticated user to a channel
func (api *Slack) JoinChannel(channel string) (*Channel, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -154,6 +164,7 @@ func (api *Slack) JoinChannel(channel string) (*Channel, error) {
return &response.Channel, nil
}

// LeaveChannel makes the authenticated user leave the given channel
func (api *Slack) LeaveChannel(channelId string) (bool, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -169,6 +180,7 @@ func (api *Slack) LeaveChannel(channelId string) (bool, error) {
return false, nil
}

// KickUserFromChannel kicks a user from a given channel
func (api *Slack) KickUserFromChannel(channelId, userId string) error {
values := url.Values{
"token": {api.config.token},
Expand All @@ -182,6 +194,7 @@ func (api *Slack) KickUserFromChannel(channelId, userId string) error {
return nil
}

// GetChannels retrieves all the channels
func (api *Slack) GetChannels(excludeArchived bool) ([]Channel, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -196,10 +209,11 @@ func (api *Slack) GetChannels(excludeArchived bool) ([]Channel, error) {
return response.Channels, nil
}

/* Clients should try to avoid making this call too often. When needing to mark a read position, a client should set a
timer before making the call. In this way, any further updates needed during the timeout will not generate extra calls
(just one per channel). This is useful for when reading scroll-back history, or following a busy live channel. A
timeout of 5 seconds is a good starting point. Be sure to flush these calls on shutdown/logout. */
// SetChannelReadMark sets the read mark of a given channel to a specific point
// Clients should try to avoid making this call too often. When needing to mark a read position, a client should set a
// timer before making the call. In this way, any further updates needed during the timeout will not generate extra calls
// (just one per channel). This is useful for when reading scroll-back history, or following a busy live channel. A
// timeout of 5 seconds is a good starting point. Be sure to flush these calls on shutdown/logout.
func (api *Slack) SetChannelReadMark(channelId, ts string) error {
values := url.Values{
"token": {api.config.token},
Expand All @@ -213,6 +227,7 @@ func (api *Slack) SetChannelReadMark(channelId, ts string) error {
return nil
}

// RenameChannel renames a given channel
func (api *Slack) RenameChannel(channelId, name string) (*Channel, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -229,6 +244,8 @@ func (api *Slack) RenameChannel(channelId, name string) (*Channel, error) {

}

// SetChannelPurpose sets the channel purpose and returns the purpose that was
// successfully set
func (api *Slack) SetChannelPurpose(channelId, purpose string) (string, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -242,6 +259,7 @@ func (api *Slack) SetChannelPurpose(channelId, purpose string) (string, error) {
return response.Purpose, nil
}

// SetChannelTopic sets the channel topic and returns the topic that was successfully set
func (api *Slack) SetChannelTopic(channelId, topic string) (string, error) {
values := url.Values{
"token": {api.config.token},
Expand Down
15 changes: 11 additions & 4 deletions chat.go
Expand Up @@ -24,12 +24,15 @@ type chatResponseFull struct {
SlackResponse
}

// AttachmentField contains information for an attachment field
// An Attachment can contain multiple of these
type AttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}

// Attachment contains all the information for an attachment
type Attachment struct {
Fallback string `json:"fallback"`

Expand All @@ -49,6 +52,7 @@ type Attachment struct {
Fields []AttachmentField `json:"fields,omitempty"`
}

// PostMessageParameters contains all the parameters necessary (including the optional ones) for a PostMessage() request
type PostMessageParameters struct {
Text string
Username string
Expand All @@ -61,6 +65,7 @@ type PostMessageParameters struct {
IconEmoji string
}

// NewPostMessageParameters provides an instance of PostMessageParameters with all the sane default values set
func NewPostMessageParameters() PostMessageParameters {
return PostMessageParameters{
Username: DEFAULT_MESSAGE_USERNAME,
Expand All @@ -86,6 +91,7 @@ func chatRequest(path string, values url.Values, debug bool) (*chatResponseFull,
return response, nil
}

// DeleteMessage deletes a message in a channel
func (api *Slack) DeleteMessage(channelId, messageTimestamp string) (string, string, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -99,7 +105,7 @@ func (api *Slack) DeleteMessage(channelId, messageTimestamp string) (string, str
return response.ChannelId, response.Timestamp, nil
}

func EscapeMessage(message string) string {
func escapeMessage(message string) string {
/*
& replaced with &
< replaced with &lt;
Expand All @@ -109,11 +115,12 @@ func EscapeMessage(message string) string {
return replacer.Replace(message)
}

// PostMessage sends a message to a channel
func (api *Slack) PostMessage(channelId string, text string, params PostMessageParameters) (channel string, timestamp string, err error) {
values := url.Values{
"token": {api.config.token},
"channel": {channelId},
"text": {EscapeMessage(text)},
"text": {escapeMessage(text)},
}
if params.Username != DEFAULT_MESSAGE_USERNAME {
values.Set("username", string(params.Username))
Expand Down Expand Up @@ -151,17 +158,17 @@ func (api *Slack) PostMessage(channelId string, text string, params PostMessageP
return response.ChannelId, response.Timestamp, nil
}

// UpdateMessage updates a message in a channel
func (api *Slack) UpdateMessage(channelId, timestamp, text string) (string, string, string, error) {
values := url.Values{
"token": {api.config.token},
"channel": {channelId},
"text": {EscapeMessage(text)},
"text": {escapeMessage(text)},
"ts": {timestamp},
}
response, err := chatRequest("chat.update", values, api.debug)
if err != nil {
return "", "", "", err
}
return response.ChannelId, response.Timestamp, response.Text, nil

}
3 changes: 3 additions & 0 deletions config.go
@@ -1,5 +1,8 @@
package slack

// Config contains some config parameters needed
// Token always needs to be set for the api to function
// Origin and Protocol are optional and only needed for websocket
type Config struct {
token string
origin string
Expand Down
7 changes: 7 additions & 0 deletions dm.go
Expand Up @@ -20,6 +20,7 @@ type imResponseFull struct {
SlackResponse
}

// IM contains information related to the Direct Message channel
type IM struct {
Id string `json:"id"`
IsIM bool `json:"is_im"`
Expand All @@ -40,6 +41,7 @@ func imRequest(path string, values url.Values, debug bool) (*imResponseFull, err
return response, nil
}

// CloseIMChannel closes the direct message channel
func (api *Slack) CloseIMChannel(channelId string) (bool, bool, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -52,6 +54,8 @@ func (api *Slack) CloseIMChannel(channelId string) (bool, bool, error) {
return response.NoOp, response.AlreadyClosed, nil
}

// OpenIMChannel opens a direct message channel to the user provided as argument
// Returns some status and the channelId
func (api *Slack) OpenIMChannel(userId string) (bool, bool, string, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -64,6 +68,7 @@ func (api *Slack) OpenIMChannel(userId string) (bool, bool, string, error) {
return response.NoOp, response.AlreadyOpen, response.Channel.Id, nil
}

// MarkIMChannel sets the read mark of a direct message channel to a specific point
func (api *Slack) MarkIMChannel(channelId, ts string) (err error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -77,6 +82,7 @@ func (api *Slack) MarkIMChannel(channelId, ts string) (err error) {
return
}

// GetIMHistory retrieves the direct message channel history
func (api *Slack) GetIMHistory(channelId string, params HistoryParameters) (*History, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -98,6 +104,7 @@ func (api *Slack) GetIMHistory(channelId string, params HistoryParameters) (*His
return &response.History, nil
}

// GetIMChannels returns the list of direct message channels
func (api *Slack) GetIMChannels() ([]IM, error) {
values := url.Values{
"token": {api.config.token},
Expand Down
1 change: 1 addition & 0 deletions emoji.go
Expand Up @@ -10,6 +10,7 @@ type emojiResponseFull struct {
SlackResponse
}

// GetEmoji retrieves all the emojis
func (api *Slack) GetEmoji() (map[string]string, error) {
values := url.Values{
"token": {api.config.token},
Expand Down
18 changes: 13 additions & 5 deletions files.go
Expand Up @@ -17,13 +17,15 @@ const (
DEFAULT_FILES_PAGE = 1
)

// Comment contains all the information relative to a comment
type Comment struct {
Id string `json:"id"`
Timestamp JSONTime `json:"timestamp"`
UserId string `json:"user"`
Comment string `json:"comment"`
}

// File contains all the information for a file
type File struct {
Id string `json:"id"`
Created JSONTime `json:"created"`
Expand All @@ -43,10 +45,10 @@ type File struct {

Size int `json:"size"`

Url string `json:"url"`
UrlDownload string `json:"url_download"`
UrlPrivate string `json:"url_private"`
UrlPrivateDownload string `json:"url_private_download"`
URL string `json:"url"`
URLDownload string `json:"url_download"`
URLPrivate string `json:"url_private"`
URLPrivateDownload string `json:"url_private_download"`

Thumb64 string `json:"thumb_64"`
Thumb80 string `json:"thumb_80"`
Expand All @@ -63,14 +65,15 @@ type File struct {
LinesMore int `json:"lines_more"`

IsPublic bool `json:"is_public"`
PublicUrlShared bool `json:"public_url_shared"`
PublicURLShared bool `json:"public_url_shared"`
Channels []string `json:"channels"`
Groups []string `json:"groups"`
InitialComment Comment `json:"initial_comment"`
NumStars int `json:"num_stars"`
IsStarred bool `json:"is_starred"`
}

// FileUploadParameters contains all the parameters necessary (including the optional ones) for an UploadFile() request
type FileUploadParameters struct {
File string
Content string
Expand All @@ -81,6 +84,7 @@ type FileUploadParameters struct {
Channels []string
}

// GetFilesParameters contains all the parameters necessary (including the optional ones) for a GetFiles() request
type GetFilesParameters struct {
UserId string
TimestampFrom JSONTime
Expand All @@ -99,6 +103,7 @@ type fileResponseFull struct {
SlackResponse
}

// NewGetFilesParameters provides an instance of GetFilesParameters with all the sane default values set
func NewGetFilesParameters() GetFilesParameters {
return GetFilesParameters{
UserId: DEFAULT_FILES_USERID,
Expand All @@ -122,6 +127,7 @@ func fileRequest(path string, values url.Values, debug bool) (*fileResponseFull,
return response, nil
}

// GetFileInfo retrieves a file and related comments
func (api *Slack) GetFileInfo(fileId string, count, page int) (*File, []Comment, *Paging, error) {
values := url.Values{
"token": {api.config.token},
Expand All @@ -136,6 +142,7 @@ func (api *Slack) GetFileInfo(fileId string, count, page int) (*File, []Comment,
return &response.File, response.Comments, &response.Paging, nil
}

// GetFiles retrieves all files according to the parameters given
func (api *Slack) GetFiles(params GetFilesParameters) ([]File, *Paging, error) {
values := url.Values{
"token": {api.config.token},
Expand Down Expand Up @@ -166,6 +173,7 @@ func (api *Slack) GetFiles(params GetFilesParameters) ([]File, *Paging, error) {
return response.Files, &response.Paging, nil
}

// UploadFile uploads a file
func (api *Slack) UploadFile(params FileUploadParameters) (file *File, err error) {
// Test if user token is valid. This helps because client.Do doesn't like this for some reason. XXX: More
// investigation needed, but for now this will do.
Expand Down

0 comments on commit 55d471b

Please sign in to comment.