-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
118 lines (109 loc) · 2.52 KB
/
report.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
package main
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"net/url"
"strings"
"codeberg.org/FiskFan1999/gemini"
bolt "go.etcd.io/bbolt"
)
var AlreadyReported = errors.New("Already reported this post")
func ReportHandler(u *url.URL, c *tls.Conn) gemini.ResponseFormat {
fp := GetFingerprint(c)
if fp == nil {
return CertRequired
}
username, _, _, _ := GetUsernameFromFP(fp)
if username == "" {
return UnauthorizedCert
}
pathspl := strings.FieldsFunc(u.EscapedPath(), func(r rune) bool { return r == '/' })
if len(pathspl) != 2 {
return gemini.ResponseFormat{
Status: gemini.BadRequest,
Mime: "Bad Request",
Lines: nil,
}
}
if u.RawQuery == "" {
return gemini.ResponseFormat{
Status: gemini.Input,
Mime: "Reason for report",
Lines: nil,
}
}
/*
Run on query input
*/
id := pathspl[1]
reason, err := url.QueryUnescape(u.RawQuery)
if err != nil {
return gemini.ResponseFormat{
Status: gemini.TemporaryFailure,
Mime: err.Error(),
Lines: nil,
}
}
/*
Get information about the post from the database
*/
var post Post
if err := db.Update(func(tx *bolt.Tx) error {
allPosts := tx.Bucket(DBALLPOSTS)
thisPost := allPosts.Bucket([]byte(id))
if thisPost == nil {
// post doesn't exist
return ErrNotFound
}
if !bytes.Equal(thisPost.Get([]byte("reports")), []byte("0")) {
return AlreadyReported
} else {
thisPost.Put([]byte("reports"), []byte("1"))
}
post.ID = []byte(id)
post.Text = string(thisPost.Get([]byte("text")))
post.Author = string(thisPost.Get([]byte("user")))
if err := (&post.Time).UnmarshalText(thisPost.Get([]byte("time"))); err != nil {
return err
}
return nil
}); errors.Is(err, AlreadyReported) {
return gemini.ResponseFormat{
Status: gemini.BadRequest,
Mime: "Post has already been reported. Thank you.",
Lines: nil,
}
} else if err != nil {
return gemini.ResponseFormat{
Status: gemini.BadRequest,
Mime: err.Error(),
Lines: nil,
}
}
// send email to administrators
if err := SendEmailOnReport(post, username, reason, c); err != nil {
if errors.Is(err, reportRateLimited) {
return gemini.ResponseFormat{
Status: gemini.SlowDown,
Mime: "60",
Lines: nil,
}
} else {
return gemini.ResponseFormat{
Status: gemini.TemporaryFailure,
Mime: err.Error(),
Lines: nil,
}
}
}
return gemini.ResponseFormat{
Status: gemini.Success,
Mime: "text/gemini",
Lines: gemini.Lines{
"Thank you for your report.",
fmt.Sprintf("%s/ Go to home.", gemini.Link),
},
}
}