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 notrand "math/rand"
27 "net/http"
28 "net/url"
29 "os"
30 "regexp"
31 "strings"
32 "time"
33
34 "humungus.tedunangst.com/r/webs/cache"
35 "humungus.tedunangst.com/r/webs/gate"
36 "humungus.tedunangst.com/r/webs/httpsig"
37 "humungus.tedunangst.com/r/webs/junk"
38 "humungus.tedunangst.com/r/webs/templates"
39)
40
41var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
42var thefakename = `application/activity+json`
43var falsenames = []string{
44 `application/ld+json`,
45 `application/activity+json`,
46}
47var itiswhatitis = "https://www.w3.org/ns/activitystreams"
48var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
49
50var fastTimeout time.Duration = 5
51var slowTimeout time.Duration = 30
52
53func friendorfoe(ct string) bool {
54 ct = strings.ToLower(ct)
55 for _, at := range falsenames {
56 if strings.HasPrefix(ct, at) {
57 return true
58 }
59 }
60 return false
61}
62
63var develClient = &http.Client{
64 Transport: &http.Transport{
65 TLSClientConfig: &tls.Config{
66 InsecureSkipVerify: true,
67 },
68 },
69}
70
71func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error {
72 return PostMsg(keyname, key, url, j.ToBytes())
73}
74
75func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error {
76 client := http.DefaultClient
77 if develMode {
78 client = develClient
79 }
80 req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
81 if err != nil {
82 return err
83 }
84 req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
85 req.Header.Set("Content-Type", theonetruename)
86 httpsig.SignRequest(keyname, key, req, msg)
87 ctx, cancel := context.WithTimeout(context.Background(), 2*slowTimeout*time.Second)
88 defer cancel()
89 req = req.WithContext(ctx)
90 resp, err := client.Do(req)
91 if err != nil {
92 return err
93 }
94 resp.Body.Close()
95 switch resp.StatusCode {
96 case 200:
97 case 201:
98 case 202:
99 default:
100 return fmt.Errorf("http post status: %d", resp.StatusCode)
101 }
102 ilog.Printf("successful post: %s %d", url, resp.StatusCode)
103 return nil
104}
105
106func GetJunk(userid int64, url string) (junk.Junk, error) {
107 return GetJunkTimeout(userid, url, slowTimeout*time.Second)
108}
109
110func GetJunkFast(userid int64, url string) (junk.Junk, error) {
111 return GetJunkTimeout(userid, url, fastTimeout*time.Second)
112}
113
114func GetJunkHardMode(userid int64, url string) (junk.Junk, error) {
115 j, err := GetJunk(userid, url)
116 if err != nil {
117 emsg := err.Error()
118 if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
119 ilog.Printf("trying again after error: %s", emsg)
120 time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
121 j, err = GetJunk(userid, url)
122 if err != nil {
123 ilog.Printf("still couldn't get it")
124 } else {
125 ilog.Printf("retry success!")
126 }
127 }
128 }
129 return j, err
130}
131
132var flightdeck = gate.NewSerializer()
133
134var signGets = true
135
136func junkGet(userid int64, url string, args junk.GetArgs) (junk.Junk, error) {
137 client := http.DefaultClient
138 if args.Client != nil {
139 client = args.Client
140 }
141 req, err := http.NewRequest("GET", url, nil)
142 if err != nil {
143 return nil, err
144 }
145 if args.Accept != "" {
146 req.Header.Set("Accept", args.Accept)
147 }
148 if args.Agent != "" {
149 req.Header.Set("User-Agent", args.Agent)
150 }
151 if signGets {
152 var ki *KeyInfo
153 ok := ziggies.Get(userid, &ki)
154 if ok {
155 httpsig.SignRequest(ki.keyname, ki.seckey, req, nil)
156 }
157 }
158 if args.Timeout != 0 {
159 ctx, cancel := context.WithTimeout(context.Background(), args.Timeout)
160 defer cancel()
161 req = req.WithContext(ctx)
162 }
163 resp, err := client.Do(req)
164 if err != nil {
165 return nil, err
166 }
167 defer resp.Body.Close()
168
169 if resp.StatusCode != 200 {
170 return nil, fmt.Errorf("http get status: %d", resp.StatusCode)
171 }
172 return junk.Read(resp.Body)
173}
174
175func GetJunkTimeout(userid int64, url string, timeout time.Duration) (junk.Junk, error) {
176 client := http.DefaultClient
177 if develMode {
178 client = develClient
179 }
180 fn := func() (interface{}, error) {
181 at := thefakename
182 if strings.Contains(url, ".well-known/webfinger?resource") {
183 at = "application/jrd+json"
184 }
185 j, err := junkGet(userid, url, junk.GetArgs{
186 Accept: at,
187 Agent: "honksnonk/5.0; " + serverName,
188 Timeout: timeout,
189 Client: client,
190 })
191 return j, err
192 }
193
194 ji, err := flightdeck.Call(url, fn)
195 if err != nil {
196 return nil, err
197 }
198 j := ji.(junk.Junk)
199 return j, nil
200}
201
202func fetchsome(url string) ([]byte, error) {
203 client := http.DefaultClient
204 if develMode {
205 client = develClient
206 }
207 req, err := http.NewRequest("GET", url, nil)
208 if err != nil {
209 ilog.Printf("error fetching %s: %s", url, err)
210 return nil, err
211 }
212 req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
213 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
214 defer cancel()
215 req = req.WithContext(ctx)
216 resp, err := client.Do(req)
217 if err != nil {
218 ilog.Printf("error fetching %s: %s", url, err)
219 return nil, err
220 }
221 defer resp.Body.Close()
222 switch resp.StatusCode {
223 case 200:
224 case 201:
225 case 202:
226 default:
227 return nil, fmt.Errorf("http get not 200: %d %s", resp.StatusCode, url)
228 }
229 var buf bytes.Buffer
230 limiter := io.LimitReader(resp.Body, 10*1024*1024)
231 io.Copy(&buf, limiter)
232 return buf.Bytes(), nil
233}
234
235func savedonk(url string, name, desc, media string, localize bool) *Donk {
236 if url == "" {
237 return nil
238 }
239 if donk := finddonk(url); donk != nil {
240 return donk
241 }
242 ilog.Printf("saving donk: %s", url)
243 data := []byte{}
244 if localize {
245 fn := func() (interface{}, error) {
246 return fetchsome(url)
247 }
248 ii, err := flightdeck.Call(url, fn)
249 if err != nil {
250 ilog.Printf("error fetching donk: %s", err)
251 localize = false
252 goto saveit
253 }
254 data = ii.([]byte)
255
256 if len(data) == 10*1024*1024 {
257 ilog.Printf("truncation likely")
258 }
259 if strings.HasPrefix(media, "image") {
260 img, err := shrinkit(data)
261 if err != nil {
262 ilog.Printf("unable to decode image: %s", err)
263 localize = false
264 data = []byte{}
265 goto saveit
266 }
267 data = img.Data
268 media = "image/" + img.Format
269 } else if media == "application/pdf" {
270 if len(data) > 1000000 {
271 ilog.Printf("not saving large pdf")
272 localize = false
273 data = []byte{}
274 }
275 } else if len(data) > 100000 {
276 ilog.Printf("not saving large attachment")
277 localize = false
278 data = []byte{}
279 }
280 }
281saveit:
282 fileid, err := savefile(name, desc, url, media, localize, data)
283 if err != nil {
284 elog.Printf("error saving file %s: %s", url, err)
285 return nil
286 }
287 donk := new(Donk)
288 donk.FileID = fileid
289 return donk
290}
291
292func iszonked(userid int64, xid string) bool {
293 var id int64
294 row := stmtFindZonk.QueryRow(userid, xid)
295 err := row.Scan(&id)
296 if err == nil {
297 return true
298 }
299 if err != sql.ErrNoRows {
300 ilog.Printf("error querying zonk: %s", err)
301 }
302 return false
303}
304
305func needxonk(user *WhatAbout, x *Honk) bool {
306 if rejectxonk(x) {
307 return false
308 }
309 return needxonkid(user, x.XID)
310}
311func needbonkid(user *WhatAbout, xid string) bool {
312 return needxonkidX(user, xid, true)
313}
314func needxonkid(user *WhatAbout, xid string) bool {
315 return needxonkidX(user, xid, false)
316}
317func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
318 if !strings.HasPrefix(xid, "https://") {
319 return false
320 }
321 if strings.HasPrefix(xid, user.URL+"/") {
322 return false
323 }
324 if rejectorigin(user.ID, xid, isannounce) {
325 ilog.Printf("rejecting origin: %s", xid)
326 return false
327 }
328 if iszonked(user.ID, xid) {
329 ilog.Printf("already zonked: %s", xid)
330 return false
331 }
332 var id int64
333 row := stmtFindXonk.QueryRow(user.ID, xid)
334 err := row.Scan(&id)
335 if err == nil {
336 return false
337 }
338 if err != sql.ErrNoRows {
339 ilog.Printf("error querying xonk: %s", err)
340 }
341 return true
342}
343
344func eradicatexonk(userid int64, xid string) {
345 xonk := getxonk(userid, xid)
346 if xonk != nil {
347 deletehonk(xonk.ID)
348 }
349 _, err := stmtSaveZonker.Exec(userid, xid, "zonk")
350 if err != nil {
351 elog.Printf("error eradicating: %s", err)
352 }
353}
354
355func savexonk(x *Honk) {
356 ilog.Printf("saving xonk: %s", x.XID)
357 go handles(x.Honker)
358 go handles(x.Oonker)
359 savehonk(x)
360}
361
362type Box struct {
363 In string
364 Out string
365 Shared string
366}
367
368var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) {
369 var info string
370 row := stmtGetXonker.QueryRow(ident, "boxes")
371 err := row.Scan(&info)
372 if err != nil {
373 dlog.Printf("need to get boxes for %s", ident)
374 var j junk.Junk
375 j, err = GetJunk(readyLuserOne, ident)
376 if err != nil {
377 dlog.Printf("error getting boxes: %s", err)
378 return nil, false
379 }
380 allinjest(originate(ident), j)
381 row = stmtGetXonker.QueryRow(ident, "boxes")
382 err = row.Scan(&info)
383 }
384 if err == nil {
385 m := strings.Split(info, " ")
386 b := &Box{In: m[0], Out: m[1], Shared: m[2]}
387 return b, true
388 }
389 return nil, false
390}})
391
392func gimmexonks(user *WhatAbout, outbox string) {
393 dlog.Printf("getting outbox: %s", outbox)
394 j, err := GetJunk(user.ID, outbox)
395 if err != nil {
396 ilog.Printf("error getting outbox: %s", err)
397 return
398 }
399 t, _ := j.GetString("type")
400 origin := originate(outbox)
401 if t == "OrderedCollection" {
402 items, _ := j.GetArray("orderedItems")
403 if items == nil {
404 items, _ = j.GetArray("items")
405 }
406 if items == nil {
407 obj, ok := j.GetMap("first")
408 if ok {
409 items, _ = obj.GetArray("orderedItems")
410 } else {
411 page1, ok := j.GetString("first")
412 if ok {
413 j, err = GetJunk(user.ID, page1)
414 if err != nil {
415 ilog.Printf("error getting page1: %s", err)
416 return
417 }
418 items, _ = j.GetArray("orderedItems")
419 }
420 }
421 }
422 if len(items) > 20 {
423 items = items[0:20]
424 }
425 for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
426 items[i], items[j] = items[j], items[i]
427 }
428 for _, item := range items {
429 obj, ok := item.(junk.Junk)
430 if ok {
431 xonksaver(user, obj, origin)
432 continue
433 }
434 xid, ok := item.(string)
435 if ok {
436 if !needxonkid(user, xid) {
437 continue
438 }
439 obj, err = GetJunk(user.ID, xid)
440 if err != nil {
441 ilog.Printf("error getting item: %s", err)
442 continue
443 }
444 xonksaver(user, obj, originate(xid))
445 }
446 }
447 }
448}
449
450func newphone(a []string, obj junk.Junk) []string {
451 for _, addr := range []string{"to", "cc", "attributedTo"} {
452 who, _ := obj.GetString(addr)
453 if who != "" {
454 a = append(a, who)
455 }
456 whos, _ := obj.GetArray(addr)
457 for _, w := range whos {
458 who, _ := w.(string)
459 if who != "" {
460 a = append(a, who)
461 }
462 }
463 }
464 return a
465}
466
467func extractattrto(obj junk.Junk) string {
468 who, _ := obj.GetString("attributedTo")
469 if who != "" {
470 return who
471 }
472 o, ok := obj.GetMap("attributedTo")
473 if ok {
474 id, ok := o.GetString("id")
475 if ok {
476 return id
477 }
478 }
479 arr, _ := obj.GetArray("attributedTo")
480 for _, a := range arr {
481 o, ok := a.(junk.Junk)
482 if ok {
483 t, _ := o.GetString("type")
484 id, _ := o.GetString("id")
485 if t == "Person" || t == "" {
486 return id
487 }
488 }
489 s, ok := a.(string)
490 if ok {
491 return s
492 }
493 }
494 return ""
495}
496
497func firstofmany(obj junk.Junk, key string) string {
498 if val, _ := obj.GetString(key); val != "" {
499 return val
500 }
501 if arr, _ := obj.GetArray(key); len(arr) > 0 {
502 val, ok := arr[0].(string)
503 if ok {
504 return val
505 }
506 }
507 return ""
508}
509
510var re_mast0link = regexp.MustCompile(`https://[[:alnum:].]+/users/[[:alnum:]]+/statuses/[[:digit:]]+`)
511var re_masto1ink = regexp.MustCompile(`https://[[:alnum:].]+/@[[:alnum:]]+/[[:digit:]]+`)
512var re_misslink = regexp.MustCompile(`https://[[:alnum:].]+/notes/[[:alnum:]]+`)
513var re_honklink = regexp.MustCompile(`https://[[:alnum:].]+/u/[[:alnum:]]+/h/[[:alnum:]]+`)
514var re_romalink = regexp.MustCompile(`https://[[:alnum:].]+/objects/[[:alnum:]-]+`)
515var re_qtlinks = regexp.MustCompile(`>https://[^\s<]+<`)
516
517func qutify(user *WhatAbout, content string) string {
518 // well this is gross
519 malcontent := strings.ReplaceAll(content, `</span><span class="ellipsis">`, "")
520 malcontent = strings.ReplaceAll(malcontent, `</span><span class="invisible">`, "")
521 mlinks := re_qtlinks.FindAllString(malcontent, -1)
522 for _, m := range mlinks {
523 m = m[1 : len(m)-1]
524 if re_mast0link.MatchString(m) ||
525 re_masto1ink.MatchString(m) ||
526 re_misslink.MatchString(m) ||
527 re_honklink.MatchString(m) ||
528 re_romalink.MatchString(m) {
529 j, err := GetJunk(user.ID, m)
530 if err == nil {
531 q, ok := j.GetString("content")
532 if ok {
533 content = fmt.Sprintf("%s<blockquote>%s</blockquote>", content, q)
534 }
535 }
536 }
537 }
538 return content
539}
540
541func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
542 depth := 0
543 maxdepth := 10
544 currenttid := ""
545 goingup := 0
546 var xonkxonkfn func(item junk.Junk, origin string, isUpdate bool) *Honk
547
548 saveonemore := func(xid string) {
549 dlog.Printf("getting onemore: %s", xid)
550 if depth >= maxdepth {
551 ilog.Printf("in too deep")
552 return
553 }
554 obj, err := GetJunkHardMode(user.ID, xid)
555 if err != nil {
556 ilog.Printf("error getting onemore: %s: %s", xid, err)
557 return
558 }
559 depth++
560 xonkxonkfn(obj, originate(xid), false)
561 depth--
562 }
563
564 xonkxonkfn = func(item junk.Junk, origin string, isUpdate bool) *Honk {
565 id, _ := item.GetString("id")
566 what := firstofmany(item, "type")
567 dt, ok := item.GetString("published")
568 if !ok {
569 dt = time.Now().Format(time.RFC3339)
570 }
571
572 var err error
573 var xid, rid, url, convoy string
574 var replies []string
575 var obj junk.Junk
576 waspage := false
577 switch what {
578 case "Delete":
579 obj, ok = item.GetMap("object")
580 if ok {
581 xid, _ = obj.GetString("id")
582 } else {
583 xid, _ = item.GetString("object")
584 }
585 if xid == "" {
586 return nil
587 }
588 if originate(xid) != origin {
589 ilog.Printf("forged delete: %s", xid)
590 return nil
591 }
592 ilog.Printf("eradicating %s", xid)
593 eradicatexonk(user.ID, xid)
594 return nil
595 case "Remove":
596 xid, _ = item.GetString("object")
597 targ, _ := obj.GetString("target")
598 ilog.Printf("remove %s from %s", obj, targ)
599 return nil
600 case "Tombstone":
601 xid, _ = item.GetString("id")
602 if xid == "" {
603 return nil
604 }
605 if originate(xid) != origin {
606 ilog.Printf("forged delete: %s", xid)
607 return nil
608 }
609 ilog.Printf("eradicating %s", xid)
610 eradicatexonk(user.ID, xid)
611 return nil
612 case "Announce":
613 obj, ok = item.GetMap("object")
614 if ok {
615 xid, _ = obj.GetString("id")
616 } else {
617 xid, _ = item.GetString("object")
618 }
619 if !needbonkid(user, xid) {
620 return nil
621 }
622 dlog.Printf("getting bonk: %s", xid)
623 obj, err = GetJunkHardMode(user.ID, xid)
624 if err != nil {
625 ilog.Printf("error getting bonk: %s: %s", xid, err)
626 }
627 origin = originate(xid)
628 what = "bonk"
629 case "Update":
630 isUpdate = true
631 fallthrough
632 case "Create":
633 obj, ok = item.GetMap("object")
634 if !ok {
635 xid, _ = item.GetString("object")
636 dlog.Printf("getting created honk: %s", xid)
637 if originate(xid) != origin {
638 ilog.Printf("out of bounds %s not from %s", xid, origin)
639 return nil
640 }
641 obj, err = GetJunkHardMode(user.ID, xid)
642 if err != nil {
643 ilog.Printf("error getting creation: %s", err)
644 }
645 }
646 if obj == nil {
647 ilog.Printf("no object for creation %s", id)
648 return nil
649 }
650 return xonkxonkfn(obj, origin, isUpdate)
651 case "Read":
652 xid, ok = item.GetString("object")
653 if ok {
654 if !needxonkid(user, xid) {
655 dlog.Printf("don't need read obj: %s", xid)
656 return nil
657 }
658 obj, err = GetJunkHardMode(user.ID, xid)
659 if err != nil {
660 ilog.Printf("error getting read: %s", err)
661 return nil
662 }
663 return xonkxonkfn(obj, originate(xid), false)
664 }
665 return nil
666 case "Add":
667 xid, ok = item.GetString("object")
668 if ok {
669 // check target...
670 if !needxonkid(user, xid) {
671 dlog.Printf("don't need added obj: %s", xid)
672 return nil
673 }
674 obj, err = GetJunkHardMode(user.ID, xid)
675 if err != nil {
676 ilog.Printf("error getting add: %s", err)
677 return nil
678 }
679 return xonkxonkfn(obj, originate(xid), false)
680 }
681 return nil
682 case "Move":
683 obj = item
684 what = "move"
685 case "Page":
686 waspage = true
687 fallthrough
688 case "Audio":
689 fallthrough
690 case "Image":
691 fallthrough
692 case "Video":
693 fallthrough
694 case "Question":
695 fallthrough
696 case "Note":
697 fallthrough
698 case "Article":
699 obj = item
700 what = "honk"
701 case "Event":
702 obj = item
703 what = "event"
704 case "ChatMessage":
705 obj = item
706 what = "chonk"
707 default:
708 ilog.Printf("unknown activity: %s", what)
709 dumpactivity(item)
710 return nil
711 }
712
713 if obj != nil {
714 xid, _ = obj.GetString("id")
715 }
716
717 if xid == "" {
718 ilog.Printf("don't know what xid is")
719 item.Write(ilog.Writer())
720 return nil
721 }
722 if originate(xid) != origin {
723 if !develMode && origin != "" {
724 ilog.Printf("original sin: %s not from %s", xid, origin)
725 item.Write(ilog.Writer())
726 return nil
727 }
728 }
729
730 var xonk Honk
731 // early init
732 xonk.XID = xid
733 xonk.UserID = user.ID
734 xonk.Honker, _ = item.GetString("actor")
735 if xonk.Honker == "" {
736 xonk.Honker, _ = item.GetString("attributedTo")
737 }
738 if obj != nil {
739 if xonk.Honker == "" {
740 xonk.Honker = extractattrto(obj)
741 }
742 xonk.Oonker = extractattrto(obj)
743 if xonk.Oonker == xonk.Honker {
744 xonk.Oonker = ""
745 }
746 xonk.Audience = newphone(nil, obj)
747 }
748 xonk.Audience = append(xonk.Audience, xonk.Honker)
749 xonk.Audience = oneofakind(xonk.Audience)
750 xonk.Public = loudandproud(xonk.Audience)
751
752 var mentions []Mention
753 if obj != nil {
754 ot, _ := obj.GetString("type")
755 url, _ = obj.GetString("url")
756 if dt2, ok := obj.GetString("published"); ok {
757 dt = dt2
758 }
759 content, _ := obj.GetString("content")
760 if !strings.HasPrefix(content, "<p>") {
761 content = "<p>" + content
762 }
763 precis, _ := obj.GetString("summary")
764 if name, ok := obj.GetString("name"); ok {
765 if precis != "" {
766 content = precis + "<p>" + content
767 }
768 precis = html.EscapeString(name)
769 }
770 if sens, _ := obj["sensitive"].(bool); sens && precis == "" {
771 precis = "unspecified horror"
772 }
773 if waspage {
774 content += fmt.Sprintf(`<p><a href="%s">%s</a>`, url, url)
775 url = xid
776 }
777 if user.Options.InlineQuotes {
778 content = qutify(user, content)
779 }
780 rid, ok = obj.GetString("inReplyTo")
781 if !ok {
782 if robj, ok := obj.GetMap("inReplyTo"); ok {
783 rid, _ = robj.GetString("id")
784 }
785 }
786 convoy, _ = obj.GetString("context")
787 if convoy == "" {
788 convoy, _ = obj.GetString("conversation")
789 }
790 if ot == "Question" {
791 if what == "honk" {
792 what = "qonk"
793 }
794 content += "<ul>"
795 ans, _ := obj.GetArray("oneOf")
796 for _, ai := range ans {
797 a, ok := ai.(junk.Junk)
798 if !ok {
799 continue
800 }
801 as, _ := a.GetString("name")
802 content += "<li>" + as
803 }
804 ans, _ = obj.GetArray("anyOf")
805 for _, ai := range ans {
806 a, ok := ai.(junk.Junk)
807 if !ok {
808 continue
809 }
810 as, _ := a.GetString("name")
811 content += "<li>" + as
812 }
813 content += "</ul>"
814 }
815 if ot == "Move" {
816 targ, _ := obj.GetString("target")
817 content += string(templates.Sprintf(`<p>Moved to <a href="%s">%s</a>`, targ, targ))
818 }
819 if what == "honk" && rid != "" {
820 what = "tonk"
821 }
822 if len(content) > 90001 {
823 ilog.Printf("content too long. truncating")
824 content = content[:90001]
825 }
826
827 xonk.Noise = content
828 xonk.Precis = precis
829 if rejectxonk(&xonk) {
830 dlog.Printf("fast reject: %s", xid)
831 return nil
832 }
833
834 numatts := 0
835 procatt := func(att junk.Junk) {
836 at, _ := att.GetString("type")
837 mt, _ := att.GetString("mediaType")
838 u, ok := att.GetString("url")
839 if !ok {
840 if ua, ok := att.GetArray("url"); ok && len(ua) > 0 {
841 u, ok = ua[0].(string)
842 if !ok {
843 if uu, ok := ua[0].(junk.Junk); ok {
844 u, _ = uu.GetString("href")
845 if mt == "" {
846 mt, _ = uu.GetString("mediaType")
847 }
848 }
849 }
850 } else if uu, ok := att.GetMap("url"); ok {
851 u, _ = uu.GetString("href")
852 if mt == "" {
853 mt, _ = uu.GetString("mediaType")
854 }
855 }
856 }
857 name, _ := att.GetString("name")
858 desc, _ := att.GetString("summary")
859 desc = html.UnescapeString(desc)
860 if desc == "" {
861 desc = name
862 }
863 localize := false
864 if numatts > 4 {
865 ilog.Printf("excessive attachment: %s", at)
866 } else if at == "Document" || at == "Image" {
867 mt = strings.ToLower(mt)
868 dlog.Printf("attachment: %s %s", mt, u)
869 if mt == "text/plain" || mt == "application/pdf" ||
870 strings.HasPrefix(mt, "image") {
871 localize = true
872 }
873 } else {
874 ilog.Printf("unknown attachment: %s", at)
875 }
876 if skipMedia(&xonk) {
877 localize = false
878 }
879 donk := savedonk(u, name, desc, mt, localize)
880 if donk != nil {
881 xonk.Donks = append(xonk.Donks, donk)
882 }
883 numatts++
884 }
885 atts, _ := obj.GetArray("attachment")
886 for _, atti := range atts {
887 att, ok := atti.(junk.Junk)
888 if !ok {
889 ilog.Printf("attachment that wasn't map?")
890 continue
891 }
892 procatt(att)
893 }
894 if att, ok := obj.GetMap("attachment"); ok {
895 procatt(att)
896 }
897 tags, _ := obj.GetArray("tag")
898 for _, tagi := range tags {
899 tag, ok := tagi.(junk.Junk)
900 if !ok {
901 continue
902 }
903 tt, _ := tag.GetString("type")
904 name, _ := tag.GetString("name")
905 desc, _ := tag.GetString("summary")
906 desc = html.UnescapeString(desc)
907 if desc == "" {
908 desc = name
909 }
910 if tt == "Emoji" {
911 icon, _ := tag.GetMap("icon")
912 mt, _ := icon.GetString("mediaType")
913 if mt == "" {
914 mt = "image/png"
915 }
916 u, _ := icon.GetString("url")
917 donk := savedonk(u, name, desc, mt, true)
918 if donk != nil {
919 xonk.Donks = append(xonk.Donks, donk)
920 }
921 }
922 if tt == "Hashtag" {
923 if name == "" || name == "#" {
924 // skip it
925 } else {
926 if name[0] != '#' {
927 name = "#" + name
928 }
929 xonk.Onts = append(xonk.Onts, name)
930 }
931 }
932 if tt == "Place" {
933 p := new(Place)
934 p.Name = name
935 p.Latitude, _ = tag.GetNumber("latitude")
936 p.Longitude, _ = tag.GetNumber("longitude")
937 p.Url, _ = tag.GetString("url")
938 xonk.Place = p
939 }
940 if tt == "Mention" {
941 var m Mention
942 m.Who, _ = tag.GetString("name")
943 m.Where, _ = tag.GetString("href")
944 mentions = append(mentions, m)
945 }
946 }
947 if starttime, ok := obj.GetString("startTime"); ok {
948 if start, err := time.Parse(time.RFC3339, starttime); err == nil {
949 t := new(Time)
950 t.StartTime = start
951 endtime, _ := obj.GetString("endTime")
952 t.EndTime, _ = time.Parse(time.RFC3339, endtime)
953 dura, _ := obj.GetString("duration")
954 if strings.HasPrefix(dura, "PT") {
955 dura = strings.ToLower(dura[2:])
956 d, _ := time.ParseDuration(dura)
957 t.Duration = Duration(d)
958 }
959 xonk.Time = t
960 }
961 }
962 if loca, ok := obj.GetMap("location"); ok {
963 if tt, _ := loca.GetString("type"); tt == "Place" {
964 p := new(Place)
965 p.Name, _ = loca.GetString("name")
966 p.Latitude, _ = loca.GetNumber("latitude")
967 p.Longitude, _ = loca.GetNumber("longitude")
968 p.Url, _ = loca.GetString("url")
969 xonk.Place = p
970 }
971 }
972
973 xonk.Onts = oneofakind(xonk.Onts)
974 replyobj, ok := obj.GetMap("replies")
975 if ok {
976 items, ok := replyobj.GetArray("items")
977 if !ok {
978 first, ok := replyobj.GetMap("first")
979 if ok {
980 items, _ = first.GetArray("items")
981 }
982 }
983 for _, repl := range items {
984 s, ok := repl.(string)
985 if ok {
986 replies = append(replies, s)
987 }
988 }
989 }
990
991 }
992
993 if currenttid == "" {
994 currenttid = convoy
995 }
996
997 // init xonk
998 xonk.What = what
999 xonk.RID = rid
1000 xonk.Date, _ = time.Parse(time.RFC3339, dt)
1001 xonk.URL = url
1002 xonk.Format = "html"
1003 xonk.Convoy = convoy
1004 xonk.Mentions = mentions
1005 for _, m := range mentions {
1006 if m.Where == user.URL {
1007 xonk.Whofore = 1
1008 }
1009 }
1010 imaginate(&xonk)
1011
1012 if what == "chonk" {
1013 ch := Chonk{
1014 UserID: xonk.UserID,
1015 XID: xid,
1016 Who: xonk.Honker,
1017 Target: xonk.Honker,
1018 Date: xonk.Date,
1019 Noise: xonk.Noise,
1020 Format: xonk.Format,
1021 Donks: xonk.Donks,
1022 }
1023 savechonk(&ch)
1024 return nil
1025 }
1026
1027 if isUpdate {
1028 dlog.Printf("something has changed! %s", xonk.XID)
1029 prev := getxonk(user.ID, xonk.XID)
1030 if prev == nil {
1031 ilog.Printf("didn't find old version for update: %s", xonk.XID)
1032 isUpdate = false
1033 } else {
1034 xonk.ID = prev.ID
1035 updatehonk(&xonk)
1036 }
1037 }
1038 if !isUpdate && needxonk(user, &xonk) {
1039 if rid != "" && xonk.Public {
1040 if needxonkid(user, rid) {
1041 goingup++
1042 saveonemore(rid)
1043 goingup--
1044 }
1045 if convoy == "" {
1046 xx := getxonk(user.ID, rid)
1047 if xx != nil {
1048 convoy = xx.Convoy
1049 }
1050 }
1051 }
1052 if convoy == "" {
1053 convoy = currenttid
1054 }
1055 if convoy == "" {
1056 convoy = "data:,missing-" + xfiltrate()
1057 currenttid = convoy
1058 }
1059 xonk.Convoy = convoy
1060 savexonk(&xonk)
1061 }
1062 if goingup == 0 {
1063 for _, replid := range replies {
1064 if needxonkid(user, replid) {
1065 dlog.Printf("missing a reply: %s", replid)
1066 saveonemore(replid)
1067 }
1068 }
1069 }
1070 return &xonk
1071 }
1072
1073 return xonkxonkfn(item, origin, false)
1074}
1075
1076func dumpactivity(item junk.Junk) {
1077 fd, err := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
1078 if err != nil {
1079 elog.Printf("error opening inbox! %s", err)
1080 return
1081 }
1082 defer fd.Close()
1083 item.Write(fd)
1084 io.WriteString(fd, "\n")
1085}
1086
1087func rubadubdub(user *WhatAbout, req junk.Junk) {
1088 actor, _ := req.GetString("actor")
1089 j := junk.New()
1090 j["@context"] = itiswhatitis
1091 j["id"] = user.URL + "/dub/" + xfiltrate()
1092 j["type"] = "Accept"
1093 j["actor"] = user.URL
1094 j["to"] = actor
1095 j["published"] = time.Now().UTC().Format(time.RFC3339)
1096 j["object"] = req
1097
1098 deliverate(0, user.ID, actor, j.ToBytes(), true)
1099}
1100
1101func itakeitallback(user *WhatAbout, xid string, owner string, folxid string) {
1102 j := junk.New()
1103 j["@context"] = itiswhatitis
1104 j["id"] = user.URL + "/unsub/" + folxid
1105 j["type"] = "Undo"
1106 j["actor"] = user.URL
1107 j["to"] = owner
1108 f := junk.New()
1109 f["id"] = user.URL + "/sub/" + folxid
1110 f["type"] = "Follow"
1111 f["actor"] = user.URL
1112 f["to"] = owner
1113 f["object"] = xid
1114 j["object"] = f
1115 j["published"] = time.Now().UTC().Format(time.RFC3339)
1116
1117 deliverate(0, user.ID, owner, j.ToBytes(), true)
1118}
1119
1120func subsub(user *WhatAbout, xid string, owner string, folxid string) {
1121 if xid == "" {
1122 ilog.Printf("can't subscribe to empty")
1123 return
1124 }
1125 j := junk.New()
1126 j["@context"] = itiswhatitis
1127 j["id"] = user.URL + "/sub/" + folxid
1128 j["type"] = "Follow"
1129 j["actor"] = user.URL
1130 j["to"] = owner
1131 j["object"] = xid
1132 j["published"] = time.Now().UTC().Format(time.RFC3339)
1133
1134 deliverate(0, user.ID, owner, j.ToBytes(), true)
1135}
1136
1137func activatedonks(donks []*Donk) []junk.Junk {
1138 var atts []junk.Junk
1139 for _, d := range donks {
1140 if re_emus.MatchString(d.Name) {
1141 continue
1142 }
1143 jd := junk.New()
1144 jd["mediaType"] = d.Media
1145 if false {
1146 // nobody does this right
1147 jd["name"] = d.Name
1148 jd["summary"] = html.EscapeString(d.Desc)
1149 } else {
1150 jd["name"] = d.Desc
1151 }
1152 jd["type"] = "Document"
1153 jd["url"] = d.URL
1154 atts = append(atts, jd)
1155 }
1156 return atts
1157}
1158
1159// returns activity, object
1160func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
1161 dt := h.Date.Format(time.RFC3339)
1162 var jo junk.Junk
1163 j := junk.New()
1164 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
1165 j["actor"] = user.URL
1166 j["published"] = dt
1167 j["to"] = h.Audience[0]
1168 if len(h.Audience) > 1 {
1169 j["cc"] = h.Audience[1:]
1170 }
1171
1172 switch h.What {
1173 case "update":
1174 fallthrough
1175 case "tonk":
1176 fallthrough
1177 case "event":
1178 fallthrough
1179 case "honk":
1180 j["type"] = "Create"
1181 jo = junk.New()
1182 jo["id"] = h.XID
1183 jo["type"] = "Note"
1184 if h.What == "event" {
1185 jo["type"] = "Event"
1186 }
1187 if h.What == "update" {
1188 j["type"] = "Update"
1189 jo["updated"] = dt
1190 }
1191 jo["published"] = dt
1192 jo["url"] = h.XID
1193 jo["attributedTo"] = user.URL
1194 if h.RID != "" {
1195 jo["inReplyTo"] = h.RID
1196 }
1197 if h.Convoy != "" {
1198 jo["context"] = h.Convoy
1199 jo["conversation"] = h.Convoy
1200 }
1201 jo["to"] = h.Audience[0]
1202 if len(h.Audience) > 1 {
1203 jo["cc"] = h.Audience[1:]
1204 }
1205 if !h.Public {
1206 jo["directMessage"] = true
1207 }
1208 translate(h)
1209 redoimages(h)
1210 if h.Precis != "" {
1211 jo["sensitive"] = true
1212 }
1213
1214 var replies []string
1215 for _, reply := range h.Replies {
1216 replies = append(replies, reply.XID)
1217 }
1218 if len(replies) > 0 {
1219 jr := junk.New()
1220 jr["type"] = "Collection"
1221 jr["totalItems"] = len(replies)
1222 jr["items"] = replies
1223 jo["replies"] = jr
1224 }
1225
1226 var tags []junk.Junk
1227 for _, m := range h.Mentions {
1228 t := junk.New()
1229 t["type"] = "Mention"
1230 t["name"] = m.Who
1231 t["href"] = m.Where
1232 tags = append(tags, t)
1233 }
1234 for _, o := range h.Onts {
1235 t := junk.New()
1236 t["type"] = "Hashtag"
1237 o = strings.ToLower(o)
1238 t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1239 t["name"] = o
1240 tags = append(tags, t)
1241 }
1242 for _, e := range herdofemus(h.Noise) {
1243 t := junk.New()
1244 t["id"] = e.ID
1245 t["type"] = "Emoji"
1246 t["name"] = e.Name
1247 i := junk.New()
1248 i["type"] = "Image"
1249 i["mediaType"] = e.Type
1250 i["url"] = e.ID
1251 t["icon"] = i
1252 tags = append(tags, t)
1253 }
1254 for _, e := range fixupflags(h) {
1255 t := junk.New()
1256 t["id"] = e.ID
1257 t["type"] = "Emoji"
1258 t["name"] = e.Name
1259 i := junk.New()
1260 i["type"] = "Image"
1261 i["mediaType"] = "image/png"
1262 i["url"] = e.ID
1263 t["icon"] = i
1264 tags = append(tags, t)
1265 }
1266 if len(tags) > 0 {
1267 jo["tag"] = tags
1268 }
1269 if p := h.Place; p != nil {
1270 t := junk.New()
1271 t["type"] = "Place"
1272 if p.Name != "" {
1273 t["name"] = p.Name
1274 }
1275 if p.Latitude != 0 {
1276 t["latitude"] = p.Latitude
1277 }
1278 if p.Longitude != 0 {
1279 t["longitude"] = p.Longitude
1280 }
1281 if p.Url != "" {
1282 t["url"] = p.Url
1283 }
1284 jo["location"] = t
1285 }
1286 if t := h.Time; t != nil {
1287 jo["startTime"] = t.StartTime.Format(time.RFC3339)
1288 if t.Duration != 0 {
1289 jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1290 }
1291 }
1292 atts := activatedonks(h.Donks)
1293 if len(atts) > 0 {
1294 jo["attachment"] = atts
1295 }
1296 jo["summary"] = h.Precis
1297 jo["content"] = h.Noise
1298 j["object"] = jo
1299 case "bonk":
1300 j["type"] = "Announce"
1301 if h.Convoy != "" {
1302 j["context"] = h.Convoy
1303 }
1304 j["object"] = h.XID
1305 case "unbonk":
1306 b := junk.New()
1307 b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1308 b["type"] = "Announce"
1309 b["actor"] = user.URL
1310 if h.Convoy != "" {
1311 b["context"] = h.Convoy
1312 }
1313 b["object"] = h.XID
1314 j["type"] = "Undo"
1315 j["object"] = b
1316 case "zonk":
1317 j["type"] = "Delete"
1318 j["object"] = h.XID
1319 case "ack":
1320 j["type"] = "Read"
1321 j["object"] = h.XID
1322 if h.Convoy != "" {
1323 j["context"] = h.Convoy
1324 }
1325 case "react":
1326 j["type"] = "EmojiReact"
1327 j["object"] = h.XID
1328 if h.Convoy != "" {
1329 j["context"] = h.Convoy
1330 }
1331 j["content"] = h.Noise
1332 case "deack":
1333 b := junk.New()
1334 b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1335 b["type"] = "Read"
1336 b["actor"] = user.URL
1337 b["object"] = h.XID
1338 if h.Convoy != "" {
1339 b["context"] = h.Convoy
1340 }
1341 j["type"] = "Undo"
1342 j["object"] = b
1343 }
1344
1345 return j, jo
1346}
1347
1348var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1349 row := stmtAnyXonk.QueryRow(xid)
1350 honk := scanhonk(row)
1351 if honk == nil || !honk.Public {
1352 return nil, true
1353 }
1354 user, _ := butwhatabout(honk.Username)
1355 rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1356 reversehonks(rawhonks)
1357 for _, h := range rawhonks {
1358 if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1359 honk.Replies = append(honk.Replies, h)
1360 }
1361 }
1362 donksforhonks([]*Honk{honk})
1363 _, j := jonkjonk(user, honk)
1364 if j == nil {
1365 elog.Fatalf("what just happened? %v", honk)
1366 }
1367 j["@context"] = itiswhatitis
1368
1369 return j.ToBytes(), true
1370}, Limit: 128})
1371
1372func gimmejonk(xid string) ([]byte, bool) {
1373 var j []byte
1374 ok := oldjonks.Get(xid, &j)
1375 return j, ok
1376}
1377
1378func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1379 rcpts := make(map[string]bool)
1380 for _, a := range addresses {
1381 if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1382 continue
1383 }
1384 if a[0] == '%' {
1385 rcpts[a] = true
1386 continue
1387 }
1388 var box *Box
1389 ok := boxofboxes.Get(a, &box)
1390 if ok && useshared && box.Shared != "" {
1391 rcpts["%"+box.Shared] = true
1392 } else {
1393 rcpts[a] = true
1394 }
1395 }
1396 return rcpts
1397}
1398
1399func chonkifymsg(user *WhatAbout, ch *Chonk) []byte {
1400 dt := ch.Date.Format(time.RFC3339)
1401 aud := []string{ch.Target}
1402
1403 jo := junk.New()
1404 jo["id"] = ch.XID
1405 jo["type"] = "ChatMessage"
1406 jo["published"] = dt
1407 jo["attributedTo"] = user.URL
1408 jo["to"] = aud
1409 jo["content"] = ch.HTML
1410 atts := activatedonks(ch.Donks)
1411 if len(atts) > 0 {
1412 jo["attachment"] = atts
1413 }
1414 var tags []junk.Junk
1415 for _, e := range herdofemus(ch.Noise) {
1416 t := junk.New()
1417 t["id"] = e.ID
1418 t["type"] = "Emoji"
1419 t["name"] = e.Name
1420 i := junk.New()
1421 i["type"] = "Image"
1422 i["mediaType"] = e.Type
1423 i["url"] = e.ID
1424 t["icon"] = i
1425 tags = append(tags, t)
1426 }
1427 if len(tags) > 0 {
1428 jo["tag"] = tags
1429 }
1430
1431 j := junk.New()
1432 j["@context"] = itiswhatitis
1433 j["id"] = user.URL + "/" + "honk" + "/" + shortxid(ch.XID)
1434 j["type"] = "Create"
1435 j["actor"] = user.URL
1436 j["published"] = dt
1437 j["to"] = aud
1438 j["object"] = jo
1439
1440 return j.ToBytes()
1441}
1442
1443func sendchonk(user *WhatAbout, ch *Chonk) {
1444 msg := chonkifymsg(user, ch)
1445
1446 rcpts := make(map[string]bool)
1447 rcpts[ch.Target] = true
1448 for a := range rcpts {
1449 go deliverate(0, user.ID, a, msg, true)
1450 }
1451}
1452
1453func honkworldwide(user *WhatAbout, honk *Honk) {
1454 jonk, _ := jonkjonk(user, honk)
1455 jonk["@context"] = itiswhatitis
1456 msg := jonk.ToBytes()
1457
1458 rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1459
1460 if honk.Public {
1461 for _, h := range getdubs(user.ID) {
1462 if h.XID == user.URL {
1463 continue
1464 }
1465 var box *Box
1466 ok := boxofboxes.Get(h.XID, &box)
1467 if ok && box.Shared != "" {
1468 rcpts["%"+box.Shared] = true
1469 } else {
1470 rcpts[h.XID] = true
1471 }
1472 }
1473 for _, f := range getbacktracks(honk.XID) {
1474 if f[0] == '%' {
1475 rcpts[f] = true
1476 } else {
1477 var box *Box
1478 ok := boxofboxes.Get(f, &box)
1479 if ok && box.Shared != "" {
1480 rcpts["%"+box.Shared] = true
1481 } else {
1482 rcpts[f] = true
1483 }
1484 }
1485 }
1486 }
1487 for a := range rcpts {
1488 go deliverate(0, user.ID, a, msg, doesitmatter(honk.What))
1489 }
1490 if honk.Public && len(honk.Onts) > 0 {
1491 collectiveaction(honk)
1492 }
1493}
1494
1495func doesitmatter(what string) bool {
1496 switch what {
1497 case "ack":
1498 return false
1499 case "react":
1500 return false
1501 case "deack":
1502 return false
1503 }
1504 return true
1505}
1506
1507func collectiveaction(honk *Honk) {
1508 user := getserveruser()
1509 for _, ont := range honk.Onts {
1510 dubs := getnameddubs(readyLuserOne, ont)
1511 if len(dubs) == 0 {
1512 continue
1513 }
1514 j := junk.New()
1515 j["@context"] = itiswhatitis
1516 j["type"] = "Add"
1517 j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1518 j["actor"] = user.URL
1519 j["object"] = honk.XID
1520 j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1521 rcpts := make(map[string]bool)
1522 for _, dub := range dubs {
1523 var box *Box
1524 ok := boxofboxes.Get(dub.XID, &box)
1525 if ok && box.Shared != "" {
1526 rcpts["%"+box.Shared] = true
1527 } else {
1528 rcpts[dub.XID] = true
1529 }
1530 }
1531 msg := j.ToBytes()
1532 for a := range rcpts {
1533 go deliverate(0, user.ID, a, msg, false)
1534 }
1535 }
1536}
1537
1538func junkuser(user *WhatAbout) junk.Junk {
1539 j := junk.New()
1540 j["@context"] = itiswhatitis
1541 j["id"] = user.URL
1542 j["inbox"] = user.URL + "/inbox"
1543 j["outbox"] = user.URL + "/outbox"
1544 j["name"] = user.Display
1545 j["preferredUsername"] = user.Name
1546 j["summary"] = user.HTAbout
1547 var tags []junk.Junk
1548 for _, o := range user.Onts {
1549 t := junk.New()
1550 t["type"] = "Hashtag"
1551 o = strings.ToLower(o)
1552 t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1553 t["name"] = o
1554 tags = append(tags, t)
1555 }
1556 if len(tags) > 0 {
1557 j["tag"] = tags
1558 }
1559
1560 if user.ID > 0 {
1561 j["type"] = "Person"
1562 j["url"] = user.URL
1563 j["followers"] = user.URL + "/followers"
1564 j["following"] = user.URL + "/following"
1565 a := junk.New()
1566 a["type"] = "Image"
1567 a["mediaType"] = "image/png"
1568 if ava := user.Options.Avatar; ava != "" {
1569 a["url"] = ava
1570 } else {
1571 u := fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1572 a["url"] = u
1573 }
1574 j["icon"] = a
1575 if ban := user.Options.Banner; ban != "" {
1576 a := junk.New()
1577 a["type"] = "Image"
1578 a["mediaType"] = "image/jpg"
1579 a["url"] = ban
1580 j["image"] = a
1581 }
1582 } else {
1583 j["type"] = "Service"
1584 }
1585 k := junk.New()
1586 k["id"] = user.URL + "#key"
1587 k["owner"] = user.URL
1588 k["publicKeyPem"] = user.Key
1589 j["publicKey"] = k
1590
1591 return j
1592}
1593
1594var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1595 user, err := butwhatabout(name)
1596 if err != nil {
1597 return nil, false
1598 }
1599 var buf bytes.Buffer
1600 j := junkuser(user)
1601 j.Write(&buf)
1602 return buf.Bytes(), true
1603}, Duration: 1 * time.Minute})
1604
1605func asjonker(name string) ([]byte, bool) {
1606 var j []byte
1607 ok := oldjonkers.Get(name, &j)
1608 return j, ok
1609}
1610
1611var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1612 m := strings.Split(name, "@")
1613 if len(m) != 2 {
1614 dlog.Printf("bad fish name: %s", name)
1615 return "", true
1616 }
1617 var href string
1618 row := stmtGetXonker.QueryRow(name, "fishname")
1619 err := row.Scan(&href)
1620 if err == nil {
1621 return href, true
1622 }
1623 dlog.Printf("fishing for %s", name)
1624 j, err := GetJunkFast(readyLuserOne, fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1625 if err != nil {
1626 ilog.Printf("failed to go fish %s: %s", name, err)
1627 return "", true
1628 }
1629 links, _ := j.GetArray("links")
1630 for _, li := range links {
1631 l, ok := li.(junk.Junk)
1632 if !ok {
1633 continue
1634 }
1635 href, _ := l.GetString("href")
1636 rel, _ := l.GetString("rel")
1637 t, _ := l.GetString("type")
1638 if rel == "self" && friendorfoe(t) {
1639 when := time.Now().UTC().Format(dbtimeformat)
1640 _, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1641 if err != nil {
1642 elog.Printf("error saving fishname: %s", err)
1643 }
1644 return href, true
1645 }
1646 }
1647 return href, true
1648}, Duration: 1 * time.Minute})
1649
1650func gofish(name string) string {
1651 if name[0] == '@' {
1652 name = name[1:]
1653 }
1654 var href string
1655 handfull.Get(name, &href)
1656 return href
1657}
1658
1659func investigate(name string) (*SomeThing, error) {
1660 if name == "" {
1661 return nil, fmt.Errorf("no name")
1662 }
1663 if name[0] == '@' {
1664 name = gofish(name)
1665 }
1666 if name == "" {
1667 return nil, fmt.Errorf("no name")
1668 }
1669 obj, err := GetJunkFast(readyLuserOne, name)
1670 if err != nil {
1671 return nil, err
1672 }
1673 allinjest(originate(name), obj)
1674 return somethingabout(obj)
1675}
1676
1677func somethingabout(obj junk.Junk) (*SomeThing, error) {
1678 info := new(SomeThing)
1679 t, _ := obj.GetString("type")
1680 switch t {
1681 case "Person":
1682 fallthrough
1683 case "Organization":
1684 fallthrough
1685 case "Application":
1686 fallthrough
1687 case "Service":
1688 info.What = SomeActor
1689 case "OrderedCollection":
1690 fallthrough
1691 case "Collection":
1692 info.What = SomeCollection
1693 default:
1694 return nil, fmt.Errorf("unknown object type")
1695 }
1696 info.XID, _ = obj.GetString("id")
1697 info.Name, _ = obj.GetString("preferredUsername")
1698 if info.Name == "" {
1699 info.Name, _ = obj.GetString("name")
1700 }
1701 info.Owner, _ = obj.GetString("attributedTo")
1702 if info.Owner == "" {
1703 info.Owner = info.XID
1704 }
1705 return info, nil
1706}
1707
1708func allinjest(origin string, obj junk.Junk) {
1709 keyobj, ok := obj.GetMap("publicKey")
1710 if ok {
1711 ingestpubkey(origin, keyobj)
1712 }
1713 ingestboxes(origin, obj)
1714 ingesthandle(origin, obj)
1715}
1716
1717func ingestpubkey(origin string, obj junk.Junk) {
1718 keyobj, ok := obj.GetMap("publicKey")
1719 if ok {
1720 obj = keyobj
1721 }
1722 keyname, ok := obj.GetString("id")
1723 var data string
1724 row := stmtGetXonker.QueryRow(keyname, "pubkey")
1725 err := row.Scan(&data)
1726 if err == nil {
1727 return
1728 }
1729 if !ok || origin != originate(keyname) {
1730 ilog.Printf("bad key origin %s <> %s", origin, keyname)
1731 return
1732 }
1733 dlog.Printf("ingesting a needed pubkey: %s", keyname)
1734 owner, ok := obj.GetString("owner")
1735 if !ok {
1736 ilog.Printf("error finding %s pubkey owner", keyname)
1737 return
1738 }
1739 data, ok = obj.GetString("publicKeyPem")
1740 if !ok {
1741 ilog.Printf("error finding %s pubkey", keyname)
1742 return
1743 }
1744 if originate(owner) != origin {
1745 ilog.Printf("bad key owner: %s <> %s", owner, origin)
1746 return
1747 }
1748 _, _, err = httpsig.DecodeKey(data)
1749 if err != nil {
1750 ilog.Printf("error decoding %s pubkey: %s", keyname, err)
1751 return
1752 }
1753 when := time.Now().UTC().Format(dbtimeformat)
1754 _, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1755 if err != nil {
1756 elog.Printf("error saving key: %s", err)
1757 }
1758}
1759
1760func ingestboxes(origin string, obj junk.Junk) {
1761 ident, _ := obj.GetString("id")
1762 if ident == "" {
1763 return
1764 }
1765 if originate(ident) != origin {
1766 return
1767 }
1768 var info string
1769 row := stmtGetXonker.QueryRow(ident, "boxes")
1770 err := row.Scan(&info)
1771 if err == nil {
1772 return
1773 }
1774 dlog.Printf("ingesting boxes: %s", ident)
1775 inbox, _ := obj.GetString("inbox")
1776 outbox, _ := obj.GetString("outbox")
1777 sbox, _ := obj.GetString("endpoints", "sharedInbox")
1778 if inbox != "" {
1779 when := time.Now().UTC().Format(dbtimeformat)
1780 m := strings.Join([]string{inbox, outbox, sbox}, " ")
1781 _, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1782 if err != nil {
1783 elog.Printf("error saving boxes: %s", err)
1784 }
1785 }
1786}
1787
1788func ingesthandle(origin string, obj junk.Junk) {
1789 xid, _ := obj.GetString("id")
1790 if xid == "" {
1791 return
1792 }
1793 if originate(xid) != origin {
1794 return
1795 }
1796 var handle string
1797 row := stmtGetXonker.QueryRow(xid, "handle")
1798 err := row.Scan(&handle)
1799 if err == nil {
1800 return
1801 }
1802 handle, _ = obj.GetString("preferredUsername")
1803 if handle != "" {
1804 when := time.Now().UTC().Format(dbtimeformat)
1805 _, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1806 if err != nil {
1807 elog.Printf("error saving handle: %s", err)
1808 }
1809 }
1810}
1811
1812func updateMe(username string) {
1813 var user *WhatAbout
1814 somenamedusers.Get(username, &user)
1815 dt := time.Now().UTC().Format(time.RFC3339)
1816 j := junk.New()
1817 j["@context"] = itiswhatitis
1818 j["id"] = fmt.Sprintf("%s/upme/%s/%d", user.URL, user.Name, time.Now().Unix())
1819 j["actor"] = user.URL
1820 j["published"] = dt
1821 j["to"] = thewholeworld
1822 j["type"] = "Update"
1823 j["object"] = junkuser(user)
1824
1825 msg := j.ToBytes()
1826
1827 rcpts := make(map[string]bool)
1828 for _, f := range getdubs(user.ID) {
1829 if f.XID == user.URL {
1830 continue
1831 }
1832 var box *Box
1833 boxofboxes.Get(f.XID, &box)
1834 if box != nil && box.Shared != "" {
1835 rcpts["%"+box.Shared] = true
1836 } else {
1837 rcpts[f.XID] = true
1838 }
1839 }
1840 for a := range rcpts {
1841 go deliverate(0, user.ID, a, msg, false)
1842 }
1843}
1844
1845func followme(user *WhatAbout, who string, name string, j junk.Junk) {
1846 folxid, _ := j.GetString("id")
1847
1848 ilog.Printf("updating honker follow: %s %s", who, folxid)
1849
1850 var x string
1851 db := opendatabase()
1852 row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", name, who, user.ID)
1853 err := row.Scan(&x)
1854 if err != sql.ErrNoRows {
1855 ilog.Printf("duplicate follow request: %s", who)
1856 _, err = stmtUpdateFlavor.Exec("dub", folxid, user.ID, name, who, "undub")
1857 if err != nil {
1858 elog.Printf("error updating honker: %s", err)
1859 }
1860 } else {
1861 stmtSaveDub.Exec(user.ID, name, who, "dub", folxid)
1862 }
1863 go rubadubdub(user, j)
1864}
1865
1866func unfollowme(user *WhatAbout, who string, name string, j junk.Junk) {
1867 var folxid string
1868 if who == "" {
1869 folxid, _ = j.GetString("object")
1870
1871 db := opendatabase()
1872 row := db.QueryRow("select xid, name from honkers where userid = ? and folxid = ? and flavor in ('dub', 'undub')", user.ID, folxid)
1873 err := row.Scan(&who, &name)
1874 if err != nil {
1875 if err != sql.ErrNoRows {
1876 elog.Printf("error scanning honker: %s", err)
1877 }
1878 return
1879 }
1880 }
1881
1882 ilog.Printf("updating honker undo: %s %s", who, folxid)
1883 _, err := stmtUpdateFlavor.Exec("undub", folxid, user.ID, name, who, "dub")
1884 if err != nil {
1885 elog.Printf("error updating honker: %s", err)
1886 return
1887 }
1888}
1889
1890func followyou(user *WhatAbout, honkerid int64, sync bool) {
1891 var url, owner string
1892 db := opendatabase()
1893 row := db.QueryRow("select xid, owner from honkers where honkerid = ? and userid = ? and flavor in ('unsub', 'peep', 'presub', 'sub')",
1894 honkerid, user.ID)
1895 err := row.Scan(&url, &owner)
1896 if err != nil {
1897 elog.Printf("can't get honker xid: %s", err)
1898 return
1899 }
1900 folxid := xfiltrate()
1901 ilog.Printf("subscribing to %s", url)
1902 _, err = db.Exec("update honkers set flavor = ?, folxid = ? where honkerid = ?", "presub", folxid, honkerid)
1903 if err != nil {
1904 elog.Printf("error updating honker: %s", err)
1905 return
1906 }
1907 if sync {
1908 subsub(user, url, owner, folxid)
1909 } else {
1910 go subsub(user, url, owner, folxid)
1911 }
1912
1913}
1914func unfollowyou(user *WhatAbout, honkerid int64, sync bool) {
1915 db := opendatabase()
1916 row := db.QueryRow("select xid, owner, folxid from honkers where honkerid = ? and userid = ? and flavor in ('sub')",
1917 honkerid, user.ID)
1918 var url, owner, folxid string
1919 err := row.Scan(&url, &owner, &folxid)
1920 if err != nil {
1921 elog.Printf("can't get honker xid: %s", err)
1922 return
1923 }
1924 ilog.Printf("unsubscribing from %s", url)
1925 _, err = db.Exec("update honkers set flavor = ? where honkerid = ?", "unsub", honkerid)
1926 if err != nil {
1927 elog.Printf("error updating honker: %s", err)
1928 return
1929 }
1930 if sync {
1931 itakeitallback(user, url, owner, folxid)
1932 } else {
1933 go itakeitallback(user, url, owner, folxid)
1934 }
1935}
1936
1937func followyou2(user *WhatAbout, j junk.Junk) {
1938 who, _ := j.GetString("actor")
1939
1940 ilog.Printf("updating honker accept: %s", who)
1941 db := opendatabase()
1942 row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
1943 user.ID, who)
1944 var name, folxid string
1945 err := row.Scan(&name, &folxid)
1946 if err != nil {
1947 elog.Printf("can't get honker name: %s", err)
1948 return
1949 }
1950 _, err = stmtUpdateFlavor.Exec("sub", folxid, user.ID, name, who, "presub")
1951 if err != nil {
1952 elog.Printf("error updating honker: %s", err)
1953 return
1954 }
1955}
1956
1957func nofollowyou2(user *WhatAbout, j junk.Junk) {
1958 who, _ := j.GetString("actor")
1959
1960 ilog.Printf("updating honker reject: %s", who)
1961 db := opendatabase()
1962 row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
1963 user.ID, who)
1964 var name, folxid string
1965 err := row.Scan(&name, &folxid)
1966 if err != nil {
1967 elog.Printf("can't get honker name: %s", err)
1968 return
1969 }
1970 _, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "presub")
1971 _, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "sub")
1972 if err != nil {
1973 elog.Printf("error updating honker: %s", err)
1974 return
1975 }
1976}