repo: actpub
action: blob
revision: 
path_from: ap-object2html
revision_from: refs/heads/master:
path_to: 
revision_to: 
git.thebackupbox.net
actpub
git clone git://git.thebackupbox.net/actpub

blob of:

actpub

/ ap-object2html

blob_plain of this file

refs/heads/master:/ap-object2html

 #!/usr/bin/env bash

 object_file="$1"

 base="$(basename "${object_file}")"
 if [ "$TILDE" ];then
   cache_uri="/~${TILDE}/ap-cache/object/${base}"
 else
   cache_uri="/ap-cache/object/${base}"
 fi

 if [ ! -s "${object_file}" ];then
   printf '
\n' printf 'error\n' printf 'the cached object file is empty somehow.
\n' printf 'Cache File: %s
\n' "${object_file}" printf 'Cached
\n' "${cache_uri}" printf '
\n' exit 1 fi entitied_error="$(jq -r '.error//""' < "${object_file}" | html_entities_encode)" if [ "$entitied_error" ];then printf '
\n' printf 'error\n' printf "the json object is valid, but contains an 'error' key.
\n" printf "value: %s
\n" "${entitied_error}" printf 'Cache File: %s
\n' "${object_file}" printf 'Cached
\n' "${cache_uri}" printf '
\n' exit 1 fi type="$(jq -r '.type' < "${object_file}")" printf 'Object Type: %s
' "${type}" recursion_count="${2:-0}" if [ "$recursion_count" -gt 10 ];then printf "WHOOOOOOA THERE BUDDY. WE'RE RECURSING TOO DEEP.\n" exit 1 fi if [ ! "$object_file" ];then printf "usage: ap-object2html \n" >&2 exit 1 fi summary="$(jq -r '.summary//""' < "${object_file}")" sensitive="$(jq -r '.sensitive//"open"' < "${object_file}" | sed 's/true//')" id="$(jq -r '.id' < "${object_file}")" entitied_id="$(html_entities_encode <<< "${id}")" printf '
\n' "$sensitive" if [ "$type" != "Person" ];then printf '%s' "$summary" else printf '%s' "$entitied_id" fi id_type="$(jq -r '.id|type' < "${object_file}")" case "$id_type" in null) printf 'Id: (null)
\n' ;; string) qs_id="$(uriescape "$id" | sed 's|\&|%26|g;s|;|%3b|g;s|#|%23|g')" entitied_qs_id="$(html_entities_encode <<< "${qs_id}")" printf 'Id
\n' "$entitied_id" ;; *) printf 'Id: (weird type: %s)
\n' "${id_type}" ;; esac printf 'Cache File Link
' "${cache_uri}" if [ "$type" = "Create" -o "$type" = "Like" ];then ### this is likely the object of an Announce from a Lemmy ### we need to do minimal stuff here, then recurse qs_object="$(jq -r .object < "${object_file}")" entitied_qs_object="$(html_entities_encode <<< "${qs_object}")" printf 'Like Of: %s' "${entitied_qs_object}" "${entitied_qs_object}" printf '
loading...
' "${entitied_qs_id}" printf '
\n' exit 0 fi ### Like and Create should probably be handled differently. attributed_to="$(jq -r '.attributedTo' < "${object_file}")" attributed_to_type="$(jq -r '.attributedTo|type' < "${object_file}")" case "$attributed_to_type" in null) printf 'Attributed To: (null)
\n' ;; string) printf '' ;; *) printf 'Attributed To: (weird type: %s)
\n' "${attributed_to_type}" ;; esac published="$(jq -r '.published//""' < "${object_file}")" if [ "$published" ];then entitied_published="$(html_entities_encode <<< "$published")" entitied_published_dtl="$(TZ="@$REMOTE_IDENT" date +%Y-%m-%dT%H:%M "--date=$published" | html_entities_encode)" printf 'Published:
\n' "${entitied_published}" "${entitied_published_dtl}" fi inReplyTo="$(jq -r '.inReplyTo//""' 2>/dev/null < "${object_file}" | html_entities_encode)" if [ "$inReplyTo" ];then ### TODO: make this dynamically load client-side printf "
\n" printf 'inReplyTo: %s\n' "${inReplyTo}" "${inReplyTo}" #qs_irt="$(uriescape "$inReplyTo" | sed 's|\&|%26|g;s|;|%3b|g;s|#|%23|g')" #entitied_qs_irt="$(html_entities_encode <<< "${qs_irt}")" printf '
loading...
' "${entitied_qs_id}" ## let's cache this fucker too and show it first :D #cache_file="$(ap-cacheobject "${inReplyTo}")" #ap-object2html "${cache_file}" $[$recursion_count + 1] printf "
\n" fi if [ "$type" != "Person" ];then mediaType="$(jq -r '.mediaType//"text/html"' < "${object_file}")" printf "
\n" printf '
' ap-content2html "$mediaType" "$(jq -r '.content//""' < "${object_file}")" printf '
' else printf "Summary: %s
\n" "${summary}" fi if [ "$type" == "Question" ];then printf "Poll Choices: [TODO: WIP: XXX ]
" printf '
\n' #kek. can't actually "get" to non-http, but you know what I mean printf '\n' "$entitied_id" printf '
    \n' i=0 if [ "$(jq -r '.oneOf | type' < "${object_file}")" = "array" ];then printf 'oneOf\n' total_votes=$(jq -r '[.oneOf[] | .replies.totalItems] | add' < "${object_file}") printf 'total votes: %s\n' "$total_votes" jq -r '.oneOf[] | .replies.totalItems,.name' < "${object_file}" | paste - - | while read -r votes name;do printf '
  • \n' printf '\n' "$i" "$name" printf '%s votes\n' "$i" "$votes" "$total_votes" "$votes" printf '\n' "$i" "$name" "$votes" printf '
  • \n' i=$[$i+1] done fi if [ "$(jq -r '.anyOf | type' < "${object_file}")" = "array" ];then printf 'anyOf\n' total_votes=$(jq -r '[.anyOf[] | .replies.totalItems] | add' < "${object_file}") printf 'total votes: %s\n' "$total_votes" jq -r '.oneOf[] | .replies.totalItems,.name' < "${object_file}" | paste - - | while read -r votes name;do printf '
  • \n' printf '\n' "$i" "$name" printf '%s votes\n' "$i" "$votes" "$total_votes" "$votes" printf '\n' "$i" "$name" "$votes" printf '
  • \n' i=$[$i+1] done fi printf '
\n' printf '\n' printf '
\n' fi echo '
' if [ "$(jq -r '.attachment | type' < "${object_file}")" == "array" ];then ### loop over the PropertyValues first and show all of those in a table. pv_pairs="$(jq -r '.attachment[] | select(.type=="PropertyValue") | .name,.value' < "${object_file}")" if [ "$pv_pairs" ];then printf "\n" printf "%s" "$pv_pairs" | paste - - | while read name value;do printf "\n" "$name" "$value" done printf "
%s%s
\n" fi ### then do the media jq -c '.attachment[] | select(.type!="PropertyValue")' < "${object_file}" | while read -r attachment;do # printf '
%s
' "$attachment" # | .url,.type,.mediaType,.href' 2>/dev/null < "${object_file}" | paste - - - - | html_entities_encode | while read url type mediatype href;do url="$(jq -cr '.url' <<< "$attachment")" url_type="$(jq -cr '.url | type' <<< "$attachment")" # printf 'url: "%s"\n' "$url" # printf 'url_type: "%s"\n' "$url_type" if [ "$url_type" = "string" ];then type="$(jq -r .type <<< "$attachment" | tr -d '\n' | jq -Rs)" mediaType="$(jq -r .mediaType <<< "$attachment" | tr -d '\n' | jq -Rs)" href="$(jq -r .url <<< "$attachment" | tr -d '\n' | jq -Rs)" url='[{"href":'"$href"',"mediaType":'"$mediaType"',"type":'"$type"'}]' url_type="array" fi # printf 'url: "%s"\n' "$url" # printf 'url_type: "%s"\n' "$url_type" if [ "${url_type}" = "array" ];then ### we need to loop over all of the values in "url" jq -r '.[] | .type,.mediaType,.href' <<< "$url" | paste - - - | html_entities_encode | while read -r type mediaType href;do #printf '
.type: %s\n.mediaType: %s\n.href: %s\n
' "$type" "$mediaType" "$href" ### let's just ignore Link type for now I guess. ### stuff from honk will have images that have multiple mediaTypes set to type = Link ### in an array of url... which is the main reason this got messy. #if [ "$type" = "Link" ];then # printf 'attachment: %s
\n' "$href" "$href" #else case "$mediaType" in image/*) printf 'attachment:
\n' "$href" "$href" ;; audio/*) printf 'attachment: