Skip to content
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

Add RSS/Atom feed support for user actions #16002

Merged
merged 42 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7dcb601
User/Org/Repo reserve "*.rss" pattern
6543 Jun 10, 2021
e8fba1f
refactor user.retriveFeeds
6543 Jun 10, 2021
b839c84
wip
6543 Jun 10, 2021
460e8ef
wip
6543 Jun 11, 2021
73f9af2
Merge branch 'master' into rss
6543 Jun 16, 2021
83eda4d
just reserve for now
6543 Jun 17, 2021
29f61be
Merge branch 'master' into rss
6543 Jul 1, 2021
1b67926
Merge branch 'main' into rss
6543 Jul 26, 2021
0af4fc9
Merge branch 'main' into rss
6543 Aug 29, 2021
83dd3a6
Merge branch 'master' into rss
6543 Sep 5, 2021
ed1522a
adopt
6543 Sep 5, 2021
4bb0a4d
use writer interface
6543 Sep 5, 2021
8568cb0
set correct Content-Type
6543 Sep 5, 2021
902be01
wip next
6543 Sep 5, 2021
3f6fe33
Merge branch 'main' into rss
6543 Sep 9, 2021
be3d0a8
minify-diff
6543 Sep 9, 2021
f4aad73
ADD org rss as todo
6543 Sep 9, 2021
9ddd72a
refactor
6543 Sep 9, 2021
96cdba9
wip
6543 Sep 9, 2021
8205dc6
fix lint
6543 Sep 9, 2021
ab92f54
Merge branch 'master' into rss
6543 Sep 10, 2021
41c9175
resolve last todos
6543 Sep 10, 2021
59b7b0b
Merge branch 'main' into rss
6543 Sep 11, 2021
4a9132d
Merge branch 'main' into rss
6543 Sep 12, 2021
fb27ee3
Merge branch 'main' into rss
6543 Sep 14, 2021
0d0e18f
Merge branch 'master' into rss
6543 Sep 20, 2021
af9d9e2
use accept header too
6543 Sep 20, 2021
b667943
Merge branch 'master' into rss
6543 Sep 21, 2021
3cc6cba
call it feed not rss
6543 Sep 21, 2021
8866ddf
Merge branch 'main' into rss
6543 Sep 23, 2021
1909375
Merge branch 'master' into rss
6543 Sep 25, 2021
d15b238
Merge branch 'main' into rss
6543 Sep 29, 2021
4cf34a0
Merge branch 'main' into rss
6543 Sep 29, 2021
f6e582a
Merge branch 'main' into rss
6543 Oct 3, 2021
eeaa982
Merge branch 'main' into rss
6543 Oct 8, 2021
c13753a
Merge branch 'main' into rss
6543 Oct 8, 2021
5c11530
Merge branch 'master' into rss
6543 Oct 14, 2021
4e979cc
return error & use strconv
6543 Oct 14, 2021
3fb765a
Merge branch 'main' into rss
lunny Oct 15, 2021
eb0fea6
Merge branch 'main' into rss
6543 Oct 15, 2021
bc6d3a1
Merge branch 'main' into rss
6543 Oct 15, 2021
090233c
Merge branch 'main' into rss
6543 Oct 16, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.2.0
github.com/gorilla/context v1.1.1
github.com/gorilla/feeds v1.1.1
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down
2 changes: 1 addition & 1 deletion modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (ctx *Context) PlainText(status int, bs []byte) {
ctx.Resp.WriteHeader(status)
ctx.Resp.Header().Set("Content-Type", "text/plain;charset=utf-8")
if _, err := ctx.Resp.Write(bs); err != nil {
ctx.ServerError("Render JSON failed", err)
ctx.ServerError("Write bytes failed", err)
}
}

Expand Down
3 changes: 3 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ view_home = View %s
search_repos = Find a repository…
filter = Other Filters
filter_by_team_repositories = Filter by team repositories
feed_of = Feed of "%s"

show_archived = Archived
show_both_archived_unarchived = Showing both archived and unarchived
Expand Down Expand Up @@ -2775,6 +2776,8 @@ publish_release = `released <a href="%s/releases/tag/%s"> "%[4]s" </a> at <a hr
review_dismissed = `dismissed review from <b>%[4]s</b> for <a href="%[1]s/pulls/%[2]s">%[3]s#%[2]s</a>`
review_dismissed_reason = Reason:
create_branch = created branch <a href="%[1]s/src/branch/%[2]s">%[3]s</a> in <a href="%[1]s">%[4]s</a>
stared_repo = stared <a href="%[1]s">%[2]s</a>
watched_repo = started watching <a href="%[1]s">%[2]s</a>

[tool]
ago = %s ago
Expand Down
154 changes: 154 additions & 0 deletions routers/web/feed/convert.go
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"
"strconv"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"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, err error) {
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:
return nil, fmt.Errorf("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: strconv.FormatInt(act.ID, 10),
Created: act.CreatedUnix.AsTime(),
Content: content,
})
}
return
}
98 changes: 98 additions & 0 deletions routers/web/feed/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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(),
}

var err error
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) {
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)
}
}
}
40 changes: 3 additions & 37 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/web/feed"
issue_service "code.gitea.io/gitea/services/issue"
pull_service "code.gitea.io/gitea/services/pull"

Expand Down Expand Up @@ -60,42 +61,6 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
return ctxUser
}

// retrieveFeeds loads feeds for the specified user
func retrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) {
actions, err := models.GetFeeds(options)
if err != nil {
ctx.ServerError("GetFeeds", err)
return
}

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
}
userCache[repoOwner.ID] = repoOwner
}
act.Repo.Owner = repoOwner
}
ctx.Data["Feeds"] = actions
}

// Dashboard render the dashboard page
func Dashboard(ctx *context.Context) {
ctxUser := getDashboardContextUser(ctx)
Expand Down Expand Up @@ -151,7 +116,7 @@ func Dashboard(ctx *context.Context) {
ctx.Data["MirrorCount"] = len(mirrors)
ctx.Data["Mirrors"] = mirrors

retrieveFeeds(ctx, models.GetFeedsOptions{
ctx.Data["Feeds"] = feed.RetrieveFeeds(ctx, models.GetFeedsOptions{
RequestedUser: ctxUser,
RequestedTeam: ctx.Org.Team,
Actor: ctx.User,
Expand All @@ -164,6 +129,7 @@ func Dashboard(ctx *context.Context) {
if ctx.Written() {
return
}

ctx.HTML(http.StatusOK, tplDashboard)
}

Expand Down
Loading