some code towards enabling following a collection

This commit is contained in:
Ted Unangst 2019-10-26 18:31:42 -04:00
parent 4ee177e157
commit 58292a808c
4 changed files with 68 additions and 38 deletions

View File

@ -946,7 +946,7 @@ func itakeitallback(user *WhatAbout, xid string) {
deliverate(0, user.ID, xid, j.ToBytes()) deliverate(0, user.ID, xid, j.ToBytes())
} }
func subsub(user *WhatAbout, xid string) { func subsub(user *WhatAbout, xid string, owner string) {
if xid == "" { if xid == "" {
log.Printf("can't subscribe to empty") log.Printf("can't subscribe to empty")
return return
@ -956,15 +956,11 @@ func subsub(user *WhatAbout, xid string) {
j["id"] = user.URL + "/sub/" + url.QueryEscape(xid) j["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
j["type"] = "Follow" j["type"] = "Follow"
j["actor"] = user.URL j["actor"] = user.URL
j["to"] = xid j["to"] = owner
j["object"] = xid j["object"] = xid
j["published"] = time.Now().UTC().Format(time.RFC3339) j["published"] = time.Now().UTC().Format(time.RFC3339)
var buf bytes.Buffer deliverate(0, user.ID, owner, j.ToBytes())
j.Write(&buf)
msg := buf.Bytes()
deliverate(0, user.ID, xid, msg)
} }
// returns activity, object // returns activity, object
@ -1335,19 +1331,7 @@ func gofish(name string) string {
return href return href
} }
func isactor(t string) bool { func investigate(name string) (*SomeThing, error) {
switch t {
case "Person":
case "Organization":
case "Application":
case "Service":
default:
return false
}
return true
}
func investigate(name string) (*Honker, error) {
if name == "" { if name == "" {
return nil, fmt.Errorf("no name") return nil, fmt.Errorf("no name")
} }
@ -1361,11 +1345,36 @@ func investigate(name string) (*Honker, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
t, _ := obj.GetString("type") return somethingabout(obj)
if !isactor(t) { }
return nil, fmt.Errorf("not a person")
} func somethingabout(obj junk.Junk) (*SomeThing, error) {
xid, _ := obj.GetString("id") info := new(SomeThing)
handle, _ := obj.GetString("preferredUsername") t, _ := obj.GetString("type")
return &Honker{XID: xid, Handle: handle}, nil switch t {
case "Person":
fallthrough
case "Organization":
fallthrough
case "Application":
fallthrough
case "Service":
info.What = SomeActor
case "OrderedCollection":
fallthrough
case "Collection":
info.What = SomeCollection
default:
return nil, fmt.Errorf("unknown object type")
}
info.XID, _ = obj.GetString("id")
info.Name, _ = obj.GetString("preferredUsername")
if info.Name == "" {
info.Name, _ = obj.GetString("name")
}
info.Owner, _ = obj.GetString("attributedTo")
if info.Owner == "" {
info.Owner = info.XID
}
return info, nil
} }

6
fun.go
View File

@ -514,8 +514,8 @@ func findhandle(xid string) string {
var handle string var handle string
err := row.Scan(&handle) err := row.Scan(&handle)
if err != nil { if err != nil {
p, _ := investigate(xid) info, _ := investigate(xid)
if p == nil { if info == nil {
m := re_unurl.FindStringSubmatch(xid) m := re_unurl.FindStringSubmatch(xid)
if len(m) > 2 { if len(m) > 2 {
handle = m[2] handle = m[2]
@ -523,7 +523,7 @@ func findhandle(xid string) string {
handle = xid handle = xid
} }
} else { } else {
handle = p.Handle handle = info.Name
} }
_, err = stmtSaveXonker.Exec(xid, handle, "handle") _, err = stmtSaveXonker.Exec(xid, handle, "handle")
if err != nil { if err != nil {

13
honk.go
View File

@ -156,6 +156,19 @@ type Honker struct {
Combos []string Combos []string
} }
type SomeThing struct {
What int
XID string
Owner string
Name string
}
const (
SomeNothing int = iota
SomeActor
SomeCollection
)
var serverName string var serverName string
var iconName = "icon.png" var iconName = "icon.png"
var serverMsg template.HTML var serverMsg template.HTML

24
web.go
View File

@ -538,14 +538,19 @@ func ximport(w http.ResponseWriter, r *http.Request) {
log.Printf("importing %s", xid) log.Printf("importing %s", xid)
user, _ := butwhatabout(u.Username) user, _ := butwhatabout(u.Username)
what, _ := j.GetString("type") info, _ := somethingabout(j)
if isactor(what) { if info == nil {
xonk = xonksaver(user, j, originate(xid))
} else if info.What == SomeActor {
outbox, _ := j.GetString("outbox") outbox, _ := j.GetString("outbox")
gimmexonks(user, outbox) gimmexonks(user, outbox)
http.Redirect(w, r, "/h?xid="+url.QueryEscape(xid), http.StatusSeeOther) http.Redirect(w, r, "/h?xid="+url.QueryEscape(xid), http.StatusSeeOther)
return return
} else if info.What == SomeCollection {
gimmexonks(user, xid)
http.Redirect(w, r, "/xzone", http.StatusSeeOther)
return
} }
xonk = xonksaver(user, j, originate(xid))
} }
convoy := "" convoy := ""
if xonk != nil { if xonk != nil {
@ -770,6 +775,7 @@ func showontology(w http.ResponseWriter, r *http.Request) {
j := junk.New() j := junk.New()
j["@context"] = itiswhatitis j["@context"] = itiswhatitis
j["id"] = fmt.Sprintf("https://%s/o/%s", serverName, name) j["id"] = fmt.Sprintf("https://%s/o/%s", serverName, name)
j["name"] = name
j["attributedTo"] = user.URL j["attributedTo"] = user.URL
j["type"] = "OrderedCollection" j["type"] = "OrderedCollection"
j["totalItems"] = len(xids) j["totalItems"] = len(xids)
@ -1501,7 +1507,9 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
log.Printf("error updating honker: %s", err) log.Printf("error updating honker: %s", err)
return return
} }
go subsub(user, url) // incomplete
owner := url
go subsub(user, url, owner)
http.Redirect(w, r, "/honkers", http.StatusSeeOther) http.Redirect(w, r, "/honkers", http.StatusSeeOther)
return return
@ -1519,13 +1527,13 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
if peep == "peep" { if peep == "peep" {
flavor = "peep" flavor = "peep"
} }
p, err := investigate(url) info, err := investigate(url)
if err != nil { if err != nil {
http.Error(w, "error investigating: "+err.Error(), http.StatusInternalServerError) http.Error(w, "error investigating: "+err.Error(), http.StatusInternalServerError)
log.Printf("failed to investigate honker: %s", err) log.Printf("failed to investigate honker: %s", err)
return return
} }
url = p.XID url = info.XID
db := opendatabase() db := opendatabase()
row := db.QueryRow("select xid from honkers where xid = ? and userid = ? and flavor in ('sub', 'unsub', 'peep')", url, u.UserID) row := db.QueryRow("select xid from honkers where xid = ? and userid = ? and flavor in ('sub', 'unsub', 'peep')", url, u.UserID)
@ -1540,7 +1548,7 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
} }
if name == "" { if name == "" {
name = p.Handle name = info.Name
} }
_, err = stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos) _, err = stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos)
if err != nil { if err != nil {
@ -1549,7 +1557,7 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
} }
if flavor == "presub" { if flavor == "presub" {
user, _ := butwhatabout(u.Username) user, _ := butwhatabout(u.Username)
go subsub(user, url) go subsub(user, url, info.Owner)
} }
http.Redirect(w, r, "/honkers", http.StatusSeeOther) http.Redirect(w, r, "/honkers", http.StatusSeeOther)
} }