all repos — honk @ 05ff58bdf47778df3ba9db17162e6bbebedcbda7

my fork of honk

activity.go (view raw)

   1//
   2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
   3//
   4// Permission to use, copy, modify, and distribute this software for any
   5// purpose with or without fee is hereby granted, provided that the above
   6// copyright notice and this permission notice appear in all copies.
   7//
   8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15
  16package main
  17
  18import (
  19	"bytes"
  20	"crypto/rsa"
  21	"database/sql"
  22	"fmt"
  23	"io"
  24	"log"
  25	notrand "math/rand"
  26	"net/http"
  27	"net/url"
  28	"os"
  29	"strings"
  30	"sync"
  31	"time"
  32
  33	"humungus.tedunangst.com/r/webs/httpsig"
  34	"humungus.tedunangst.com/r/webs/image"
  35	"humungus.tedunangst.com/r/webs/junk"
  36)
  37
  38var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
  39var thefakename = `application/activity+json`
  40var falsenames = []string{
  41	`application/ld+json`,
  42	`application/activity+json`,
  43}
  44var itiswhatitis = "https://www.w3.org/ns/activitystreams"
  45var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
  46
  47func friendorfoe(ct string) bool {
  48	ct = strings.ToLower(ct)
  49	for _, at := range falsenames {
  50		if strings.HasPrefix(ct, at) {
  51			return true
  52		}
  53	}
  54	return false
  55}
  56
  57func PostJunk(keyname string, key *rsa.PrivateKey, url string, j junk.Junk) error {
  58	var buf bytes.Buffer
  59	j.Write(&buf)
  60	return PostMsg(keyname, key, url, buf.Bytes())
  61}
  62
  63func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error {
  64	client := http.DefaultClient
  65	req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
  66	if err != nil {
  67		return err
  68	}
  69	req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
  70	req.Header.Set("Content-Type", theonetruename)
  71	httpsig.SignRequest(keyname, key, req, msg)
  72	resp, err := client.Do(req)
  73	if err != nil {
  74		return err
  75	}
  76	resp.Body.Close()
  77	switch resp.StatusCode {
  78	case 200:
  79	case 201:
  80	case 202:
  81	default:
  82		return fmt.Errorf("http post status: %d", resp.StatusCode)
  83	}
  84	log.Printf("successful post: %s %d", url, resp.StatusCode)
  85	return nil
  86}
  87
  88func GetJunk(url string) (junk.Junk, error) {
  89	return GetJunkTimeout(url, 0)
  90}
  91
  92func GetJunkFast(url string) (junk.Junk, error) {
  93	return GetJunkTimeout(url, 5*time.Second)
  94}
  95
  96func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
  97	at := thefakename
  98	if strings.Contains(url, ".well-known/webfinger?resource") {
  99		at = "application/jrd+json"
 100	}
 101	return junk.Get(url, junk.GetArgs{
 102		Accept:  at,
 103		Agent:   "honksnonk/5.0; " + serverName,
 104		Timeout: timeout,
 105	})
 106}
 107
 108func savedonk(url string, name, media string, localize bool) *Donk {
 109	if url == "" {
 110		return nil
 111	}
 112	var donk Donk
 113	row := stmtFindFile.QueryRow(url)
 114	err := row.Scan(&donk.FileID)
 115	if err == nil {
 116		return &donk
 117	}
 118	log.Printf("saving donk: %s", url)
 119	if err != nil && err != sql.ErrNoRows {
 120		log.Printf("error querying: %s", err)
 121	}
 122	xid := xfiltrate()
 123	data := []byte{}
 124	if localize {
 125		resp, err := http.Get(url)
 126		if err != nil {
 127			log.Printf("error fetching %s: %s", url, err)
 128			localize = false
 129			goto saveit
 130		}
 131		defer resp.Body.Close()
 132		if resp.StatusCode != 200 {
 133			localize = false
 134			goto saveit
 135		}
 136		var buf bytes.Buffer
 137		io.Copy(&buf, resp.Body)
 138
 139		data = buf.Bytes()
 140		if strings.HasPrefix(media, "image") {
 141			img, err := image.Vacuum(&buf,
 142				image.Params{LimitSize: 4800 * 4800, MaxWidth: 2048, MaxHeight: 2048})
 143			if err != nil {
 144				log.Printf("unable to decode image: %s", err)
 145				localize = false
 146				data = []byte{}
 147				goto saveit
 148			}
 149			data = img.Data
 150			media = "image/" + img.Format
 151		} else if len(data) > 100000 {
 152			log.Printf("not saving large attachment")
 153			localize = false
 154			data = []byte{}
 155		}
 156	}
 157saveit:
 158	res, err := stmtSaveFile.Exec(xid, name, url, media, localize, data)
 159	if err != nil {
 160		log.Printf("error saving file %s: %s", url, err)
 161		return nil
 162	}
 163	donk.FileID, _ = res.LastInsertId()
 164	return &donk
 165}
 166
 167func iszonked(userid int64, xid string) bool {
 168	row := stmtFindZonk.QueryRow(userid, xid)
 169	var id int64
 170	err := row.Scan(&id)
 171	if err == nil {
 172		return true
 173	}
 174	if err != sql.ErrNoRows {
 175		log.Printf("err querying zonk: %s", err)
 176	}
 177	return false
 178}
 179
 180func needxonk(user *WhatAbout, x *Honk) bool {
 181	if x.What == "eradicate" {
 182		return true
 183	}
 184	if thoudostbitethythumb(user.ID, x.Audience, x.XID) {
 185		log.Printf("not saving thumb biter? %s via %s", x.XID, x.Honker)
 186		return false
 187	}
 188	return needxonkid(user, x.XID)
 189}
 190func needxonkid(user *WhatAbout, xid string) bool {
 191	if strings.HasPrefix(xid, user.URL+"/") {
 192		return false
 193	}
 194	if iszonked(user.ID, xid) {
 195		log.Printf("already zonked: %s", xid)
 196		return false
 197	}
 198	row := stmtFindXonk.QueryRow(user.ID, xid)
 199	var id int64
 200	err := row.Scan(&id)
 201	if err == nil {
 202		return false
 203	}
 204	if err != sql.ErrNoRows {
 205		log.Printf("err querying xonk: %s", err)
 206	}
 207	return true
 208}
 209
 210func savexonk(user *WhatAbout, x *Honk) {
 211	if x.What == "eradicate" {
 212		log.Printf("eradicating %s by %s", x.XID, x.Honker)
 213		xonk := getxonk(user.ID, x.XID)
 214		if xonk != nil {
 215			stmtZonkDonks.Exec(xonk.ID)
 216			_, err := stmtZonkIt.Exec(user.ID, x.XID)
 217			if err != nil {
 218				log.Printf("error eradicating: %s", err)
 219			}
 220		}
 221		stmtSaveZonker.Exec(user.ID, x.XID, "zonk")
 222		return
 223	}
 224	log.Printf("saving xonk: %s", x.XID)
 225	dt := x.Date.UTC().Format(dbtimeformat)
 226	aud := strings.Join(x.Audience, " ")
 227	whofore := 0
 228	if strings.Contains(aud, user.URL) {
 229		whofore = 1
 230	}
 231	res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
 232		x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker)
 233	if err != nil {
 234		log.Printf("err saving xonk: %s", err)
 235		return
 236	}
 237	x.ID, _ = res.LastInsertId()
 238	for _, d := range x.Donks {
 239		_, err = stmtSaveDonk.Exec(x.ID, d.FileID)
 240		if err != nil {
 241			log.Printf("err saving donk: %s", err)
 242			return
 243		}
 244	}
 245}
 246
 247type Box struct {
 248	In     string
 249	Out    string
 250	Shared string
 251}
 252
 253var boxofboxes = make(map[string]*Box)
 254var boxlock sync.Mutex
 255var boxinglock sync.Mutex
 256
 257func getboxes(ident string) (*Box, error) {
 258	boxlock.Lock()
 259	b, ok := boxofboxes[ident]
 260	boxlock.Unlock()
 261	if ok {
 262		return b, nil
 263	}
 264
 265	boxinglock.Lock()
 266	defer boxinglock.Unlock()
 267
 268	boxlock.Lock()
 269	b, ok = boxofboxes[ident]
 270	boxlock.Unlock()
 271	if ok {
 272		return b, nil
 273	}
 274
 275	var info string
 276	row := stmtGetXonker.QueryRow(ident, "boxes")
 277	err := row.Scan(&info)
 278	if err != nil {
 279		j, err := GetJunk(ident)
 280		if err != nil {
 281			return nil, err
 282		}
 283		inbox, _ := j.GetString("inbox")
 284		outbox, _ := j.GetString("outbox")
 285		sbox, _ := j.FindString([]string{"endpoints", "sharedInbox"})
 286		b = &Box{In: inbox, Out: outbox, Shared: sbox}
 287		if inbox != "" {
 288			m := strings.Join([]string{inbox, outbox, sbox}, " ")
 289			_, err = stmtSaveXonker.Exec(ident, m, "boxes")
 290			if err != nil {
 291				log.Printf("error saving boxes: %s", err)
 292			}
 293		}
 294	} else {
 295		m := strings.Split(info, " ")
 296		b = &Box{In: m[0], Out: m[1], Shared: m[2]}
 297	}
 298
 299	boxlock.Lock()
 300	boxofboxes[ident] = b
 301	boxlock.Unlock()
 302	return b, nil
 303}
 304
 305func gimmexonks(user *WhatAbout, outbox string) {
 306	log.Printf("getting outbox: %s", outbox)
 307	j, err := GetJunk(outbox)
 308	if err != nil {
 309		log.Printf("error getting outbox: %s", err)
 310		return
 311	}
 312	t, _ := j.GetString("type")
 313	origin := originate(outbox)
 314	if t == "OrderedCollection" {
 315		items, _ := j.GetArray("orderedItems")
 316		if items == nil {
 317			obj, ok := j.GetMap("first")
 318			if ok {
 319				items, _ = obj.GetArray("orderedItems")
 320			} else {
 321				page1, _ := j.GetString("first")
 322				j, err = GetJunk(page1)
 323				if err != nil {
 324					log.Printf("error gettings page1: %s", err)
 325					return
 326				}
 327				items, _ = j.GetArray("orderedItems")
 328			}
 329		}
 330		if len(items) > 20 {
 331			items = items[0:20]
 332		}
 333		for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
 334			items[i], items[j] = items[j], items[i]
 335		}
 336		for _, item := range items {
 337			obj, ok := item.(junk.Junk)
 338			if !ok {
 339				continue
 340			}
 341			xonk := xonkxonk(user, obj, origin)
 342			if xonk != nil {
 343				savexonk(user, xonk)
 344			}
 345		}
 346	}
 347}
 348
 349func peeppeep() {
 350	user, _ := butwhatabout("htest")
 351	honkers := gethonkers(user.ID)
 352	for _, f := range honkers {
 353		if f.Flavor != "peep" {
 354			continue
 355		}
 356		log.Printf("getting updates: %s", f.XID)
 357		box, err := getboxes(f.XID)
 358		if err != nil {
 359			log.Printf("error getting outbox: %s", err)
 360			continue
 361		}
 362		gimmexonks(user, box.Out)
 363	}
 364}
 365func whosthere(xid string) ([]string, string) {
 366	obj, err := GetJunk(xid)
 367	if err != nil {
 368		log.Printf("error getting remote xonk: %s", err)
 369		return nil, ""
 370	}
 371	convoy, _ := obj.GetString("context")
 372	if convoy == "" {
 373		convoy, _ = obj.GetString("conversation")
 374	}
 375	return newphone(nil, obj), convoy
 376}
 377
 378func newphone(a []string, obj junk.Junk) []string {
 379	for _, addr := range []string{"to", "cc", "attributedTo"} {
 380		who, _ := obj.GetString(addr)
 381		if who != "" {
 382			a = append(a, who)
 383		}
 384		whos, _ := obj.GetArray(addr)
 385		for _, w := range whos {
 386			who, _ := w.(string)
 387			if who != "" {
 388				a = append(a, who)
 389			}
 390		}
 391	}
 392	return a
 393}
 394
 395func extractattrto(obj junk.Junk) string {
 396	who, _ := obj.GetString("attributedTo")
 397	if who != "" {
 398		return who
 399	}
 400	o, ok := obj.GetMap("attributedTo")
 401	if ok {
 402		id, ok := o.GetString("id")
 403		if ok {
 404			return id
 405		}
 406	}
 407	arr, _ := obj.GetArray("attributedTo")
 408	for _, a := range arr {
 409		o, ok := a.(junk.Junk)
 410		if ok {
 411			t, _ := o.GetString("type")
 412			id, _ := o.GetString("id")
 413			if t == "Person" || t == "" {
 414				return id
 415			}
 416		}
 417	}
 418	return ""
 419}
 420
 421func consumeactivity(user *WhatAbout, j junk.Junk, origin string) {
 422	xonk := xonkxonk(user, j, origin)
 423	if xonk != nil {
 424		savexonk(user, xonk)
 425	}
 426}
 427
 428func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
 429	depth := 0
 430	maxdepth := 10
 431	currenttid := ""
 432	var xonkxonkfn func(item junk.Junk, origin string) *Honk
 433
 434	saveoneup := func(xid string) {
 435		log.Printf("getting oneup: %s", xid)
 436		if depth >= maxdepth {
 437			log.Printf("in too deep")
 438			return
 439		}
 440		obj, err := GetJunk(xid)
 441		if err != nil {
 442			log.Printf("error getting oneup: %s", err)
 443			return
 444		}
 445		depth++
 446		xonk := xonkxonkfn(obj, originate(xid))
 447		if xonk != nil {
 448			savexonk(user, xonk)
 449		}
 450		depth--
 451	}
 452
 453	xonkxonkfn = func(item junk.Junk, origin string) *Honk {
 454		// id, _ := item.GetString( "id")
 455		what, _ := item.GetString("type")
 456		dt, _ := item.GetString("published")
 457
 458		var audience []string
 459		var err error
 460		var xid, rid, url, content, precis, convoy, oonker string
 461		var obj junk.Junk
 462		var ok bool
 463		switch what {
 464		case "Announce":
 465			obj, ok = item.GetMap("object")
 466			if ok {
 467				xid, _ = obj.GetString("id")
 468			} else {
 469				xid, _ = item.GetString("object")
 470			}
 471			if !needxonkid(user, xid) {
 472				return nil
 473			}
 474			log.Printf("getting bonk: %s", xid)
 475			obj, err = GetJunk(xid)
 476			if err != nil {
 477				log.Printf("error regetting: %s", err)
 478				if err.Error() == "http get status: 502" {
 479					time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
 480					obj, err = GetJunk(xid)
 481					if err != nil {
 482						log.Printf("still couldn't get it")
 483					} else {
 484						log.Printf("retry success!")
 485					}
 486				}
 487			}
 488			origin = originate(xid)
 489			what = "bonk"
 490		case "Create":
 491			obj, ok = item.GetMap("object")
 492			if !ok {
 493				xid, _ = obj.GetString("object")
 494				log.Printf("getting created honk: %s", xid)
 495				obj, err = GetJunk(xid)
 496				if err != nil {
 497					log.Printf("error getting creation: %s", err)
 498				}
 499			}
 500			what = "honk"
 501		case "Delete":
 502			obj, _ = item.GetMap("object")
 503			xid, _ = item.GetString("object")
 504			what = "eradicate"
 505		case "Video":
 506			fallthrough
 507		case "Question":
 508			fallthrough
 509		case "Note":
 510			fallthrough
 511		case "Article":
 512			fallthrough
 513		case "Page":
 514			obj = item
 515			what = "honk"
 516		default:
 517			log.Printf("unknown activity: %s", what)
 518			fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 519			item.Write(fd)
 520			io.WriteString(fd, "\n")
 521			fd.Close()
 522			return nil
 523		}
 524
 525		var xonk Honk
 526		who, _ := item.GetString("actor")
 527		if obj != nil {
 528			if who == "" {
 529				who = extractattrto(obj)
 530			}
 531			oonker = extractattrto(obj)
 532			ot, _ := obj.GetString("type")
 533			url, _ = obj.GetString("url")
 534			if ot == "Tombstone" {
 535				xid, _ = obj.GetString("id")
 536			} else {
 537				audience = newphone(audience, obj)
 538				xid, _ = obj.GetString("id")
 539				precis, _ = obj.GetString("summary")
 540				if precis == "" {
 541					precis, _ = obj.GetString("name")
 542				}
 543				content, _ = obj.GetString("content")
 544				if !strings.HasPrefix(content, "<p>") {
 545					content = "<p>" + content
 546				}
 547				sens, _ := obj["sensitive"].(bool)
 548				if sens && precis == "" {
 549					precis = "unspecified horror"
 550				}
 551				rid, ok = obj.GetString("inReplyTo")
 552				if !ok {
 553					robj, ok := obj.GetMap("inReplyTo")
 554					if ok {
 555						rid, _ = robj.GetString("id")
 556					}
 557				}
 558				convoy, _ = obj.GetString("context")
 559				if convoy == "" {
 560					convoy, _ = obj.GetString("conversation")
 561				}
 562				if ot == "Question" {
 563					if what == "honk" {
 564						what = "qonk"
 565					}
 566					content += "<ul>"
 567					ans, _ := obj.GetArray("oneOf")
 568					for _, ai := range ans {
 569						a, ok := ai.(junk.Junk)
 570						if !ok {
 571							continue
 572						}
 573						as, _ := a.GetString("name")
 574						content += "<li>" + as
 575					}
 576					ans, _ = obj.GetArray("anyOf")
 577					for _, ai := range ans {
 578						a, ok := ai.(junk.Junk)
 579						if !ok {
 580							continue
 581						}
 582						as, _ := a.GetString("name")
 583						content += "<li>" + as
 584					}
 585					content += "</ul>"
 586				}
 587				if what == "honk" && rid != "" {
 588					what = "tonk"
 589				}
 590			}
 591			atts, _ := obj.GetArray("attachment")
 592			for i, atti := range atts {
 593				att, ok := atti.(junk.Junk)
 594				if !ok {
 595					continue
 596				}
 597				at, _ := att.GetString("type")
 598				mt, _ := att.GetString("mediaType")
 599				u, _ := att.GetString("url")
 600				name, _ := att.GetString("name")
 601				localize := false
 602				if i > 4 {
 603					log.Printf("excessive attachment: %s", at)
 604				} else if at == "Document" || at == "Image" {
 605					mt = strings.ToLower(mt)
 606					log.Printf("attachment: %s %s", mt, u)
 607					if mt == "text/plain" || strings.HasPrefix(mt, "image") {
 608						localize = true
 609					}
 610				} else {
 611					log.Printf("unknown attachment: %s", at)
 612				}
 613				donk := savedonk(u, name, mt, localize)
 614				if donk != nil {
 615					xonk.Donks = append(xonk.Donks, donk)
 616				}
 617			}
 618			tags, _ := obj.GetArray("tag")
 619			for _, tagi := range tags {
 620				tag, ok := tagi.(junk.Junk)
 621				if !ok {
 622					continue
 623				}
 624				tt, _ := tag.GetString("type")
 625				name, _ := tag.GetString("name")
 626				if tt == "Emoji" {
 627					icon, _ := tag.GetMap("icon")
 628					mt, _ := icon.GetString("mediaType")
 629					if mt == "" {
 630						mt = "image/png"
 631					}
 632					u, _ := icon.GetString("url")
 633					donk := savedonk(u, name, mt, true)
 634					if donk != nil {
 635						xonk.Donks = append(xonk.Donks, donk)
 636					}
 637				}
 638			}
 639		}
 640		if originate(xid) != origin {
 641			log.Printf("original sin: %s <> %s", xid, origin)
 642			item.Write(os.Stdout)
 643			return nil
 644		}
 645		audience = append(audience, who)
 646
 647		audience = oneofakind(audience)
 648
 649		if currenttid == "" {
 650			currenttid = convoy
 651		}
 652
 653		if oonker == who {
 654			oonker = ""
 655		}
 656		xonk.UserID = user.ID
 657		xonk.What = what
 658		xonk.Honker = who
 659		xonk.XID = xid
 660		xonk.RID = rid
 661		xonk.Date, _ = time.Parse(time.RFC3339, dt)
 662		xonk.URL = url
 663		xonk.Noise = content
 664		xonk.Precis = precis
 665		xonk.Audience = audience
 666		xonk.Oonker = oonker
 667
 668		if needxonk(user, &xonk) {
 669			if rid != "" {
 670				if needxonkid(user, rid) {
 671					saveoneup(rid)
 672				}
 673			}
 674			if convoy == "" {
 675				convoy = currenttid
 676			}
 677			xonk.Convoy = convoy
 678			return &xonk
 679		}
 680		return nil
 681	}
 682
 683	return xonkxonkfn(item, origin)
 684}
 685
 686func rubadubdub(user *WhatAbout, req junk.Junk) {
 687	xid, _ := req.GetString("id")
 688	actor, _ := req.GetString("actor")
 689	j := junk.New()
 690	j["@context"] = itiswhatitis
 691	j["id"] = user.URL + "/dub/" + url.QueryEscape(xid)
 692	j["type"] = "Accept"
 693	j["actor"] = user.URL
 694	j["to"] = actor
 695	j["published"] = time.Now().UTC().Format(time.RFC3339)
 696	j["object"] = req
 697
 698	var buf bytes.Buffer
 699	j.Write(&buf)
 700	msg := buf.Bytes()
 701
 702	deliverate(0, user.Name, actor, msg)
 703}
 704
 705func itakeitallback(user *WhatAbout, xid string) {
 706	j := junk.New()
 707	j["@context"] = itiswhatitis
 708	j["id"] = user.URL + "/unsub/" + url.QueryEscape(xid)
 709	j["type"] = "Undo"
 710	j["actor"] = user.URL
 711	j["to"] = xid
 712	f := junk.New()
 713	f["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
 714	f["type"] = "Follow"
 715	f["actor"] = user.URL
 716	f["to"] = xid
 717	f["object"] = xid
 718	j["object"] = f
 719	j["published"] = time.Now().UTC().Format(time.RFC3339)
 720
 721	var buf bytes.Buffer
 722	j.Write(&buf)
 723	msg := buf.Bytes()
 724
 725	deliverate(0, user.Name, xid, msg)
 726}
 727
 728func subsub(user *WhatAbout, xid string) {
 729	j := junk.New()
 730	j["@context"] = itiswhatitis
 731	j["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
 732	j["type"] = "Follow"
 733	j["actor"] = user.URL
 734	j["to"] = xid
 735	j["object"] = xid
 736	j["published"] = time.Now().UTC().Format(time.RFC3339)
 737
 738	var buf bytes.Buffer
 739	j.Write(&buf)
 740	msg := buf.Bytes()
 741
 742	deliverate(0, user.Name, xid, msg)
 743}
 744
 745func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
 746	dt := h.Date.Format(time.RFC3339)
 747	var jo junk.Junk
 748	j := junk.New()
 749	j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
 750	j["actor"] = user.URL
 751	j["published"] = dt
 752	if h.Public {
 753		j["to"] = []string{h.Audience[0], user.URL + "/followers"}
 754	} else {
 755		j["to"] = h.Audience[0]
 756	}
 757	if len(h.Audience) > 1 {
 758		j["cc"] = h.Audience[1:]
 759	}
 760
 761	switch h.What {
 762	case "tonk":
 763		fallthrough
 764	case "honk":
 765		j["type"] = "Create"
 766
 767		jo = junk.New()
 768		jo["id"] = h.XID
 769		jo["type"] = "Note"
 770		jo["published"] = dt
 771		jo["url"] = h.XID
 772		jo["attributedTo"] = user.URL
 773		if h.RID != "" {
 774			jo["inReplyTo"] = h.RID
 775		}
 776		if h.Convoy != "" {
 777			jo["context"] = h.Convoy
 778			jo["conversation"] = h.Convoy
 779		}
 780		jo["to"] = h.Audience[0]
 781		if len(h.Audience) > 1 {
 782			jo["cc"] = h.Audience[1:]
 783		}
 784		if !h.Public {
 785			jo["directMessage"] = true
 786		}
 787		jo["summary"] = h.Precis
 788		jo["content"] = mentionize(h.Noise)
 789		if strings.HasPrefix(h.Precis, "DZ:") {
 790			jo["sensitive"] = true
 791		}
 792
 793		var tags []junk.Junk
 794		g := bunchofgrapes(h.Noise)
 795		for _, m := range g {
 796			t := junk.New()
 797			t["type"] = "Mention"
 798			t["name"] = m.who
 799			t["href"] = m.where
 800			tags = append(tags, t)
 801		}
 802		ooo := ontologies(h.Noise)
 803		for _, o := range ooo {
 804			t := junk.New()
 805			t["type"] = "Hashtag"
 806			t["name"] = o
 807			tags = append(tags, t)
 808		}
 809		herd := herdofemus(h.Noise)
 810		for _, e := range herd {
 811			t := junk.New()
 812			t["id"] = e.ID
 813			t["type"] = "Emoji"
 814			t["name"] = e.Name
 815			i := junk.New()
 816			i["type"] = "Image"
 817			i["mediaType"] = "image/png"
 818			i["url"] = e.ID
 819			t["icon"] = i
 820			tags = append(tags, t)
 821		}
 822		if len(tags) > 0 {
 823			jo["tag"] = tags
 824		}
 825		var atts []junk.Junk
 826		for _, d := range h.Donks {
 827			if re_emus.MatchString(d.Name) {
 828				continue
 829			}
 830			jd := junk.New()
 831			jd["mediaType"] = d.Media
 832			jd["name"] = d.Name
 833			jd["type"] = "Document"
 834			jd["url"] = d.URL
 835			atts = append(atts, jd)
 836		}
 837		if len(atts) > 0 {
 838			jo["attachment"] = atts
 839		}
 840		j["object"] = jo
 841	case "bonk":
 842		j["type"] = "Announce"
 843		if h.Convoy != "" {
 844			j["context"] = h.Convoy
 845		}
 846		j["object"] = h.XID
 847	case "zonk":
 848		j["type"] = "Delete"
 849		j["object"] = h.XID
 850	}
 851
 852	return j, jo
 853}
 854
 855func honkworldwide(user *WhatAbout, honk *Honk) {
 856	jonk, _ := jonkjonk(user, honk)
 857	jonk["@context"] = itiswhatitis
 858	var buf bytes.Buffer
 859	jonk.Write(&buf)
 860	msg := buf.Bytes()
 861
 862	rcpts := make(map[string]bool)
 863	for _, a := range honk.Audience {
 864		if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
 865			box, _ := getboxes(a)
 866			if box != nil && honk.Public && box.Shared != "" {
 867				rcpts["%"+box.Shared] = true
 868			} else {
 869				rcpts[a] = true
 870			}
 871		}
 872	}
 873	if honk.Public {
 874		for _, f := range getdubs(user.ID) {
 875			box, _ := getboxes(f.XID)
 876			if box != nil && box.Shared != "" {
 877				rcpts["%"+box.Shared] = true
 878			} else {
 879				rcpts[f.XID] = true
 880			}
 881		}
 882	}
 883	for a := range rcpts {
 884		go deliverate(0, user.Name, a, msg)
 885	}
 886}
 887
 888func asjonker(user *WhatAbout) junk.Junk {
 889	about := obfusbreak(user.About)
 890
 891	j := junk.New()
 892	j["@context"] = itiswhatitis
 893	j["id"] = user.URL
 894	j["type"] = "Person"
 895	j["inbox"] = user.URL + "/inbox"
 896	j["outbox"] = user.URL + "/outbox"
 897	j["followers"] = user.URL + "/followers"
 898	j["following"] = user.URL + "/following"
 899	j["name"] = user.Display
 900	j["preferredUsername"] = user.Name
 901	j["summary"] = about
 902	j["url"] = user.URL
 903	a := junk.New()
 904	a["type"] = "Image"
 905	a["mediaType"] = "image/png"
 906	a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
 907	j["icon"] = a
 908	k := junk.New()
 909	k["id"] = user.URL + "#key"
 910	k["owner"] = user.URL
 911	k["publicKeyPem"] = user.Key
 912	j["publicKey"] = k
 913
 914	return j
 915}
 916
 917var handfull = make(map[string]string)
 918var handlock sync.Mutex
 919
 920func gofish(name string) string {
 921	if name[0] == '@' {
 922		name = name[1:]
 923	}
 924	m := strings.Split(name, "@")
 925	if len(m) != 2 {
 926		log.Printf("bad fish name: %s", name)
 927		return ""
 928	}
 929	handlock.Lock()
 930	ref, ok := handfull[name]
 931	handlock.Unlock()
 932	if ok {
 933		return ref
 934	}
 935	row := stmtGetXonker.QueryRow(name, "fishname")
 936	var href string
 937	err := row.Scan(&href)
 938	if err == nil {
 939		handlock.Lock()
 940		handfull[name] = href
 941		handlock.Unlock()
 942		return href
 943	}
 944	log.Printf("fishing for %s", name)
 945	j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
 946	if err != nil {
 947		log.Printf("failed to go fish %s: %s", name, err)
 948		handlock.Lock()
 949		handfull[name] = ""
 950		handlock.Unlock()
 951		return ""
 952	}
 953	links, _ := j.GetArray("links")
 954	for _, li := range links {
 955		l, ok := li.(junk.Junk)
 956		if !ok {
 957			continue
 958		}
 959		href, _ := l.GetString("href")
 960		rel, _ := l.GetString("rel")
 961		t, _ := l.GetString("type")
 962		if rel == "self" && friendorfoe(t) {
 963			stmtSaveXonker.Exec(name, href, "fishname")
 964			handlock.Lock()
 965			handfull[name] = href
 966			handlock.Unlock()
 967			return href
 968		}
 969	}
 970	handlock.Lock()
 971	handfull[name] = ""
 972	handlock.Unlock()
 973	return ""
 974}
 975
 976func isactor(t string) bool {
 977	switch t {
 978	case "Person":
 979	case "Application":
 980	case "Service":
 981	default:
 982		return false
 983	}
 984	return true
 985}
 986
 987func investigate(name string) string {
 988	if name == "" {
 989		return ""
 990	}
 991	if name[0] == '@' {
 992		name = gofish(name)
 993	}
 994	if name == "" {
 995		return ""
 996	}
 997	obj, err := GetJunkFast(name)
 998	if err != nil {
 999		log.Printf("error investigating honker: %s", err)
1000		return ""
1001	}
1002	t, _ := obj.GetString("type")
1003	if !isactor(t) {
1004		log.Printf("it's not a person! %s", name)
1005		return ""
1006	}
1007	id, _ := obj.GetString("id")
1008	return id
1009}