Skip to content

Commit 5d9fd10

Browse files
committed
add rss support for repos
1 parent db7486f commit 5d9fd10

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

modules/context/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
418418
userName := ctx.Params(":username")
419419
repoName := ctx.Params(":reponame")
420420
repoName = strings.TrimSuffix(repoName, ".git")
421+
repoName = strings.TrimSuffix(repoName, ".rss")
422+
repoName = strings.TrimSuffix(repoName, ".atom")
421423

422424
// Check if the user is the same as the repository owner
423425
if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {

routers/web/feed/convert.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package feed
77
import (
88
"fmt"
99
"html"
10+
"net/http"
1011
"net/url"
1112
"strconv"
1213
"strings"
@@ -226,3 +227,24 @@ func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (it
226227
}
227228
return
228229
}
230+
231+
// GetFeedType return altered name and feed type, if type is empty it's no feed.
232+
func GetFeedType(name string, req *http.Request) (string, string) {
233+
if strings.Contains(req.Header.Get("Accept"), "application/rss+xml") {
234+
return name, "rss"
235+
}
236+
237+
if strings.Contains(req.Header.Get("Accept"), "application/atom+xml") {
238+
return name, "atom"
239+
}
240+
241+
if strings.HasSuffix(name, ".rss") {
242+
return strings.TrimSuffix(name, ".rss"), "rss"
243+
}
244+
245+
if strings.HasSuffix(name, ".atom") {
246+
return strings.TrimSuffix(name, ".atom"), "atom"
247+
}
248+
249+
return name, ""
250+
}

routers/web/feed/repo.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package feed
6+
7+
import (
8+
"time"
9+
10+
"code.gitea.io/gitea/models"
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/modules/context"
13+
14+
"github.com/gorilla/feeds"
15+
)
16+
17+
// ShowRepoFeed show user activitys based on repo as RSS / Atom feed
18+
func ShowRepoFeed(ctx *context.Context, repo *repo_model.Repository, formatType string) {
19+
actions, err := models.GetFeeds(ctx, models.GetFeedsOptions{
20+
RequestedRepo: repo,
21+
Actor: ctx.User,
22+
IncludePrivate: false,
23+
OnlyPerformedBy: false,
24+
IncludeDeleted: false,
25+
Date: ctx.FormString("date"),
26+
})
27+
if err != nil {
28+
ctx.ServerError("GetFeeds", err)
29+
return
30+
}
31+
32+
feed := &feeds.Feed{
33+
Title: ctx.Tr("home.feed_of", repo.FullName()),
34+
Link: &feeds.Link{Href: repo.HTMLURL()},
35+
Description: repo.Description,
36+
Created: time.Now(),
37+
}
38+
39+
feed.Items, err = feedActionsToFeedItems(ctx, actions)
40+
if err != nil {
41+
ctx.ServerError("convert feed", err)
42+
return
43+
}
44+
45+
writeFeed(ctx, feed, formatType)
46+
}

routers/web/repo/view.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"code.gitea.io/gitea/modules/structs"
3939
"code.gitea.io/gitea/modules/typesniffer"
4040
"code.gitea.io/gitea/modules/util"
41+
"code.gitea.io/gitea/routers/web/feed"
4142
)
4243

4344
const (
@@ -691,6 +692,12 @@ func checkHomeCodeViewable(ctx *context.Context) {
691692

692693
// Home render repository home page
693694
func Home(ctx *context.Context) {
695+
_, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req)
696+
if len(showFeedType) != 0 {
697+
feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType)
698+
return
699+
}
700+
694701
checkHomeCodeViewable(ctx)
695702
if ctx.Written() {
696703
return

routers/web/user/profile.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,7 @@ func Profile(ctx *context.Context) {
7474
uname = strings.TrimSuffix(uname, ".gpg")
7575
}
7676

77-
showFeedType := ""
78-
if strings.HasSuffix(uname, ".rss") {
79-
showFeedType = "rss"
80-
uname = strings.TrimSuffix(uname, ".rss")
81-
} else if strings.Contains(ctx.Req.Header.Get("Accept"), "application/rss+xml") {
82-
showFeedType = "rss"
83-
}
84-
if strings.HasSuffix(uname, ".atom") {
85-
showFeedType = "atom"
86-
uname = strings.TrimSuffix(uname, ".atom")
87-
} else if strings.Contains(ctx.Req.Header.Get("Accept"), "application/atom+xml") {
88-
showFeedType = "atom"
89-
}
77+
uname, showFeedType := feed.GetFeedType(uname, ctx.Req)
9078

9179
ctxUser := GetUserByName(ctx, uname)
9280
if ctx.Written() {

0 commit comments

Comments
 (0)