-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts_update.go
More file actions
145 lines (129 loc) · 3.09 KB
/
Copy pathposts_update.go
File metadata and controls
145 lines (129 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package karu
import (
"context"
"database/sql"
"fmt"
)
func (k *Karu) UpdatePost(ctx context.Context, postID, authorID, title, content, permString string) error {
if err := validateLength("title", title, MaxTitleLength); err != nil {
return err
}
if err := validateLength("content", content, MaxContentLength); err != nil {
return err
}
perms, err := ParsePermissions(permString)
if err != nil {
return err
}
post, err := k.getPostByID(ctx, postID)
if err != nil {
if err == sql.ErrNoRows {
return ErrPostNotFound
}
return err
}
allowed := false
needCode := byte('d')
if post.AuthorID == authorID && perms.Has(post.Path, 'd') {
allowed = true
}
if perms.Has(post.Path, 'D') {
allowed = true
}
if !allowed {
if post.AuthorID != authorID {
needCode = 'D'
}
return &PermissionError{Path: post.Path, Code: needCode}
}
now := k.nowMilli()
_, err = k.db.ExecContext(ctx,
`UPDATE posts SET title = $1, content = $2, updated_at = $3 WHERE id = $4`,
title, content, now, postID)
if err != nil {
return fmt.Errorf("updating post: %w", err)
}
return nil
}
func (k *Karu) DeletePost(ctx context.Context, postID, authorID, permString string) error {
perms, err := ParsePermissions(permString)
if err != nil {
return err
}
post, err := k.getPostByID(ctx, postID)
if err != nil {
if err == sql.ErrNoRows {
return ErrPostNotFound
}
return err
}
allowed := false
needCode := byte('d')
if post.AuthorID == authorID && perms.Has(post.Path, 'd') {
allowed = true
}
if perms.Has(post.Path, 'D') {
allowed = true
}
if !allowed {
if post.AuthorID != authorID {
needCode = 'D'
}
return &PermissionError{Path: post.Path, Code: needCode}
}
var childCount int
err = k.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM posts WHERE parent_id = $1`, postID).Scan(&childCount)
if err != nil {
return fmt.Errorf("checking for replies: %w", err)
}
if childCount > 0 {
return ErrPostHasReplies
}
_, err = k.db.ExecContext(ctx, `DELETE FROM posts WHERE id = $1`, postID)
if err != nil {
return fmt.Errorf("deleting post: %w", err)
}
return nil
}
func (k *Karu) DeleteThread(ctx context.Context, postID, authorID, permString string) error {
perms, err := ParsePermissions(permString)
if err != nil {
return err
}
post, err := k.getPostByID(ctx, postID)
if err != nil {
if err == sql.ErrNoRows {
return ErrPostNotFound
}
return err
}
if post.ParentID != nil {
return fmt.Errorf("post is not a thread root")
}
allowed := false
needCode := byte('t')
if post.AuthorID == authorID && perms.Has(post.Path, 't') {
allowed = true
}
if perms.Has(post.Path, 'D') {
allowed = true
}
if !allowed {
if post.AuthorID != authorID {
needCode = 'D'
}
return &PermissionError{Path: post.Path, Code: needCode}
}
_, err = k.db.ExecContext(ctx,
`WITH RECURSIVE thread_ids AS (
SELECT id FROM posts WHERE id = $1
UNION ALL
SELECT p.id FROM posts p JOIN thread_ids t ON p.parent_id = t.id
)
DELETE FROM posts WHERE id IN (SELECT id FROM thread_ids)`,
postID)
if err != nil {
return fmt.Errorf("deleting thread: %w", err)
}
return nil
}