gitea源码

block.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. "code.gitea.io/gitea/modules/container"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "code.gitea.io/gitea/modules/util"
  10. "xorm.io/builder"
  11. )
  12. var (
  13. ErrBlockOrganization = util.NewInvalidArgumentErrorf("cannot block an organization")
  14. ErrCanNotBlock = util.NewInvalidArgumentErrorf("cannot block the user")
  15. ErrCanNotUnblock = util.NewInvalidArgumentErrorf("cannot unblock the user")
  16. ErrBlockedUser = util.NewPermissionDeniedErrorf("user is blocked")
  17. )
  18. type Blocking struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. BlockerID int64 `xorm:"UNIQUE(block)"`
  21. Blocker *User `xorm:"-"`
  22. BlockeeID int64 `xorm:"UNIQUE(block)"`
  23. Blockee *User `xorm:"-"`
  24. Note string
  25. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  26. }
  27. func (*Blocking) TableName() string {
  28. return "user_blocking"
  29. }
  30. func init() {
  31. db.RegisterModel(new(Blocking))
  32. }
  33. func UpdateBlockingNote(ctx context.Context, id int64, note string) error {
  34. _, err := db.GetEngine(ctx).ID(id).Cols("note").Update(&Blocking{Note: note})
  35. return err
  36. }
  37. func IsUserBlockedBy(ctx context.Context, blockee *User, blockerIDs ...int64) bool {
  38. if len(blockerIDs) == 0 {
  39. return false
  40. }
  41. if blockee.IsAdmin {
  42. return false
  43. }
  44. cond := builder.Eq{"user_blocking.blockee_id": blockee.ID}.
  45. And(builder.In("user_blocking.blocker_id", blockerIDs))
  46. has, _ := db.GetEngine(ctx).Where(cond).Exist(&Blocking{})
  47. return has
  48. }
  49. type FindBlockingOptions struct {
  50. db.ListOptions
  51. BlockerID int64
  52. BlockeeID int64
  53. }
  54. func (opts *FindBlockingOptions) ToConds() builder.Cond {
  55. cond := builder.NewCond()
  56. if opts.BlockerID != 0 {
  57. cond = cond.And(builder.Eq{"user_blocking.blocker_id": opts.BlockerID})
  58. }
  59. if opts.BlockeeID != 0 {
  60. cond = cond.And(builder.Eq{"user_blocking.blockee_id": opts.BlockeeID})
  61. }
  62. return cond
  63. }
  64. func FindBlockings(ctx context.Context, opts *FindBlockingOptions) ([]*Blocking, int64, error) {
  65. return db.FindAndCount[Blocking](ctx, opts)
  66. }
  67. func GetBlocking(ctx context.Context, blockerID, blockeeID int64) (*Blocking, error) {
  68. blocks, _, err := FindBlockings(ctx, &FindBlockingOptions{
  69. BlockerID: blockerID,
  70. BlockeeID: blockeeID,
  71. })
  72. if err != nil {
  73. return nil, err
  74. }
  75. if len(blocks) == 0 {
  76. return nil, nil
  77. }
  78. return blocks[0], nil
  79. }
  80. type BlockingList []*Blocking
  81. func (blocks BlockingList) LoadAttributes(ctx context.Context) error {
  82. ids := make(container.Set[int64], len(blocks)*2)
  83. for _, b := range blocks {
  84. ids.Add(b.BlockerID)
  85. ids.Add(b.BlockeeID)
  86. }
  87. userList, err := GetUsersByIDs(ctx, ids.Values())
  88. if err != nil {
  89. return err
  90. }
  91. userMap := make(map[int64]*User, len(userList))
  92. for _, u := range userList {
  93. userMap[u.ID] = u
  94. }
  95. for _, b := range blocks {
  96. b.Blocker = userMap[b.BlockerID]
  97. b.Blockee = userMap[b.BlockeeID]
  98. }
  99. return nil
  100. }