This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / appview / db / bsky.go
2.6 kB 120 lines
1package db 2 3import ( 4 "database/sql" 5 "encoding/json" 6 "time" 7 8 "tangled.org/core/appview/models" 9) 10 11func InsertBlueskyPosts(e Execer, posts []models.BskyPost) error { 12 if len(posts) == 0 { 13 return nil 14 } 15 16 stmt, err := e.Prepare(` 17 insert or replace into bluesky_posts (rkey, text, created_at, langs, facets, embed, like_count, reply_count, repost_count, quote_count, author_did) 18 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 19 `) 20 if err != nil { 21 return err 22 } 23 defer stmt.Close() 24 25 for _, post := range posts { 26 var langsJSON, facetsJSON, embedJSON []byte 27 28 if len(post.Langs) > 0 { 29 langsJSON, _ = json.Marshal(post.Langs) 30 } 31 if len(post.Facets) > 0 { 32 facetsJSON = post.Facets // already raw JSON bytes 33 } 34 if post.Embed != nil { 35 embedJSON, _ = json.Marshal(post.Embed) 36 } 37 38 _, err := stmt.Exec( 39 post.Rkey, 40 post.Text, 41 post.CreatedAt.Format(time.RFC3339), 42 nullString(langsJSON), 43 nullString(facetsJSON), 44 nullString(embedJSON), 45 post.LikeCount, 46 post.ReplyCount, 47 post.RepostCount, 48 post.QuoteCount, 49 post.AuthorDid, 50 ) 51 if err != nil { 52 return err 53 } 54 } 55 56 return nil 57} 58 59func nullString(b []byte) any { 60 if len(b) == 0 { 61 return nil 62 } 63 return string(b) 64} 65 66func GetBlueskyPosts(e Execer, limit int) ([]models.BskyPost, error) { 67 query := ` 68 select rkey, text, created_at, langs, facets, embed, like_count, reply_count, repost_count, quote_count, author_did 69 from bluesky_posts 70 order by created_at desc 71 limit ? 72 ` 73 74 rows, err := e.Query(query, limit) 75 if err != nil { 76 return nil, err 77 } 78 defer rows.Close() 79 80 var posts []models.BskyPost 81 for rows.Next() { 82 var rkey, text, createdAt string 83 var langs, facets, embed sql.Null[string] 84 var likeCount, replyCount, repostCount, quoteCount int64 85 var authorDid string 86 87 err := rows.Scan(&rkey, &text, &createdAt, &langs, &facets, &embed, &likeCount, &replyCount, &repostCount, &quoteCount, &authorDid) 88 if err != nil { 89 return nil, err 90 } 91 92 post := models.BskyPost{ 93 Rkey: rkey, 94 Text: text, 95 LikeCount: likeCount, 96 ReplyCount: replyCount, 97 RepostCount: repostCount, 98 QuoteCount: quoteCount, 99 AuthorDid: authorDid, 100 } 101 102 if t, err := time.Parse(time.RFC3339, createdAt); err == nil { 103 post.CreatedAt = t 104 } 105 if langs.Valid && langs.V != "" { 106 json.Unmarshal([]byte(langs.V), &post.Langs) 107 } 108 if facets.Valid && facets.V != "" { 109 json.Unmarshal([]byte(facets.V), &post.Facets) 110 } 111 if embed.Valid && embed.V != "" { 112 post.Embed = new(models.PostEmbed) 113 json.Unmarshal([]byte(embed.V), post.Embed) 114 } 115 116 posts = append(posts, post) 117 } 118 119 return posts, rows.Err() 120}