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.GetNumber("latitude")
834 p.Longitude, _ = tag.GetNumber("longitude")
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.GetNumber("latitude")
865 p.Longitude, _ = loca.GetNumber("longitude")
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 actor, _ := req.GetString("actor")
987 j := junk.New()
988 j["@context"] = itiswhatitis
989 j["id"] = user.URL + "/dub/" + xfiltrate()
990 j["type"] = "Accept"
991 j["actor"] = user.URL
992 j["to"] = actor
993 j["published"] = time.Now().UTC().Format(time.RFC3339)
994 j["object"] = req
995
996 deliverate(0, user.ID, actor, j.ToBytes())
997}
998
999func itakeitallback(user *WhatAbout, xid string, owner string, folxid string) {
1000 j := junk.New()
1001 j["@context"] = itiswhatitis
1002 j["id"] = user.URL + "/unsub/" + folxid
1003 j["type"] = "Undo"
1004 j["actor"] = user.URL
1005 j["to"] = owner
1006 f := junk.New()
1007 f["id"] = user.URL + "/sub/" + folxid
1008 f["type"] = "Follow"
1009 f["actor"] = user.URL
1010 f["to"] = owner
1011 f["object"] = xid
1012 j["object"] = f
1013 j["published"] = time.Now().UTC().Format(time.RFC3339)
1014
1015 deliverate(0, user.ID, owner, j.ToBytes())
1016}
1017
1018func subsub(user *WhatAbout, xid string, owner string, folxid string) {
1019 if xid == "" {
1020 log.Printf("can't subscribe to empty")
1021 return
1022 }
1023 j := junk.New()
1024 j["@context"] = itiswhatitis
1025 j["id"] = user.URL + "/sub/" + folxid
1026 j["type"] = "Follow"
1027 j["actor"] = user.URL
1028 j["to"] = owner
1029 j["object"] = xid
1030 j["published"] = time.Now().UTC().Format(time.RFC3339)
1031
1032 deliverate(0, user.ID, owner, j.ToBytes())
1033}
1034
1035func activatedonks(donks []*Donk) []junk.Junk {
1036 var atts []junk.Junk
1037 for _, d := range donks {
1038 if re_emus.MatchString(d.Name) {
1039 continue
1040 }
1041 jd := junk.New()
1042 jd["mediaType"] = d.Media
1043 jd["name"] = d.Name
1044 jd["summary"] = html.EscapeString(d.Desc)
1045 jd["type"] = "Document"
1046 jd["url"] = d.URL
1047 atts = append(atts, jd)
1048 }
1049 return atts
1050}
1051
1052// returns activity, object
1053func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
1054 dt := h.Date.Format(time.RFC3339)
1055 var jo junk.Junk
1056 j := junk.New()
1057 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
1058 j["actor"] = user.URL
1059 j["published"] = dt
1060 j["to"] = h.Audience[0]
1061 if len(h.Audience) > 1 {
1062 j["cc"] = h.Audience[1:]
1063 }
1064
1065 switch h.What {
1066 case "update":
1067 fallthrough
1068 case "tonk":
1069 fallthrough
1070 case "event":
1071 fallthrough
1072 case "honk":
1073 j["type"] = "Create"
1074 if h.What == "update" {
1075 j["type"] = "Update"
1076 }
1077
1078 jo = junk.New()
1079 jo["id"] = h.XID
1080 jo["type"] = "Note"
1081 if h.What == "event" {
1082 jo["type"] = "Event"
1083 }
1084 jo["published"] = dt
1085 jo["url"] = h.XID
1086 jo["attributedTo"] = user.URL
1087 if h.RID != "" {
1088 jo["inReplyTo"] = h.RID
1089 }
1090 if h.Convoy != "" {
1091 jo["context"] = h.Convoy
1092 jo["conversation"] = h.Convoy
1093 }
1094 jo["to"] = h.Audience[0]
1095 if len(h.Audience) > 1 {
1096 jo["cc"] = h.Audience[1:]
1097 }
1098 if !h.Public {
1099 jo["directMessage"] = true
1100 }
1101 translate(h)
1102 redoimages(h)
1103 if h.Precis != "" {
1104 jo["sensitive"] = true
1105 }
1106
1107 var replies []string
1108 for _, reply := range h.Replies {
1109 replies = append(replies, reply.XID)
1110 }
1111 if len(replies) > 0 {
1112 jr := junk.New()
1113 jr["type"] = "Collection"
1114 jr["totalItems"] = len(replies)
1115 jr["items"] = replies
1116 jo["replies"] = jr
1117 }
1118
1119 var tags []junk.Junk
1120 for _, m := range h.Mentions {
1121 t := junk.New()
1122 t["type"] = "Mention"
1123 t["name"] = m.Who
1124 t["href"] = m.Where
1125 tags = append(tags, t)
1126 }
1127 for _, o := range h.Onts {
1128 t := junk.New()
1129 t["type"] = "Hashtag"
1130 o = strings.ToLower(o)
1131 t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1132 t["name"] = o
1133 tags = append(tags, t)
1134 }
1135 for _, e := range herdofemus(h.Noise) {
1136 t := junk.New()
1137 t["id"] = e.ID
1138 t["type"] = "Emoji"
1139 t["name"] = e.Name
1140 i := junk.New()
1141 i["type"] = "Image"
1142 i["mediaType"] = "image/png"
1143 i["url"] = e.ID
1144 t["icon"] = i
1145 tags = append(tags, t)
1146 }
1147 for _, e := range fixupflags(h) {
1148 t := junk.New()
1149 t["id"] = e.ID
1150 t["type"] = "Emoji"
1151 t["name"] = e.Name
1152 i := junk.New()
1153 i["type"] = "Image"
1154 i["mediaType"] = "image/png"
1155 i["url"] = e.ID
1156 t["icon"] = i
1157 tags = append(tags, t)
1158 }
1159 if len(tags) > 0 {
1160 jo["tag"] = tags
1161 }
1162 if p := h.Place; p != nil {
1163 t := junk.New()
1164 t["type"] = "Place"
1165 if p.Name != "" {
1166 t["name"] = p.Name
1167 }
1168 if p.Latitude != 0 {
1169 t["latitude"] = p.Latitude
1170 }
1171 if p.Longitude != 0 {
1172 t["longitude"] = p.Longitude
1173 }
1174 if p.Url != "" {
1175 t["url"] = p.Url
1176 }
1177 jo["location"] = t
1178 }
1179 if t := h.Time; t != nil {
1180 jo["startTime"] = t.StartTime.Format(time.RFC3339)
1181 if t.Duration != 0 {
1182 jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1183 }
1184 }
1185 atts := activatedonks(h.Donks)
1186 if len(atts) > 0 {
1187 jo["attachment"] = atts
1188 }
1189 jo["summary"] = html.EscapeString(h.Precis)
1190 jo["content"] = h.Noise
1191 j["object"] = jo
1192 case "bonk":
1193 j["type"] = "Announce"
1194 if h.Convoy != "" {
1195 j["context"] = h.Convoy
1196 }
1197 j["object"] = h.XID
1198 case "unbonk":
1199 b := junk.New()
1200 b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1201 b["type"] = "Announce"
1202 b["actor"] = user.URL
1203 if h.Convoy != "" {
1204 b["context"] = h.Convoy
1205 }
1206 b["object"] = h.XID
1207 j["type"] = "Undo"
1208 j["object"] = b
1209 case "zonk":
1210 j["type"] = "Delete"
1211 j["object"] = h.XID
1212 case "ack":
1213 j["type"] = "Read"
1214 j["object"] = h.XID
1215 if h.Convoy != "" {
1216 j["context"] = h.Convoy
1217 }
1218 case "react":
1219 j["type"] = "EmojiReact"
1220 j["object"] = h.XID
1221 if h.Convoy != "" {
1222 j["context"] = h.Convoy
1223 }
1224 j["content"] = h.Noise
1225 case "deack":
1226 b := junk.New()
1227 b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1228 b["type"] = "Read"
1229 b["actor"] = user.URL
1230 b["object"] = h.XID
1231 if h.Convoy != "" {
1232 b["context"] = h.Convoy
1233 }
1234 j["type"] = "Undo"
1235 j["object"] = b
1236 }
1237
1238 return j, jo
1239}
1240
1241var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1242 row := stmtAnyXonk.QueryRow(xid)
1243 honk := scanhonk(row)
1244 if honk == nil || !honk.Public {
1245 return nil, true
1246 }
1247 user, _ := butwhatabout(honk.Username)
1248 rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1249 reversehonks(rawhonks)
1250 for _, h := range rawhonks {
1251 if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1252 honk.Replies = append(honk.Replies, h)
1253 }
1254 }
1255 donksforhonks([]*Honk{honk})
1256 _, j := jonkjonk(user, honk)
1257 j["@context"] = itiswhatitis
1258
1259 return j.ToBytes(), true
1260}, Limit: 128})
1261
1262func gimmejonk(xid string) ([]byte, bool) {
1263 var j []byte
1264 ok := oldjonks.Get(xid, &j)
1265 return j, ok
1266}
1267
1268func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1269 rcpts := make(map[string]bool)
1270 for _, a := range addresses {
1271 if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1272 continue
1273 }
1274 if a[0] == '%' {
1275 rcpts[a] = true
1276 continue
1277 }
1278 var box *Box
1279 ok := boxofboxes.Get(a, &box)
1280 if ok && useshared && box.Shared != "" {
1281 rcpts["%"+box.Shared] = true
1282 } else {
1283 rcpts[a] = true
1284 }
1285 }
1286 return rcpts
1287}
1288
1289func chonkifymsg(user *WhatAbout, ch *Chonk) []byte {
1290 dt := ch.Date.Format(time.RFC3339)
1291 aud := []string{ch.Target}
1292
1293 jo := junk.New()
1294 jo["id"] = ch.XID
1295 jo["type"] = "ChatMessage"
1296 jo["published"] = dt
1297 jo["attributedTo"] = user.URL
1298 jo["to"] = aud
1299 jo["content"] = ch.HTML
1300 atts := activatedonks(ch.Donks)
1301 if len(atts) > 0 {
1302 jo["attachment"] = atts
1303 }
1304 var tags []junk.Junk
1305 for _, e := range herdofemus(ch.Noise) {
1306 t := junk.New()
1307 t["id"] = e.ID
1308 t["type"] = "Emoji"
1309 t["name"] = e.Name
1310 i := junk.New()
1311 i["type"] = "Image"
1312 i["mediaType"] = "image/png"
1313 i["url"] = e.ID
1314 t["icon"] = i
1315 tags = append(tags, t)
1316 }
1317 if len(tags) > 0 {
1318 jo["tag"] = tags
1319 }
1320
1321 j := junk.New()
1322 j["@context"] = itiswhatitis
1323 j["id"] = user.URL + "/" + "honk" + "/" + shortxid(ch.XID)
1324 j["type"] = "Create"
1325 j["actor"] = user.URL
1326 j["published"] = dt
1327 j["to"] = aud
1328 j["object"] = jo
1329
1330 return j.ToBytes()
1331}
1332
1333func sendchonk(user *WhatAbout, ch *Chonk) {
1334 msg := chonkifymsg(user, ch)
1335
1336 rcpts := make(map[string]bool)
1337 rcpts[ch.Target] = true
1338 for a := range rcpts {
1339 go deliverate(0, user.ID, a, msg)
1340 }
1341}
1342
1343func honkworldwide(user *WhatAbout, honk *Honk) {
1344 jonk, _ := jonkjonk(user, honk)
1345 jonk["@context"] = itiswhatitis
1346 msg := jonk.ToBytes()
1347
1348 rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1349
1350 if honk.Public {
1351 for _, h := range getdubs(user.ID) {
1352 if h.XID == user.URL {
1353 continue
1354 }
1355 var box *Box
1356 ok := boxofboxes.Get(h.XID, &box)
1357 if ok && box.Shared != "" {
1358 rcpts["%"+box.Shared] = true
1359 } else {
1360 rcpts[h.XID] = true
1361 }
1362 }
1363 for _, f := range getbacktracks(honk.XID) {
1364 var box *Box
1365 ok := boxofboxes.Get(f, &box)
1366 if ok && box.Shared != "" {
1367 rcpts["%"+box.Shared] = true
1368 } else {
1369 rcpts[f] = true
1370 }
1371 }
1372 }
1373 for a := range rcpts {
1374 go deliverate(0, user.ID, a, msg)
1375 }
1376 if honk.Public && len(honk.Onts) > 0 {
1377 collectiveaction(honk)
1378 }
1379}
1380
1381func collectiveaction(honk *Honk) {
1382 user := getserveruser()
1383 for _, ont := range honk.Onts {
1384 dubs := getnameddubs(serverUID, ont)
1385 if len(dubs) == 0 {
1386 continue
1387 }
1388 j := junk.New()
1389 j["@context"] = itiswhatitis
1390 j["type"] = "Add"
1391 j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1392 j["actor"] = user.URL
1393 j["object"] = honk.XID
1394 j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1395 rcpts := make(map[string]bool)
1396 for _, dub := range dubs {
1397 var box *Box
1398 ok := boxofboxes.Get(dub.XID, &box)
1399 if ok && box.Shared != "" {
1400 rcpts["%"+box.Shared] = true
1401 } else {
1402 rcpts[dub.XID] = true
1403 }
1404 }
1405 msg := j.ToBytes()
1406 for a := range rcpts {
1407 go deliverate(0, user.ID, a, msg)
1408 }
1409 }
1410}
1411
1412func junkuser(user *WhatAbout) junk.Junk {
1413 j := junk.New()
1414 j["@context"] = itiswhatitis
1415 j["id"] = user.URL
1416 j["inbox"] = user.URL + "/inbox"
1417 j["outbox"] = user.URL + "/outbox"
1418 j["name"] = user.Display
1419 j["preferredUsername"] = user.Name
1420 j["summary"] = user.HTAbout
1421 if user.ID > 0 {
1422 j["type"] = "Person"
1423 j["url"] = user.URL
1424 j["followers"] = user.URL + "/followers"
1425 j["following"] = user.URL + "/following"
1426 a := junk.New()
1427 a["type"] = "Image"
1428 a["mediaType"] = "image/png"
1429 if ava := user.Options.Avatar; ava != "" {
1430 a["url"] = ava
1431 } else {
1432 a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1433 }
1434 j["icon"] = a
1435 } else {
1436 j["type"] = "Service"
1437 }
1438 k := junk.New()
1439 k["id"] = user.URL + "#key"
1440 k["owner"] = user.URL
1441 k["publicKeyPem"] = user.Key
1442 j["publicKey"] = k
1443
1444 return j
1445}
1446
1447var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1448 user, err := butwhatabout(name)
1449 if err != nil {
1450 return nil, false
1451 }
1452 var buf bytes.Buffer
1453 j := junkuser(user)
1454 j.Write(&buf)
1455 return buf.Bytes(), true
1456}, Duration: 1 * time.Minute})
1457
1458func asjonker(name string) ([]byte, bool) {
1459 var j []byte
1460 ok := oldjonkers.Get(name, &j)
1461 return j, ok
1462}
1463
1464var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1465 m := strings.Split(name, "@")
1466 if len(m) != 2 {
1467 log.Printf("bad fish name: %s", name)
1468 return "", true
1469 }
1470 var href string
1471 row := stmtGetXonker.QueryRow(name, "fishname")
1472 err := row.Scan(&href)
1473 if err == nil {
1474 return href, true
1475 }
1476 log.Printf("fishing for %s", name)
1477 j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1478 if err != nil {
1479 log.Printf("failed to go fish %s: %s", name, err)
1480 return "", true
1481 }
1482 links, _ := j.GetArray("links")
1483 for _, li := range links {
1484 l, ok := li.(junk.Junk)
1485 if !ok {
1486 continue
1487 }
1488 href, _ := l.GetString("href")
1489 rel, _ := l.GetString("rel")
1490 t, _ := l.GetString("type")
1491 if rel == "self" && friendorfoe(t) {
1492 when := time.Now().UTC().Format(dbtimeformat)
1493 _, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1494 if err != nil {
1495 log.Printf("error saving fishname: %s", err)
1496 }
1497 return href, true
1498 }
1499 }
1500 return href, true
1501}, Duration: 1 * time.Minute})
1502
1503func gofish(name string) string {
1504 if name[0] == '@' {
1505 name = name[1:]
1506 }
1507 var href string
1508 handfull.Get(name, &href)
1509 return href
1510}
1511
1512func investigate(name string) (*SomeThing, error) {
1513 if name == "" {
1514 return nil, fmt.Errorf("no name")
1515 }
1516 if name[0] == '@' {
1517 name = gofish(name)
1518 }
1519 if name == "" {
1520 return nil, fmt.Errorf("no name")
1521 }
1522 obj, err := GetJunkFast(name)
1523 if err != nil {
1524 return nil, err
1525 }
1526 allinjest(originate(name), obj)
1527 return somethingabout(obj)
1528}
1529
1530func somethingabout(obj junk.Junk) (*SomeThing, error) {
1531 info := new(SomeThing)
1532 t, _ := obj.GetString("type")
1533 switch t {
1534 case "Person":
1535 fallthrough
1536 case "Organization":
1537 fallthrough
1538 case "Application":
1539 fallthrough
1540 case "Service":
1541 info.What = SomeActor
1542 case "OrderedCollection":
1543 fallthrough
1544 case "Collection":
1545 info.What = SomeCollection
1546 default:
1547 return nil, fmt.Errorf("unknown object type")
1548 }
1549 info.XID, _ = obj.GetString("id")
1550 info.Name, _ = obj.GetString("preferredUsername")
1551 if info.Name == "" {
1552 info.Name, _ = obj.GetString("name")
1553 }
1554 info.Owner, _ = obj.GetString("attributedTo")
1555 if info.Owner == "" {
1556 info.Owner = info.XID
1557 }
1558 return info, nil
1559}
1560
1561func allinjest(origin string, obj junk.Junk) {
1562 keyobj, ok := obj.GetMap("publicKey")
1563 if ok {
1564 ingestpubkey(origin, keyobj)
1565 }
1566 ingestboxes(origin, obj)
1567 ingesthandle(origin, obj)
1568}
1569
1570func ingestpubkey(origin string, obj junk.Junk) {
1571 keyobj, ok := obj.GetMap("publicKey")
1572 if ok {
1573 obj = keyobj
1574 }
1575 keyname, ok := obj.GetString("id")
1576 var data string
1577 row := stmtGetXonker.QueryRow(keyname, "pubkey")
1578 err := row.Scan(&data)
1579 if err == nil {
1580 return
1581 }
1582 if !ok || origin != originate(keyname) {
1583 log.Printf("bad key origin %s <> %s", origin, keyname)
1584 return
1585 }
1586 log.Printf("ingesting a needed pubkey: %s", keyname)
1587 owner, ok := obj.GetString("owner")
1588 if !ok {
1589 log.Printf("error finding %s pubkey owner", keyname)
1590 return
1591 }
1592 data, ok = obj.GetString("publicKeyPem")
1593 if !ok {
1594 log.Printf("error finding %s pubkey", keyname)
1595 return
1596 }
1597 if originate(owner) != origin {
1598 log.Printf("bad key owner: %s <> %s", owner, origin)
1599 return
1600 }
1601 _, _, err = httpsig.DecodeKey(data)
1602 if err != nil {
1603 log.Printf("error decoding %s pubkey: %s", keyname, err)
1604 return
1605 }
1606 when := time.Now().UTC().Format(dbtimeformat)
1607 _, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1608 if err != nil {
1609 log.Printf("error saving key: %s", err)
1610 }
1611}
1612
1613func ingestboxes(origin string, obj junk.Junk) {
1614 ident, _ := obj.GetString("id")
1615 if ident == "" {
1616 return
1617 }
1618 if originate(ident) != origin {
1619 return
1620 }
1621 var info string
1622 row := stmtGetXonker.QueryRow(ident, "boxes")
1623 err := row.Scan(&info)
1624 if err == nil {
1625 return
1626 }
1627 log.Printf("ingesting boxes: %s", ident)
1628 inbox, _ := obj.GetString("inbox")
1629 outbox, _ := obj.GetString("outbox")
1630 sbox, _ := obj.GetString("endpoints", "sharedInbox")
1631 if inbox != "" {
1632 when := time.Now().UTC().Format(dbtimeformat)
1633 m := strings.Join([]string{inbox, outbox, sbox}, " ")
1634 _, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1635 if err != nil {
1636 log.Printf("error saving boxes: %s", err)
1637 }
1638 }
1639}
1640
1641func ingesthandle(origin string, obj junk.Junk) {
1642 xid, _ := obj.GetString("id")
1643 if xid == "" {
1644 return
1645 }
1646 if originate(xid) != origin {
1647 return
1648 }
1649 var handle string
1650 row := stmtGetXonker.QueryRow(xid, "handle")
1651 err := row.Scan(&handle)
1652 if err == nil {
1653 return
1654 }
1655 handle, _ = obj.GetString("preferredUsername")
1656 if handle != "" {
1657 when := time.Now().UTC().Format(dbtimeformat)
1658 _, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1659 if err != nil {
1660 log.Printf("error saving handle: %s", err)
1661 }
1662 }
1663}
1664
1665func updateMe(username string) {
1666 var user *WhatAbout
1667 somenamedusers.Get(username, &user)
1668 dt := time.Now().UTC().Format(time.RFC3339)
1669 j := junk.New()
1670 j["@context"] = itiswhatitis
1671 j["id"] = fmt.Sprintf("%s/upme/%s/%d", user.URL, user.Name, time.Now().Unix())
1672 j["actor"] = user.URL
1673 j["published"] = dt
1674 j["to"] = thewholeworld
1675 j["type"] = "Update"
1676 j["object"] = junkuser(user)
1677
1678 msg := j.ToBytes()
1679
1680 rcpts := make(map[string]bool)
1681 for _, f := range getdubs(user.ID) {
1682 if f.XID == user.URL {
1683 continue
1684 }
1685 var box *Box
1686 boxofboxes.Get(f.XID, &box)
1687 if box != nil && box.Shared != "" {
1688 rcpts["%"+box.Shared] = true
1689 } else {
1690 rcpts[f.XID] = true
1691 }
1692 }
1693 for a := range rcpts {
1694 go deliverate(0, user.ID, a, msg)
1695 }
1696}
1697
1698func followme(user *WhatAbout, who string, name string, j junk.Junk) {
1699 folxid, _ := j.GetString("id")
1700
1701 log.Printf("updating honker follow: %s %s", who, folxid)
1702
1703 var x string
1704 db := opendatabase()
1705 row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", name, who, user.ID)
1706 err := row.Scan(&x)
1707 if err != sql.ErrNoRows {
1708 log.Printf("duplicate follow request: %s", who)
1709 _, err = stmtUpdateFlavor.Exec("dub", folxid, user.ID, name, who, "undub")
1710 if err != nil {
1711 log.Printf("error updating honker: %s", err)
1712 }
1713 } else {
1714 stmtSaveDub.Exec(user.ID, name, who, "dub", folxid)
1715 }
1716 go rubadubdub(user, j)
1717}
1718
1719func unfollowme(user *WhatAbout, who string, name string, j junk.Junk) {
1720 var folxid string
1721 if who == "" {
1722 folxid, _ = j.GetString("object")
1723
1724 db := opendatabase()
1725 row := db.QueryRow("select xid, name from honkers where userid = ? and folxid = ? and flavor in ('dub', 'undub')", user.ID, folxid)
1726 err := row.Scan(&who, &name)
1727 if err != nil {
1728 if err != sql.ErrNoRows {
1729 log.Printf("error scanning honker: %s", err)
1730 }
1731 return
1732 }
1733 }
1734
1735 log.Printf("updating honker undo: %s %s", who, folxid)
1736 _, err := stmtUpdateFlavor.Exec("undub", folxid, user.ID, name, who, "dub")
1737 if err != nil {
1738 log.Printf("error updating honker: %s", err)
1739 return
1740 }
1741}
1742
1743func followyou(user *WhatAbout, honkerid int64) {
1744 var url, owner string
1745 db := opendatabase()
1746 row := db.QueryRow("select xid, owner from honkers where honkerid = ? and userid = ? and flavor in ('unsub', 'peep', 'presub', 'sub')",
1747 honkerid, user.ID)
1748 err := row.Scan(&url, &owner)
1749 if err != nil {
1750 log.Printf("can't get honker xid: %s", err)
1751 return
1752 }
1753 folxid := xfiltrate()
1754 log.Printf("subscribing to %s", url)
1755 _, err = db.Exec("update honkers set flavor = ?, folxid = ? where honkerid = ?", "presub", folxid, honkerid)
1756 if err != nil {
1757 log.Printf("error updating honker: %s", err)
1758 return
1759 }
1760 go subsub(user, url, owner, folxid)
1761
1762}
1763func unfollowyou(user *WhatAbout, honkerid int64) {
1764 db := opendatabase()
1765 row := db.QueryRow("select xid, owner, folxid from honkers where honkerid = ? and userid = ? and flavor in ('sub')",
1766 honkerid, user.ID)
1767 var url, owner, folxid string
1768 err := row.Scan(&url, &owner, &folxid)
1769 if err != nil {
1770 log.Printf("can't get honker xid: %s", err)
1771 return
1772 }
1773 log.Printf("unsubscribing from %s", url)
1774 _, err = db.Exec("update honkers set flavor = ? where honkerid = ?", "unsub", honkerid)
1775 if err != nil {
1776 log.Printf("error updating honker: %s", err)
1777 return
1778 }
1779 go itakeitallback(user, url, owner, folxid)
1780}
1781
1782func followyou2(user *WhatAbout, j junk.Junk) {
1783 who, _ := j.GetString("actor")
1784
1785 log.Printf("updating honker accept: %s", who)
1786 db := opendatabase()
1787 row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub')",
1788 user.ID, who)
1789 var name, folxid string
1790 err := row.Scan(&name, &folxid)
1791 if err != nil {
1792 log.Printf("can't get honker name: %s", err)
1793 return
1794 }
1795 _, err = stmtUpdateFlavor.Exec("sub", folxid, user.ID, name, who, "presub")
1796 if err != nil {
1797 log.Printf("error updating honker: %s", err)
1798 return
1799 }
1800}