package repo import ( "context" "database/sql" "errors" "fmt" "strings" "time" "polar/internal/user" ) const ( RoleRead = "read" RoleWrite = "write" ) var ( ErrCollabUserNotFound = errors.New("user not found") ErrCannotAddOwner = errors.New("repository owner cannot be added as a collaborator") ) type Collaborator struct { UserID int64 Username string FullName string AvatarURL string Role string CreatedAt time.Time } func normalizeRole(role string) string { if strings.TrimSpace(role) == RoleRead { return RoleRead } return RoleWrite } func (s *Service) AddCollaborator(ctx context.Context, r *Repository, username, role string) error { username = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(username), "@")) if username == "" { return ErrCollabUserNotFound } cctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() var userID int64 err := s.db.QueryRowContext(cctx, `SELECT id FROM users WHERE LOWER(username) = LOWER(?)`, username).Scan(&userID) if err != nil { if errors.Is(err, sql.ErrNoRows) { return ErrCollabUserNotFound } return fmt.Errorf("lookup collaborator user: %w", err) } if userID == r.OwnerID { return ErrCannotAddOwner } now := time.Now().UTC() _, err = s.db.ExecContext(cctx, ` INSERT INTO collaborators (repo_id, user_id, role, created_at) VALUES (?, ?, ?, ?) ON CONFLICT(repo_id, user_id) DO UPDATE SET role = excluded.role `, r.ID, userID, normalizeRole(role), now) if err != nil { return fmt.Errorf("upsert collaborator: %w", err) } return nil } func (s *Service) RemoveCollaborator(ctx context.Context, repoID, userID int64) error { cctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() _, err := s.db.ExecContext(cctx, `DELETE FROM collaborators WHERE repo_id = ? AND user_id = ?`, repoID, userID) if err != nil { return fmt.Errorf("remove collaborator: %w", err) } return nil } func (s *Service) ListCollaborators(ctx context.Context, repoID int64) ([]Collaborator, error) { cctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() rows, err := s.db.QueryContext(cctx, ` SELECT c.user_id, u.username, u.full_name, u.avatar_url, c.role, c.created_at FROM collaborators c JOIN users u ON u.id = c.user_id WHERE c.repo_id = ? ORDER BY c.created_at ASC `, repoID) if err != nil { return nil, fmt.Errorf("list collaborators: %w", err) } defer rows.Close() var list []Collaborator for rows.Next() { var c Collaborator if err := rows.Scan(&c.UserID, &c.Username, &c.FullName, &c.AvatarURL, &c.Role, &c.CreatedAt); err != nil { return nil, fmt.Errorf("scan collaborator: %w", err) } list = append(list, c) } return list, rows.Err() } func (s *Service) GetCollaboratorRole(ctx context.Context, repoID, userID int64) string { cctx, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() var role string err := s.db.QueryRowContext(cctx, `SELECT role FROM collaborators WHERE repo_id = ? AND user_id = ?`, repoID, userID).Scan(&role) if err != nil { return "" } return role } // CanRead reports whether the user may read the repository (owner, admin, // collaborator of any role, or anyone for public repositories). func (s *Service) CanRead(ctx context.Context, r *Repository, u *user.User) bool { if r == nil { return false } if !r.IsPrivate { return true } if u == nil { return false } if u.ID == r.OwnerID || u.IsAdmin { return true } role := s.GetCollaboratorRole(ctx, r.ID, u.ID) return role == RoleRead || role == RoleWrite } // CanWrite reports whether the user may push to the repository (owner, admin // or a collaborator with the write role). func (s *Service) CanWrite(ctx context.Context, r *Repository, u *user.User) bool { if r == nil || u == nil { return false } if u.ID == r.OwnerID || u.IsAdmin { return true } return s.GetCollaboratorRole(ctx, r.ID, u.ID) == RoleWrite }