发现列表之前没有机器人帖。打开广场发现时,给已有机器人各写一条原创短帖。 Co-authored-by: Cursor <cursoragent@cursor.com>
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"go.uber.org/zap"
|
||
|
||
"spark-server/internal/module/square/entity"
|
||
userentity "spark-server/internal/module/user/entity"
|
||
"spark-server/internal/pkg/errs"
|
||
)
|
||
|
||
const discoverBotURLPrefix = "https://spark.wyrs365.com/square/discover/bot/"
|
||
|
||
// ensureDiscoverBots puts one original short post from each existing robot
|
||
// onto the square discovery feed. It does not fetch or copy outside articles.
|
||
func (s *Service) ensureDiscoverBots(ctx context.Context, channel entity.Channel, category entity.Category) {
|
||
if s == nil || channel != entity.ChannelSquare || category.IsTag() || s.discoverReady.Load() {
|
||
return
|
||
}
|
||
s.discoverMu.Lock()
|
||
defer s.discoverMu.Unlock()
|
||
if s.discoverReady.Load() {
|
||
return
|
||
}
|
||
if err := s.fillDiscoverBots(ctx); err != nil {
|
||
if s.log != nil {
|
||
s.log.Warn("square discover bots", zap.Error(err))
|
||
}
|
||
return
|
||
}
|
||
s.discoverReady.Store(true)
|
||
}
|
||
|
||
func (s *Service) fillDiscoverBots(ctx context.Context) error {
|
||
var failed bool
|
||
for i := 1; i <= userentity.BotCount; i++ {
|
||
email := userentity.BotEmail(i)
|
||
user, err := s.users.GetByEmail(ctx, email)
|
||
if err != nil {
|
||
if errors.Is(err, errs.ErrNotFound) {
|
||
continue
|
||
}
|
||
failed = true
|
||
continue
|
||
}
|
||
if user == nil || user.ID <= 0 || user.Status != userentity.StatusActive {
|
||
continue
|
||
}
|
||
_, err = s.PublishBotMedia(ctx, user.ID, &BotPublishInput{
|
||
Text: discoverBotText(i, user.Nickname),
|
||
SourceURL: discoverBotURL(i),
|
||
TTL: 15 * 24 * time.Hour,
|
||
})
|
||
if err == nil || errors.Is(err, errs.ErrDuplicate) {
|
||
continue
|
||
}
|
||
failed = true
|
||
}
|
||
if failed {
|
||
return errs.ErrInner
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func discoverBotURL(index int) string {
|
||
if index < 1 {
|
||
return ""
|
||
}
|
||
return fmt.Sprintf("%s%03d", discoverBotURLPrefix, index)
|
||
}
|
||
|
||
func discoverBotText(index int, nickname string) string {
|
||
name := strings.TrimSpace(nickname)
|
||
if name == "" {
|
||
name = fmt.Sprintf("bot%03d", index)
|
||
}
|
||
lines := []string{
|
||
"%s:把今天看到的光记在发现里。",
|
||
"%s:路过广场,留一句给后来的人。",
|
||
"%s:这条是给发现页的,不跟上一句重复。",
|
||
"%s:风很小,适合把一句话放在这里。",
|
||
"%s:发现页空着的时候,补一条短的。",
|
||
"%s:同一条不连发,换个人再看下一句。",
|
||
"%s:文字先到,配图要有合法的对象键才附上。",
|
||
"%s:推荐流里见,不占别的广场频道。",
|
||
}
|
||
return fmt.Sprintf(lines[(index-1)%len(lines)], name)
|
||
}
|