-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add RSS/Atom feed support for user actions #16002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 36 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
7dcb601
User/Org/Repo reserve "*.rss" pattern
6543 e8fba1f
refactor user.retriveFeeds
6543 b839c84
wip
6543 460e8ef
wip
6543 73f9af2
Merge branch 'master' into rss
6543 83eda4d
just reserve for now
6543 29f61be
Merge branch 'master' into rss
6543 1b67926
Merge branch 'main' into rss
6543 0af4fc9
Merge branch 'main' into rss
6543 83dd3a6
Merge branch 'master' into rss
6543 ed1522a
adopt
6543 4bb0a4d
use writer interface
6543 8568cb0
set correct Content-Type
6543 902be01
wip next
6543 3f6fe33
Merge branch 'main' into rss
6543 be3d0a8
minify-diff
6543 f4aad73
ADD org rss as todo
6543 9ddd72a
refactor
6543 96cdba9
wip
6543 8205dc6
fix lint
6543 ab92f54
Merge branch 'master' into rss
6543 41c9175
resolve last todos
6543 59b7b0b
Merge branch 'main' into rss
6543 4a9132d
Merge branch 'main' into rss
6543 fb27ee3
Merge branch 'main' into rss
6543 0d0e18f
Merge branch 'master' into rss
6543 af9d9e2
use accept header too
6543 b667943
Merge branch 'master' into rss
6543 3cc6cba
call it feed not rss
6543 8866ddf
Merge branch 'main' into rss
6543 1909375
Merge branch 'master' into rss
6543 d15b238
Merge branch 'main' into rss
6543 4cf34a0
Merge branch 'main' into rss
6543 f6e582a
Merge branch 'main' into rss
6543 eeaa982
Merge branch 'main' into rss
6543 c13753a
Merge branch 'main' into rss
6543 5c11530
Merge branch 'master' into rss
6543 4e979cc
return error & use strconv
6543 3fb765a
Merge branch 'main' into rss
lunny eb0fea6
Merge branch 'main' into rss
6543 bc6d3a1
Merge branch 'main' into rss
6543 090233c
Merge branch 'main' into rss
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// 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 feed | ||
|
||
import ( | ||
"fmt" | ||
"html" | ||
"net/url" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/templates" | ||
|
||
"github.com/gorilla/feeds" | ||
) | ||
|
||
// feedActionsToFeedItems convert gitea's Action feed to feeds Item | ||
func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item) { | ||
for _, act := range actions { | ||
act.LoadActUser() | ||
|
||
content, desc, title := "", "", "" | ||
|
||
link := &feeds.Link{Href: act.GetCommentLink()} | ||
|
||
// title | ||
title = act.ActUser.DisplayName() + " " | ||
switch act.OpType { | ||
case models.ActionCreateRepo: | ||
title += ctx.Tr("action.create_repo", act.GetRepoLink(), act.ShortRepoPath()) | ||
case models.ActionRenameRepo: | ||
title += ctx.Tr("action.rename_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath()) | ||
case models.ActionCommitRepo: | ||
branchLink := act.GetBranch() | ||
if len(act.Content) != 0 { | ||
title += ctx.Tr("action.commit_repo", act.GetRepoLink(), branchLink, act.GetBranch(), act.ShortRepoPath()) | ||
} else { | ||
title += ctx.Tr("action.create_branch", act.GetRepoLink(), branchLink, act.GetBranch(), act.ShortRepoPath()) | ||
} | ||
case models.ActionCreateIssue: | ||
title += ctx.Tr("action.create_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionCreatePullRequest: | ||
title += ctx.Tr("action.create_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionTransferRepo: | ||
title += ctx.Tr("action.transfer_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath()) | ||
case models.ActionPushTag: | ||
title += ctx.Tr("action.push_tag", act.GetRepoLink(), url.QueryEscape(act.GetTag()), act.ShortRepoPath()) | ||
case models.ActionCommentIssue: | ||
title += ctx.Tr("action.comment_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionMergePullRequest: | ||
title += ctx.Tr("action.merge_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionCloseIssue: | ||
title += ctx.Tr("action.close_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionReopenIssue: | ||
title += ctx.Tr("action.reopen_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionClosePullRequest: | ||
title += ctx.Tr("action.close_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionReopenPullRequest: | ||
title += ctx.Tr("action.reopen_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath) | ||
case models.ActionDeleteTag: | ||
title += ctx.Tr("action.delete_tag", act.GetRepoLink(), html.EscapeString(act.GetTag()), act.ShortRepoPath()) | ||
case models.ActionDeleteBranch: | ||
title += ctx.Tr("action.delete_branch", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) | ||
case models.ActionMirrorSyncPush: | ||
title += ctx.Tr("action.mirror_sync_push", act.GetRepoLink(), url.QueryEscape(act.GetBranch()), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) | ||
case models.ActionMirrorSyncCreate: | ||
title += ctx.Tr("action.mirror_sync_create", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) | ||
case models.ActionMirrorSyncDelete: | ||
title += ctx.Tr("action.mirror_sync_delete", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) | ||
case models.ActionApprovePullRequest: | ||
title += ctx.Tr("action.approve_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionRejectPullRequest: | ||
title += ctx.Tr("action.reject_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionCommentPull: | ||
title += ctx.Tr("action.comment_pull", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) | ||
case models.ActionPublishRelease: | ||
title += ctx.Tr("action.publish_release", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath(), act.Content) | ||
case models.ActionPullReviewDismissed: | ||
title += ctx.Tr("action.review_dismissed", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath(), act.GetIssueInfos()[1]) | ||
case models.ActionStarRepo: | ||
title += ctx.Tr("action.stared_repo", act.GetRepoLink(), act.GetRepoPath()) | ||
link = &feeds.Link{Href: act.GetRepoLink()} | ||
case models.ActionWatchRepo: | ||
title += ctx.Tr("action.watched_repo", act.GetRepoLink(), act.GetRepoPath()) | ||
link = &feeds.Link{Href: act.GetRepoLink()} | ||
default: | ||
log.Error("unknown action type: %v", act.OpType) | ||
} | ||
|
||
// description & content | ||
{ | ||
switch act.OpType { | ||
case models.ActionCommitRepo, models.ActionMirrorSyncPush: | ||
push := templates.ActionContent2Commits(act) | ||
repoLink := act.GetRepoLink() | ||
|
||
for _, commit := range push.Commits { | ||
if len(desc) != 0 { | ||
desc += "\n\n" | ||
} | ||
desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s", | ||
fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), commit.Sha1), | ||
commit.Sha1, | ||
templates.RenderCommitMessage(commit.Message, repoLink, nil), | ||
) | ||
} | ||
|
||
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.GetRepoLink(), push.Commits[0].Sha1)} | ||
} | ||
|
||
case models.ActionCreateIssue, models.ActionCreatePullRequest: | ||
desc = strings.Join(act.GetIssueInfos(), "#") | ||
content = act.GetIssueContent() | ||
case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull: | ||
desc = act.GetIssueTitle() | ||
comment := act.GetIssueInfos()[1] | ||
if len(comment) != 0 { | ||
desc += "\n\n" + comment | ||
} | ||
case models.ActionMergePullRequest: | ||
desc = act.GetIssueInfos()[1] | ||
case models.ActionCloseIssue, models.ActionReopenIssue, models.ActionClosePullRequest, models.ActionReopenPullRequest: | ||
desc = act.GetIssueTitle() | ||
case models.ActionPullReviewDismissed: | ||
desc = ctx.Tr("action.review_dismissed_reason") + "\n\n" + act.GetIssueInfos()[2] | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if len(content) == 0 { | ||
content = desc | ||
} | ||
|
||
items = append(items, &feeds.Item{ | ||
Title: title, | ||
Link: link, | ||
Description: desc, | ||
Author: &feeds.Author{ | ||
Name: act.ActUser.DisplayName(), | ||
Email: act.ActUser.GetEmail(), | ||
}, | ||
Id: fmt.Sprint(act.ID), | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Created: act.CreatedUnix.AsTime(), | ||
Content: content, | ||
}) | ||
} | ||
return | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 feed | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/context" | ||
|
||
"github.com/gorilla/feeds" | ||
) | ||
|
||
// RetrieveFeeds loads feeds for the specified user | ||
func RetrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) []*models.Action { | ||
actions, err := models.GetFeeds(options) | ||
if err != nil { | ||
ctx.ServerError("GetFeeds", err) | ||
return nil | ||
} | ||
|
||
userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser} | ||
if ctx.User != nil { | ||
userCache[ctx.User.ID] = ctx.User | ||
} | ||
for _, act := range actions { | ||
if act.ActUser != nil { | ||
userCache[act.ActUserID] = act.ActUser | ||
} | ||
} | ||
|
||
for _, act := range actions { | ||
repoOwner, ok := userCache[act.Repo.OwnerID] | ||
if !ok { | ||
repoOwner, err = models.GetUserByID(act.Repo.OwnerID) | ||
if err != nil { | ||
if models.IsErrUserNotExist(err) { | ||
continue | ||
} | ||
ctx.ServerError("GetUserByID", err) | ||
return nil | ||
} | ||
userCache[repoOwner.ID] = repoOwner | ||
} | ||
act.Repo.Owner = repoOwner | ||
} | ||
return actions | ||
} | ||
|
||
// ShowUserFeed show user activity as RSS / Atom feed | ||
func ShowUserFeed(ctx *context.Context, ctxUser *models.User, formatType string) { | ||
actions := RetrieveFeeds(ctx, models.GetFeedsOptions{ | ||
RequestedUser: ctxUser, | ||
Actor: ctx.User, | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IncludePrivate: false, | ||
OnlyPerformedBy: true, | ||
IncludeDeleted: false, | ||
Date: ctx.FormString("date"), | ||
}) | ||
if ctx.Written() { | ||
return | ||
} | ||
|
||
feed := &feeds.Feed{ | ||
Title: ctx.Tr("home.feed_of", ctxUser.DisplayName()), | ||
Link: &feeds.Link{Href: ctxUser.HTMLURL()}, | ||
Description: ctxUser.Description, | ||
Created: time.Now(), | ||
} | ||
|
||
feed.Items = feedActionsToFeedItems(ctx, actions) | ||
|
||
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) { | ||
ctx.Resp.WriteHeader(http.StatusOK) | ||
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) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.