forked from tangled.org/core
this repo has no description

appview/models: move db.Follow into models

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li 451ebd4a fde77b9f

verified
Changed files
+98 -85
appview
+26 -57
appview/db/follow.go
···
"log"
"strings"
"time"
+
+
"tangled.org/core/appview/models"
)
-
type Follow struct {
-
UserDid string
-
SubjectDid string
-
FollowedAt time.Time
-
Rkey string
-
}
-
-
func AddFollow(e Execer, follow *Follow) error {
+
func AddFollow(e Execer, follow *models.Follow) error {
query := `insert or ignore into follows (user_did, subject_did, rkey) values (?, ?, ?)`
_, err := e.Exec(query, follow.UserDid, follow.SubjectDid, follow.Rkey)
return err
}
// Get a follow record
-
func GetFollow(e Execer, userDid, subjectDid string) (*Follow, error) {
+
func GetFollow(e Execer, userDid, subjectDid string) (*models.Follow, error) {
query := `select user_did, subject_did, followed_at, rkey from follows where user_did = ? and subject_did = ?`
row := e.QueryRow(query, userDid, subjectDid)
-
var follow Follow
+
var follow models.Follow
var followedAt string
err := row.Scan(&follow.UserDid, &follow.SubjectDid, &followedAt, &follow.Rkey)
if err != nil {
···
return err
}
-
type FollowStats struct {
-
Followers int64
-
Following int64
-
}
-
-
func GetFollowerFollowingCount(e Execer, did string) (FollowStats, error) {
+
func GetFollowerFollowingCount(e Execer, did string) (models.FollowStats, error) {
var followers, following int64
err := e.QueryRow(
`SELECT
···
COUNT(CASE WHEN user_did = ? THEN 1 END) AS following
FROM follows;`, did, did).Scan(&followers, &following)
if err != nil {
-
return FollowStats{}, err
+
return models.FollowStats{}, err
}
-
return FollowStats{
+
return models.FollowStats{
Followers: followers,
Following: following,
}, nil
}
-
func GetFollowerFollowingCounts(e Execer, dids []string) (map[string]FollowStats, error) {
+
func GetFollowerFollowingCounts(e Execer, dids []string) (map[string]models.FollowStats, error) {
if len(dids) == 0 {
return nil, nil
}
···
) g on f.did = g.did`,
placeholderStr, placeholderStr)
-
result := make(map[string]FollowStats)
+
result := make(map[string]models.FollowStats)
rows, err := e.Query(query, args...)
if err != nil {
···
if err := rows.Scan(&did, &followers, &following); err != nil {
return nil, err
}
-
result[did] = FollowStats{
+
result[did] = models.FollowStats{
Followers: followers,
Following: following,
}
···
for _, did := range dids {
if _, exists := result[did]; !exists {
-
result[did] = FollowStats{
+
result[did] = models.FollowStats{
Followers: 0,
Following: 0,
}
···
return result, nil
}
-
func GetFollows(e Execer, limit int, filters ...filter) ([]Follow, error) {
-
var follows []Follow
+
func GetFollows(e Execer, limit int, filters ...filter) ([]models.Follow, error) {
+
var follows []models.Follow
var conditions []string
var args []any
···
return nil, err
}
for rows.Next() {
-
var follow Follow
+
var follow models.Follow
var followedAt string
err := rows.Scan(
&follow.UserDid,
···
return follows, nil
}
-
func GetFollowers(e Execer, did string) ([]Follow, error) {
+
func GetFollowers(e Execer, did string) ([]models.Follow, error) {
return GetFollows(e, 0, FilterEq("subject_did", did))
}
-
func GetFollowing(e Execer, did string) ([]Follow, error) {
+
func GetFollowing(e Execer, did string) ([]models.Follow, error) {
return GetFollows(e, 0, FilterEq("user_did", did))
}
-
type FollowStatus int
-
-
const (
-
IsNotFollowing FollowStatus = iota
-
IsFollowing
-
IsSelf
-
)
-
-
func (s FollowStatus) String() string {
-
switch s {
-
case IsNotFollowing:
-
return "IsNotFollowing"
-
case IsFollowing:
-
return "IsFollowing"
-
case IsSelf:
-
return "IsSelf"
-
default:
-
return "IsNotFollowing"
-
}
-
}
-
-
func getFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]FollowStatus, error) {
+
func getFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]models.FollowStatus, error) {
if len(subjectDids) == 0 || userDid == "" {
-
return make(map[string]FollowStatus), nil
+
return make(map[string]models.FollowStatus), nil
}
-
result := make(map[string]FollowStatus)
+
result := make(map[string]models.FollowStatus)
for _, subjectDid := range subjectDids {
if userDid == subjectDid {
-
result[subjectDid] = IsSelf
+
result[subjectDid] = models.IsSelf
} else {
-
result[subjectDid] = IsNotFollowing
+
result[subjectDid] = models.IsNotFollowing
}
}
···
if err := rows.Scan(&subjectDid); err != nil {
return nil, err
}
-
result[subjectDid] = IsFollowing
+
result[subjectDid] = models.IsFollowing
}
return result, nil
}
-
func GetFollowStatus(e Execer, userDid, subjectDid string) FollowStatus {
+
func GetFollowStatus(e Execer, userDid, subjectDid string) models.FollowStatus {
statuses, err := getFollowStatuses(e, userDid, []string{subjectDid})
if err != nil {
-
return IsNotFollowing
+
return models.IsNotFollowing
}
return statuses[subjectDid]
}
-
func GetFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]FollowStatus, error) {
+
func GetFollowStatuses(e Execer, userDid string, subjectDids []string) (map[string]models.FollowStatus, error) {
return getFollowStatuses(e, userDid, subjectDids)
}
+6 -5
appview/db/timeline.go
···
"time"
"github.com/bluesky-social/indigo/atproto/syntax"
+
"tangled.org/core/appview/models"
)
type TimelineEvent struct {
*Repo
-
*Follow
+
*models.Follow
*Star
EventAt time.Time
···
// optional: populate only if event is Follow
*Profile
-
*FollowStats
-
*FollowStatus
+
*models.FollowStats
+
*models.FollowStatus
// optional: populate only if event is Repo
IsStarred bool
···
return nil, err
}
-
var followStatuses map[string]FollowStatus
+
var followStatuses map[string]models.FollowStatus
if loggedInUserDid != "" {
followStatuses, err = GetFollowStatuses(e, loggedInUserDid, subjects)
if err != nil {
···
profile, _ := profiles[f.SubjectDid]
followStatMap, _ := followStatMap[f.SubjectDid]
-
followStatus := IsNotFollowing
+
followStatus := models.IsNotFollowing
if followStatuses != nil {
followStatus = followStatuses[f.SubjectDid]
}
+1 -1
appview/ingester.go
···
return err
}
-
err = db.AddFollow(i.Db, &db.Follow{
+
err = db.AddFollow(i.Db, &models.Follow{
UserDid: did,
SubjectDid: record.Subject,
Rkey: e.Commit.RKey,
+38
appview/models/follow.go
···
+
package models
+
+
import (
+
"time"
+
)
+
+
type Follow struct {
+
UserDid string
+
SubjectDid string
+
FollowedAt time.Time
+
Rkey string
+
}
+
+
type FollowStats struct {
+
Followers int64
+
Following int64
+
}
+
+
type FollowStatus int
+
+
const (
+
IsNotFollowing FollowStatus = iota
+
IsFollowing
+
IsSelf
+
)
+
+
func (s FollowStatus) String() string {
+
switch s {
+
case IsNotFollowing:
+
return "IsNotFollowing"
+
case IsFollowing:
+
return "IsFollowing"
+
case IsSelf:
+
return "IsSelf"
+
default:
+
return "IsNotFollowing"
+
}
+
}
+3 -2
appview/notify/merged_notifier.go
···
"context"
"tangled.org/core/appview/db"
+
"tangled.org/core/appview/models"
)
type mergedNotifier struct {
···
}
}
-
func (m *mergedNotifier) NewFollow(ctx context.Context, follow *db.Follow) {
+
func (m *mergedNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
for _, notifier := range m.notifiers {
notifier.NewFollow(ctx, follow)
}
}
-
func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {
+
func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
for _, notifier := range m.notifiers {
notifier.DeleteFollow(ctx, follow)
}
+5 -4
appview/notify/notifier.go
···
"context"
"tangled.org/core/appview/db"
+
"tangled.org/core/appview/models"
)
type Notifier interface {
···
NewIssue(ctx context.Context, issue *db.Issue)
-
NewFollow(ctx context.Context, follow *db.Follow)
-
DeleteFollow(ctx context.Context, follow *db.Follow)
+
NewFollow(ctx context.Context, follow *models.Follow)
+
DeleteFollow(ctx context.Context, follow *models.Follow)
NewPull(ctx context.Context, pull *db.Pull)
NewPullComment(ctx context.Context, comment *db.PullComment)
···
func (m *BaseNotifier) NewIssue(ctx context.Context, issue *db.Issue) {}
-
func (m *BaseNotifier) NewFollow(ctx context.Context, follow *db.Follow) {}
-
func (m *BaseNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {}
+
func (m *BaseNotifier) NewFollow(ctx context.Context, follow *models.Follow) {}
+
func (m *BaseNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {}
func (m *BaseNotifier) NewPull(ctx context.Context, pull *db.Pull) {}
func (m *BaseNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) {}
+3 -3
appview/pages/pages.go
···
type ProfileCard struct {
UserDid string
UserHandle string
-
FollowStatus db.FollowStatus
+
FollowStatus models.FollowStatus
Punchcard *db.Punchcard
Profile *db.Profile
Stats ProfileStats
···
type FollowCard struct {
UserDid string
-
FollowStatus db.FollowStatus
+
FollowStatus models.FollowStatus
FollowersCount int64
FollowingCount int64
Profile *db.Profile
···
type FollowFragmentParams struct {
UserDid string
-
FollowStatus db.FollowStatus
+
FollowStatus models.FollowStatus
}
func (p *Pages) FollowFragment(w io.Writer, params FollowFragmentParams) error {
+3 -2
appview/posthog/notifier.go
···
"github.com/posthog/posthog-go"
"tangled.org/core/appview/db"
+
"tangled.org/core/appview/models"
"tangled.org/core/appview/notify"
)
···
}
}
-
func (n *posthogNotifier) NewFollow(ctx context.Context, follow *db.Follow) {
+
func (n *posthogNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
err := n.client.Enqueue(posthog.Capture{
DistinctId: follow.UserDid,
Event: "follow",
···
}
}
-
func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {
+
func (n *posthogNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
err := n.client.Enqueue(posthog.Capture{
DistinctId: follow.UserDid,
Event: "unfollow",
+4 -3
appview/state/follow.go
···
lexutil "github.com/bluesky-social/indigo/lex/util"
"tangled.org/core/api/tangled"
"tangled.org/core/appview/db"
+
"tangled.org/core/appview/models"
"tangled.org/core/appview/pages"
"tangled.org/core/tid"
)
···
log.Println("created atproto record: ", resp.Uri)
-
follow := &db.Follow{
+
follow := &models.Follow{
UserDid: currentUser.Did,
SubjectDid: subjectIdent.DID.String(),
Rkey: rkey,
···
s.pages.FollowFragment(w, pages.FollowFragmentParams{
UserDid: subjectIdent.DID.String(),
-
FollowStatus: db.IsFollowing,
+
FollowStatus: models.IsFollowing,
})
return
···
s.pages.FollowFragment(w, pages.FollowFragmentParams{
UserDid: subjectIdent.DID.String(),
-
FollowStatus: db.IsNotFollowing,
+
FollowStatus: models.IsNotFollowing,
})
s.notifier.DeleteFollow(r.Context(), follow)
+9 -8
appview/state/profile.go
···
"github.com/gorilla/feeds"
"tangled.org/core/api/tangled"
"tangled.org/core/appview/db"
+
"tangled.org/core/appview/models"
"tangled.org/core/appview/pages"
)
···
}
loggedInUser := s.oauth.GetUser(r)
-
followStatus := db.IsNotFollowing
+
followStatus := models.IsNotFollowing
if loggedInUser != nil {
followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did)
}
···
func (s *State) followPage(
r *http.Request,
-
fetchFollows func(db.Execer, string) ([]db.Follow, error),
-
extractDid func(db.Follow) string,
+
fetchFollows func(db.Execer, string) ([]models.Follow, error),
+
extractDid func(models.Follow) string,
) (*FollowsPageParams, error) {
l := s.logger.With("handler", "reposPage")
···
followCards := make([]pages.FollowCard, len(follows))
for i, did := range followDids {
followStats := followStatsMap[did]
-
followStatus := db.IsNotFollowing
+
followStatus := models.IsNotFollowing
if _, exists := loggedInUserFollowing[did]; exists {
-
followStatus = db.IsFollowing
+
followStatus = models.IsFollowing
} else if loggedInUser != nil && loggedInUser.Did == did {
-
followStatus = db.IsSelf
+
followStatus = models.IsSelf
}
var profile *db.Profile
···
}
func (s *State) followersPage(w http.ResponseWriter, r *http.Request) {
-
followPage, err := s.followPage(r, db.GetFollowers, func(f db.Follow) string { return f.UserDid })
+
followPage, err := s.followPage(r, db.GetFollowers, func(f models.Follow) string { return f.UserDid })
if err != nil {
s.pages.Notice(w, "all-followers", "Failed to load followers")
return
···
}
func (s *State) followingPage(w http.ResponseWriter, r *http.Request) {
-
followPage, err := s.followPage(r, db.GetFollowing, func(f db.Follow) string { return f.SubjectDid })
+
followPage, err := s.followPage(r, db.GetFollowing, func(f models.Follow) string { return f.SubjectDid })
if err != nil {
s.pages.Notice(w, "all-following", "Failed to load following")
return