mirror of
https://github.com/FromGram/server.git
synced 2026-11-19 05:51:37 +03:00
feat: supply limits and released-by for plain (non-collectible) gifts
Previously ImportStarGift (custom, non-official gifts) always set Limited=false and AvailabilityTotal/Remains=0 -- there was no way to give a plain gift a limited supply at all; only the official-import path (with IncludeCollectible) supported that. - ImportStarGiftRequest: new SupplyTotal (0 = unlimited; an unlimited gift can't later be upgraded to a collectible -- that stays a separate CreateCollectibleRevision step for supply-limited gifts), ReleasedByType/ReleasedByID (optional). - resolveReleasedByPeer: validates a 'released by' user or channel and requires it to already have a username, since clients always render 'Released by @username'. - Wired into StarGiftCatalogWrite: Limited = SupplyTotal > 0, AvailabilityTotal/AvailabilityRemains = SupplyTotal, ReleasedBy = the validated peer. The store layer already persisted and served these fields for other gift types -- this was purely an admin-layer gap. - Web panel: supply + released-by (user/channel + ID) fields on the custom gift upload form. Verified: frontend builds clean (tsc + vite build). Go side gofmt only, same toolchain limitation as prior commits -- test locally: go test ./internal/admin/...
This commit is contained in:
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1
-1
@@ -4,7 +4,7 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>telesrv admin</title>
|
||||
<script type="module" crossorigin src="/assets/index-Cez6_L6c.js"></script>
|
||||
<script type="module" crossorigin src="/assets/index-BhDZ46FH.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-DHdrFM5j.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -304,6 +304,14 @@ const translations: Record<Language, Record<string, string>> = {
|
||||
"gifts.reason": "Audit reason",
|
||||
"gifts.reasonPlaceholder": "Briefly describe why this gift is being imported",
|
||||
"gifts.enableAfterImport": "Enable after import",
|
||||
"gifts.supplyTotal": "Supply (0 = unlimited)",
|
||||
"gifts.supplyTotalHint": "0 means unlimited copies; an unlimited gift can never be upgraded to a collectible later. A limited supply (>0) is tracked and shown to clients as remaining/total, same as the original.",
|
||||
"gifts.releasedByType": "Released by",
|
||||
"gifts.releasedByNone": "Not set",
|
||||
"gifts.releasedByUser": "User",
|
||||
"gifts.releasedByChannel": "Channel",
|
||||
"gifts.releasedById": "Released-by ID",
|
||||
"gifts.releasedByHint": "The referenced user/channel must already have a username -- clients show \"Released by @username\".",
|
||||
"gifts.validate": "Dry-run validation",
|
||||
"gifts.confirmImport": "Confirm import",
|
||||
"gifts.stepDetails": "File and details",
|
||||
@@ -685,6 +693,14 @@ const translations: Record<Language, Record<string, string>> = {
|
||||
"gifts.reason": "审计原因",
|
||||
"gifts.reasonPlaceholder": "简要说明本次导入礼物的原因",
|
||||
"gifts.enableAfterImport": "导入后启用",
|
||||
"gifts.supplyTotal": "供应量(0 = 无限)",
|
||||
"gifts.supplyTotalHint": "0 表示无限供应;无限供应的礼物以后无法升级为收藏品。有限供应(>0)会被跟踪,并像原版一样向客户端展示剩余/总量。",
|
||||
"gifts.releasedByType": "发行方",
|
||||
"gifts.releasedByNone": "未设置",
|
||||
"gifts.releasedByUser": "用户",
|
||||
"gifts.releasedByChannel": "频道",
|
||||
"gifts.releasedById": "发行方 ID",
|
||||
"gifts.releasedByHint": "所选用户/频道必须已设置用户名 —— 客户端会显示“发行方 @用户名”。",
|
||||
"gifts.validate": "Dry-run 校验",
|
||||
"gifts.confirmImport": "确认导入",
|
||||
"gifts.stepDetails": "文件与信息",
|
||||
@@ -1066,6 +1082,14 @@ const translations: Record<Language, Record<string, string>> = {
|
||||
"gifts.reason": "Причина для аудита",
|
||||
"gifts.reasonPlaceholder": "Кратко опишите причину импорта этого подарка",
|
||||
"gifts.enableAfterImport": "Включить после импорта",
|
||||
"gifts.supplyTotal": "Тираж (0 = неограниченно)",
|
||||
"gifts.supplyTotalHint": "0 — неограниченный тираж; такой подарок нельзя будет потом сделать коллекционным (NFT). Ограниченный тираж (>0) отслеживается и показывается клиентам как \"осталось/всего\", как в оригинале.",
|
||||
"gifts.releasedByType": "Выпущен кем",
|
||||
"gifts.releasedByNone": "Не указано",
|
||||
"gifts.releasedByUser": "Пользователь",
|
||||
"gifts.releasedByChannel": "Канал",
|
||||
"gifts.releasedById": "ID выпустившего",
|
||||
"gifts.releasedByHint": "У указанного пользователя/канала обязательно должен быть юзернейм — клиент показывает \"Выпущен @юзернейм\".",
|
||||
"gifts.validate": "Тестовая проверка",
|
||||
"gifts.confirmImport": "Подтвердить импорт",
|
||||
"gifts.stepDetails": "Файл и описание",
|
||||
|
||||
@@ -95,6 +95,8 @@ export function GiftsPage() {
|
||||
const [includeCollectible, setIncludeCollectible] = useState(true);
|
||||
const [upgradeStars, setUpgradeStars] = useState("0");
|
||||
const [supplyTotal, setSupplyTotal] = useState("0");
|
||||
const [releasedByType, setReleasedByType] = useState("");
|
||||
const [releasedById, setReleasedById] = useState("");
|
||||
const [slugPrefix, setSlugPrefix] = useState("");
|
||||
const [giftID, setGiftID] = useState("0");
|
||||
const [title, setTitle] = useState("");
|
||||
@@ -165,7 +167,9 @@ export function GiftsPage() {
|
||||
stars,
|
||||
convert_stars: convertStars,
|
||||
enabled,
|
||||
sort_order: Number(sortOrder)
|
||||
sort_order: Number(sortOrder),
|
||||
supply_total: Number(supplyTotal),
|
||||
...(releasedByType ? { released_by_type: releasedByType, released_by_id: Number(releasedById) } : {})
|
||||
}));
|
||||
form.set("file", file, file.name);
|
||||
return form;
|
||||
@@ -343,6 +347,26 @@ export function GiftsPage() {
|
||||
<span className="gift-file-copy"><span className="gift-field-label">{t("gifts.animation")}</span><strong>{file ? file.name : t("gifts.filePrompt")}</strong><small>{file ? formatBytes(file.size) : t("gifts.fileHint")}</small></span>
|
||||
<span className="gift-file-action">{file ? t("gifts.changeFile") : t("gifts.chooseFile")}</span>
|
||||
</label>
|
||||
<div className="gift-fields-grid">
|
||||
<label>
|
||||
<span>{t("gifts.supplyTotal")}</span>
|
||||
<input type="number" min="0" value={supplyTotal} onChange={(e) => { setSupplyTotal(e.target.value); setPreview(null); }} />
|
||||
<small>{t("gifts.supplyTotalHint")}</small>
|
||||
</label>
|
||||
<label>
|
||||
<span>{t("gifts.releasedByType")}</span>
|
||||
<select value={releasedByType} onChange={(e) => { setReleasedByType(e.target.value); setPreview(null); }}>
|
||||
<option value="">{t("gifts.releasedByNone")}</option>
|
||||
<option value="user">{t("gifts.releasedByUser")}</option>
|
||||
<option value="channel">{t("gifts.releasedByChannel")}</option>
|
||||
</select>
|
||||
</label>
|
||||
{releasedByType && <label>
|
||||
<span>{t("gifts.releasedById")}</span>
|
||||
<input type="number" value={releasedById} onChange={(e) => { setReleasedById(e.target.value); setPreview(null); }} />
|
||||
<small>{t("gifts.releasedByHint")}</small>
|
||||
</label>}
|
||||
</div>
|
||||
</>}
|
||||
<div className="gift-fields-grid">
|
||||
<label><span>{t("gifts.title")}</span><input value={title} maxLength={128} placeholder={t("gifts.titlePlaceholder")} onChange={(e) => { setTitle(e.target.value); setPreview(null); }} /></label>
|
||||
|
||||
@@ -253,6 +253,20 @@ type ImportStarGiftRequest struct {
|
||||
FileName string `json:"file_name"`
|
||||
ContentSHA string `json:"content_sha256"`
|
||||
Data []byte `json:"-"`
|
||||
// SupplyTotal is the total number of copies of this (non-collectible)
|
||||
// gift that can ever be sold. 0 means unlimited -- and an unlimited
|
||||
// gift can never later be upgraded to a collectible/NFT version; only
|
||||
// a supply-limited gift (SupplyTotal > 0) is eligible for that, as a
|
||||
// separate step (CreateCollectibleRevision).
|
||||
SupplyTotal int `json:"supply_total,omitempty"`
|
||||
// ReleasedByType/ReleasedByID optionally set who "released" this gift
|
||||
// (shown to clients as "Released by @username"), e.g. an official
|
||||
// channel or a user/bot. Either both must be set or both left empty.
|
||||
// ReleasedByType must be "user" or "channel". The referenced peer must
|
||||
// have a username -- the client always renders "Released by
|
||||
// @<username>", so a peer without one can't be set here.
|
||||
ReleasedByType string `json:"released_by_type,omitempty"`
|
||||
ReleasedByID int64 `json:"released_by_id,omitempty"`
|
||||
}
|
||||
|
||||
type ImportOfficialStarGiftRequest struct {
|
||||
@@ -1067,15 +1081,61 @@ func (s *Service) DeletePrivateHistory(ctx context.Context, req DeletePrivateHis
|
||||
})
|
||||
}
|
||||
|
||||
// resolveReleasedByPeer validates a "released by" peer reference and
|
||||
// returns it as a domain.Peer. It requires the referenced user or channel
|
||||
// to have a username, since clients always render "Released by
|
||||
// @<username>" and there'd be nothing to show otherwise.
|
||||
func (s *Service) resolveReleasedByPeer(ctx context.Context, peerType string, peerID int64) (domain.Peer, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(peerType)) {
|
||||
case "user":
|
||||
if s.users == nil {
|
||||
return domain.Peer{}, fmt.Errorf("admin user dependency is not configured")
|
||||
}
|
||||
u, found, err := s.users.AdminUser(ctx, peerID)
|
||||
if err != nil {
|
||||
return domain.Peer{}, err
|
||||
}
|
||||
if !found {
|
||||
return domain.Peer{}, domain.ErrUserNotFound
|
||||
}
|
||||
if strings.TrimSpace(u.Username) == "" {
|
||||
return domain.Peer{}, fmt.Errorf("released_by user has no username")
|
||||
}
|
||||
return domain.Peer{Type: domain.PeerTypeUser, ID: u.ID}, nil
|
||||
case "channel":
|
||||
if s.channels == nil {
|
||||
return domain.Peer{}, fmt.Errorf("admin channels dependency is not configured")
|
||||
}
|
||||
ch, err := s.channels.GetChannelByID(ctx, peerID)
|
||||
if err != nil {
|
||||
return domain.Peer{}, err
|
||||
}
|
||||
if strings.TrimSpace(ch.Username) == "" {
|
||||
return domain.Peer{}, fmt.Errorf("released_by channel has no username")
|
||||
}
|
||||
return domain.Peer{Type: domain.PeerTypeChannel, ID: ch.ID}, nil
|
||||
default:
|
||||
return domain.Peer{}, fmt.Errorf("released_by_type must be \"user\" or \"channel\"")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ImportStarGift(ctx context.Context, req ImportStarGiftRequest) (CommandResult, error) {
|
||||
if s == nil || s.gifts == nil {
|
||||
return CommandResult{}, fmt.Errorf("star gift service is not configured")
|
||||
}
|
||||
if req.GiftID < 0 || req.Stars <= 0 || req.ConvertStars < 0 || req.ConvertStars > req.Stars ||
|
||||
req.SortOrder < math.MinInt32 || req.SortOrder > math.MaxInt32 ||
|
||||
len([]rune(strings.TrimSpace(req.Title))) > domain.MaxStarGiftTitleRunes {
|
||||
len([]rune(strings.TrimSpace(req.Title))) > domain.MaxStarGiftTitleRunes || req.SupplyTotal < 0 {
|
||||
return CommandResult{}, domain.ErrStarGiftInvalid
|
||||
}
|
||||
var releasedBy domain.Peer
|
||||
if req.ReleasedByType != "" || req.ReleasedByID != 0 {
|
||||
var err error
|
||||
releasedBy, err = s.resolveReleasedByPeer(ctx, req.ReleasedByType, req.ReleasedByID)
|
||||
if err != nil {
|
||||
return CommandResult{}, err
|
||||
}
|
||||
}
|
||||
animation, err := s.gifts.PrepareAnimation(req.FileName, req.Data)
|
||||
if err != nil {
|
||||
return CommandResult{}, err
|
||||
@@ -1089,6 +1149,7 @@ func (s *Service) ImportStarGift(ctx context.Context, req ImportStarGiftRequest)
|
||||
"source_format": animation.SourceFormat, "source_name": animation.SourceName,
|
||||
"sha256": req.ContentSHA, "width": animation.Width, "height": animation.Height,
|
||||
"frame_rate": animation.FrameRate, "compressed_bytes": len(animation.TGS), "json_bytes": len(animation.JSON),
|
||||
"supply_total": req.SupplyTotal, "released_by_type": req.ReleasedByType, "released_by_id": req.ReleasedByID,
|
||||
}
|
||||
if req.DryRun {
|
||||
return CommandResult{Message: "star gift import validated", Details: details}, nil
|
||||
@@ -1097,6 +1158,8 @@ func (s *Service) ImportStarGift(ctx context.Context, req ImportStarGiftRequest)
|
||||
GiftID: req.GiftID, Title: req.Title, Stars: req.Stars, ConvertStars: req.ConvertStars,
|
||||
Enabled: req.Enabled, SortOrder: req.SortOrder, Animation: animation,
|
||||
Actor: req.Actor, CommandID: req.CommandID,
|
||||
Limited: req.SupplyTotal > 0, AvailabilityTotal: req.SupplyTotal, AvailabilityRemains: req.SupplyTotal,
|
||||
ReleasedBy: releasedBy,
|
||||
})
|
||||
if err != nil {
|
||||
return CommandResult{Details: details}, err
|
||||
|
||||
Reference in New Issue
Block a user