utils.actvitypub.posting.go

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main

import (
	"encoding/json"
	"fmt"
	"slices"
	"strings"
)

type PostMeta struct {
	ID     string        `json:"id"`
	Type   string        `json:"type"`
	Actor  string        `json:"actor"`
	To     StringOrArray `json:"to"`
	CC     StringOrArray `json:"cc"`
	Object NoteMeta      `json:"object"`
}

type PostMetaWithContext struct {
	Context []ContextElement `json:"@context"`
	PostMeta
}

type LDSignature struct {
	Type    string `json:"type"`
	Creator string `json:"creator"`
	Created string `json:"created"`
	Value   string `json:"signatureValue"`
}

type NoteMetaWithContext struct {
	Context []ContextElement `json:"@context"`
	NoteMeta
}

type Tag struct {
	Type string `json:"type"`
	Href string `json:"href"`
	Name string `json:"name"`
}

func AtToMention(atstring string) *Tag {
	withoutAt := strings.TrimPrefix(atstring, "@")
	parts := strings.SplitN(withoutAt, "@", 2)
	if len(parts) == 2 {
		c, err := NewHTTPClient("GET", "https://"+parts[1]+"/.well-known/webfinger?resource=acct:"+withoutAt, nil)
		if err != nil {
			return nil
		}
		if body, err := c.DoClear(); err == nil {
			wfr := &WebFingerResponse{}
			if err := json.Unmarshal([]byte(body), wfr); err == nil {
				for _, link := range wfr.Links {
					if link.Rel == "self" {
						return &Tag{
							Type: "Mention",
							Name: atstring,
							Href: link.Href,
						}
					}
				}

			}
		}
	}
	return nil
}

func ExtractMentions(content string) []*Tag {
	mentions := []*Tag{}
	words := strings.Fields(content)
	for _, word := range words {
		if strings.HasPrefix(word, "@") {
			if mention := AtToMention(word); mention != nil {
				mentions = append(mentions, mention)
			}
		}
	}
	return mentions
}

func (user *User) PostMessage(note NoteMeta) {
	user.wlock.Lock()
	user.posts = append(user.posts, FromNote(CacheId(user, "notes", "", note.ID), user, user.ID, &note))
	slices.SortFunc(user.posts, ComparePost)
	slices.Reverse(user.posts)
	user.wlock.Unlock()

	item := PostMetaWithContext{}
	item.Context = []ContextElement{
		{Simple: StringOrArray{"https://www.w3.org/ns/activitystreams"}},
		{Complex: map[string]string{"ostatus": "http://ostatus.org#", "atomUri": "ostatus:atomUri", "inReplyToAtomUri": "ostatus:inReplyToAtomUri", "conversation": "ostatus:conversation", "sensitive": "as:sensitive", "toot": "http://joinmastodon.org/ns#", "votersCount": "toot:votersCount"}},
	}
	item.Object = note
	item.To = note.To
	item.CC = note.CC

	item.Type = "Create"
	item.Actor = user.ID
	item.ID = note.ID + "/activity"

	if data, err := json.Marshal(item); err == nil {
		CacheData(user, "outbox", "create", item.ID, data)
		go func() {

			for _, cc := range item.CC {
				if actor, err := user.fetchActorInfo(cc, false); err == nil {
					fmt.Printf("[action] sending activity from %v to %s\n", user.ID, actor.Inbox)
					if err := postSigned(user, data, actor.Inbox); err != nil {
						fmt.Printf("[error] could not publish activity to %s: %s", actor.Inbox, err)
					}
				}
			}

			for _, follower := range user.followers {
				fmt.Printf("[action] sending activity from %v to %s\n", user.ID, follower.Inbox)
				if err := postSigned(user, data, follower.Inbox); err != nil {
					fmt.Printf("[error] could not publish activity to %s: %s", follower.Inbox, err)
				}
			}

		}()
	}

}

// Send a like to the provided inbox
func (user *User) SendLike(object string, inbox string) error {
	fmt.Printf("[action] sending like from %v\n", user.ID)
	item := InboxRequestBasicWithContext{}
	item.Object = object
	item.Type = "Like"
	item.Actor = user.ID
	item.Context = []string{"https://www.w3.org/ns/activitystreams"}

	if data, err := json.Marshal(item); err == nil {
		// note we store the likes by the object id (not the activity id)
		CacheData(user, "outbox", "likes", object, data)
		return postSigned(user, data, inbox)
	} else {
		return err
	}
}