|
| 1 | +package comms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "bridgerton.audius.co/trashid" |
| 8 | + "github.com/jackc/pgx/v5" |
| 9 | +) |
| 10 | + |
| 11 | +// Result struct to hold chat_id and to_user_id |
| 12 | +type ChatBlastResult struct { |
| 13 | + ChatID string `db:"chat_id"` |
| 14 | + ToUserID int32 `db:"to_user_id"` |
| 15 | +} |
| 16 | + |
| 17 | +type OutgoingChatMessage struct { |
| 18 | + ChatMessageRPC ChatMessageRPC `json:"chat_message_rpc"` |
| 19 | +} |
| 20 | + |
| 21 | +func chatBlast(tx pgx.Tx, ctx context.Context, userId int32, ts time.Time, params ChatBlastRPCParams) ([]OutgoingChatMessage, error) { |
| 22 | + var audienceContentID *int |
| 23 | + if params.AudienceContentID != nil { |
| 24 | + id, _ := trashid.DecodeHashId(*params.AudienceContentID) |
| 25 | + audienceContentID = &id |
| 26 | + } |
| 27 | + |
| 28 | + // insert params.Message into chat_blast table |
| 29 | + _, err := tx.Exec(ctx, ` |
| 30 | + insert into chat_blast |
| 31 | + (blast_id, from_user_id, audience, audience_content_type, audience_content_id, plaintext, created_at) |
| 32 | + values |
| 33 | + ($1, $2, $3, $4, $5, $6, $7) |
| 34 | + on conflict (blast_id) |
| 35 | + do nothing |
| 36 | + `, params.BlastID, userId, params.Audience, params.AudienceContentType, audienceContentID, params.Message, ts) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + // fan out messages to existing threads |
| 42 | + // see also: similar but subtly different inverse query in `getNewBlasts helper in chat.go` |
| 43 | + var results []ChatBlastResult |
| 44 | + |
| 45 | + fanOutSql := ` |
| 46 | + WITH targ AS ( |
| 47 | + SELECT |
| 48 | + blast_id, |
| 49 | + from_user_id, |
| 50 | + to_user_id, |
| 51 | + member_b.chat_id |
| 52 | + FROM chat_blast |
| 53 | + JOIN chat_blast_audience(chat_blast.blast_id) USING (blast_id) |
| 54 | + LEFT JOIN chat_member member_a on from_user_id = member_a.user_id |
| 55 | + LEFT JOIN chat_member member_b on to_user_id = member_b.user_id and member_b.chat_id = member_a.chat_id |
| 56 | + WHERE blast_id = $1 |
| 57 | + AND member_b.chat_id IS NOT NULL |
| 58 | + AND chat_allowed(from_user_id, to_user_id) |
| 59 | + ), |
| 60 | + insert_message AS ( |
| 61 | + INSERT INTO chat_message |
| 62 | + (message_id, chat_id, user_id, created_at, blast_id) |
| 63 | + SELECT |
| 64 | + blast_id || targ.chat_id, -- this ordering needs to match Misc.BlastMessageID |
| 65 | + targ.chat_id, |
| 66 | + targ.from_user_id, |
| 67 | + $2, |
| 68 | + blast_id |
| 69 | + FROM targ |
| 70 | + ON conflict do nothing |
| 71 | + ) |
| 72 | + SELECT chat_id FROM targ; |
| 73 | + ` |
| 74 | + |
| 75 | + rows, err := tx.Query(ctx, fanOutSql, params.BlastID, ts) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + defer rows.Close() |
| 80 | + |
| 81 | + // Scan the results into the results slice |
| 82 | + results, err = pgx.CollectRows(rows, func(row pgx.CollectableRow) (ChatBlastResult, error) { |
| 83 | + var result ChatBlastResult |
| 84 | + err := row.Scan(&result.ChatID, &result.ToUserID) |
| 85 | + return result, err |
| 86 | + }) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + // Formulate chat rpc messages for recipients who have an existing chat with sender |
| 92 | + var outgoingMessages []OutgoingChatMessage |
| 93 | + for _, result := range results { |
| 94 | + messageID := trashid.BlastMessageID(params.BlastID, result.ChatID) |
| 95 | + |
| 96 | + isPlaintext := true |
| 97 | + outgoingMessages = append(outgoingMessages, OutgoingChatMessage{ |
| 98 | + ChatMessageRPC: ChatMessageRPC{ |
| 99 | + Method: MethodChatMessage, |
| 100 | + Params: ChatMessageRPCParams{ |
| 101 | + ChatID: result.ChatID, |
| 102 | + Message: params.Message, |
| 103 | + MessageID: messageID, |
| 104 | + IsPlaintext: &isPlaintext, |
| 105 | + Audience: ¶ms.Audience, |
| 106 | + }}}) |
| 107 | + |
| 108 | + if err := chatUpdateLatestFields(tx, ctx, result.ChatID); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return outgoingMessages, nil |
| 114 | +} |
0 commit comments