all repos — honk @ b0025f872b6dcd3ed18eeb91d2bacb102e4e1c53

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			log.Printf("error fetching donk: %s", err)
 180			localize = false
 181			goto saveit
 182		}
 183		data = ii.([]byte)
 184
 185		if len(data) == 10*1024*1024 {
 186			log.Printf("truncation likely")
 187		}
 188		if strings.HasPrefix(media, "image") {
 189			img, err := shrinkit(data)
 190			if err != nil {
 191				log.Printf("unable to decode image: %s", err)
 192				localize = false
 193				data = []byte{}
 194				goto saveit
 195			}
 196			data = img.Data
 197			format := img.Format
 198			media = "image/" + format
 199			if format == "jpeg" {
 200				format = "jpg"
 201			}
 202			xid = xid + "." + format
 203		} else if media == "application/pdf" {
 204			if len(data) > 1000000 {
 205				log.Printf("not saving large pdf")
 206				localize = false
 207				data = []byte{}
 208			}
 209		} else if len(data) > 100000 {
 210			log.Printf("not saving large attachment")
 211			localize = false
 212			data = []byte{}
 213		}
 214	}
 215saveit:
 216	fileid, err := savefile(xid, name, desc, url, media, localize, data)
 217	if err != nil {
 218		log.Printf("error saving file %s: %s", url, err)
 219		return nil
 220	}
 221	donk.FileID = fileid
 222	donk.XID = xid
 223	return donk
 224}
 225
 226func iszonked(userid int64, xid string) bool {
 227	var id int64
 228	row := stmtFindZonk.QueryRow(userid, xid)
 229	err := row.Scan(&id)
 230	if err == nil {
 231		return true
 232	}
 233	if err != sql.ErrNoRows {
 234		log.Printf("error querying zonk: %s", err)
 235	}
 236	return false
 237}
 238
 239func needxonk(user *WhatAbout, x *Honk) bool {
 240	if rejectxonk(x) {
 241		return false
 242	}
 243	return needxonkid(user, x.XID)
 244}
 245func needbonkid(user *WhatAbout, xid string) bool {
 246	return needxonkidX(user, xid, true)
 247}
 248func needxonkid(user *WhatAbout, xid string) bool {
 249	return needxonkidX(user, xid, false)
 250}
 251func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
 252	if !strings.HasPrefix(xid, "https://") {
 253		return false
 254	}
 255	if strings.HasPrefix(xid, user.URL+"/") {
 256		return false
 257	}
 258	if rejectorigin(user.ID, xid, isannounce) {
 259		log.Printf("rejecting origin: %s", xid)
 260		return false
 261	}
 262	if iszonked(user.ID, xid) {
 263		log.Printf("already zonked: %s", xid)
 264		return false
 265	}
 266	var id int64
 267	row := stmtFindXonk.QueryRow(user.ID, xid)
 268	err := row.Scan(&id)
 269	if err == nil {
 270		return false
 271	}
 272	if err != sql.ErrNoRows {
 273		log.Printf("error querying xonk: %s", err)
 274	}
 275	return true
 276}
 277
 278func eradicatexonk(userid int64, xid string) {
 279	xonk := getxonk(userid, xid)
 280	if xonk != nil {
 281		deletehonk(xonk.ID)
 282	}
 283	_, err := stmtSaveZonker.Exec(userid, xid, "zonk")
 284	if err != nil {
 285		log.Printf("error eradicating: %s", err)
 286	}
 287}
 288
 289func savexonk(x *Honk) {
 290	log.Printf("saving xonk: %s", x.XID)
 291	go handles(x.Honker)
 292	go handles(x.Oonker)
 293	savehonk(x)
 294}
 295
 296type Box struct {
 297	In     string
 298	Out    string
 299	Shared string
 300}
 301
 302var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) {
 303	var info string
 304	row := stmtGetXonker.QueryRow(ident, "boxes")
 305	err := row.Scan(&info)
 306	if err != nil {
 307		log.Printf("need to get boxes for %s", ident)
 308		var j junk.Junk
 309		j, err = GetJunk(ident)
 310		if err != nil {
 311			log.Printf("error getting boxes: %s", err)
 312			return nil, false
 313		}
 314		allinjest(originate(ident), j)
 315		row = stmtGetXonker.QueryRow(ident, "boxes")
 316		err = row.Scan(&info)
 317	}
 318	if err == nil {
 319		m := strings.Split(info, " ")
 320		b := &Box{In: m[0], Out: m[1], Shared: m[2]}
 321		return b, true
 322	}
 323	return nil, false
 324}})
 325
 326func gimmexonks(user *WhatAbout, outbox string) {
 327	log.Printf("getting outbox: %s", outbox)
 328	j, err := GetJunk(outbox)
 329	if err != nil {
 330		log.Printf("error getting outbox: %s", err)
 331		return
 332	}
 333	t, _ := j.GetString("type")
 334	origin := originate(outbox)
 335	if t == "OrderedCollection" {
 336		items, _ := j.GetArray("orderedItems")
 337		if items == nil {
 338			items, _ = j.GetArray("items")
 339		}
 340		if items == nil {
 341			obj, ok := j.GetMap("first")
 342			if ok {
 343				items, _ = obj.GetArray("orderedItems")
 344			} else {
 345				page1, ok := j.GetString("first")
 346				if ok {
 347					j, err = GetJunk(page1)
 348					if err != nil {
 349						log.Printf("error gettings page1: %s", err)
 350						return
 351					}
 352					items, _ = j.GetArray("orderedItems")
 353				}
 354			}
 355		}
 356		if len(items) > 20 {
 357			items = items[0:20]
 358		}
 359		for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
 360			items[i], items[j] = items[j], items[i]
 361		}
 362		for _, item := range items {
 363			obj, ok := item.(junk.Junk)
 364			if ok {
 365				xonksaver(user, obj, origin)
 366				continue
 367			}
 368			xid, ok := item.(string)
 369			if ok {
 370				if !needxonkid(user, xid) {
 371					continue
 372				}
 373				obj, err = GetJunk(xid)
 374				if err != nil {
 375					log.Printf("error getting item: %s", err)
 376					continue
 377				}
 378				xonksaver(user, obj, originate(xid))
 379			}
 380		}
 381	}
 382}
 383
 384func newphone(a []string, obj junk.Junk) []string {
 385	for _, addr := range []string{"to", "cc", "attributedTo"} {
 386		who, _ := obj.GetString(addr)
 387		if who != "" {
 388			a = append(a, who)
 389		}
 390		whos, _ := obj.GetArray(addr)
 391		for _, w := range whos {
 392			who, _ := w.(string)
 393			if who != "" {
 394				a = append(a, who)
 395			}
 396		}
 397	}
 398	return a
 399}
 400
 401func extractattrto(obj junk.Junk) string {
 402	who, _ := obj.GetString("attributedTo")
 403	if who != "" {
 404		return who
 405	}
 406	o, ok := obj.GetMap("attributedTo")
 407	if ok {
 408		id, ok := o.GetString("id")
 409		if ok {
 410			return id
 411		}
 412	}
 413	arr, _ := obj.GetArray("attributedTo")
 414	for _, a := range arr {
 415		o, ok := a.(junk.Junk)
 416		if ok {
 417			t, _ := o.GetString("type")
 418			id, _ := o.GetString("id")
 419			if t == "Person" || t == "" {
 420				return id
 421			}
 422		}
 423		s, ok := a.(string)
 424		if ok {
 425			return s
 426		}
 427	}
 428	return ""
 429}
 430
 431func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
 432	depth := 0
 433	maxdepth := 10
 434	currenttid := ""
 435	goingup := 0
 436	var xonkxonkfn func(item junk.Junk, origin string) *Honk
 437
 438	saveonemore := func(xid string) {
 439		log.Printf("getting onemore: %s", xid)
 440		if depth >= maxdepth {
 441			log.Printf("in too deep")
 442			return
 443		}
 444		obj, err := GetJunkHardMode(xid)
 445		if err != nil {
 446			log.Printf("error getting onemore: %s: %s", xid, err)
 447			return
 448		}
 449		depth++
 450		xonkxonkfn(obj, originate(xid))
 451		depth--
 452	}
 453
 454	xonkxonkfn = func(item junk.Junk, origin string) *Honk {
 455		// id, _ := item.GetString( "id")
 456		what, _ := item.GetString("type")
 457		dt, ok := item.GetString("published")
 458		if !ok {
 459			dt = time.Now().Format(time.RFC3339)
 460		}
 461
 462		var err error
 463		var xid, rid, url, content, precis, convoy string
 464		var replies []string
 465		var obj junk.Junk
 466		isUpdate := false
 467		switch what {
 468		case "Delete":
 469			obj, ok = item.GetMap("object")
 470			if ok {
 471				xid, _ = obj.GetString("id")
 472			} else {
 473				xid, _ = item.GetString("object")
 474			}
 475			if xid == "" {
 476				return nil
 477			}
 478			if originate(xid) != origin {
 479				log.Printf("forged delete: %s", xid)
 480				return nil
 481			}
 482			log.Printf("eradicating %s", xid)
 483			eradicatexonk(user.ID, xid)
 484			return nil
 485		case "Tombstone":
 486			xid, _ = item.GetString("id")
 487			if xid == "" {
 488				return nil
 489			}
 490			if originate(xid) != origin {
 491				log.Printf("forged delete: %s", xid)
 492				return nil
 493			}
 494			log.Printf("eradicating %s", xid)
 495			eradicatexonk(user.ID, xid)
 496			return nil
 497		case "Announce":
 498			obj, ok = item.GetMap("object")
 499			if ok {
 500				xid, _ = obj.GetString("id")
 501			} else {
 502				xid, _ = item.GetString("object")
 503			}
 504			if !needbonkid(user, xid) {
 505				return nil
 506			}
 507			log.Printf("getting bonk: %s", xid)
 508			obj, err = GetJunkHardMode(xid)
 509			if err != nil {
 510				log.Printf("error getting bonk: %s: %s", xid, err)
 511			}
 512			origin = originate(xid)
 513			what = "bonk"
 514		case "Update":
 515			isUpdate = true
 516			fallthrough
 517		case "Create":
 518			obj, ok = item.GetMap("object")
 519			if !ok {
 520				xid, _ = item.GetString("object")
 521				log.Printf("getting created honk: %s", xid)
 522				obj, err = GetJunkHardMode(xid)
 523				if err != nil {
 524					log.Printf("error getting creation: %s", err)
 525				}
 526			}
 527			what = "honk"
 528			if obj != nil {
 529				t, _ := obj.GetString("type")
 530				switch t {
 531				case "Event":
 532					what = "event"
 533				}
 534			}
 535		case "Read":
 536			xid, ok = item.GetString("object")
 537			if ok {
 538				if !needxonkid(user, xid) {
 539					log.Printf("don't need read obj: %s", xid)
 540					return nil
 541				}
 542				obj, err = GetJunkHardMode(xid)
 543				if err != nil {
 544					log.Printf("error getting read: %s", err)
 545					return nil
 546				}
 547				return xonkxonkfn(obj, originate(xid))
 548			}
 549			return nil
 550		case "Add":
 551			xid, ok = item.GetString("object")
 552			if ok {
 553				// check target...
 554				if !needxonkid(user, xid) {
 555					log.Printf("don't need added obj: %s", xid)
 556					return nil
 557				}
 558				obj, err = GetJunkHardMode(xid)
 559				if err != nil {
 560					log.Printf("error getting add: %s", err)
 561					return nil
 562				}
 563				return xonkxonkfn(obj, originate(xid))
 564			}
 565			return nil
 566		case "Move":
 567			obj = item
 568			what = "move"
 569		case "Audio":
 570			fallthrough
 571		case "Image":
 572			fallthrough
 573		case "Video":
 574			fallthrough
 575		case "Question":
 576			fallthrough
 577		case "Note":
 578			fallthrough
 579		case "Article":
 580			fallthrough
 581		case "Page":
 582			obj = item
 583			what = "honk"
 584		case "Event":
 585			obj = item
 586			what = "event"
 587		default:
 588			log.Printf("unknown activity: %s", what)
 589			fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 590			item.Write(fd)
 591			io.WriteString(fd, "\n")
 592			fd.Close()
 593			return nil
 594		}
 595
 596		if obj != nil {
 597			if _, ok := obj.GetString("diaspora:guid"); ok {
 598				// friendica does the silliest bonks
 599				if c, ok := obj.GetString("source", "content"); ok {
 600					re_link := regexp.MustCompile(`link='([^']*)'`)
 601					if m := re_link.FindStringSubmatch(c); len(m) > 1 {
 602						xid = m[1]
 603						log.Printf("getting friendica flavored bonk: %s", xid)
 604						if !needxonkid(user, xid) {
 605							return nil
 606						}
 607						if newobj, err := GetJunkHardMode(xid); err == nil {
 608							obj = newobj
 609							origin = originate(xid)
 610							what = "bonk"
 611						} else {
 612							log.Printf("error getting bonk: %s: %s", xid, err)
 613						}
 614					}
 615				}
 616			}
 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 <> %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 strings.HasSuffix(convoy, "#context") {
 892				// friendica...
 893				if rid != "" {
 894					convoy = ""
 895				} else {
 896					convoy = url
 897				}
 898			}
 899			if rid != "" {
 900				if needxonkid(user, rid) {
 901					goingup++
 902					saveonemore(rid)
 903					goingup--
 904				}
 905				if convoy == "" {
 906					xx := getxonk(user.ID, rid)
 907					if xx != nil {
 908						convoy = xx.Convoy
 909					}
 910				}
 911			}
 912			if convoy == "" {
 913				convoy = currenttid
 914			}
 915			if convoy == "" {
 916				convoy = "data:,missing-" + xfiltrate()
 917				currenttid = convoy
 918			}
 919			xonk.Convoy = convoy
 920			savexonk(&xonk)
 921		}
 922		if goingup == 0 {
 923			for _, replid := range replies {
 924				if needxonkid(user, replid) {
 925					log.Printf("missing a reply: %s", replid)
 926					saveonemore(replid)
 927				}
 928			}
 929		}
 930		return &xonk
 931	}
 932
 933	return xonkxonkfn(item, origin)
 934}
 935
 936func rubadubdub(user *WhatAbout, req junk.Junk) {
 937	xid, _ := req.GetString("id")
 938	actor, _ := req.GetString("actor")
 939	j := junk.New()
 940	j["@context"] = itiswhatitis
 941	j["id"] = user.URL + "/dub/" + url.QueryEscape(xid)
 942	j["type"] = "Accept"
 943	j["actor"] = user.URL
 944	j["to"] = actor
 945	j["published"] = time.Now().UTC().Format(time.RFC3339)
 946	j["object"] = req
 947
 948	deliverate(0, user.ID, actor, j.ToBytes())
 949}
 950
 951func itakeitallback(user *WhatAbout, xid string) {
 952	j := junk.New()
 953	j["@context"] = itiswhatitis
 954	j["id"] = user.URL + "/unsub/" + url.QueryEscape(xid)
 955	j["type"] = "Undo"
 956	j["actor"] = user.URL
 957	j["to"] = xid
 958	f := junk.New()
 959	f["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
 960	f["type"] = "Follow"
 961	f["actor"] = user.URL
 962	f["to"] = xid
 963	f["object"] = xid
 964	j["object"] = f
 965	j["published"] = time.Now().UTC().Format(time.RFC3339)
 966
 967	deliverate(0, user.ID, xid, j.ToBytes())
 968}
 969
 970func subsub(user *WhatAbout, xid string, owner string) {
 971	if xid == "" {
 972		log.Printf("can't subscribe to empty")
 973		return
 974	}
 975	j := junk.New()
 976	j["@context"] = itiswhatitis
 977	j["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
 978	j["type"] = "Follow"
 979	j["actor"] = user.URL
 980	j["to"] = owner
 981	j["object"] = xid
 982	j["published"] = time.Now().UTC().Format(time.RFC3339)
 983
 984	deliverate(0, user.ID, owner, j.ToBytes())
 985}
 986
 987// returns activity, object
 988func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
 989	dt := h.Date.Format(time.RFC3339)
 990	var jo junk.Junk
 991	j := junk.New()
 992	j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
 993	j["actor"] = user.URL
 994	j["published"] = dt
 995	if h.Public {
 996		j["to"] = []string{h.Audience[0], user.URL + "/followers"}
 997	} else {
 998		j["to"] = h.Audience[0]
 999	}
1000	if len(h.Audience) > 1 {
1001		j["cc"] = h.Audience[1:]
1002	}
1003
1004	switch h.What {
1005	case "update":
1006		fallthrough
1007	case "tonk":
1008		fallthrough
1009	case "event":
1010		fallthrough
1011	case "honk":
1012		j["type"] = "Create"
1013		if h.What == "update" {
1014			j["type"] = "Update"
1015		}
1016
1017		jo = junk.New()
1018		jo["id"] = h.XID
1019		jo["type"] = "Note"
1020		if h.What == "event" {
1021			jo["type"] = "Event"
1022		}
1023		jo["published"] = dt
1024		jo["url"] = h.XID
1025		jo["attributedTo"] = user.URL
1026		if h.RID != "" {
1027			jo["inReplyTo"] = h.RID
1028		}
1029		if h.Convoy != "" {
1030			jo["context"] = h.Convoy
1031			jo["conversation"] = h.Convoy
1032		}
1033		jo["to"] = h.Audience[0]
1034		if len(h.Audience) > 1 {
1035			jo["cc"] = h.Audience[1:]
1036		}
1037		if !h.Public {
1038			jo["directMessage"] = true
1039		}
1040		mentions := bunchofgrapes(h.Noise)
1041		translate(h)
1042		redoimages(h)
1043		jo["summary"] = html.EscapeString(h.Precis)
1044		jo["content"] = h.Noise
1045		if h.Precis != "" {
1046			jo["sensitive"] = true
1047		}
1048
1049		var replies []string
1050		for _, reply := range h.Replies {
1051			replies = append(replies, reply.XID)
1052		}
1053		if len(replies) > 0 {
1054			jr := junk.New()
1055			jr["type"] = "Collection"
1056			jr["totalItems"] = len(replies)
1057			jr["items"] = replies
1058			jo["replies"] = jr
1059		}
1060
1061		var tags []junk.Junk
1062		for _, m := range mentions {
1063			t := junk.New()
1064			t["type"] = "Mention"
1065			t["name"] = m.Who
1066			t["href"] = m.Where
1067			tags = append(tags, t)
1068		}
1069		for _, o := range h.Onts {
1070			t := junk.New()
1071			t["type"] = "Hashtag"
1072			o = strings.ToLower(o)
1073			t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1074			t["name"] = o
1075			tags = append(tags, t)
1076		}
1077		for _, e := range herdofemus(h.Noise) {
1078			t := junk.New()
1079			t["id"] = e.ID
1080			t["type"] = "Emoji"
1081			t["name"] = e.Name
1082			i := junk.New()
1083			i["type"] = "Image"
1084			i["mediaType"] = "image/png"
1085			i["url"] = e.ID
1086			t["icon"] = i
1087			tags = append(tags, t)
1088		}
1089		if len(tags) > 0 {
1090			jo["tag"] = tags
1091		}
1092		if p := h.Place; p != nil {
1093			t := junk.New()
1094			t["type"] = "Place"
1095			if p.Name != "" {
1096				t["name"] = p.Name
1097			}
1098			if p.Latitude != 0 {
1099				t["latitude"] = p.Latitude
1100			}
1101			if p.Longitude != 0 {
1102				t["longitude"] = p.Longitude
1103			}
1104			if p.Url != "" {
1105				t["url"] = p.Url
1106			}
1107			jo["location"] = t
1108		}
1109		if t := h.Time; t != nil {
1110			jo["startTime"] = t.StartTime.Format(time.RFC3339)
1111			if t.Duration != 0 {
1112				jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1113			}
1114		}
1115		var atts []junk.Junk
1116		for _, d := range h.Donks {
1117			if re_emus.MatchString(d.Name) {
1118				continue
1119			}
1120			jd := junk.New()
1121			jd["mediaType"] = d.Media
1122			jd["name"] = d.Name
1123			jd["summary"] = html.EscapeString(d.Desc)
1124			jd["type"] = "Document"
1125			jd["url"] = d.URL
1126			atts = append(atts, jd)
1127		}
1128		if len(atts) > 0 {
1129			jo["attachment"] = atts
1130		}
1131		j["object"] = jo
1132	case "bonk":
1133		j["type"] = "Announce"
1134		if h.Convoy != "" {
1135			j["context"] = h.Convoy
1136		}
1137		j["object"] = h.XID
1138	case "unbonk":
1139		b := junk.New()
1140		b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1141		b["type"] = "Announce"
1142		b["actor"] = user.URL
1143		if h.Convoy != "" {
1144			b["context"] = h.Convoy
1145		}
1146		b["object"] = h.XID
1147		j["type"] = "Undo"
1148		j["object"] = b
1149	case "zonk":
1150		j["type"] = "Delete"
1151		j["object"] = h.XID
1152	case "ack":
1153		j["type"] = "Read"
1154		j["object"] = h.XID
1155		if h.Convoy != "" {
1156			j["context"] = h.Convoy
1157		}
1158	case "react":
1159		j["type"] = "EmojiReaction"
1160		j["object"] = h.XID
1161		if h.Convoy != "" {
1162			j["context"] = h.Convoy
1163		}
1164		j["content"] = user.Options.Reaction
1165	case "deack":
1166		b := junk.New()
1167		b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1168		b["type"] = "Read"
1169		b["actor"] = user.URL
1170		b["object"] = h.XID
1171		if h.Convoy != "" {
1172			b["context"] = h.Convoy
1173		}
1174		j["type"] = "Undo"
1175		j["object"] = b
1176	}
1177
1178	return j, jo
1179}
1180
1181var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1182	row := stmtAnyXonk.QueryRow(xid)
1183	honk := scanhonk(row)
1184	if honk == nil || !honk.Public {
1185		return nil, true
1186	}
1187	user, _ := butwhatabout(honk.Username)
1188	rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1189	reversehonks(rawhonks)
1190	for _, h := range rawhonks {
1191		if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1192			honk.Replies = append(honk.Replies, h)
1193		}
1194	}
1195	donksforhonks([]*Honk{honk})
1196	_, j := jonkjonk(user, honk)
1197	j["@context"] = itiswhatitis
1198
1199	return j.ToBytes(), true
1200}, Limit: 128})
1201
1202func gimmejonk(xid string) ([]byte, bool) {
1203	var j []byte
1204	ok := oldjonks.Get(xid, &j)
1205	return j, ok
1206}
1207
1208func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1209	rcpts := make(map[string]bool)
1210	for _, a := range addresses {
1211		if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1212			continue
1213		}
1214		if a[0] == '%' {
1215			rcpts[a] = true
1216			continue
1217		}
1218		var box *Box
1219		ok := boxofboxes.Get(a, &box)
1220		if ok && useshared && box.Shared != "" {
1221			rcpts["%"+box.Shared] = true
1222		} else {
1223			rcpts[a] = true
1224		}
1225	}
1226	return rcpts
1227}
1228
1229func honkworldwide(user *WhatAbout, honk *Honk) {
1230	jonk, _ := jonkjonk(user, honk)
1231	jonk["@context"] = itiswhatitis
1232	msg := jonk.ToBytes()
1233
1234	rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1235
1236	if honk.Public {
1237		for _, h := range getdubs(user.ID) {
1238			if h.XID == user.URL {
1239				continue
1240			}
1241			var box *Box
1242			ok := boxofboxes.Get(h.XID, &box)
1243			if ok && box.Shared != "" {
1244				rcpts["%"+box.Shared] = true
1245			} else {
1246				rcpts[h.XID] = true
1247			}
1248		}
1249		for _, f := range getbacktracks(honk.XID) {
1250			rcpts[f] = true
1251		}
1252	}
1253	for a := range rcpts {
1254		go deliverate(0, user.ID, a, msg)
1255	}
1256	if honk.Public && len(honk.Onts) > 0 {
1257		collectiveaction(honk)
1258	}
1259}
1260
1261func collectiveaction(honk *Honk) {
1262	user := getserveruser()
1263	for _, ont := range honk.Onts {
1264		dubs := getnameddubs(serverUID, ont)
1265		if len(dubs) == 0 {
1266			continue
1267		}
1268		j := junk.New()
1269		j["@context"] = itiswhatitis
1270		j["type"] = "Add"
1271		j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1272		j["actor"] = user.URL
1273		j["object"] = honk.XID
1274		j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1275		rcpts := make(map[string]bool)
1276		for _, dub := range dubs {
1277			var box *Box
1278			ok := boxofboxes.Get(dub.XID, &box)
1279			if ok && box.Shared != "" {
1280				rcpts["%"+box.Shared] = true
1281			} else {
1282				rcpts[dub.XID] = true
1283			}
1284		}
1285		msg := j.ToBytes()
1286		for a := range rcpts {
1287			go deliverate(0, user.ID, a, msg)
1288		}
1289	}
1290}
1291
1292func junkuser(user *WhatAbout) []byte {
1293	about := markitzero(user.About)
1294
1295	j := junk.New()
1296	j["@context"] = itiswhatitis
1297	j["id"] = user.URL
1298	j["inbox"] = user.URL + "/inbox"
1299	j["outbox"] = user.URL + "/outbox"
1300	j["name"] = user.Display
1301	j["preferredUsername"] = user.Name
1302	j["summary"] = about
1303	if user.ID > 0 {
1304		j["type"] = "Person"
1305		j["url"] = user.URL
1306		j["followers"] = user.URL + "/followers"
1307		j["following"] = user.URL + "/following"
1308		a := junk.New()
1309		a["type"] = "Image"
1310		a["mediaType"] = "image/png"
1311		if ava := user.Options.Avatar; ava != "" {
1312			a["url"] = ava
1313		} else {
1314			a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1315		}
1316		j["icon"] = a
1317	} else {
1318		j["type"] = "Service"
1319	}
1320	k := junk.New()
1321	k["id"] = user.URL + "#key"
1322	k["owner"] = user.URL
1323	k["publicKeyPem"] = user.Key
1324	j["publicKey"] = k
1325
1326	return j.ToBytes()
1327}
1328
1329var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1330	user, err := butwhatabout(name)
1331	if err != nil {
1332		return nil, false
1333	}
1334	return junkuser(user), true
1335}, Duration: 1 * time.Minute})
1336
1337func asjonker(name string) ([]byte, bool) {
1338	var j []byte
1339	ok := oldjonkers.Get(name, &j)
1340	return j, ok
1341}
1342
1343var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1344	m := strings.Split(name, "@")
1345	if len(m) != 2 {
1346		log.Printf("bad fish name: %s", name)
1347		return "", true
1348	}
1349	var href string
1350	row := stmtGetXonker.QueryRow(name, "fishname")
1351	err := row.Scan(&href)
1352	if err == nil {
1353		return href, true
1354	}
1355	log.Printf("fishing for %s", name)
1356	j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1357	if err != nil {
1358		log.Printf("failed to go fish %s: %s", name, err)
1359		return "", true
1360	}
1361	links, _ := j.GetArray("links")
1362	for _, li := range links {
1363		l, ok := li.(junk.Junk)
1364		if !ok {
1365			continue
1366		}
1367		href, _ := l.GetString("href")
1368		rel, _ := l.GetString("rel")
1369		t, _ := l.GetString("type")
1370		if rel == "self" && friendorfoe(t) {
1371			when := time.Now().UTC().Format(dbtimeformat)
1372			_, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1373			if err != nil {
1374				log.Printf("error saving fishname: %s", err)
1375			}
1376			return href, true
1377		}
1378	}
1379	return href, true
1380}})
1381
1382func gofish(name string) string {
1383	if name[0] == '@' {
1384		name = name[1:]
1385	}
1386	var href string
1387	handfull.Get(name, &href)
1388	return href
1389}
1390
1391func investigate(name string) (*SomeThing, error) {
1392	if name == "" {
1393		return nil, fmt.Errorf("no name")
1394	}
1395	if name[0] == '@' {
1396		name = gofish(name)
1397	}
1398	if name == "" {
1399		return nil, fmt.Errorf("no name")
1400	}
1401	obj, err := GetJunkFast(name)
1402	if err != nil {
1403		return nil, err
1404	}
1405	allinjest(originate(name), obj)
1406	return somethingabout(obj)
1407}
1408
1409func somethingabout(obj junk.Junk) (*SomeThing, error) {
1410	info := new(SomeThing)
1411	t, _ := obj.GetString("type")
1412	switch t {
1413	case "Person":
1414		fallthrough
1415	case "Organization":
1416		fallthrough
1417	case "Application":
1418		fallthrough
1419	case "Service":
1420		info.What = SomeActor
1421	case "OrderedCollection":
1422		fallthrough
1423	case "Collection":
1424		info.What = SomeCollection
1425	default:
1426		return nil, fmt.Errorf("unknown object type")
1427	}
1428	info.XID, _ = obj.GetString("id")
1429	info.Name, _ = obj.GetString("preferredUsername")
1430	if info.Name == "" {
1431		info.Name, _ = obj.GetString("name")
1432	}
1433	info.Owner, _ = obj.GetString("attributedTo")
1434	if info.Owner == "" {
1435		info.Owner = info.XID
1436	}
1437	return info, nil
1438}
1439
1440func allinjest(origin string, obj junk.Junk) {
1441	keyobj, ok := obj.GetMap("publicKey")
1442	if ok {
1443		ingestpubkey(origin, keyobj)
1444	}
1445	ingestboxes(origin, obj)
1446	ingesthandle(origin, obj)
1447}
1448
1449func ingestpubkey(origin string, obj junk.Junk) {
1450	keyobj, ok := obj.GetMap("publicKey")
1451	if ok {
1452		obj = keyobj
1453	}
1454	keyname, ok := obj.GetString("id")
1455	var data string
1456	row := stmtGetXonker.QueryRow(keyname, "pubkey")
1457	err := row.Scan(&data)
1458	if err == nil {
1459		return
1460	}
1461	if !ok || origin != originate(keyname) {
1462		log.Printf("bad key origin %s <> %s", origin, keyname)
1463		return
1464	}
1465	log.Printf("ingesting a needed pubkey: %s", keyname)
1466	owner, ok := obj.GetString("owner")
1467	if !ok {
1468		log.Printf("error finding %s pubkey owner", keyname)
1469		return
1470	}
1471	data, ok = obj.GetString("publicKeyPem")
1472	if !ok {
1473		log.Printf("error finding %s pubkey", keyname)
1474		return
1475	}
1476	if originate(owner) != origin {
1477		log.Printf("bad key owner: %s <> %s", owner, origin)
1478		return
1479	}
1480	_, _, err = httpsig.DecodeKey(data)
1481	if err != nil {
1482		log.Printf("error decoding %s pubkey: %s", keyname, err)
1483		return
1484	}
1485	when := time.Now().UTC().Format(dbtimeformat)
1486	_, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1487	if err != nil {
1488		log.Printf("error saving key: %s", err)
1489	}
1490}
1491
1492func ingestboxes(origin string, obj junk.Junk) {
1493	ident, _ := obj.GetString("id")
1494	if ident == "" {
1495		return
1496	}
1497	if originate(ident) != origin {
1498		return
1499	}
1500	var info string
1501	row := stmtGetXonker.QueryRow(ident, "boxes")
1502	err := row.Scan(&info)
1503	if err == nil {
1504		return
1505	}
1506	log.Printf("ingesting boxes: %s", ident)
1507	inbox, _ := obj.GetString("inbox")
1508	outbox, _ := obj.GetString("outbox")
1509	sbox, _ := obj.GetString("endpoints", "sharedInbox")
1510	if inbox != "" {
1511		when := time.Now().UTC().Format(dbtimeformat)
1512		m := strings.Join([]string{inbox, outbox, sbox}, " ")
1513		_, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1514		if err != nil {
1515			log.Printf("error saving boxes: %s", err)
1516		}
1517	}
1518}
1519
1520func ingesthandle(origin string, obj junk.Junk) {
1521	xid, _ := obj.GetString("id")
1522	if xid == "" {
1523		return
1524	}
1525	if originate(xid) != origin {
1526		return
1527	}
1528	var handle string
1529	row := stmtGetXonker.QueryRow(xid, "handle")
1530	err := row.Scan(&handle)
1531	if err == nil {
1532		return
1533	}
1534	handle, _ = obj.GetString("preferredUsername")
1535	if handle != "" {
1536		when := time.Now().UTC().Format(dbtimeformat)
1537		_, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1538		if err != nil {
1539			log.Printf("error saving handle: %s", err)
1540		}
1541	}
1542}