Skip to content

Commit

Permalink
Fix for repo lists
Browse files Browse the repository at this point in the history
  • Loading branch information
richmahn committed Oct 10, 2023
1 parent 10a715f commit 9e321b2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 41 deletions.
14 changes: 14 additions & 0 deletions models/repo/repo_dcs.go
Expand Up @@ -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
}
50 changes: 17 additions & 33 deletions routers/api/v1/admin/user_dcs.go
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -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
}
11 changes: 9 additions & 2 deletions routers/web/explore/repo.go
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions routers/web/org/home.go
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 ***/

Expand Down
8 changes: 8 additions & 0 deletions routers/web/user/notification.go
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion templates/explore/repo_list.tmpl
@@ -1,6 +1,5 @@
<div class="ui repository list">
{{range .Repos}}
{{.LoadLatestDMs $.ctx}}
<div class="item">
<div class="ui header gt-df gt-ac">
<div class="repo-title">
Expand Down

0 comments on commit 9e321b2

Please sign in to comment.