forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
1package db 2 3import ( 4 "log" 5 "time" 6 7 "github.com/bluesky-social/indigo/atproto/syntax" 8 "tangled.org/core/appview/models" 9) 10 11func AddReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind models.ReactionKind, rkey string) error { 12 query := `insert or ignore into reactions (reacted_by_did, thread_at, kind, rkey) values (?, ?, ?, ?)` 13 _, err := e.Exec(query, reactedByDid, threadAt, kind, rkey) 14 return err 15} 16 17// Get a reaction record 18func GetReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind models.ReactionKind) (*models.Reaction, error) { 19 query := ` 20 select reacted_by_did, thread_at, created, rkey 21 from reactions 22 where reacted_by_did = ? and thread_at = ? and kind = ?` 23 row := e.QueryRow(query, reactedByDid, threadAt, kind) 24 25 var reaction models.Reaction 26 var created string 27 err := row.Scan(&reaction.ReactedByDid, &reaction.ThreadAt, &created, &reaction.Rkey) 28 if err != nil { 29 return nil, err 30 } 31 32 createdAtTime, err := time.Parse(time.RFC3339, created) 33 if err != nil { 34 log.Println("unable to determine followed at time") 35 reaction.Created = time.Now() 36 } else { 37 reaction.Created = createdAtTime 38 } 39 40 return &reaction, nil 41} 42 43// Remove a reaction 44func DeleteReaction(e Execer, reactedByDid string, threadAt syntax.ATURI, kind models.ReactionKind) error { 45 _, err := e.Exec(`delete from reactions where reacted_by_did = ? and thread_at = ? and kind = ?`, reactedByDid, threadAt, kind) 46 return err 47} 48 49// Remove a reaction 50func DeleteReactionByRkey(e Execer, reactedByDid string, rkey string) error { 51 _, err := e.Exec(`delete from reactions where reacted_by_did = ? and rkey = ?`, reactedByDid, rkey) 52 return err 53} 54 55func GetReactionCount(e Execer, threadAt syntax.ATURI, kind models.ReactionKind) (int, error) { 56 count := 0 57 err := e.QueryRow( 58 `select count(reacted_by_did) from reactions where thread_at = ? and kind = ?`, threadAt, kind).Scan(&count) 59 if err != nil { 60 return 0, err 61 } 62 return count, nil 63} 64 65func GetReactionCountMap(e Execer, threadAt syntax.ATURI) (map[models.ReactionKind]int, error) { 66 countMap := map[models.ReactionKind]int{} 67 for _, kind := range models.OrderedReactionKinds { 68 count, err := GetReactionCount(e, threadAt, kind) 69 if err != nil { 70 return map[models.ReactionKind]int{}, nil 71 } 72 countMap[kind] = count 73 } 74 return countMap, nil 75} 76 77func GetReactionStatus(e Execer, userDid string, threadAt syntax.ATURI, kind models.ReactionKind) bool { 78 if _, err := GetReaction(e, userDid, threadAt, kind); err != nil { 79 return false 80 } else { 81 return true 82 } 83} 84 85func GetReactionStatusMap(e Execer, userDid string, threadAt syntax.ATURI) map[models.ReactionKind]bool { 86 statusMap := map[models.ReactionKind]bool{} 87 for _, kind := range models.OrderedReactionKinds { 88 count := GetReactionStatus(e, userDid, threadAt, kind) 89 statusMap[kind] = count 90 } 91 return statusMap 92}