0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Bash と Caddy サーバで ActivityPub のインスタンス(7) 単に Note する。

Last updated at Posted at 2024-01-17

だが各フォロワーにも配送しなくてはならない

誰に対してでもなくただ Note するだけなら別に配送する必要がなく、単に自分のプロフィールページに書けばいいように思うが、もし誰かにフォローされていたなら、フォロワーにも見えるように自分の Note 投稿を配送しなくてはならない。
まず followers というファイルを作り、そこに自分の inbox を記す。

followers
https://ホニャララ/users/aktor/inbox

他のアクターから follow リクエストが来たら、承認してその inbox をここに追加していく。(承認するとは限らないが)
そして note 、 reply 、 announce (ブースト)等するときはリプライする相手 + 各フォロワー全てに配送(curl で POST)する。面倒い。

Note.json を作る

note.json
{
  "@context": [
    "https://www.w3.org/ns/activitystreams"
  ],
  "id": "https://{my_instance}/users/{myself}/statuses/{id}/activity",
  "type": "Create",
  "actor": "https://{my_instance}/users/{myself}",
  "published": "{date_now}",
  "to": [
    "https://www.w3.org/ns/activitystreams#Public"
  ],
  "cc": [
    "https://{my_instance}/users/{myself}/followers"
  ],
  "object": {
    "id": "https://{my_instance}/users/{myself}/statuses/{id}",
    "type": "Note",
    "published": "{date_now}",
    "attributedTo": "https://{my_instance}/users/{myself}",
    "to": [
      "https://www.w3.org/ns/activitystreams#Public"
    ],
    "cc": [
      "https://{my_instance}/users/{myself}/followers"
    ],
    "content": "<p>{content}</p>"
  }
}

reply.json から "tag" タグと "inReplyTo" タグを外し、cc で配送する先をフォロワーだけにしたものだ。

カレントディレクトリの整理

これを作るのだが、そろそろカレントディレクトリがゴチャついて来たので scripts ディレクトリと json_templates ディレクトリを作り、 .sh ファイルと .json ファイルをそれぞれそこへ突っ込む。また post_like.sh は単に like.sh にリネイムしておく。よって reply.sh の

sed -e "s/{id}/$unixtime/g" reply.json > live

sed -e "s/{id}/$unixtime/g" ./json_templates/reply.json > live

に、 like.sh の

sed -e "s/{id}/$unixtime/g" like.json > live

sed -e "s/{id}/$unixtime/g" ./json_templates/like.json > live

に変える。
note.json も json_templates ディレクトリへ置く。
現在のディレクトリは以下。

current/
 ├ 202
 ├ 204
 ├ personal/ host-meta webfinger actor private public
 ├ script/ verify.sh like.sh reply.sh note.sh(これから作る)
 ├ json_template/ like.json reply.json note.json
 ├ statuses/ 送った Create リクエストの Note 部分s
 ├ post/ 送った josn ファイルs
 └ followers 他、色々と出来ちまうファイルs

note.sh の作成

基本は reply.sh と同じだが、各フォロワーの inbox へも POST しなくてはならない。面倒い。
まず、 followers ファイルから各行を取って配列にする。そして for で回し、各 inbox へ送りつける。それ以外は reply.sh よりは簡単である。

note.sh
#!/bin/bash


read -p "@myself@my_instance: " MY
MY_arry=(${MY//@/ })
myself=${MY_arry[0]}
my_instance=${MY_arry[1]}
echo

read -p "what: " content
echo

unixtime=$(date "+%s")
date_now=$(date -uR  | head -c -6 | sed -e 's/$/GMT/')

sed -e "s/{id}/$unixtime/g" ./json_templates/note.json > live
sed -i "s/{myself}/$myself/g" live
sed -i "s/{my_instance}/$my_instance/g" live
sed -i "s/{date_now}/$date_now/g" live
sed -i "s/{content}/$content/g" live

body=$(cat live | jq -c .)
digest_val=$(echo -n "$body" | sha256sum | xxd -r -p | base64)

### フォロワーの inbox 一覧を followers ファイルから取得
while read -r line; do
	followers_inbox_URL+=( $(echo $line) ) # 配列に
done < followers

### for で各フォロワーへcurl
for INBOX in ${followers_inbox_URL[*]}; do

	to_instance=$(sed -E 's/^.*(http|https):\/\/([^/]+).*/\2/g' <<< $INBOX) # インスタンス名
	REQUEST_TARGET=$(echo "/"$(sed -E 's/^.*(http|https):\/\/([^/]+).([^/]+)/\3/g' <<< $INBOX)) # https://インスタンス/users/アクター/inbox => /users/アクター/inbox

	signature_headers="(request-target): post ${REQUEST_TARGET}\ndate: ${date_now}\nhost: ${to_instance}\ndigest: SHA-256=${digest_val}"
	echo -e "$signature_headers" > 4_sig_headers # 後に見返すためにファイルに
	sig_val=$(echo -en $signature_headers | openssl dgst -binary -sign ./personal/private -sha256 | openssl enc -A -base64)
	signature="Signature: keyId=\"https://${my_instance}/users/${myself}#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) date host digest\",signature=\"$sig_val\""

	curl -H "Date: $date_now" -H "Digest: SHA-256=$digest_val" -H "$signature" $INBOX -d "$body"

done

echo $body | jq .object > ./statuses/$unixtime
echo $body > ./post/$unixtime
bash ./script/note.sh

と実行する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?