This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-club.go
89 lines (75 loc) · 2.19 KB
/
command-club.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
CLUB_WATCHING_CLUB = "Watching club %s in this channel"
CLUB_RESET_SUCCESSFUL = "Ok. No longer watching any clubs in this channel."
CLUB_ID_SHOULD_BE_INTEGER = "Sorry. Club id's should be integers"
CLUB_IS_ALREADY_SET = "Sorry, you're already watching a club in this channel."
CLUB_DOESNT_EXIST = "Sorry. That club id doesn't exist or you aren't in the club"
CLUB_SET_SUCCESSFUL = "Great! I will post updates from this Strava club"
)
type ClubCommand struct {
}
func (cmd *ClubCommand) Name() string {
return "club"
}
func (cmd *ClubCommand) Execute(params []string, message *IncomingSlackMessage, executor *CommandExecutor) (string, error) {
if len(params) > 1 {
return COMMAND_TOO_MANY_PARAMETERS, nil
}
job, err := executor.repo.JobDetails.Get(message.TeamId, message.ChannelId)
if err != nil {
return "", err
}
if len(params) == 0 {
if job == nil {
return COMMAND_NO_CLUBS_WATCHED, nil
} else {
return fmt.Sprintf(CLUB_WATCHING_CLUB, job.ClubId), nil
}
}
if strings.ToLower(params[0]) == "reset" {
if job != nil {
err = executor.repo.JobDetails.Delete(job)
if err != nil {
return "", err
}
executor.jobsExecutor.RemoveJob(job)
}
return CLUB_RESET_SUCCESSFUL, nil
}
if job != nil {
return CLUB_IS_ALREADY_SET, nil
}
clubId := params[0]
numericClubId, err := strconv.ParseInt(clubId, 10, 64)
if err != nil {
return CLUB_ID_SHOULD_BE_INTEGER, nil
}
details, err := executor.repo.AccessDetails.GetForTeam(message.TeamId, message.UserId)
if err != nil {
return "", err
} else if details == nil {
return COMMAND_STRAVA_NOT_CONNECTED, nil
}
activities, err := executor.strava.GetClubActivities(details, numericClubId)
if err != nil {
return CLUB_DOESNT_EXIST, nil
}
job = &JobDetails{TeamId: message.TeamId, ClubId: clubId, ChannelId: message.ChannelId, SlackUserId: message.UserId}
err = executor.repo.JobDetails.Create(job)
if err != nil {
return "", err
}
executor.jobsExecutor.AddJob(job)
go func() {
time.Sleep(3 * time.Second)
executor.poster.PostActivities(clubId, activities, []*JobDetails{job})
}()
return CLUB_SET_SUCCESSFUL, nil
}