gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package forms

import (
	"math/big"

	issues_model "code.gitea.io/gitea/models/issues"
	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/log"
)

type hiddenCommentTypeGroupsType map[string][]issues_model.CommentType

// hiddenCommentTypeGroups maps the group names to comment types, these group names comes from the Web UI (appearance.tmpl)
var hiddenCommentTypeGroups = hiddenCommentTypeGroupsType{
	"reference": {
		/*3*/ issues_model.CommentTypeIssueRef,
		/*4*/ issues_model.CommentTypeCommitRef,
		/*5*/ issues_model.CommentTypeCommentRef,
		/*6*/ issues_model.CommentTypePullRef,
	},
	"label": {
		/*7*/ issues_model.CommentTypeLabel,
	},
	"milestone": {
		/*8*/ issues_model.CommentTypeMilestone,
	},
	"assignee": {
		/*9*/ issues_model.CommentTypeAssignees,
	},
	"title": {
		/*10*/ issues_model.CommentTypeChangeTitle,
	},
	"branch": {
		/*11*/ issues_model.CommentTypeDeleteBranch,
		/*25*/ issues_model.CommentTypeChangeTargetBranch,
	},
	"time_tracking": {
		/*12*/ issues_model.CommentTypeStartTracking,
		/*13*/ issues_model.CommentTypeStopTracking,
		/*14*/ issues_model.CommentTypeAddTimeManual,
		/*15*/ issues_model.CommentTypeCancelTracking,
		/*26*/ issues_model.CommentTypeDeleteTimeManual,
	},
	"deadline": {
		/*16*/ issues_model.CommentTypeAddedDeadline,
		/*17*/ issues_model.CommentTypeModifiedDeadline,
		/*18*/ issues_model.CommentTypeRemovedDeadline,
	},
	"dependency": {
		/*19*/ issues_model.CommentTypeAddDependency,
		/*20*/ issues_model.CommentTypeRemoveDependency,
	},
	"lock": {
		/*23*/ issues_model.CommentTypeLock,
		/*24*/ issues_model.CommentTypeUnlock,
	},
	"review_request": {
		/*27*/ issues_model.CommentTypeReviewRequest,
	},
	"pull_request_push": {
		/*29*/ issues_model.CommentTypePullRequestPush,
	},
	"project": {
		/*30*/ issues_model.CommentTypeProject,
		/*31*/ issues_model.CommentTypeProjectBoard,
	},
	"issue_ref": {
		/*33*/ issues_model.CommentTypeChangeIssueRef,
	},
}

// UserHiddenCommentTypesFromRequest parse the form to hidden comment types bitset
func UserHiddenCommentTypesFromRequest(ctx *context.Context) *big.Int {
	bitset := new(big.Int)
	for group, commentTypes := range hiddenCommentTypeGroups {
		if ctx.FormBool(group) {
			for _, commentType := range commentTypes {
				bitset = bitset.SetBit(bitset, int(commentType), 1)
			}
		}
	}
	return bitset
}

// IsUserHiddenCommentTypeGroupChecked check whether a hidden comment type group is "enabled" (checked on UI)
func IsUserHiddenCommentTypeGroupChecked(group string, hiddenCommentTypes *big.Int) (ret bool) {
	commentTypes, ok := hiddenCommentTypeGroups[group]
	if !ok {
		log.Critical("the group map for hidden comment types is out of sync, unknown group: %v", group)
		return
	}
	if hiddenCommentTypes == nil {
		return false
	}
	for _, commentType := range commentTypes {
		if hiddenCommentTypes.Bit(int(commentType)) == 1 {
			return true
		}
	}
	return false
}