all repos — honk @ 2657bd242c04c2e5f572e488547e9c7a4eb170fc

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	"context"
  21	"crypto/tls"
  22	"database/sql"
  23	"fmt"
  24	"html"
  25	"io"
  26	notrand "math/rand"
  27	"net/http"
  28	"net/url"
  29	"os"
  30	"regexp"
  31	"strings"
  32	"time"
  33
  34	"humungus.tedunangst.com/r/webs/cache"
  35	"humungus.tedunangst.com/r/webs/gate"
  36	"humungus.tedunangst.com/r/webs/httpsig"
  37	"humungus.tedunangst.com/r/webs/junk"
  38	"humungus.tedunangst.com/r/webs/templates"
  39)
  40
  41var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
  42var thefakename = `application/activity+json`
  43var falsenames = []string{
  44	`application/ld+json`,
  45	`application/activity+json`,
  46}
  47var itiswhatitis = "https://www.w3.org/ns/activitystreams"
  48var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
  49
  50var fastTimeout time.Duration = 5
  51var slowTimeout time.Duration = 30
  52
  53func friendorfoe(ct string) bool {
  54	ct = strings.ToLower(ct)
  55	for _, at := range falsenames {
  56		if strings.HasPrefix(ct, at) {
  57			return true
  58		}
  59	}
  60	return false
  61}
  62
  63var develClient = &http.Client{
  64	Transport: &http.Transport{
  65		TLSClientConfig: &tls.Config{
  66			InsecureSkipVerify: true,
  67		},
  68	},
  69}
  70
  71func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error {
  72	return PostMsg(keyname, key, url, j.ToBytes())
  73}
  74
  75func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error {
  76	client := http.DefaultClient
  77	if develMode {
  78		client = develClient
  79	}
  80	req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
  81	if err != nil {
  82		return err
  83	}
  84	req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
  85	req.Header.Set("Content-Type", theonetruename)
  86	httpsig.SignRequest(keyname, key, req, msg)
  87	ctx, cancel := context.WithTimeout(context.Background(), 2*slowTimeout*time.Second)
  88	defer cancel()
  89	req = req.WithContext(ctx)
  90	resp, err := client.Do(req)
  91	if err != nil {
  92		return err
  93	}
  94	resp.Body.Close()
  95	switch resp.StatusCode {
  96	case 200:
  97	case 201:
  98	case 202:
  99	default:
 100		return fmt.Errorf("http post status: %d", resp.StatusCode)
 101	}
 102	ilog.Printf("successful post: %s %d", url, resp.StatusCode)
 103	return nil
 104}
 105
 106func GetJunk(userid int64, url string) (junk.Junk, error) {
 107	return GetJunkTimeout(userid, url, slowTimeout*time.Second)
 108}
 109
 110func GetJunkFast(userid int64, url string) (junk.Junk, error) {
 111	return GetJunkTimeout(userid, url, fastTimeout*time.Second)
 112}
 113
 114func GetJunkHardMode(userid int64, url string) (junk.Junk, error) {
 115	j, err := GetJunk(userid, url)
 116	if err != nil {
 117		emsg := err.Error()
 118		if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
 119			ilog.Printf("trying again after error: %s", emsg)
 120			time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
 121			j, err = GetJunk(userid, url)
 122			if err != nil {
 123				ilog.Printf("still couldn't get it")
 124			} else {
 125				ilog.Printf("retry success!")
 126			}
 127		}
 128	}
 129	return j, err
 130}
 131
 132var flightdeck = gate.NewSerializer()
 133
 134var signGets = true
 135
 136func junkGet(userid int64, url string, args junk.GetArgs) (junk.Junk, error) {
 137	client := http.DefaultClient
 138	if args.Client != nil {
 139		client = args.Client
 140	}
 141	req, err := http.NewRequest("GET", url, nil)
 142	if err != nil {
 143		return nil, err
 144	}
 145	if args.Accept != "" {
 146		req.Header.Set("Accept", args.Accept)
 147	}
 148	if args.Agent != "" {
 149		req.Header.Set("User-Agent", args.Agent)
 150	}
 151	if signGets {
 152		var ki *KeyInfo
 153		ok := ziggies.Get(userid, &ki)
 154		if ok {
 155			httpsig.SignRequest(ki.keyname, ki.seckey, req, nil)
 156		}
 157	}
 158	if args.Timeout != 0 {
 159		ctx, cancel := context.WithTimeout(context.Background(), args.Timeout)
 160		defer cancel()
 161		req = req.WithContext(ctx)
 162	}
 163	resp, err := client.Do(req)
 164	if err != nil {
 165		return nil, err
 166	}
 167	defer resp.Body.Close()
 168
 169	if resp.StatusCode != 200 {
 170		return nil, fmt.Errorf("http get status: %d", resp.StatusCode)
 171	}
 172	return junk.Read(resp.Body)
 173}
 174
 175func GetJunkTimeout(userid int64, url string, timeout time.Duration) (junk.Junk, error) {
 176	client := http.DefaultClient
 177	if develMode {
 178		client = develClient
 179	}
 180	fn := func() (interface{}, error) {
 181		at := thefakename
 182		if strings.Contains(url, ".well-known/webfinger?resource") {
 183			at = "application/jrd+json"
 184		}
 185		j, err := junkGet(userid, url, junk.GetArgs{
 186			Accept:  at,
 187			Agent:   "honksnonk/5.0; " + serverName,
 188			Timeout: timeout,
 189			Client:  client,
 190		})
 191		return j, err
 192	}
 193
 194	ji, err := flightdeck.Call(url, fn)
 195	if err != nil {
 196		return nil, err
 197	}
 198	j := ji.(junk.Junk)
 199	return j, nil
 200}
 201
 202func fetchsome(url string) ([]byte, error) {
 203	client := http.DefaultClient
 204	if develMode {
 205		client = develClient
 206	}
 207	req, err := http.NewRequest("GET", url, nil)
 208	if err != nil {
 209		ilog.Printf("error fetching %s: %s", url, err)
 210		return nil, err
 211	}
 212	req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
 213	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
 214	defer cancel()
 215	req = req.WithContext(ctx)
 216	resp, err := client.Do(req)
 217	if err != nil {
 218		ilog.Printf("error fetching %s: %s", url, err)
 219		return nil, err
 220	}
 221	defer resp.Body.Close()
 222	switch resp.StatusCode {
 223	case 200:
 224	case 201:
 225	case 202:
 226	default:
 227		return nil, fmt.Errorf("http get not 200: %d %s", resp.StatusCode, url)
 228	}
 229	var buf bytes.Buffer
 230	limiter := io.LimitReader(resp.Body, 10*1024*1024)
 231	io.Copy(&buf, limiter)
 232	return buf.Bytes(), nil
 233}
 234
 235func savedonk(url string, name, desc, media string, localize bool) *Donk {
 236	if url == "" {
 237		return nil
 238	}
 239	if donk := finddonk(url); donk != nil {
 240		return donk
 241	}
 242	ilog.Printf("saving donk: %s", url)
 243	data := []byte{}
 244	if localize {
 245		fn := func() (interface{}, error) {
 246			return fetchsome(url)
 247		}
 248		ii, err := flightdeck.Call(url, fn)
 249		if err != nil {
 250			ilog.Printf("error fetching donk: %s", err)
 251			localize = false
 252			goto saveit
 253		}
 254		data = ii.([]byte)
 255
 256		if len(data) == 10*1024*1024 {
 257			ilog.Printf("truncation likely")
 258		}
 259		if strings.HasPrefix(media, "image") {
 260			img, err := shrinkit(data)
 261			if err != nil {
 262				ilog.Printf("unable to decode image: %s", err)
 263				localize = false
 264				data = []byte{}
 265				goto saveit
 266			}
 267			data = img.Data
 268			media = "image/" + img.Format
 269		} else if media == "application/pdf" {
 270			if len(data) > 1000000 {
 271				ilog.Printf("not saving large pdf")
 272				localize = false
 273				data = []byte{}
 274			}
 275		} else if len(data) > 100000 {
 276			ilog.Printf("not saving large attachment")
 277			localize = false
 278			data = []byte{}
 279		}
 280	}
 281saveit:
 282	fileid, err := savefile(name, desc, url, media, localize, data)
 283	if err != nil {
 284		elog.Printf("error saving file %s: %s", url, err)
 285		return nil
 286	}
 287	donk := new(Donk)
 288	donk.FileID = fileid
 289	return donk
 290}
 291
 292func iszonked(userid int64, xid string) bool {
 293	var id int64
 294	row := stmtFindZonk.QueryRow(userid, xid)
 295	err := row.Scan(&id)
 296	if err == nil {
 297		return true
 298	}
 299	if err != sql.ErrNoRows {
 300		ilog.Printf("error querying zonk: %s", err)
 301	}
 302	return false
 303}
 304
 305func needxonk(user *WhatAbout, x *Honk) bool {
 306	if rejectxonk(x) {
 307		return false
 308	}
 309	return needxonkid(user, x.XID)
 310}
 311func needbonkid(user *WhatAbout, xid string) bool {
 312	return needxonkidX(user, xid, true)
 313}
 314func needxonkid(user *WhatAbout, xid string) bool {
 315	return needxonkidX(user, xid, false)
 316}
 317func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
 318	if !strings.HasPrefix(xid, "https://") {
 319		return false
 320	}
 321	if strings.HasPrefix(xid, user.URL+"/") {
 322		return false
 323	}
 324	if rejectorigin(user.ID, xid, isannounce) {
 325		ilog.Printf("rejecting origin: %s", xid)
 326		return false
 327	}
 328	if iszonked(user.ID, xid) {
 329		ilog.Printf("already zonked: %s", xid)
 330		return false
 331	}
 332	var id int64
 333	row := stmtFindXonk.QueryRow(user.ID, xid)
 334	err := row.Scan(&id)
 335	if err == nil {
 336		return false
 337	}
 338	if err != sql.ErrNoRows {
 339		ilog.Printf("error querying xonk: %s", err)
 340	}
 341	return true
 342}
 343
 344func eradicatexonk(userid int64, xid string) {
 345	xonk := getxonk(userid, xid)
 346	if xonk != nil {
 347		deletehonk(xonk.ID)
 348	}
 349	_, err := stmtSaveZonker.Exec(userid, xid, "zonk")
 350	if err != nil {
 351		elog.Printf("error eradicating: %s", err)
 352	}
 353}
 354
 355func savexonk(x *Honk) {
 356	ilog.Printf("saving xonk: %s", x.XID)
 357	go handles(x.Honker)
 358	go handles(x.Oonker)
 359	savehonk(x)
 360}
 361
 362type Box struct {
 363	In     string
 364	Out    string
 365	Shared string
 366}
 367
 368var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) {
 369	var info string
 370	row := stmtGetXonker.QueryRow(ident, "boxes")
 371	err := row.Scan(&info)
 372	if err != nil {
 373		dlog.Printf("need to get boxes for %s", ident)
 374		var j junk.Junk
 375		j, err = GetJunk(readyLuserOne, ident)
 376		if err != nil {
 377			dlog.Printf("error getting boxes: %s", err)
 378			return nil, false
 379		}
 380		allinjest(originate(ident), j)
 381		row = stmtGetXonker.QueryRow(ident, "boxes")
 382		err = row.Scan(&info)
 383	}
 384	if err == nil {
 385		m := strings.Split(info, " ")
 386		b := &Box{In: m[0], Out: m[1], Shared: m[2]}
 387		return b, true
 388	}
 389	return nil, false
 390}})
 391
 392func gimmexonks(user *WhatAbout, outbox string) {
 393	dlog.Printf("getting outbox: %s", outbox)
 394	j, err := GetJunk(user.ID, outbox)
 395	if err != nil {
 396		ilog.Printf("error getting outbox: %s", err)
 397		return
 398	}
 399	t, _ := j.GetString("type")
 400	origin := originate(outbox)
 401	if t == "OrderedCollection" {
 402		items, _ := j.GetArray("orderedItems")
 403		if items == nil {
 404			items, _ = j.GetArray("items")
 405		}
 406		if items == nil {
 407			obj, ok := j.GetMap("first")
 408			if ok {
 409				items, _ = obj.GetArray("orderedItems")
 410			} else {
 411				page1, ok := j.GetString("first")
 412				if ok {
 413					j, err = GetJunk(user.ID, page1)
 414					if err != nil {
 415						ilog.Printf("error getting page1: %s", err)
 416						return
 417					}
 418					items, _ = j.GetArray("orderedItems")
 419				}
 420			}
 421		}
 422		if len(items) > 20 {
 423			items = items[0:20]
 424		}
 425		for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
 426			items[i], items[j] = items[j], items[i]
 427		}
 428		for _, item := range items {
 429			obj, ok := item.(junk.Junk)
 430			if ok {
 431				xonksaver(user, obj, origin)
 432				continue
 433			}
 434			xid, ok := item.(string)
 435			if ok {
 436				if !needxonkid(user, xid) {
 437					continue
 438				}
 439				obj, err = GetJunk(user.ID, xid)
 440				if err != nil {
 441					ilog.Printf("error getting item: %s", err)
 442					continue
 443				}
 444				xonksaver(user, obj, originate(xid))
 445			}
 446		}
 447	}
 448}
 449
 450func newphone(a []string, obj junk.Junk) []string {
 451	for _, addr := range []string{"to", "cc", "attributedTo"} {
 452		who, _ := obj.GetString(addr)
 453		if who != "" {
 454			a = append(a, who)
 455		}
 456		whos, _ := obj.GetArray(addr)
 457		for _, w := range whos {
 458			who, _ := w.(string)
 459			if who != "" {
 460				a = append(a, who)
 461			}
 462		}
 463	}
 464	return a
 465}
 466
 467func extractattrto(obj junk.Junk) string {
 468	who, _ := obj.GetString("attributedTo")
 469	if who != "" {
 470		return who
 471	}
 472	o, ok := obj.GetMap("attributedTo")
 473	if ok {
 474		id, ok := o.GetString("id")
 475		if ok {
 476			return id
 477		}
 478	}
 479	arr, _ := obj.GetArray("attributedTo")
 480	for _, a := range arr {
 481		o, ok := a.(junk.Junk)
 482		if ok {
 483			t, _ := o.GetString("type")
 484			id, _ := o.GetString("id")
 485			if t == "Person" || t == "" {
 486				return id
 487			}
 488		}
 489		s, ok := a.(string)
 490		if ok {
 491			return s
 492		}
 493	}
 494	return ""
 495}
 496
 497func firstofmany(obj junk.Junk, key string) string {
 498	if val, _ := obj.GetString(key); val != "" {
 499		return val
 500	}
 501	if arr, _ := obj.GetArray(key); len(arr) > 0 {
 502		val, ok := arr[0].(string)
 503		if ok {
 504			return val
 505		}
 506	}
 507	return ""
 508}
 509
 510var re_mast0link = regexp.MustCompile(`https://[[:alnum:].]+/users/[[:alnum:]]+/statuses/[[:digit:]]+`)
 511var re_masto1ink = regexp.MustCompile(`https://[[:alnum:].]+/@[[:alnum:]]+/[[:digit:]]+`)
 512var re_misslink = regexp.MustCompile(`https://[[:alnum:].]+/notes/[[:alnum:]]+`)
 513var re_honklink = regexp.MustCompile(`https://[[:alnum:].]+/u/[[:alnum:]]+/h/[[:alnum:]]+`)
 514var re_r0malink = regexp.MustCompile(`https://[[:alnum:].]+/objects/[[:alnum:]-]+`)
 515var re_roma1ink = regexp.MustCompile(`https://[[:alnum:].]+/notice/[[:alnum:]]+`)
 516var re_qtlinks = regexp.MustCompile(`>https://[^\s<]+<`)
 517
 518func qutify(user *WhatAbout, content string) string {
 519	// well this is gross
 520	malcontent := strings.ReplaceAll(content, `</span><span class="ellipsis">`, "")
 521	malcontent = strings.ReplaceAll(malcontent, `</span><span class="invisible">`, "")
 522	mlinks := re_qtlinks.FindAllString(malcontent, -1)
 523	for _, m := range mlinks {
 524		m = m[1 : len(m)-1]
 525		if re_mast0link.MatchString(m) || re_masto1ink.MatchString(m) ||
 526			re_misslink.MatchString(m) ||
 527			re_honklink.MatchString(m) ||
 528			re_r0malink.MatchString(m) || re_roma1ink.MatchString(m) {
 529			j, err := GetJunk(user.ID, m)
 530			if err == nil {
 531				q, ok := j.GetString("content")
 532				if ok {
 533					content = fmt.Sprintf("%s<blockquote>%s</blockquote>", content, q)
 534				}
 535			}
 536		}
 537	}
 538	return content
 539}
 540
 541func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
 542	depth := 0
 543	maxdepth := 10
 544	currenttid := ""
 545	goingup := 0
 546	var xonkxonkfn func(item junk.Junk, origin string, isUpdate bool) *Honk
 547
 548	saveonemore := func(xid string) {
 549		dlog.Printf("getting onemore: %s", xid)
 550		if depth >= maxdepth {
 551			ilog.Printf("in too deep")
 552			return
 553		}
 554		obj, err := GetJunkHardMode(user.ID, xid)
 555		if err != nil {
 556			ilog.Printf("error getting onemore: %s: %s", xid, err)
 557			return
 558		}
 559		depth++
 560		xonkxonkfn(obj, originate(xid), false)
 561		depth--
 562	}
 563
 564	xonkxonkfn = func(item junk.Junk, origin string, isUpdate bool) *Honk {
 565		id, _ := item.GetString("id")
 566		what := firstofmany(item, "type")
 567		dt, ok := item.GetString("published")
 568		if !ok {
 569			dt = time.Now().Format(time.RFC3339)
 570		}
 571
 572		var err error
 573		var xid, rid, url, convoy string
 574		var replies []string
 575		var obj junk.Junk
 576		waspage := false
 577		switch what {
 578		case "Delete":
 579			obj, ok = item.GetMap("object")
 580			if ok {
 581				xid, _ = obj.GetString("id")
 582			} else {
 583				xid, _ = item.GetString("object")
 584			}
 585			if xid == "" {
 586				return nil
 587			}
 588			if originate(xid) != origin {
 589				ilog.Printf("forged delete: %s", xid)
 590				return nil
 591			}
 592			ilog.Printf("eradicating %s", xid)
 593			eradicatexonk(user.ID, xid)
 594			return nil
 595		case "Remove":
 596			xid, _ = item.GetString("object")
 597			targ, _ := obj.GetString("target")
 598			ilog.Printf("remove %s from %s", obj, targ)
 599			return nil
 600		case "Tombstone":
 601			xid, _ = item.GetString("id")
 602			if xid == "" {
 603				return nil
 604			}
 605			if originate(xid) != origin {
 606				ilog.Printf("forged delete: %s", xid)
 607				return nil
 608			}
 609			ilog.Printf("eradicating %s", xid)
 610			eradicatexonk(user.ID, xid)
 611			return nil
 612		case "Announce":
 613			obj, ok = item.GetMap("object")
 614			if ok {
 615				xid, _ = obj.GetString("id")
 616			} else {
 617				xid, _ = item.GetString("object")
 618			}
 619			if !needbonkid(user, xid) {
 620				return nil
 621			}
 622			dlog.Printf("getting bonk: %s", xid)
 623			obj, err = GetJunkHardMode(user.ID, xid)
 624			if err != nil {
 625				ilog.Printf("error getting bonk: %s: %s", xid, err)
 626			}
 627			origin = originate(xid)
 628			what = "bonk"
 629		case "Update":
 630			isUpdate = true
 631			fallthrough
 632		case "Create":
 633			obj, ok = item.GetMap("object")
 634			if !ok {
 635				xid, _ = item.GetString("object")
 636				dlog.Printf("getting created honk: %s", xid)
 637				if originate(xid) != origin {
 638					ilog.Printf("out of bounds %s not from %s", xid, origin)
 639					return nil
 640				}
 641				obj, err = GetJunkHardMode(user.ID, xid)
 642				if err != nil {
 643					ilog.Printf("error getting creation: %s", err)
 644				}
 645			}
 646			if obj == nil {
 647				ilog.Printf("no object for creation %s", id)
 648				return nil
 649			}
 650			return xonkxonkfn(obj, origin, isUpdate)
 651		case "Read":
 652			xid, ok = item.GetString("object")
 653			if ok {
 654				if !needxonkid(user, xid) {
 655					dlog.Printf("don't need read obj: %s", xid)
 656					return nil
 657				}
 658				obj, err = GetJunkHardMode(user.ID, xid)
 659				if err != nil {
 660					ilog.Printf("error getting read: %s", err)
 661					return nil
 662				}
 663				return xonkxonkfn(obj, originate(xid), false)
 664			}
 665			return nil
 666		case "Add":
 667			xid, ok = item.GetString("object")
 668			if ok {
 669				// check target...
 670				if !needxonkid(user, xid) {
 671					dlog.Printf("don't need added obj: %s", xid)
 672					return nil
 673				}
 674				obj, err = GetJunkHardMode(user.ID, xid)
 675				if err != nil {
 676					ilog.Printf("error getting add: %s", err)
 677					return nil
 678				}
 679				return xonkxonkfn(obj, originate(xid), false)
 680			}
 681			return nil
 682		case "Move":
 683			obj = item
 684			what = "move"
 685		case "Page":
 686			waspage = true
 687			fallthrough
 688		case "Audio":
 689			fallthrough
 690		case "Image":
 691			fallthrough
 692		case "Video":
 693			fallthrough
 694		case "Question":
 695			fallthrough
 696		case "Note":
 697			fallthrough
 698		case "Article":
 699			obj = item
 700			what = "honk"
 701		case "Event":
 702			obj = item
 703			what = "event"
 704		case "ChatMessage":
 705			obj = item
 706			what = "chonk"
 707		default:
 708			ilog.Printf("unknown activity: %s", what)
 709			dumpactivity(item)
 710			return nil
 711		}
 712
 713		if obj != nil {
 714			xid, _ = obj.GetString("id")
 715		}
 716
 717		if xid == "" {
 718			ilog.Printf("don't know what xid is")
 719			item.Write(ilog.Writer())
 720			return nil
 721		}
 722		if originate(xid) != origin {
 723			if !develMode && origin != "" {
 724				ilog.Printf("original sin: %s not from %s", xid, origin)
 725				item.Write(ilog.Writer())
 726				return nil
 727			}
 728		}
 729
 730		var xonk Honk
 731		// early init
 732		xonk.XID = xid
 733		xonk.UserID = user.ID
 734		xonk.Honker, _ = item.GetString("actor")
 735		if xonk.Honker == "" {
 736			xonk.Honker, _ = item.GetString("attributedTo")
 737		}
 738		if obj != nil {
 739			if xonk.Honker == "" {
 740				xonk.Honker = extractattrto(obj)
 741			}
 742			xonk.Oonker = extractattrto(obj)
 743			if xonk.Oonker == xonk.Honker {
 744				xonk.Oonker = ""
 745			}
 746			xonk.Audience = newphone(nil, obj)
 747		}
 748		xonk.Audience = append(xonk.Audience, xonk.Honker)
 749		xonk.Audience = oneofakind(xonk.Audience)
 750		xonk.Public = loudandproud(xonk.Audience)
 751
 752		var mentions []Mention
 753		if obj != nil {
 754			ot, _ := obj.GetString("type")
 755			url, _ = obj.GetString("url")
 756			if dt2, ok := obj.GetString("published"); ok {
 757				dt = dt2
 758			}
 759			content, _ := obj.GetString("content")
 760			if !strings.HasPrefix(content, "<p>") {
 761				content = "<p>" + content
 762			}
 763			precis, _ := obj.GetString("summary")
 764			if name, ok := obj.GetString("name"); ok {
 765				if precis != "" {
 766					content = precis + "<p>" + content
 767				}
 768				precis = html.EscapeString(name)
 769			}
 770			if sens, _ := obj["sensitive"].(bool); sens && precis == "" {
 771				precis = "unspecified horror"
 772			}
 773			if waspage {
 774				content += fmt.Sprintf(`<p><a href="%s">%s</a>`, url, url)
 775				url = xid
 776			}
 777			if user.Options.InlineQuotes {
 778				content = qutify(user, content)
 779			}
 780			rid, ok = obj.GetString("inReplyTo")
 781			if !ok {
 782				if robj, ok := obj.GetMap("inReplyTo"); ok {
 783					rid, _ = robj.GetString("id")
 784				}
 785			}
 786			convoy, _ = obj.GetString("context")
 787			if convoy == "" {
 788				convoy, _ = obj.GetString("conversation")
 789			}
 790			if ot == "Question" {
 791				if what == "honk" {
 792					what = "qonk"
 793				}
 794				content += "<ul>"
 795				ans, _ := obj.GetArray("oneOf")
 796				for _, ai := range ans {
 797					a, ok := ai.(junk.Junk)
 798					if !ok {
 799						continue
 800					}
 801					as, _ := a.GetString("name")
 802					content += "<li>" + as
 803				}
 804				ans, _ = obj.GetArray("anyOf")
 805				for _, ai := range ans {
 806					a, ok := ai.(junk.Junk)
 807					if !ok {
 808						continue
 809					}
 810					as, _ := a.GetString("name")
 811					content += "<li>" + as
 812				}
 813				content += "</ul>"
 814			}
 815			if ot == "Move" {
 816				targ, _ := obj.GetString("target")
 817				content += string(templates.Sprintf(`<p>Moved to <a href="%s">%s</a>`, targ, targ))
 818			}
 819			if what == "honk" && rid != "" {
 820				what = "tonk"
 821			}
 822			if len(content) > 90001 {
 823				ilog.Printf("content too long. truncating")
 824				content = content[:90001]
 825			}
 826
 827			xonk.Noise = content
 828			xonk.Precis = precis
 829			if rejectxonk(&xonk) {
 830				dlog.Printf("fast reject: %s", xid)
 831				return nil
 832			}
 833
 834			numatts := 0
 835			procatt := func(att junk.Junk) {
 836				at, _ := att.GetString("type")
 837				mt, _ := att.GetString("mediaType")
 838				u, ok := att.GetString("url")
 839				if !ok {
 840					if ua, ok := att.GetArray("url"); ok && len(ua) > 0 {
 841						u, ok = ua[0].(string)
 842						if !ok {
 843							if uu, ok := ua[0].(junk.Junk); ok {
 844								u, _ = uu.GetString("href")
 845								if mt == "" {
 846									mt, _ = uu.GetString("mediaType")
 847								}
 848							}
 849						}
 850					} else if uu, ok := att.GetMap("url"); ok {
 851						u, _ = uu.GetString("href")
 852						if mt == "" {
 853							mt, _ = uu.GetString("mediaType")
 854						}
 855					}
 856				}
 857				name, _ := att.GetString("name")
 858				desc, _ := att.GetString("summary")
 859				desc = html.UnescapeString(desc)
 860				if desc == "" {
 861					desc = name
 862				}
 863				localize := false
 864				if numatts > 4 {
 865					ilog.Printf("excessive attachment: %s", at)
 866				} else if at == "Document" || at == "Image" {
 867					mt = strings.ToLower(mt)
 868					dlog.Printf("attachment: %s %s", mt, u)
 869					if mt == "text/plain" || mt == "application/pdf" ||
 870						strings.HasPrefix(mt, "image") {
 871						localize = true
 872					}
 873				} else {
 874					ilog.Printf("unknown attachment: %s", at)
 875				}
 876				if skipMedia(&xonk) {
 877					localize = false
 878				}
 879				donk := savedonk(u, name, desc, mt, localize)
 880				if donk != nil {
 881					xonk.Donks = append(xonk.Donks, donk)
 882				}
 883				numatts++
 884			}
 885			atts, _ := obj.GetArray("attachment")
 886			for _, atti := range atts {
 887				att, ok := atti.(junk.Junk)
 888				if !ok {
 889					ilog.Printf("attachment that wasn't map?")
 890					continue
 891				}
 892				procatt(att)
 893			}
 894			if att, ok := obj.GetMap("attachment"); ok {
 895				procatt(att)
 896			}
 897			tags, _ := obj.GetArray("tag")
 898			for _, tagi := range tags {
 899				tag, ok := tagi.(junk.Junk)
 900				if !ok {
 901					continue
 902				}
 903				tt, _ := tag.GetString("type")
 904				name, _ := tag.GetString("name")
 905				desc, _ := tag.GetString("summary")
 906				desc = html.UnescapeString(desc)
 907				if desc == "" {
 908					desc = name
 909				}
 910				if tt == "Emoji" {
 911					icon, _ := tag.GetMap("icon")
 912					mt, _ := icon.GetString("mediaType")
 913					if mt == "" {
 914						mt = "image/png"
 915					}
 916					u, _ := icon.GetString("url")
 917					donk := savedonk(u, name, desc, mt, true)
 918					if donk != nil {
 919						xonk.Donks = append(xonk.Donks, donk)
 920					}
 921				}
 922				if tt == "Hashtag" {
 923					if name == "" || name == "#" {
 924						// skip it
 925					} else {
 926						if name[0] != '#' {
 927							name = "#" + name
 928						}
 929						xonk.Onts = append(xonk.Onts, name)
 930					}
 931				}
 932				if tt == "Place" {
 933					p := new(Place)
 934					p.Name = name
 935					p.Latitude, _ = tag.GetNumber("latitude")
 936					p.Longitude, _ = tag.GetNumber("longitude")
 937					p.Url, _ = tag.GetString("url")
 938					xonk.Place = p
 939				}
 940				if tt == "Mention" {
 941					var m Mention
 942					m.Who, _ = tag.GetString("name")
 943					m.Where, _ = tag.GetString("href")
 944					mentions = append(mentions, m)
 945				}
 946			}
 947			if starttime, ok := obj.GetString("startTime"); ok {
 948				if start, err := time.Parse(time.RFC3339, starttime); err == nil {
 949					t := new(Time)
 950					t.StartTime = start
 951					endtime, _ := obj.GetString("endTime")
 952					t.EndTime, _ = time.Parse(time.RFC3339, endtime)
 953					dura, _ := obj.GetString("duration")
 954					if strings.HasPrefix(dura, "PT") {
 955						dura = strings.ToLower(dura[2:])
 956						d, _ := time.ParseDuration(dura)
 957						t.Duration = Duration(d)
 958					}
 959					xonk.Time = t
 960				}
 961			}
 962			if loca, ok := obj.GetMap("location"); ok {
 963				if tt, _ := loca.GetString("type"); tt == "Place" {
 964					p := new(Place)
 965					p.Name, _ = loca.GetString("name")
 966					p.Latitude, _ = loca.GetNumber("latitude")
 967					p.Longitude, _ = loca.GetNumber("longitude")
 968					p.Url, _ = loca.GetString("url")
 969					xonk.Place = p
 970				}
 971			}
 972
 973			xonk.Onts = oneofakind(xonk.Onts)
 974			replyobj, ok := obj.GetMap("replies")
 975			if ok {
 976				items, ok := replyobj.GetArray("items")
 977				if !ok {
 978					first, ok := replyobj.GetMap("first")
 979					if ok {
 980						items, _ = first.GetArray("items")
 981					}
 982				}
 983				for _, repl := range items {
 984					s, ok := repl.(string)
 985					if ok {
 986						replies = append(replies, s)
 987					}
 988				}
 989			}
 990
 991		}
 992
 993		if currenttid == "" {
 994			currenttid = convoy
 995		}
 996
 997		// init xonk
 998		xonk.What = what
 999		xonk.RID = rid
1000		xonk.Date, _ = time.Parse(time.RFC3339, dt)
1001		xonk.URL = url
1002		xonk.Format = "html"
1003		xonk.Convoy = convoy
1004		xonk.Mentions = mentions
1005		for _, m := range mentions {
1006			if m.Where == user.URL {
1007				xonk.Whofore = 1
1008			}
1009		}
1010		imaginate(&xonk)
1011
1012		if what == "chonk" {
1013			ch := Chonk{
1014				UserID: xonk.UserID,
1015				XID:    xid,
1016				Who:    xonk.Honker,
1017				Target: xonk.Honker,
1018				Date:   xonk.Date,
1019				Noise:  xonk.Noise,
1020				Format: xonk.Format,
1021				Donks:  xonk.Donks,
1022			}
1023			savechonk(&ch)
1024			return nil
1025		}
1026
1027		if isUpdate {
1028			dlog.Printf("something has changed! %s", xonk.XID)
1029			prev := getxonk(user.ID, xonk.XID)
1030			if prev == nil {
1031				ilog.Printf("didn't find old version for update: %s", xonk.XID)
1032				isUpdate = false
1033			} else {
1034				xonk.ID = prev.ID
1035				updatehonk(&xonk)
1036			}
1037		}
1038		if !isUpdate && needxonk(user, &xonk) {
1039			if rid != "" && xonk.Public {
1040				if needxonkid(user, rid) {
1041					goingup++
1042					saveonemore(rid)
1043					goingup--
1044				}
1045				if convoy == "" {
1046					xx := getxonk(user.ID, rid)
1047					if xx != nil {
1048						convoy = xx.Convoy
1049					}
1050				}
1051			}
1052			if convoy == "" {
1053				convoy = currenttid
1054			}
1055			if convoy == "" {
1056				convoy = "data:,missing-" + xfiltrate()
1057				currenttid = convoy
1058			}
1059			xonk.Convoy = convoy
1060			savexonk(&xonk)
1061		}
1062		if goingup == 0 {
1063			for _, replid := range replies {
1064				if needxonkid(user, replid) {
1065					dlog.Printf("missing a reply: %s", replid)
1066					saveonemore(replid)
1067				}
1068			}
1069		}
1070		return &xonk
1071	}
1072
1073	return xonkxonkfn(item, origin, false)
1074}
1075
1076func dumpactivity(item junk.Junk) {
1077	fd, err := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
1078	if err != nil {
1079		elog.Printf("error opening inbox! %s", err)
1080		return
1081	}
1082	defer fd.Close()
1083	item.Write(fd)
1084	io.WriteString(fd, "\n")
1085}
1086
1087func rubadubdub(user *WhatAbout, req junk.Junk) {
1088	actor, _ := req.GetString("actor")
1089	j := junk.New()
1090	j["@context"] = itiswhatitis
1091	j["id"] = user.URL + "/dub/" + xfiltrate()
1092	j["type"] = "Accept"
1093	j["actor"] = user.URL
1094	j["to"] = actor
1095	j["published"] = time.Now().UTC().Format(time.RFC3339)
1096	j["object"] = req
1097
1098	deliverate(0, user.ID, actor, j.ToBytes(), true)
1099}
1100
1101func itakeitallback(user *WhatAbout, xid string, owner string, folxid string) {
1102	j := junk.New()
1103	j["@context"] = itiswhatitis
1104	j["id"] = user.URL + "/unsub/" + folxid
1105	j["type"] = "Undo"
1106	j["actor"] = user.URL
1107	j["to"] = owner
1108	f := junk.New()
1109	f["id"] = user.URL + "/sub/" + folxid
1110	f["type"] = "Follow"
1111	f["actor"] = user.URL
1112	f["to"] = owner
1113	f["object"] = xid
1114	j["object"] = f
1115	j["published"] = time.Now().UTC().Format(time.RFC3339)
1116
1117	deliverate(0, user.ID, owner, j.ToBytes(), true)
1118}
1119
1120func subsub(user *WhatAbout, xid string, owner string, folxid string) {
1121	if xid == "" {
1122		ilog.Printf("can't subscribe to empty")
1123		return
1124	}
1125	j := junk.New()
1126	j["@context"] = itiswhatitis
1127	j["id"] = user.URL + "/sub/" + folxid
1128	j["type"] = "Follow"
1129	j["actor"] = user.URL
1130	j["to"] = owner
1131	j["object"] = xid
1132	j["published"] = time.Now().UTC().Format(time.RFC3339)
1133
1134	deliverate(0, user.ID, owner, j.ToBytes(), true)
1135}
1136
1137func activatedonks(donks []*Donk) []junk.Junk {
1138	var atts []junk.Junk
1139	for _, d := range donks {
1140		if re_emus.MatchString(d.Name) {
1141			continue
1142		}
1143		jd := junk.New()
1144		jd["mediaType"] = d.Media
1145		jd["name"] = d.Name
1146		jd["summary"] = html.EscapeString(d.Desc)
1147		jd["type"] = "Document"
1148		jd["url"] = d.URL
1149		atts = append(atts, jd)
1150	}
1151	return atts
1152}
1153
1154// returns activity, object
1155func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
1156	dt := h.Date.Format(time.RFC3339)
1157	var jo junk.Junk
1158	j := junk.New()
1159	j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
1160	j["actor"] = user.URL
1161	j["published"] = dt
1162	j["to"] = h.Audience[0]
1163	if len(h.Audience) > 1 {
1164		j["cc"] = h.Audience[1:]
1165	}
1166
1167	switch h.What {
1168	case "update":
1169		fallthrough
1170	case "tonk":
1171		fallthrough
1172	case "event":
1173		fallthrough
1174	case "honk":
1175		j["type"] = "Create"
1176		jo = junk.New()
1177		jo["id"] = h.XID
1178		jo["type"] = "Note"
1179		if h.What == "event" {
1180			jo["type"] = "Event"
1181		}
1182		if h.What == "update" {
1183			j["type"] = "Update"
1184			jo["updated"] = dt
1185		}
1186		jo["published"] = dt
1187		jo["url"] = h.XID
1188		jo["attributedTo"] = user.URL
1189		if h.RID != "" {
1190			jo["inReplyTo"] = h.RID
1191		}
1192		if h.Convoy != "" {
1193			jo["context"] = h.Convoy
1194			jo["conversation"] = h.Convoy
1195		}
1196		jo["to"] = h.Audience[0]
1197		if len(h.Audience) > 1 {
1198			jo["cc"] = h.Audience[1:]
1199		}
1200		if !h.Public {
1201			jo["directMessage"] = true
1202		}
1203		translate(h)
1204		redoimages(h)
1205		if h.Precis != "" {
1206			jo["sensitive"] = true
1207		}
1208
1209		var replies []string
1210		for _, reply := range h.Replies {
1211			replies = append(replies, reply.XID)
1212		}
1213		if len(replies) > 0 {
1214			jr := junk.New()
1215			jr["type"] = "Collection"
1216			jr["totalItems"] = len(replies)
1217			jr["items"] = replies
1218			jo["replies"] = jr
1219		}
1220
1221		var tags []junk.Junk
1222		for _, m := range h.Mentions {
1223			t := junk.New()
1224			t["type"] = "Mention"
1225			t["name"] = m.Who
1226			t["href"] = m.Where
1227			tags = append(tags, t)
1228		}
1229		for _, o := range h.Onts {
1230			t := junk.New()
1231			t["type"] = "Hashtag"
1232			o = strings.ToLower(o)
1233			t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1234			t["name"] = o
1235			tags = append(tags, t)
1236		}
1237		for _, e := range herdofemus(h.Noise) {
1238			t := junk.New()
1239			t["id"] = e.ID
1240			t["type"] = "Emoji"
1241			t["name"] = e.Name
1242			i := junk.New()
1243			i["type"] = "Image"
1244			i["mediaType"] = e.Type
1245			i["url"] = e.ID
1246			t["icon"] = i
1247			tags = append(tags, t)
1248		}
1249		for _, e := range fixupflags(h) {
1250			t := junk.New()
1251			t["id"] = e.ID
1252			t["type"] = "Emoji"
1253			t["name"] = e.Name
1254			i := junk.New()
1255			i["type"] = "Image"
1256			i["mediaType"] = "image/png"
1257			i["url"] = e.ID
1258			t["icon"] = i
1259			tags = append(tags, t)
1260		}
1261		if len(tags) > 0 {
1262			jo["tag"] = tags
1263		}
1264		if p := h.Place; p != nil {
1265			t := junk.New()
1266			t["type"] = "Place"
1267			if p.Name != "" {
1268				t["name"] = p.Name
1269			}
1270			if p.Latitude != 0 {
1271				t["latitude"] = p.Latitude
1272			}
1273			if p.Longitude != 0 {
1274				t["longitude"] = p.Longitude
1275			}
1276			if p.Url != "" {
1277				t["url"] = p.Url
1278			}
1279			jo["location"] = t
1280		}
1281		if t := h.Time; t != nil {
1282			jo["startTime"] = t.StartTime.Format(time.RFC3339)
1283			if t.Duration != 0 {
1284				jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1285			}
1286		}
1287		atts := activatedonks(h.Donks)
1288		if len(atts) > 0 {
1289			jo["attachment"] = atts
1290		}
1291		jo["summary"] = h.Precis
1292		jo["content"] = h.Noise
1293		j["object"] = jo
1294	case "bonk":
1295		j["type"] = "Announce"
1296		if h.Convoy != "" {
1297			j["context"] = h.Convoy
1298		}
1299		j["object"] = h.XID
1300	case "unbonk":
1301		b := junk.New()
1302		b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1303		b["type"] = "Announce"
1304		b["actor"] = user.URL
1305		if h.Convoy != "" {
1306			b["context"] = h.Convoy
1307		}
1308		b["object"] = h.XID
1309		j["type"] = "Undo"
1310		j["object"] = b
1311	case "zonk":
1312		j["type"] = "Delete"
1313		j["object"] = h.XID
1314	case "ack":
1315		j["type"] = "Read"
1316		j["object"] = h.XID
1317		if h.Convoy != "" {
1318			j["context"] = h.Convoy
1319		}
1320	case "react":
1321		j["type"] = "EmojiReact"
1322		j["object"] = h.XID
1323		if h.Convoy != "" {
1324			j["context"] = h.Convoy
1325		}
1326		j["content"] = h.Noise
1327	case "deack":
1328		b := junk.New()
1329		b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1330		b["type"] = "Read"
1331		b["actor"] = user.URL
1332		b["object"] = h.XID
1333		if h.Convoy != "" {
1334			b["context"] = h.Convoy
1335		}
1336		j["type"] = "Undo"
1337		j["object"] = b
1338	}
1339
1340	return j, jo
1341}
1342
1343var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1344	row := stmtAnyXonk.QueryRow(xid)
1345	honk := scanhonk(row)
1346	if honk == nil || !honk.Public {
1347		return nil, true
1348	}
1349	user, _ := butwhatabout(honk.Username)
1350	rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1351	reversehonks(rawhonks)
1352	for _, h := range rawhonks {
1353		if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1354			honk.Replies = append(honk.Replies, h)
1355		}
1356	}
1357	donksforhonks([]*Honk{honk})
1358	_, j := jonkjonk(user, honk)
1359	if j == nil {
1360		elog.Fatalf("what just happened? %v", honk)
1361	}
1362	j["@context"] = itiswhatitis
1363
1364	return j.ToBytes(), true
1365}, Limit: 128})
1366
1367func gimmejonk(xid string) ([]byte, bool) {
1368	var j []byte
1369	ok := oldjonks.Get(xid, &j)
1370	return j, ok
1371}
1372
1373func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1374	rcpts := make(map[string]bool)
1375	for _, a := range addresses {
1376		if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1377			continue
1378		}
1379		if a[0] == '%' {
1380			rcpts[a] = true
1381			continue
1382		}
1383		var box *Box
1384		ok := boxofboxes.Get(a, &box)
1385		if ok && useshared && box.Shared != "" {
1386			rcpts["%"+box.Shared] = true
1387		} else {
1388			rcpts[a] = true
1389		}
1390	}
1391	return rcpts
1392}
1393
1394func chonkifymsg(user *WhatAbout, ch *Chonk) []byte {
1395	dt := ch.Date.Format(time.RFC3339)
1396	aud := []string{ch.Target}
1397
1398	jo := junk.New()
1399	jo["id"] = ch.XID
1400	jo["type"] = "ChatMessage"
1401	jo["published"] = dt
1402	jo["attributedTo"] = user.URL
1403	jo["to"] = aud
1404	jo["content"] = ch.HTML
1405	atts := activatedonks(ch.Donks)
1406	if len(atts) > 0 {
1407		jo["attachment"] = atts
1408	}
1409	var tags []junk.Junk
1410	for _, e := range herdofemus(ch.Noise) {
1411		t := junk.New()
1412		t["id"] = e.ID
1413		t["type"] = "Emoji"
1414		t["name"] = e.Name
1415		i := junk.New()
1416		i["type"] = "Image"
1417		i["mediaType"] = e.Type
1418		i["url"] = e.ID
1419		t["icon"] = i
1420		tags = append(tags, t)
1421	}
1422	if len(tags) > 0 {
1423		jo["tag"] = tags
1424	}
1425
1426	j := junk.New()
1427	j["@context"] = itiswhatitis
1428	j["id"] = user.URL + "/" + "honk" + "/" + shortxid(ch.XID)
1429	j["type"] = "Create"
1430	j["actor"] = user.URL
1431	j["published"] = dt
1432	j["to"] = aud
1433	j["object"] = jo
1434
1435	return j.ToBytes()
1436}
1437
1438func sendchonk(user *WhatAbout, ch *Chonk) {
1439	msg := chonkifymsg(user, ch)
1440
1441	rcpts := make(map[string]bool)
1442	rcpts[ch.Target] = true
1443	for a := range rcpts {
1444		go deliverate(0, user.ID, a, msg, true)
1445	}
1446}
1447
1448func honkworldwide(user *WhatAbout, honk *Honk) {
1449	jonk, _ := jonkjonk(user, honk)
1450	jonk["@context"] = itiswhatitis
1451	msg := jonk.ToBytes()
1452
1453	rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1454
1455	if honk.Public {
1456		for _, h := range getdubs(user.ID) {
1457			if h.XID == user.URL {
1458				continue
1459			}
1460			var box *Box
1461			ok := boxofboxes.Get(h.XID, &box)
1462			if ok && box.Shared != "" {
1463				rcpts["%"+box.Shared] = true
1464			} else {
1465				rcpts[h.XID] = true
1466			}
1467		}
1468		for _, f := range getbacktracks(honk.XID) {
1469			if f[0] == '%' {
1470				rcpts[f] = true
1471			} else {
1472				var box *Box
1473				ok := boxofboxes.Get(f, &box)
1474				if ok && box.Shared != "" {
1475					rcpts["%"+box.Shared] = true
1476				} else {
1477					rcpts[f] = true
1478				}
1479			}
1480		}
1481	}
1482	for a := range rcpts {
1483		go deliverate(0, user.ID, a, msg, doesitmatter(honk.What))
1484	}
1485	if honk.Public && len(honk.Onts) > 0 {
1486		collectiveaction(honk)
1487	}
1488}
1489
1490func doesitmatter(what string) bool {
1491	switch what {
1492	case "ack":
1493		return false
1494	case "react":
1495		return false
1496	case "deack":
1497		return false
1498	}
1499	return true
1500}
1501
1502func collectiveaction(honk *Honk) {
1503	user := getserveruser()
1504	for _, ont := range honk.Onts {
1505		dubs := getnameddubs(readyLuserOne, ont)
1506		if len(dubs) == 0 {
1507			continue
1508		}
1509		j := junk.New()
1510		j["@context"] = itiswhatitis
1511		j["type"] = "Add"
1512		j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1513		j["actor"] = user.URL
1514		j["object"] = honk.XID
1515		j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1516		rcpts := make(map[string]bool)
1517		for _, dub := range dubs {
1518			var box *Box
1519			ok := boxofboxes.Get(dub.XID, &box)
1520			if ok && box.Shared != "" {
1521				rcpts["%"+box.Shared] = true
1522			} else {
1523				rcpts[dub.XID] = true
1524			}
1525		}
1526		msg := j.ToBytes()
1527		for a := range rcpts {
1528			go deliverate(0, user.ID, a, msg, false)
1529		}
1530	}
1531}
1532
1533func junkuser(user *WhatAbout) junk.Junk {
1534	j := junk.New()
1535	j["@context"] = itiswhatitis
1536	j["id"] = user.URL
1537	j["inbox"] = user.URL + "/inbox"
1538	j["outbox"] = user.URL + "/outbox"
1539	j["name"] = user.Display
1540	j["preferredUsername"] = user.Name
1541	j["summary"] = user.HTAbout
1542	var tags []junk.Junk
1543	for _, o := range user.Onts {
1544		t := junk.New()
1545		t["type"] = "Hashtag"
1546		o = strings.ToLower(o)
1547		t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1548		t["name"] = o
1549		tags = append(tags, t)
1550	}
1551	if len(tags) > 0 {
1552		j["tag"] = tags
1553	}
1554
1555	if user.ID > 0 {
1556		j["type"] = "Person"
1557		j["url"] = user.URL
1558		j["followers"] = user.URL + "/followers"
1559		j["following"] = user.URL + "/following"
1560		a := junk.New()
1561		a["type"] = "Image"
1562		a["mediaType"] = "image/png"
1563		if ava := user.Options.Avatar; ava != "" {
1564			a["url"] = ava
1565		} else {
1566			u := fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1567			a["url"] = u
1568		}
1569		j["icon"] = a
1570		if ban := user.Options.Banner; ban != "" {
1571			a := junk.New()
1572			a["type"] = "Image"
1573			a["mediaType"] = "image/jpg"
1574			a["url"] = ban
1575			j["image"] = a
1576		}
1577	} else {
1578		j["type"] = "Service"
1579	}
1580	k := junk.New()
1581	k["id"] = user.URL + "#key"
1582	k["owner"] = user.URL
1583	k["publicKeyPem"] = user.Key
1584	j["publicKey"] = k
1585
1586	return j
1587}
1588
1589var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1590	user, err := butwhatabout(name)
1591	if err != nil {
1592		return nil, false
1593	}
1594	var buf bytes.Buffer
1595	j := junkuser(user)
1596	j.Write(&buf)
1597	return buf.Bytes(), true
1598}, Duration: 1 * time.Minute})
1599
1600func asjonker(name string) ([]byte, bool) {
1601	var j []byte
1602	ok := oldjonkers.Get(name, &j)
1603	return j, ok
1604}
1605
1606var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1607	m := strings.Split(name, "@")
1608	if len(m) != 2 {
1609		dlog.Printf("bad fish name: %s", name)
1610		return "", true
1611	}
1612	var href string
1613	row := stmtGetXonker.QueryRow(name, "fishname")
1614	err := row.Scan(&href)
1615	if err == nil {
1616		return href, true
1617	}
1618	dlog.Printf("fishing for %s", name)
1619	j, err := GetJunkFast(readyLuserOne, fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1620	if err != nil {
1621		ilog.Printf("failed to go fish %s: %s", name, err)
1622		return "", true
1623	}
1624	links, _ := j.GetArray("links")
1625	for _, li := range links {
1626		l, ok := li.(junk.Junk)
1627		if !ok {
1628			continue
1629		}
1630		href, _ := l.GetString("href")
1631		rel, _ := l.GetString("rel")
1632		t, _ := l.GetString("type")
1633		if rel == "self" && friendorfoe(t) {
1634			when := time.Now().UTC().Format(dbtimeformat)
1635			_, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1636			if err != nil {
1637				elog.Printf("error saving fishname: %s", err)
1638			}
1639			return href, true
1640		}
1641	}
1642	return href, true
1643}, Duration: 1 * time.Minute})
1644
1645func gofish(name string) string {
1646	if name[0] == '@' {
1647		name = name[1:]
1648	}
1649	var href string
1650	handfull.Get(name, &href)
1651	return href
1652}
1653
1654func investigate(name string) (*SomeThing, error) {
1655	if name == "" {
1656		return nil, fmt.Errorf("no name")
1657	}
1658	if name[0] == '@' {
1659		name = gofish(name)
1660	}
1661	if name == "" {
1662		return nil, fmt.Errorf("no name")
1663	}
1664	obj, err := GetJunkFast(readyLuserOne, name)
1665	if err != nil {
1666		return nil, err
1667	}
1668	allinjest(originate(name), obj)
1669	return somethingabout(obj)
1670}
1671
1672func somethingabout(obj junk.Junk) (*SomeThing, error) {
1673	info := new(SomeThing)
1674	t, _ := obj.GetString("type")
1675	switch t {
1676	case "Person":
1677		fallthrough
1678	case "Organization":
1679		fallthrough
1680	case "Application":
1681		fallthrough
1682	case "Service":
1683		info.What = SomeActor
1684	case "OrderedCollection":
1685		fallthrough
1686	case "Collection":
1687		info.What = SomeCollection
1688	default:
1689		return nil, fmt.Errorf("unknown object type")
1690	}
1691	info.XID, _ = obj.GetString("id")
1692	info.Name, _ = obj.GetString("preferredUsername")
1693	if info.Name == "" {
1694		info.Name, _ = obj.GetString("name")
1695	}
1696	info.Owner, _ = obj.GetString("attributedTo")
1697	if info.Owner == "" {
1698		info.Owner = info.XID
1699	}
1700	return info, nil
1701}
1702
1703func allinjest(origin string, obj junk.Junk) {
1704	keyobj, ok := obj.GetMap("publicKey")
1705	if ok {
1706		ingestpubkey(origin, keyobj)
1707	}
1708	ingestboxes(origin, obj)
1709	ingesthandle(origin, obj)
1710}
1711
1712func ingestpubkey(origin string, obj junk.Junk) {
1713	keyobj, ok := obj.GetMap("publicKey")
1714	if ok {
1715		obj = keyobj
1716	}
1717	keyname, ok := obj.GetString("id")
1718	var data string
1719	row := stmtGetXonker.QueryRow(keyname, "pubkey")
1720	err := row.Scan(&data)
1721	if err == nil {
1722		return
1723	}
1724	if !ok || origin != originate(keyname) {
1725		ilog.Printf("bad key origin %s <> %s", origin, keyname)
1726		return
1727	}
1728	dlog.Printf("ingesting a needed pubkey: %s", keyname)
1729	owner, ok := obj.GetString("owner")
1730	if !ok {
1731		ilog.Printf("error finding %s pubkey owner", keyname)
1732		return
1733	}
1734	data, ok = obj.GetString("publicKeyPem")
1735	if !ok {
1736		ilog.Printf("error finding %s pubkey", keyname)
1737		return
1738	}
1739	if originate(owner) != origin {
1740		ilog.Printf("bad key owner: %s <> %s", owner, origin)
1741		return
1742	}
1743	_, _, err = httpsig.DecodeKey(data)
1744	if err != nil {
1745		ilog.Printf("error decoding %s pubkey: %s", keyname, err)
1746		return
1747	}
1748	when := time.Now().UTC().Format(dbtimeformat)
1749	_, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1750	if err != nil {
1751		elog.Printf("error saving key: %s", err)
1752	}
1753}
1754
1755func ingestboxes(origin string, obj junk.Junk) {
1756	ident, _ := obj.GetString("id")
1757	if ident == "" {
1758		return
1759	}
1760	if originate(ident) != origin {
1761		return
1762	}
1763	var info string
1764	row := stmtGetXonker.QueryRow(ident, "boxes")
1765	err := row.Scan(&info)
1766	if err == nil {
1767		return
1768	}
1769	dlog.Printf("ingesting boxes: %s", ident)
1770	inbox, _ := obj.GetString("inbox")
1771	outbox, _ := obj.GetString("outbox")
1772	sbox, _ := obj.GetString("endpoints", "sharedInbox")
1773	if inbox != "" {
1774		when := time.Now().UTC().Format(dbtimeformat)
1775		m := strings.Join([]string{inbox, outbox, sbox}, " ")
1776		_, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1777		if err != nil {
1778			elog.Printf("error saving boxes: %s", err)
1779		}
1780	}
1781}
1782
1783func ingesthandle(origin string, obj junk.Junk) {
1784	xid, _ := obj.GetString("id")
1785	if xid == "" {
1786		return
1787	}
1788	if originate(xid) != origin {
1789		return
1790	}
1791	var handle string
1792	row := stmtGetXonker.QueryRow(xid, "handle")
1793	err := row.Scan(&handle)
1794	if err == nil {
1795		return
1796	}
1797	handle, _ = obj.GetString("preferredUsername")
1798	if handle != "" {
1799		when := time.Now().UTC().Format(dbtimeformat)
1800		_, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1801		if err != nil {
1802			elog.Printf("error saving handle: %s", err)
1803		}
1804	}
1805}
1806
1807func updateMe(username string) {
1808	var user *WhatAbout
1809	somenamedusers.Get(username, &user)
1810	dt := time.Now().UTC().Format(time.RFC3339)
1811	j := junk.New()
1812	j["@context"] = itiswhatitis
1813	j["id"] = fmt.Sprintf("%s/upme/%s/%d", user.URL, user.Name, time.Now().Unix())
1814	j["actor"] = user.URL
1815	j["published"] = dt
1816	j["to"] = thewholeworld
1817	j["type"] = "Update"
1818	j["object"] = junkuser(user)
1819
1820	msg := j.ToBytes()
1821
1822	rcpts := make(map[string]bool)
1823	for _, f := range getdubs(user.ID) {
1824		if f.XID == user.URL {
1825			continue
1826		}
1827		var box *Box
1828		boxofboxes.Get(f.XID, &box)
1829		if box != nil && box.Shared != "" {
1830			rcpts["%"+box.Shared] = true
1831		} else {
1832			rcpts[f.XID] = true
1833		}
1834	}
1835	for a := range rcpts {
1836		go deliverate(0, user.ID, a, msg, false)
1837	}
1838}
1839
1840func followme(user *WhatAbout, who string, name string, j junk.Junk) {
1841	folxid, _ := j.GetString("id")
1842
1843	ilog.Printf("updating honker follow: %s %s", who, folxid)
1844
1845	var x string
1846	db := opendatabase()
1847	row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", name, who, user.ID)
1848	err := row.Scan(&x)
1849	if err != sql.ErrNoRows {
1850		ilog.Printf("duplicate follow request: %s", who)
1851		_, err = stmtUpdateFlavor.Exec("dub", folxid, user.ID, name, who, "undub")
1852		if err != nil {
1853			elog.Printf("error updating honker: %s", err)
1854		}
1855	} else {
1856		stmtSaveDub.Exec(user.ID, name, who, "dub", folxid)
1857	}
1858	go rubadubdub(user, j)
1859}
1860
1861func unfollowme(user *WhatAbout, who string, name string, j junk.Junk) {
1862	var folxid string
1863	if who == "" {
1864		folxid, _ = j.GetString("object")
1865
1866		db := opendatabase()
1867		row := db.QueryRow("select xid, name from honkers where userid = ? and folxid = ? and flavor in ('dub', 'undub')", user.ID, folxid)
1868		err := row.Scan(&who, &name)
1869		if err != nil {
1870			if err != sql.ErrNoRows {
1871				elog.Printf("error scanning honker: %s", err)
1872			}
1873			return
1874		}
1875	}
1876
1877	ilog.Printf("updating honker undo: %s %s", who, folxid)
1878	_, err := stmtUpdateFlavor.Exec("undub", folxid, user.ID, name, who, "dub")
1879	if err != nil {
1880		elog.Printf("error updating honker: %s", err)
1881		return
1882	}
1883}
1884
1885func followyou(user *WhatAbout, honkerid int64, sync bool) {
1886	var url, owner string
1887	db := opendatabase()
1888	row := db.QueryRow("select xid, owner from honkers where honkerid = ? and userid = ? and flavor in ('unsub', 'peep', 'presub', 'sub')",
1889		honkerid, user.ID)
1890	err := row.Scan(&url, &owner)
1891	if err != nil {
1892		elog.Printf("can't get honker xid: %s", err)
1893		return
1894	}
1895	folxid := xfiltrate()
1896	ilog.Printf("subscribing to %s", url)
1897	_, err = db.Exec("update honkers set flavor = ?, folxid = ? where honkerid = ?", "presub", folxid, honkerid)
1898	if err != nil {
1899		elog.Printf("error updating honker: %s", err)
1900		return
1901	}
1902	if sync {
1903		subsub(user, url, owner, folxid)
1904	} else {
1905		go subsub(user, url, owner, folxid)
1906	}
1907
1908}
1909func unfollowyou(user *WhatAbout, honkerid int64, sync bool) {
1910	db := opendatabase()
1911	row := db.QueryRow("select xid, owner, folxid from honkers where honkerid = ? and userid = ? and flavor in ('sub')",
1912		honkerid, user.ID)
1913	var url, owner, folxid string
1914	err := row.Scan(&url, &owner, &folxid)
1915	if err != nil {
1916		elog.Printf("can't get honker xid: %s", err)
1917		return
1918	}
1919	ilog.Printf("unsubscribing from %s", url)
1920	_, err = db.Exec("update honkers set flavor = ? where honkerid = ?", "unsub", honkerid)
1921	if err != nil {
1922		elog.Printf("error updating honker: %s", err)
1923		return
1924	}
1925	if sync {
1926		itakeitallback(user, url, owner, folxid)
1927	} else {
1928		go itakeitallback(user, url, owner, folxid)
1929	}
1930}
1931
1932func followyou2(user *WhatAbout, j junk.Junk) {
1933	who, _ := j.GetString("actor")
1934
1935	ilog.Printf("updating honker accept: %s", who)
1936	db := opendatabase()
1937	row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
1938		user.ID, who)
1939	var name, folxid string
1940	err := row.Scan(&name, &folxid)
1941	if err != nil {
1942		elog.Printf("can't get honker name: %s", err)
1943		return
1944	}
1945	_, err = stmtUpdateFlavor.Exec("sub", folxid, user.ID, name, who, "presub")
1946	if err != nil {
1947		elog.Printf("error updating honker: %s", err)
1948		return
1949	}
1950}
1951
1952func nofollowyou2(user *WhatAbout, j junk.Junk) {
1953	who, _ := j.GetString("actor")
1954
1955	ilog.Printf("updating honker reject: %s", who)
1956	db := opendatabase()
1957	row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
1958		user.ID, who)
1959	var name, folxid string
1960	err := row.Scan(&name, &folxid)
1961	if err != nil {
1962		elog.Printf("can't get honker name: %s", err)
1963		return
1964	}
1965	_, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "presub")
1966	_, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "sub")
1967	if err != nil {
1968		elog.Printf("error updating honker: %s", err)
1969		return
1970	}
1971}