all repos — honk @ 2b518cdece32ea593886d500781a5b79cae91ce4

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	"log"
  27	notrand "math/rand"
  28	"net/http"
  29	"net/url"
  30	"os"
  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
  50func friendorfoe(ct string) bool {
  51	ct = strings.ToLower(ct)
  52	for _, at := range falsenames {
  53		if strings.HasPrefix(ct, at) {
  54			return true
  55		}
  56	}
  57	return false
  58}
  59
  60var debugClient = &http.Client{
  61	Transport: &http.Transport{
  62		TLSClientConfig: &tls.Config{
  63			InsecureSkipVerify: true,
  64		},
  65	},
  66}
  67
  68func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error {
  69	return PostMsg(keyname, key, url, j.ToBytes())
  70}
  71
  72func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error {
  73	client := http.DefaultClient
  74	if debugMode {
  75		client = debugClient
  76	}
  77	req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
  78	if err != nil {
  79		return err
  80	}
  81	req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
  82	req.Header.Set("Content-Type", theonetruename)
  83	httpsig.SignRequest(keyname, key, req, msg)
  84	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
  85	defer cancel()
  86	req = req.WithContext(ctx)
  87	resp, err := client.Do(req)
  88	if err != nil {
  89		return err
  90	}
  91	resp.Body.Close()
  92	switch resp.StatusCode {
  93	case 200:
  94	case 201:
  95	case 202:
  96	default:
  97		return fmt.Errorf("http post status: %d", resp.StatusCode)
  98	}
  99	log.Printf("successful post: %s %d", url, resp.StatusCode)
 100	return nil
 101}
 102
 103type JunkError struct {
 104	Junk junk.Junk
 105	Err  error
 106}
 107
 108func GetJunk(url string) (junk.Junk, error) {
 109	return GetJunkTimeout(url, 30*time.Second)
 110}
 111
 112func GetJunkFast(url string) (junk.Junk, error) {
 113	return GetJunkTimeout(url, 5*time.Second)
 114}
 115
 116func GetJunkHardMode(url string) (junk.Junk, error) {
 117	j, err := GetJunk(url)
 118	if err != nil {
 119		emsg := err.Error()
 120		if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
 121			log.Printf("trying again after error: %s", emsg)
 122			time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
 123			j, err = GetJunk(url)
 124			if err != nil {
 125				log.Printf("still couldn't get it")
 126			} else {
 127				log.Printf("retry success!")
 128			}
 129		}
 130	}
 131	return j, err
 132}
 133
 134var flightdeck = gate.NewSerializer()
 135
 136func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
 137
 138	fn := func() (interface{}, error) {
 139		at := thefakename
 140		if strings.Contains(url, ".well-known/webfinger?resource") {
 141			at = "application/jrd+json"
 142		}
 143		j, err := junk.Get(url, junk.GetArgs{
 144			Accept:  at,
 145			Agent:   "honksnonk/5.0; " + serverName,
 146			Timeout: timeout,
 147		})
 148		return j, err
 149	}
 150
 151	ji, err := flightdeck.Call(url, fn)
 152	if err != nil {
 153		return nil, err
 154	}
 155	j := ji.(junk.Junk)
 156	return j, nil
 157}
 158
 159func fetchsome(url string) ([]byte, error) {
 160	client := http.DefaultClient
 161	req, err := http.NewRequest("GET", url, nil)
 162	if err != nil {
 163		log.Printf("error fetching %s: %s", url, err)
 164		return nil, err
 165	}
 166	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
 167	defer cancel()
 168	req = req.WithContext(ctx)
 169	resp, err := client.Do(req)
 170	if err != nil {
 171		log.Printf("error fetching %s: %s", url, err)
 172		return nil, err
 173	}
 174	defer resp.Body.Close()
 175	if resp.StatusCode != 200 {
 176		return nil, fmt.Errorf("not 200: %d %s", resp.StatusCode, url)
 177	}
 178	var buf bytes.Buffer
 179	limiter := io.LimitReader(resp.Body, 10*1024*1024)
 180	io.Copy(&buf, limiter)
 181	return buf.Bytes(), nil
 182}
 183
 184func savedonk(url string, name, desc, media string, localize bool) *Donk {
 185	if url == "" {
 186		return nil
 187	}
 188	if donk := finddonk(url); donk != nil {
 189		return donk
 190	}
 191	log.Printf("saving donk: %s", url)
 192	xid := xfiltrate()
 193	data := []byte{}
 194	if localize {
 195		fn := func() (interface{}, error) {
 196			return fetchsome(url)
 197		}
 198		ii, err := flightdeck.Call(url, fn)
 199		if err != nil {
 200			log.Printf("error fetching donk: %s", err)
 201			localize = false
 202			goto saveit
 203		}
 204		data = ii.([]byte)
 205
 206		if len(data) == 10*1024*1024 {
 207			log.Printf("truncation likely")
 208		}
 209		if strings.HasPrefix(media, "image") {
 210			img, err := shrinkit(data)
 211			if err != nil {
 212				log.Printf("unable to decode image: %s", err)
 213				localize = false
 214				data = []byte{}
 215				goto saveit
 216			}
 217			data = img.Data
 218			format := img.Format
 219			media = "image/" + format
 220			if format == "jpeg" {
 221				format = "jpg"
 222			}
 223			xid = xid + "." + format
 224		} else if media == "application/pdf" {
 225			if len(data) > 1000000 {
 226				log.Printf("not saving large pdf")
 227				localize = false
 228				data = []byte{}
 229			}
 230		} else if len(data) > 100000 {
 231			log.Printf("not saving large attachment")
 232			localize = false
 233			data = []byte{}
 234		}
 235	}
 236saveit:
 237	fileid, err := savefile(xid, name, desc, url, media, localize, data)
 238	if err != nil {
 239		log.Printf("error saving file %s: %s", url, err)
 240		return nil
 241	}
 242	donk := new(Donk)
 243	donk.FileID = fileid
 244	donk.XID = xid
 245	return donk
 246}
 247
 248func iszonked(userid int64, xid string) bool {
 249	var id int64
 250	row := stmtFindZonk.QueryRow(userid, xid)
 251	err := row.Scan(&id)
 252	if err == nil {
 253		return true
 254	}
 255	if err != sql.ErrNoRows {
 256		log.Printf("error querying zonk: %s", err)
 257	}
 258	return false
 259}
 260
 261func needxonk(user *WhatAbout, x *Honk) bool {
 262	if rejectxonk(x) {
 263		return false
 264	}
 265	return needxonkid(user, x.XID)
 266}
 267func needbonkid(user *WhatAbout, xid string) bool {
 268	return needxonkidX(user, xid, true)
 269}
 270func needxonkid(user *WhatAbout, xid string) bool {
 271	return needxonkidX(user, xid, false)
 272}
 273func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
 274	if !strings.HasPrefix(xid, "https://") {
 275		return false
 276	}
 277	if strings.HasPrefix(xid, user.URL+"/") {
 278		return false
 279	}
 280	if rejectorigin(user.ID, xid, isannounce) {
 281		log.Printf("rejecting origin: %s", xid)
 282		return false
 283	}
 284	if iszonked(user.ID, xid) {
 285		log.Printf("already zonked: %s", xid)
 286		return false
 287	}
 288	var id int64
 289	row := stmtFindXonk.QueryRow(user.ID, xid)
 290	err := row.Scan(&id)
 291	if err == nil {
 292		return false
 293	}
 294	if err != sql.ErrNoRows {
 295		log.Printf("error querying xonk: %s", err)
 296	}
 297	return true
 298}
 299
 300func eradicatexonk(userid int64, xid string) {
 301	xonk := getxonk(userid, xid)
 302	if xonk != nil {
 303		deletehonk(xonk.ID)
 304	}
 305	_, err := stmtSaveZonker.Exec(userid, xid, "zonk")
 306	if err != nil {
 307		log.Printf("error eradicating: %s", err)
 308	}
 309}
 310
 311func savexonk(x *Honk) {
 312	log.Printf("saving xonk: %s", x.XID)
 313	go handles(x.Honker)
 314	go handles(x.Oonker)
 315	savehonk(x)
 316}
 317
 318type Box struct {
 319	In     string
 320	Out    string
 321	Shared string
 322}
 323
 324var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) {
 325	var info string
 326	row := stmtGetXonker.QueryRow(ident, "boxes")
 327	err := row.Scan(&info)
 328	if err != nil {
 329		log.Printf("need to get boxes for %s", ident)
 330		var j junk.Junk
 331		j, err = GetJunk(ident)
 332		if err != nil {
 333			log.Printf("error getting boxes: %s", err)
 334			return nil, false
 335		}
 336		allinjest(originate(ident), j)
 337		row = stmtGetXonker.QueryRow(ident, "boxes")
 338		err = row.Scan(&info)
 339	}
 340	if err == nil {
 341		m := strings.Split(info, " ")
 342		b := &Box{In: m[0], Out: m[1], Shared: m[2]}
 343		return b, true
 344	}
 345	return nil, false
 346}})
 347
 348func gimmexonks(user *WhatAbout, outbox string) {
 349	log.Printf("getting outbox: %s", outbox)
 350	j, err := GetJunk(outbox)
 351	if err != nil {
 352		log.Printf("error getting outbox: %s", err)
 353		return
 354	}
 355	t, _ := j.GetString("type")
 356	origin := originate(outbox)
 357	if t == "OrderedCollection" {
 358		items, _ := j.GetArray("orderedItems")
 359		if items == nil {
 360			items, _ = j.GetArray("items")
 361		}
 362		if items == nil {
 363			obj, ok := j.GetMap("first")
 364			if ok {
 365				items, _ = obj.GetArray("orderedItems")
 366			} else {
 367				page1, ok := j.GetString("first")
 368				if ok {
 369					j, err = GetJunk(page1)
 370					if err != nil {
 371						log.Printf("error gettings page1: %s", err)
 372						return
 373					}
 374					items, _ = j.GetArray("orderedItems")
 375				}
 376			}
 377		}
 378		if len(items) > 20 {
 379			items = items[0:20]
 380		}
 381		for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
 382			items[i], items[j] = items[j], items[i]
 383		}
 384		for _, item := range items {
 385			obj, ok := item.(junk.Junk)
 386			if ok {
 387				xonksaver(user, obj, origin)
 388				continue
 389			}
 390			xid, ok := item.(string)
 391			if ok {
 392				if !needxonkid(user, xid) {
 393					continue
 394				}
 395				obj, err = GetJunk(xid)
 396				if err != nil {
 397					log.Printf("error getting item: %s", err)
 398					continue
 399				}
 400				xonksaver(user, obj, originate(xid))
 401			}
 402		}
 403	}
 404}
 405
 406func newphone(a []string, obj junk.Junk) []string {
 407	for _, addr := range []string{"to", "cc", "attributedTo"} {
 408		who, _ := obj.GetString(addr)
 409		if who != "" {
 410			a = append(a, who)
 411		}
 412		whos, _ := obj.GetArray(addr)
 413		for _, w := range whos {
 414			who, _ := w.(string)
 415			if who != "" {
 416				a = append(a, who)
 417			}
 418		}
 419	}
 420	return a
 421}
 422
 423func extractattrto(obj junk.Junk) string {
 424	who, _ := obj.GetString("attributedTo")
 425	if who != "" {
 426		return who
 427	}
 428	o, ok := obj.GetMap("attributedTo")
 429	if ok {
 430		id, ok := o.GetString("id")
 431		if ok {
 432			return id
 433		}
 434	}
 435	arr, _ := obj.GetArray("attributedTo")
 436	for _, a := range arr {
 437		o, ok := a.(junk.Junk)
 438		if ok {
 439			t, _ := o.GetString("type")
 440			id, _ := o.GetString("id")
 441			if t == "Person" || t == "" {
 442				return id
 443			}
 444		}
 445		s, ok := a.(string)
 446		if ok {
 447			return s
 448		}
 449	}
 450	return ""
 451}
 452
 453func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
 454	depth := 0
 455	maxdepth := 10
 456	currenttid := ""
 457	goingup := 0
 458	var xonkxonkfn func(item junk.Junk, origin string) *Honk
 459
 460	saveonemore := func(xid string) {
 461		log.Printf("getting onemore: %s", xid)
 462		if depth >= maxdepth {
 463			log.Printf("in too deep")
 464			return
 465		}
 466		obj, err := GetJunkHardMode(xid)
 467		if err != nil {
 468			log.Printf("error getting onemore: %s: %s", xid, err)
 469			return
 470		}
 471		depth++
 472		xonkxonkfn(obj, originate(xid))
 473		depth--
 474	}
 475
 476	xonkxonkfn = func(item junk.Junk, origin string) *Honk {
 477		id, _ := item.GetString("id")
 478		what, _ := item.GetString("type")
 479		dt, ok := item.GetString("published")
 480		if !ok {
 481			dt = time.Now().Format(time.RFC3339)
 482		}
 483
 484		var err error
 485		var xid, rid, url, content, precis, convoy string
 486		var replies []string
 487		var obj junk.Junk
 488		isUpdate := false
 489		switch what {
 490		case "Delete":
 491			obj, ok = item.GetMap("object")
 492			if ok {
 493				xid, _ = obj.GetString("id")
 494			} else {
 495				xid, _ = item.GetString("object")
 496			}
 497			if xid == "" {
 498				return nil
 499			}
 500			if originate(xid) != origin {
 501				log.Printf("forged delete: %s", xid)
 502				return nil
 503			}
 504			log.Printf("eradicating %s", xid)
 505			eradicatexonk(user.ID, xid)
 506			return nil
 507		case "Tombstone":
 508			xid, _ = item.GetString("id")
 509			if xid == "" {
 510				return nil
 511			}
 512			if originate(xid) != origin {
 513				log.Printf("forged delete: %s", xid)
 514				return nil
 515			}
 516			log.Printf("eradicating %s", xid)
 517			eradicatexonk(user.ID, xid)
 518			return nil
 519		case "Announce":
 520			obj, ok = item.GetMap("object")
 521			if ok {
 522				xid, _ = obj.GetString("id")
 523			} else {
 524				xid, _ = item.GetString("object")
 525			}
 526			if !needbonkid(user, xid) {
 527				return nil
 528			}
 529			log.Printf("getting bonk: %s", xid)
 530			obj, err = GetJunkHardMode(xid)
 531			if err != nil {
 532				log.Printf("error getting bonk: %s: %s", xid, err)
 533			}
 534			origin = originate(xid)
 535			what = "bonk"
 536		case "Update":
 537			isUpdate = true
 538			fallthrough
 539		case "Create":
 540			obj, ok = item.GetMap("object")
 541			if !ok {
 542				xid, _ = item.GetString("object")
 543				log.Printf("getting created honk: %s", xid)
 544				if originate(xid) != origin {
 545					log.Printf("out of bounds %s not from %s", xid, origin)
 546					return nil
 547				}
 548				obj, err = GetJunkHardMode(xid)
 549				if err != nil {
 550					log.Printf("error getting creation: %s", err)
 551				}
 552			}
 553			if obj == nil {
 554				log.Printf("no object for creation %s", id)
 555				return nil
 556			}
 557			return xonkxonkfn(obj, origin)
 558		case "Read":
 559			xid, ok = item.GetString("object")
 560			if ok {
 561				if !needxonkid(user, xid) {
 562					log.Printf("don't need read obj: %s", xid)
 563					return nil
 564				}
 565				obj, err = GetJunkHardMode(xid)
 566				if err != nil {
 567					log.Printf("error getting read: %s", err)
 568					return nil
 569				}
 570				return xonkxonkfn(obj, originate(xid))
 571			}
 572			return nil
 573		case "Add":
 574			xid, ok = item.GetString("object")
 575			if ok {
 576				// check target...
 577				if !needxonkid(user, xid) {
 578					log.Printf("don't need added obj: %s", xid)
 579					return nil
 580				}
 581				obj, err = GetJunkHardMode(xid)
 582				if err != nil {
 583					log.Printf("error getting add: %s", err)
 584					return nil
 585				}
 586				return xonkxonkfn(obj, originate(xid))
 587			}
 588			return nil
 589		case "Move":
 590			obj = item
 591			what = "move"
 592		case "Audio":
 593			fallthrough
 594		case "Image":
 595			fallthrough
 596		case "Video":
 597			fallthrough
 598		case "Question":
 599			fallthrough
 600		case "Note":
 601			fallthrough
 602		case "Article":
 603			fallthrough
 604		case "Page":
 605			obj = item
 606			what = "honk"
 607		case "Event":
 608			obj = item
 609			what = "event"
 610		default:
 611			log.Printf("unknown activity: %s", what)
 612			dumpactivity(item)
 613			return nil
 614		}
 615
 616		if obj != nil {
 617			xid, _ = obj.GetString("id")
 618		}
 619
 620		if xid == "" {
 621			log.Printf("don't know what xid is")
 622			item.Write(os.Stdout)
 623			return nil
 624		}
 625		if originate(xid) != origin {
 626			log.Printf("original sin: %s not from %s", xid, origin)
 627			item.Write(os.Stdout)
 628			return nil
 629		}
 630
 631		var xonk Honk
 632		// early init
 633		xonk.XID = xid
 634		xonk.UserID = user.ID
 635		xonk.Honker, _ = item.GetString("actor")
 636		if xonk.Honker == "" {
 637			xonk.Honker, _ = item.GetString("attributedTo")
 638		}
 639		if obj != nil {
 640			if xonk.Honker == "" {
 641				xonk.Honker = extractattrto(obj)
 642			}
 643			xonk.Oonker = extractattrto(obj)
 644			if xonk.Oonker == xonk.Honker {
 645				xonk.Oonker = ""
 646			}
 647			xonk.Audience = newphone(nil, obj)
 648		}
 649		xonk.Audience = append(xonk.Audience, xonk.Honker)
 650		xonk.Audience = oneofakind(xonk.Audience)
 651
 652		var mentions []Mention
 653		if obj != nil {
 654			ot, _ := obj.GetString("type")
 655			url, _ = obj.GetString("url")
 656			if dt2, ok := obj.GetString("published"); ok {
 657				dt = dt2
 658			}
 659			content, _ = obj.GetString("content")
 660			if !strings.HasPrefix(content, "<p>") {
 661				content = "<p>" + content
 662			}
 663			precis, _ = obj.GetString("summary")
 664			if name, ok := obj.GetString("name"); ok {
 665				if precis != "" {
 666					content = precis + "<p>" + content
 667				}
 668				precis = html.EscapeString(name)
 669			}
 670			if sens, _ := obj["sensitive"].(bool); sens && precis == "" {
 671				precis = "unspecified horror"
 672			}
 673			rid, ok = obj.GetString("inReplyTo")
 674			if !ok {
 675				if robj, ok := obj.GetMap("inReplyTo"); ok {
 676					rid, _ = robj.GetString("id")
 677				}
 678			}
 679			convoy, _ = obj.GetString("context")
 680			if convoy == "" {
 681				convoy, _ = obj.GetString("conversation")
 682			}
 683			if ot == "Question" {
 684				if what == "honk" {
 685					what = "qonk"
 686				}
 687				content += "<ul>"
 688				ans, _ := obj.GetArray("oneOf")
 689				for _, ai := range ans {
 690					a, ok := ai.(junk.Junk)
 691					if !ok {
 692						continue
 693					}
 694					as, _ := a.GetString("name")
 695					content += "<li>" + as
 696				}
 697				ans, _ = obj.GetArray("anyOf")
 698				for _, ai := range ans {
 699					a, ok := ai.(junk.Junk)
 700					if !ok {
 701						continue
 702					}
 703					as, _ := a.GetString("name")
 704					content += "<li>" + as
 705				}
 706				content += "</ul>"
 707			}
 708			if ot == "Move" {
 709				targ, _ := obj.GetString("target")
 710				content += string(templates.Sprintf(`<p>Moved to <a href="%s">%s</a>`, targ, targ))
 711			}
 712			if what == "honk" && rid != "" {
 713				what = "tonk"
 714			}
 715			atts, _ := obj.GetArray("attachment")
 716			for i, atti := range atts {
 717				if rejectxonk(&xonk) {
 718					log.Printf("skipping rejected attachment: %s", xid)
 719					continue
 720				}
 721				att, ok := atti.(junk.Junk)
 722				if !ok {
 723					continue
 724				}
 725				at, _ := att.GetString("type")
 726				mt, _ := att.GetString("mediaType")
 727				u, _ := att.GetString("url")
 728				name, _ := att.GetString("name")
 729				desc, _ := att.GetString("summary")
 730				if desc == "" {
 731					desc = name
 732				}
 733				localize := false
 734				if i > 4 {
 735					log.Printf("excessive attachment: %s", at)
 736				} else if at == "Document" || at == "Image" {
 737					mt = strings.ToLower(mt)
 738					log.Printf("attachment: %s %s", mt, u)
 739					if mt == "text/plain" || mt == "application/pdf" ||
 740						strings.HasPrefix(mt, "image") {
 741						localize = true
 742					}
 743				} else {
 744					log.Printf("unknown attachment: %s", at)
 745				}
 746				if skipMedia(&xonk) {
 747					localize = false
 748				}
 749				donk := savedonk(u, name, desc, mt, localize)
 750				if donk != nil {
 751					xonk.Donks = append(xonk.Donks, donk)
 752				}
 753			}
 754			tags, _ := obj.GetArray("tag")
 755			for _, tagi := range tags {
 756				if rejectxonk(&xonk) {
 757					log.Printf("skipping rejected attachment: %s", xid)
 758					continue
 759				}
 760				tag, ok := tagi.(junk.Junk)
 761				if !ok {
 762					continue
 763				}
 764				tt, _ := tag.GetString("type")
 765				name, _ := tag.GetString("name")
 766				desc, _ := tag.GetString("summary")
 767				if desc == "" {
 768					desc = name
 769				}
 770				if tt == "Emoji" {
 771					icon, _ := tag.GetMap("icon")
 772					mt, _ := icon.GetString("mediaType")
 773					if mt == "" {
 774						mt = "image/png"
 775					}
 776					u, _ := icon.GetString("url")
 777					donk := savedonk(u, name, desc, mt, true)
 778					if donk != nil {
 779						xonk.Donks = append(xonk.Donks, donk)
 780					}
 781				}
 782				if tt == "Hashtag" {
 783					if name == "" || name == "#" {
 784						// skip it
 785					} else {
 786						if name[0] != '#' {
 787							name = "#" + name
 788						}
 789						xonk.Onts = append(xonk.Onts, name)
 790					}
 791				}
 792				if tt == "Place" {
 793					p := new(Place)
 794					p.Name = name
 795					p.Latitude, _ = tag["latitude"].(float64)
 796					p.Longitude, _ = tag["longitude"].(float64)
 797					p.Url, _ = tag.GetString("url")
 798					xonk.Place = p
 799				}
 800				if tt == "Mention" {
 801					var m Mention
 802					m.Who, _ = tag.GetString("name")
 803					m.Where, _ = tag.GetString("href")
 804					mentions = append(mentions, m)
 805				}
 806			}
 807			if starttime, ok := obj.GetString("startTime"); ok {
 808				if start, err := time.Parse(time.RFC3339, starttime); err == nil {
 809					t := new(Time)
 810					t.StartTime = start
 811					endtime, _ := obj.GetString("endTime")
 812					t.EndTime, _ = time.Parse(time.RFC3339, endtime)
 813					dura, _ := obj.GetString("duration")
 814					if strings.HasPrefix(dura, "PT") {
 815						dura = strings.ToLower(dura[2:])
 816						d, _ := time.ParseDuration(dura)
 817						t.Duration = Duration(d)
 818					}
 819					xonk.Time = t
 820				}
 821			}
 822			if loca, ok := obj.GetMap("location"); ok {
 823				if tt, _ := loca.GetString("type"); tt == "Place" {
 824					p := new(Place)
 825					p.Name, _ = loca.GetString("name")
 826					p.Latitude, _ = loca["latitude"].(float64)
 827					p.Longitude, _ = loca["longitude"].(float64)
 828					p.Url, _ = loca.GetString("url")
 829					xonk.Place = p
 830				}
 831			}
 832
 833			xonk.Onts = oneofakind(xonk.Onts)
 834			replyobj, ok := obj.GetMap("replies")
 835			if ok {
 836				items, ok := replyobj.GetArray("items")
 837				if !ok {
 838					first, ok := replyobj.GetMap("first")
 839					if ok {
 840						items, _ = first.GetArray("items")
 841					}
 842				}
 843				for _, repl := range items {
 844					s, ok := repl.(string)
 845					if ok {
 846						replies = append(replies, s)
 847					}
 848				}
 849			}
 850
 851		}
 852
 853		if currenttid == "" {
 854			currenttid = convoy
 855		}
 856
 857		if len(content) > 90001 {
 858			log.Printf("content too long. truncating")
 859			content = content[:90001]
 860		}
 861
 862		// init xonk
 863		xonk.What = what
 864		xonk.RID = rid
 865		xonk.Date, _ = time.Parse(time.RFC3339, dt)
 866		xonk.URL = url
 867		xonk.Noise = content
 868		xonk.Precis = precis
 869		xonk.Format = "html"
 870		xonk.Convoy = convoy
 871		xonk.Mentions = mentions
 872		for _, m := range mentions {
 873			if m.Where == user.URL {
 874				xonk.Whofore = 1
 875			}
 876		}
 877		imaginate(&xonk)
 878
 879		if isUpdate {
 880			log.Printf("something has changed! %s", xonk.XID)
 881			prev := getxonk(user.ID, xonk.XID)
 882			if prev == nil {
 883				log.Printf("didn't find old version for update: %s", xonk.XID)
 884				isUpdate = false
 885			} else {
 886				xonk.ID = prev.ID
 887				updatehonk(&xonk)
 888			}
 889		}
 890		if !isUpdate && needxonk(user, &xonk) {
 891			if rid != "" {
 892				if needxonkid(user, rid) {
 893					goingup++
 894					saveonemore(rid)
 895					goingup--
 896				}
 897				if convoy == "" {
 898					xx := getxonk(user.ID, rid)
 899					if xx != nil {
 900						convoy = xx.Convoy
 901					}
 902				}
 903			}
 904			if convoy == "" {
 905				convoy = currenttid
 906			}
 907			if convoy == "" {
 908				convoy = "data:,missing-" + xfiltrate()
 909				currenttid = convoy
 910			}
 911			xonk.Convoy = convoy
 912			savexonk(&xonk)
 913		}
 914		if goingup == 0 {
 915			for _, replid := range replies {
 916				if needxonkid(user, replid) {
 917					log.Printf("missing a reply: %s", replid)
 918					saveonemore(replid)
 919				}
 920			}
 921		}
 922		return &xonk
 923	}
 924
 925	return xonkxonkfn(item, origin)
 926}
 927
 928func dumpactivity(item junk.Junk) {
 929	fd, err := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 930	if err != nil {
 931		log.Printf("error opening inbox! %s", err)
 932		return
 933	}
 934	defer fd.Close()
 935	item.Write(fd)
 936	io.WriteString(fd, "\n")
 937}
 938
 939func rubadubdub(user *WhatAbout, req junk.Junk) {
 940	xid, _ := req.GetString("id")
 941	actor, _ := req.GetString("actor")
 942	j := junk.New()
 943	j["@context"] = itiswhatitis
 944	j["id"] = user.URL + "/dub/" + url.QueryEscape(xid)
 945	j["type"] = "Accept"
 946	j["actor"] = user.URL
 947	j["to"] = actor
 948	j["published"] = time.Now().UTC().Format(time.RFC3339)
 949	j["object"] = req
 950
 951	deliverate(0, user.ID, actor, j.ToBytes())
 952}
 953
 954func itakeitallback(user *WhatAbout, xid string) {
 955	j := junk.New()
 956	j["@context"] = itiswhatitis
 957	j["id"] = user.URL + "/unsub/" + url.QueryEscape(xid)
 958	j["type"] = "Undo"
 959	j["actor"] = user.URL
 960	j["to"] = xid
 961	f := junk.New()
 962	f["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
 963	f["type"] = "Follow"
 964	f["actor"] = user.URL
 965	f["to"] = xid
 966	f["object"] = xid
 967	j["object"] = f
 968	j["published"] = time.Now().UTC().Format(time.RFC3339)
 969
 970	deliverate(0, user.ID, xid, j.ToBytes())
 971}
 972
 973func subsub(user *WhatAbout, xid string, owner string) {
 974	if xid == "" {
 975		log.Printf("can't subscribe to empty")
 976		return
 977	}
 978	j := junk.New()
 979	j["@context"] = itiswhatitis
 980	j["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
 981	j["type"] = "Follow"
 982	j["actor"] = user.URL
 983	j["to"] = owner
 984	j["object"] = xid
 985	j["published"] = time.Now().UTC().Format(time.RFC3339)
 986
 987	deliverate(0, user.ID, owner, j.ToBytes())
 988}
 989
 990// returns activity, object
 991func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
 992	dt := h.Date.Format(time.RFC3339)
 993	var jo junk.Junk
 994	j := junk.New()
 995	j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
 996	j["actor"] = user.URL
 997	j["published"] = dt
 998	if h.Public {
 999		j["to"] = []string{h.Audience[0], user.URL + "/followers"}
1000	} else {
1001		j["to"] = h.Audience[0]
1002	}
1003	if len(h.Audience) > 1 {
1004		j["cc"] = h.Audience[1:]
1005	}
1006
1007	switch h.What {
1008	case "update":
1009		fallthrough
1010	case "tonk":
1011		fallthrough
1012	case "event":
1013		fallthrough
1014	case "honk":
1015		j["type"] = "Create"
1016		if h.What == "update" {
1017			j["type"] = "Update"
1018		}
1019
1020		jo = junk.New()
1021		jo["id"] = h.XID
1022		jo["type"] = "Note"
1023		if h.What == "event" {
1024			jo["type"] = "Event"
1025		}
1026		jo["published"] = dt
1027		jo["url"] = h.XID
1028		jo["attributedTo"] = user.URL
1029		if h.RID != "" {
1030			jo["inReplyTo"] = h.RID
1031		}
1032		if h.Convoy != "" {
1033			jo["context"] = h.Convoy
1034			jo["conversation"] = h.Convoy
1035		}
1036		jo["to"] = h.Audience[0]
1037		if len(h.Audience) > 1 {
1038			jo["cc"] = h.Audience[1:]
1039		}
1040		if !h.Public {
1041			jo["directMessage"] = true
1042		}
1043		var mentions []Mention
1044		if len(h.Mentions) > 0 {
1045			mentions = h.Mentions
1046		} else {
1047			mentions = bunchofgrapes(h.Noise)
1048		}
1049		translate(h)
1050		redoimages(h)
1051		if h.Precis != "" {
1052			jo["sensitive"] = true
1053		}
1054
1055		var replies []string
1056		for _, reply := range h.Replies {
1057			replies = append(replies, reply.XID)
1058		}
1059		if len(replies) > 0 {
1060			jr := junk.New()
1061			jr["type"] = "Collection"
1062			jr["totalItems"] = len(replies)
1063			jr["items"] = replies
1064			jo["replies"] = jr
1065		}
1066
1067		var tags []junk.Junk
1068		for _, m := range mentions {
1069			t := junk.New()
1070			t["type"] = "Mention"
1071			t["name"] = m.Who
1072			t["href"] = m.Where
1073			tags = append(tags, t)
1074		}
1075		for _, o := range h.Onts {
1076			t := junk.New()
1077			t["type"] = "Hashtag"
1078			o = strings.ToLower(o)
1079			t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1080			t["name"] = o
1081			tags = append(tags, t)
1082		}
1083		for _, e := range herdofemus(h.Noise) {
1084			t := junk.New()
1085			t["id"] = e.ID
1086			t["type"] = "Emoji"
1087			t["name"] = e.Name
1088			i := junk.New()
1089			i["type"] = "Image"
1090			i["mediaType"] = "image/png"
1091			i["url"] = e.ID
1092			t["icon"] = i
1093			tags = append(tags, t)
1094		}
1095		for _, e := range bloat_fixupflags(h) {
1096			t := junk.New()
1097			t["id"] = e.ID
1098			t["type"] = "Emoji"
1099			t["name"] = e.Name
1100			i := junk.New()
1101			i["type"] = "Image"
1102			i["mediaType"] = "image/png"
1103			i["url"] = e.ID
1104			t["icon"] = i
1105			tags = append(tags, t)
1106		}
1107		if len(tags) > 0 {
1108			jo["tag"] = tags
1109		}
1110		if p := h.Place; p != nil {
1111			t := junk.New()
1112			t["type"] = "Place"
1113			if p.Name != "" {
1114				t["name"] = p.Name
1115			}
1116			if p.Latitude != 0 {
1117				t["latitude"] = p.Latitude
1118			}
1119			if p.Longitude != 0 {
1120				t["longitude"] = p.Longitude
1121			}
1122			if p.Url != "" {
1123				t["url"] = p.Url
1124			}
1125			jo["location"] = t
1126		}
1127		if t := h.Time; t != nil {
1128			jo["startTime"] = t.StartTime.Format(time.RFC3339)
1129			if t.Duration != 0 {
1130				jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1131			}
1132		}
1133		var atts []junk.Junk
1134		for _, d := range h.Donks {
1135			if re_emus.MatchString(d.Name) {
1136				continue
1137			}
1138			jd := junk.New()
1139			jd["mediaType"] = d.Media
1140			jd["name"] = d.Name
1141			jd["summary"] = html.EscapeString(d.Desc)
1142			jd["type"] = "Document"
1143			jd["url"] = d.URL
1144			atts = append(atts, jd)
1145		}
1146		if len(atts) > 0 {
1147			jo["attachment"] = atts
1148		}
1149		jo["summary"] = html.EscapeString(h.Precis)
1150		jo["content"] = h.Noise
1151		j["object"] = jo
1152	case "bonk":
1153		j["type"] = "Announce"
1154		if h.Convoy != "" {
1155			j["context"] = h.Convoy
1156		}
1157		j["object"] = h.XID
1158	case "unbonk":
1159		b := junk.New()
1160		b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1161		b["type"] = "Announce"
1162		b["actor"] = user.URL
1163		if h.Convoy != "" {
1164			b["context"] = h.Convoy
1165		}
1166		b["object"] = h.XID
1167		j["type"] = "Undo"
1168		j["object"] = b
1169	case "zonk":
1170		j["type"] = "Delete"
1171		j["object"] = h.XID
1172	case "ack":
1173		j["type"] = "Read"
1174		j["object"] = h.XID
1175		if h.Convoy != "" {
1176			j["context"] = h.Convoy
1177		}
1178	case "react":
1179		j["type"] = "EmojiReact"
1180		j["object"] = h.XID
1181		if h.Convoy != "" {
1182			j["context"] = h.Convoy
1183		}
1184		j["content"] = h.Noise
1185	case "deack":
1186		b := junk.New()
1187		b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1188		b["type"] = "Read"
1189		b["actor"] = user.URL
1190		b["object"] = h.XID
1191		if h.Convoy != "" {
1192			b["context"] = h.Convoy
1193		}
1194		j["type"] = "Undo"
1195		j["object"] = b
1196	}
1197
1198	return j, jo
1199}
1200
1201var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1202	row := stmtAnyXonk.QueryRow(xid)
1203	honk := scanhonk(row)
1204	if honk == nil || !honk.Public {
1205		return nil, true
1206	}
1207	user, _ := butwhatabout(honk.Username)
1208	rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1209	reversehonks(rawhonks)
1210	for _, h := range rawhonks {
1211		if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1212			honk.Replies = append(honk.Replies, h)
1213		}
1214	}
1215	donksforhonks([]*Honk{honk})
1216	_, j := jonkjonk(user, honk)
1217	j["@context"] = itiswhatitis
1218
1219	return j.ToBytes(), true
1220}, Limit: 128})
1221
1222func gimmejonk(xid string) ([]byte, bool) {
1223	var j []byte
1224	ok := oldjonks.Get(xid, &j)
1225	return j, ok
1226}
1227
1228func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1229	rcpts := make(map[string]bool)
1230	for _, a := range addresses {
1231		if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1232			continue
1233		}
1234		if a[0] == '%' {
1235			rcpts[a] = true
1236			continue
1237		}
1238		var box *Box
1239		ok := boxofboxes.Get(a, &box)
1240		if ok && useshared && box.Shared != "" {
1241			rcpts["%"+box.Shared] = true
1242		} else {
1243			rcpts[a] = true
1244		}
1245	}
1246	return rcpts
1247}
1248
1249func honkworldwide(user *WhatAbout, honk *Honk) {
1250	jonk, _ := jonkjonk(user, honk)
1251	jonk["@context"] = itiswhatitis
1252	msg := jonk.ToBytes()
1253
1254	rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1255
1256	if honk.Public {
1257		for _, h := range getdubs(user.ID) {
1258			if h.XID == user.URL {
1259				continue
1260			}
1261			var box *Box
1262			ok := boxofboxes.Get(h.XID, &box)
1263			if ok && box.Shared != "" {
1264				rcpts["%"+box.Shared] = true
1265			} else {
1266				rcpts[h.XID] = true
1267			}
1268		}
1269		for _, f := range getbacktracks(honk.XID) {
1270			rcpts[f] = true
1271		}
1272	}
1273	for a := range rcpts {
1274		go deliverate(0, user.ID, a, msg)
1275	}
1276	if honk.Public && len(honk.Onts) > 0 {
1277		collectiveaction(honk)
1278	}
1279}
1280
1281func collectiveaction(honk *Honk) {
1282	user := getserveruser()
1283	for _, ont := range honk.Onts {
1284		dubs := getnameddubs(serverUID, ont)
1285		if len(dubs) == 0 {
1286			continue
1287		}
1288		j := junk.New()
1289		j["@context"] = itiswhatitis
1290		j["type"] = "Add"
1291		j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1292		j["actor"] = user.URL
1293		j["object"] = honk.XID
1294		j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1295		rcpts := make(map[string]bool)
1296		for _, dub := range dubs {
1297			var box *Box
1298			ok := boxofboxes.Get(dub.XID, &box)
1299			if ok && box.Shared != "" {
1300				rcpts["%"+box.Shared] = true
1301			} else {
1302				rcpts[dub.XID] = true
1303			}
1304		}
1305		msg := j.ToBytes()
1306		for a := range rcpts {
1307			go deliverate(0, user.ID, a, msg)
1308		}
1309	}
1310}
1311
1312func junkuser(user *WhatAbout) junk.Junk {
1313	about := markitzero(user.About)
1314
1315	j := junk.New()
1316	j["@context"] = itiswhatitis
1317	j["id"] = user.URL
1318	j["inbox"] = user.URL + "/inbox"
1319	j["outbox"] = user.URL + "/outbox"
1320	j["name"] = user.Display
1321	j["preferredUsername"] = user.Name
1322	j["summary"] = about
1323	if user.ID > 0 {
1324		j["type"] = "Person"
1325		j["url"] = user.URL
1326		j["followers"] = user.URL + "/followers"
1327		j["following"] = user.URL + "/following"
1328		a := junk.New()
1329		a["type"] = "Image"
1330		a["mediaType"] = "image/png"
1331		if ava := user.Options.Avatar; ava != "" {
1332			a["url"] = ava
1333		} else {
1334			a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1335		}
1336		j["icon"] = a
1337	} else {
1338		j["type"] = "Service"
1339	}
1340	k := junk.New()
1341	k["id"] = user.URL + "#key"
1342	k["owner"] = user.URL
1343	k["publicKeyPem"] = user.Key
1344	j["publicKey"] = k
1345
1346	return j
1347}
1348
1349var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1350	user, err := butwhatabout(name)
1351	if err != nil {
1352		return nil, false
1353	}
1354	var buf bytes.Buffer
1355	j := junkuser(user)
1356	j.Write(&buf)
1357	return buf.Bytes(), true
1358}, Duration: 1 * time.Minute})
1359
1360func asjonker(name string) ([]byte, bool) {
1361	var j []byte
1362	ok := oldjonkers.Get(name, &j)
1363	return j, ok
1364}
1365
1366var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1367	m := strings.Split(name, "@")
1368	if len(m) != 2 {
1369		log.Printf("bad fish name: %s", name)
1370		return "", true
1371	}
1372	var href string
1373	row := stmtGetXonker.QueryRow(name, "fishname")
1374	err := row.Scan(&href)
1375	if err == nil {
1376		return href, true
1377	}
1378	log.Printf("fishing for %s", name)
1379	j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1380	if err != nil {
1381		log.Printf("failed to go fish %s: %s", name, err)
1382		return "", true
1383	}
1384	links, _ := j.GetArray("links")
1385	for _, li := range links {
1386		l, ok := li.(junk.Junk)
1387		if !ok {
1388			continue
1389		}
1390		href, _ := l.GetString("href")
1391		rel, _ := l.GetString("rel")
1392		t, _ := l.GetString("type")
1393		if rel == "self" && friendorfoe(t) {
1394			when := time.Now().UTC().Format(dbtimeformat)
1395			_, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1396			if err != nil {
1397				log.Printf("error saving fishname: %s", err)
1398			}
1399			return href, true
1400		}
1401	}
1402	return href, true
1403}, Duration: 1 * time.Minute})
1404
1405func gofish(name string) string {
1406	if name[0] == '@' {
1407		name = name[1:]
1408	}
1409	var href string
1410	handfull.Get(name, &href)
1411	return href
1412}
1413
1414func investigate(name string) (*SomeThing, error) {
1415	if name == "" {
1416		return nil, fmt.Errorf("no name")
1417	}
1418	if name[0] == '@' {
1419		name = gofish(name)
1420	}
1421	if name == "" {
1422		return nil, fmt.Errorf("no name")
1423	}
1424	obj, err := GetJunkFast(name)
1425	if err != nil {
1426		return nil, err
1427	}
1428	allinjest(originate(name), obj)
1429	return somethingabout(obj)
1430}
1431
1432func somethingabout(obj junk.Junk) (*SomeThing, error) {
1433	info := new(SomeThing)
1434	t, _ := obj.GetString("type")
1435	switch t {
1436	case "Person":
1437		fallthrough
1438	case "Organization":
1439		fallthrough
1440	case "Application":
1441		fallthrough
1442	case "Service":
1443		info.What = SomeActor
1444	case "OrderedCollection":
1445		fallthrough
1446	case "Collection":
1447		info.What = SomeCollection
1448	default:
1449		return nil, fmt.Errorf("unknown object type")
1450	}
1451	info.XID, _ = obj.GetString("id")
1452	info.Name, _ = obj.GetString("preferredUsername")
1453	if info.Name == "" {
1454		info.Name, _ = obj.GetString("name")
1455	}
1456	info.Owner, _ = obj.GetString("attributedTo")
1457	if info.Owner == "" {
1458		info.Owner = info.XID
1459	}
1460	return info, nil
1461}
1462
1463func allinjest(origin string, obj junk.Junk) {
1464	keyobj, ok := obj.GetMap("publicKey")
1465	if ok {
1466		ingestpubkey(origin, keyobj)
1467	}
1468	ingestboxes(origin, obj)
1469	ingesthandle(origin, obj)
1470}
1471
1472func ingestpubkey(origin string, obj junk.Junk) {
1473	keyobj, ok := obj.GetMap("publicKey")
1474	if ok {
1475		obj = keyobj
1476	}
1477	keyname, ok := obj.GetString("id")
1478	var data string
1479	row := stmtGetXonker.QueryRow(keyname, "pubkey")
1480	err := row.Scan(&data)
1481	if err == nil {
1482		return
1483	}
1484	if !ok || origin != originate(keyname) {
1485		log.Printf("bad key origin %s <> %s", origin, keyname)
1486		return
1487	}
1488	log.Printf("ingesting a needed pubkey: %s", keyname)
1489	owner, ok := obj.GetString("owner")
1490	if !ok {
1491		log.Printf("error finding %s pubkey owner", keyname)
1492		return
1493	}
1494	data, ok = obj.GetString("publicKeyPem")
1495	if !ok {
1496		log.Printf("error finding %s pubkey", keyname)
1497		return
1498	}
1499	if originate(owner) != origin {
1500		log.Printf("bad key owner: %s <> %s", owner, origin)
1501		return
1502	}
1503	_, _, err = httpsig.DecodeKey(data)
1504	if err != nil {
1505		log.Printf("error decoding %s pubkey: %s", keyname, err)
1506		return
1507	}
1508	when := time.Now().UTC().Format(dbtimeformat)
1509	_, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1510	if err != nil {
1511		log.Printf("error saving key: %s", err)
1512	}
1513}
1514
1515func ingestboxes(origin string, obj junk.Junk) {
1516	ident, _ := obj.GetString("id")
1517	if ident == "" {
1518		return
1519	}
1520	if originate(ident) != origin {
1521		return
1522	}
1523	var info string
1524	row := stmtGetXonker.QueryRow(ident, "boxes")
1525	err := row.Scan(&info)
1526	if err == nil {
1527		return
1528	}
1529	log.Printf("ingesting boxes: %s", ident)
1530	inbox, _ := obj.GetString("inbox")
1531	outbox, _ := obj.GetString("outbox")
1532	sbox, _ := obj.GetString("endpoints", "sharedInbox")
1533	if inbox != "" {
1534		when := time.Now().UTC().Format(dbtimeformat)
1535		m := strings.Join([]string{inbox, outbox, sbox}, " ")
1536		_, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1537		if err != nil {
1538			log.Printf("error saving boxes: %s", err)
1539		}
1540	}
1541}
1542
1543func ingesthandle(origin string, obj junk.Junk) {
1544	xid, _ := obj.GetString("id")
1545	if xid == "" {
1546		return
1547	}
1548	if originate(xid) != origin {
1549		return
1550	}
1551	var handle string
1552	row := stmtGetXonker.QueryRow(xid, "handle")
1553	err := row.Scan(&handle)
1554	if err == nil {
1555		return
1556	}
1557	handle, _ = obj.GetString("preferredUsername")
1558	if handle != "" {
1559		when := time.Now().UTC().Format(dbtimeformat)
1560		_, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1561		if err != nil {
1562			log.Printf("error saving handle: %s", err)
1563		}
1564	}
1565}
1566
1567func updateMe(username string) {
1568	var user *WhatAbout
1569	somenamedusers.Get(username, &user)
1570	dt := time.Now().UTC().Format(time.RFC3339)
1571	j := junk.New()
1572	j["@context"] = itiswhatitis
1573	j["id"] = fmt.Sprintf("%s/upme/%s/%d", user.URL, user.Name, time.Now().Unix())
1574	j["actor"] = user.URL
1575	j["published"] = dt
1576	j["to"] = []string{thewholeworld, user.URL + "/followers"}
1577	j["type"] = "Update"
1578	j["object"] = junkuser(user)
1579
1580	msg := j.ToBytes()
1581
1582	rcpts := make(map[string]bool)
1583	for _, f := range getdubs(user.ID) {
1584		if f.XID == user.URL {
1585			continue
1586		}
1587		var box *Box
1588		boxofboxes.Get(f.XID, &box)
1589		if box != nil && box.Shared != "" {
1590			rcpts["%"+box.Shared] = true
1591		} else {
1592			rcpts[f.XID] = true
1593		}
1594	}
1595	for a := range rcpts {
1596		go deliverate(0, user.ID, a, msg)
1597	}
1598}