数据:收窄账号列表与所有权消费方

This commit is contained in:
Christ committed 2026-08-14 11:39:37 +08:00
1 parent 5bb60cabcd
commit d54377d2ef
6 files changed
+27 -25

No files matched your search

+2 -2
View File
@@ -65,8 +65,8 @@ func (s *Server) adminDeleteUser(w http.ResponseWriter, r *http.Request) {
return
}
if s.Manager != nil {
if accounts, listErr := s.Store.Cookies.AllForUser(r.Context(), uid); listErr == nil {
for cookieID := range accounts {
if accountIDs, listErr := s.Store.Cookies.ListOwnedIDs(r.Context(), uid); listErr == nil { // accountIDs 是待停止的账号 ID。
for _, cookieID := range accountIDs {
s.Manager.Stop(cookieID)
}
}
+3 -3
View File
@@ -167,11 +167,11 @@ func (s *Server) itemOwnedByUser(r *http.Request, userID int64, cookieID, itemID
if itemID == "" {
return true
}
all, err := s.Store.Cookies.AllForUser(r.Context(), userID)
if err != nil || cookieID == "" {
if cookieID == "" {
return false
}
if _, ok := all[cookieID]; !ok {
owned, err := s.Store.Cookies.ExistsOwned(r.Context(), userID, cookieID) // owned 和 err 表示账号归属及查询错误。
if err != nil || !owned {
return false
}
_, err = s.Store.Items.Get(r.Context(), cookieID, itemID)
+6 -6
View File
@@ -434,14 +434,14 @@ func (s *Server) ownsAccount(r *http.Request, accountID string) bool {
return false
}
sess := auth.SessionFromContext(r.Context())
accounts, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
if err != nil {
return false
}
_, ok := accounts[accountID]
return ok
owned, err := s.Store.Cookies.ExistsOwned(r.Context(), sess.UserID, accountID) // owned 和 err 表示账号归属及查询错误。
return err == nil && owned
}
/*
账号查询已采用所有权窄接口。
*/
// parsePositiveInt 将正整数文本转换为整数,无法解析时返回备用值。
func parsePositiveInt(raw string, fallback int) int {
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
+10 -10
View File
@@ -317,21 +317,21 @@ func parseMoneyCents(raw string) (int64, error) {
func (s *Server) listItems(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
all, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
cookieIDs, err := s.Store.Cookies.ListOwnedIDs(r.Context(), sess.UserID) // cookieIDs 和 err 是账号 ID 列表及查询错误。
if err != nil {
writeErr(w, http.StatusInternalServerError, "查询失败")
return
}
cookieID := strings.TrimSpace(r.URL.Query().Get("cookie_id"))
if cookieID != "" {
if _, ok := all[cookieID]; !ok {
if !s.cookieOwnedByUser(r.Context(), sess.UserID, cookieID) {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return
}
all = map[string]string{cookieID: all[cookieID]}
cookieIDs = []string{cookieID}
}
result := []map[string]any{}
for cid := range all {
for _, cid := range cookieIDs {
items, _ := s.Store.Items.AllForCookie(r.Context(), cid)
for _, it := range items {
result = append(result, itemToMap(it))
@@ -538,16 +538,15 @@ func (s *Server) syncItemsPageFromAccount(w http.ResponseWriter, r *http.Request
func (s *Server) cookieForCurrentUser(w http.ResponseWriter, r *http.Request, cookieID string) (string, int64, bool) {
sess := auth.SessionFromContext(r.Context())
all, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
value, err := s.Store.Cookies.GetValueOwned(r.Context(), sess.UserID, cookieID) // value 和 err 是单个 Cookie 明文及读取错误。
if err != nil {
if errors.Is(err, db.ErrNotFound) {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return "", 0, false
}
writeErr(w, http.StatusInternalServerError, "查询账号失败")
return "", 0, false
}
value, ok := all[cookieID]
if !ok {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return "", 0, false
}
if value == "" {
writeErr(w, http.StatusBadRequest, "账号 cookie 为空")
return "", 0, false
@@ -555,6 +554,7 @@ func (s *Server) cookieForCurrentUser(w http.ResponseWriter, r *http.Request, co
return value, sess.UserID, true
}
// saveSyncedItems 以下历史同步流程保持原有行为。
func (s *Server) saveSyncedItems(ctx context.Context, cookieID string, items []mtop.ItemListItem) int {
saved := 0
for _, item := range items {
+2 -2
View File
@@ -299,9 +299,9 @@ func (s *Server) mountItemRepliesReal(r chi.Router) {
func (s *Server) listItemReplies(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
all, _ := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
cookieIDs, _ := s.Store.Cookies.ListOwnedIDs(r.Context(), sess.UserID) // cookieIDs 是当前用户拥有的账号 ID。
var result []map[string]any
for cid := range all {
for _, cid := range cookieIDs {
rows, _ := s.Store.ItemReps.AllForUser(r.Context(), cid)
for _, ir := range rows {
result = append(result, map[string]any{