all repos — honk @ 394d8b47c1d37bafad44318b77777af3cc64693a

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