# organise; an event arrives as an Event with its dates and place; she joins it and leaves; comments both ways; an edit
# and comments closed; deletes; a group post; an event kept for decePubClient's e2e; the unfollow. An event made through its API without options has its comments closed
# (`commentsEnabled: false`), which PrivaPub honours, so this one opens them. Mobilizon's API is GraphQL (/api), signed in by its login mutation; what it holds is read from
p_home_has(){ 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"mobilizon"
[ -s "$here/.state/mobilizon.token"]&& ok "Mobilizon credentials for mzuser"||{ ko "Mobilizon credentials";return 1;}
mz 'mutation($n:String!){createGroup(preferredUsername:$n,name:"Pasture events",summary:"<p>events in the pasture</p>",visibility:PUBLIC,openness:OPEN){id}}'\
["$(echo"$event_json"| j "e = (d.get('privapub') or {}).get('event') or {}; print(e.get('start', '')[:16], e.get('end', '')[:16])")"="${begins:0:16}${ends:0:16}"]\
&& ok "it arrives as an event with its start and end"|| ko "the event's dates are missing ($(echo"$event_json"| j "print((d.get('privapub') or {}).get('event'))"))"
echo"$event_json"| j "e = (d.get('privapub') or {}).get('event') or {}; print(any('Borghese' in ((p.get('name') or '') + (p.get('address') or '')) and p.get('latitude') for p in e.get('places', [])))"| grep -q True \
&& ok "and with its place, located"|| ko "the event's place is missing"
# alice joins the event (owner decision 2026-10-05): a Join Mobilizon takes at once for a free event, then a Leave
participation(){ curl -s -H "$PH""$P/api/v1/statuses/$e_on_p"| j "print(((d.get('privapub') or {}).get('event') or {}).get('participation'))";}
mz_participant(){ mz_sql "select p.role from participants p join actors a on a.id = p.actor_id join events e on e.id = p.event_id where a.url like '%alice_mobilizon%' and e.id = $event_id";}
curl -s -o /dev/null -X POST -H "$PH""$P/api/privapub/v1/statuses/$e_on_p/join"
until_true 45'[ "$(mz_participant)" = "participant" ]'&& ok "alice_mobilizon's Join makes her a participant on Mobilizon"|| ko "the Join never made a participant ($(mz_participant))"
until_true 45'[ "$(participation)" = "accepted" ]'&& ok "Mobilizon's Accept reaches PrivaPub"|| ko "the participation stays $(participation)"
curl -s -o /dev/null -X POST -H "$PH""$P/api/privapub/v1/statuses/$e_on_p/leave"
until_true 45'[ -z "$(mz_participant)" ]'&& ok "her Leave takes her off the event on Mobilizon"|| ko "she is still a participant after leaving"
["$(participation)"="None"]&& ok "and PrivaPub forgets it"|| ko "PrivaPub still shows a participation"
curl -s -o /dev/null -X POST -H "$PH""$P/api/v1/statuses" -d "status=a PrivaPub comment on the picnic&in_reply_to_id=$e_on_p&visibility=public"
until_true 45'[ "$(mz_sql "select count(*) from comments where text like '"'%a PrivaPub comment on the picnic%'"' and deleted_at is null")" -ge 1 ]'\
&& ok "alice_mobilizon's reply becomes a comment on the event"|| ko "the comment never reached Mobilizon"
comment=$(mz 'mutation($e:ID!){createComment(eventId:$e,text:"<p>a Mobilizon comment on the picnic</p>"){id}}'"{\"e\":\"$event_id\"}"| j "print(d['data']['createComment']['id'])")
until_true 45'[ "$(curl -s -H "$PH" "$P/api/v1/statuses/$e_on_p/context" | j "print(any(\"a Mobilizon comment on the picnic\" in s[\"content\"] for s in d[\"descendants\"]))")" = "True" ]'\
&& ok "mzuser's comment threads under the event on PrivaPub"|| ko "Mobilizon's comment missing on PrivaPub"
echo" edits and deletes"
mz 'mutation($e:ID!,$t:String!){updateEvent(eventId:$e,title:$t){id}}'"{\"e\":\"$event_id\",\"t\":\"$title, moved indoors\"}" >/dev/null
until_true 45'curl -s -H "$PH" "$P/api/v1/statuses/$e_on_p" | grep -q "moved indoors"'&& ok "the event's edit reaches PrivaPub"|| ko "the event's edit not applied"
until_true 45'[ "$(curl -s -H "$PH" "$P/api/v1/statuses/$e_on_p/context" | j "print(any(\"a Mobilizon comment on the picnic\" in s[\"content\"] for s in d[\"descendants\"]))")" = "False" ]'\
&& ok "mzuser's deleted comment leaves PrivaPub"|| ko "the deleted comment stays on PrivaPub"
until_true 60'[ -n "$(p_home_has "$post_title")" ]'&& ok "the group's post reaches alice_mobilizon's home, with its title"|| ko "the group's post never arrived"
mz 'mutation($t:String!,$b:DateTime!,$e:DateTime!,$o:ID!,$g:ID){createEvent(title:$t,description:"<p>come and listen</p>",beginsOn:$b,endsOn:$e,organizerActorId:$o,attributedToId:$g,visibility:PUBLIC,joinOptions:FREE,options:{commentModeration:ALLOW_ALL},physicalAddress:{description:"Piazza del Popolo",locality:"Roma",country:"Italia",geom:"12.4763;41.9109"}){id url}}'\