2019-04-09 13:59:33 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rsa"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2019-08-02 16:45:42 +02:00
|
|
|
notrand "math/rand"
|
2019-04-09 13:59:33 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-05-05 00:05:15 +02:00
|
|
|
|
2019-07-29 01:44:27 +02:00
|
|
|
"humungus.tedunangst.com/r/webs/httpsig"
|
2019-05-05 00:05:15 +02:00
|
|
|
"humungus.tedunangst.com/r/webs/image"
|
2019-06-11 16:14:12 +02:00
|
|
|
"humungus.tedunangst.com/r/webs/junk"
|
2019-04-09 13:59:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
|
2019-04-23 21:58:13 +02:00
|
|
|
var thefakename = `application/activity+json`
|
2019-04-09 13:59:33 +02:00
|
|
|
var falsenames = []string{
|
|
|
|
`application/ld+json`,
|
|
|
|
`application/activity+json`,
|
|
|
|
}
|
|
|
|
var itiswhatitis = "https://www.w3.org/ns/activitystreams"
|
|
|
|
var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
|
|
|
func friendorfoe(ct string) bool {
|
|
|
|
ct = strings.ToLower(ct)
|
|
|
|
for _, at := range falsenames {
|
|
|
|
if strings.HasPrefix(ct, at) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func PostJunk(keyname string, key *rsa.PrivateKey, url string, j junk.Junk) error {
|
2019-04-09 13:59:33 +02:00
|
|
|
var buf bytes.Buffer
|
2019-06-11 16:14:12 +02:00
|
|
|
j.Write(&buf)
|
2019-04-14 16:06:26 +02:00
|
|
|
return PostMsg(keyname, key, url, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error {
|
|
|
|
client := http.DefaultClient
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-16 02:49:01 +02:00
|
|
|
req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
|
2019-04-09 13:59:33 +02:00
|
|
|
req.Header.Set("Content-Type", theonetruename)
|
2019-07-29 01:44:27 +02:00
|
|
|
httpsig.SignRequest(keyname, key, req, msg)
|
2019-04-09 13:59:33 +02:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-18 22:09:10 +02:00
|
|
|
resp.Body.Close()
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case 200:
|
|
|
|
case 201:
|
|
|
|
case 202:
|
|
|
|
default:
|
2019-04-09 13:59:33 +02:00
|
|
|
return fmt.Errorf("http post status: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
log.Printf("successful post: %s %d", url, resp.StatusCode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func GetJunk(url string) (junk.Junk, error) {
|
2019-08-14 19:15:11 +02:00
|
|
|
return GetJunkTimeout(url, 30*time.Second)
|
2019-07-11 07:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetJunkFast(url string) (junk.Junk, error) {
|
|
|
|
return GetJunkTimeout(url, 5*time.Second)
|
|
|
|
}
|
|
|
|
|
2019-08-14 19:15:11 +02:00
|
|
|
func GetJunkHardMode(url string) (junk.Junk, error) {
|
|
|
|
j, err := GetJunk(url)
|
|
|
|
if err != nil {
|
|
|
|
emsg := err.Error()
|
|
|
|
if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
|
|
|
|
log.Printf("trying again after error: %s", emsg)
|
|
|
|
time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
|
|
|
|
j, err = GetJunk(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("still couldn't get it")
|
|
|
|
} else {
|
|
|
|
log.Printf("retry success!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return j, err
|
|
|
|
}
|
|
|
|
|
2019-07-11 07:20:53 +02:00
|
|
|
func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
|
2019-04-23 21:58:13 +02:00
|
|
|
at := thefakename
|
2019-04-15 16:27:36 +02:00
|
|
|
if strings.Contains(url, ".well-known/webfinger?resource") {
|
|
|
|
at = "application/jrd+json"
|
|
|
|
}
|
2019-07-29 01:54:39 +02:00
|
|
|
return junk.Get(url, junk.GetArgs{
|
|
|
|
Accept: at,
|
|
|
|
Agent: "honksnonk/5.0; " + serverName,
|
|
|
|
Timeout: timeout,
|
|
|
|
})
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 10:23:36 +02:00
|
|
|
func savedonk(url string, name, media string, localize bool) *Donk {
|
|
|
|
if url == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
var donk Donk
|
|
|
|
row := stmtFindFile.QueryRow(url)
|
|
|
|
err := row.Scan(&donk.FileID)
|
|
|
|
if err == nil {
|
|
|
|
return &donk
|
|
|
|
}
|
2019-04-15 04:53:26 +02:00
|
|
|
log.Printf("saving donk: %s", url)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2019-04-15 04:53:26 +02:00
|
|
|
log.Printf("error querying: %s", err)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
xid := xfiltrate()
|
2019-05-28 10:23:36 +02:00
|
|
|
data := []byte{}
|
|
|
|
if localize {
|
|
|
|
resp, err := http.Get(url)
|
2019-04-26 23:01:17 +02:00
|
|
|
if err != nil {
|
2019-05-28 10:23:36 +02:00
|
|
|
log.Printf("error fetching %s: %s", url, err)
|
|
|
|
localize = false
|
|
|
|
goto saveit
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
localize = false
|
|
|
|
goto saveit
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
|
|
|
|
data = buf.Bytes()
|
|
|
|
if strings.HasPrefix(media, "image") {
|
2019-08-07 18:01:57 +02:00
|
|
|
img, err := image.Vacuum(&buf,
|
|
|
|
image.Params{LimitSize: 4800 * 4800, MaxWidth: 2048, MaxHeight: 2048})
|
2019-05-28 10:23:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to decode image: %s", err)
|
|
|
|
localize = false
|
2019-07-05 00:46:15 +02:00
|
|
|
data = []byte{}
|
2019-05-28 10:23:36 +02:00
|
|
|
goto saveit
|
|
|
|
}
|
|
|
|
data = img.Data
|
|
|
|
media = "image/" + img.Format
|
2019-07-05 00:47:45 +02:00
|
|
|
} else if len(data) > 100000 {
|
|
|
|
log.Printf("not saving large attachment")
|
|
|
|
localize = false
|
|
|
|
data = []byte{}
|
2019-04-26 23:01:17 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 10:23:36 +02:00
|
|
|
saveit:
|
|
|
|
res, err := stmtSaveFile.Exec(xid, name, url, media, localize, data)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving file %s: %s", url, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
donk.FileID, _ = res.LastInsertId()
|
|
|
|
return &donk
|
|
|
|
}
|
|
|
|
|
2019-06-17 18:18:37 +02:00
|
|
|
func iszonked(userid int64, xid string) bool {
|
|
|
|
row := stmtFindZonk.QueryRow(userid, xid)
|
|
|
|
var id int64
|
|
|
|
err := row.Scan(&id)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.Printf("err querying zonk: %s", err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:15:47 +02:00
|
|
|
func needxonk(user *WhatAbout, x *Honk) bool {
|
2019-04-14 19:38:05 +02:00
|
|
|
if x.What == "eradicate" {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-26 21:11:24 +02:00
|
|
|
if thoudostbitethythumb(user.ID, x.Audience, x.XID) {
|
2019-04-30 21:51:09 +02:00
|
|
|
log.Printf("not saving thumb biter? %s via %s", x.XID, x.Honker)
|
2019-04-26 15:35:22 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-04-25 07:18:39 +02:00
|
|
|
return needxonkid(user, x.XID)
|
|
|
|
}
|
|
|
|
func needxonkid(user *WhatAbout, xid string) bool {
|
2019-07-23 19:21:09 +02:00
|
|
|
if strings.HasPrefix(xid, user.URL+"/") {
|
2019-04-25 07:18:39 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-06-17 18:18:37 +02:00
|
|
|
if iszonked(user.ID, xid) {
|
|
|
|
log.Printf("already zonked: %s", xid)
|
|
|
|
return false
|
|
|
|
}
|
2019-04-25 07:18:39 +02:00
|
|
|
row := stmtFindXonk.QueryRow(user.ID, xid)
|
|
|
|
var id int64
|
|
|
|
err := row.Scan(&id)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.Printf("err querying xonk: %s", err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-20 17:17:00 +02:00
|
|
|
func savexonk(user *WhatAbout, x *Honk) {
|
2019-04-09 13:59:33 +02:00
|
|
|
if x.What == "eradicate" {
|
2019-05-22 01:32:43 +02:00
|
|
|
log.Printf("eradicating %s by %s", x.XID, x.Honker)
|
|
|
|
xonk := getxonk(user.ID, x.XID)
|
2019-05-05 19:52:46 +02:00
|
|
|
if xonk != nil {
|
2019-08-16 06:15:55 +02:00
|
|
|
_, err := stmtZonkDonks.Exec(xonk.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error eradicating: %s", err)
|
|
|
|
}
|
|
|
|
_, err = stmtZonkIt.Exec(user.ID, x.XID)
|
2019-05-05 19:52:46 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error eradicating: %s", err)
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-08-16 06:15:55 +02:00
|
|
|
_, err := stmtSaveZonker.Exec(user.ID, x.XID, "zonk")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error eradicating: %s", err)
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-20 16:53:16 +02:00
|
|
|
log.Printf("saving xonk: %s", x.XID)
|
2019-04-09 13:59:33 +02:00
|
|
|
dt := x.Date.UTC().Format(dbtimeformat)
|
|
|
|
aud := strings.Join(x.Audience, " ")
|
2019-04-20 17:17:00 +02:00
|
|
|
whofore := 0
|
|
|
|
if strings.Contains(aud, user.URL) {
|
|
|
|
whofore = 1
|
|
|
|
}
|
2019-08-19 00:32:44 +02:00
|
|
|
go prehandle(x.Honker)
|
|
|
|
go prehandle(x.Oonker)
|
2019-04-20 17:17:00 +02:00
|
|
|
res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
|
2019-08-25 05:11:53 +02:00
|
|
|
x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker, 0, strings.Join(x.Onts, " "))
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err saving xonk: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
x.ID, _ = res.LastInsertId()
|
|
|
|
for _, d := range x.Donks {
|
|
|
|
_, err = stmtSaveDonk.Exec(x.ID, d.FileID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err saving donk: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 05:11:53 +02:00
|
|
|
for _, o := range x.Onts {
|
|
|
|
_, err = stmtSaveOnts.Exec(strings.ToLower(o), x.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving ont: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 20:40:23 +02:00
|
|
|
type Box struct {
|
2019-04-16 20:47:08 +02:00
|
|
|
In string
|
|
|
|
Out string
|
2019-04-16 20:40:23 +02:00
|
|
|
Shared string
|
|
|
|
}
|
|
|
|
|
|
|
|
var boxofboxes = make(map[string]*Box)
|
2019-04-09 13:59:33 +02:00
|
|
|
var boxlock sync.Mutex
|
2019-04-17 02:20:42 +02:00
|
|
|
var boxinglock sync.Mutex
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-04-16 20:40:23 +02:00
|
|
|
func getboxes(ident string) (*Box, error) {
|
2019-04-09 13:59:33 +02:00
|
|
|
boxlock.Lock()
|
|
|
|
b, ok := boxofboxes[ident]
|
2019-04-14 19:23:27 +02:00
|
|
|
boxlock.Unlock()
|
2019-04-09 13:59:33 +02:00
|
|
|
if ok {
|
2019-04-16 20:40:23 +02:00
|
|
|
return b, nil
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-17 02:20:42 +02:00
|
|
|
|
|
|
|
boxinglock.Lock()
|
|
|
|
defer boxinglock.Unlock()
|
|
|
|
|
|
|
|
boxlock.Lock()
|
|
|
|
b, ok = boxofboxes[ident]
|
|
|
|
boxlock.Unlock()
|
|
|
|
if ok {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-06-03 06:15:06 +02:00
|
|
|
var info string
|
|
|
|
row := stmtGetXonker.QueryRow(ident, "boxes")
|
|
|
|
err := row.Scan(&info)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
2019-04-20 17:38:16 +02:00
|
|
|
j, err := GetJunk(ident)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
inbox, _ := j.GetString("inbox")
|
|
|
|
outbox, _ := j.GetString("outbox")
|
|
|
|
sbox, _ := j.FindString([]string{"endpoints", "sharedInbox"})
|
2019-04-20 17:38:16 +02:00
|
|
|
b = &Box{In: inbox, Out: outbox, Shared: sbox}
|
|
|
|
if inbox != "" {
|
2019-06-03 06:15:06 +02:00
|
|
|
m := strings.Join([]string{inbox, outbox, sbox}, " ")
|
|
|
|
_, err = stmtSaveXonker.Exec(ident, m, "boxes")
|
2019-04-29 04:48:46 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving boxes: %s", err)
|
|
|
|
}
|
2019-04-20 17:38:16 +02:00
|
|
|
}
|
2019-06-03 06:15:06 +02:00
|
|
|
} else {
|
|
|
|
m := strings.Split(info, " ")
|
|
|
|
b = &Box{In: m[0], Out: m[1], Shared: m[2]}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-06-03 06:15:06 +02:00
|
|
|
|
2019-04-14 19:23:27 +02:00
|
|
|
boxlock.Lock()
|
2019-04-16 20:40:23 +02:00
|
|
|
boxofboxes[ident] = b
|
2019-04-14 19:23:27 +02:00
|
|
|
boxlock.Unlock()
|
2019-04-16 20:40:23 +02:00
|
|
|
return b, nil
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 04:36:52 +02:00
|
|
|
func gimmexonks(user *WhatAbout, outbox string) {
|
|
|
|
log.Printf("getting outbox: %s", outbox)
|
|
|
|
j, err := GetJunk(outbox)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting outbox: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t, _ := j.GetString("type")
|
|
|
|
origin := originate(outbox)
|
|
|
|
if t == "OrderedCollection" {
|
|
|
|
items, _ := j.GetArray("orderedItems")
|
|
|
|
if items == nil {
|
|
|
|
obj, ok := j.GetMap("first")
|
|
|
|
if ok {
|
|
|
|
items, _ = obj.GetArray("orderedItems")
|
|
|
|
} else {
|
|
|
|
page1, _ := j.GetString("first")
|
|
|
|
j, err = GetJunk(page1)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error gettings page1: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
items, _ = j.GetArray("orderedItems")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(items) > 20 {
|
|
|
|
items = items[0:20]
|
|
|
|
}
|
|
|
|
for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
items[i], items[j] = items[j], items[i]
|
|
|
|
}
|
|
|
|
for _, item := range items {
|
|
|
|
obj, ok := item.(junk.Junk)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
xonk := xonkxonk(user, obj, origin)
|
|
|
|
if xonk != nil {
|
|
|
|
savexonk(user, xonk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:59:33 +02:00
|
|
|
func peeppeep() {
|
2019-04-19 17:50:35 +02:00
|
|
|
user, _ := butwhatabout("htest")
|
2019-04-09 13:59:33 +02:00
|
|
|
honkers := gethonkers(user.ID)
|
|
|
|
for _, f := range honkers {
|
|
|
|
if f.Flavor != "peep" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("getting updates: %s", f.XID)
|
2019-04-16 20:40:23 +02:00
|
|
|
box, err := getboxes(f.XID)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting outbox: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-06-17 04:36:52 +02:00
|
|
|
gimmexonks(user, box.Out)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-19 17:50:35 +02:00
|
|
|
func whosthere(xid string) ([]string, string) {
|
2019-04-11 16:44:50 +02:00
|
|
|
obj, err := GetJunk(xid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting remote xonk: %s", err)
|
2019-04-19 17:50:35 +02:00
|
|
|
return nil, ""
|
2019-04-11 16:44:50 +02:00
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
convoy, _ := obj.GetString("context")
|
2019-04-19 18:48:44 +02:00
|
|
|
if convoy == "" {
|
2019-06-11 16:14:12 +02:00
|
|
|
convoy, _ = obj.GetString("conversation")
|
2019-04-19 18:48:44 +02:00
|
|
|
}
|
2019-04-19 17:50:35 +02:00
|
|
|
return newphone(nil, obj), convoy
|
2019-04-11 16:44:50 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func newphone(a []string, obj junk.Junk) []string {
|
2019-04-09 13:59:33 +02:00
|
|
|
for _, addr := range []string{"to", "cc", "attributedTo"} {
|
2019-06-11 16:14:12 +02:00
|
|
|
who, _ := obj.GetString(addr)
|
2019-04-09 13:59:33 +02:00
|
|
|
if who != "" {
|
|
|
|
a = append(a, who)
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
whos, _ := obj.GetArray(addr)
|
2019-04-09 13:59:33 +02:00
|
|
|
for _, w := range whos {
|
|
|
|
who, _ := w.(string)
|
|
|
|
if who != "" {
|
|
|
|
a = append(a, who)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:43:38 +02:00
|
|
|
func extractattrto(obj junk.Junk) string {
|
|
|
|
who, _ := obj.GetString("attributedTo")
|
|
|
|
if who != "" {
|
|
|
|
return who
|
|
|
|
}
|
2019-07-21 04:45:43 +02:00
|
|
|
o, ok := obj.GetMap("attributedTo")
|
|
|
|
if ok {
|
|
|
|
id, ok := o.GetString("id")
|
|
|
|
if ok {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 16:43:38 +02:00
|
|
|
arr, _ := obj.GetArray("attributedTo")
|
|
|
|
for _, a := range arr {
|
|
|
|
o, ok := a.(junk.Junk)
|
|
|
|
if ok {
|
|
|
|
t, _ := o.GetString("type")
|
|
|
|
id, _ := o.GetString("id")
|
|
|
|
if t == "Person" || t == "" {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func consumeactivity(user *WhatAbout, j junk.Junk, origin string) {
|
2019-05-22 00:51:04 +02:00
|
|
|
xonk := xonkxonk(user, j, origin)
|
|
|
|
if xonk != nil {
|
|
|
|
savexonk(user, xonk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
|
2019-04-25 19:23:18 +02:00
|
|
|
depth := 0
|
2019-07-29 20:24:45 +02:00
|
|
|
maxdepth := 10
|
2019-06-07 17:15:46 +02:00
|
|
|
currenttid := ""
|
2019-06-11 16:14:12 +02:00
|
|
|
var xonkxonkfn func(item junk.Junk, origin string) *Honk
|
2019-04-25 19:23:18 +02:00
|
|
|
|
|
|
|
saveoneup := func(xid string) {
|
|
|
|
log.Printf("getting oneup: %s", xid)
|
|
|
|
if depth >= maxdepth {
|
|
|
|
log.Printf("in too deep")
|
|
|
|
return
|
|
|
|
}
|
2019-08-14 19:15:11 +02:00
|
|
|
obj, err := GetJunkHardMode(xid)
|
2019-04-25 19:23:18 +02:00
|
|
|
if err != nil {
|
2019-08-22 17:06:31 +02:00
|
|
|
log.Printf("error getting oneup: %s: %s", xid, err)
|
2019-04-25 19:23:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
depth++
|
2019-05-21 19:56:15 +02:00
|
|
|
xonk := xonkxonkfn(obj, originate(xid))
|
2019-04-29 20:06:34 +02:00
|
|
|
if xonk != nil {
|
2019-04-25 19:23:18 +02:00
|
|
|
savexonk(user, xonk)
|
|
|
|
}
|
|
|
|
depth--
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
xonkxonkfn = func(item junk.Junk, origin string) *Honk {
|
|
|
|
// id, _ := item.GetString( "id")
|
|
|
|
what, _ := item.GetString("type")
|
|
|
|
dt, _ := item.GetString("published")
|
2019-04-25 19:23:18 +02:00
|
|
|
|
|
|
|
var audience []string
|
|
|
|
var err error
|
2019-05-15 22:27:50 +02:00
|
|
|
var xid, rid, url, content, precis, convoy, oonker string
|
2019-06-11 16:14:12 +02:00
|
|
|
var obj junk.Junk
|
2019-04-25 19:23:18 +02:00
|
|
|
var ok bool
|
|
|
|
switch what {
|
|
|
|
case "Announce":
|
2019-06-11 16:14:12 +02:00
|
|
|
obj, ok = item.GetMap("object")
|
2019-04-25 19:23:18 +02:00
|
|
|
if ok {
|
2019-06-11 16:14:12 +02:00
|
|
|
xid, _ = obj.GetString("id")
|
2019-04-25 19:23:18 +02:00
|
|
|
} else {
|
2019-06-11 16:14:12 +02:00
|
|
|
xid, _ = item.GetString("object")
|
2019-05-20 16:51:15 +02:00
|
|
|
}
|
|
|
|
if !needxonkid(user, xid) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("getting bonk: %s", xid)
|
2019-08-14 19:15:11 +02:00
|
|
|
obj, err = GetJunkHardMode(xid)
|
2019-08-22 17:06:31 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting bonk: %s: %s", xid, err)
|
|
|
|
}
|
2019-05-21 19:56:15 +02:00
|
|
|
origin = originate(xid)
|
2019-04-25 19:23:18 +02:00
|
|
|
what = "bonk"
|
|
|
|
case "Create":
|
2019-07-21 05:01:04 +02:00
|
|
|
obj, ok = item.GetMap("object")
|
|
|
|
if !ok {
|
|
|
|
xid, _ = obj.GetString("object")
|
|
|
|
log.Printf("getting created honk: %s", xid)
|
2019-08-14 19:15:11 +02:00
|
|
|
obj, err = GetJunkHardMode(xid)
|
2019-07-21 05:01:04 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting creation: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
what = "honk"
|
|
|
|
case "Delete":
|
2019-06-11 16:14:12 +02:00
|
|
|
obj, _ = item.GetMap("object")
|
|
|
|
xid, _ = item.GetString("object")
|
2019-04-25 19:23:18 +02:00
|
|
|
what = "eradicate"
|
2019-08-21 07:23:00 +02:00
|
|
|
case "Read":
|
|
|
|
xid, ok = item.GetString("object")
|
|
|
|
if ok {
|
|
|
|
if !needxonkid(user, xid) {
|
|
|
|
log.Printf("don't need read obj: %s", xid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
obj, err = GetJunkHardMode(xid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting read: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return xonkxonkfn(obj, originate(xid))
|
|
|
|
}
|
|
|
|
return nil
|
2019-07-01 16:43:38 +02:00
|
|
|
case "Video":
|
|
|
|
fallthrough
|
2019-06-12 05:03:53 +02:00
|
|
|
case "Question":
|
|
|
|
fallthrough
|
2019-05-13 15:36:28 +02:00
|
|
|
case "Note":
|
|
|
|
fallthrough
|
|
|
|
case "Article":
|
|
|
|
fallthrough
|
|
|
|
case "Page":
|
2019-06-11 16:14:12 +02:00
|
|
|
obj = item
|
2019-05-13 15:36:28 +02:00
|
|
|
what = "honk"
|
2019-04-25 19:23:18 +02:00
|
|
|
default:
|
|
|
|
log.Printf("unknown activity: %s", what)
|
2019-05-08 02:55:11 +02:00
|
|
|
fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
2019-06-11 16:14:12 +02:00
|
|
|
item.Write(fd)
|
2019-05-08 02:55:11 +02:00
|
|
|
io.WriteString(fd, "\n")
|
|
|
|
fd.Close()
|
2019-04-25 19:23:18 +02:00
|
|
|
return nil
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
|
|
|
|
var xonk Honk
|
2019-06-11 16:14:12 +02:00
|
|
|
who, _ := item.GetString("actor")
|
2019-04-25 19:23:18 +02:00
|
|
|
if obj != nil {
|
|
|
|
if who == "" {
|
2019-07-01 16:43:38 +02:00
|
|
|
who = extractattrto(obj)
|
2019-04-22 21:05:44 +02:00
|
|
|
}
|
2019-07-01 16:43:38 +02:00
|
|
|
oonker = extractattrto(obj)
|
2019-06-11 16:14:12 +02:00
|
|
|
ot, _ := obj.GetString("type")
|
|
|
|
url, _ = obj.GetString("url")
|
2019-07-01 16:43:38 +02:00
|
|
|
if ot == "Tombstone" {
|
|
|
|
xid, _ = obj.GetString("id")
|
|
|
|
} else {
|
2019-04-25 19:23:18 +02:00
|
|
|
audience = newphone(audience, obj)
|
2019-06-11 16:14:12 +02:00
|
|
|
xid, _ = obj.GetString("id")
|
|
|
|
precis, _ = obj.GetString("summary")
|
2019-07-16 09:35:04 +02:00
|
|
|
if precis == "" {
|
|
|
|
precis, _ = obj.GetString("name")
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
content, _ = obj.GetString("content")
|
2019-04-25 19:23:18 +02:00
|
|
|
if !strings.HasPrefix(content, "<p>") {
|
|
|
|
content = "<p>" + content
|
|
|
|
}
|
2019-07-10 20:46:43 +02:00
|
|
|
sens, _ := obj["sensitive"].(bool)
|
|
|
|
if sens && precis == "" {
|
|
|
|
precis = "unspecified horror"
|
|
|
|
}
|
2019-07-22 06:09:16 +02:00
|
|
|
rid, ok = obj.GetString("inReplyTo")
|
|
|
|
if !ok {
|
|
|
|
robj, ok := obj.GetMap("inReplyTo")
|
|
|
|
if ok {
|
|
|
|
rid, _ = robj.GetString("id")
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
convoy, _ = obj.GetString("context")
|
2019-04-25 19:23:18 +02:00
|
|
|
if convoy == "" {
|
2019-06-11 16:14:12 +02:00
|
|
|
convoy, _ = obj.GetString("conversation")
|
2019-04-25 19:23:18 +02:00
|
|
|
}
|
2019-06-12 05:03:53 +02:00
|
|
|
if ot == "Question" {
|
2019-07-06 19:25:25 +02:00
|
|
|
if what == "honk" {
|
|
|
|
what = "qonk"
|
|
|
|
}
|
2019-06-12 05:03:53 +02:00
|
|
|
content += "<ul>"
|
|
|
|
ans, _ := obj.GetArray("oneOf")
|
|
|
|
for _, ai := range ans {
|
|
|
|
a, ok := ai.(junk.Junk)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
as, _ := a.GetString("name")
|
|
|
|
content += "<li>" + as
|
|
|
|
}
|
|
|
|
ans, _ = obj.GetArray("anyOf")
|
|
|
|
for _, ai := range ans {
|
|
|
|
a, ok := ai.(junk.Junk)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
as, _ := a.GetString("name")
|
|
|
|
content += "<li>" + as
|
|
|
|
}
|
|
|
|
content += "</ul>"
|
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
if what == "honk" && rid != "" {
|
|
|
|
what = "tonk"
|
|
|
|
}
|
2019-04-13 19:21:32 +02:00
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
atts, _ := obj.GetArray("attachment")
|
|
|
|
for i, atti := range atts {
|
|
|
|
att, ok := atti.(junk.Junk)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
at, _ := att.GetString("type")
|
|
|
|
mt, _ := att.GetString("mediaType")
|
|
|
|
u, _ := att.GetString("url")
|
|
|
|
name, _ := att.GetString("name")
|
2019-05-28 10:23:36 +02:00
|
|
|
localize := false
|
2019-05-25 20:34:46 +02:00
|
|
|
if i > 4 {
|
|
|
|
log.Printf("excessive attachment: %s", at)
|
|
|
|
} else if at == "Document" || at == "Image" {
|
2019-04-25 19:23:18 +02:00
|
|
|
mt = strings.ToLower(mt)
|
|
|
|
log.Printf("attachment: %s %s", mt, u)
|
2019-06-27 22:28:08 +02:00
|
|
|
if mt == "text/plain" || strings.HasPrefix(mt, "image") {
|
2019-05-28 10:23:36 +02:00
|
|
|
localize = true
|
2019-04-25 19:23:18 +02:00
|
|
|
}
|
2019-04-29 04:28:59 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("unknown attachment: %s", at)
|
2019-04-25 19:23:18 +02:00
|
|
|
}
|
2019-05-28 10:23:36 +02:00
|
|
|
donk := savedonk(u, name, mt, localize)
|
|
|
|
if donk != nil {
|
|
|
|
xonk.Donks = append(xonk.Donks, donk)
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
tags, _ := obj.GetArray("tag")
|
|
|
|
for _, tagi := range tags {
|
|
|
|
tag, ok := tagi.(junk.Junk)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tt, _ := tag.GetString("type")
|
|
|
|
name, _ := tag.GetString("name")
|
2019-04-25 19:23:18 +02:00
|
|
|
if tt == "Emoji" {
|
2019-06-11 16:14:12 +02:00
|
|
|
icon, _ := tag.GetMap("icon")
|
|
|
|
mt, _ := icon.GetString("mediaType")
|
2019-04-25 20:18:33 +02:00
|
|
|
if mt == "" {
|
|
|
|
mt = "image/png"
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
u, _ := icon.GetString("url")
|
2019-05-28 10:23:36 +02:00
|
|
|
donk := savedonk(u, name, mt, true)
|
2019-04-09 13:59:33 +02:00
|
|
|
if donk != nil {
|
|
|
|
xonk.Donks = append(xonk.Donks, donk)
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 05:18:19 +02:00
|
|
|
if tt == "Hashtag" {
|
|
|
|
xonk.Onts = append(xonk.Onts, name)
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 19:56:15 +02:00
|
|
|
if originate(xid) != origin {
|
|
|
|
log.Printf("original sin: %s <> %s", xid, origin)
|
2019-06-11 16:14:12 +02:00
|
|
|
item.Write(os.Stdout)
|
2019-05-21 19:56:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
audience = append(audience, who)
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-04-25 19:23:18 +02:00
|
|
|
audience = oneofakind(audience)
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-06-07 17:15:46 +02:00
|
|
|
if currenttid == "" {
|
|
|
|
currenttid = convoy
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:27:50 +02:00
|
|
|
if oonker == who {
|
|
|
|
oonker = ""
|
|
|
|
}
|
2019-04-26 15:35:22 +02:00
|
|
|
xonk.UserID = user.ID
|
2019-04-25 19:23:18 +02:00
|
|
|
xonk.What = what
|
|
|
|
xonk.Honker = who
|
|
|
|
xonk.XID = xid
|
|
|
|
xonk.RID = rid
|
|
|
|
xonk.Date, _ = time.Parse(time.RFC3339, dt)
|
|
|
|
xonk.URL = url
|
|
|
|
xonk.Noise = content
|
2019-05-15 22:27:50 +02:00
|
|
|
xonk.Precis = precis
|
2019-04-25 19:23:18 +02:00
|
|
|
xonk.Audience = audience
|
2019-05-15 22:27:50 +02:00
|
|
|
xonk.Oonker = oonker
|
2019-04-25 19:23:18 +02:00
|
|
|
|
2019-04-29 20:06:34 +02:00
|
|
|
if needxonk(user, &xonk) {
|
2019-07-08 21:02:49 +02:00
|
|
|
if rid != "" {
|
2019-04-29 20:06:34 +02:00
|
|
|
if needxonkid(user, rid) {
|
|
|
|
saveoneup(rid)
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 19:31:11 +02:00
|
|
|
if convoy == "" {
|
|
|
|
convoy = currenttid
|
|
|
|
}
|
|
|
|
xonk.Convoy = convoy
|
2019-04-29 20:06:34 +02:00
|
|
|
return &xonk
|
|
|
|
}
|
|
|
|
return nil
|
2019-04-25 19:23:18 +02:00
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-05-21 19:56:15 +02:00
|
|
|
return xonkxonkfn(item, origin)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func rubadubdub(user *WhatAbout, req junk.Junk) {
|
|
|
|
xid, _ := req.GetString("id")
|
|
|
|
actor, _ := req.GetString("actor")
|
|
|
|
j := junk.New()
|
2019-04-09 13:59:33 +02:00
|
|
|
j["@context"] = itiswhatitis
|
2019-06-21 05:47:05 +02:00
|
|
|
j["id"] = user.URL + "/dub/" + url.QueryEscape(xid)
|
2019-04-09 13:59:33 +02:00
|
|
|
j["type"] = "Accept"
|
|
|
|
j["actor"] = user.URL
|
2019-06-11 05:57:03 +02:00
|
|
|
j["to"] = actor
|
2019-04-09 13:59:33 +02:00
|
|
|
j["published"] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
j["object"] = req
|
|
|
|
|
2019-06-11 05:57:03 +02:00
|
|
|
var buf bytes.Buffer
|
2019-06-11 16:14:12 +02:00
|
|
|
j.Write(&buf)
|
2019-06-11 05:57:03 +02:00
|
|
|
msg := buf.Bytes()
|
|
|
|
|
|
|
|
deliverate(0, user.Name, actor, msg)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 05:57:03 +02:00
|
|
|
func itakeitallback(user *WhatAbout, xid string) {
|
2019-06-11 16:14:12 +02:00
|
|
|
j := junk.New()
|
2019-04-26 21:31:42 +02:00
|
|
|
j["@context"] = itiswhatitis
|
2019-06-21 05:47:05 +02:00
|
|
|
j["id"] = user.URL + "/unsub/" + url.QueryEscape(xid)
|
2019-04-26 21:31:42 +02:00
|
|
|
j["type"] = "Undo"
|
|
|
|
j["actor"] = user.URL
|
|
|
|
j["to"] = xid
|
2019-06-11 16:14:12 +02:00
|
|
|
f := junk.New()
|
2019-06-21 05:47:05 +02:00
|
|
|
f["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
|
2019-04-26 21:31:42 +02:00
|
|
|
f["type"] = "Follow"
|
|
|
|
f["actor"] = user.URL
|
|
|
|
f["to"] = xid
|
2019-06-21 05:47:05 +02:00
|
|
|
f["object"] = xid
|
2019-04-26 21:31:42 +02:00
|
|
|
j["object"] = f
|
|
|
|
j["published"] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
|
2019-06-11 05:57:03 +02:00
|
|
|
var buf bytes.Buffer
|
2019-06-11 16:14:12 +02:00
|
|
|
j.Write(&buf)
|
2019-06-11 05:57:03 +02:00
|
|
|
msg := buf.Bytes()
|
|
|
|
|
|
|
|
deliverate(0, user.Name, xid, msg)
|
2019-04-26 21:31:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 13:59:33 +02:00
|
|
|
func subsub(user *WhatAbout, xid string) {
|
2019-06-11 16:14:12 +02:00
|
|
|
j := junk.New()
|
2019-04-09 13:59:33 +02:00
|
|
|
j["@context"] = itiswhatitis
|
2019-06-21 05:47:05 +02:00
|
|
|
j["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
|
2019-04-09 13:59:33 +02:00
|
|
|
j["type"] = "Follow"
|
|
|
|
j["actor"] = user.URL
|
|
|
|
j["to"] = xid
|
|
|
|
j["object"] = xid
|
|
|
|
j["published"] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
|
2019-06-11 05:51:04 +02:00
|
|
|
var buf bytes.Buffer
|
2019-06-11 16:14:12 +02:00
|
|
|
j.Write(&buf)
|
2019-06-11 05:51:04 +02:00
|
|
|
msg := buf.Bytes()
|
|
|
|
|
2019-06-11 05:57:03 +02:00
|
|
|
deliverate(0, user.Name, xid, msg)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
|
2019-04-09 13:59:33 +02:00
|
|
|
dt := h.Date.Format(time.RFC3339)
|
2019-06-11 16:14:12 +02:00
|
|
|
var jo junk.Junk
|
|
|
|
j := junk.New()
|
2019-05-20 16:48:02 +02:00
|
|
|
j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
|
2019-04-09 13:59:33 +02:00
|
|
|
j["actor"] = user.URL
|
|
|
|
j["published"] = dt
|
2019-06-26 23:19:45 +02:00
|
|
|
if h.Public {
|
|
|
|
j["to"] = []string{h.Audience[0], user.URL + "/followers"}
|
|
|
|
} else {
|
|
|
|
j["to"] = h.Audience[0]
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
if len(h.Audience) > 1 {
|
|
|
|
j["cc"] = h.Audience[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch h.What {
|
|
|
|
case "tonk":
|
|
|
|
fallthrough
|
|
|
|
case "honk":
|
|
|
|
j["type"] = "Create"
|
2019-04-15 22:18:38 +02:00
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
jo = junk.New()
|
2019-05-20 16:48:02 +02:00
|
|
|
jo["id"] = h.XID
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["type"] = "Note"
|
|
|
|
jo["published"] = dt
|
2019-05-20 16:48:02 +02:00
|
|
|
jo["url"] = h.XID
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["attributedTo"] = user.URL
|
|
|
|
if h.RID != "" {
|
|
|
|
jo["inReplyTo"] = h.RID
|
|
|
|
}
|
2019-04-19 17:50:35 +02:00
|
|
|
if h.Convoy != "" {
|
2019-04-19 18:48:44 +02:00
|
|
|
jo["context"] = h.Convoy
|
2019-04-19 17:50:35 +02:00
|
|
|
jo["conversation"] = h.Convoy
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["to"] = h.Audience[0]
|
|
|
|
if len(h.Audience) > 1 {
|
|
|
|
jo["cc"] = h.Audience[1:]
|
|
|
|
}
|
2019-05-28 09:54:03 +02:00
|
|
|
if !h.Public {
|
|
|
|
jo["directMessage"] = true
|
|
|
|
}
|
2019-05-15 22:27:50 +02:00
|
|
|
jo["summary"] = h.Precis
|
2019-08-25 05:03:05 +02:00
|
|
|
jo["content"] = ontologize(mentionize(h.Noise))
|
2019-06-24 01:07:28 +02:00
|
|
|
if strings.HasPrefix(h.Precis, "DZ:") {
|
|
|
|
jo["sensitive"] = true
|
2019-05-04 18:28:00 +02:00
|
|
|
}
|
2019-06-24 01:07:28 +02:00
|
|
|
|
2019-08-19 05:25:11 +02:00
|
|
|
var replies []string
|
|
|
|
for _, reply := range h.Replies {
|
|
|
|
replies = append(replies, reply.XID)
|
|
|
|
}
|
|
|
|
if len(replies) > 0 {
|
|
|
|
jr := junk.New()
|
|
|
|
jr["type"] = "Collection"
|
|
|
|
jr["totalItems"] = len(replies)
|
|
|
|
jr["items"] = replies
|
|
|
|
jo["replies"] = jr
|
|
|
|
}
|
|
|
|
|
2019-07-29 02:01:12 +02:00
|
|
|
var tags []junk.Junk
|
2019-04-09 13:59:33 +02:00
|
|
|
g := bunchofgrapes(h.Noise)
|
2019-04-12 21:02:56 +02:00
|
|
|
for _, m := range g {
|
2019-06-11 16:14:12 +02:00
|
|
|
t := junk.New()
|
2019-04-12 21:02:56 +02:00
|
|
|
t["type"] = "Mention"
|
|
|
|
t["name"] = m.who
|
|
|
|
t["href"] = m.where
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
2019-06-16 00:24:18 +02:00
|
|
|
ooo := ontologies(h.Noise)
|
|
|
|
for _, o := range ooo {
|
|
|
|
t := junk.New()
|
|
|
|
t["type"] = "Hashtag"
|
2019-08-25 05:03:05 +02:00
|
|
|
o = strings.ToLower(o)
|
|
|
|
t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
|
2019-06-16 00:24:18 +02:00
|
|
|
t["name"] = o
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
2019-04-12 21:02:56 +02:00
|
|
|
herd := herdofemus(h.Noise)
|
|
|
|
for _, e := range herd {
|
2019-06-11 16:14:12 +02:00
|
|
|
t := junk.New()
|
2019-04-12 21:02:56 +02:00
|
|
|
t["id"] = e.ID
|
|
|
|
t["type"] = "Emoji"
|
|
|
|
t["name"] = e.Name
|
2019-06-11 16:14:12 +02:00
|
|
|
i := junk.New()
|
2019-04-12 21:02:56 +02:00
|
|
|
i["type"] = "Image"
|
|
|
|
i["mediaType"] = "image/png"
|
|
|
|
i["url"] = e.ID
|
|
|
|
t["icon"] = i
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
|
|
|
if len(tags) > 0 {
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["tag"] = tags
|
|
|
|
}
|
2019-07-29 02:01:12 +02:00
|
|
|
var atts []junk.Junk
|
2019-04-09 13:59:33 +02:00
|
|
|
for _, d := range h.Donks {
|
2019-04-13 18:32:24 +02:00
|
|
|
if re_emus.MatchString(d.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
jd := junk.New()
|
2019-04-09 13:59:33 +02:00
|
|
|
jd["mediaType"] = d.Media
|
|
|
|
jd["name"] = d.Name
|
|
|
|
jd["type"] = "Document"
|
|
|
|
jd["url"] = d.URL
|
|
|
|
atts = append(atts, jd)
|
|
|
|
}
|
|
|
|
if len(atts) > 0 {
|
|
|
|
jo["attachment"] = atts
|
|
|
|
}
|
|
|
|
j["object"] = jo
|
|
|
|
case "bonk":
|
|
|
|
j["type"] = "Announce"
|
2019-06-26 23:19:45 +02:00
|
|
|
if h.Convoy != "" {
|
|
|
|
j["context"] = h.Convoy
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
j["object"] = h.XID
|
2019-08-28 03:22:59 +02:00
|
|
|
case "unbonk":
|
|
|
|
b := junk.New()
|
|
|
|
b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
|
|
|
|
b["type"] = "Announce"
|
|
|
|
if h.Convoy != "" {
|
|
|
|
b["context"] = h.Convoy
|
|
|
|
}
|
|
|
|
b["object"] = h.XID
|
|
|
|
j["type"] = "Undo"
|
|
|
|
j["object"] = b
|
2019-05-03 19:42:39 +02:00
|
|
|
case "zonk":
|
|
|
|
j["type"] = "Delete"
|
2019-05-20 16:48:02 +02:00
|
|
|
j["object"] = h.XID
|
2019-08-21 07:23:00 +02:00
|
|
|
case "ack":
|
|
|
|
j["type"] = "Read"
|
|
|
|
j["object"] = h.XID
|
|
|
|
case "deack":
|
|
|
|
j["type"] = "Ignore"
|
|
|
|
j["object"] = h.XID
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return j, jo
|
|
|
|
}
|
|
|
|
|
|
|
|
func honkworldwide(user *WhatAbout, honk *Honk) {
|
|
|
|
jonk, _ := jonkjonk(user, honk)
|
|
|
|
jonk["@context"] = itiswhatitis
|
2019-04-14 16:06:26 +02:00
|
|
|
var buf bytes.Buffer
|
2019-06-11 16:14:12 +02:00
|
|
|
jonk.Write(&buf)
|
2019-04-14 16:06:26 +02:00
|
|
|
msg := buf.Bytes()
|
2019-04-17 02:14:24 +02:00
|
|
|
|
|
|
|
rcpts := make(map[string]bool)
|
|
|
|
for _, a := range honk.Audience {
|
|
|
|
if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
|
|
|
|
box, _ := getboxes(a)
|
2019-07-25 02:27:13 +02:00
|
|
|
if box != nil && honk.Public && box.Shared != "" {
|
|
|
|
rcpts["%"+box.Shared] = true
|
2019-04-17 02:14:24 +02:00
|
|
|
} else {
|
|
|
|
rcpts[a] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 08:36:47 +02:00
|
|
|
if honk.Public {
|
|
|
|
for _, f := range getdubs(user.ID) {
|
|
|
|
box, _ := getboxes(f.XID)
|
|
|
|
if box != nil && box.Shared != "" {
|
|
|
|
rcpts["%"+box.Shared] = true
|
|
|
|
} else {
|
|
|
|
rcpts[f.XID] = true
|
|
|
|
}
|
2019-04-16 20:40:23 +02:00
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-14 16:06:26 +02:00
|
|
|
for a := range rcpts {
|
2019-04-17 02:22:16 +02:00
|
|
|
go deliverate(0, user.Name, a, msg)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
func asjonker(user *WhatAbout) junk.Junk {
|
2019-04-13 19:58:42 +02:00
|
|
|
about := obfusbreak(user.About)
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-06-11 16:14:12 +02:00
|
|
|
j := junk.New()
|
2019-04-09 13:59:33 +02:00
|
|
|
j["@context"] = itiswhatitis
|
|
|
|
j["id"] = user.URL
|
|
|
|
j["type"] = "Person"
|
|
|
|
j["inbox"] = user.URL + "/inbox"
|
|
|
|
j["outbox"] = user.URL + "/outbox"
|
2019-04-25 06:35:19 +02:00
|
|
|
j["followers"] = user.URL + "/followers"
|
|
|
|
j["following"] = user.URL + "/following"
|
2019-04-09 13:59:33 +02:00
|
|
|
j["name"] = user.Display
|
|
|
|
j["preferredUsername"] = user.Name
|
2019-04-13 19:58:42 +02:00
|
|
|
j["summary"] = about
|
2019-04-09 13:59:33 +02:00
|
|
|
j["url"] = user.URL
|
2019-06-11 16:14:12 +02:00
|
|
|
a := junk.New()
|
2019-07-08 21:02:03 +02:00
|
|
|
a["type"] = "Image"
|
2019-04-09 13:59:33 +02:00
|
|
|
a["mediaType"] = "image/png"
|
|
|
|
a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
|
|
|
|
j["icon"] = a
|
2019-06-11 16:14:12 +02:00
|
|
|
k := junk.New()
|
2019-04-09 13:59:33 +02:00
|
|
|
k["id"] = user.URL + "#key"
|
|
|
|
k["owner"] = user.URL
|
|
|
|
k["publicKeyPem"] = user.Key
|
|
|
|
j["publicKey"] = k
|
|
|
|
|
|
|
|
return j
|
|
|
|
}
|
2019-05-11 00:18:32 +02:00
|
|
|
|
|
|
|
var handfull = make(map[string]string)
|
|
|
|
var handlock sync.Mutex
|
|
|
|
|
|
|
|
func gofish(name string) string {
|
|
|
|
if name[0] == '@' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
m := strings.Split(name, "@")
|
|
|
|
if len(m) != 2 {
|
|
|
|
log.Printf("bad fish name: %s", name)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
handlock.Lock()
|
|
|
|
ref, ok := handfull[name]
|
|
|
|
handlock.Unlock()
|
|
|
|
if ok {
|
|
|
|
return ref
|
|
|
|
}
|
2019-06-03 06:15:06 +02:00
|
|
|
row := stmtGetXonker.QueryRow(name, "fishname")
|
2019-05-11 00:18:32 +02:00
|
|
|
var href string
|
|
|
|
err := row.Scan(&href)
|
|
|
|
if err == nil {
|
|
|
|
handlock.Lock()
|
|
|
|
handfull[name] = href
|
|
|
|
handlock.Unlock()
|
|
|
|
return href
|
|
|
|
}
|
|
|
|
log.Printf("fishing for %s", name)
|
2019-07-11 07:20:53 +02:00
|
|
|
j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
|
2019-05-11 00:18:32 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to go fish %s: %s", name, err)
|
|
|
|
handlock.Lock()
|
|
|
|
handfull[name] = ""
|
|
|
|
handlock.Unlock()
|
|
|
|
return ""
|
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
links, _ := j.GetArray("links")
|
|
|
|
for _, li := range links {
|
|
|
|
l, ok := li.(junk.Junk)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
href, _ := l.GetString("href")
|
|
|
|
rel, _ := l.GetString("rel")
|
|
|
|
t, _ := l.GetString("type")
|
2019-05-11 00:18:32 +02:00
|
|
|
if rel == "self" && friendorfoe(t) {
|
2019-08-16 06:15:55 +02:00
|
|
|
_, err := stmtSaveXonker.Exec(name, href, "fishname")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving fishname: %s", err)
|
|
|
|
}
|
2019-05-11 00:18:32 +02:00
|
|
|
handlock.Lock()
|
|
|
|
handfull[name] = href
|
|
|
|
handlock.Unlock()
|
|
|
|
return href
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handlock.Lock()
|
|
|
|
handfull[name] = ""
|
|
|
|
handlock.Unlock()
|
|
|
|
return ""
|
|
|
|
}
|
2019-06-03 09:20:03 +02:00
|
|
|
|
2019-07-10 07:21:56 +02:00
|
|
|
func isactor(t string) bool {
|
|
|
|
switch t {
|
|
|
|
case "Person":
|
2019-08-16 06:32:18 +02:00
|
|
|
case "Organization":
|
2019-07-10 07:21:56 +02:00
|
|
|
case "Application":
|
|
|
|
case "Service":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-16 05:27:55 +02:00
|
|
|
func investigate(name string) *Honker {
|
2019-06-03 09:20:03 +02:00
|
|
|
if name == "" {
|
2019-08-16 05:27:55 +02:00
|
|
|
return nil
|
2019-06-03 09:20:03 +02:00
|
|
|
}
|
|
|
|
if name[0] == '@' {
|
|
|
|
name = gofish(name)
|
|
|
|
}
|
|
|
|
if name == "" {
|
2019-08-16 05:27:55 +02:00
|
|
|
return nil
|
2019-06-03 09:20:03 +02:00
|
|
|
}
|
2019-08-16 05:27:55 +02:00
|
|
|
log.Printf("digging up some info on %s", name)
|
2019-07-11 07:20:53 +02:00
|
|
|
obj, err := GetJunkFast(name)
|
2019-06-03 09:20:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error investigating honker: %s", err)
|
2019-08-16 05:27:55 +02:00
|
|
|
return nil
|
2019-06-03 09:20:03 +02:00
|
|
|
}
|
2019-06-11 16:14:12 +02:00
|
|
|
t, _ := obj.GetString("type")
|
2019-07-10 07:21:56 +02:00
|
|
|
if !isactor(t) {
|
2019-06-03 09:20:03 +02:00
|
|
|
log.Printf("it's not a person! %s", name)
|
2019-08-16 05:27:55 +02:00
|
|
|
return nil
|
2019-06-03 09:20:03 +02:00
|
|
|
}
|
2019-08-16 05:27:55 +02:00
|
|
|
xid, _ := obj.GetString("id")
|
|
|
|
handle, _ := obj.GetString("preferredUsername")
|
|
|
|
return &Honker{XID: xid, Handle: handle}
|
2019-06-03 09:20:03 +02:00
|
|
|
}
|