thread-master/apps/backend/internal/logic/utm/publicview.go

39 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utm
import (
"strings"
"apps/backend/internal/config"
"apps/backend/internal/module/growth/domain"
"apps/backend/internal/types"
)
const trackPathPrefix = "/api/v1/public/u/"
// publicView 把追蹤連結轉成對外型別。TrackUrl 必須由後端算:連結會被貼進 Threads
// 貼文,前端自己拼 window.location.origin 在 API 與前端不同 host 時會產出 404 連結。
func publicView(c config.Config, u *domain.UtmLink) types.UtmLinkPublic {
path := trackPathPrefix + u.Code
return types.UtmLinkPublic{
Id: u.ID,
Code: u.Code,
TrackPath: path,
TrackUrl: publicAPIBase(c) + path,
DestinationUrl: u.DestinationURL,
OutcomeId: u.OutcomeID,
Label: u.Label,
Clicks: u.Clicks,
CreatedAt: u.CreatedAt,
}
}
// publicAPIBase 與 threads oauth callback 用同一套推導PublicAPIBase 優先,否則
// PublicWebBase正式站 nginx、dev 的 vite 都會把 /api 轉到 gateway。兩者皆空
// 時回空字串TrackUrl 退回相對路徑讓瀏覽器自己解。
func publicAPIBase(c config.Config) string {
if b := strings.TrimRight(strings.TrimSpace(c.PublicAPIBase), "/"); b != "" {
return b
}
return strings.TrimRight(strings.TrimSpace(c.PublicWebBase), "/")
}