Skip to content

Commit

Permalink
Set correct updated at when creating a post
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika committed Nov 10, 2023
1 parent dcf93cb commit 550b987
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions db/postgres/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ const (
sqlInsertPost = `
INSERT INTO posts
(user_id, filename, slug, title, text, description, publish_at, hidden, cur_space,
file_size, mime_type, shasum, data, expires_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
file_size, mime_type, shasum, data, expires_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
RETURNING id`
sqlInsertUser = `INSERT INTO app_users DEFAULT VALUES returning id`
sqlInsertTag = `INSERT INTO post_tags (post_id, name) VALUES($1, $2) RETURNING id;`
Expand Down Expand Up @@ -730,6 +730,7 @@ func (me *PsqlDB) InsertPost(post *db.Post) (*db.Post, error) {
post.Shasum,
post.Data,
post.ExpiresAt,
post.UpdatedAt,
).Scan(&id)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions filehandlers/imgs/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
return err
}

modTime := time.Unix(data.Mtime, 0)

if len(data.OrigText) == 0 {
err = h.removePost(data)
if err != nil {
Expand Down Expand Up @@ -175,6 +177,7 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
Slug: data.Slug,
Text: data.Text,
Title: data.Title,
UpdatedAt: &modTime,
}
_, err := h.DBPool.InsertPost(&insertPost)
if err != nil {
Expand All @@ -194,8 +197,6 @@ func (h *UploadImgHandler) writeImg(s ssh.Session, data *PostMetaData) error {
}
}
} else {
modTime := time.Unix(data.Mtime, 0)

if data.Shasum == data.Cur.Shasum && modTime.Equal(*data.Cur.UpdatedAt) {
h.Cfg.Logger.Infof("(%s) found, but image is identical, skipping", data.Filename)
return nil
Expand Down
5 changes: 3 additions & 2 deletions filehandlers/post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
return "", err
}

modTime := time.Unix(entry.Mtime, 0)

// if the file is empty we remove it from our database
if len(origText) == 0 {
// skip empty files from being added to db
Expand Down Expand Up @@ -264,6 +266,7 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
Text: metadata.Text,
Title: metadata.Title,
ExpiresAt: metadata.ExpiresAt,
UpdatedAt: &modTime,
}
post, err = h.DBPool.InsertPost(&insertPost)
if err != nil {
Expand Down Expand Up @@ -295,8 +298,6 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
}
}
} else {
modTime := time.Unix(entry.Mtime, 0)

if metadata.Text == post.Text && modTime.Equal(*post.UpdatedAt) {
logger.Infof("(%s) found, but text is identical, skipping", filename)
curl := shared.NewCreateURL(h.Cfg)
Expand Down

0 comments on commit 550b987

Please sign in to comment.