first-commit
This commit is contained in:
50
routers/web/feed/branch.go
Normal file
50
routers/web/feed/branch.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
// ShowBranchFeed shows tags and/or releases on the repo as RSS / Atom feed
|
||||
func ShowBranchFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
|
||||
commits, err := ctx.Repo.Commit.CommitsByRange(0, 10, "", "", "")
|
||||
if err != nil {
|
||||
ctx.ServerError("ShowBranchFeed", err)
|
||||
return
|
||||
}
|
||||
|
||||
title := "Latest commits for branch " + ctx.Repo.BranchName
|
||||
link := &feeds.Link{Href: repo.HTMLURL() + "/" + ctx.Repo.RefTypeNameSubURL()}
|
||||
|
||||
feed := &feeds.Feed{
|
||||
Title: title,
|
||||
Link: link,
|
||||
Description: repo.Description,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
for _, commit := range commits {
|
||||
feed.Items = append(feed.Items, &feeds.Item{
|
||||
Id: commit.ID.String(),
|
||||
Title: strings.TrimSpace(strings.Split(commit.Message(), "\n")[0]),
|
||||
Link: &feeds.Link{Href: repo.HTMLURL() + "/commit/" + commit.ID.String()},
|
||||
Author: &feeds.Author{
|
||||
Name: commit.Author.Name,
|
||||
Email: commit.Author.Email,
|
||||
},
|
||||
Description: commit.Message(),
|
||||
Content: commit.Message(),
|
||||
Created: commit.Committer.When,
|
||||
})
|
||||
}
|
||||
|
||||
writeFeed(ctx, feed, formatType)
|
||||
}
|
314
routers/web/feed/convert.go
Normal file
314
routers/web/feed/convert.go
Normal file
@@ -0,0 +1,314 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
activities_model "code.gitea.io/gitea/models/activities"
|
||||
"code.gitea.io/gitea/models/renderhelper"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/templates"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
func toBranchLink(ctx *context.Context, act *activities_model.Action) string {
|
||||
return act.GetRepoAbsoluteLink(ctx) + "/src/branch/" + util.PathEscapeSegments(act.GetBranch())
|
||||
}
|
||||
|
||||
func toTagLink(ctx *context.Context, act *activities_model.Action) string {
|
||||
return act.GetRepoAbsoluteLink(ctx) + "/src/tag/" + util.PathEscapeSegments(act.GetTag())
|
||||
}
|
||||
|
||||
func toIssueLink(ctx *context.Context, act *activities_model.Action) string {
|
||||
return act.GetRepoAbsoluteLink(ctx) + "/issues/" + url.PathEscape(act.GetIssueInfos()[0])
|
||||
}
|
||||
|
||||
func toPullLink(ctx *context.Context, act *activities_model.Action) string {
|
||||
return act.GetRepoAbsoluteLink(ctx) + "/pulls/" + url.PathEscape(act.GetIssueInfos()[0])
|
||||
}
|
||||
|
||||
func toSrcLink(ctx *context.Context, act *activities_model.Action) string {
|
||||
return act.GetRepoAbsoluteLink(ctx) + "/src/" + util.PathEscapeSegments(act.GetBranch())
|
||||
}
|
||||
|
||||
func toReleaseLink(ctx *context.Context, act *activities_model.Action) string {
|
||||
return act.GetRepoAbsoluteLink(ctx) + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
|
||||
}
|
||||
|
||||
// renderCommentMarkdown renders the comment markdown to html
|
||||
func renderCommentMarkdown(ctx *context.Context, act *activities_model.Action, content string) template.HTML {
|
||||
_ = act.LoadRepo(ctx)
|
||||
if act.Repo == nil {
|
||||
return ""
|
||||
}
|
||||
rctx := renderhelper.NewRenderContextRepoComment(ctx, act.Repo).WithUseAbsoluteLink(true)
|
||||
rendered, err := markdown.RenderString(rctx, content)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return rendered
|
||||
}
|
||||
|
||||
// feedActionsToFeedItems convert gitea's Action feed to feeds Item
|
||||
func feedActionsToFeedItems(ctx *context.Context, actions activities_model.ActionList) (items []*feeds.Item, err error) {
|
||||
renderUtils := templates.NewRenderUtils(ctx)
|
||||
for _, act := range actions {
|
||||
act.LoadActUser(ctx)
|
||||
|
||||
// TODO: the code seems quite strange (maybe not right)
|
||||
// sometimes it uses text content but sometimes it uses HTML content
|
||||
// it should clearly defines which kind of content it should use for the feed items: plan text or rich HTML
|
||||
var title, desc string
|
||||
var content template.HTML
|
||||
|
||||
link := &feeds.Link{Href: act.GetCommentHTMLURL(ctx)}
|
||||
|
||||
// title
|
||||
title = act.ActUser.GetDisplayName() + " "
|
||||
var titleExtra template.HTML
|
||||
switch act.OpType {
|
||||
case activities_model.ActionCreateRepo:
|
||||
titleExtra = ctx.Locale.Tr("action.create_repo", act.GetRepoAbsoluteLink(ctx), act.ShortRepoPath(ctx))
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
case activities_model.ActionRenameRepo:
|
||||
titleExtra = ctx.Locale.Tr("action.rename_repo", act.GetContent(), act.GetRepoAbsoluteLink(ctx), act.ShortRepoPath(ctx))
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
case activities_model.ActionCommitRepo:
|
||||
link.Href = toBranchLink(ctx, act)
|
||||
if len(act.Content) != 0 {
|
||||
titleExtra = ctx.Locale.Tr("action.commit_repo", act.GetRepoAbsoluteLink(ctx), link.Href, act.GetBranch(), act.ShortRepoPath(ctx))
|
||||
} else {
|
||||
titleExtra = ctx.Locale.Tr("action.create_branch", act.GetRepoAbsoluteLink(ctx), link.Href, act.GetBranch(), act.ShortRepoPath(ctx))
|
||||
}
|
||||
case activities_model.ActionCreateIssue:
|
||||
link.Href = toIssueLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.create_issue", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionCreatePullRequest:
|
||||
link.Href = toPullLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.create_pull_request", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionTransferRepo:
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
titleExtra = ctx.Locale.Tr("action.transfer_repo", act.GetContent(), act.GetRepoAbsoluteLink(ctx), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionPushTag:
|
||||
link.Href = toTagLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.push_tag", act.GetRepoAbsoluteLink(ctx), link.Href, act.GetTag(), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionCommentIssue:
|
||||
issueLink := toIssueLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = issueLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.comment_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionMergePullRequest:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = pullLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.merge_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionAutoMergePullRequest:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = pullLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.auto_merge_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionCloseIssue:
|
||||
issueLink := toIssueLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = issueLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.close_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionReopenIssue:
|
||||
issueLink := toIssueLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = issueLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.reopen_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionClosePullRequest:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = pullLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.close_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionReopenPullRequest:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = pullLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.reopen_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionDeleteTag:
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
titleExtra = ctx.Locale.Tr("action.delete_tag", act.GetRepoAbsoluteLink(ctx), act.GetTag(), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionDeleteBranch:
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
titleExtra = ctx.Locale.Tr("action.delete_branch", act.GetRepoAbsoluteLink(ctx), html.EscapeString(act.GetBranch()), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionMirrorSyncPush:
|
||||
srcLink := toSrcLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = srcLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.mirror_sync_push", act.GetRepoAbsoluteLink(ctx), srcLink, act.GetBranch(), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionMirrorSyncCreate:
|
||||
srcLink := toSrcLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = srcLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.mirror_sync_create", act.GetRepoAbsoluteLink(ctx), srcLink, act.GetBranch(), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionMirrorSyncDelete:
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
titleExtra = ctx.Locale.Tr("action.mirror_sync_delete", act.GetRepoAbsoluteLink(ctx), act.GetBranch(), act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionApprovePullRequest:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.approve_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionRejectPullRequest:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.reject_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionCommentPull:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.comment_pull", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
|
||||
case activities_model.ActionPublishRelease:
|
||||
releaseLink := toReleaseLink(ctx, act)
|
||||
if link.Href == "#" {
|
||||
link.Href = releaseLink
|
||||
}
|
||||
titleExtra = ctx.Locale.Tr("action.publish_release", act.GetRepoAbsoluteLink(ctx), releaseLink, act.ShortRepoPath(ctx), act.Content)
|
||||
case activities_model.ActionPullReviewDismissed:
|
||||
pullLink := toPullLink(ctx, act)
|
||||
titleExtra = ctx.Locale.Tr("action.review_dismissed", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx), act.GetIssueInfos()[1])
|
||||
case activities_model.ActionStarRepo:
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
titleExtra = ctx.Locale.Tr("action.starred_repo", act.GetRepoAbsoluteLink(ctx), act.GetRepoPath(ctx))
|
||||
case activities_model.ActionWatchRepo:
|
||||
link.Href = act.GetRepoAbsoluteLink(ctx)
|
||||
titleExtra = ctx.Locale.Tr("action.watched_repo", act.GetRepoAbsoluteLink(ctx), act.GetRepoPath(ctx))
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown action type: %v", act.OpType)
|
||||
}
|
||||
|
||||
// description & content
|
||||
{
|
||||
switch act.OpType {
|
||||
case activities_model.ActionCommitRepo, activities_model.ActionMirrorSyncPush:
|
||||
push := templates.ActionContent2Commits(act)
|
||||
_ = act.LoadRepo(ctx)
|
||||
for _, commit := range push.Commits {
|
||||
if len(desc) != 0 {
|
||||
desc += "\n\n"
|
||||
}
|
||||
desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s",
|
||||
html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(ctx), commit.Sha1)),
|
||||
commit.Sha1,
|
||||
renderUtils.RenderCommitMessage(commit.Message, act.Repo),
|
||||
)
|
||||
}
|
||||
|
||||
if push.Len > 1 {
|
||||
link = &feeds.Link{Href: fmt.Sprintf("%s/%s", setting.AppSubURL, push.CompareURL)}
|
||||
} else if push.Len == 1 {
|
||||
link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoAbsoluteLink(ctx), push.Commits[0].Sha1)}
|
||||
}
|
||||
|
||||
case activities_model.ActionCreateIssue, activities_model.ActionCreatePullRequest:
|
||||
desc = strings.Join(act.GetIssueInfos(), "#")
|
||||
content = renderCommentMarkdown(ctx, act, act.GetIssueContent(ctx))
|
||||
case activities_model.ActionCommentIssue, activities_model.ActionApprovePullRequest, activities_model.ActionRejectPullRequest, activities_model.ActionCommentPull:
|
||||
desc = act.GetIssueTitle(ctx)
|
||||
comment := act.GetIssueInfos()[1]
|
||||
if len(comment) != 0 {
|
||||
desc += "\n\n" + string(renderCommentMarkdown(ctx, act, comment))
|
||||
}
|
||||
case activities_model.ActionMergePullRequest, activities_model.ActionAutoMergePullRequest:
|
||||
desc = act.GetIssueInfos()[1]
|
||||
case activities_model.ActionCloseIssue, activities_model.ActionReopenIssue, activities_model.ActionClosePullRequest, activities_model.ActionReopenPullRequest:
|
||||
desc = act.GetIssueTitle(ctx)
|
||||
case activities_model.ActionPullReviewDismissed:
|
||||
desc = ctx.Locale.TrString("action.review_dismissed_reason") + "\n\n" + act.GetIssueInfos()[2]
|
||||
}
|
||||
}
|
||||
if len(content) == 0 {
|
||||
content = templates.SanitizeHTML(desc)
|
||||
}
|
||||
|
||||
items = append(items, &feeds.Item{
|
||||
Title: template.HTMLEscapeString(title) + string(titleExtra),
|
||||
Link: link,
|
||||
Description: desc,
|
||||
IsPermaLink: "false",
|
||||
Author: &feeds.Author{
|
||||
Name: act.ActUser.GetDisplayName(),
|
||||
Email: act.ActUser.GetEmail(),
|
||||
},
|
||||
Id: fmt.Sprintf("%v: %v", strconv.FormatInt(act.ID, 10), link.Href),
|
||||
Created: act.CreatedUnix.AsTime(),
|
||||
Content: string(content),
|
||||
})
|
||||
}
|
||||
return items, err
|
||||
}
|
||||
|
||||
// GetFeedType return if it is a feed request and altered name and feed type.
|
||||
func GetFeedType(name string, req *http.Request) (showFeed bool, feedType string) {
|
||||
if strings.HasSuffix(name, ".rss") ||
|
||||
strings.Contains(req.Header.Get("Accept"), "application/rss+xml") {
|
||||
return true, "rss"
|
||||
}
|
||||
|
||||
if strings.HasSuffix(name, ".atom") ||
|
||||
strings.Contains(req.Header.Get("Accept"), "application/atom+xml") {
|
||||
return true, "atom"
|
||||
}
|
||||
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// feedActionsToFeedItems convert gitea's Repo's Releases to feeds Item
|
||||
func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (items []*feeds.Item, err error) {
|
||||
for _, rel := range releases {
|
||||
err := rel.LoadAttributes(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var title string
|
||||
var content template.HTML
|
||||
|
||||
if rel.IsTag {
|
||||
title = rel.TagName
|
||||
} else {
|
||||
title = rel.Title
|
||||
}
|
||||
|
||||
link := &feeds.Link{Href: rel.HTMLURL()}
|
||||
rctx := renderhelper.NewRenderContextRepoComment(ctx, rel.Repo).WithUseAbsoluteLink(true)
|
||||
content, err = markdown.RenderString(rctx,
|
||||
rel.Note)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items = append(items, &feeds.Item{
|
||||
Title: title,
|
||||
Link: link,
|
||||
Created: rel.CreatedUnix.AsTime(),
|
||||
Author: &feeds.Author{
|
||||
Name: rel.Publisher.GetDisplayName(),
|
||||
Email: rel.Publisher.GetEmail(),
|
||||
},
|
||||
Id: fmt.Sprintf("%v: %v", strconv.FormatInt(rel.ID, 10), link.Href),
|
||||
Content: string(content),
|
||||
})
|
||||
}
|
||||
|
||||
return items, err
|
||||
}
|
62
routers/web/feed/file.go
Normal file
62
routers/web/feed/file.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
// ShowFileFeed shows tags and/or releases on the repo as RSS / Atom feed
|
||||
func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
|
||||
fileName := ctx.Repo.TreePath
|
||||
if len(fileName) == 0 {
|
||||
return
|
||||
}
|
||||
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
|
||||
git.CommitsByFileAndRangeOptions{
|
||||
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
|
||||
File: fileName,
|
||||
Page: 1,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("ShowBranchFeed", err)
|
||||
return
|
||||
}
|
||||
|
||||
title := "Latest commits for file " + ctx.Repo.TreePath
|
||||
|
||||
link := &feeds.Link{Href: repo.HTMLURL() + "/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)}
|
||||
|
||||
feed := &feeds.Feed{
|
||||
Title: title,
|
||||
Link: link,
|
||||
Description: repo.Description,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
for _, commit := range commits {
|
||||
feed.Items = append(feed.Items, &feeds.Item{
|
||||
Id: commit.ID.String(),
|
||||
Title: strings.TrimSpace(strings.Split(commit.Message(), "\n")[0]),
|
||||
Link: &feeds.Link{Href: repo.HTMLURL() + "/commit/" + commit.ID.String()},
|
||||
Author: &feeds.Author{
|
||||
Name: commit.Author.Name,
|
||||
Email: commit.Author.Email,
|
||||
},
|
||||
Description: commit.Message(),
|
||||
Content: commit.Message(),
|
||||
Created: commit.Committer.When,
|
||||
})
|
||||
}
|
||||
|
||||
writeFeed(ctx, feed, formatType)
|
||||
}
|
94
routers/web/feed/profile.go
Normal file
94
routers/web/feed/profile.go
Normal file
@@ -0,0 +1,94 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
activities_model "code.gitea.io/gitea/models/activities"
|
||||
"code.gitea.io/gitea/models/organization"
|
||||
"code.gitea.io/gitea/models/renderhelper"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
feed_service "code.gitea.io/gitea/services/feed"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
// ShowUserFeedRSS show user activity as RSS feed
|
||||
func ShowUserFeedRSS(ctx *context.Context) {
|
||||
showUserFeed(ctx, "rss")
|
||||
}
|
||||
|
||||
// ShowUserFeedAtom show user activity as Atom feed
|
||||
func ShowUserFeedAtom(ctx *context.Context) {
|
||||
showUserFeed(ctx, "atom")
|
||||
}
|
||||
|
||||
// showUserFeed show user activity as RSS / Atom feed
|
||||
func showUserFeed(ctx *context.Context, formatType string) {
|
||||
includePrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
|
||||
isOrganisation := ctx.ContextUser.IsOrganization()
|
||||
if ctx.IsSigned && isOrganisation && !includePrivate {
|
||||
// When feed is requested by a member of the organization,
|
||||
// include the private repo's the member has access to.
|
||||
isOrgMember, err := organization.IsOrganizationMember(ctx, ctx.ContextUser.ID, ctx.Doer.ID)
|
||||
if err != nil {
|
||||
ctx.ServerError("IsOrganizationMember", err)
|
||||
return
|
||||
}
|
||||
includePrivate = isOrgMember
|
||||
}
|
||||
|
||||
actions, _, err := feed_service.GetFeeds(ctx, activities_model.GetFeedsOptions{
|
||||
RequestedUser: ctx.ContextUser,
|
||||
Actor: ctx.Doer,
|
||||
IncludePrivate: includePrivate,
|
||||
OnlyPerformedBy: !isOrganisation,
|
||||
IncludeDeleted: false,
|
||||
Date: ctx.FormString("date"),
|
||||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("GetFeeds", err)
|
||||
return
|
||||
}
|
||||
|
||||
rctx := renderhelper.NewRenderContextSimpleDocument(ctx, ctx.ContextUser.HTMLURL())
|
||||
ctxUserDescription, err := markdown.RenderString(rctx,
|
||||
ctx.ContextUser.Description)
|
||||
if err != nil {
|
||||
ctx.ServerError("RenderString", err)
|
||||
return
|
||||
}
|
||||
|
||||
feed := &feeds.Feed{
|
||||
Title: ctx.Locale.TrString("home.feed_of", ctx.ContextUser.DisplayName()),
|
||||
Link: &feeds.Link{Href: ctx.ContextUser.HTMLURL()},
|
||||
Description: string(ctxUserDescription),
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
feed.Items, err = feedActionsToFeedItems(ctx, actions)
|
||||
if err != nil {
|
||||
ctx.ServerError("convert feed", err)
|
||||
return
|
||||
}
|
||||
|
||||
writeFeed(ctx, feed, formatType)
|
||||
}
|
||||
|
||||
// writeFeed write a feeds.Feed as atom or rss to ctx.Resp
|
||||
func writeFeed(ctx *context.Context, feed *feeds.Feed, formatType string) {
|
||||
if formatType == "atom" {
|
||||
ctx.Resp.Header().Set("Content-Type", "application/atom+xml;charset=utf-8")
|
||||
if err := feed.WriteAtom(ctx.Resp); err != nil {
|
||||
ctx.ServerError("Render Atom failed", err)
|
||||
}
|
||||
} else {
|
||||
ctx.Resp.Header().Set("Content-Type", "application/rss+xml;charset=utf-8")
|
||||
if err := feed.WriteRss(ctx.Resp); err != nil {
|
||||
ctx.ServerError("Render RSS failed", err)
|
||||
}
|
||||
}
|
||||
}
|
38
routers/web/feed/profile_test.go
Normal file
38
routers/web/feed/profile_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
package feed_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/routers/web/feed"
|
||||
"code.gitea.io/gitea/services/contexttest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
unittest.MainTest(m)
|
||||
}
|
||||
|
||||
func TestCheckGetOrgFeedsAsOrgMember(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
t.Run("OrgMember", func(t *testing.T) {
|
||||
ctx, resp := contexttest.MockContext(t, "org3.atom")
|
||||
ctx.ContextUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
||||
contexttest.LoadUser(t, ctx, 2)
|
||||
ctx.IsSigned = true
|
||||
feed.ShowUserFeedAtom(ctx)
|
||||
assert.Contains(t, resp.Body.String(), "<entry>") // Should contain 1 private entry
|
||||
})
|
||||
t.Run("NonOrgMember", func(t *testing.T) {
|
||||
ctx, resp := contexttest.MockContext(t, "org3.atom")
|
||||
ctx.ContextUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
||||
contexttest.LoadUser(t, ctx, 5)
|
||||
ctx.IsSigned = true
|
||||
feed.ShowUserFeedAtom(ctx)
|
||||
assert.NotContains(t, resp.Body.String(), "<entry>") // Should not contain any entries
|
||||
})
|
||||
}
|
52
routers/web/feed/release.go
Normal file
52
routers/web/feed/release.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
// shows tags and/or releases on the repo as RSS / Atom feed
|
||||
func ShowReleaseFeed(ctx *context.Context, repo *repo_model.Repository, isReleasesOnly bool, formatType string) {
|
||||
releases, err := db.Find[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
||||
IncludeTags: !isReleasesOnly,
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("GetReleasesByRepoID", err)
|
||||
return
|
||||
}
|
||||
|
||||
var title string
|
||||
var link *feeds.Link
|
||||
|
||||
if isReleasesOnly {
|
||||
title = ctx.Locale.TrString("repo.release.releases_for", repo.FullName())
|
||||
link = &feeds.Link{Href: repo.HTMLURL() + "/release"}
|
||||
} else {
|
||||
title = ctx.Locale.TrString("repo.release.tags_for", repo.FullName())
|
||||
link = &feeds.Link{Href: repo.HTMLURL() + "/tags"}
|
||||
}
|
||||
|
||||
feed := &feeds.Feed{
|
||||
Title: title,
|
||||
Link: link,
|
||||
Description: repo.Description,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
feed.Items, err = releasesToFeedItems(ctx, releases)
|
||||
if err != nil {
|
||||
ctx.ServerError("releasesToFeedItems", err)
|
||||
return
|
||||
}
|
||||
|
||||
writeFeed(ctx, feed, formatType)
|
||||
}
|
18
routers/web/feed/render.go
Normal file
18
routers/web/feed/render.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/services/context"
|
||||
)
|
||||
|
||||
// RenderBranchFeed render format for branch or file
|
||||
func RenderBranchFeed(ctx *context.Context) {
|
||||
_, showFeedType := GetFeedType(ctx.PathParam("reponame"), ctx.Req)
|
||||
if ctx.Repo.TreePath == "" {
|
||||
ShowBranchFeed(ctx, ctx.Repo.Repository, showFeedType)
|
||||
} else {
|
||||
ShowFileFeed(ctx, ctx.Repo.Repository, showFeedType)
|
||||
}
|
||||
}
|
44
routers/web/feed/repo.go
Normal file
44
routers/web/feed/repo.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package feed
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
activities_model "code.gitea.io/gitea/models/activities"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
feed_service "code.gitea.io/gitea/services/feed"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
// ShowRepoFeed shows user activity on the repo as RSS / Atom feed
|
||||
func ShowRepoFeed(ctx *context.Context, repo *repo_model.Repository, formatType string) {
|
||||
actions, _, err := feed_service.GetFeeds(ctx, activities_model.GetFeedsOptions{
|
||||
RequestedRepo: repo,
|
||||
Actor: ctx.Doer,
|
||||
IncludePrivate: true,
|
||||
Date: ctx.FormString("date"),
|
||||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("GetFeeds", err)
|
||||
return
|
||||
}
|
||||
|
||||
feed := &feeds.Feed{
|
||||
Title: ctx.Locale.TrString("home.feed_of", repo.FullName()),
|
||||
Link: &feeds.Link{Href: repo.HTMLURL()},
|
||||
Description: repo.Description,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
feed.Items, err = feedActionsToFeedItems(ctx, actions)
|
||||
if err != nil {
|
||||
ctx.ServerError("convert feed", err)
|
||||
return
|
||||
}
|
||||
|
||||
writeFeed(ctx, feed, formatType)
|
||||
}
|
Reference in New Issue
Block a user