Skip to content

Commit c9daf17

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

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func (h *LoginHandler) PostResetPassword(w http.ResponseWriter, r *http.Request)
468468
]
469469
}
470470
]`
471-
if err := utils.SendSlackMessage(h.config.Security.AirtableAPIKey, user.ID, msg, blocks); err != nil {
471+
if err := utils.SendSlackMessage(h.config.Security.HackatimeMessageQueueAPIKey, user.ID, msg, blocks); err != nil {
472472
conf.Log().Request(r).Error("failed to send slack message", "error", err)
473473
} else {
474474
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)