From bd9ac9ac6f0c7374c8254f9fe65f53758d90e0d2 Mon Sep 17 00:00:00 2001 From: Grigory Kirillov Date: Fri, 13 Oct 2023 14:40:41 +0300 Subject: [PATCH] [GITEA] convert feed items' titles to plain text Refs: https://codeberg.org/forgejo/forgejo/pulls/1595 (cherry picked from commit 35b962e6313df748e8855b4dfbf748f095ea1003) (cherry picked from commit 1004e35b84a4a0deae999cb8a4c2924b85b47c8b) (cherry picked from commit af51dd594db229f7a986325a6070d33782d85d28) (cherry picked from commit ef10fae29607533db3616a23043cc0f2fc2dc71a) (cherry picked from commit ff8027ed1b0a1274b7b6e4840e31e2ad4d18b159) (cherry picked from commit 2540ff52ef2229ad6e17578c30ae617b3771c696) (cherry picked from commit 57b4d775e1734d2cb6dd78a4e890d3548e2324eb) (cherry picked from commit c388aba9b50bbdd7eb13518d91f8e00c5d1bce18) (cherry picked from commit 7a3b605c11d5a9033b7c3db882b606fb009afca3) (cherry picked from commit cc02354d0a6872e761d8215b6630a1467c6f8e75) (cherry picked from commit e11c5ce82aeaaa62ced4bead72aa3d37453b792a) (cherry picked from commit d1e7798bb2b32eb3a8bd1be669191d3e3a9a2510) (cherry picked from commit 1813af7391a47b79b4cd44d4feb64e3002032db6) (cherry picked from commit 0d55a8894508aae4225d76235d4bd7a9f862a849) --- routers/web/feed/convert.go | 10 ++++- .../api_feed_plain_text_titles_test.go | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/integration/api_feed_plain_text_titles_test.go diff --git a/routers/web/feed/convert.go b/routers/web/feed/convert.go index 95b1062253..31fbd17624 100644 --- a/routers/web/feed/convert.go +++ b/routers/web/feed/convert.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/gorilla/feeds" + "github.com/jaytaylor/html2text" ) func toBranchLink(ctx *context.Context, act *activities_model.Action) string { @@ -240,8 +241,15 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio content = desc } + // It's a common practice for feed generators to use plain text titles. + // See https://codeberg.org/forgejo/forgejo/pulls/1595 + plainTitle, err := html2text.FromString(title, html2text.Options{OmitLinks: true}) + if err != nil { + return nil, err + } + items = append(items, &feeds.Item{ - Title: title, + Title: plainTitle, Link: link, Description: desc, Author: &feeds.Author{ diff --git a/tests/integration/api_feed_plain_text_titles_test.go b/tests/integration/api_feed_plain_text_titles_test.go new file mode 100644 index 0000000000..a058b7321c --- /dev/null +++ b/tests/integration/api_feed_plain_text_titles_test.go @@ -0,0 +1,40 @@ +// Copyright 2023 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package integration + +import ( + "net/http" + "testing" + + "code.gitea.io/gitea/tests" + + "github.com/stretchr/testify/assert" +) + +func TestFeedPlainTextTitles(t *testing.T) { + // This test verifies that items' titles in feeds are generated as plain text. + // See https://codeberg.org/forgejo/forgejo/pulls/1595 + + t.Run("Feed plain text titles", func(t *testing.T) { + t.Run("Atom", func(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + req := NewRequest(t, "GET", "/user2/repo1.atom") + resp := MakeRequest(t, req, http.StatusOK) + + data := resp.Body.String() + assert.Contains(t, data, "the_1-user.with.all.allowedChars closed issue user2/repo1#4") + }) + + t.Run("RSS", func(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + req := NewRequest(t, "GET", "/user2/repo1.rss") + resp := MakeRequest(t, req, http.StatusOK) + + data := resp.Body.String() + assert.Contains(t, data, "the_1-user.with.all.allowedChars closed issue user2/repo1#4") + }) + }) +}