all repos — honk @ 7d1e2617a8f139ab7c225d01d673b354a34ecdda

my fork of honk

import following from mastodon.
adapted from patch from peter sanchez
Ted Unangst tedu@tedunangst.com
Thu, 28 Apr 2022 15:38:51 -0400
commit

7d1e2617a8f139ab7c225d01d673b354a34ecdda

parent

1dab6623e5cfb202c33a976d3dd63f6f89f6473a

3 files changed, 95 insertions(+), 43 deletions(-)

jump to
M database.godatabase.go

@@ -972,6 +972,52 @@ func savexonker(what, value, flav, when string) {

stmtSaveXonker.Exec(what, value, flav, when) } +func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) error { + var owner string + if url[0] == '#' { + flavor = "peep" + if name == "" { + name = url[1:] + } + owner = url + } else { + info, err := investigate(url) + if err != nil { + ilog.Printf("failed to investigate honker: %s", err) + return err + } + url = info.XID + if name == "" { + name = info.Name + } + owner = info.Owner + } + + var x string + db := opendatabase() + row := db.QueryRow("select xid from honkers where xid = ? and userid = ? and flavor in ('sub', 'unsub', 'peep')", url, user.ID) + err := row.Scan(&x) + if err != sql.ErrNoRows { + if err != nil { + elog.Printf("honker scan err: %s", err) + } else { + err = fmt.Errorf("it seems you are already subscribed to them") + } + return err + } + + res, err := stmtSaveHonker.Exec(user.ID, name, url, flavor, combos, owner, mj) + if err != nil { + elog.Print(err) + return err + } + honkerid, _ := res.LastInsertId() + if flavor == "presub" { + followyou(user, honkerid) + } + return nil +} + func cleanupdb(arg string) { db := opendatabase() days, err := strconv.Atoi(arg)
M import.goimport.go

@@ -16,6 +16,7 @@

package main import ( + "encoding/csv" "encoding/json" "fmt" "html"

@@ -69,6 +70,20 @@ user, err := butwhatabout(username)

if err != nil { elog.Fatal(err) } + + if _, err := os.Stat(source + "/outbox.json"); err == nil { + importMastotoots(user, source) + } else { + ilog.Printf("skipping outbox.json!") + } + if _, err := os.Stat(source + "/following_accounts.csv"); err == nil { + importMastotooters(user, source) + } else { + ilog.Printf("skipping following_accounts.csv!") + } +} + +func importMastotoots(user *WhatAbout, source string) { type Toot struct { Id string Type string

@@ -79,6 +94,7 @@ }

var outbox struct { OrderedItems []Toot } + ilog.Println("Importing honks...") fd, err := os.Open(source + "/outbox.json") if err != nil { elog.Fatal(err)

@@ -166,6 +182,37 @@ honk.Onts = append(honk.Onts, t.Name)

} } savehonk(&honk) + } +} + +func importMastotooters(user *WhatAbout, source string) { + ilog.Println("Importing honkers...") + fd, err := os.Open(source + "/following_accounts.csv") + if err != nil { + elog.Fatal(err) + } + r := csv.NewReader(fd) + data, err := r.ReadAll() + if err != nil { + elog.Fatal(err) + } + fd.Close() + + var meta HonkerMeta + mj, _ := jsonify(&meta) + + for i, d := range data { + if i == 0 { + continue + } + url := "@" + d[0] + name := "" + flavor := "peep" + combos := "" + err := savehonker(user, url, name, flavor, combos, mj) + if err != nil { + elog.Printf("trouble with a honker: %s", err) + } } }
M web.goweb.go

@@ -1923,53 +1923,12 @@ if peep == "peep" {

flavor = "peep" } - if url[0] == '#' { - flavor = "peep" - if name == "" { - name = url[1:] - } - _, err := stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos, url, mj) - if err != nil { - elog.Print(err) - return - } - http.Redirect(w, r, "/honkers", http.StatusSeeOther) - return - } - - info, err := investigate(url) + err := savehonker(user, url, name, flavor, combos, mj) if err != nil { - http.Error(w, "error investigating: "+err.Error(), http.StatusInternalServerError) - ilog.Printf("failed to investigate honker: %s", err) + http.Error(w, "had some trouble with that: "+err.Error(), http.StatusInternalServerError) return } - url = info.XID - if name == "" { - name = info.Name - } - - var x string - db := opendatabase() - row := db.QueryRow("select xid from honkers where xid = ? and userid = ? and flavor in ('sub', 'unsub', 'peep')", url, u.UserID) - err = row.Scan(&x) - if err != sql.ErrNoRows { - http.Error(w, "it seems you are already subscribed to them", http.StatusInternalServerError) - if err != nil { - elog.Printf("honker scan err: %s", err) - } - return - } - - res, err := stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos, info.Owner, mj) - if err != nil { - elog.Print(err) - return - } - honkerid, _ = res.LastInsertId() - if flavor == "presub" { - followyou(user, honkerid) - } http.Redirect(w, r, "/honkers", http.StatusSeeOther) }