summaryrefslogtreecommitdiff
path: root/internal/typeutils/csv.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2024-08-02 13:41:46 +0200
committerLibravatar GitHub <noreply@github.com>2024-08-02 12:41:46 +0100
commit7b5917d6ae48f83c92f92d7277960cfa6ae8ec56 (patch)
tree93ee6999195060714f41f9b9476d4d76ad50520c /internal/typeutils/csv.go
parent[chore] Take account of rotation data when calculating full size image dimens... (diff)
downloadgotosocial-7b5917d6ae48f83c92f92d7277960cfa6ae8ec56.tar.xz
[feature] Allow import of following and blocks via CSV (#3150)
* [feature] Import follows + blocks via settings panel * test import follows
Diffstat (limited to 'internal/typeutils/csv.go')
-rw-r--r--internal/typeutils/csv.go135
1 files changed, 135 insertions, 0 deletions
diff --git a/internal/typeutils/csv.go b/internal/typeutils/csv.go
index 2ef56cb0c..063e31d54 100644
--- a/internal/typeutils/csv.go
+++ b/internal/typeutils/csv.go
@@ -26,6 +26,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
)
func (c *Converter) AccountToExportStats(
@@ -383,3 +384,137 @@ func (c *Converter) MutesToCSV(
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 {
+ if len(record) != 2 {
+ // 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 = ""
+ }
+
+ showReblogs, err := strconv.ParseBool(record[1])
+ if err != nil {
+ // Badly formatted,
+ // skip this one.
+ continue
+ }
+
+ // Looks good, whack it in the slice.
+ follows = append(follows, &gtsmodel.Follow{
+ TargetAccount: &gtsmodel.Account{
+ Username: username,
+ Domain: domain,
+ },
+ ShowReblogs: &showReblogs,
+ })
+ }
+
+ 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
+}