gitea源码

convert.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package feed
  4. import (
  5. "fmt"
  6. "html"
  7. "html/template"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. activities_model "code.gitea.io/gitea/models/activities"
  13. "code.gitea.io/gitea/models/renderhelper"
  14. repo_model "code.gitea.io/gitea/models/repo"
  15. "code.gitea.io/gitea/modules/markup/markdown"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/templates"
  18. "code.gitea.io/gitea/modules/util"
  19. "code.gitea.io/gitea/services/context"
  20. "github.com/gorilla/feeds"
  21. )
  22. func toBranchLink(ctx *context.Context, act *activities_model.Action) string {
  23. return act.GetRepoAbsoluteLink(ctx) + "/src/branch/" + util.PathEscapeSegments(act.GetBranch())
  24. }
  25. func toTagLink(ctx *context.Context, act *activities_model.Action) string {
  26. return act.GetRepoAbsoluteLink(ctx) + "/src/tag/" + util.PathEscapeSegments(act.GetTag())
  27. }
  28. func toIssueLink(ctx *context.Context, act *activities_model.Action) string {
  29. return act.GetRepoAbsoluteLink(ctx) + "/issues/" + url.PathEscape(act.GetIssueInfos()[0])
  30. }
  31. func toPullLink(ctx *context.Context, act *activities_model.Action) string {
  32. return act.GetRepoAbsoluteLink(ctx) + "/pulls/" + url.PathEscape(act.GetIssueInfos()[0])
  33. }
  34. func toSrcLink(ctx *context.Context, act *activities_model.Action) string {
  35. return act.GetRepoAbsoluteLink(ctx) + "/src/" + util.PathEscapeSegments(act.GetBranch())
  36. }
  37. func toReleaseLink(ctx *context.Context, act *activities_model.Action) string {
  38. return act.GetRepoAbsoluteLink(ctx) + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
  39. }
  40. // renderCommentMarkdown renders the comment markdown to html
  41. func renderCommentMarkdown(ctx *context.Context, act *activities_model.Action, content string) template.HTML {
  42. _ = act.LoadRepo(ctx)
  43. if act.Repo == nil {
  44. return ""
  45. }
  46. rctx := renderhelper.NewRenderContextRepoComment(ctx, act.Repo).WithUseAbsoluteLink(true)
  47. rendered, err := markdown.RenderString(rctx, content)
  48. if err != nil {
  49. return ""
  50. }
  51. return rendered
  52. }
  53. // feedActionsToFeedItems convert gitea's Action feed to feeds Item
  54. func feedActionsToFeedItems(ctx *context.Context, actions activities_model.ActionList) (items []*feeds.Item, err error) {
  55. renderUtils := templates.NewRenderUtils(ctx)
  56. for _, act := range actions {
  57. act.LoadActUser(ctx)
  58. // TODO: the code seems quite strange (maybe not right)
  59. // sometimes it uses text content but sometimes it uses HTML content
  60. // it should clearly defines which kind of content it should use for the feed items: plan text or rich HTML
  61. var title, desc string
  62. var content template.HTML
  63. link := &feeds.Link{Href: act.GetCommentHTMLURL(ctx)}
  64. // title
  65. title = act.ActUser.GetDisplayName() + " "
  66. var titleExtra template.HTML
  67. switch act.OpType {
  68. case activities_model.ActionCreateRepo:
  69. titleExtra = ctx.Locale.Tr("action.create_repo", act.GetRepoAbsoluteLink(ctx), act.ShortRepoPath(ctx))
  70. link.Href = act.GetRepoAbsoluteLink(ctx)
  71. case activities_model.ActionRenameRepo:
  72. titleExtra = ctx.Locale.Tr("action.rename_repo", act.GetContent(), act.GetRepoAbsoluteLink(ctx), act.ShortRepoPath(ctx))
  73. link.Href = act.GetRepoAbsoluteLink(ctx)
  74. case activities_model.ActionCommitRepo:
  75. link.Href = toBranchLink(ctx, act)
  76. if len(act.Content) != 0 {
  77. titleExtra = ctx.Locale.Tr("action.commit_repo", act.GetRepoAbsoluteLink(ctx), link.Href, act.GetBranch(), act.ShortRepoPath(ctx))
  78. } else {
  79. titleExtra = ctx.Locale.Tr("action.create_branch", act.GetRepoAbsoluteLink(ctx), link.Href, act.GetBranch(), act.ShortRepoPath(ctx))
  80. }
  81. case activities_model.ActionCreateIssue:
  82. link.Href = toIssueLink(ctx, act)
  83. titleExtra = ctx.Locale.Tr("action.create_issue", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  84. case activities_model.ActionCreatePullRequest:
  85. link.Href = toPullLink(ctx, act)
  86. titleExtra = ctx.Locale.Tr("action.create_pull_request", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  87. case activities_model.ActionTransferRepo:
  88. link.Href = act.GetRepoAbsoluteLink(ctx)
  89. titleExtra = ctx.Locale.Tr("action.transfer_repo", act.GetContent(), act.GetRepoAbsoluteLink(ctx), act.ShortRepoPath(ctx))
  90. case activities_model.ActionPushTag:
  91. link.Href = toTagLink(ctx, act)
  92. titleExtra = ctx.Locale.Tr("action.push_tag", act.GetRepoAbsoluteLink(ctx), link.Href, act.GetTag(), act.ShortRepoPath(ctx))
  93. case activities_model.ActionCommentIssue:
  94. issueLink := toIssueLink(ctx, act)
  95. if link.Href == "#" {
  96. link.Href = issueLink
  97. }
  98. titleExtra = ctx.Locale.Tr("action.comment_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  99. case activities_model.ActionMergePullRequest:
  100. pullLink := toPullLink(ctx, act)
  101. if link.Href == "#" {
  102. link.Href = pullLink
  103. }
  104. titleExtra = ctx.Locale.Tr("action.merge_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  105. case activities_model.ActionAutoMergePullRequest:
  106. pullLink := toPullLink(ctx, act)
  107. if link.Href == "#" {
  108. link.Href = pullLink
  109. }
  110. titleExtra = ctx.Locale.Tr("action.auto_merge_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  111. case activities_model.ActionCloseIssue:
  112. issueLink := toIssueLink(ctx, act)
  113. if link.Href == "#" {
  114. link.Href = issueLink
  115. }
  116. titleExtra = ctx.Locale.Tr("action.close_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  117. case activities_model.ActionReopenIssue:
  118. issueLink := toIssueLink(ctx, act)
  119. if link.Href == "#" {
  120. link.Href = issueLink
  121. }
  122. titleExtra = ctx.Locale.Tr("action.reopen_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  123. case activities_model.ActionClosePullRequest:
  124. pullLink := toPullLink(ctx, act)
  125. if link.Href == "#" {
  126. link.Href = pullLink
  127. }
  128. titleExtra = ctx.Locale.Tr("action.close_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  129. case activities_model.ActionReopenPullRequest:
  130. pullLink := toPullLink(ctx, act)
  131. if link.Href == "#" {
  132. link.Href = pullLink
  133. }
  134. titleExtra = ctx.Locale.Tr("action.reopen_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  135. case activities_model.ActionDeleteTag:
  136. link.Href = act.GetRepoAbsoluteLink(ctx)
  137. titleExtra = ctx.Locale.Tr("action.delete_tag", act.GetRepoAbsoluteLink(ctx), act.GetTag(), act.ShortRepoPath(ctx))
  138. case activities_model.ActionDeleteBranch:
  139. link.Href = act.GetRepoAbsoluteLink(ctx)
  140. titleExtra = ctx.Locale.Tr("action.delete_branch", act.GetRepoAbsoluteLink(ctx), html.EscapeString(act.GetBranch()), act.ShortRepoPath(ctx))
  141. case activities_model.ActionMirrorSyncPush:
  142. srcLink := toSrcLink(ctx, act)
  143. if link.Href == "#" {
  144. link.Href = srcLink
  145. }
  146. titleExtra = ctx.Locale.Tr("action.mirror_sync_push", act.GetRepoAbsoluteLink(ctx), srcLink, act.GetBranch(), act.ShortRepoPath(ctx))
  147. case activities_model.ActionMirrorSyncCreate:
  148. srcLink := toSrcLink(ctx, act)
  149. if link.Href == "#" {
  150. link.Href = srcLink
  151. }
  152. titleExtra = ctx.Locale.Tr("action.mirror_sync_create", act.GetRepoAbsoluteLink(ctx), srcLink, act.GetBranch(), act.ShortRepoPath(ctx))
  153. case activities_model.ActionMirrorSyncDelete:
  154. link.Href = act.GetRepoAbsoluteLink(ctx)
  155. titleExtra = ctx.Locale.Tr("action.mirror_sync_delete", act.GetRepoAbsoluteLink(ctx), act.GetBranch(), act.ShortRepoPath(ctx))
  156. case activities_model.ActionApprovePullRequest:
  157. pullLink := toPullLink(ctx, act)
  158. titleExtra = ctx.Locale.Tr("action.approve_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  159. case activities_model.ActionRejectPullRequest:
  160. pullLink := toPullLink(ctx, act)
  161. titleExtra = ctx.Locale.Tr("action.reject_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  162. case activities_model.ActionCommentPull:
  163. pullLink := toPullLink(ctx, act)
  164. titleExtra = ctx.Locale.Tr("action.comment_pull", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
  165. case activities_model.ActionPublishRelease:
  166. releaseLink := toReleaseLink(ctx, act)
  167. if link.Href == "#" {
  168. link.Href = releaseLink
  169. }
  170. titleExtra = ctx.Locale.Tr("action.publish_release", act.GetRepoAbsoluteLink(ctx), releaseLink, act.ShortRepoPath(ctx), act.Content)
  171. case activities_model.ActionPullReviewDismissed:
  172. pullLink := toPullLink(ctx, act)
  173. titleExtra = ctx.Locale.Tr("action.review_dismissed", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx), act.GetIssueInfos()[1])
  174. case activities_model.ActionStarRepo:
  175. link.Href = act.GetRepoAbsoluteLink(ctx)
  176. titleExtra = ctx.Locale.Tr("action.starred_repo", act.GetRepoAbsoluteLink(ctx), act.GetRepoPath(ctx))
  177. case activities_model.ActionWatchRepo:
  178. link.Href = act.GetRepoAbsoluteLink(ctx)
  179. titleExtra = ctx.Locale.Tr("action.watched_repo", act.GetRepoAbsoluteLink(ctx), act.GetRepoPath(ctx))
  180. default:
  181. return nil, fmt.Errorf("unknown action type: %v", act.OpType)
  182. }
  183. // description & content
  184. {
  185. switch act.OpType {
  186. case activities_model.ActionCommitRepo, activities_model.ActionMirrorSyncPush:
  187. push := templates.ActionContent2Commits(act)
  188. _ = act.LoadRepo(ctx)
  189. for _, commit := range push.Commits {
  190. if len(desc) != 0 {
  191. desc += "\n\n"
  192. }
  193. desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s",
  194. html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(ctx), commit.Sha1)),
  195. commit.Sha1,
  196. renderUtils.RenderCommitMessage(commit.Message, act.Repo),
  197. )
  198. }
  199. if push.Len > 1 {
  200. link = &feeds.Link{Href: fmt.Sprintf("%s/%s", setting.AppSubURL, push.CompareURL)}
  201. } else if push.Len == 1 {
  202. link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(ctx), push.Commits[0].Sha1)}
  203. }
  204. case activities_model.ActionCreateIssue, activities_model.ActionCreatePullRequest:
  205. desc = strings.Join(act.GetIssueInfos(), "#")
  206. content = renderCommentMarkdown(ctx, act, act.GetIssueContent(ctx))
  207. case activities_model.ActionCommentIssue, activities_model.ActionApprovePullRequest, activities_model.ActionRejectPullRequest, activities_model.ActionCommentPull:
  208. desc = act.GetIssueTitle(ctx)
  209. comment := act.GetIssueInfos()[1]
  210. if len(comment) != 0 {
  211. desc += "\n\n" + string(renderCommentMarkdown(ctx, act, comment))
  212. }
  213. case activities_model.ActionMergePullRequest, activities_model.ActionAutoMergePullRequest:
  214. desc = act.GetIssueInfos()[1]
  215. case activities_model.ActionCloseIssue, activities_model.ActionReopenIssue, activities_model.ActionClosePullRequest, activities_model.ActionReopenPullRequest:
  216. desc = act.GetIssueTitle(ctx)
  217. case activities_model.ActionPullReviewDismissed:
  218. desc = ctx.Locale.TrString("action.review_dismissed_reason") + "\n\n" + act.GetIssueInfos()[2]
  219. }
  220. }
  221. if len(content) == 0 {
  222. content = templates.SanitizeHTML(desc)
  223. }
  224. items = append(items, &feeds.Item{
  225. Title: template.HTMLEscapeString(title) + string(titleExtra),
  226. Link: link,
  227. Description: desc,
  228. IsPermaLink: "false",
  229. Author: &feeds.Author{
  230. Name: act.ActUser.GetDisplayName(),
  231. Email: act.ActUser.GetEmail(),
  232. },
  233. Id: fmt.Sprintf("%v: %v", strconv.FormatInt(act.ID, 10), link.Href),
  234. Created: act.CreatedUnix.AsTime(),
  235. Content: string(content),
  236. })
  237. }
  238. return items, err
  239. }
  240. // GetFeedType return if it is a feed request and altered name and feed type.
  241. func GetFeedType(name string, req *http.Request) (showFeed bool, feedType string) {
  242. if strings.HasSuffix(name, ".rss") ||
  243. strings.Contains(req.Header.Get("Accept"), "application/rss+xml") {
  244. return true, "rss"
  245. }
  246. if strings.HasSuffix(name, ".atom") ||
  247. strings.Contains(req.Header.Get("Accept"), "application/atom+xml") {
  248. return true, "atom"
  249. }
  250. return false, ""
  251. }
  252. // feedActionsToFeedItems convert gitea's Repo's Releases to feeds Item
  253. func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (items []*feeds.Item, err error) {
  254. for _, rel := range releases {
  255. err := rel.LoadAttributes(ctx)
  256. if err != nil {
  257. return nil, err
  258. }
  259. var title string
  260. var content template.HTML
  261. if rel.IsTag {
  262. title = rel.TagName
  263. } else {
  264. title = rel.Title
  265. }
  266. link := &feeds.Link{Href: rel.HTMLURL()}
  267. rctx := renderhelper.NewRenderContextRepoComment(ctx, rel.Repo).WithUseAbsoluteLink(true)
  268. content, err = markdown.RenderString(rctx,
  269. rel.Note)
  270. if err != nil {
  271. return nil, err
  272. }
  273. items = append(items, &feeds.Item{
  274. Title: title,
  275. Link: link,
  276. Created: rel.CreatedUnix.AsTime(),
  277. Author: &feeds.Author{
  278. Name: rel.Publisher.GetDisplayName(),
  279. Email: rel.Publisher.GetEmail(),
  280. },
  281. Id: fmt.Sprintf("%v: %v", strconv.FormatInt(rel.ID, 10), link.Href),
  282. Content: string(content),
  283. })
  284. }
  285. return items, err
  286. }