@@ 2,7 2,6 @@ package slack
import (
"bytes"
- "context"
"fmt"
"links"
"links/internal/localizer"
@@ 12,6 11,7 @@ import (
"git.sr.ht/~emersion/gqlclient"
sq "github.com/Masterminds/squirrel"
+ "github.com/labstack/echo/v4"
"netlandish.com/x/gobwebs/auth"
"netlandish.com/x/gobwebs/database"
)
@@ 30,11 30,13 @@ type SlackCommandResponse struct {
Blocks []SlackBlock `json:"blocks"`
}
-func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*SlackCommandResponse, error) {
+func linkAddShort(c echo.Context, url, teamID, slackUser, text string) (*SlackCommandResponse, error) {
opts := &database.FilterOptions{
Filter: sq.Eq{"sc.team_id": teamID},
Limit: 1,
}
+ ctx := c.Request().Context()
+ lt := localizer.GetSessionLocalizer(c)
slackConns, err := models.GetSlackConnections(ctx, opts)
if err != nil {
return nil, err
@@ 62,7 64,7 @@ func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*Sl
}
if len(slackUsers) == 0 {
- return sendInstructionLink(slackConn, url, slackUser)
+ return sendInstructionLink(c, slackConn, url, slackUser)
}
user, err := models.GetUser(ctx, slackUsers[0].UserID, true)
@@ 79,7 81,7 @@ func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*Sl
input := strings.Split(text, " ")
shortCodeArg := strings.Split(input[1], ":")
if len(shortCodeArg) != 2 {
- block.Text.Text = "Wrong short code argument"
+ block.Text.Text = lt.Translate("Wrong short code argument")
commandResp := &SlackCommandResponse{
Text: "Error",
Blocks: []SlackBlock{block},
@@ 90,7 92,7 @@ func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*Sl
domainArg := strings.Split(input[2], ":")
if len(domainArg) != 2 {
- block.Text.Text = "Wrong short code argument"
+ block.Text.Text = lt.Translate("Wrong short code argument")
commandResp := &SlackCommandResponse{
Text: "Error",
Blocks: []SlackBlock{block},
@@ 117,7 119,7 @@ func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*Sl
return nil, err
}
if len(domains) == 0 {
- block.Text.Text = fmt.Sprintf("%s: domain not found", domainArg[1])
+ block.Text.Text = lt.Translate("%s: domain not found", domainArg[1])
commandResp := &SlackCommandResponse{
Text: "Error",
Blocks: []SlackBlock{block},
@@ 155,7 157,7 @@ func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*Sl
}
block.Type = "section"
block.Text.Type = "mrkdwn"
- block.Text.Text = "Your short link was successfully created"
+ block.Text.Text = lt.Translate("Your short link was successfully created")
commandResp := &SlackCommandResponse{
Text: "Add Link",
Blocks: []SlackBlock{block},
@@ 163,12 165,12 @@ func linkAddShort(ctx context.Context, url, teamID, slackUser, text string) (*Sl
return commandResp, nil
}
-func linkAdd(ctx context.Context, url, teamID, slackUser, text string) (*SlackCommandResponse, error) {
+func linkAdd(c echo.Context, url, teamID, slackUser, text string) (*SlackCommandResponse, error) {
opts := &database.FilterOptions{
Filter: sq.Eq{"sc.team_id": teamID},
Limit: 1,
}
- slackConns, err := models.GetSlackConnections(ctx, opts)
+ slackConns, err := models.GetSlackConnections(c.Request().Context(), opts)
if err != nil {
return nil, err
}
@@ 176,6 178,7 @@ func linkAdd(ctx context.Context, url, teamID, slackUser, text string) (*SlackCo
return nil, fmt.Errorf("No slack connection found")
}
slackConn := slackConns[0]
+ ctx := c.Request().Context()
err = slackConn.DecodeToken(ctx)
if err != nil {
return nil, err
@@ 189,21 192,20 @@ func linkAdd(ctx context.Context, url, teamID, slackUser, text string) (*SlackCo
Limit: 1,
}
+ lt := localizer.GetSessionLocalizer(c)
slackUsers, err := models.GetSlackUsers(ctx, opts)
if err != nil {
return nil, err
}
if len(slackUsers) == 0 {
- return sendInstructionLink(slackConn, url, slackUser)
+ return sendInstructionLink(c, slackConn, url, slackUser)
}
user, err := models.GetUser(ctx, slackUsers[0].UserID, true)
if err != nil {
return nil, err
}
- lang := user.Settings.Account.DefaultLang
- lt := localizer.GetLocalizer(lang)
ctx = auth.Context(ctx, user)
// index 0 is url
// index 1 is title
@@ 242,7 244,7 @@ func linkAdd(ctx context.Context, url, teamID, slackUser, text string) (*SlackCo
return commandResp, nil
}
-func linkSearch(ctx context.Context, teamID, text string) (*SlackCommandResponse, error) {
+func linkSearch(c echo.Context, teamID, text string) (*SlackCommandResponse, error) {
opts := &database.FilterOptions{
Filter: sq.And{
sq.Eq{"sc.team_id": teamID},
@@ 250,6 252,8 @@ func linkSearch(ctx context.Context, teamID, text string) (*SlackCommandResponse
},
Limit: 1,
}
+ ctx := c.Request().Context()
+ lt := localizer.GetSessionLocalizer(c)
orgs, err := models.GetOrganizations(ctx, opts)
if err != nil {
return nil, err
@@ 259,8 263,6 @@ func linkSearch(ctx context.Context, teamID, text string) (*SlackCommandResponse
}
org := orgs[0]
user, err := models.GetUser(ctx, org.OwnerID, true)
- lang := user.Settings.Account.DefaultLang
- lt := localizer.GetLocalizer(lang)
ctx = auth.Context(ctx, user)
type GraphQLResponse struct {
OrgLinks struct {
@@ 311,7 313,7 @@ func linkSearch(ctx context.Context, teamID, text string) (*SlackCommandResponse
return nil, err
}
commandResp := &SlackCommandResponse{
- Text: "Link Search",
+ Text: lt.Translate("Link Search"),
ResponseType: "in_channel",
}
header := SlackBlock{}
@@ 336,10 338,11 @@ func linkSearch(ctx context.Context, teamID, text string) (*SlackCommandResponse
return commandResp, nil
}
-func sendInstructionLink(slackConn *models.SlackConnection, url, slackUser string) (*SlackCommandResponse, error) {
+func sendInstructionLink(c echo.Context, slackConn *models.SlackConnection, url, slackUser string) (*SlackCommandResponse, error) {
+ lt := localizer.GetSessionLocalizer(c)
block := SlackBlock{}
apiURL := "https://slack.com/api/chat.postMessage"
- msg := fmt.Sprintf("Please link your slack user with link: %s", url)
+ msg := lt.Translate("Please link your slack user with link: %s", url)
requestBody := []byte(fmt.Sprintf(`{"channel": "%s", "text": "%s"}`, slackUser, msg))
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(requestBody))
if err != nil {
@@ 357,9 360,9 @@ func sendInstructionLink(slackConn *models.SlackConnection, url, slackUser strin
block.Type = "section"
block.Text.Type = "mrkdwn"
- block.Text.Text = "We sent you a direct message with instructions"
+ block.Text.Text = lt.Translate("We sent you a direct message with instructions")
commandResp := &SlackCommandResponse{
- Text: "Add Link",
+ Text: lt.Translate("Add Link"),
Blocks: []SlackBlock{block},
}
return commandResp, nil