import following from mastodon.

adapted from patch from peter sanchez
This commit is contained in:
Ted Unangst 2022-04-28 15:38:51 -04:00
parent c0fc930e36
commit bbf1fd9b9a
3 changed files with 95 additions and 43 deletions

View File

@ -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)

View File

@ -16,6 +16,7 @@
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"html"
@ -69,6 +70,20 @@ func importMastodon(username, source string) {
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 @@ func importMastodon(username, source string) {
var outbox struct {
OrderedItems []Toot
}
ilog.Println("Importing honks...")
fd, err := os.Open(source + "/outbox.json")
if err != nil {
elog.Fatal(err)
@ -169,6 +185,37 @@ func importMastodon(username, source string) {
}
}
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)
}
}
}
func importTwitter(username, source string) {
user, err := butwhatabout(username)
if err != nil {

45
web.go
View File

@ -1923,53 +1923,12 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
flavor = "peep"
}
if url[0] == '#' {
flavor = "peep"
if name == "" {
name = url[1:]
}
_, err := stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos, url, mj)
err := savehonker(user, url, name, flavor, combos, mj)
if err != nil {
elog.Print(err)
return
}
http.Redirect(w, r, "/honkers", http.StatusSeeOther)
http.Error(w, "had some trouble with that: "+err.Error(), http.StatusInternalServerError)
return
}
info, err := investigate(url)
if err != nil {
http.Error(w, "error investigating: "+err.Error(), http.StatusInternalServerError)
ilog.Printf("failed to investigate honker: %s", err)
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)
}