|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "code.gitea.io/gitea/modules/log" |
| 10 | + |
| 11 | + issue_model "code.gitea.io/gitea/models/issues" |
| 12 | + repo_model "code.gitea.io/gitea/models/repo" |
| 13 | + "code.gitea.io/gitea/routers/utils" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + "code.gitea.io/gitea/modules/templates" |
| 16 | + "code.gitea.io/gitea/models/db" |
| 17 | + "code.gitea.io/gitea/services/context" |
| 18 | + user_model "code.gitea.io/gitea/models/user" |
| 19 | +) |
| 20 | + |
| 21 | +const tplIssuesWithLinks templates.TplName = "admin/issues_with_links" |
| 22 | + |
| 23 | +// item with link in content |
| 24 | +type linkItem struct { |
| 25 | + Type string |
| 26 | + Content string |
| 27 | + User *user_model.User |
| 28 | + UserCreated time.Time |
| 29 | + RepoLink string |
| 30 | + ItemLink string |
| 31 | + Created time.Time |
| 32 | +} |
| 33 | + |
| 34 | +// IssuesWithLinks shows issues and comments with external links |
| 35 | +func IssuesWithLinks(ctx *context.Context) { |
| 36 | + ctx.Data["Title"] = ctx.Tr("admin.issues_with_links") |
| 37 | + ctx.Data["PageIsIssuesWithLinks"] = true |
| 38 | + |
| 39 | + page := ctx.FormInt("page") |
| 40 | + if page <= 1 { |
| 41 | + page = 1 |
| 42 | + } |
| 43 | + |
| 44 | + // fetch recent issues and comments |
| 45 | + limit := setting.UI.Admin.UserPagingNum |
| 46 | + issues, err := issue_model.GetRecentIssues(ctx, &db.ListOptions{Page: page, PageSize: limit}) |
| 47 | + if err != nil { |
| 48 | + ctx.ServerError("GetRecentIssues", err) |
| 49 | + return |
| 50 | + } |
| 51 | + comments, err := issue_model.GetRecentComments(ctx, &db.ListOptions{Page: page, PageSize: limit}) |
| 52 | + if err != nil { |
| 53 | + ctx.ServerError("GetRecentComments", err) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + var excludedDomains = []string{ |
| 58 | + "localhost:3000", |
| 59 | + "github.com", |
| 60 | + } |
| 61 | + |
| 62 | + var items []linkItem |
| 63 | + appendIfHasLink := func(typeLabel, content, itemLink, repoLink string, created time.Time, u *user_model.User) { |
| 64 | + if !utils.ContainsHyperlink(content) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + snippet := extractFirstLinkSnippet(content) |
| 69 | + if snippet == "" || utils.ContainsExcludedDomain(snippet, excludedDomains) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + items = append(items, linkItem{ |
| 74 | + Type: typeLabel, |
| 75 | + Content: snippet, |
| 76 | + User: u, |
| 77 | + UserCreated: u.CreatedUnix.AsTime(), |
| 78 | + RepoLink: repoLink, |
| 79 | + ItemLink: itemLink, |
| 80 | + Created: created, |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + for _, issue := range issues { |
| 85 | + |
| 86 | + if issue.Content == "" { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + if issue.Repo == nil { |
| 91 | + issue.Repo, err = repo_model.GetRepositoryByID(ctx, issue.RepoID) |
| 92 | + if err != nil { |
| 93 | + log.Warn("Could not load repo for issue %d: %v", issue.ID, err) |
| 94 | + continue |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + u, err := user_model.GetUserByID(ctx, issue.PosterID) |
| 99 | + if err != nil { |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + appendIfHasLink("Issue", issue.Content, issue.HTMLURL(), issue.Repo.HTMLURL(), issue.CreatedUnix.AsTime(), u) |
| 104 | + } |
| 105 | + |
| 106 | + for _, comment := range comments { |
| 107 | + if comment.Issue == nil { |
| 108 | + comment.Issue, err = issue_model.GetIssueByID(ctx, comment.IssueID) |
| 109 | + if err != nil { |
| 110 | + log.Warn("Could not load issue for comment %d: %v", comment.ID, err) |
| 111 | + continue |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if comment.Issue.Repo == nil { |
| 116 | + comment.Issue.Repo, err = repo_model.GetRepositoryByID(ctx, comment.Issue.RepoID) |
| 117 | + if err != nil { |
| 118 | + log.Warn("Could not load repo for issue %d: %v", comment.Issue.ID, err) |
| 119 | + continue |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + if comment.Content == "" { |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + u, err := user_model.GetUserByID(ctx, comment.PosterID) |
| 129 | + if err != nil { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + appendIfHasLink("Comment", comment.Content, comment.HTMLURL(ctx), comment.Issue.Repo.HTMLURL(), comment.CreatedUnix.AsTime(), u) |
| 134 | + } |
| 135 | + |
| 136 | + // Sort by user creation date desc |
| 137 | + sort.Slice(items, func(i, j int) bool { |
| 138 | + return items[i].UserCreated.After(items[j].UserCreated) |
| 139 | + }) |
| 140 | + |
| 141 | + total := len(items) |
| 142 | + start := (page - 1) * limit |
| 143 | + end := start + limit |
| 144 | + if start > total { |
| 145 | + start = total |
| 146 | + } |
| 147 | + if end > total { |
| 148 | + end = total |
| 149 | + } |
| 150 | + paged := items[start:end] |
| 151 | + |
| 152 | + ctx.Data["Items"] = paged |
| 153 | + ctx.Data["Total"] = total |
| 154 | + |
| 155 | + // pager |
| 156 | + pager := context.NewPagination(total, limit, page, 5) |
| 157 | + pager.AddParamFromRequest(ctx.Req) |
| 158 | + ctx.Data["Page"] = pager |
| 159 | + |
| 160 | + ctx.HTML(http.StatusOK, tplIssuesWithLinks) |
| 161 | +} |
| 162 | + |
| 163 | +// for brevity, just return the first 200 chars containing http[s] |
| 164 | +func extractFirstLinkSnippet(text string) string { |
| 165 | + lower := strings.ToLower(text) |
| 166 | + i := strings.Index(lower, "http://") |
| 167 | + if i < 0 { |
| 168 | + i = strings.Index(lower, "https://") |
| 169 | + } |
| 170 | + if i < 0 { |
| 171 | + return "" |
| 172 | + } |
| 173 | + start := i |
| 174 | + end := start + 200 |
| 175 | + if end > len(text) { |
| 176 | + end = len(text) |
| 177 | + } |
| 178 | + return text[start:end] |
| 179 | +} |
0 commit comments