Seed news posts

This commit is contained in:
Ben Visness 2022-05-07 14:45:21 -05:00
parent 3c4238994a
commit f4601198c9
3 changed files with 38 additions and 30 deletions

View File

@ -628,7 +628,7 @@ func UserCanEditPost(ctx context.Context, connOrTx db.ConnOrTx, user models.User
func CreateNewPost(
ctx context.Context,
conn db.ConnOrTx,
tx pgx.Tx,
projectId int,
threadId int, threadType models.ThreadType,
userId int,
@ -637,7 +637,7 @@ func CreateNewPost(
ipString string,
) (postId, versionId int) {
// Create post
err := conn.QueryRow(ctx,
err := tx.QueryRow(ctx,
`
INSERT INTO post (postdate, thread_id, thread_type, current_id, author_id, project_id, reply_id, preview)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
@ -657,10 +657,10 @@ func CreateNewPost(
}
// Create and associate version
versionId = CreatePostVersion(ctx, conn, postId, unparsedContent, ipString, "", nil)
versionId = CreatePostVersion(ctx, tx, postId, unparsedContent, ipString, "", nil)
// Fix up thread
err = FixThreadPostIds(ctx, conn, threadId)
err = FixThreadPostIds(ctx, tx, threadId)
if err != nil {
panic(oops.New(err, "failed to fix up thread post IDs"))
}
@ -678,7 +678,7 @@ func CreateNewPost(
}
updates := strings.Join(updateEntries, ", ")
_, err = conn.Exec(ctx,
_, err = tx.Exec(ctx,
`
UPDATE project
SET `+updates+`
@ -768,7 +768,7 @@ func DeletePost(
const maxPostContentLength = 200000
func CreatePostVersion(ctx context.Context, conn db.ConnOrTx, postId int, unparsedContent string, ipString string, editReason string, editorId *int) (versionId int) {
func CreatePostVersion(ctx context.Context, tx pgx.Tx, postId int, unparsedContent string, ipString string, editReason string, editorId *int) (versionId int) {
if len(unparsedContent) > maxPostContentLength {
logging.ExtractLogger(ctx).Warn().
Str("preview", unparsedContent[:400]).
@ -787,7 +787,7 @@ func CreatePostVersion(ctx context.Context, conn db.ConnOrTx, postId int, unpars
}
// Create post version
err := conn.QueryRow(ctx,
err := tx.QueryRow(ctx,
`
INSERT INTO post_version (post_id, text_raw, text_parsed, ip, date, edit_reason, editor_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
@ -806,7 +806,7 @@ func CreatePostVersion(ctx context.Context, conn db.ConnOrTx, postId int, unpars
}
// Update post with version id and preview
_, err = conn.Exec(ctx,
_, err = tx.Exec(ctx,
`
UPDATE post
SET current_id = $1, preview = $2
@ -822,7 +822,7 @@ func CreatePostVersion(ctx context.Context, conn db.ConnOrTx, postId int, unpars
// Update asset usage
_, err = conn.Exec(ctx,
_, err = tx.Exec(ctx,
`
DELETE FROM post_asset_usage
WHERE post_id = $1
@ -839,7 +839,7 @@ func CreatePostVersion(ctx context.Context, conn db.ConnOrTx, postId int, unpars
keys = append(keys, key)
}
assetIDs, err := db.QueryScalar[uuid.UUID](ctx, conn,
assetIDs, err := db.QueryScalar[uuid.UUID](ctx, tx,
`
SELECT id
FROM asset
@ -857,7 +857,7 @@ func CreatePostVersion(ctx context.Context, conn db.ConnOrTx, postId int, unpars
values = append(values, []interface{}{postId, assetID})
}
_, err = conn.CopyFrom(ctx, pgx.Identifier{"post_asset_usage"}, []string{"post_id", "asset_id"}, pgx.CopyFromRows(values))
_, err = tx.CopyFrom(ctx, pgx.Identifier{"post_asset_usage"}, []string{"post_id", "asset_id"}, pgx.CopyFromRows(values))
if err != nil {
panic(oops.New(err, "failed to insert post asset usage"))
}

View File

@ -111,15 +111,15 @@ func SampleSeed() {
defer tx.Rollback(ctx)
fmt.Println("Creating admin user (\"admin\"/\"password\")...")
seedUser(ctx, conn, models.User{Username: "admin", Email: "admin@handmade.network", IsStaff: true})
admin := seedUser(ctx, tx, models.User{Username: "admin", Name: "Admin", Email: "admin@handmade.network", IsStaff: true})
fmt.Println("Creating normal users (all with password \"password\")...")
alice := seedUser(ctx, conn, models.User{Username: "alice", Name: "Alice"})
bob := seedUser(ctx, conn, models.User{Username: "bob", Name: "Bob"})
charlie := seedUser(ctx, conn, models.User{Username: "charlie", Name: "Charlie"})
alice := seedUser(ctx, tx, models.User{Username: "alice", Name: "Alice"})
bob := seedUser(ctx, tx, models.User{Username: "bob", Name: "Bob"})
charlie := seedUser(ctx, tx, models.User{Username: "charlie", Name: "Charlie"})
fmt.Println("Creating a spammer...")
spammer := seedUser(ctx, conn, models.User{
spammer := seedUser(ctx, tx, models.User{
Username: "spam",
Status: models.UserStatusConfirmed,
Name: "Hot singletons in your local area",
@ -128,16 +128,24 @@ func SampleSeed() {
users := []*models.User{alice, bob, charlie, spammer}
fmt.Println("Creating some threads...")
fmt.Println("Creating some forum threads...")
for i := 0; i < 5; i++ {
thread := seedThread(ctx, conn, models.Thread{})
populateThread(ctx, conn, thread, users, rand.Intn(5)+1)
thread := seedThread(ctx, tx, models.Thread{})
populateThread(ctx, tx, thread, users, rand.Intn(5)+1)
}
// spam-only thread
{
thread := seedThread(ctx, conn, models.Thread{})
populateThread(ctx, conn, thread, []*models.User{spammer}, 1)
thread := seedThread(ctx, tx, models.Thread{})
populateThread(ctx, tx, thread, []*models.User{spammer}, 1)
}
fmt.Println("Creating the news posts...")
{
for i := 0; i < 3; i++ {
thread := seedThread(ctx, tx, models.Thread{Type: models.ThreadTypeProjectBlogPost})
populateThread(ctx, tx, thread, []*models.User{admin, alice, bob, charlie}, rand.Intn(5)+1)
}
}
// admin := CreateAdminUser("admin", "12345678")
@ -197,7 +205,7 @@ func seedUser(ctx context.Context, conn db.ConnOrTx, input models.User) *models.
return user
}
func seedThread(ctx context.Context, conn db.ConnOrTx, input models.Thread) *models.Thread {
func seedThread(ctx context.Context, tx pgx.Tx, input models.Thread) *models.Thread {
input.Type = utils.OrDefault(input.Type, models.ThreadTypeForumPost)
var defaultSubforum *int
@ -206,7 +214,7 @@ func seedThread(ctx context.Context, conn db.ConnOrTx, input models.Thread) *mod
defaultSubforum = &id
}
thread, err := db.QueryOne[models.Thread](ctx, conn,
thread, err := db.QueryOne[models.Thread](ctx, tx,
`
INSERT INTO thread (
title,
@ -234,7 +242,7 @@ func seedThread(ctx context.Context, conn db.ConnOrTx, input models.Thread) *mod
return thread
}
func populateThread(ctx context.Context, conn db.ConnOrTx, thread *models.Thread, users []*models.User, numPosts int) {
func populateThread(ctx context.Context, tx pgx.Tx, thread *models.Thread, users []*models.User, numPosts int) {
var lastPostId int
for i := 0; i < numPosts; i++ {
user := users[i%len(users)]
@ -246,7 +254,7 @@ func populateThread(ctx context.Context, conn db.ConnOrTx, thread *models.Thread
}
}
hmndata.CreateNewPost(ctx, conn, thread.ProjectID, thread.ID, thread.Type, user.ID, replyId, lorem.Paragraph(1, 10), "192.168.2.1")
hmndata.CreateNewPost(ctx, tx, thread.ProjectID, thread.ID, thread.Type, user.ID, replyId, lorem.Paragraph(1, 10), "192.168.2.1")
}
}

View File

@ -181,12 +181,12 @@
sizes using CSS grid properties.
*/}}
<div>
<h2>Latest News</h2>
{{ with .NewsPost }}
{{ with .NewsPost }}
<div>
<h2>Latest News</h2>
{{ template "timeline_item.html" . }}
{{ end }}
</div>
</div>
{{ end }}
<div class="landing-right">
<h2>Around the Network</h2>
<div class="optionbar mb2">