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 のインスタンス(14) 通知など

Last updated at Posted at 2024-01-23

like や follow リクエストなどを通知する HTML を作る

前回と同じような要領で Like オブジェクトや Follow オブジェクトを HTML 化する。ここではあまり取得時間順にこだわる必要は無いだろう。しかし混ぜて処理する。なぜなら面倒だから。

mkdir ./4notification

この 4notification ディレクトリに Like/Follow/Accept/Undo(follow) のリクエストファイルを突っ込んでいく。他の Undo は差し当たって処理しない。なぜなら面倒だから。
4notification.html へ通知を書き込んでいく make_notification.sh を ./scripts ディレクトリに作る。

make_notification.sh
#!/bin/bash


if [ -n "$(ls ./receive/like/)" ]; then
	mv ./receive/like/* ./4notification
fi
if [ -n "$(ls ./receive/follow_request/)" ]; then
	mv ./receive/follow_request/* ./4notification
fi
if [ -n "$(ls ./receive/accept/)" ]; then
	mv ./receive/accept/* ./4notification
fi
if [ -n "$(ls ./receive/undo_follow/)" ]; then
	mv ./receive/undo_follow/* ./4notification
fi


sorted_files=( $(ls ./4notification | sort) )

tac notification.html > TMP


for i in ${sorted_files[*]}; do

	req_file="./4notification/$i"
	json_file=$(cat "$req_file" | grep "{")
	json_type=$(echo "$json_file" | jq .type | tr -d '"')
	ID=$(echo "$json_file" | jq .id | tr -d '"')
	Actor=$(echo "$json_file" | jq .actor | tr -d '"')
	Object=$(echo "$json_file" | jq .object | tr -d '"')

if [ "$json_type" == "Like" ]; then
	echo "<div>$Actor likes $Object .</div>" >> TMP
	mv $req_file ./receive/like_done
 
elif [ "$json_type" == "Follow" ]; then
	echo "<div>Follow request from $Actor , id: $ID</div>" >> TMP
	mv $req_file ./receive/follow_request_done

elif [ "$json_type" == "Accept" ]; then
	echo "<div>Your follow request to $Actor is accepted .</div>" >> TMP
	mv $req_file ./receive/accept_done
	echo $Actor >> followings # followings というファイルにフォロイーを追加する

elif [ "$json_type" == "Undo" ]; then
	echo "<div>$Actor unfollows you .</div>" >> TMP
	mv $req_file ./receive/undo_follow_done
	grep -v $Actor followers > >(VAR=$(cat) && echo "$VAR" > followers) # followers ファイルから $Actor を消す

fi

done

tac TMP > notification.html

./scripts/accept.sh を書き直す

承認(accept)すべきファイルは処理された後 ./receive/follow_request_done ディレクトリに移っているはずだから、( 9 )[ https://qiita.com/tochu-cha/items/4ec36b67db64c0340d8a ]で作った ./scripts/accept.sh ファイルも書き直す。

# grep でカレントディレクトリから accept したいリクエストのファイルの json 行を取得
follow_request_json=$(grep -rh "$id_of_request" ./)

# grep で follow_request_done から accept したいリクエストのファイルの json 行を取得
follow_request_json=$(grep -rh "$id_of_request" ./receive/follow_request_done)

へ。一応全文を記す。

accept.sh
#!/bin/bash


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

read -p "@to@to_instance: " TO
TO_arry=(${TO//@/ })
to=${TO_arry[0]}
to_instance=${TO_arry[1]}
echo

read -p "What request do you accept?: " id_of_request
echo

# grep で follow_request_done から accept したいリクエストのファイルの json 行を取得
follow_request_json=$(grep -rh "$id_of_request" ./receive/follow_request_done)

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

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

sed -i "s|{follow_request}|$follow_request_json|g" live

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

URL=$(curl https://$to_instance/.well-known/webfinger?resource=acct:$to@$to_instance | jq -r '.links[] | select (.rel == "self")'.href | tr -d "\"")
INBOX=$(curl -H "Accept: application/activity+json" $URL | jq .inbox | tr -d "\"")
REQUEST_TARGET=$(echo  "/"$(sed -E 's/^.*(https):\/\/([^/]+).([^/]+)/\3/g' <<< $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 -v -H "Date: $date_now" -H "Digest: SHA-256=$digest_val" -H "$signature" $INBOX -d "$body"

echo $body > ./post/$unixtime

echo $INBOX >> followers

参考記事

bashでファイルを処理して結果を同じファイルに上書きする

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?