WIP broken schema changes for forum editing

This commit is contained in:
Ben Visness 2021-07-04 15:25:28 -05:00
parent d11094481f
commit 86e228d845
4 changed files with 118 additions and 23 deletions

View File

@ -0,0 +1,44 @@
package migrations
import (
"context"
"time"
"git.handmade.network/hmn/hmn/src/migration/types"
"git.handmade.network/hmn/hmn/src/oops"
"github.com/jackc/pgx/v4"
)
func init() {
registerMigration(RemoveParserField{})
}
type RemoveParserField struct{}
func (m RemoveParserField) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 7, 3, 20, 2, 33, 0, time.UTC))
}
func (m RemoveParserField) Name() string {
return "RemoveParserField"
}
func (m RemoveParserField) Description() string {
return "Remove the parser field on post versions, since we now have a universal parser"
}
func (m RemoveParserField) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE handmade_postversion
DROP parser;
`)
if err != nil {
return oops.New(err, "failed to delete parser field")
}
return nil
}
func (m RemoveParserField) Down(ctx context.Context, tx pgx.Tx) error {
panic("Implement me")
}

View File

@ -18,36 +18,27 @@ type Post struct {
CategoryKind CategoryKind `db:"category_kind"` CategoryKind CategoryKind `db:"category_kind"`
Depth int `db:"depth"` Depth int `db:"depth"` // TODO: Drop this.
Slug string `db:"slug"` Slug string `db:"slug"` // TODO: Drop this.
AuthorName string `db:"author_name"` // TODO: Drop this. AuthorName string `db:"author_name"` // TODO: Drop this.
PostDate time.Time `db:"postdate"` PostDate time.Time `db:"postdate"` // TODO: Drop this.
IP net.IPNet `db:"ip"` IP net.IPNet `db:"ip"` // TODO: Drop this.
Sticky bool `db:"sticky"` Sticky bool `db:"sticky"` // TODO: Drop this.
Deleted bool `db:"deleted"` // TODO: I'm not sure this is ever meaningfully used. It always seems to be 0 / false? Deleted bool `db:"deleted"`
Hits int `db:"hits"` Hits int `db:"hits"` // TODO: Drop this.
Featured bool `db:"featured"` Featured bool `db:"featured"` // TODO: Drop this.
FeatureVotes int `db:"featurevotes"` // TODO: Remove this column from the db, it's never used FeatureVotes int `db:"featurevotes"` // TODO: Drop this.
Preview string `db:"preview"` Preview string `db:"preview"`
ReadOnly bool `db:"readonly"` ReadOnly bool `db:"readonly"`
} }
type Parser int
const (
ParserBBCode Parser = 1
ParserCleanHTML = 2
ParserMarkdown = 3
)
type PostVersion struct { type PostVersion struct {
ID int `db:"id"` ID int `db:"id"`
PostID int `db:"post_id"` PostID int `db:"post_id"`
TextRaw string `db:"text_raw"` TextRaw string `db:"text_raw"`
TextParsed string `db:"text_parsed"` TextParsed string `db:"text_parsed"`
Parser Parser `db:"parser"`
EditIP *net.IPNet `db:"edit_ip"` EditIP *net.IPNet `db:"edit_ip"`
EditDate time.Time `db:"edit_date"` EditDate time.Time `db:"edit_date"`

View File

@ -12,6 +12,7 @@ import (
"git.handmade.network/hmn/hmn/src/hmnurl" "git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/models" "git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/oops" "git.handmade.network/hmn/hmn/src/oops"
"git.handmade.network/hmn/hmn/src/parsing"
"git.handmade.network/hmn/hmn/src/templates" "git.handmade.network/hmn/hmn/src/templates"
"git.handmade.network/hmn/hmn/src/utils" "git.handmade.network/hmn/hmn/src/utils"
) )
@ -567,6 +568,65 @@ func ForumNewThread(c *RequestContext) ResponseData {
} }
func ForumNewThreadSubmit(c *RequestContext) ResponseData { func ForumNewThreadSubmit(c *RequestContext) ResponseData {
tx, err := c.Conn.Begin(c.Context())
if err != nil {
panic(err)
}
c.Perf.StartBlock("SQL", "Fetch category tree")
categoryTree := models.GetFullCategoryTree(c.Context(), c.Conn)
lineageBuilder := models.MakeCategoryLineageBuilder(categoryTree)
c.Perf.EndBlock()
currentCatId, valid := validateSubforums(lineageBuilder, c.CurrentProject, c.PathParams["cats"])
if !valid {
return FourOhFour(c)
}
c.Req.ParseForm()
title := c.Req.Form.Get("title")
unparsed := c.Req.Form.Get("body")
sticky := false
if c.CurrentUser.IsStaff && c.Req.Form.Get("sticky") != "" {
sticky = true
}
parsed := parsing.ParsePostInput(unparsed, false)
// Create thread
var threadId int
err = tx.QueryRow(c.Context(),
`
INSERT INTO handmade_thread (title, sticky, locked, category_id)
RETURNING id
`,
title,
sticky,
false,
currentCatId,
).Scan(&threadId)
if err != nil {
panic(oops.New(err, "failed to create thread"))
}
// Create post version
_, err = tx.Exec(c.Context(),
`
INSERT INTO handmade_postversion (post_id, text_raw, text_parsed)
VALUES ($1, $2, $3)
`,
// TODO: post id
unparsed,
parsed,
)
err = tx.Commit(c.Context())
if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to create new forum thread"))
}
// TODO: Redirect to newly created thread
return c.Redirect(hmnurl.BuildForumNewThread(models.HMNProjectSlug, nil, false), http.StatusSeeOther) return c.Redirect(hmnurl.BuildForumNewThread(models.HMNProjectSlug, nil, false), http.StatusSeeOther)
} }

View File

@ -11,11 +11,11 @@ import (
) )
var TimelineTypeMap = map[models.CategoryKind][]templates.TimelineType{ var TimelineTypeMap = map[models.CategoryKind][]templates.TimelineType{
// No parent, Has parent // { No parent , Has parent }
models.CatKindBlog: []templates.TimelineType{templates.TimelineTypeBlogPost, templates.TimelineTypeBlogComment}, models.CatKindBlog: {templates.TimelineTypeBlogPost, templates.TimelineTypeBlogComment},
models.CatKindForum: []templates.TimelineType{templates.TimelineTypeForumThread, templates.TimelineTypeForumReply}, models.CatKindForum: {templates.TimelineTypeForumThread, templates.TimelineTypeForumReply},
models.CatKindWiki: []templates.TimelineType{templates.TimelineTypeWikiCreate, templates.TimelineTypeWikiTalk}, models.CatKindWiki: {templates.TimelineTypeWikiCreate, templates.TimelineTypeWikiTalk},
models.CatKindLibraryResource: []templates.TimelineType{templates.TimelineTypeLibraryComment, templates.TimelineTypeLibraryComment}, models.CatKindLibraryResource: {templates.TimelineTypeLibraryComment, templates.TimelineTypeLibraryComment},
} }
var TimelineItemClassMap = map[templates.TimelineType]string{ var TimelineItemClassMap = map[templates.TimelineType]string{