#shellcheck disable=SC2034

cd "$(dirname "$path")"

userposts=userposts

[[ ! -e "$userposts" ]] && touch "$userposts"

TITLE="blag-forum"
DESCRIPTION="the most dubious forum software ever written"
AUTHOR="$USER"
TAGS=(meta interactive)
WRITTEN_AT=2024-12-16T14:00:00Z

is_posting=$(jq -r '.action == "post"' <<< "$BLAG_QUERY")
if [[ "$is_posting" == 'true' ]]; then
	DONT_CACHE=1
fi

metadata

header 1 <<< "$TITLE"
header 2 <<< "$DESCRIPTION"

link "forum.sh" <<< "view source" | paragraph

header 3 <<< "rules"

{ br_lines | paragraph; } <<EOF
be nice
dont spam
EOF

header 3 <<< "make a post"

if [[ "$is_posting" == "true" ]]; then 
	post=$(jq -r '.post' <<< "$BLAG_QUERY")
	if (( ${#post} > 1000 || ${#post} == 0 )); then
		paragraph <<< "your post must be between 1 and 1000 characters"
	else
		(set -e;
			encoded=$(jq -Rsr 'ltrimstr(" ") | rtrimstr(" ") | @json' <<< "$post")
			if rg -qxF "$encoded" "$userposts"; then
				paragraph <<< "say something unique."
			else
				echo "$encoded" >> "$userposts"
				touch -c "$(basename "$path")" # cache invalidation
				paragraph <<< "post submitted"
			fi
		) || {
			paragraph <<< "error while submitting post"
		}
	fi
fi
	
cat form.html

header 3 <<< "posts"

i=0

while read -r post; do
	jq -r '"<blockquote>" + @html + "</blockquote>"' <<< "$post" | paragraph
	(( i++ ))
done < "$userposts"

paragraph <<< "serving <b>$i</b> posts for some reason"
