# a PrivaPub status in alice_peertube's home whose text or title has the given words; a channel shares its videos by
# boosting them, so it is the boosted status
p_home_id(){ curl -s -H "$PH""$P/api/v1/timelines/home?limit=40"| j "print(next((o['id'] for o in ((s.get('reblog') or s) for s in d) if '$1' in (o['content'] or '') or '$1' in ((o.get('privapub') or {}).get('title') or '')), ''))";}
echo"peertube"
[ -n "$PTROOT"]&& ok "PeerTube root token"||{ ko "PeerTube root token";return 1;}
[ -n "$channel_on_p"]&& ok "PrivaPub resolves the channel @$channel@peertube.test"|| ko "PrivaPub cannot resolve the channel"
["$(curl -s -H "$PH""$P/api/v1/accounts/$channel_on_p"| j "print(d['group'])")"="True"]&& ok "the channel is a group account"|| ko "the channel is not shown as a group"
until_true 60'[ -n "$(p_home_id "Hello PrivaPub from PeerTube $run")" ]'&& ok "the video reaches alice_peertube's home, boosted by its channel"|| ko "the video never arrived"
v_on_p=$(p_home_id "Hello PrivaPub from PeerTube $run")
["$(echo"$v_status"| j "print(d['media_attachments'][0]['type'])")"="video"]&& ok "the video is a playable attachment"|| ko "no video attachment: $(echo"$v_status"| j "print(d['media_attachments'])"| head -c 200)"
until_true 30'[ "$(pcurl "$PTB/api/v1/videos/$video_id/comment-threads" | j "print(any(\"lovely video\" in c[\"text\"] for c in d[\"data\"]))")" = "True" ]'\
&& ok "alice_peertube's reply is a comment on the video"|| ko "the reply never became a comment"
thread=$(pcurl "$PTB/api/v1/videos/$video_id/comment-threads"| j "print(next(c['id'] for c in d['data'] if 'lovely video' in c['text']))")
pcurl -o /dev/null -X POST -H "$TH" -H 'Content-Type: application/json'"$PTB/api/v1/videos/$video_id/comments/$thread" -d '{"text":"thank you from PeerTube"}'
until_true 30'[ "$(curl -s -H "$PH" "$P/api/v1/statuses/$v_on_p/context" | j "print(any(\"thank you from PeerTube\" in s[\"content\"] for s in d[\"descendants\"]))")" = "True" ]'\
&& ok "ptuser's answer threads under the video on PrivaPub"|| ko "PeerTube's answer missing from the thread"
until_true 60'[ -n "$(p_home_id "A moderated video $held")" ]' >/dev/null
m_on_p=$(p_home_id "A moderated video $held")
m_reply=$(curl -s -X POST -H "$PH"$P/api/v1/statuses -d "status=@ptuser@peertube.test a comment to approve $held&in_reply_to_id=$m_on_p&visibility=public"| j "print(d['id'])")
["$(curl -s -H "$PH""$P/api/v1/statuses/$m_reply"| j "print((d.get('privapub') or {}).get('approval'))")"="pending"]\
&& ok "alice's comment on a moderated video waits for its approval"|| ko "alice's comment on a moderated video does not wait"
# (its owner sees it in the video's threads, held for review; PeerTube 8.3's list of held comments answers 500)
pt_held(){ pcurl -H "$TH""$PTB/api/v1/videos/$mvideo/comment-threads"| j "print(next((c['id'] for c in d['data'] if c.get('heldForReview') and 'approve $held' in c['text']), ''))";}
until_true 30'[ -n "$(pt_held)" ]'&& ok "PeerTube holds it for review"|| ko "PeerTube never held alice's comment"
held_id=$(pt_held)
pcurl -o /dev/null -X POST -H "$TH""$PTB/api/v1/videos/$mvideo/comments/$held_id/approve"
curl -s -o /dev/null -X POST -H "$PH""$P/api/v1/statuses/$v_on_p/favourite"
until_true 30'[ "$(pcurl "$PTB/api/v1/videos/$video_id" | j "print(d[\"likes\"])")" = "1" ]'&& ok "alice_peertube's like counts on PeerTube"|| ko "like not counted on PeerTube"
curl -s -o /dev/null -X POST -H "$PH""$P/api/v1/statuses/$v_on_p/unfavourite"
until_true 30'[ "$(pcurl "$PTB/api/v1/videos/$video_id" | j "print(d[\"likes\"])")" = "0" ]'&& ok "alice_peertube's unlike reaches PeerTube"|| ko "unlike not applied on PeerTube"
echo" edits and deletes"
pcurl -o /dev/null -X PUT -H "$TH" -H 'Content-Type: application/json'"$PTB/api/v1/videos/$video_id" -d '{"name":"Hello again from PeerTube","description":"renamed"}'
until_true 30'[ "$(curl -s -H "$PH" "$P/api/v1/statuses/$v_on_p" | j "print(\"Hello again from PeerTube\" in json.dumps(d))")" = "True" ]'\
&& ok "the video's new name reaches PrivaPub"|| ko "the edit never arrived"