357 lines · 13.5 KB
import { createServerFn } from "@tanstack/react-start";
import { assertBelowCap, CAPS } from "~/server/limits";
import { limitWrite } from "~/server/limits";
import { z } from "zod";
import { requireViewer, viewerMiddleware } from "./middleware";
import { all, first, MAX_IN_LIST, placeholders, run } from "~/server/db";
import { randomId, randomToken, sha256Hex, hashPassword } from "~/server/crypto";
import { PAT_PREFIX, createSession } from "~/server/auth";
import { confirmPassword, notifySecurityEvent, revokeAllSessions } from "~/server/account-security";
import { createOrg } from "~/server/accounts";
import { canAdministerAccount, toRepoSummaries, visibleReposClause } from "~/server/repos";
import { accountOrGhost, accountsById } from "~/server/people";
import { badRequest, forbidden, notFoundError } from "~/server/errors";
import { accountName, password } from "~/lib/validation";
import type { AccountRow, JsonValue, RepoRow } from "~/lib/types";

const ACTIVITY_LIMIT = 40;
const VISIBLE_TOKEN_CHARS = 6;
const DAY_MS = 24 * 60 * 60 * 1000;

async function activityFeed(where: string, ...params: string[]) {
  const rows = await all<{
    id: string;
    actor_id: string | null;
    repo_id: string | null;
    kind: string;
    data: string;
    created_at: number;
    repo_name: string | null;
    owner_name: string | null;
  }>(
    `SELECT ac.*, r.name AS repo_name, a.name AS owner_name FROM activity ac
     LEFT JOIN repos r ON r.id = ac.repo_id LEFT JOIN accounts a ON a.id = r.owner_id
     WHERE ${where} AND ac.kind != 'view' ORDER BY ac.created_at DESC LIMIT ${ACTIVITY_LIMIT}`,
    ...params,
  );
  const people = await accountsById(rows.map((row) => row.actor_id));
  return rows.map((row) => ({
    id: row.id,
    actor: accountOrGhost(people, row.actor_id),