| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package social |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | |
| 8 | "github.com/jackc/pgx/v5/pgtype" |
| 9 | |
| 10 | socialdb "github.com/tenseleyFlow/shithub/internal/social/sqlc" |
| 11 | ) |
| 12 | |
| 13 | // EmitEvent is the canonical seam for inserting a row into |
| 14 | // `domain_events`. Other packages (issues, pulls, repos lifecycle) |
| 15 | // will call this rather than reaching into the sqlc query directly, |
| 16 | // so the event-shape contract has one place to evolve as S29 and |
| 17 | // S33 wire up their consumers. |
| 18 | // |
| 19 | // Pass repoID = 0 for user-scoped events (where there's no repo |
| 20 | // involved). The handler / orchestrator owns the public-flag |
| 21 | // decision: public-repo events should set true; private-repo events |
| 22 | // must set false. |
| 23 | type EmitParams struct { |
| 24 | ActorUserID int64 |
| 25 | Kind string |
| 26 | RepoID int64 // 0 for user-scoped |
| 27 | SourceKind string |
| 28 | SourceID int64 |
| 29 | Public bool |
| 30 | Payload []byte // already-marshaled JSON; pass `[]byte("{}")` for empty |
| 31 | } |
| 32 | |
| 33 | // Emit writes one row to domain_events. Non-fatal at the caller's |
| 34 | // discretion; orchestrators typically log on error rather than |
| 35 | // failing the whole transaction (the in-band action is more |
| 36 | // important than the event log). |
| 37 | func Emit(ctx context.Context, deps Deps, p EmitParams) error { |
| 38 | payload := p.Payload |
| 39 | if len(payload) == 0 { |
| 40 | payload = []byte("{}") |
| 41 | } |
| 42 | _, err := socialdb.New().InsertDomainEvent(ctx, deps.Pool, socialdb.InsertDomainEventParams{ |
| 43 | ActorUserID: pgInt(p.ActorUserID), |
| 44 | Kind: p.Kind, |
| 45 | RepoID: pgInt(p.RepoID), |
| 46 | SourceKind: p.SourceKind, |
| 47 | SourceID: p.SourceID, |
| 48 | Public: p.Public, |
| 49 | Payload: payload, |
| 50 | }) |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | func pgInt(v int64) pgtype.Int8 { |
| 55 | return pgtype.Int8{Int64: v, Valid: v != 0} |
| 56 | } |
| 57 |