From 9e321b212dce3ab4ed041f02a745ba467446868f Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Tue, 10 Oct 2023 10:27:38 -0600 Subject: [PATCH] Fix for repo lists --- models/repo/repo_dcs.go | 14 +++++++++ routers/api/v1/admin/user_dcs.go | 50 +++++++++++--------------------- routers/web/explore/repo.go | 11 +++++-- routers/web/org/home.go | 10 +++---- routers/web/user/notification.go | 8 +++++ templates/explore/repo_list.tmpl | 1 - 6 files changed, 53 insertions(+), 41 deletions(-) diff --git a/models/repo/repo_dcs.go b/models/repo/repo_dcs.go index b256720998..131c59eda9 100644 --- a/models/repo/repo_dcs.go +++ b/models/repo/repo_dcs.go @@ -85,3 +85,17 @@ func (repo *Repository) LoadLatestDMs(ctx context.Context) error { return nil } + +// LoadLatestDMs loads the latest Door43Metadatas for the given RepositoryList +func (rl RepositoryList) LoadLatestDMs(ctx context.Context) error { + if rl.Len() == 0 { + return nil + } + var lastErr error + for _, repo := range rl { + if err := repo.LoadLatestDMs(ctx); err != nil && lastErr == nil { + lastErr = err + } + } + return lastErr +} diff --git a/routers/api/v1/admin/user_dcs.go b/routers/api/v1/admin/user_dcs.go index e0f75e7bd5..5c78134a98 100644 --- a/routers/api/v1/admin/user_dcs.go +++ b/routers/api/v1/admin/user_dcs.go @@ -13,8 +13,6 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" - "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/convert" user_service "code.gitea.io/gitea/services/user" ) @@ -23,33 +21,16 @@ import ( func ListSpamUsers(ctx *context.APIContext) { // swagger:operation GET /admin/users/spam admin adminListSpamUsers // --- - // summary: List all users considered to be spam. NOTE - not all will be deleted in the DELETE action. see its description + // summary: List all users considered to be spam. (have a description & website, last logged in on the day they signed up, and is older than a week) // produces: // - application/json - // parameters: - // - name: page - // in: query - // description: page number of results to return (1-based) - // type: integer - // - name: limit - // in: query - // description: page size of results - // type: integer // responses: // "200": // "$ref": "#/responses/UserList" // "403": // "$ref": "#/responses/forbidden" - listOptions := utils.GetListOptions(ctx) - - users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ - Actor: ctx.Doer, - Type: user_model.UserTypeIndividual, - OrderBy: db.SearchUserOrderByAlphabetically, - IsSpamUser: util.OptionalBoolTrue, - ListOptions: listOptions, - }) + users, err := getSpamUsers(ctx) if err != nil { ctx.Error(http.StatusInternalServerError, "ListSpamUsers", err) return @@ -60,8 +41,6 @@ func ListSpamUsers(ctx *context.APIContext) { results[i] = convert.ToUser(ctx, users[i], ctx.Doer) } - ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) - ctx.SetTotalCountHeader(maxResults) ctx.JSON(http.StatusOK, &results) } @@ -85,15 +64,7 @@ func DeleteSpamUsers(ctx *context.APIContext) { // "422": // "$ref": "#/responses/validationError" - users := make([]*user_model.User, 0) - err := db.GetEngine(ctx). - OrderBy("id"). - Where("type = ?", user_model.UserTypeIndividual). - And("TIMESTAMPDIFF(DAY, FROM_UNIXTIME(created_unix), FROM_UNIXTIME(last_login_unix)) <= 2"). - And("description != ''"). - And("website != ''"). - And("num_repos = 0"). - And("last_login_unix < UNIX_TIMESTAMP(NOW() - INTERVAL 1 WEEK)").Find(&users) + users, err := getSpamUsers(ctx) if err != nil { ctx.Error(http.StatusInternalServerError, "DeleteSpamUsers", err) return @@ -110,8 +81,21 @@ func DeleteSpamUsers(ctx *context.APIContext) { } return } - log.Trace("Account deleted by admin(%s) due to being spam: %s", ctx.Doer.Name, user.Name) + log.Info("Account deleted by admin(%s) due to being spam: %s", ctx.Doer.Name, user.Name) } ctx.Status(http.StatusNoContent) } + +func getSpamUsers(ctx *context.APIContext) ([]*user_model.User, error) { + users := make([]*user_model.User, 0) + err := db.GetEngine(ctx). + OrderBy("id"). + Where("type = ?", user_model.UserTypeIndividual). + And("TIMESTAMPDIFF(DAY, FROM_UNIXTIME(created_unix), FROM_UNIXTIME(last_login_unix)) <= 2"). + And("description != ''"). + And("website != ''"). + And("num_repos = 0"). + And("last_login_unix < UNIX_TIMESTAMP(NOW() - INTERVAL 1 WEEK)").Find(&users) + return users, err +} diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index fa8c4e8425..487068f46a 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -53,7 +53,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { } var ( - repos []*repo_model.Repository + repos repo_model.RepositoryList // DCS Customizations - Fixed this count int64 err error orderBy db.SearchOrderBy @@ -159,6 +159,14 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { ctx.ServerError("SearchRepository", err) return } + + /*** DCS Customizations ***/ + err = repos.LoadLatestDMs(ctx) + if err != nil { + log.Error("LoadLatestDMs: unable to load DMs for repos") + } + /*** END DCS Customizations ***/ + if isSitemap { m := sitemap.NewSitemap() for _, item := range repos { @@ -174,7 +182,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { ctx.Data["Keyword"] = origKeyword // DCS Customizations ctx.Data["Total"] = count ctx.Data["Repos"] = repos - ctx.Data["ctx"] = ctx // DCS Customizations ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled pager := context.NewPagination(int(count), opts.PageSize, page, 5) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 648f525896..73746dbca4 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -126,7 +126,7 @@ func Home(ctx *context.Context) { /*** END DCS Customizations ***/ var ( - repos []*repo_model.Repository + repos repo_model.RepositoryList // DCS Customizations - Fixed this count int64 err error ) @@ -156,11 +156,11 @@ func Home(ctx *context.Context) { ctx.ServerError("SearchRepository", err) return } + /*** DCS Customizations ***/ - for _, repo := range repos { - if err := repo.LoadLatestDMs(ctx); err != nil { - log.Error("Error LoadLatestDMs [%s]: %v", repo.FullName(), err) - } + err = repos.LoadLatestDMs(ctx) + if err != nil { + log.Error("LoadLatestDMs: unable to load DMs for repos") } /*** End DCS Customizations ***/ diff --git a/routers/web/user/notification.go b/routers/web/user/notification.go index cae12f4126..4db1a5215c 100644 --- a/routers/web/user/notification.go +++ b/routers/web/user/notification.go @@ -394,6 +394,14 @@ func NotificationWatching(ctx *context.Context) { return } total := int(count) + + /*** DCS Customizations ***/ + err = repos.LoadLatestDMs(ctx) + if err != nil { + log.Error("LoadLatestDMs: unable to load DMs for repos") + } + /*** END DCS Customizations ***/ + ctx.Data["Total"] = total ctx.Data["Repos"] = repos diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index af2f1c23d2..7d235da181 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -1,6 +1,5 @@
{{range .Repos}} - {{.LoadLatestDMs $.ctx}}