summaryrefslogtreecommitdiff
path: root/internal/typeutils/csv.go
blob: 7211d5c9cb7aaecd3b6e6d22dda2ef496d99a42a (plain)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package typeutils

import (
	"cmp"
	"context"
	"slices"
	"strconv"

	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/config"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/util"
)

func (c *Converter) AccountToExportStats(
	ctx context.Context,
	a *gtsmodel.Account,
) (*apimodel.AccountExportStats, error) {
	// Ensure account stats populated.
	if err := c.state.DB.PopulateAccountStats(ctx, a); err != nil {
		return nil, gtserror.Newf(
			"error getting stats for account %s: %w",
			a.ID, err,
		)
	}

	listsCount, err := c.state.DB.CountListsByAccountID(ctx, a.ID)
	if err != nil {
		return nil, gtserror.Newf(
			"error counting lists for account %s: %w",
			a.ID, err,
		)
	}

	blockingCount, err := c.state.DB.CountAccountBlocks(ctx, a.ID)
	if err != nil {
		return nil, gtserror.Newf(
			"error counting lists for account %s: %w",
			a.ID, err,
		)
	}

	mutingCount, err := c.state.DB.CountAccountMutes(ctx, a.ID)
	if err != nil {
		return nil, gtserror.Newf(
			"error counting lists for account %s: %w",
			a.ID, err,
		)
	}

	return &apimodel.AccountExportStats{
		FollowersCount: *a.Stats.FollowersCount,
		FollowingCount: *a.Stats.FollowingCount,
		StatusesCount:  *a.Stats.StatusesCount,
		ListsCount:     listsCount,
		BlocksCount:    blockingCount,
		MutesCount:     mutingCount,
	}, nil
}

// FollowingToCSV converts a slice of follows into
// a slice of CSV-compatible Following records.
//
// Each follow should be populated.
func (c *Converter) FollowingToCSV(
	ctx context.Context,
	following []*gtsmodel.Follow,
) ([][]string, error) {
	// Records should be length of
	// input + 1 so we can add headers.
	records := make([][]string, 1, len(following)+1)

	// Add headers at the
	// top of records.
	records[0] = []string{
		"Account address",
		"Show boosts",
		"Notify on new posts",
		"Languages",
	}

	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	thisDomain := config.GetAccountDomain()
	if thisDomain == "" {
		thisDomain = config.GetHost()
	}

	// Pre-sort the follows
	// by domain and username.
	slices.SortFunc(
		following,
		func(a *gtsmodel.Follow, b *gtsmodel.Follow) int {
			aStr := a.TargetAccount.Domain + "/" + a.TargetAccount.Username
			bStr := b.TargetAccount.Domain + "/" + b.TargetAccount.Username
			return cmp.Compare(aStr, bStr)
		},
	)

	// For each item, add a record.
	for _, follow := range following {
		domain := follow.TargetAccount.Domain
		if domain == "" {
			// Local account,
			// use our domain.
			domain = thisDomain
		}

		records = append(records, []string{
			// Account address: eg., someone@example.org
			// -- NOTE: without the leading '@'!
			follow.TargetAccount.Username + "@" + domain,
			// Show boosts: eg., true
			strconv.FormatBool(*follow.ShowReblogs),
			// Notify on new posts, eg., true
			strconv.FormatBool(*follow.Notify),
			// Languages: compat only, leave blank.
			"",
		})
	}

	return records, nil
}

// FollowersToCSV converts a slice of follows into
// a slice of CSV-compatible Followers records.
//
// Each follow should be populated.
func (c *Converter) FollowersToCSV(
	ctx context.Context,
	followers []*gtsmodel.Follow,
) ([][]string, error) {
	// Records should be length of
	// input + 1 so we can add headers.
	records := make([][]string, 1, len(followers)+1)

	// Add header at the
	// top of records.
	records[0] = []string{
		"Account address",
	}

	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	thisDomain := config.GetAccountDomain()
	if thisDomain == "" {
		thisDomain = config.GetHost()
	}

	// Pre-sort the follows
	// by domain and username.
	slices.SortFunc(
		followers,
		func(a *gtsmodel.Follow, b *gtsmodel.Follow) int {
			aStr := a.Account.Domain + "/" + a.Account.Username
			bStr := b.Account.Domain + "/" + b.Account.Username
			return cmp.Compare(aStr, bStr)
		},
	)

	// For each item, add a record.
	for _, follow := range followers {
		domain := follow.Account.Domain
		if domain == "" {
			// Local account,
			// use our domain.
			domain = thisDomain
		}

		records = append(records, []string{
			// Account address: eg., someone@example.org
			// -- NOTE: without the leading '@'!
			follow.Account.Username + "@" + domain,
		})
	}

	return records, nil
}

// FollowersToCSV converts a slice of follows into
// a slice of CSV-compatible Followers records.
func (c *Converter) ListsToCSV(
	ctx context.Context,
	lists []*gtsmodel.List,
) ([][]string, error) {

	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	thisDomain := config.GetAccountDomain()
	if thisDomain == "" {
		thisDomain = config.GetHost()
	}

	// NOTE: Mastodon-compatible lists
	// CSV doesn't use column headers.
	records := make([][]string, 0)

	// Pre-sort the lists
	// alphabetically.
	slices.SortFunc(
		lists,
		func(a *gtsmodel.List, b *gtsmodel.List) int {
			return cmp.Compare(a.Title, b.Title)
		},
	)

	// For each item, add a record.
	for _, list := range lists {

		// Get all follows contained with this list.
		follows, err := c.state.DB.GetFollowsInList(ctx,
			list.ID,
			nil,
		)
		if err != nil {
			err := gtserror.Newf("db error getting follows for list: %w", err)
			return nil, err
		}

		// Pre-sort the follows
		// by domain and username.
		slices.SortFunc(
			follows,
			func(a *gtsmodel.Follow, b *gtsmodel.Follow) int {
				aStr := a.TargetAccount.Domain + "/" + a.TargetAccount.Username
				bStr := b.TargetAccount.Domain + "/" + b.TargetAccount.Username
				return cmp.Compare(aStr, bStr)
			},
		)

		// Append each follow as CSV record.
		for _, follow := range follows {
			var (
				// Extract username / domain from target.
				username = follow.TargetAccount.Username
				domain   = follow.TargetAccount.Domain
			)

			if domain == "" {
				// Local account,
				// use our domain.
				domain = thisDomain
			}

			records = append(records, []string{
				// List title: e.g.
				// Very cool list
				list.Title,

				// Account address: e.g.,
				// someone@example.org
				// NOTE: without the leading '@'!
				username + "@" + domain,
			})
		}
	}

	return records, nil
}

// BlocksToCSV converts a slice of blocks into
// a slice of CSV-compatible blocks records.
//
// Each block should be populated.
func (c *Converter) BlocksToCSV(
	ctx context.Context,
	blocks []*gtsmodel.Block,
) ([][]string, error) {
	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	thisDomain := config.GetAccountDomain()
	if thisDomain == "" {
		thisDomain = config.GetHost()
	}

	// NOTE: Mastodon-compatible blocks
	// CSV doesn't use column headers.
	records := make([][]string, 0, len(blocks))

	// Pre-sort the blocks
	// by domain and username.
	slices.SortFunc(
		blocks,
		func(a *gtsmodel.Block, b *gtsmodel.Block) int {
			aStr := a.TargetAccount.Domain + "/" + a.TargetAccount.Username
			bStr := b.TargetAccount.Domain + "/" + b.TargetAccount.Username
			return cmp.Compare(aStr, bStr)
		},
	)

	// For each item, add a record.
	for _, block := range blocks {
		domain := block.TargetAccount.Domain
		if domain == "" {
			// Local account,
			// use our domain.
			domain = thisDomain
		}

		records = append(records, []string{
			// Account address: eg., someone@example.org
			// -- NOTE: without the leading '@'!
			block.TargetAccount.Username + "@" + domain,
		})
	}

	return records, nil
}

// MutesToCSV converts a slice of mutes into
// a slice of CSV-compatible mute records.
//
// Each mute should be populated.
func (c *Converter) MutesToCSV(
	ctx context.Context,
	mutes []*gtsmodel.UserMute,
) ([][]string, error) {
	// Records should be length of
	// input + 1 so we can add headers.
	records := make([][]string, 1, len(mutes)+1)

	// Add headers at the
	// top of records.
	records[0] = []string{
		"Account address",
		"Hide notifications",
	}

	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	thisDomain := config.GetAccountDomain()
	if thisDomain == "" {
		thisDomain = config.GetHost()
	}

	// Pre-sort the mutes
	// by domain and username.
	slices.SortFunc(
		mutes,
		func(a *gtsmodel.UserMute, b *gtsmodel.UserMute) int {
			aStr := a.TargetAccount.Domain + "/" + a.TargetAccount.Username
			bStr := b.TargetAccount.Domain + "/" + b.TargetAccount.Username
			return cmp.Compare(aStr, bStr)
		},
	)

	// For each item, add a record.
	for _, mute := range mutes {
		domain := mute.TargetAccount.Domain
		if domain == "" {
			// Local account,
			// use our domain.
			domain = thisDomain
		}

		records = append(records, []string{
			// Account address: eg., someone@example.org
			// -- NOTE: without the leading '@'!
			mute.TargetAccount.Username + "@" + domain,
			// Hide notifications: eg., true
			strconv.FormatBool(*mute.Notifications),
		})
	}

	return records, nil
}

// CSVToFollowing converts a slice of CSV records
// to a slice of barebones *gtsmodel.Follow's,
// ready for further processing.
//
// Only TargetAccount.Username, TargetAccount.Domain,
// and ShowReblogs will be set on each Follow.
func (c *Converter) CSVToFollowing(
	ctx context.Context,
	records [][]string,
) ([]*gtsmodel.Follow, error) {
	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	var (
		thisHost          = config.GetHost()
		thisAccountDomain = config.GetAccountDomain()
		follows           = make([]*gtsmodel.Follow, 0, len(records))
	)

	for _, record := range records {
		recordLen := len(record)

		// Older versions of this Masto CSV
		// schema may not include "Show boosts",
		// "Notify on new posts", or "Languages",
		// so be lenient here in what we accept.
		if recordLen == 0 ||
			recordLen > 4 {
			// Badly formatted,
			// skip this one.
			continue
		}

		// "Account address"
		namestring := record[0]
		if namestring == "" {
			// Badly formatted,
			// skip this one.
			continue
		}

		if namestring == "Account address" {
			// CSV header row,
			// skip this one.
			continue
		}

		// Prepend with "@"
		// if not included.
		if namestring[0] != '@' {
			namestring = "@" + namestring
		}

		username, domain, err := util.ExtractNamestringParts(namestring)
		if err != nil {
			// Badly formatted,
			// skip this one.
			continue
		}

		if domain == thisHost || domain == thisAccountDomain {
			// Clear the domain,
			// since it's ours.
			domain = ""
		}

		// "Show boosts"
		var showReblogs *bool
		if recordLen > 1 {
			b, err := strconv.ParseBool(record[1])
			if err != nil {
				// Badly formatted,
				// skip this one.
				continue
			}
			showReblogs = &b
		}

		// "Notify on new posts"
		var notify *bool
		if recordLen > 2 {
			b, err := strconv.ParseBool(record[2])
			if err != nil {
				// Badly formatted,
				// skip this one.
				continue
			}
			notify = &b
		}

		// TODO: "Languages"
		//
		// Ignore this for now as we
		// don't do anything with it.

		// Looks good, whack it in the slice.
		follows = append(follows, &gtsmodel.Follow{
			TargetAccount: &gtsmodel.Account{
				Username: username,
				Domain:   domain,
			},
			ShowReblogs: showReblogs,
			Notify:      notify,
		})
	}

	return follows, nil
}

// CSVToBlocks converts a slice of CSV records
// to a slice of barebones *gtsmodel.Block's,
// ready for further processing.
//
// Only TargetAccount.Username and TargetAccount.Domain
// will be set on each Block.
func (c *Converter) CSVToBlocks(
	ctx context.Context,
	records [][]string,
) ([]*gtsmodel.Block, error) {
	// We need to know our own domain for this.
	// Try account domain, fall back to host.
	var (
		thisHost          = config.GetHost()
		thisAccountDomain = config.GetAccountDomain()
		blocks            = make([]*gtsmodel.Block, 0, len(records))
	)

	for _, record := range records {
		if len(record) != 1 {
			// Badly formatted,
			// skip this one.
			continue
		}

		namestring := record[0]
		if namestring == "" {
			// Badly formatted,
			// skip this one.
			continue
		}

		// Prepend with "@"
		// if not included.
		if namestring[0] != '@' {
			namestring = "@" + namestring
		}

		username, domain, err := util.ExtractNamestringParts(namestring)
		if err != nil {
			// Badly formatted,
			// skip this one.
			continue
		}

		if domain == thisHost || domain == thisAccountDomain {
			// Clear the domain,
			// since it's ours.
			domain = ""
		}

		// Looks good, whack it in the slice.
		blocks = append(blocks, &gtsmodel.Block{
			TargetAccount: &gtsmodel.Account{
				Username: username,
				Domain:   domain,
			},
		})
	}

	return blocks, nil
}