You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/timeutil"
  16. "github.com/unknwon/com"
  17. "xorm.io/builder"
  18. )
  19. // ActionType represents the type of an action.
  20. type ActionType int
  21. // Possible action types.
  22. const (
  23. ActionCreateRepo ActionType = iota + 1 // 1
  24. ActionRenameRepo // 2
  25. ActionStarRepo // 3
  26. ActionWatchRepo // 4
  27. ActionCommitRepo // 5
  28. ActionCreateIssue // 6
  29. ActionCreatePullRequest // 7
  30. ActionTransferRepo // 8
  31. ActionPushTag // 9
  32. ActionCommentIssue // 10
  33. ActionMergePullRequest // 11
  34. ActionCloseIssue // 12
  35. ActionReopenIssue // 13
  36. ActionClosePullRequest // 14
  37. ActionReopenPullRequest // 15
  38. ActionDeleteTag // 16
  39. ActionDeleteBranch // 17
  40. ActionMirrorSyncPush // 18
  41. ActionMirrorSyncCreate // 19
  42. ActionMirrorSyncDelete // 20
  43. ActionApprovePullRequest // 21
  44. ActionRejectPullRequest // 22
  45. ActionCommentPull // 23
  46. )
  47. // Action represents user operation type and other information to
  48. // repository. It implemented interface base.Actioner so that can be
  49. // used in template render.
  50. type Action struct {
  51. ID int64 `xorm:"pk autoincr"`
  52. UserID int64 `xorm:"INDEX"` // Receiver user id.
  53. OpType ActionType
  54. ActUserID int64 `xorm:"INDEX"` // Action user id.
  55. ActUser *User `xorm:"-"`
  56. RepoID int64 `xorm:"INDEX"`
  57. Repo *Repository `xorm:"-"`
  58. CommentID int64 `xorm:"INDEX"`
  59. Comment *Comment `xorm:"-"`
  60. IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
  61. RefName string
  62. IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
  63. Content string `xorm:"TEXT"`
  64. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  65. }
  66. // GetOpType gets the ActionType of this action.
  67. func (a *Action) GetOpType() ActionType {
  68. return a.OpType
  69. }
  70. func (a *Action) loadActUser() {
  71. if a.ActUser != nil {
  72. return
  73. }
  74. var err error
  75. a.ActUser, err = GetUserByID(a.ActUserID)
  76. if err == nil {
  77. return
  78. } else if IsErrUserNotExist(err) {
  79. a.ActUser = NewGhostUser()
  80. } else {
  81. log.Error("GetUserByID(%d): %v", a.ActUserID, err)
  82. }
  83. }
  84. func (a *Action) loadRepo() {
  85. if a.Repo != nil {
  86. return
  87. }
  88. var err error
  89. a.Repo, err = GetRepositoryByID(a.RepoID)
  90. if err != nil {
  91. log.Error("GetRepositoryByID(%d): %v", a.RepoID, err)
  92. }
  93. }
  94. // GetActFullName gets the action's user full name.
  95. func (a *Action) GetActFullName() string {
  96. a.loadActUser()
  97. return a.ActUser.FullName
  98. }
  99. // GetActUserName gets the action's user name.
  100. func (a *Action) GetActUserName() string {
  101. a.loadActUser()
  102. return a.ActUser.Name
  103. }
  104. // ShortActUserName gets the action's user name trimmed to max 20
  105. // chars.
  106. func (a *Action) ShortActUserName() string {
  107. return base.EllipsisString(a.GetActUserName(), 20)
  108. }
  109. // GetDisplayName gets the action's display name based on DEFAULT_SHOW_FULL_NAME, or falls back to the username if it is blank.
  110. func (a *Action) GetDisplayName() string {
  111. if setting.UI.DefaultShowFullName {
  112. trimmedFullName := strings.TrimSpace(a.GetActFullName())
  113. if len(trimmedFullName) > 0 {
  114. return trimmedFullName
  115. }
  116. }
  117. return a.ShortActUserName()
  118. }
  119. // GetDisplayNameTitle gets the action's display name used for the title (tooltip) based on DEFAULT_SHOW_FULL_NAME
  120. func (a *Action) GetDisplayNameTitle() string {
  121. if setting.UI.DefaultShowFullName {
  122. return a.ShortActUserName()
  123. }
  124. return a.GetActFullName()
  125. }
  126. // GetActAvatar the action's user's avatar link
  127. func (a *Action) GetActAvatar() string {
  128. a.loadActUser()
  129. return a.ActUser.RelAvatarLink()
  130. }
  131. // GetRepoUserName returns the name of the action repository owner.
  132. func (a *Action) GetRepoUserName() string {
  133. a.loadRepo()
  134. return a.Repo.OwnerName
  135. }
  136. // ShortRepoUserName returns the name of the action repository owner
  137. // trimmed to max 20 chars.
  138. func (a *Action) ShortRepoUserName() string {
  139. return base.EllipsisString(a.GetRepoUserName(), 20)
  140. }
  141. // GetRepoName returns the name of the action repository.
  142. func (a *Action) GetRepoName() string {
  143. a.loadRepo()
  144. return a.Repo.Name
  145. }
  146. // ShortRepoName returns the name of the action repository
  147. // trimmed to max 33 chars.
  148. func (a *Action) ShortRepoName() string {
  149. return base.EllipsisString(a.GetRepoName(), 33)
  150. }
  151. // GetRepoPath returns the virtual path to the action repository.
  152. func (a *Action) GetRepoPath() string {
  153. return path.Join(a.GetRepoUserName(), a.GetRepoName())
  154. }
  155. // ShortRepoPath returns the virtual path to the action repository
  156. // trimmed to max 20 + 1 + 33 chars.
  157. func (a *Action) ShortRepoPath() string {
  158. return path.Join(a.ShortRepoUserName(), a.ShortRepoName())
  159. }
  160. // GetRepoLink returns relative link to action repository.
  161. func (a *Action) GetRepoLink() string {
  162. if len(setting.AppSubURL) > 0 {
  163. return path.Join(setting.AppSubURL, a.GetRepoPath())
  164. }
  165. return "/" + a.GetRepoPath()
  166. }
  167. // GetRepositoryFromMatch returns a *Repository from a username and repo strings
  168. func GetRepositoryFromMatch(ownerName string, repoName string) (*Repository, error) {
  169. var err error
  170. refRepo, err := GetRepositoryByOwnerAndName(ownerName, repoName)
  171. if err != nil {
  172. if IsErrRepoNotExist(err) {
  173. log.Warn("Repository referenced in commit but does not exist: %v", err)
  174. return nil, err
  175. }
  176. log.Error("GetRepositoryByOwnerAndName: %v", err)
  177. return nil, err
  178. }
  179. return refRepo, nil
  180. }
  181. // GetCommentLink returns link to action comment.
  182. func (a *Action) GetCommentLink() string {
  183. return a.getCommentLink(x)
  184. }
  185. func (a *Action) getCommentLink(e Engine) string {
  186. if a == nil {
  187. return "#"
  188. }
  189. if a.Comment == nil && a.CommentID != 0 {
  190. a.Comment, _ = getCommentByID(e, a.CommentID)
  191. }
  192. if a.Comment != nil {
  193. return a.Comment.HTMLURL()
  194. }
  195. if len(a.GetIssueInfos()) == 0 {
  196. return "#"
  197. }
  198. //Return link to issue
  199. issueIDString := a.GetIssueInfos()[0]
  200. issueID, err := strconv.ParseInt(issueIDString, 10, 64)
  201. if err != nil {
  202. return "#"
  203. }
  204. issue, err := getIssueByID(e, issueID)
  205. if err != nil {
  206. return "#"
  207. }
  208. if err = issue.loadRepo(e); err != nil {
  209. return "#"
  210. }
  211. return issue.HTMLURL()
  212. }
  213. // GetBranch returns the action's repository branch.
  214. func (a *Action) GetBranch() string {
  215. return a.RefName
  216. }
  217. // GetContent returns the action's content.
  218. func (a *Action) GetContent() string {
  219. return a.Content
  220. }
  221. // GetCreate returns the action creation time.
  222. func (a *Action) GetCreate() time.Time {
  223. return a.CreatedUnix.AsTime()
  224. }
  225. // GetIssueInfos returns a list of issues associated with
  226. // the action.
  227. func (a *Action) GetIssueInfos() []string {
  228. return strings.SplitN(a.Content, "|", 2)
  229. }
  230. // GetIssueTitle returns the title of first issue associated
  231. // with the action.
  232. func (a *Action) GetIssueTitle() string {
  233. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  234. issue, err := GetIssueByIndex(a.RepoID, index)
  235. if err != nil {
  236. log.Error("GetIssueByIndex: %v", err)
  237. return "500 when get issue"
  238. }
  239. return issue.Title
  240. }
  241. // GetIssueContent returns the content of first issue associated with
  242. // this action.
  243. func (a *Action) GetIssueContent() string {
  244. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  245. issue, err := GetIssueByIndex(a.RepoID, index)
  246. if err != nil {
  247. log.Error("GetIssueByIndex: %v", err)
  248. return "500 when get issue"
  249. }
  250. return issue.Content
  251. }
  252. // GetFeedsOptions options for retrieving feeds
  253. type GetFeedsOptions struct {
  254. RequestedUser *User // the user we want activity for
  255. Actor *User // the user viewing the activity
  256. IncludePrivate bool // include private actions
  257. OnlyPerformedBy bool // only actions performed by requested user
  258. IncludeDeleted bool // include deleted actions
  259. }
  260. // GetFeeds returns actions according to the provided options
  261. func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
  262. cond := builder.NewCond()
  263. var repoIDs []int64
  264. var actorID int64
  265. if opts.Actor != nil {
  266. actorID = opts.Actor.ID
  267. }
  268. if opts.RequestedUser.IsOrganization() {
  269. env, err := opts.RequestedUser.AccessibleReposEnv(actorID)
  270. if err != nil {
  271. return nil, fmt.Errorf("AccessibleReposEnv: %v", err)
  272. }
  273. if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
  274. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  275. }
  276. cond = cond.And(builder.In("repo_id", repoIDs))
  277. } else {
  278. cond = cond.And(builder.In("repo_id", AccessibleRepoIDsQuery(opts.Actor)))
  279. }
  280. cond = cond.And(builder.Eq{"user_id": opts.RequestedUser.ID})
  281. if opts.OnlyPerformedBy {
  282. cond = cond.And(builder.Eq{"act_user_id": opts.RequestedUser.ID})
  283. }
  284. if !opts.IncludePrivate {
  285. cond = cond.And(builder.Eq{"is_private": false})
  286. }
  287. if !opts.IncludeDeleted {
  288. cond = cond.And(builder.Eq{"is_deleted": false})
  289. }
  290. actions := make([]*Action, 0, 20)
  291. if err := x.Limit(20).Desc("id").Where(cond).Find(&actions); err != nil {
  292. return nil, fmt.Errorf("Find: %v", err)
  293. }
  294. if err := ActionList(actions).LoadAttributes(); err != nil {
  295. return nil, fmt.Errorf("LoadAttributes: %v", err)
  296. }
  297. return actions, nil
  298. }