| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | |
| 7 | "gitlab.com/Timfa/bluegit/internal/access" |
| 8 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 9 | ) |
| 10 | |
| 11 | // SetLanguage bewaart de taalkeuze van een gebruiker. |
| 12 | func (s *Store) SetLanguage(ctx context.Context, userID int64, lang string) error { |
| 13 | _, err := s.db.ExecContext(ctx, `UPDATE users SET language = ? WHERE id = ?`, string(i18n.Parse(lang)), userID) |
| 14 | return err |
| 15 | } |
| 16 | |
| 17 | // UpdateGroup wijzigt beschrijving, zichtbaarheid en de standaardrol. |
| 18 | func (s *Store) UpdateGroup(ctx context.Context, id int64, description, visibility string, defaultRole access.Role) error { |
| 19 | if visibility != "public" && visibility != "private" { |
| 20 | return i18n.E("err.visibility") |
| 21 | } |
| 22 | if _, err := access.ParseRole(string(defaultRole)); err != nil { |
| 23 | return err |
| 24 | } |
| 25 | _, err := s.db.ExecContext(ctx, `UPDATE groups SET description = ?, visibility = ?, default_role = ? WHERE id = ?`, |
| 26 | strings.TrimSpace(description), visibility, string(defaultRole), id) |
| 27 | return err |
| 28 | } |
| 29 | |
| 30 | // SetLogoMIME onthoudt het type van een opgeslagen logo. Leeg wist het. |
| 31 | func (s *Store) SetLogoMIME(ctx context.Context, kind string, id int64, mime string) error { |
| 32 | switch kind { |
| 33 | case "group": |
| 34 | _, err := s.db.ExecContext(ctx, `UPDATE groups SET logo_mime = ? WHERE id = ?`, mime, id) |
| 35 | return err |
| 36 | case "repo": |
| 37 | _, err := s.db.ExecContext(ctx, `UPDATE repos SET logo_mime = ? WHERE id = ?`, mime, id) |
| 38 | return err |
| 39 | default: |
| 40 | return i18n.E("err.scope") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // GrantDefaultRoles zet op een nieuwe groepsrepository de standaardrol |
| 45 | // voor elk huidig lid. Beheerders van de groep blijven beheerder. |
| 46 | func (s *Store) GrantDefaultRoles(ctx context.Context, groupID, repoID int64) error { |
| 47 | g, err := s.GroupByID(ctx, groupID) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | rows, err := s.db.QueryContext(ctx, `SELECT user_id, role FROM group_members WHERE group_id = ?`, groupID) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | defer rows.Close() |
| 56 | for rows.Next() { |
| 57 | var uid int64 |
| 58 | var role string |
| 59 | if err := rows.Scan(&uid, &role); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | grant := g.DefaultRole |
| 63 | if access.Role(role) == access.RoleMaintainer { |
| 64 | grant = access.RoleMaintainer |
| 65 | } |
| 66 | if _, err := s.db.ExecContext(ctx, `INSERT INTO repo_members (repo_id, user_id, role) VALUES (?, ?, ?) |
| 67 | ON CONFLICT(repo_id, user_id) DO UPDATE SET role = excluded.role`, repoID, uid, string(grant)); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | } |
| 71 | return rows.Err() |
| 72 | } |
| 73 | |
| 74 | // SlugTaken meldt of een groeps- of repositorynaam al bezet is. |
| 75 | func (s *Store) SlugTaken(ctx context.Context, kind, slug, ownerKind string, ownerID int64) (bool, error) { |
| 76 | var n int |
| 77 | var err error |
| 78 | switch kind { |
| 79 | case "group": |
| 80 | err = s.db.QueryRowContext(ctx, `SELECT |
| 81 | (SELECT COUNT(*) FROM users WHERE username = ?) + |
| 82 | (SELECT COUNT(*) FROM groups WHERE path = ?)`, slug, slug).Scan(&n) |
| 83 | case "repo": |
| 84 | err = s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM repos WHERE owner_kind = ? AND owner_id = ? AND name = ?`, ownerKind, ownerID, slug).Scan(&n) |
| 85 | default: |
| 86 | return true, nil |
| 87 | } |
| 88 | if err != nil { |
| 89 | return false, err |
| 90 | } |
| 91 | return n > 0, nil |
| 92 | } |
| 93 | |
| 94 | // SetStar zet of haalt een ster weg. |
| 95 | func (s *Store) SetStar(ctx context.Context, userID, repoID int64, on bool) error { |
| 96 | if !on { |
| 97 | _, err := s.db.ExecContext(ctx, `DELETE FROM stars WHERE user_id = ? AND repo_id = ?`, userID, repoID) |
| 98 | return err |
| 99 | } |
| 100 | _, err := s.db.ExecContext(ctx, `INSERT INTO stars (user_id, repo_id, created_at) VALUES (?, ?, ?) |
| 101 | ON CONFLICT(user_id, repo_id) DO NOTHING`, userID, repoID, now()) |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | // StarInfo telt sterren en meldt welke de gebruiker zelf zette. |
| 106 | func (s *Store) StarInfo(ctx context.Context, userID int64, ids []int64) (map[int64]int, map[int64]bool, error) { |
| 107 | counts := map[int64]int{} |
| 108 | mine := map[int64]bool{} |
| 109 | if len(ids) == 0 { |
| 110 | return counts, mine, nil |
| 111 | } |
| 112 | q, args := inClause(ids) |
| 113 | rows, err := s.db.QueryContext(ctx, `SELECT repo_id, COUNT(*) FROM stars WHERE repo_id IN (`+q+`) GROUP BY repo_id`, args...) |
| 114 | if err != nil { |
| 115 | return nil, nil, err |
| 116 | } |
| 117 | defer rows.Close() |
| 118 | for rows.Next() { |
| 119 | var id int64 |
| 120 | var n int |
| 121 | if err := rows.Scan(&id, &n); err != nil { |
| 122 | return nil, nil, err |
| 123 | } |
| 124 | counts[id] = n |
| 125 | } |
| 126 | if err := rows.Err(); err != nil { |
| 127 | return nil, nil, err |
| 128 | } |
| 129 | if userID == 0 { |
| 130 | return counts, mine, nil |
| 131 | } |
| 132 | args = append([]any{userID}, args...) |
| 133 | mrows, err := s.db.QueryContext(ctx, `SELECT repo_id FROM stars WHERE user_id = ? AND repo_id IN (`+q+`)`, args...) |
| 134 | if err != nil { |
| 135 | return nil, nil, err |
| 136 | } |
| 137 | defer mrows.Close() |
| 138 | for mrows.Next() { |
| 139 | var id int64 |
| 140 | if err := mrows.Scan(&id); err != nil { |
| 141 | return nil, nil, err |
| 142 | } |
| 143 | mine[id] = true |
| 144 | } |
| 145 | return counts, mine, mrows.Err() |
| 146 | } |
| 147 | |
| 148 | func inClause(ids []int64) (string, []any) { |
| 149 | hold := make([]string, len(ids)) |
| 150 | args := make([]any, len(ids)) |
| 151 | for i, id := range ids { |
| 152 | hold[i] = "?" |
| 153 | args[i] = id |
| 154 | } |
| 155 | return strings.Join(hold, ","), args |
| 156 | } |