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