chore: refactor http out
This commit is contained in:
305
feed.go
305
feed.go
@@ -18,6 +18,8 @@ import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.sour.is/xt/internal/otel"
|
||||
"go.yarn.social/lextwt"
|
||||
"go.yarn.social/types"
|
||||
@@ -630,3 +632,306 @@ func refreshLastTwt(ctx context.Context, db db) error {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func fetchTwts(ctx context.Context, db db, uri string, limit int, offset int64) ([]types.Twt, int64, int64, error) {
|
||||
ctx, span := otel.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
args := make([]any, 0, 3)
|
||||
where := `where feed_id in (select feed_id from feeds where state != 'permanantly-dead')`
|
||||
|
||||
if uri != "" {
|
||||
feed_id := urlNS.UUID5(uri)
|
||||
where = "where feed_id = ?"
|
||||
args = append(args, feed_id)
|
||||
}
|
||||
|
||||
var end int64
|
||||
err := db.QueryRowContext(ctx, `
|
||||
select count(*) n from twts `+where+``, args...).Scan(&end)
|
||||
span.RecordError(err)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
if offset < 1 {
|
||||
offset += end
|
||||
}
|
||||
|
||||
offset = max(1, offset)
|
||||
|
||||
args = append(args, limit, offset-int64(limit))
|
||||
span.AddEvent("twts", trace.WithAttributes(
|
||||
attribute.Int("limit", limit),
|
||||
attribute.Int64("offset-end", offset),
|
||||
attribute.Int64("offset-start", offset-int64(limit)),
|
||||
attribute.Int64("max", end),
|
||||
))
|
||||
|
||||
qry := `
|
||||
SELECT
|
||||
feed_id,
|
||||
hash,
|
||||
conv,
|
||||
coalesce(nick, 'nobody') nick,
|
||||
coalesce(uri, 'https://empty.txt') uri,
|
||||
text
|
||||
FROM twts
|
||||
join (
|
||||
select feed_id, nick, uri
|
||||
from feeds
|
||||
) using (feed_id)
|
||||
where rowid in (
|
||||
select rowid from twts
|
||||
` + where + `
|
||||
order by ulid asc
|
||||
limit ?
|
||||
offset ?
|
||||
)
|
||||
order by ulid asc`
|
||||
|
||||
fmt.Println(qry, args)
|
||||
rows, err := db.QueryContext(
|
||||
ctx, qry, args...,
|
||||
)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var twts []types.Twt
|
||||
for rows.Next() {
|
||||
var o struct {
|
||||
FeedID string
|
||||
Hash string
|
||||
Conv string
|
||||
Dt string
|
||||
Nick string
|
||||
URI string
|
||||
Text string
|
||||
}
|
||||
err = rows.Scan(&o.FeedID, &o.Hash, &o.Conv, &o.Nick, &o.URI, &o.Text)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
twter := types.NewTwter(o.Nick, o.URI)
|
||||
twt, _ := lextwt.ParseLine(o.Text, &twter)
|
||||
twts = append(twts, twt)
|
||||
}
|
||||
|
||||
return twts, offset, end, err
|
||||
}
|
||||
|
||||
func fetchUsers(ctx context.Context, db db, uri, q string) ([]types.Twt, error) {
|
||||
ctx, span := otel.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
where := `where parent_id is null and state not in ('permanantly-dead', 'frozen') and last_twt_on is not null`
|
||||
args := make([]any, 0)
|
||||
if uri != "" {
|
||||
where = `where feed_id = ? or parent_id = ?`
|
||||
feed_id := urlNS.UUID5(uri)
|
||||
args = append(args, feed_id, feed_id)
|
||||
} else if q != "" {
|
||||
where = `where nick like ?`
|
||||
args = append(args, "%"+q+"%")
|
||||
}
|
||||
|
||||
qry := `
|
||||
SELECT
|
||||
feed_id,
|
||||
uri,
|
||||
nick,
|
||||
last_scan_on,
|
||||
coalesce(last_twt_on, last_scan_on) last_twt_on
|
||||
FROM feeds
|
||||
left join last_twt_on using (feed_id)
|
||||
` + where + `
|
||||
order by nick, uri
|
||||
`
|
||||
fmt.Println(qry, args)
|
||||
|
||||
rows, err := db.QueryContext(ctx, qry, args...)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var twts []types.Twt
|
||||
for rows.Next() {
|
||||
var o struct {
|
||||
FeedID string
|
||||
URI string
|
||||
Nick string
|
||||
Dt TwtTime
|
||||
LastTwtOn TwtTime
|
||||
}
|
||||
err = rows.Scan(&o.FeedID, &o.URI, &o.Nick, &o.Dt, &o.LastTwtOn)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
twts = append(twts, lextwt.NewTwt(
|
||||
types.NewTwter(o.Nick, o.URI),
|
||||
lextwt.NewDateTime(o.Dt.Time, o.LastTwtOn.Time.Format(time.RFC3339)),
|
||||
nil,
|
||||
))
|
||||
}
|
||||
return twts, nil
|
||||
}
|
||||
|
||||
func fetchMentions(ctx context.Context, db db, mention uuid, limit int, offset int64) ([]types.Twt, int64, int64, error) {
|
||||
ctx, span := otel.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
args := make([]any, 0, 3)
|
||||
args = append(args, mention)
|
||||
|
||||
var end int64
|
||||
err := db.QueryRowContext(ctx, `
|
||||
select count(*) n from twt_mentions where feed_id = ?`, args...).Scan(&end)
|
||||
span.RecordError(err)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
fmt.Println(mention.MarshalText(), end, err)
|
||||
|
||||
if offset < 1 {
|
||||
offset += end
|
||||
}
|
||||
|
||||
limit = min(100, max(1, limit))
|
||||
offset = max(1, offset)
|
||||
|
||||
args = append(args, limit, offset-int64(limit))
|
||||
|
||||
qry := `
|
||||
SELECT
|
||||
feed_id,
|
||||
hash,
|
||||
conv,
|
||||
coalesce(nick, 'nobody') nick,
|
||||
coalesce(uri, 'https://empty.txt') uri,
|
||||
text
|
||||
FROM twts
|
||||
join (
|
||||
select feed_id, nick, uri
|
||||
from feeds
|
||||
) using (feed_id)
|
||||
where rowid in (
|
||||
select rowid from twts
|
||||
where ulid in (select ulid from twt_mentions where feed_id = ?)
|
||||
order by ulid asc
|
||||
limit ?
|
||||
offset ?
|
||||
)
|
||||
order by ulid asc
|
||||
`
|
||||
fmt.Println(qry, args)
|
||||
|
||||
rows, err := db.QueryContext(
|
||||
ctx, qry, args...,
|
||||
)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var twts []types.Twt
|
||||
for rows.Next() {
|
||||
var o struct {
|
||||
FeedID string
|
||||
Hash string
|
||||
Conv string
|
||||
Dt string
|
||||
Nick string
|
||||
URI string
|
||||
Text string
|
||||
}
|
||||
err = rows.Scan(&o.FeedID, &o.Hash, &o.Conv, &o.Nick, &o.URI, &o.Text)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
twter := types.NewTwter(o.Nick, o.URI)
|
||||
// o.Text = strings.ReplaceAll(o.Text, "\n", "\u2028")
|
||||
twt, _ := lextwt.ParseLine(o.Text, &twter)
|
||||
twts = append(twts, twt)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
return twts, offset, end, err
|
||||
}
|
||||
|
||||
func fetchConv(ctx context.Context, db db, hash string, limit int, offset int64) ([]types.Twt, int64, int64, error) {
|
||||
ctx, span := otel.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
var end int64
|
||||
err := db.QueryRowContext(ctx, `
|
||||
select count(*) n from twts where hash = $1 or conv = $1`, hash).Scan(&end)
|
||||
span.RecordError(err)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
rows, err := db.QueryContext(
|
||||
ctx, `
|
||||
SELECT
|
||||
feed_id,
|
||||
hash,
|
||||
conv,
|
||||
nick,
|
||||
uri,
|
||||
text
|
||||
FROM twts
|
||||
JOIN (
|
||||
SELECT
|
||||
feed_id,
|
||||
nick,
|
||||
uri
|
||||
FROM feeds
|
||||
) using (feed_id)
|
||||
WHERE
|
||||
hash = $1 or
|
||||
conv = $1
|
||||
order by ulid asc`, hash,
|
||||
)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var twts []types.Twt
|
||||
for rows.Next() {
|
||||
var o struct {
|
||||
FeedID string
|
||||
Hash string
|
||||
Conv string
|
||||
Dt string
|
||||
Nick string
|
||||
URI string
|
||||
Text string
|
||||
}
|
||||
err = rows.Scan(&o.FeedID, &o.Hash, &o.Conv, &o.Nick, &o.URI, &o.Text)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, 0,0,err
|
||||
}
|
||||
twter := types.NewTwter(o.Nick, o.URI)
|
||||
twt, _ := lextwt.ParseLine(o.Text, &twter)
|
||||
twts = append(twts, twt)
|
||||
}
|
||||
err = rows.Err()
|
||||
|
||||
return twts, offset, end, err
|
||||
}
|
||||
Reference in New Issue
Block a user