Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start hacking on query generator #284

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions cmd/bffgen/bffgen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"fmt"
"os"
"strings"
)

func main() {
if err := run(); err != nil {
os.Exit(1)
}
}

type FeedConfig struct {
Name string
BaseType string // "new" or "hot"
}

func ConditionNotHidden() string {
return "cp.is_hidden = FALSE"
}

func ConditionHasHashtag(hashtags []string) string {
return fmt.Sprintf(`%s && cp.hashtags`, stringSliceToArray(hashtags))
}

func ConditionNotHasHashtag(hashtags []string) string {
return fmt.Sprintf(`NOT %s && cp.hashtags`, stringSliceToArray(hashtags))
}

func ConditionNotDeleted() string {
return "cp.deleted_at IS NULL"
}

func ConditionActorApproved() string {
return "ca.status = 'approved'"
}

func ConditionVideoOnly() string {
return "COALESCE(cp.has_video, FALSE)"
}

func ConditionVideoOrImageOnly() string {
return "COALESCE(cp.has_media, cp.has_video, FALSE)"
}

func stringSliceToArray(in []string) string {
str := ""
for i, h := range in {
if i > 0 {
str += ", "
}
str += fmt.Sprintf("'%s'", h)
}
return fmt.Sprintf("ARRAY[%s]", str)
}

func ConditionNSFWOnly() string {
return "(ARRAY['nsfw', 'mursuit', 'murrsuit', 'nsfwfurry', 'furrynsfw'] && cp.hashtags OR ARRAY['porn', 'nudity', 'sexual'] && cp.self_labels)"
}

func ConditionSFWOnly() string {
return "NOT " + ConditionNSFWOnly()
}

func ConditionRemoveOldCreatedAt() string {
return "cp.created_at > NOW() - INTERVAL '7 day'"
}

func ConditionRemoveOldIndexedAt() string {
return "cp.indexed_at > NOW() - INTERVAL '7 day'"
}

func ConditionIndexedAtCursor() string {
return "cp.indexed_at < sqlc.arg(cursor_timestamp)"
}

func CombineConditions(conds ...string) string {
var sb strings.Builder
for i, c := range conds {
sb.WriteString("\t")
if i != 0 {
sb.WriteString("AND ")
}
sb.WriteString(c)
sb.WriteString("\n")
}
return sb.String()
}

func run() error {

const baseType = "new"
const feedName = "NewFurryNew"
var sb strings.Builder

sb.WriteString(fmt.Sprintf("-- name: Get%sFeed :many\n", feedName))
if baseType == "new" {
sb.WriteString(`SELECT
cp.*
FROM
candidate_posts AS cp
INNER JOIN
candidate_actors AS ca ON cp.actor_did = ca.did
WHERE
`)
conditions := []string{
// Generic
ConditionActorApproved(),
ConditionNotHidden(),
ConditionNotDeleted(),
ConditionRemoveOldCreatedAt(),
ConditionRemoveOldIndexedAt(),
ConditionIndexedAtCursor(),
// Specific
ConditionHasHashtag([]string{"furry", "furryart"}),
ConditionNotHasHashtag([]string{"aiart"}),
ConditionVideoOrImageOnly(),
ConditionSFWOnly(),
}
sb.WriteString(CombineConditions(conditions...))

sb.WriteString(`ORDER BY
cp.indexed_at DESC
LIMIT
sqlc.arg(_limit);`)
}

f, err := os.Create(".//store/queries/" + fmt.Sprintf("feed_%s.gen.sql", feedName))
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
f.WriteString(sb.String())

Check failure on line 134 in cmd/bffgen/bffgen.go

View workflow job for this annotation

GitHub Actions / Lint Go

Error return value of `f.WriteString` is not checked (errcheck)
return nil
}
72 changes: 72 additions & 0 deletions store/gen/feed_NewFurryNew.gen.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions store/queries/feed_NewFurryNew.gen.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- name: GetNewFurryNewFeed :many
SELECT
cp.*
FROM
candidate_posts AS cp
INNER JOIN
candidate_actors AS ca ON cp.actor_did = ca.did
WHERE
ca.status = 'approved'
AND cp.is_hidden = FALSE
AND cp.deleted_at IS NULL
AND cp.created_at > NOW() - INTERVAL '7 day'
AND cp.indexed_at > NOW() - INTERVAL '7 day'
AND cp.indexed_at < sqlc.arg(cursor_timestamp)
AND ARRAY['furry', 'furryart'] && cp.hashtags
AND NOT ARRAY['aiart'] && cp.hashtags
AND COALESCE(cp.has_media, cp.has_video, FALSE)
AND NOT (ARRAY['nsfw', 'mursuit', 'murrsuit', 'nsfwfurry', 'furrynsfw'] && cp.hashtags OR ARRAY['porn', 'nudity', 'sexual'] && cp.self_labels)
ORDER BY
cp.indexed_at DESC
LIMIT
sqlc.arg(_limit);
Loading