@@ -29,8 +29,8 @@ import ( |
| 29 | 29 | |
| 30 | 30 | const ( |
| 31 | 31 | profileContribWeeks = 53 |
| 32 | | - profileContribRepoLimit = 80 |
| 33 | | - profileContribMaxPerRepo = 2000 |
| 32 | + profileContribRepoLimit = 500 |
| 33 | + profileContribMaxPerRepo = 5000 |
| 34 | 34 | profileReadmeMaxBytes = 1 * 1024 * 1024 |
| 35 | 35 | ) |
| 36 | 36 | |
@@ -50,15 +50,23 @@ type profileReadme struct { |
| 50 | 50 | |
| 51 | 51 | type contributionCalendar struct { |
| 52 | 52 | Total int |
| 53 | + Period string |
| 53 | 54 | Weeks []contributionWeek |
| 54 | | - Years []int |
| 55 | + Years []contributionYear |
| 55 | 56 | CurrentYear int |
| 57 | + SelectedYear int |
| 56 | 58 | MonthLabel string |
| 57 | 59 | MonthCommitCount int |
| 58 | 60 | MonthRepoCount int |
| 59 | 61 | HasRepositoryData bool |
| 60 | 62 | } |
| 61 | 63 | |
| 64 | +type contributionYear struct { |
| 65 | + Year int |
| 66 | + Href string |
| 67 | + Active bool |
| 68 | +} |
| 69 | + |
| 62 | 70 | type contributionWeek struct { |
| 63 | 71 | MonthLabel string |
| 64 | 72 | Days []contributionDay |
@@ -73,6 +81,11 @@ type contributionDay struct { |
| 73 | 81 | IsInWindow bool |
| 74 | 82 | } |
| 75 | 83 | |
| 84 | +type profileContributionRepo struct { |
| 85 | + Repo reposdb.Repo |
| 86 | + OwnerSlug string |
| 87 | +} |
| 88 | + |
| 76 | 89 | func (h *Handlers) visibleUserRepos(ctx context.Context, userID int64, viewer middleware.CurrentUser) []reposdb.Repo { |
| 77 | 90 | rows, err := reposdb.New().ListReposForOwnerUser(ctx, h.d.Pool, pgtype.Int8{Int64: userID, Valid: true}) |
| 78 | 91 | if err != nil { |
@@ -183,27 +196,38 @@ func (h *Handlers) profileReadme(ctx context.Context, user usersdb.User, viewer |
| 183 | 196 | return profileReadme{}, false |
| 184 | 197 | } |
| 185 | 198 | |
| 186 | | -func (h *Handlers) contributionCalendar(ctx context.Context, user usersdb.User, repos []reposdb.Repo) contributionCalendar { |
| 199 | +func (h *Handlers) contributionCalendar(ctx context.Context, user usersdb.User, viewer middleware.CurrentUser, query url.Values) contributionCalendar { |
| 187 | 200 | now := time.Now().UTC() |
| 188 | 201 | today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) |
| 202 | + currentYear := today.Year() |
| 203 | + selectedYear := selectedContributionYear(query, currentYear) |
| 189 | 204 | windowStart := today.AddDate(-1, 0, 1) |
| 205 | + windowEndDay := today |
| 206 | + period := "in the last year" |
| 207 | + if selectedYear != currentYear { |
| 208 | + windowStart = time.Date(selectedYear, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 209 | + windowEndDay = time.Date(selectedYear, time.December, 31, 0, 0, 0, 0, time.UTC) |
| 210 | + period = "in " + strconv.Itoa(selectedYear) |
| 211 | + } |
| 190 | 212 | gridStart := windowStart.AddDate(0, 0, -int(windowStart.Weekday())) |
| 191 | | - windowEnd := today.Add(24 * time.Hour) |
| 213 | + windowEnd := windowEndDay.Add(24 * time.Hour) |
| 214 | + activityMonth := time.Date(windowEndDay.Year(), windowEndDay.Month(), 1, 0, 0, 0, 0, time.UTC) |
| 215 | + repos := h.profileContributionRepos(ctx, user, viewer) |
| 192 | 216 | |
| 193 | 217 | counts := map[string]int{} |
| 194 | 218 | reposWithMonthActivity := map[int64]struct{}{} |
| 195 | 219 | if h.d.RepoFS != nil && len(repos) > 0 { |
| 196 | 220 | emails := h.verifiedEmails(ctx, user.ID) |
| 197 | | - for i, repo := range repos { |
| 221 | + for i, source := range repos { |
| 198 | 222 | if i >= profileContribRepoLimit { |
| 199 | 223 | break |
| 200 | 224 | } |
| 201 | | - gitDir, err := h.d.RepoFS.RepoPath(user.Username, repo.Name) |
| 225 | + gitDir, err := h.d.RepoFS.RepoPath(source.OwnerSlug, source.Repo.Name) |
| 202 | 226 | if err != nil { |
| 203 | 227 | continue |
| 204 | 228 | } |
| 205 | 229 | commits, err := repogit.Log(ctx, gitDir, repogit.LogOptions{ |
| 206 | | - Ref: repo.DefaultBranch, |
| 230 | + Ref: source.Repo.DefaultBranch, |
| 207 | 231 | MaxCount: profileContribMaxPerRepo, |
| 208 | 232 | Since: windowStart, |
| 209 | 233 | Until: windowEnd, |
@@ -212,19 +236,17 @@ func (h *Handlers) contributionCalendar(ctx context.Context, user usersdb.User, |
| 212 | 236 | continue |
| 213 | 237 | } |
| 214 | 238 | for _, commit := range commits { |
| 215 | | - if len(emails) > 0 { |
| 216 | | - if _, ok := emails[strings.ToLower(strings.TrimSpace(commit.AuthorEmail))]; !ok { |
| 217 | | - continue |
| 218 | | - } |
| 239 | + if !commitMatchesProfileUser(commit, emails, user) { |
| 240 | + continue |
| 219 | 241 | } |
| 220 | 242 | day := time.Date(commit.AuthorWhen.UTC().Year(), commit.AuthorWhen.UTC().Month(), commit.AuthorWhen.UTC().Day(), 0, 0, 0, 0, time.UTC) |
| 221 | | - if day.Before(windowStart) || day.After(today) { |
| 243 | + if day.Before(windowStart) || day.After(windowEndDay) { |
| 222 | 244 | continue |
| 223 | 245 | } |
| 224 | 246 | key := day.Format("2006-01-02") |
| 225 | 247 | counts[key]++ |
| 226 | | - if day.Year() == today.Year() && day.Month() == today.Month() { |
| 227 | | - reposWithMonthActivity[repo.ID] = struct{}{} |
| 248 | + if day.Year() == activityMonth.Year() && day.Month() == activityMonth.Month() { |
| 249 | + reposWithMonthActivity[source.Repo.ID] = struct{}{} |
| 228 | 250 | } |
| 229 | 251 | } |
| 230 | 252 | } |
@@ -239,10 +261,10 @@ func (h *Handlers) contributionCalendar(ctx context.Context, user usersdb.User, |
| 239 | 261 | day := gridStart.AddDate(0, 0, w*7+d) |
| 240 | 262 | key := day.Format("2006-01-02") |
| 241 | 263 | count := counts[key] |
| 242 | | - inWindow := !day.Before(windowStart) && !day.After(today) |
| 264 | + inWindow := !day.Before(windowStart) && !day.After(windowEndDay) |
| 243 | 265 | if inWindow { |
| 244 | 266 | total += count |
| 245 | | - if day.Year() == today.Year() && day.Month() == today.Month() { |
| 267 | + if day.Year() == activityMonth.Year() && day.Month() == activityMonth.Month() { |
| 246 | 268 | monthCommitCount += count |
| 247 | 269 | } |
| 248 | 270 | } |
@@ -260,19 +282,126 @@ func (h *Handlers) contributionCalendar(ctx context.Context, user usersdb.User, |
| 260 | 282 | } |
| 261 | 283 | weeks = append(weeks, week) |
| 262 | 284 | } |
| 263 | | - years := []int{today.Year(), today.Year() - 1, today.Year() - 2, today.Year() - 3} |
| 264 | 285 | return contributionCalendar{ |
| 265 | 286 | Total: total, |
| 287 | + Period: period, |
| 266 | 288 | Weeks: weeks, |
| 267 | | - Years: years, |
| 268 | | - CurrentYear: today.Year(), |
| 269 | | - MonthLabel: today.Format("January 2006"), |
| 289 | + Years: contributionYears(user.Username, currentYear, selectedYear), |
| 290 | + CurrentYear: currentYear, |
| 291 | + SelectedYear: selectedYear, |
| 292 | + MonthLabel: activityMonth.Format("January 2006"), |
| 270 | 293 | MonthCommitCount: monthCommitCount, |
| 271 | 294 | MonthRepoCount: len(reposWithMonthActivity), |
| 272 | 295 | HasRepositoryData: h.d.RepoFS != nil && len(repos) > 0, |
| 273 | 296 | } |
| 274 | 297 | } |
| 275 | 298 | |
| 299 | +func (h *Handlers) profileContributionRepos(ctx context.Context, user usersdb.User, viewer middleware.CurrentUser) []profileContributionRepo { |
| 300 | + actor := policy.AnonymousActor() |
| 301 | + if !viewer.IsAnonymous() { |
| 302 | + actor = viewer.PolicyActor() |
| 303 | + } |
| 304 | + deps := policy.Deps{Pool: h.d.Pool} |
| 305 | + queries := reposdb.New() |
| 306 | + seen := map[int64]struct{}{} |
| 307 | + out := make([]profileContributionRepo, 0, 64) |
| 308 | + add := func(ownerSlug string, repo reposdb.Repo) { |
| 309 | + if ownerSlug == "" { |
| 310 | + return |
| 311 | + } |
| 312 | + if _, ok := seen[repo.ID]; ok { |
| 313 | + return |
| 314 | + } |
| 315 | + if !policy.IsVisibleTo(ctx, deps, actor, policy.NewRepoRefFromRepo(repo)) { |
| 316 | + return |
| 317 | + } |
| 318 | + seen[repo.ID] = struct{}{} |
| 319 | + out = append(out, profileContributionRepo{Repo: repo, OwnerSlug: ownerSlug}) |
| 320 | + } |
| 321 | + |
| 322 | + userRepos, err := queries.ListReposForOwnerUser(ctx, h.d.Pool, pgtype.Int8{Int64: user.ID, Valid: true}) |
| 323 | + if err != nil { |
| 324 | + h.d.Logger.WarnContext(ctx, "profile overview: contribution user repos", "user_id", user.ID, "error", err) |
| 325 | + } else { |
| 326 | + for _, repo := range userRepos { |
| 327 | + add(user.Username, repo) |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + orgRows, err := orgsdb.New().ListOrgsForUser(ctx, h.d.Pool, user.ID) |
| 332 | + if err != nil { |
| 333 | + h.d.Logger.WarnContext(ctx, "profile overview: contribution orgs", "user_id", user.ID, "error", err) |
| 334 | + } else { |
| 335 | + for _, org := range orgRows { |
| 336 | + orgRepos, err := queries.ListReposForOwnerOrg(ctx, h.d.Pool, pgtype.Int8{Int64: org.OrgID, Valid: true}) |
| 337 | + if err != nil { |
| 338 | + h.d.Logger.WarnContext(ctx, "profile overview: contribution org repos", "org_id", org.OrgID, "error", err) |
| 339 | + continue |
| 340 | + } |
| 341 | + for _, repo := range orgRepos { |
| 342 | + add(org.Slug, repo) |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + publicRepos, err := queries.ListPublicContributionRepos(ctx, h.d.Pool, int32(profileContribRepoLimit)) |
| 348 | + if err != nil { |
| 349 | + h.d.Logger.WarnContext(ctx, "profile overview: contribution public repos", "user_id", user.ID, "error", err) |
| 350 | + return out |
| 351 | + } |
| 352 | + for _, row := range publicRepos { |
| 353 | + add(row.OwnerSlug, row.Repo) |
| 354 | + } |
| 355 | + return out |
| 356 | +} |
| 357 | + |
| 358 | +func selectedContributionYear(query url.Values, currentYear int) int { |
| 359 | + for _, key := range []string{"year", "from"} { |
| 360 | + raw := strings.TrimSpace(query.Get(key)) |
| 361 | + if len(raw) >= 4 { |
| 362 | + raw = raw[:4] |
| 363 | + } |
| 364 | + year, err := strconv.Atoi(raw) |
| 365 | + if err == nil && year >= currentYear-100 && year <= currentYear { |
| 366 | + return year |
| 367 | + } |
| 368 | + } |
| 369 | + return currentYear |
| 370 | +} |
| 371 | + |
| 372 | +func contributionYears(username string, currentYear, selectedYear int) []contributionYear { |
| 373 | + years := make([]contributionYear, 0, 4) |
| 374 | + base := "/" + url.PathEscape(username) |
| 375 | + for i := 0; i < 4; i++ { |
| 376 | + year := currentYear - i |
| 377 | + href := base |
| 378 | + if year != currentYear { |
| 379 | + href += "?year=" + strconv.Itoa(year) |
| 380 | + } |
| 381 | + years = append(years, contributionYear{ |
| 382 | + Year: year, |
| 383 | + Href: href, |
| 384 | + Active: year == selectedYear, |
| 385 | + }) |
| 386 | + } |
| 387 | + return years |
| 388 | +} |
| 389 | + |
| 390 | +func commitMatchesProfileUser(commit repogit.Commit, verifiedEmails map[string]struct{}, user usersdb.User) bool { |
| 391 | + if len(verifiedEmails) > 0 { |
| 392 | + _, ok := verifiedEmails[strings.ToLower(strings.TrimSpace(commit.AuthorEmail))] |
| 393 | + return ok |
| 394 | + } |
| 395 | + name := strings.ToLower(strings.TrimSpace(commit.AuthorName)) |
| 396 | + if name == "" { |
| 397 | + return false |
| 398 | + } |
| 399 | + if name == strings.ToLower(user.Username) { |
| 400 | + return true |
| 401 | + } |
| 402 | + return user.DisplayName != "" && name == strings.ToLower(strings.TrimSpace(user.DisplayName)) |
| 403 | +} |
| 404 | + |
| 276 | 405 | func (h *Handlers) verifiedEmails(ctx context.Context, userID int64) map[string]struct{} { |
| 277 | 406 | rows, err := h.q.ListUserEmailsForUser(ctx, h.d.Pool, userID) |
| 278 | 407 | if err != nil { |
@@ -303,13 +432,29 @@ func contributionLevel(count int) int { |
| 303 | 432 | } |
| 304 | 433 | |
| 305 | 434 | func contributionDayTitle(count int, day time.Time) string { |
| 435 | + date := day.Format("January ") + ordinalDay(day.Day()) + "." |
| 306 | 436 | if count == 0 { |
| 307 | | - return "No contributions on " + day.Format("January 2") + "." |
| 437 | + return "No contributions on " + date |
| 308 | 438 | } |
| 309 | 439 | if count == 1 { |
| 310 | | - return "1 contribution on " + day.Format("January 2") + "." |
| 440 | + return "1 contribution on " + date |
| 441 | + } |
| 442 | + return strconv.Itoa(count) + " contributions on " + date |
| 443 | +} |
| 444 | + |
| 445 | +func ordinalDay(day int) string { |
| 446 | + suffix := "th" |
| 447 | + if day%100 < 11 || day%100 > 13 { |
| 448 | + switch day % 10 { |
| 449 | + case 1: |
| 450 | + suffix = "st" |
| 451 | + case 2: |
| 452 | + suffix = "nd" |
| 453 | + case 3: |
| 454 | + suffix = "rd" |
| 455 | + } |
| 311 | 456 | } |
| 312 | | - return strconv.Itoa(count) + " contributions on " + day.Format("January 2") + "." |
| 457 | + return strconv.Itoa(day) + suffix |
| 313 | 458 | } |
| 314 | 459 | |
| 315 | 460 | func profileCodeRouteBase(owner, repoName, route, ref, dir string) string { |