all repos — honk @ 89ec1d7d87dc41bd924785c69cf0ab59581b7baf

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