-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_export.go
145 lines (123 loc) · 4.2 KB
/
domain_export.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
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 main
import (
"encoding/json"
"net/http"
"time"
)
func domainExportBeginError(email string, toName string, domain string, err error) {
// we're not using err at the moment because it's all errorInternal
if err2 := smtpDomainExportError(email, toName, domain); err2 != nil {
logger.Errorf("cannot send domain export error email for %s: %v", domain, err2)
return
}
}
func domainExportBegin(email string, toName string, domain string) {
e := commentoExportV1{Version: 1, Comments: []comment{}, Commenters: []commenter{}}
statement := `
SELECT commentHex, domain, path, commenterHex, markdown, parentHex, score, state, creationDate
FROM comments
WHERE domain = $1;
`
rows1, err := db.Query(statement, domain)
if err != nil {
logger.Errorf("cannot select comments while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
defer rows1.Close()
for rows1.Next() {
c := comment{}
if err = rows1.Scan(&c.CommentHex, &c.Domain, &c.Path, &c.CommenterHex, &c.Markdown, &c.ParentHex, &c.Score, &c.State, &c.CreationDate); err != nil {
logger.Errorf("cannot scan comment while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
e.Comments = append(e.Comments, c)
}
statement = `
SELECT commenters.commenterHex, commenters.email, commenters.name, commenters.link, commenters.photo, commenters.provider, commenters.joinDate
FROM commenters, comments
WHERE comments.domain = $1 AND commenters.commenterHex = comments.commenterHex;
`
rows2, err := db.Query(statement, domain)
if err != nil {
logger.Errorf("cannot select commenters while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
defer rows2.Close()
for rows2.Next() {
c := commenter{}
if err := rows2.Scan(&c.CommenterHex, &c.Email, &c.Name, &c.Link, &c.Photo, &c.Provider, &c.JoinDate); err != nil {
logger.Errorf("cannot scan commenter while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
e.Commenters = append(e.Commenters, c)
}
je, err := json.Marshal(e)
if err != nil {
logger.Errorf("cannot marshall JSON while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
gje, err := gzipStatic(je)
if err != nil {
logger.Errorf("cannot gzip JSON while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
exportHex, err := randomHex(32)
if err != nil {
logger.Errorf("cannot generate exportHex while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
statement = `
INSERT INTO
exports (exportHex, binData, domain, creationDate)
VALUES ($1, $2, $3 , $4 );
`
_, err = db.Exec(statement, exportHex, gje, domain, time.Now().UTC())
if err != nil {
logger.Errorf("error inserting expiry binary data while exporting %s: %v", domain, err)
domainExportBeginError(email, toName, domain, errorInternal)
return
}
err = smtpDomainExport(email, toName, domain, exportHex)
if err != nil {
logger.Errorf("error sending data export email for %s: %v", domain, err)
return
}
}
func domainExportBeginHandler(w http.ResponseWriter, r *http.Request) {
type request struct {
OwnerToken *string `json:"ownerToken"`
Domain *string `json:"domain"`
}
var x request
if err := bodyUnmarshal(r, &x); err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
if !smtpConfigured {
bodyMarshal(w, response{"success": false, "message": errorSmtpNotConfigured.Error()})
return
}
o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
isOwner, err := domainOwnershipVerify(o.OwnerHex, *x.Domain)
if err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()})
return
}
if !isOwner {
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return
}
go domainExportBegin(o.Email, o.Name, *x.Domain)
bodyMarshal(w, response{"success": true})
}