Clean up lots of old post fields

This commit is contained in:
Ben Visness 2021-07-04 16:24:48 -05:00
parent 86e228d845
commit de0b7a08fb
8 changed files with 82 additions and 26 deletions

View File

@ -0,0 +1,68 @@
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(ReworkPostFields{})
}
type ReworkPostFields struct{}
func (m ReworkPostFields) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 7, 4, 20, 46, 42, 0, time.UTC))
}
func (m ReworkPostFields) Name() string {
return "ReworkPostFields"
}
func (m ReworkPostFields) Description() string {
return "Clean up post and postversion fields"
}
func (m ReworkPostFields) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE handmade_post
DROP depth,
DROP slug,
DROP author_name,
DROP ip,
DROP sticky,
DROP hits,
DROP featured,
DROP featurevotes;
`)
if err != nil {
return oops.New(err, "failed to drop unnecessary post fields")
}
_, err = tx.Exec(ctx, `
ALTER TABLE handmade_postversion
RENAME edit_ip TO ip;
ALTER TABLE handmade_postversion
RENAME edit_date TO date;
`)
if err != nil {
return oops.New(err, "failed to rename postversion fields")
}
_, err = tx.Exec(ctx, `
DROP TABLE handmade_kunenapost;
`)
if err != nil {
return oops.New(err, "failed to drop ancient weirdo table")
}
return nil
}
func (m ReworkPostFields) Down(ctx context.Context, tx pgx.Tx) error {
panic("Implement me")
}

View File

@ -18,16 +18,8 @@ type Post struct {
CategoryKind CategoryKind `db:"category_kind"`
Depth int `db:"depth"` // TODO: Drop this.
Slug string `db:"slug"` // TODO: Drop this.
AuthorName string `db:"author_name"` // TODO: Drop this.
PostDate time.Time `db:"postdate"` // TODO: Drop this.
IP net.IPNet `db:"ip"` // TODO: Drop this.
Sticky bool `db:"sticky"` // TODO: Drop this.
PostDate time.Time `db:"postdate"`
Deleted bool `db:"deleted"`
Hits int `db:"hits"` // TODO: Drop this.
Featured bool `db:"featured"` // TODO: Drop this.
FeatureVotes int `db:"featurevotes"` // TODO: Drop this.
Preview string `db:"preview"`
ReadOnly bool `db:"readonly"`
@ -40,8 +32,8 @@ type PostVersion struct {
TextRaw string `db:"text_raw"`
TextParsed string `db:"text_parsed"`
EditIP *net.IPNet `db:"edit_ip"`
EditDate time.Time `db:"edit_date"`
IP *net.IPNet `db:"ip"`
Date time.Time `db:"date"`
EditReason string `db:"edit_reason"`
EditorID *int `db:"editor_id"`
}

View File

@ -29,19 +29,17 @@ func PostToTemplate(p *models.Post, author *models.User, currentTheme string) Po
Author: authorUser,
// No content. A lot of the time we don't have this handy and don't need it. See AddContentVersion.
PostDate: p.PostDate,
IP: p.IP.String(),
}
}
func (p *Post) AddContentVersion(ver models.PostVersion, editor *models.User, currentTheme string) {
func (p *Post) AddContentVersion(ver models.PostVersion, editor *models.User) {
p.Content = template.HTML(ver.TextParsed)
p.IP = maybeIp(ver.IP)
if editor != nil {
editorTmpl := UserToTemplate(editor, currentTheme)
editorTmpl := UserToTemplate(editor, "theme not required here")
p.Editor = &editorTmpl
p.EditDate = ver.EditDate
p.EditIP = maybeIp(ver.EditIP)
p.EditDate = ver.Date
p.EditReason = ver.EditReason
}
}

View File

@ -87,16 +87,15 @@
Edited by
<a class="name" href="{{ .Editor.ProfileUrl }}" target="_blank">{{ coalesce .Editor.Name .Editor.Username }}</a>
{{ if $.User }}
{{ if and $.User.IsStaff .EditIP }}<span class="ip">[{{ .EditIP }}]</span>{{ end }}
{{ if and $.User.IsStaff .IP }}<span class="ip">[{{ .IP }}]</span>{{ end }}
{{ end }}
on {{ timehtml (absolutedate .EditDate) .EditDate }}
{{ with .EditReason }}
Reason: {{ . }}
{{ end }}
</span>
{{ end }}
{{ if $.User }}
{{ if $.User.IsStaff }}
{{ else if $.User }}
{{ if and $.User.IsStaff .IP }}
<span>[{{ .IP }}]</span>
{{ end }}
{{ end }}

View File

@ -84,7 +84,6 @@ type Post struct {
Editor *User
EditDate time.Time
EditIP string
EditReason string
IP string

View File

@ -385,7 +385,7 @@ func fetchAllPosts(c *RequestContext, lineageBuilder *models.CategoryLineageBuil
)
postItem.UUID = uuid.NewSHA1(uuid.NameSpaceURL, []byte(postItem.Url)).URN()
postItem.LastEditDate = postResult.PostVersion.EditDate
postItem.LastEditDate = postResult.PostVersion.Date
postItems = append(postItems, postItem)
}

View File

@ -405,7 +405,7 @@ func ForumThread(c *RequestContext) ResponseData {
row := irow.(*postsQueryResult)
post := templates.PostToTemplate(&row.Post, row.Author, c.Theme)
post.AddContentVersion(row.Ver, row.Editor, c.Theme)
post.AddContentVersion(row.Ver, row.Editor)
post.AddUrls(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, post.ID)
posts = append(posts, post)

View File

@ -98,7 +98,7 @@ func PostVersionToWikiTimelineItem(lineageBuilder *models.CategoryLineageBuilder
Type: templates.TimelineTypeWikiEdit,
TypeTitle: TimelineTypeTitleMap[templates.TimelineTypeWikiEdit],
Class: TimelineItemClassMap[templates.TimelineTypeWikiEdit],
Date: version.EditDate,
Date: version.Date,
Url: hmnurl.BuildWikiArticle(project.Slug, thread.ID, thread.Title),
OwnerAvatarUrl: templates.UserAvatarUrl(owner, currentTheme),