Spaces:
Running

File size: 3,142 Bytes
f41aded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Proje verisine erişim için ortak yardımcılar: yetki, dosya yazma, sürüm anlık görüntüsü.
import { db } from './db.js';
import { HttpError } from './util.js';

const RANK = { viewer: 1, editor: 2, owner: 3 };

/** Express ara katmanı: projeyi yükler, kullanıcının en az `minRole` yetkisi olduğunu doğrular. */
export const withProject = (minRole = 'viewer') => (req, _res, next) => {
  const project = db.prepare('SELECT * FROM projects WHERE id = ?').get(req.params.id);
  let role = null;
  if (project) {
    role = project.owner_id === req.user.id
      ? 'owner'
      : db.prepare('SELECT role FROM project_members WHERE project_id = ? AND user_id = ?')
          .get(project.id, req.user.id)?.role ?? null;
  }
  // Yetkisiz kullanıcıya projenin var olduğunu sezdirmemek için 404 dönülür.
  if (!role) throw new HttpError(404, 'Proje bulunamadı.');
  if (RANK[role] < RANK[minRole]) throw new HttpError(403, 'Bu işlem için yetkiniz yok.');
  req.project = project;
  req.role = role;
  next();
};

/** Proje genelinde tekil ve artan bir revizyon numarası üretir; projenin güncellenme zamanını yeniler. */
export function nextRev(projectId) {
  return db
    .prepare(`UPDATE projects SET rev_counter = rev_counter + 1, updated_at = datetime('now') WHERE id = ? RETURNING rev_counter`)
    .get(projectId).rev_counter;
}

export function putFile(projectId, path, content, isBinary) {
  const rev = nextRev(projectId);
  db.prepare(`
    INSERT INTO files (project_id, path, content, is_binary, rev) VALUES (?, ?, ?, ?, ?)
    ON CONFLICT (project_id, path) DO UPDATE SET content = excluded.content, is_binary = excluded.is_binary, rev = excluded.rev
  `).run(projectId, path, content, isBinary ? 1 : 0, rev);
  return rev;
}

export function listFiles(projectId) {
  return db
    .prepare('SELECT path, is_binary AS isBinary, rev, length(content) AS size FROM files WHERE project_id = ? ORDER BY path')
    .all(projectId)
    .map((f) => ({ ...f, isBinary: Boolean(f.isBinary) }));
}

export function snapshot(projectId, userId, label, isAuto = false) {
  const { lastInsertRowid } = db
    .prepare('INSERT INTO versions (project_id, user_id, label, is_auto) VALUES (?, ?, ?, ?)')
    .run(projectId, userId, label, isAuto ? 1 : 0);
  db.prepare(`
    INSERT INTO version_files (version_id, path, content, is_binary)
    SELECT ?, path, content, is_binary FROM files WHERE project_id = ?
  `).run(lastInsertRowid, projectId);
  // Otomatik kayıtların yalnızca son 40'ı tutulur.
  db.prepare(`
    DELETE FROM versions WHERE project_id = ? AND is_auto = 1 AND id NOT IN (
      SELECT id FROM versions WHERE project_id = ? AND is_auto = 1 ORDER BY id DESC LIMIT 40
    )
  `).run(projectId, projectId);
  return Number(lastInsertRowid);
}

/** Son sürüm kaydı 10 dakikadan eskiyse otomatik bir kayıt oluşturur. */
export function maybeAutoSnapshot(projectId, userId) {
  const recent = db
    .prepare(`SELECT 1 FROM versions WHERE project_id = ? AND created_at > datetime('now', '-10 minutes')`)
    .get(projectId);
  if (!recent) snapshot(projectId, userId, 'Otomatik kayıt', true);
}