all repos — honk @ 93d725e1e4f61d44cd42f071c1957c1d3fa7ed68

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