Skip to content

Commit f486a5f

Browse files
wxiaoguangyardenshoham
authored andcommitted
refactor
1 parent aab51ec commit f486a5f

File tree

9 files changed

+44
-77
lines changed

9 files changed

+44
-77
lines changed

contrib/pr/checkout.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"code.gitea.io/gitea/modules/setting"
3434
"code.gitea.io/gitea/modules/util"
3535
"code.gitea.io/gitea/routers"
36+
markup_service "code.gitea.io/gitea/services/markup"
3637

3738
"github.com/go-git/go-git/v5"
3839
"github.com/go-git/go-git/v5/config"
@@ -112,7 +113,7 @@ func runPR() {
112113
log.Printf("[PR] Setting up router\n")
113114
// routers.GlobalInit()
114115
external.RegisterRenderers()
115-
markup.Init()
116+
markup.Init(markup_service.ProcessorHelper())
116117
c := routers.NormalRoutes(graceful.GetManager().HammerContext())
117118

118119
log.Printf("[PR] Ready for testing !\n")

models/fixtures/user.yml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,40 +1220,3 @@
12201220
repo_admin_change_team_access: false
12211221
theme: ""
12221222
keep_activity_private: false
1223-
1224-
-
1225-
id: 34
1226-
lower_name: r-lyeh
1227-
name: r-lyeh
1228-
full_name: user34
1229-
1230-
keep_email_private: true
1231-
email_notifications_preference: enabled
1232-
passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b
1233-
passwd_hash_algo: argon2
1234-
must_change_password: false
1235-
login_source: 0
1236-
login_name: user34
1237-
type: 0
1238-
salt: ZogKvWdyEx
1239-
max_repo_creation: -1
1240-
is_active: true
1241-
is_admin: false
1242-
is_restricted: false
1243-
allow_git_hook: false
1244-
allow_import_local: false
1245-
allow_create_organization: true
1246-
prohibit_login: false
1247-
avatar: avatar34
1248-
avatar_email: [email protected]
1249-
use_custom_avatar: false
1250-
num_followers: 0
1251-
num_following: 0
1252-
num_stars: 0
1253-
num_repos: 0
1254-
num_teams: 0
1255-
num_members: 0
1256-
visibility: 0
1257-
repo_admin_change_team_access: false
1258-
theme: ""
1259-
keep_activity_private: false

models/user/user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) {
9696
}
9797

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

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

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

107107
testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
108108
[]int64{1, 10, 11, 12, 13, 14, 15, 16, 18})

modules/markup/html.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"sync"
1616

17-
"code.gitea.io/gitea/models/user"
1817
"code.gitea.io/gitea/modules/base"
1918
"code.gitea.io/gitea/modules/emoji"
2019
"code.gitea.io/gitea/modules/git"
@@ -606,13 +605,7 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
606605
}
607606
mentionedUsername := mention[1:]
608607

609-
// Only link if the user actually exists
610-
userExists, err := user.IsUserExist(ctx.Ctx, 0, mentionedUsername)
611-
if err != nil {
612-
log.Error("Failed to validate user in mention %v exists, assuming it does", mention)
613-
userExists = true
614-
}
615-
if userExists {
608+
if processorHelper.IsUsernameMentionable != nil && processorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) {
616609
replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention"))
617610
node = node.NextSibling.NextSibling
618611
} else {

modules/markup/markdown/markdown_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ package markdown_test
66

77
import (
88
"context"
9-
"path/filepath"
9+
"os"
1010
"strings"
1111
"testing"
1212

13-
"code.gitea.io/gitea/models/unittest"
1413
"code.gitea.io/gitea/modules/git"
1514
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/markup"
@@ -39,10 +38,7 @@ func TestMain(m *testing.M) {
3938
if err := git.InitSimple(context.Background()); err != nil {
4039
log.Fatal("git init failed, err: %v", err)
4140
}
42-
unittest.MainTest(m, &unittest.TestOptions{
43-
GiteaRootPath: filepath.Join("..", "..", ".."),
44-
FixtureFiles: []string{"user.yml"},
45-
})
41+
os.Exit(m.Run())
4642
}
4743

4844
func TestRender_StandardLinks(t *testing.T) {
@@ -304,8 +300,6 @@ func TestTotal_RenderWiki(t *testing.T) {
304300
setting.AppURL = AppURL
305301
setting.AppSubURL = AppSubURL
306302

307-
assert.NoError(t, unittest.PrepareTestDatabase())
308-
309303
answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))
310304

311305
for i := 0; i < len(sameCases); i++ {

modules/markup/renderer.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ import (
1919
"code.gitea.io/gitea/modules/setting"
2020
)
2121

22+
type ProcessorHelper struct {
23+
IsUsernameMentionable func(ctx context.Context, username string) bool
24+
}
25+
26+
var processorHelper ProcessorHelper
27+
2228
// Init initialize regexps for markdown parsing
23-
func Init() {
29+
func Init(ph *ProcessorHelper) {
30+
if ph != nil {
31+
processorHelper = *ph
32+
}
33+
2434
NewSanitizer()
2535
if len(setting.Markdown.CustomURLSchemes) > 0 {
2636
CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)

routers/api/v1/misc/main_test.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

routers/init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"code.gitea.io/gitea/services/automerge"
4242
"code.gitea.io/gitea/services/cron"
4343
"code.gitea.io/gitea/services/mailer"
44+
markup_service "code.gitea.io/gitea/services/markup"
4445
repo_migrations "code.gitea.io/gitea/services/migrations"
4546
mirror_service "code.gitea.io/gitea/services/mirror"
4647
pull_service "code.gitea.io/gitea/services/pull"
@@ -123,7 +124,7 @@ func GlobalInitInstalled(ctx context.Context) {
123124

124125
highlight.NewContext()
125126
external.RegisterRenderers()
126-
markup.Init()
127+
markup.Init(markup_service.ProcessorHelper())
127128

128129
if setting.EnableSQLite3 {
129130
log.Info("SQLite3 support is enabled")

services/markup/processorhelper.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package markup
2+
3+
import (
4+
"context"
5+
6+
"code.gitea.io/gitea/models/user"
7+
"code.gitea.io/gitea/modules/log"
8+
"code.gitea.io/gitea/modules/markup"
9+
)
10+
11+
func ProcessorHelper() *markup.ProcessorHelper {
12+
return &markup.ProcessorHelper{
13+
IsUsernameMentionable: func(ctx context.Context, username string) bool {
14+
// here, you could also try to cast the ctx to a modules/context.Context
15+
// Only link if the user actually exists
16+
userExists, err := user.IsUserExist(ctx, 0, username)
17+
if err != nil {
18+
log.Error("Failed to validate user in mention %q exists, assuming it does", username)
19+
userExists = true
20+
}
21+
return userExists
22+
},
23+
}
24+
}

0 commit comments

Comments
 (0)