Vectrum
Sign In
33 lines · 1.3 KB · TypeScript
import "server-only";
import { lastActivity, log, type Commit } from "./git";
import { countLikes } from "./lists";
import { visibleParent, type RepoRecord } from "./repos";

/**
 * Adds last-activity info and the number of likes to repositories for list views, and for forks the
 * repository they came from if the viewer may see it.
 */
export async function withActivity(repos: RepoRecord[], viewerId?: string | null) {
  return Promise.all(
    repos.map(async (r) => {
      const [last, parent, likes] = await Promise.all([lastActivity(r.dir), visibleParent(r, viewerId), countLikes(r.id)]);
      return { ...r, updatedAt: last ?? r.createdAt, empty: last === null, parent, likes };
    }),
  );
}

export type ActivityItem = { repo: RepoRecord; commit: Commit };

/** The most recent commits across the given repositories, newest first. */
export async function recentActivity(repos: RepoRecord[], limit = 20): Promise<ActivityItem[]> {
  const perRepo = await Promise.all(
    repos.slice(0, 20).map(async (repo) => {
      const commits = await log(repo.dir, "--branches", { limit: 8 }).catch(() => []);
      return commits.map((commit) => ({ repo, commit }));
    }),
  );
  return perRepo
    .flat()
    .sort((a, b) => b.commit.date - a.commit.date)
    .slice(0, limit);
}