This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_export.go
438 lines (410 loc) · 9.16 KB
/
data_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
import (
"archive/zip"
"bytes"
"encoding/csv"
"encoding/json"
"io"
"log"
"os"
"strconv"
"time"
"github.com/minio/minio-go/v7"
"github.com/vmihailenco/msgpack/v5"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DataExport struct {
Id string `bson:"_id"`
User string `bson:"user"`
}
func (d *DataExport) execute() {
// Create ZIP file
archive, err := os.Create(os.Getenv("OUTPUT_DIR") + "/" + d.Id)
if err != nil {
d.markAsFailed(err)
return
}
defer archive.Close()
// Create ZIP writer
zipWriter := zip.NewWriter(archive)
defer zipWriter.Close()
// Run export
err = d.runExport(zipWriter)
if err != nil {
d.markAsFailed(err)
return
}
// Close ZIP writer and archive
err = zipWriter.Close()
if err != nil {
d.markAsFailed(err)
return
}
err = archive.Close()
if err != nil {
d.markAsFailed(err)
return
}
// Upload ZIP file to MinIO
_, err = s3.FPutObject(ctx, "data-exports", d.Id, os.Getenv("OUTPUT_DIR")+"/"+d.Id, minio.PutObjectOptions{
UserMetadata: map[string]string{
"user-id": d.User,
},
})
if err != nil {
d.markAsFailed(err)
return
}
// Remove ZIP file
err = os.Remove(os.Getenv("OUTPUT_DIR") + "/" + d.Id)
if err != nil {
d.markAsFailed(err)
return
}
// Mark as completed
err = d.markAsCompleted()
if err != nil {
d.markAsFailed(err)
return
}
}
func (d *DataExport) markAsCompleted() error {
// Update database item
_, err := mdb.Collection("data_exports").UpdateOne(ctx, bson.D{{Key: "_id", Value: d.Id}}, bson.D{{Key: "$set", Value: bson.D{
{Key: "status", Value: "completed"},
{Key: "completed_at", Value: time.Now().Unix()},
}}})
if err != nil {
return err
}
// Send inbox message
marshaledEvent, err := msgpack.Marshal(map[string]string{
"op": "alert_user",
"user": d.User,
"content": "Your data export is ready for download. You can download your data export from the [settings page](/settings) any time during the next 7 days.",
})
if err != nil {
return err
}
err = rdb.Publish(ctx, "admin", marshaledEvent).Err()
if err != nil {
return err
}
return err
}
func (d *DataExport) markAsFailed(err error) {
// Update database item
_, err = mdb.Collection("data_exports").UpdateOne(ctx, bson.D{{Key: "_id", Value: d.Id}}, bson.D{{Key: "$set", Value: bson.D{
{Key: "status", Value: "failed"},
{Key: "error", Value: err.Error()},
}}})
if err != nil {
log.Fatalln(err)
}
// Send inbox message
marshaledEvent, err := msgpack.Marshal(map[string]string{
"op": "alert_user",
"user": d.User,
"content": "Your data export failed. Please request another data export from the [settings page](/settings). If you continue to experience issues, please contact [support@meower.org](mailto:support@meower.org).",
})
if err != nil {
log.Fatalln(err)
}
err = rdb.Publish(ctx, "admin", marshaledEvent).Err()
if err != nil {
log.Fatalln(err)
}
}
func (d *DataExport) runExport(zipWriter *zip.Writer) error {
var err error
var cursor *mongo.Cursor
var fileWriter io.Writer
var csvWriter *csv.Writer
// Export account, settings, relationships, and netlog into user.json
var user User
cursor, err = mdb.Collection("usersv0").Aggregate(ctx, bson.A{
bson.D{{Key: "$match", Value: bson.D{{Key: "_id", Value: d.User}}}},
bson.D{
{Key: "$lookup",
Value: bson.D{
{Key: "from", Value: "user_settings"},
{Key: "localField", Value: "_id"},
{Key: "foreignField", Value: "_id"},
{Key: "as", Value: "settings"},
},
},
},
bson.D{
{Key: "$unwind",
Value: bson.D{
{Key: "path", Value: "$settings"},
{Key: "preserveNullAndEmptyArrays", Value: true},
},
},
},
bson.D{
{Key: "$lookup",
Value: bson.D{
{Key: "from", Value: "relationships"},
{Key: "localField", Value: "_id"},
{Key: "foreignField", Value: "_id.from"},
{Key: "as", Value: "relationships"},
},
},
},
bson.D{
{Key: "$lookup",
Value: bson.D{
{Key: "from", Value: "netlog"},
{Key: "localField", Value: "_id"},
{Key: "foreignField", Value: "_id.user"},
{Key: "as", Value: "netlogs"},
},
},
},
bson.D{
{Key: "$project",
Value: bson.D{
{Key: "pswd", Value: 0},
{Key: "tokens", Value: 0},
{Key: "settings._id", Value: 0},
{Key: "relationships._id.from", Value: 0},
{Key: "netlogs._id.user", Value: 0},
},
},
},
}, options.Aggregate())
if err != nil {
return err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
err = cursor.Decode(&user)
if err != nil {
return err
}
}
marshaledUser, err := json.MarshalIndent(user, "", "\t")
if err != nil {
return err
}
fileWriter, err = zipWriter.Create("user.json")
if err != nil {
return err
}
_, err = io.Copy(fileWriter, bytes.NewReader(marshaledUser))
if err != nil {
return err
}
zipWriter.Flush()
// Export reports
var reports []Report
cursor, err = mdb.Collection("reports").Find(ctx, bson.D{{Key: "reports.user", Value: d.User}}, options.Find())
if err != nil {
return err
}
err = cursor.All(ctx, &reports)
if err != nil {
return err
}
fileWriter, err = zipWriter.Create("safety/reports.csv")
if err != nil {
return err
}
csvWriter = csv.NewWriter(fileWriter)
csvWriter.Write([]string{
"id",
"type",
"content_id",
"status",
"ip",
"reason",
"comment",
"time",
})
for _, report := range reports {
for _, reporter := range report.Reporters {
if reporter.User == d.User {
csvWriter.Write([]string{
report.Id,
report.Type,
report.ContentId,
report.Status,
reporter.Ip,
reporter.Reason,
reporter.Comment,
strconv.FormatInt(reporter.Time, 10),
})
break
}
}
}
csvWriter.Flush()
zipWriter.Flush()
// Export chats
var chats []Chat
cursor, err = mdb.Collection("chats").Find(ctx, bson.D{{Key: "members", Value: d.User}}, options.Find())
if err != nil {
return err
}
err = cursor.All(ctx, &chats)
if err != nil {
return err
}
for _, chat := range chats {
marshaledChat, err := json.MarshalIndent(chat, "", "\t")
if err != nil {
return err
}
fileWriter, err = zipWriter.Create("chats/" + chat.Id + ".json")
if err != nil {
return err
}
_, err = io.Copy(fileWriter, bytes.NewReader(marshaledChat))
if err != nil {
return err
}
}
zipWriter.Flush()
// Get post origins
postOrigins, err := mdb.Collection("posts").Distinct(ctx, "post_origin", bson.D{{Key: "u", Value: d.User}}, options.Distinct())
if err != nil {
return err
}
// Export posts
for _, postOrigin := range postOrigins {
var posts []Post
cursor, err = mdb.Collection("posts").Aggregate(ctx, bson.A{
bson.D{{Key: "$match", Value: bson.D{
{Key: "u", Value: d.User},
{Key: "post_origin", Value: postOrigin},
}}},
bson.D{
{Key: "$lookup",
Value: bson.D{
{Key: "from", Value: "post_revisions"},
{Key: "localField", Value: "_id"},
{Key: "foreignField", Value: "post_id"},
{Key: "as", Value: "revisions"},
},
},
},
}, options.Aggregate())
if err != nil {
return err
}
err = cursor.All(ctx, &posts)
if err != nil {
return err
}
fileWriter, err = zipWriter.Create("posts/" + postOrigin.(string) + ".csv")
if err != nil {
return err
}
csvWriter = csv.NewWriter(fileWriter)
csvWriter.Write([]string{
"id",
"content",
"attachments",
"timestamp",
"revisions",
"edited_at",
"pinned",
"deleted",
"mod_deleted",
"deleted_at",
})
for _, post := range posts {
marshaledRevisions, err := json.Marshal(post.Revisions)
if err != nil {
return err
}
marshaledAttachments, err := json.Marshal(post.Attachments)
if err != nil {
return err
}
var editedAt string
if post.EditedAt != nil {
editedAt = strconv.FormatInt(*post.EditedAt, 10)
}
var deletedAt string
if post.DeletedAt != nil {
deletedAt = strconv.FormatInt(*post.DeletedAt, 10)
}
csvWriter.Write([]string{
post.Id,
post.Content,
string(marshaledAttachments),
strconv.FormatInt(post.Timestamp.Epoch, 10),
string(marshaledRevisions),
editedAt,
strconv.FormatBool(post.Pinned),
strconv.FormatBool(post.Deleted),
strconv.FormatBool(post.ModDeleted),
deletedAt,
})
}
csvWriter.Flush()
zipWriter.Flush()
}
// Export file uploads
fileWriter, err = zipWriter.Create("uploads.csv")
if err != nil {
return err
}
csvWriter = csv.NewWriter(fileWriter)
csvWriter.Write([]string{
"id",
"hash",
"bucket",
"filename",
"width",
"height",
"uploaded_at",
"claimed",
})
rows, err := udb.Query(`SELECT
id,
hash,
bucket,
filename,
width,
height,
uploaded_at,
claimed
FROM files WHERE uploaded_by=$1`, d.User)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var f FileUpload
err = rows.Scan(
&f.Id,
&f.Hash,
&f.Bucket,
&f.Filename,
&f.Width,
&f.Height,
&f.UploadedAt,
&f.Claimed,
)
if err != nil {
return err
}
csvWriter.Write([]string{
f.Id,
f.Hash,
f.Bucket,
f.Filename,
strconv.Itoa(f.Width),
strconv.Itoa(f.Height),
strconv.FormatInt(f.UploadedAt, 10),
strconv.FormatBool(f.Claimed),
})
}
csvWriter.Flush()
return nil
}