Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
markup_service "code.gitea.io/gitea/services/markup"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
Expand Down Expand Up @@ -112,7 +113,7 @@ func runPR() {
log.Printf("[PR] Setting up router\n")
// routers.GlobalInit()
external.RegisterRenderers()
markup.Init()
markup.Init(markup_service.ProcessorHelper())
c := routers.NormalRoutes(graceful.GetManager().HammerContext())

log.Printf("[PR] Ready for testing !\n")
Expand Down
37 changes: 0 additions & 37 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1220,40 +1220,3 @@
repo_admin_change_team_access: false
theme: ""
keep_activity_private: false

-
id: 34
lower_name: r-lyeh
name: r-lyeh
full_name: user34
email: [email protected]
keep_email_private: true
email_notifications_preference: enabled
passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b
passwd_hash_algo: argon2
must_change_password: false
login_source: 0
login_name: user34
type: 0
salt: ZogKvWdyEx
max_repo_creation: -1
is_active: true
is_admin: false
is_restricted: false
allow_git_hook: false
allow_import_local: false
allow_create_organization: true
prohibit_login: false
avatar: avatar34
avatar_email: [email protected]
use_custom_avatar: false
num_followers: 0
num_following: 0
num_stars: 0
num_repos: 0
num_teams: 0
num_members: 0
visibility: 0
repo_admin_change_team_access: false
theme: ""
keep_activity_private: false
4 changes: 2 additions & 2 deletions models/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) {
}

testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}},
[]int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34})
[]int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32})

testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse},
[]int64{9})

testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
[]int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34})
[]int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32})

testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
[]int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
Expand Down
9 changes: 1 addition & 8 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"strings"
"sync"

"code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/emoji"
"code.gitea.io/gitea/modules/git"
Expand Down Expand Up @@ -606,13 +605,7 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
}
mentionedUsername := mention[1:]

// Only link if the user actually exists
userExists, err := user.IsUserExist(ctx.Ctx, 0, mentionedUsername)
if err != nil {
log.Error("Failed to validate user in mention %v exists, assuming it does", mention)
userExists = true
}
if userExists {
if processorHelper.IsUsernameMentionable != nil && processorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) {
replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention"))
node = node.NextSibling.NextSibling
} else {
Expand Down
10 changes: 2 additions & 8 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package markdown_test

import (
"context"
"path/filepath"
"os"
"strings"
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
Expand Down Expand Up @@ -39,10 +38,7 @@ func TestMain(m *testing.M) {
if err := git.InitSimple(context.Background()); err != nil {
log.Fatal("git init failed, err: %v", err)
}
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", ".."),
FixtureFiles: []string{"user.yml"},
})
os.Exit(m.Run())
}

func TestRender_StandardLinks(t *testing.T) {
Expand Down Expand Up @@ -304,8 +300,6 @@ func TestTotal_RenderWiki(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL

assert.NoError(t, unittest.PrepareTestDatabase())

answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))

for i := 0; i < len(sameCases); i++ {
Expand Down
12 changes: 11 additions & 1 deletion modules/markup/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ import (
"code.gitea.io/gitea/modules/setting"
)

type ProcessorHelper struct {
IsUsernameMentionable func(ctx context.Context, username string) bool
}

var processorHelper ProcessorHelper

// Init initialize regexps for markdown parsing
func Init() {
func Init(ph *ProcessorHelper) {
if ph != nil {
processorHelper = *ph
}

NewSanitizer()
if len(setting.Markdown.CustomURLSchemes) > 0 {
CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
Expand Down
19 changes: 0 additions & 19 deletions routers/api/v1/misc/main_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"code.gitea.io/gitea/services/automerge"
"code.gitea.io/gitea/services/cron"
"code.gitea.io/gitea/services/mailer"
markup_service "code.gitea.io/gitea/services/markup"
repo_migrations "code.gitea.io/gitea/services/migrations"
mirror_service "code.gitea.io/gitea/services/mirror"
pull_service "code.gitea.io/gitea/services/pull"
Expand Down Expand Up @@ -123,7 +124,7 @@ func GlobalInitInstalled(ctx context.Context) {

highlight.NewContext()
external.RegisterRenderers()
markup.Init()
markup.Init(markup_service.ProcessorHelper())

if setting.EnableSQLite3 {
log.Info("SQLite3 support is enabled")
Expand Down
24 changes: 24 additions & 0 deletions services/markup/processorhelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package markup

import (
"context"

"code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
)

func ProcessorHelper() *markup.ProcessorHelper {
return &markup.ProcessorHelper{
IsUsernameMentionable: func(ctx context.Context, username string) bool {
// here, you could also try to cast the ctx to a modules/context.Context
// Only link if the user actually exists
userExists, err := user.IsUserExist(ctx, 0, username)
if err != nil {
log.Error("Failed to validate user in mention %q exists, assuming it does", username)
userExists = true
}
return userExists
},
}
}