From cc64a925602d54f3439dd19f16b5280bd0377a7a Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Sun, 30 Apr 2023 04:13:58 +0900 Subject: [PATCH] Add follow organization and fix the logic of following page (#24345) ![image](https://user-images.githubusercontent.com/18380374/234740589-066f2e5c-30c7-4fc3-a539-066100e1f138.png) ![image](https://user-images.githubusercontent.com/18380374/234740605-88efe55d-7eaa-422e-ab86-0b5a402ca11c.png) Maybe we can fix user card tmpl in #24319? Or maybe a list is better here ![image](https://user-images.githubusercontent.com/18380374/234451417-7f93df20-4b19-4abb-a62d-4c67e1aa2220.png) --------- Co-authored-by: silverwind Co-authored-by: Giteabot --- models/user/user.go | 18 ++++++++-------- routers/web/org/home.go | 7 +++++++ routers/web/user/profile.go | 41 +++++++++++++++++++------------------ templates/org/home.tmpl | 12 +++++++++++ templates/user/profile.tmpl | 2 +- 5 files changed, 51 insertions(+), 29 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index 053d6680cd..46c4440e5f 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -346,7 +346,7 @@ func GetUserFollowing(ctx context.Context, u, viewer *User, listOptions db.ListO Select("`user`.*"). Join("LEFT", "follow", "`user`.id=follow.follow_id"). Where("follow.user_id=?", u.ID). - And("`user`.type=?", UserTypeIndividual). + And("`user`.type IN (?, ?)", UserTypeIndividual, UserTypeOrganization). And(isUserVisibleToViewerCond(viewer)) if listOptions.Page != 0 { @@ -1210,23 +1210,25 @@ func isUserVisibleToViewerCond(viewer *User) builder.Cond { return builder.Neq{ "`user`.visibility": structs.VisibleTypePrivate, }.Or( + // viewer's following builder.In("`user`.id", builder. Select("`follow`.user_id"). From("follow"). Where(builder.Eq{"`follow`.follow_id": viewer.ID})), - builder.In("`user`.id", - builder. - Select("`team_user`.uid"). - From("team_user"). - Join("INNER", "`team_user` AS t2", "`team_user`.id = `t2`.id"). - Where(builder.Eq{"`t2`.uid": viewer.ID})), + // viewer's org user builder.In("`user`.id", builder. Select("`team_user`.uid"). From("team_user"). Join("INNER", "`team_user` AS t2", "`team_user`.org_id = `t2`.org_id"). - Where(builder.Eq{"`t2`.uid": viewer.ID}))) + Where(builder.Eq{"`t2`.uid": viewer.ID})), + // viewer's org + builder.In("`user`.id", + builder. + Select("`team_user`.org_id"). + From("team_user"). + Where(builder.Eq{"`team_user`.uid": viewer.ID}))) } // IsUserVisibleToViewer check if viewer is able to see user profile diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 201eefa614..7f38ec51ba 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/markup" @@ -143,6 +144,11 @@ func Home(ctx *context.Context) { return } + var isFollowing bool + if ctx.Doer != nil { + isFollowing = user_model.IsFollowing(ctx.Doer.ID, ctx.ContextUser.ID) + } + ctx.Data["Repos"] = repos ctx.Data["Total"] = count ctx.Data["MembersTotal"] = membersCount @@ -150,6 +156,7 @@ func Home(ctx *context.Context) { ctx.Data["Teams"] = ctx.Org.Teams ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull ctx.Data["PageIsViewRepositories"] = true + ctx.Data["IsFollowing"] = isFollowing pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5) pager.SetDefaultParams(ctx) diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 367bb306b8..ef91d89d14 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -167,30 +167,31 @@ func Profile(ctx *context.Context) { language := ctx.FormTrim("language") ctx.Data["Language"] = language + followers, numFollowers, err := user_model.GetUserFollowers(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{ + PageSize: pagingNum, + Page: page, + }) + if err != nil { + ctx.ServerError("GetUserFollowers", err) + return + } + ctx.Data["NumFollowers"] = numFollowers + following, numFollowing, err := user_model.GetUserFollowing(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{ + PageSize: pagingNum, + Page: page, + }) + if err != nil { + ctx.ServerError("GetUserFollowing", err) + return + } + ctx.Data["NumFollowing"] = numFollowing + switch tab { case "followers": - items, count, err := user_model.GetUserFollowers(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{ - PageSize: pagingNum, - Page: page, - }) - if err != nil { - ctx.ServerError("GetUserFollowers", err) - return - } - ctx.Data["Cards"] = items - + ctx.Data["Cards"] = followers total = int(count) case "following": - items, count, err := user_model.GetUserFollowing(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{ - PageSize: pagingNum, - Page: page, - }) - if err != nil { - ctx.ServerError("GetUserFollowing", err) - return - } - ctx.Data["Cards"] = items - + ctx.Data["Cards"] = following total = int(count) case "activity": date := ctx.FormString("date") diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 1bd017d119..afd7d61ca9 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -19,6 +19,18 @@ {{if .Org.Website}}
{{svg "octicon-link"}} {{.Org.Website}}
{{end}} + {{template "org/menu" .}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index c3a06a16ee..9f454a82f1 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -22,7 +22,7 @@ {{svg "octicon-rss" 18}} {{end}}