| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: feed.sql |
| 5 | |
| 6 | package socialdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const insertTrendingSnapshot = `-- name: InsertTrendingSnapshot :one |
| 15 | INSERT INTO trending_snapshots (scope, kind, payload) |
| 16 | VALUES ($1, $2, $3) |
| 17 | RETURNING id, scope, kind, captured_at, payload |
| 18 | ` |
| 19 | |
| 20 | type InsertTrendingSnapshotParams struct { |
| 21 | Scope TrendingScope |
| 22 | Kind TrendingKind |
| 23 | Payload []byte |
| 24 | } |
| 25 | |
| 26 | func (q *Queries) InsertTrendingSnapshot(ctx context.Context, db DBTX, arg InsertTrendingSnapshotParams) (TrendingSnapshot, error) { |
| 27 | row := db.QueryRow(ctx, insertTrendingSnapshot, arg.Scope, arg.Kind, arg.Payload) |
| 28 | var i TrendingSnapshot |
| 29 | err := row.Scan( |
| 30 | &i.ID, |
| 31 | &i.Scope, |
| 32 | &i.Kind, |
| 33 | &i.CapturedAt, |
| 34 | &i.Payload, |
| 35 | ) |
| 36 | return i, err |
| 37 | } |
| 38 | |
| 39 | const latestTrendingSnapshot = `-- name: LatestTrendingSnapshot :one |
| 40 | SELECT id, scope, kind, captured_at, payload |
| 41 | FROM trending_snapshots |
| 42 | WHERE scope = $1 |
| 43 | AND kind = $2 |
| 44 | ORDER BY captured_at DESC |
| 45 | LIMIT 1 |
| 46 | ` |
| 47 | |
| 48 | type LatestTrendingSnapshotParams struct { |
| 49 | Scope TrendingScope |
| 50 | Kind TrendingKind |
| 51 | } |
| 52 | |
| 53 | func (q *Queries) LatestTrendingSnapshot(ctx context.Context, db DBTX, arg LatestTrendingSnapshotParams) (TrendingSnapshot, error) { |
| 54 | row := db.QueryRow(ctx, latestTrendingSnapshot, arg.Scope, arg.Kind) |
| 55 | var i TrendingSnapshot |
| 56 | err := row.Scan( |
| 57 | &i.ID, |
| 58 | &i.Scope, |
| 59 | &i.Kind, |
| 60 | &i.CapturedAt, |
| 61 | &i.Payload, |
| 62 | ) |
| 63 | return i, err |
| 64 | } |
| 65 | |
| 66 | const listDashboardFeedEvents = `-- name: ListDashboardFeedEvents :many |
| 67 | |
| 68 | SELECT |
| 69 | de.id, de.actor_user_id, de.kind, de.repo_id, de.source_kind, |
| 70 | de.source_id, de.public, de.payload, de.created_at, |
| 71 | actor.username AS actor_username, |
| 72 | actor.display_name AS actor_display_name, |
| 73 | COALESCE(r.name::text, '')::text AS repo_name, |
| 74 | COALESCE(r.description, '') AS repo_description, |
| 75 | COALESCE(r.primary_language, '') AS repo_primary_language, |
| 76 | COALESCE(r.star_count, 0)::bigint AS repo_star_count, |
| 77 | COALESCE(r.fork_count, 0)::bigint AS repo_fork_count, |
| 78 | COALESCE(owner_user.username::text, owner_org.slug::text, '')::text AS repo_owner, |
| 79 | COALESCE(source_user.username::text, source_org.slug::text, '')::text AS source_name |
| 80 | FROM domain_events de |
| 81 | JOIN users actor ON actor.id = de.actor_user_id |
| 82 | LEFT JOIN repos r ON r.id = de.repo_id AND r.deleted_at IS NULL |
| 83 | LEFT JOIN users owner_user ON owner_user.id = r.owner_user_id |
| 84 | LEFT JOIN orgs owner_org ON owner_org.id = r.owner_org_id |
| 85 | LEFT JOIN users source_user ON de.source_kind = 'user' AND source_user.id = de.source_id |
| 86 | LEFT JOIN orgs source_org ON de.source_kind = 'org' AND source_org.id = de.source_id |
| 87 | WHERE de.public = true |
| 88 | AND actor.suspended_at IS NULL |
| 89 | AND actor.deleted_at IS NULL |
| 90 | AND ( |
| 91 | de.repo_id IS NULL |
| 92 | OR (r.id IS NOT NULL AND r.visibility = 'public') |
| 93 | ) |
| 94 | AND ( |
| 95 | de.actor_user_id = $1::bigint |
| 96 | OR de.actor_user_id IN ( |
| 97 | SELECT followee_user_id FROM follows |
| 98 | WHERE follower_user_id = $1::bigint |
| 99 | AND followee_user_id IS NOT NULL |
| 100 | ) |
| 101 | OR de.repo_id IN ( |
| 102 | SELECT repo_id FROM watches |
| 103 | WHERE user_id = $1::bigint |
| 104 | AND level <> 'ignore' |
| 105 | ) |
| 106 | OR ( |
| 107 | r.owner_org_id IN ( |
| 108 | SELECT followee_org_id FROM follows |
| 109 | WHERE follower_user_id = $1::bigint |
| 110 | AND followee_org_id IS NOT NULL |
| 111 | ) |
| 112 | ) |
| 113 | OR ( |
| 114 | de.source_kind = 'org' |
| 115 | AND de.source_id IN ( |
| 116 | SELECT followee_org_id FROM follows |
| 117 | WHERE follower_user_id = $1::bigint |
| 118 | AND followee_org_id IS NOT NULL |
| 119 | ) |
| 120 | ) |
| 121 | ) |
| 122 | AND ( |
| 123 | $2::timestamptz IS NULL |
| 124 | OR (de.created_at, de.id) < ( |
| 125 | $2::timestamptz, |
| 126 | $3::bigint |
| 127 | ) |
| 128 | ) |
| 129 | ORDER BY de.created_at DESC, de.id DESC |
| 130 | LIMIT $4::int |
| 131 | ` |
| 132 | |
| 133 | type ListDashboardFeedEventsParams struct { |
| 134 | ViewerUserID int64 |
| 135 | BeforeCreatedAt pgtype.Timestamptz |
| 136 | BeforeID pgtype.Int8 |
| 137 | LimitCount int32 |
| 138 | } |
| 139 | |
| 140 | type ListDashboardFeedEventsRow struct { |
| 141 | ID int64 |
| 142 | ActorUserID pgtype.Int8 |
| 143 | Kind string |
| 144 | RepoID pgtype.Int8 |
| 145 | SourceKind string |
| 146 | SourceID int64 |
| 147 | Public bool |
| 148 | Payload []byte |
| 149 | CreatedAt pgtype.Timestamptz |
| 150 | ActorUsername string |
| 151 | ActorDisplayName string |
| 152 | RepoName string |
| 153 | RepoDescription string |
| 154 | RepoPrimaryLanguage string |
| 155 | RepoStarCount int64 |
| 156 | RepoForkCount int64 |
| 157 | RepoOwner string |
| 158 | SourceName string |
| 159 | } |
| 160 | |
| 161 | // ─── activity feed / trending ───────────────────────────────────── |
| 162 | func (q *Queries) ListDashboardFeedEvents(ctx context.Context, db DBTX, arg ListDashboardFeedEventsParams) ([]ListDashboardFeedEventsRow, error) { |
| 163 | rows, err := db.Query(ctx, listDashboardFeedEvents, |
| 164 | arg.ViewerUserID, |
| 165 | arg.BeforeCreatedAt, |
| 166 | arg.BeforeID, |
| 167 | arg.LimitCount, |
| 168 | ) |
| 169 | if err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | defer rows.Close() |
| 173 | items := []ListDashboardFeedEventsRow{} |
| 174 | for rows.Next() { |
| 175 | var i ListDashboardFeedEventsRow |
| 176 | if err := rows.Scan( |
| 177 | &i.ID, |
| 178 | &i.ActorUserID, |
| 179 | &i.Kind, |
| 180 | &i.RepoID, |
| 181 | &i.SourceKind, |
| 182 | &i.SourceID, |
| 183 | &i.Public, |
| 184 | &i.Payload, |
| 185 | &i.CreatedAt, |
| 186 | &i.ActorUsername, |
| 187 | &i.ActorDisplayName, |
| 188 | &i.RepoName, |
| 189 | &i.RepoDescription, |
| 190 | &i.RepoPrimaryLanguage, |
| 191 | &i.RepoStarCount, |
| 192 | &i.RepoForkCount, |
| 193 | &i.RepoOwner, |
| 194 | &i.SourceName, |
| 195 | ); err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | items = append(items, i) |
| 199 | } |
| 200 | if err := rows.Err(); err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | return items, nil |
| 204 | } |
| 205 | |
| 206 | const listDashboardReposForUser = `-- name: ListDashboardReposForUser :many |
| 207 | SELECT |
| 208 | r.id AS repo_id, |
| 209 | r.name::text AS name, |
| 210 | r.description, |
| 211 | r.visibility, |
| 212 | COALESCE(r.primary_language, '') AS primary_language, |
| 213 | r.star_count, |
| 214 | r.fork_count, |
| 215 | r.updated_at |
| 216 | FROM repos r |
| 217 | WHERE r.owner_user_id = $1 |
| 218 | AND r.deleted_at IS NULL |
| 219 | ORDER BY r.updated_at DESC |
| 220 | LIMIT $2 |
| 221 | ` |
| 222 | |
| 223 | type ListDashboardReposForUserParams struct { |
| 224 | OwnerUserID pgtype.Int8 |
| 225 | Limit int32 |
| 226 | } |
| 227 | |
| 228 | type ListDashboardReposForUserRow struct { |
| 229 | RepoID int64 |
| 230 | Name string |
| 231 | Description string |
| 232 | Visibility RepoVisibility |
| 233 | PrimaryLanguage string |
| 234 | StarCount int64 |
| 235 | ForkCount int64 |
| 236 | UpdatedAt pgtype.Timestamptz |
| 237 | } |
| 238 | |
| 239 | func (q *Queries) ListDashboardReposForUser(ctx context.Context, db DBTX, arg ListDashboardReposForUserParams) ([]ListDashboardReposForUserRow, error) { |
| 240 | rows, err := db.Query(ctx, listDashboardReposForUser, arg.OwnerUserID, arg.Limit) |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | defer rows.Close() |
| 245 | items := []ListDashboardReposForUserRow{} |
| 246 | for rows.Next() { |
| 247 | var i ListDashboardReposForUserRow |
| 248 | if err := rows.Scan( |
| 249 | &i.RepoID, |
| 250 | &i.Name, |
| 251 | &i.Description, |
| 252 | &i.Visibility, |
| 253 | &i.PrimaryLanguage, |
| 254 | &i.StarCount, |
| 255 | &i.ForkCount, |
| 256 | &i.UpdatedAt, |
| 257 | ); err != nil { |
| 258 | return nil, err |
| 259 | } |
| 260 | items = append(items, i) |
| 261 | } |
| 262 | if err := rows.Err(); err != nil { |
| 263 | return nil, err |
| 264 | } |
| 265 | return items, nil |
| 266 | } |
| 267 | |
| 268 | const listPublicFeedEvents = `-- name: ListPublicFeedEvents :many |
| 269 | SELECT |
| 270 | de.id, de.actor_user_id, de.kind, de.repo_id, de.source_kind, |
| 271 | de.source_id, de.public, de.payload, de.created_at, |
| 272 | actor.username AS actor_username, |
| 273 | actor.display_name AS actor_display_name, |
| 274 | COALESCE(r.name::text, '')::text AS repo_name, |
| 275 | COALESCE(r.description, '') AS repo_description, |
| 276 | COALESCE(r.primary_language, '') AS repo_primary_language, |
| 277 | COALESCE(r.star_count, 0)::bigint AS repo_star_count, |
| 278 | COALESCE(r.fork_count, 0)::bigint AS repo_fork_count, |
| 279 | COALESCE(owner_user.username::text, owner_org.slug::text, '')::text AS repo_owner, |
| 280 | COALESCE(source_user.username::text, source_org.slug::text, '')::text AS source_name |
| 281 | FROM domain_events de |
| 282 | JOIN users actor ON actor.id = de.actor_user_id |
| 283 | LEFT JOIN repos r ON r.id = de.repo_id AND r.deleted_at IS NULL |
| 284 | LEFT JOIN users owner_user ON owner_user.id = r.owner_user_id |
| 285 | LEFT JOIN orgs owner_org ON owner_org.id = r.owner_org_id |
| 286 | LEFT JOIN users source_user ON de.source_kind = 'user' AND source_user.id = de.source_id |
| 287 | LEFT JOIN orgs source_org ON de.source_kind = 'org' AND source_org.id = de.source_id |
| 288 | WHERE de.public = true |
| 289 | AND actor.suspended_at IS NULL |
| 290 | AND actor.deleted_at IS NULL |
| 291 | AND ( |
| 292 | de.repo_id IS NULL |
| 293 | OR (r.id IS NOT NULL AND r.visibility = 'public') |
| 294 | ) |
| 295 | AND ( |
| 296 | $1::timestamptz IS NULL |
| 297 | OR (de.created_at, de.id) < ( |
| 298 | $1::timestamptz, |
| 299 | $2::bigint |
| 300 | ) |
| 301 | ) |
| 302 | ORDER BY de.created_at DESC, de.id DESC |
| 303 | LIMIT $3::int |
| 304 | ` |
| 305 | |
| 306 | type ListPublicFeedEventsParams struct { |
| 307 | BeforeCreatedAt pgtype.Timestamptz |
| 308 | BeforeID pgtype.Int8 |
| 309 | LimitCount int32 |
| 310 | } |
| 311 | |
| 312 | type ListPublicFeedEventsRow struct { |
| 313 | ID int64 |
| 314 | ActorUserID pgtype.Int8 |
| 315 | Kind string |
| 316 | RepoID pgtype.Int8 |
| 317 | SourceKind string |
| 318 | SourceID int64 |
| 319 | Public bool |
| 320 | Payload []byte |
| 321 | CreatedAt pgtype.Timestamptz |
| 322 | ActorUsername string |
| 323 | ActorDisplayName string |
| 324 | RepoName string |
| 325 | RepoDescription string |
| 326 | RepoPrimaryLanguage string |
| 327 | RepoStarCount int64 |
| 328 | RepoForkCount int64 |
| 329 | RepoOwner string |
| 330 | SourceName string |
| 331 | } |
| 332 | |
| 333 | func (q *Queries) ListPublicFeedEvents(ctx context.Context, db DBTX, arg ListPublicFeedEventsParams) ([]ListPublicFeedEventsRow, error) { |
| 334 | rows, err := db.Query(ctx, listPublicFeedEvents, arg.BeforeCreatedAt, arg.BeforeID, arg.LimitCount) |
| 335 | if err != nil { |
| 336 | return nil, err |
| 337 | } |
| 338 | defer rows.Close() |
| 339 | items := []ListPublicFeedEventsRow{} |
| 340 | for rows.Next() { |
| 341 | var i ListPublicFeedEventsRow |
| 342 | if err := rows.Scan( |
| 343 | &i.ID, |
| 344 | &i.ActorUserID, |
| 345 | &i.Kind, |
| 346 | &i.RepoID, |
| 347 | &i.SourceKind, |
| 348 | &i.SourceID, |
| 349 | &i.Public, |
| 350 | &i.Payload, |
| 351 | &i.CreatedAt, |
| 352 | &i.ActorUsername, |
| 353 | &i.ActorDisplayName, |
| 354 | &i.RepoName, |
| 355 | &i.RepoDescription, |
| 356 | &i.RepoPrimaryLanguage, |
| 357 | &i.RepoStarCount, |
| 358 | &i.RepoForkCount, |
| 359 | &i.RepoOwner, |
| 360 | &i.SourceName, |
| 361 | ); err != nil { |
| 362 | return nil, err |
| 363 | } |
| 364 | items = append(items, i) |
| 365 | } |
| 366 | if err := rows.Err(); err != nil { |
| 367 | return nil, err |
| 368 | } |
| 369 | return items, nil |
| 370 | } |
| 371 | |
| 372 | const listTrendingRepos = `-- name: ListTrendingRepos :many |
| 373 | WITH recent AS ( |
| 374 | SELECT |
| 375 | repo_id, |
| 376 | ( |
| 377 | COUNT(*) FILTER (WHERE kind = 'star') * 3 |
| 378 | + COUNT(*) FILTER (WHERE kind = 'forked') * 2 |
| 379 | + COUNT(DISTINCT actor_user_id) FILTER (WHERE kind = 'push') |
| 380 | )::bigint AS score |
| 381 | FROM domain_events |
| 382 | WHERE public = true |
| 383 | AND repo_id IS NOT NULL |
| 384 | AND created_at >= now() - make_interval(days => $2::int) |
| 385 | GROUP BY repo_id |
| 386 | ) |
| 387 | SELECT |
| 388 | r.id AS repo_id, |
| 389 | COALESCE(owner_user.username::text, owner_org.slug::text, '')::text AS owner, |
| 390 | r.name::text AS name, |
| 391 | r.description, |
| 392 | COALESCE(r.primary_language, '') AS primary_language, |
| 393 | r.star_count, |
| 394 | r.fork_count, |
| 395 | COALESCE(recent.score, 0)::bigint AS score, |
| 396 | r.updated_at |
| 397 | FROM repos r |
| 398 | LEFT JOIN recent ON recent.repo_id = r.id |
| 399 | LEFT JOIN users owner_user ON owner_user.id = r.owner_user_id |
| 400 | LEFT JOIN orgs owner_org ON owner_org.id = r.owner_org_id |
| 401 | WHERE r.visibility = 'public' |
| 402 | AND r.deleted_at IS NULL |
| 403 | AND r.is_archived = false |
| 404 | ORDER BY COALESCE(recent.score, 0) DESC, r.star_count DESC, r.updated_at DESC |
| 405 | LIMIT $1::int |
| 406 | ` |
| 407 | |
| 408 | type ListTrendingReposParams struct { |
| 409 | LimitCount int32 |
| 410 | WindowDays int32 |
| 411 | } |
| 412 | |
| 413 | type ListTrendingReposRow struct { |
| 414 | RepoID int64 |
| 415 | Owner string |
| 416 | Name string |
| 417 | Description string |
| 418 | PrimaryLanguage string |
| 419 | StarCount int64 |
| 420 | ForkCount int64 |
| 421 | Score int64 |
| 422 | UpdatedAt pgtype.Timestamptz |
| 423 | } |
| 424 | |
| 425 | func (q *Queries) ListTrendingRepos(ctx context.Context, db DBTX, arg ListTrendingReposParams) ([]ListTrendingReposRow, error) { |
| 426 | rows, err := db.Query(ctx, listTrendingRepos, arg.LimitCount, arg.WindowDays) |
| 427 | if err != nil { |
| 428 | return nil, err |
| 429 | } |
| 430 | defer rows.Close() |
| 431 | items := []ListTrendingReposRow{} |
| 432 | for rows.Next() { |
| 433 | var i ListTrendingReposRow |
| 434 | if err := rows.Scan( |
| 435 | &i.RepoID, |
| 436 | &i.Owner, |
| 437 | &i.Name, |
| 438 | &i.Description, |
| 439 | &i.PrimaryLanguage, |
| 440 | &i.StarCount, |
| 441 | &i.ForkCount, |
| 442 | &i.Score, |
| 443 | &i.UpdatedAt, |
| 444 | ); err != nil { |
| 445 | return nil, err |
| 446 | } |
| 447 | items = append(items, i) |
| 448 | } |
| 449 | if err := rows.Err(); err != nil { |
| 450 | return nil, err |
| 451 | } |
| 452 | return items, nil |
| 453 | } |
| 454 | |
| 455 | const listTrendingUsers = `-- name: ListTrendingUsers :many |
| 456 | WITH recent_events AS ( |
| 457 | SELECT actor_user_id AS user_id, COUNT(*)::bigint AS event_count |
| 458 | FROM domain_events |
| 459 | WHERE public = true |
| 460 | AND actor_user_id IS NOT NULL |
| 461 | AND created_at >= now() - make_interval(days => $2::int) |
| 462 | GROUP BY actor_user_id |
| 463 | ), |
| 464 | recent_followers AS ( |
| 465 | SELECT followee_user_id AS user_id, COUNT(*)::bigint AS follower_count |
| 466 | FROM follows |
| 467 | WHERE followee_user_id IS NOT NULL |
| 468 | AND followed_at >= now() - make_interval(days => $2::int) |
| 469 | GROUP BY followee_user_id |
| 470 | ) |
| 471 | SELECT |
| 472 | u.id AS user_id, |
| 473 | u.username, |
| 474 | u.display_name, |
| 475 | (COALESCE(recent_followers.follower_count, 0) * 2 + COALESCE(recent_events.event_count, 0))::bigint AS score, |
| 476 | COALESCE(recent_followers.follower_count, 0)::bigint AS follower_delta, |
| 477 | COALESCE(recent_events.event_count, 0)::bigint AS event_count |
| 478 | FROM users u |
| 479 | LEFT JOIN recent_events ON recent_events.user_id = u.id |
| 480 | LEFT JOIN recent_followers ON recent_followers.user_id = u.id |
| 481 | WHERE u.suspended_at IS NULL |
| 482 | AND u.deleted_at IS NULL |
| 483 | AND (COALESCE(recent_followers.follower_count, 0) > 0 OR COALESCE(recent_events.event_count, 0) > 0) |
| 484 | ORDER BY score DESC, u.created_at DESC |
| 485 | LIMIT $1::int |
| 486 | ` |
| 487 | |
| 488 | type ListTrendingUsersParams struct { |
| 489 | LimitCount int32 |
| 490 | WindowDays int32 |
| 491 | } |
| 492 | |
| 493 | type ListTrendingUsersRow struct { |
| 494 | UserID int64 |
| 495 | Username string |
| 496 | DisplayName string |
| 497 | Score int64 |
| 498 | FollowerDelta int64 |
| 499 | EventCount int64 |
| 500 | } |
| 501 | |
| 502 | func (q *Queries) ListTrendingUsers(ctx context.Context, db DBTX, arg ListTrendingUsersParams) ([]ListTrendingUsersRow, error) { |
| 503 | rows, err := db.Query(ctx, listTrendingUsers, arg.LimitCount, arg.WindowDays) |
| 504 | if err != nil { |
| 505 | return nil, err |
| 506 | } |
| 507 | defer rows.Close() |
| 508 | items := []ListTrendingUsersRow{} |
| 509 | for rows.Next() { |
| 510 | var i ListTrendingUsersRow |
| 511 | if err := rows.Scan( |
| 512 | &i.UserID, |
| 513 | &i.Username, |
| 514 | &i.DisplayName, |
| 515 | &i.Score, |
| 516 | &i.FollowerDelta, |
| 517 | &i.EventCount, |
| 518 | ); err != nil { |
| 519 | return nil, err |
| 520 | } |
| 521 | items = append(items, i) |
| 522 | } |
| 523 | if err := rows.Err(); err != nil { |
| 524 | return nil, err |
| 525 | } |
| 526 | return items, nil |
| 527 | } |
| 528 |