feat: add bump limit check on thread update

This commit is contained in:
Yanislav Igonin 2022-01-27 18:24:59 +02:00
parent 868a7214d2
commit d4f703acb6
4 changed files with 58 additions and 18 deletions

View File

@ -3,4 +3,5 @@ PORT=3000
SEED_DB=true
IS_RATE_LIMITER_ENABLED=true
THREADS_MAX_COUNT=50
POSTGRES_URL=postgres://localhost/micrach?pool_max_conns=5
POSTGRES_URL=postgres://localhost/micrach?pool_max_conns=5
THREAD_BUMP_LIMIT=500

View File

@ -13,6 +13,7 @@ type AppConfig struct {
SeedDb bool
IsRateLimiterEnabled bool
ThreadsMaxCount int
ThreadBumpLimit int
}
type DbConfig struct {
@ -50,6 +51,7 @@ func getAppConfig() AppConfig {
seedDb := getValueOrDefaultBoolean(os.Getenv("SEED_DB"), false)
isRateLimiterEnabled := getValueOrDefaultBoolean(os.Getenv("IS_RATE_LIMITER_ENABLED"), true)
threadsMaxCount := getValueOrDefaultInt(os.Getenv("THREADS_MAX_COUNT"), 50)
threadBumpLimit := getValueOrDefaultInt(os.Getenv("THREAD_BUMP_LIMIT"), 500)
return AppConfig{
Env: env,
@ -57,6 +59,7 @@ func getAppConfig() AppConfig {
SeedDb: seedDb,
IsRateLimiterEnabled: isRateLimiterEnabled,
ThreadsMaxCount: threadsMaxCount,
ThreadBumpLimit: threadBumpLimit,
}
}

View File

@ -309,6 +309,11 @@ func UpdateThread(c *gin.Context) {
}
defer tx.Rollback(context.TODO())
if err != nil {
log.Println("error:", err)
c.HTML(http.StatusInternalServerError, "500.html", nil)
return
}
post := Repositories.Post{
IsParent: false,
ParentID: threadID,
@ -323,6 +328,23 @@ func UpdateThread(c *gin.Context) {
return
}
postsCountInThread, err := Repositories.Posts.GetThreadPostsCount(threadID)
if err != nil {
log.Println("error:", err)
c.HTML(http.StatusInternalServerError, "500.html", nil)
return
}
isBumpLimit := postsCountInThread >= Config.App.ThreadBumpLimit
isThreadBumped := !isBumpLimit && !isSage && !post.IsParent
if isThreadBumped {
err = Repositories.Posts.BumpThreadInTx(tx, threadID)
if err != nil {
log.Println("error:", err)
c.HTML(http.StatusInternalServerError, "500.html", nil)
return
}
}
for _, fileInRequest := range filesInRequest {
file := Repositories.File{
PostID: postID,

View File

@ -208,23 +208,6 @@ func (r *PostsRepository) CreateInTx(tx pgx.Tx, p Post) (int, error) {
return 0, err
}
// updating parent post `updated_at`
if !p.IsParent && !p.IsSage {
sql = `
UPDATE posts
SET updated_at = now()
WHERE id = $1
`
row := tx.QueryRow(context.TODO(), sql, p.ParentID)
var msg string
err = row.Scan(&msg)
// UPDATE always return `no rows`
// so we need to check this condition
if err != nil && err != pgx.ErrNoRows {
return 0, err
}
}
return createdPost.ID, nil
}
@ -263,3 +246,34 @@ func (r *PostsRepository) ArchiveThreadsFrom(t time.Time) error {
_, err := Db.Pool.Exec(context.TODO(), sql, t)
return err
}
// Returns count of posts in thread by thread ID
func (r *PostsRepository) GetThreadPostsCount(id int) (int, error) {
sql := `
SELECT COUNT(*)
FROM posts
WHERE
(id = $1 AND is_parent = true AND is_deleted != true)
OR (parent_id = $1 AND is_deleted != true)
`
row := Db.Pool.QueryRow(context.TODO(), sql, id)
var count int
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
// Updates threads updated at time by thread ID
func (r *PostsRepository) BumpThreadInTx(tx pgx.Tx, id int) error {
sql := `
UPDATE posts
SET updated_at = now()
WHERE id = $1
`
_, err := tx.Query(context.TODO(), sql, id)
return err
}