-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathedit.go
120 lines (100 loc) · 3.42 KB
/
edit.go
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
/*
Copyright 2023, 2024 Dima Krasner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package front
import (
"database/sql"
"encoding/json"
"errors"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/front/text"
"math"
"net/url"
"time"
"unicode/utf8"
)
func (h *Handler) edit(w text.Writer, r *request, args ...string) {
if r.User == nil {
w.Redirect("/users")
return
}
if r.URL.RawQuery == "" {
w.Status(10, "Post content")
return
}
content, err := url.QueryUnescape(r.URL.RawQuery)
if err != nil {
w.Status(40, "Bad input")
return
}
if utf8.RuneCountInString(content) > h.Config.MaxPostsLength {
w.Status(40, "Post is too long")
return
}
postID := "https://" + args[1]
var noteString string
if err := r.QueryRow(`select object from notes where id = ? and author = ?`, postID, r.User.ID).Scan(¬eString); err != nil && errors.Is(err, sql.ErrNoRows) {
r.Log.Warn("Attempted to edit non-existing post", "post", postID, "error", err)
w.Error()
return
} else if err != nil {
r.Log.Warn("Failed to fetch post to edit", "post", postID, "error", err)
w.Error()
return
}
var note ap.Object
if err := json.Unmarshal([]byte(noteString), ¬e); err != nil {
r.Log.Warn("Failed to unmarshal post to edit", "post", postID, "error", err)
w.Error()
return
}
if note.Name != "" {
r.Log.Warn("Cannot edit votes", "vote", note.ID)
w.Status(40, "Cannot edit votes")
return
}
var edits int
if err := r.QueryRow(`select count(*) from outbox where activity->>'object.id' = ? and (activity->>'type' = 'Update' or activity->>'type' = 'Create')`, note.ID).Scan(&edits); err != nil {
r.Log.Warn("Failed to count post edits", "post", postID, "error", err)
w.Error()
return
}
lastEditTime := note.Published
if note.Updated != nil && *note.Updated != (ap.Time{}) {
lastEditTime = *note.Updated
}
canEdit := lastEditTime.Add(h.Config.EditThrottleUnit * time.Duration(math.Pow(h.Config.EditThrottleFactor, float64(edits))))
if time.Now().Before(canEdit) {
r.Log.Warn("Throttled request to edit post", "note", note.ID, "can", canEdit)
w.Status(40, "Please try again later")
return
}
if note.InReplyTo == "" {
h.post(w, r, ¬e, nil, note.To, note.CC, note.Audience, "Post content")
return
}
if err := r.QueryRow(`select object from notes where id = ?`, note.InReplyTo).Scan(¬eString); err != nil && errors.Is(err, sql.ErrNoRows) {
r.Log.Warn("Parent post does not exist", "parent", note.InReplyTo)
} else if err != nil {
r.Log.Warn("Failed to fetch parent post", "parent", note.InReplyTo, "error", err)
w.Error()
return
}
var parent ap.Object
if err := json.Unmarshal([]byte(noteString), &parent); err != nil {
r.Log.Warn("Failed to unmarshal parent post", "parent", note.InReplyTo, "error", err)
w.Error()
return
}
// the starting point is the original value of to and cc: recipients can be added but not removed when editing
h.post(w, r, ¬e, &parent, note.To, note.CC, note.Audience, "Post content")
}