From a79144aad1fd30e504f940d58c73b4946a27c77a Mon Sep 17 00:00:00 2001 From: tobi Date: Mon, 16 Sep 2024 20:14:36 +0200 Subject: [PATCH] [bugfix] Be more lenient when parsing mastodown following.csv --- internal/api/client/exports/exports_test.go | 6 +-- internal/typeutils/csv.go | 57 ++++++++++++++++++--- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/internal/api/client/exports/exports_test.go b/internal/api/client/exports/exports_test.go index 1943f2582..bfd23c9a1 100644 --- a/internal/api/client/exports/exports_test.go +++ b/internal/api/client/exports/exports_test.go @@ -160,9 +160,9 @@ func (suite *ExportsTestSuite) TestExports() { token: suite.testTokens["local_account_1"], user: suite.testUsers["local_account_1"], account: suite.testAccounts["local_account_1"], - expect: `Account address,Show boosts -admin@localhost:8080,true -1happyturtle@localhost:8080,true + expect: `Account address,Show boosts,Notify on new posts,Languages +admin@localhost:8080,true,false, +1happyturtle@localhost:8080,true,false, `, }, // Export Followers. diff --git a/internal/typeutils/csv.go b/internal/typeutils/csv.go index 063e31d54..bfffaba62 100644 --- a/internal/typeutils/csv.go +++ b/internal/typeutils/csv.go @@ -92,6 +92,8 @@ func (c *Converter) FollowingToCSV( records[0] = []string{ "Account address", "Show boosts", + "Notify on new posts", + "Languages", } // We need to know our own domain for this. @@ -132,6 +134,10 @@ func (c *Converter) FollowingToCSV( 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. + "", }) } @@ -404,12 +410,20 @@ func (c *Converter) CSVToFollowing( ) for _, record := range records { - if len(record) != 2 { + 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, @@ -417,6 +431,12 @@ func (c *Converter) CSVToFollowing( continue } + if namestring == "Account address" { + // CSV header row, + // skip this one. + continue + } + // Prepend with "@" // if not included. if namestring[0] != '@' { @@ -436,20 +456,43 @@ func (c *Converter) CSVToFollowing( domain = "" } - showReblogs, err := strconv.ParseBool(record[1]) - if err != nil { - // Badly formatted, - // skip this one. - continue + // "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, >smodel.Follow{ TargetAccount: >smodel.Account{ Username: username, Domain: domain, }, - ShowReblogs: &showReblogs, + ShowReblogs: showReblogs, + Notify: notify, }) }