Accept different `created` time on post publish

This helps with post importing and clients that want to support post
scheduling. It also changes how Collection.ForPublic() works, no longer
resetting the ID.

Closes T532
pull/32/head
Matt Baer 6 years ago
parent 7321f6d5a0
commit 99a10a2563
  1. 1
      collections.go
  2. 17
      database.go
  3. 6
      posts.go

@ -249,7 +249,6 @@ func (c *Collection) StyleSheetDisplay() template.CSS {
// ForPublic modifies the Collection for public consumption, such as via // ForPublic modifies the Collection for public consumption, such as via
// the API. // the API.
func (c *Collection) ForPublic() { func (c *Collection) ForPublic() {
c.ID = 0
c.URL = c.CanonicalURL() c.URL = c.CanonicalURL()
} }

@ -557,19 +557,28 @@ func (db *datastore) CreatePost(userID, collID int64, post *SubmittedPost) (*Pos
} }
} }
stmt, err := db.Prepare("INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, privacy, owner_id, collection_id, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)") created := time.Now()
if post.Created != nil {
created, err = time.Parse("2006-01-02T15:04:05Z", *post.Created)
if err != nil {
log.Error("Unable to parse Created time '%s': %v", *post.Created, err)
created = time.Now()
}
}
stmt, err := db.Prepare("INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, privacy, owner_id, collection_id, created, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)")
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer stmt.Close() defer stmt.Close()
_, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, 0, ownerID, ownerCollID, 0) _, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, 0, ownerID, ownerCollID, created, 0)
if err != nil { if err != nil {
if mysqlErr, ok := err.(*mysql.MySQLError); ok { if mysqlErr, ok := err.(*mysql.MySQLError); ok {
if mysqlErr.Number == mySQLErrDuplicateKey { if mysqlErr.Number == mySQLErrDuplicateKey {
// Duplicate entry error; try a new slug // Duplicate entry error; try a new slug
// TODO: make this a little more robust // TODO: make this a little more robust
slug = sql.NullString{id.GenSafeUniqueSlug(slug.String), true} slug = sql.NullString{id.GenSafeUniqueSlug(slug.String), true}
_, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, 0, ownerID, ownerCollID, 0) _, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, 0, ownerID, ownerCollID, created, 0)
if err != nil { if err != nil {
return nil, handleFailedPostInsert(fmt.Errorf("Retried slug generation, still failed: %v", err)) return nil, handleFailedPostInsert(fmt.Errorf("Retried slug generation, still failed: %v", err))
} }
@ -590,7 +599,7 @@ func (db *datastore) CreatePost(userID, collID int64, post *SubmittedPost) (*Pos
RTL: zero.NewBool(post.IsRTL.Bool, post.IsRTL.Valid), RTL: zero.NewBool(post.IsRTL.Bool, post.IsRTL.Valid),
OwnerID: null.NewInt(userID, true), OwnerID: null.NewInt(userID, true),
CollectionID: null.NewInt(userID, true), CollectionID: null.NewInt(userID, true),
Created: time.Now().Truncate(time.Second).UTC(), Created: created.Truncate(time.Second).UTC(),
Updated: time.Now().Truncate(time.Second).UTC(), Updated: time.Now().Truncate(time.Second).UTC(),
Title: zero.NewString(*(post.Title), true), Title: zero.NewString(*(post.Title), true),
Content: *(post.Content), Content: *(post.Content),

@ -541,13 +541,13 @@ func newPost(app *app, w http.ResponseWriter, r *http.Request) error {
var newPost *PublicPost = &PublicPost{} var newPost *PublicPost = &PublicPost{}
var coll *Collection var coll *Collection
var collID int64
var err error var err error
if accessToken != "" { if accessToken != "" {
newPost, err = app.db.CreateOwnedPost(p, accessToken, collAlias) newPost, err = app.db.CreateOwnedPost(p, accessToken, collAlias)
} else { } else {
//return ErrNotLoggedIn //return ErrNotLoggedIn
// TODO: verify user is logged in // TODO: verify user is logged in
var collID int64
if collAlias != "" { if collAlias != "" {
coll, err = app.db.GetCollection(collAlias) coll, err = app.db.GetCollection(collAlias)
if err != nil { if err != nil {
@ -575,8 +575,8 @@ func newPost(app *app, w http.ResponseWriter, r *http.Request) error {
// Write success now // Write success now
response := impart.WriteSuccess(w, newPost, http.StatusCreated) response := impart.WriteSuccess(w, newPost, http.StatusCreated)
if coll != nil && app.cfg.App.Federation { if newPost.Collection != nil && app.cfg.App.Federation && !newPost.Created.After(time.Now()) {
go federatePost(app, newPost, collID, false) go federatePost(app, newPost, newPost.Collection.ID, false)
} }
return response return response

Loading…
Cancel
Save