diff --git a/collections.go b/collections.go index 49fc0c0..ea5b929 100644 --- a/collections.go +++ b/collections.go @@ -249,7 +249,6 @@ func (c *Collection) StyleSheetDisplay() template.CSS { // ForPublic modifies the Collection for public consumption, such as via // the API. func (c *Collection) ForPublic() { - c.ID = 0 c.URL = c.CanonicalURL() } diff --git a/database.go b/database.go index d3e6566..b9888e6 100644 --- a/database.go +++ b/database.go @@ -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 { return nil, err } 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 mysqlErr, ok := err.(*mysql.MySQLError); ok { if mysqlErr.Number == mySQLErrDuplicateKey { // Duplicate entry error; try a new slug // TODO: make this a little more robust 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 { 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), OwnerID: 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(), Title: zero.NewString(*(post.Title), true), Content: *(post.Content), diff --git a/posts.go b/posts.go index 996602b..46c44ce 100644 --- a/posts.go +++ b/posts.go @@ -541,13 +541,13 @@ func newPost(app *app, w http.ResponseWriter, r *http.Request) error { var newPost *PublicPost = &PublicPost{} var coll *Collection - var collID int64 var err error if accessToken != "" { newPost, err = app.db.CreateOwnedPost(p, accessToken, collAlias) } else { //return ErrNotLoggedIn // TODO: verify user is logged in + var collID int64 if collAlias != "" { coll, err = app.db.GetCollection(collAlias) if err != nil { @@ -575,8 +575,8 @@ func newPost(app *app, w http.ResponseWriter, r *http.Request) error { // Write success now response := impart.WriteSuccess(w, newPost, http.StatusCreated) - if coll != nil && app.cfg.App.Federation { - go federatePost(app, newPost, collID, false) + if newPost.Collection != nil && app.cfg.App.Federation && !newPost.Created.After(time.Now()) { + go federatePost(app, newPost, newPost.Collection.ID, false) } return response