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

blob of:

actpub

/ ap-create-note

blob_plain of this file

refs/heads/master:/ap-create-note

 #!/usr/bin/env bash

 while true;do
   case "$1" in
   -s)
     shift
     summary="$1"
     shift
     ;;
   -r)
     shift
     in_reply_to="$1"
     shift
     ;;
   *)
     break
     ;;
   esac
 done

 if [ ! "$1" ];then
   printf "usage: ap-create-note [-s ] [-r ] \n" >&2
   exit 1
 fi

 ### come up with a way of getting an object to this script...
 ### GET params from a webpage UI? heh.
 ### "type" can be any from https://www.w3.org/TR/activitystreams-vocabulary/#object-types
 ### but will probably get hardcoded to Note

 #    "id": "https://example.com/~mallory/note/72",
 #    "type": "Note",
 #    "attributedTo": "https://example.net/~mallory",
 #    "content": "This is a note",
 #    "published": "2015-02-10T15:04:55Z",
 #    "to": ["https://example.org/~john/"],
 #    "cc": ["https://example.com/~erik/followers",
 #           "https://www.w3.org/ns/activitystreams#Public"]

 type="Create"
 tagged_users="$(grep '^@' <<< "$1")" #close enough. just don't do dumb shit.
 hashtags="$(grep '^#' <<< "$1")"
 content="$(ap-content2html "text/plain" "$1")"
 content_json_escaped="$(printf "%s" "$content" | jq -Rs '.')"

 ### common stuff below
 timestamp="$(date +%s)"
 ap_date="$(ap-date "--date=@${timestamp}")"
 context="https://www.w3.org/ns/activitystreams"
 uuid="$(uuidgen -t)"
 acct="$(ap-whoami)"
 actor="$(ap-getactoruri "${acct}")"
 my_outbox="$(jq -r .outbox < "$(ap-cacheactor "${actor}")")"
 id="${my_outbox}${uuid}" #need to get my own outbox URI
 to="https://www.w3.org/ns/activitystreams#Public"
 cc="$(jq -r .followers < "$(ap-cacheactor "$actor")")"

 if [ "$summary" ];then
   summary_json_escaped="$(printf "%s" "$summary" | jq -Rs '.')"
   summary=',"summary":'"$summary_json_escaped"',"sensitive":true'
 fi

 tagged_actors="$(if [ "${tagged_users}" ];then xargs -n1 ap-getactoruri <<< "${tagged_users}";fi)"
 tag_user_json="$(if [ "${tagged_users}" ];then
   for tagged_user in ${tagged_users};do
     printf '{'
     printf '"type":"Mention",'
     printf '"href":%s,' "$(ap-getactoruri "${tagged_user}" | jq -R .)" ##TODO! JSON ESCAPE THIS!
     printf '"name":%s' "$(jq -R . <<< "${tagged_user}")"
     printf '}\n'
   done
 fi)"
 tag_hash_json="$(if [ "${hashtags}" ];then
   for hashtag in ${hashtags};do
     printf '{'
     printf '"type":"Hashtag",'
     printf '"href":"%s",' "${hashtag}"
     printf '"name":"%s"' "${hashtag}"
     printf '}\n'
   done
 fi)"
 ### it'll turn into [] if the strings are empty anyway.
 tag_json="$(printf '%s%s' "$tag_user_json" "${tag_hash_json}" | sort | uniq | jq -s .)"

 if [ "$in_reply_to" ];then
   reply=',"inReplyTo":"'"${in_reply_to}"'"'
   ## we need to add to the list of cc
   irt_object="$(ap-cacheobject "${in_reply_to}")"
   irt_actor="$(jq -r '.attributedTo' < "${irt_object}")"
   irt_actor_object="$(ap-cacheactor "${irt_actor}")"
   irt_followers="$(jq -r '.followers' < "${irt_actor_object}")"
   irt_cc="$(jq -r '.cc[]//""' < "${irt_object}")"
   irt_to="$(jq -r '.to[]//""' < "${irt_object}")"
 fi

 to_list="$(printf "%s\n%s\n" "$to" "$irt_to" | grep -v '^$' | grep -v '^null$' | sort | uniq)"
 cc_list="$(printf "%s\n%s\n%s\n%s\n%s\n" "$irt_actor" "$irt_followers" "$irt_cc" "$cc" "$tagged_actors" | grep -v '^$' | grep -v '^null$' | sort | uniq)"

 to_json="$(printf '%s\n' "${to_list}" | jq -R . | jq -s .)"
 cc_json="$(printf "%s\n" "${cc_list}" | jq -R . | jq -s .)"

 ### reply and summary added into the object is kind of hacky. :/

 object='{
   "id":			"'"${id}"'#object",
   "type":		"Note",
   "attributedTo":	"'"${actor}"'",
   "content":		 '"${content_json_escaped}"',
   "tag":                 '"$tag_json"',
   "published":		"'"${ap_date}"'",
   "to": '"$to_json"',
   "cc": '"$cc_json"'
   '"$reply""$summary"'
 }'

 export POST_DATA='{
   "@context":	"'"${context}"'",
   "id":		"'"${id}"'",
   "type":	"'"${type}"'",
   "actor":	"'"${actor}"'",
   "published":	"'"${ap_date}"'",
   "object":	 '"${object}"',
   "to": '"$to_json"',
   "cc": '"$cc_json"'
 }'

 printf "%s\n" "$POST_DATA" | jq . || exit "$(printf "BAD POST_DATA:\n\n%s\n\n" "$POST_DATA" >&2 ; echo 1)"

 followers_list="$(ap-getfollowers "${acct}")"
 #to_list
 #cc_list set at top of script.
 target_list="$(printf "%s\n%s\n%s\n" "$followers_list" "$to_list" "$cc_list")"
 echo "going to send this to the inboxes all of these targets: ${target_list}"

 target_inboxes="$(printf "%s\n" "$target_list" | xargs -n1 ap-getendpoint | sort | uniq)"

 echo "the inboxes for all of them: ${target_inboxes}"

 printf "/!\\ DOES THIS ALL LOOK GOOD? /!\\\n"
 read confirm
 [ "$confirm" == "yes" ] || exit "$(printf "KK. NOT POSTING\n" >&2 ; echo 1)"

 ### create the outbox entry.
 cache_file=~/.cache/ap/outbox/"${uuid}"
 printf "%s" "$POST_DATA" > "${cache_file}"
 printf "ap-create-note: local cache file of create activity: %s\n" "${cache_file}" >&2
 ap-upload-to-outbox "${cache_file}"

 for target_inbox in $(printf "%s\n" "$target_list" | xargs -n1 ap-getendpoint | sort | uniq);do
 	ap-signed-post "${target_inbox}"
 done
 echo 'ap-create-note: [DONE]' >&2