Skip to content

Commit aed8667

Browse files
feat: use the hackatime message queue
1 parent 3e93d51 commit aed8667

File tree

3 files changed

+23
-38
lines changed

3 files changed

+23
-38
lines changed

config/config.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ type appConfig struct {
106106
}
107107

108108
type securityConfig struct {
109-
AllowSignup bool `yaml:"allow_signup" default:"true" env:"WAKAPI_ALLOW_SIGNUP"`
110-
SignupCaptcha bool `yaml:"signup_captcha" default:"false" env:"WAKAPI_SIGNUP_CAPTCHA"`
111-
AirtableAPIKey string `yaml:"airtable_api_key" env:"WAKAPI_AIRTABLE_API_KEY"`
112-
InviteCodes bool `yaml:"invite_codes" default:"true" env:"WAKAPI_INVITE_CODES"`
113-
ExposeMetrics bool `yaml:"expose_metrics" default:"false" env:"WAKAPI_EXPOSE_METRICS"`
114-
EnableProxy bool `yaml:"enable_proxy" default:"false" env:"WAKAPI_ENABLE_PROXY"` // only intended for production instance at wakapi.dev
115-
DisableFrontpage bool `yaml:"disable_frontpage" default:"false" env:"WAKAPI_DISABLE_FRONTPAGE"`
116-
AdminToken string `yaml:"admin_token" default:"blahaji_rulz_da_world" env:"WAKAPI_ADMIN_TOKEN"`
109+
AllowSignup bool `yaml:"allow_signup" default:"true" env:"WAKAPI_ALLOW_SIGNUP"`
110+
SignupCaptcha bool `yaml:"signup_captcha" default:"false" env:"WAKAPI_SIGNUP_CAPTCHA"`
111+
HackatimeMessageQueueAPIKey string `yaml:"hackatime_message_queue_api_key" env:"WAKAPI_HACKATIME_MESSAGE_QUEUE_API_KEY"`
112+
InviteCodes bool `yaml:"invite_codes" default:"true" env:"WAKAPI_INVITE_CODES"`
113+
ExposeMetrics bool `yaml:"expose_metrics" default:"false" env:"WAKAPI_EXPOSE_METRICS"`
114+
EnableProxy bool `yaml:"enable_proxy" default:"false" env:"WAKAPI_ENABLE_PROXY"` // only intended for production instance at wakapi.dev
115+
DisableFrontpage bool `yaml:"disable_frontpage" default:"false" env:"WAKAPI_DISABLE_FRONTPAGE"`
116+
AdminToken string `yaml:"admin_token" default:"blahaji_rulz_da_world" env:"WAKAPI_ADMIN_TOKEN"`
117117
// this is actually a pepper (https://en.wikipedia.org/wiki/Pepper_(cryptography))
118118
PasswordSalt string `yaml:"password_salt" default:"" env:"WAKAPI_PASSWORD_SALT"`
119119
InsecureCookies bool `yaml:"insecure_cookies" default:"false" env:"WAKAPI_INSECURE_COOKIES"`

routes/login.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,10 @@ func (h *LoginHandler) PostResetPassword(w http.ResponseWriter, r *http.Request)
436436

437437
go func(user *models.User) {
438438
link := fmt.Sprintf("%s/set-password?token=%s", h.config.Server.GetPublicUrl(), user.ResetToken)
439-
if h.config.Security.AirtableAPIKey != "" && resetRequest.Slack && strings.HasPrefix(user.ID, "U") {
440-
msgtext := fmt.Sprintf("Arr `%s`! Looks liek ye requested a password reset from hackatime for the email `%s`! If ye didn't request this then sombardy is trying to hack thee account and you should steer clear of below button :tw_crossed_swords:", func() string {
439+
if h.config.Security.HackatimeMessageQueueAPIKey != "" && resetRequest.Slack && strings.HasPrefix(user.ID, "U") {
440+
msgtext := fmt.Sprintf("Hi there `%s`! :hyper-dino-wave:\n\nI received a password reset request for the email `%s`. If you didn't request this, please ignore this message!", func() string {
441441
if user.Name == "" {
442-
return "matey"
442+
return "spelunker"
443443
} else {
444444
return user.Name
445445
}
@@ -454,21 +454,16 @@ func (h *LoginHandler) PostResetPassword(w http.ResponseWriter, r *http.Request)
454454
}
455455
},
456456
{
457-
"type": "actions",
457+
"type": "context",
458458
"elements": [
459459
{
460-
"type": "button",
461-
"text": {
462-
"type": "plain_text",
463-
"text": "Reset Password"
464-
},
465-
"style": "primary",
466-
"url": "` + link + `"
460+
"type": "mrkdwn",
461+
"text": "reset link: \u0060` + link + `\u0060"
467462
}
468463
]
469464
}
470465
]`
471-
if err := utils.SendSlackMessage(h.config.Security.AirtableAPIKey, user.ID, msg, blocks); err != nil {
466+
if err := utils.SendSlackMessage(h.config.Security.HackatimeMessageQueueAPIKey, user.ID, msg, blocks); err != nil {
472467
conf.Log().Request(r).Error("failed to send slack message", "error", err)
473468
} else {
474469
slog.Info("sent slack message", "userID", user.ID)

utils/slack.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,26 @@ import (
88
"net/http"
99
)
1010

11-
func SendSlackMessage(airtableAPIKey string, userID string, message string, blocksJSON string) error {
12-
record := map[string]interface{}{
13-
"fields": map[string]interface{}{
14-
"requester_identifier": "Hackatime Reset Password",
15-
"target_slack_id": userID,
16-
"message_text": message,
17-
"message_blocks": blocksJSON,
18-
"unfurl_links": true,
19-
"unfurl_media": true,
20-
"send_success": false,
21-
},
22-
}
23-
24-
payload := map[string]interface{}{
25-
"records": []interface{}{record},
11+
func SendSlackMessage(hackatimeMessageQueueAPIKey string, userID string, message string, blocksJSON string) error {
12+
payload := map[string]any{
13+
"channel": userID,
14+
"text": message,
15+
"blocks": blocksJSON,
2616
}
2717

2818
jsonPayload, err := json.Marshal(payload)
2919
if err != nil {
3020
return fmt.Errorf("error marshaling payload: %v", err)
3121
}
3222

33-
req, err := http.NewRequest("POST", "https://middleman.hackclub.com/airtable/v0/appTeNFYcUiYfGcR6/arrpheus_message_requests", bytes.NewBuffer(jsonPayload))
23+
req, err := http.NewRequest("POST", "//hackatime-bot.kierank.hackclub.app/slack/message", bytes.NewBuffer(jsonPayload))
3424
if err != nil {
3525
return fmt.Errorf("error creating request: %v", err)
3626
}
3727

3828
req.Header.Set("Content-Type", "application/json")
3929
req.Header.Set("Accept", "application/json")
40-
req.Header.Set("Authorization", "Bearer "+airtableAPIKey)
30+
req.Header.Set("Authorization", "Bearer "+hackatimeMessageQueueAPIKey)
4131
req.Header.Set("User-Agent", "waka.hackclub.com (reset password)")
4232

4333
client := &http.Client{}
@@ -65,7 +55,7 @@ func SendSlackMessage(airtableAPIKey string, userID string, message string, bloc
6555
}
6656

6757
if result.Error.Type != "" {
68-
return fmt.Errorf("Airtable error: %s - %s", result.Error.Type, result.Error.Message)
58+
return fmt.Errorf("Hackatime message queue error: %s - %s", result.Error.Type, result.Error.Message)
6959
}
7060

7161
return nil

0 commit comments

Comments
 (0)