Files
jiang13-forum/services/friend_link_enrich.go
freefire 3f50316ad0 fix: 完成 Gitea 目录改组收尾(import、构建与 LICENSE)
同步包路径与路由,去掉 SPA 构建步骤,对齐 Gitea 式 LICENSE,并更新规格/规则与占位 SSR。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-29 04:01:20 +08:00

94 lines
2.1 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 services
import (
"encoding/json"
"strings"
"git.iioio.com/freefire/jiang13-forum/models"
)
// EnrichFriendLinksLogos 为缺少 LOGO 的已发布友链,从已通过申请中按 URL 回填
func EnrichFriendLinksLogos(links []FriendLink) []FriendLink {
if len(links) == 0 {
return links
}
needKeys := make(map[string]int)
for i, l := range links {
if strings.TrimSpace(l.Logo) != "" {
continue
}
key := friendLinkURLKey(l.URL)
if key == "" {
continue
}
needKeys[key] = i
}
if len(needKeys) == 0 {
return links
}
var applies []models.FriendLinkApply
_ = models.DB.
Where("status = ? AND logo <> ''", models.FriendLinkApplyStatusApproved).
Order("id DESC").
Find(&applies).Error
logoByURL := make(map[string]string, len(applies))
for _, a := range applies {
key := friendLinkURLKey(a.URL)
if key == "" {
continue
}
if _, ok := logoByURL[key]; ok {
continue
}
logo := normalizeFriendLinkLogoOptional(a.Logo)
if logo != "" {
logoByURL[key] = logo
}
}
if len(logoByURL) == 0 {
return links
}
out := make([]FriendLink, len(links))
copy(out, links)
for key, idx := range needKeys {
if logo, ok := logoByURL[key]; ok {
out[idx].Logo = logo
}
}
return out
}
func friendLinksLogoSnapshot(links []FriendLink) string {
type snap struct {
URL string `json:"url"`
Logo string `json:"logo"`
}
items := make([]snap, len(links))
for i, l := range links {
items[i] = snap{URL: friendLinkURLKey(l.URL), Logo: strings.TrimSpace(l.Logo)}
}
b, _ := json.Marshal(items)
return string(b)
}
// maybePersistEnrichedFriendLinks 若回填产生新 LOGO写回 site_friend_links
func (s *ForumSettingsService) maybePersistEnrichedFriendLinks(enriched []FriendLink) error {
raw := s.getString(SettingSiteFriendLinks, "[]")
before := parseFriendLinksJSON(raw)
if friendLinksLogoSnapshot(before) == friendLinksLogoSnapshot(enriched) {
return nil
}
normalized, err := normalizeFriendLinks(enriched)
if err != nil {
return err
}
linksJSON, err := json.Marshal(normalized)
if err != nil {
return err
}
return s.setString(SettingSiteFriendLinks, string(linksJSON))
}