Switch tp from using jq to json; format td

This commit is contained in:
Jordan Bancino 2023-03-08 22:47:40 +00:00
parent 76413f834e
commit 20ebeb9c32
3 changed files with 132 additions and 110 deletions

View file

@ -306,4 +306,3 @@ done
if [ -z "$1" ]; then if [ -z "$1" ]; then
recipe_build recipe_build
fi fi

View file

@ -23,34 +23,57 @@ fi
matrix_send() { matrix_send() {
msg="$1" msg="$1"
if [ ! -z "$msg" ]; then if [ ! -z "$msg" ]; then
jq --null-input \ (
--arg body "$msg" \ printf '{'
--arg formatted_body "$msg" \ printf '"body":'
--arg format "org.matrix.custom.html" \ json -e "$msg"
--arg msgtype "m.text" \ printf ',"formatted_body":'
'{"body":$body,"formatted_body":$formatted_body,"format":$format,"msgtype":$msgtype}' | json -e "$msg"
$CURL -X PUT -d @- "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN" printf ',"format":"org.matrix.custom.html",'
printf '"msgtype":"m.text"'
printf '}'
) | $CURL -X PUT -d @- "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN"
fi fi
} }
matrix_get_files() {
$CURL "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" |
jq ".rooms.join.\"$PATCHES_ROOM\".timeline.events[] | select(.type==\"m.room.message\") | .content | select(.msgtype==\"m.file\") | [.body,.info.size,.url] | @tsv" |
cut -d '"' -f 2 |
sed 's/\\t/,/g'
}
case "$1" in case "$1" in
"ingress") "ingress")
matrix_get_files | while IFS="," read -r file size url; do timeline="/tmp/timeline.json"
server=$(echo "$url" | cut -d '/' -f 3) $CURL "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" |
id=$(echo "$url" | cut -d '/' -f 4) json -s "rooms->join->${PATCHES_ROOM}->timeline" >"$timeline"
ext=$(echo "$file" | rev | cut -d '.' -f 1 | rev)
if [ "$ext" != "patch" ] || [ "$size" -gt "$MAX_SIZE" ] || [ -f "$TP_DIR/ingress/$id.patch" ]; then length=$(cat "$timeline" | json -s "events->@length")
i=0
while [ $i -lt $length ]; do
content=$(cat "$timeline" | json -s "events[$i]->content->^body->^formatted_body")
i=$((i + 1))
type=$(echo "$content" | json -s "msgtype->@decode")
if [ "$type" != "m.file" ]; then
continue continue
fi fi
size=$(echo "$content" | json -s "info->size")
if [ "$size" -gt "$MAX_SIZE" ]; then
continue
fi
file=$(echo "$content" | json -s "filename->@decode")
ext=$(echo "$file" | rev | cut -d '.' -f 1 | rev)
if [ "$ext" != "patch" ]; then
continue
fi
url=$(echo "$content" | json -s "url->@decode")
id=$(echo "$url" | cut -d '/' -f 4)
if [ -f "$TP_DIR/ingress/$id.patch" ]; then
continue
fi
server=$(echo "$url" | cut -d '/' -f 3)
if ! $CURL -o "$TP_DIR/ingress/$id.patch" "$HOMESERVER/media/v3/download/$server/$id"; then if ! $CURL -o "$TP_DIR/ingress/$id.patch" "$HOMESERVER/media/v3/download/$server/$id"; then
rm "$TP_DIR/ingress/$id.patch" rm "$TP_DIR/ingress/$id.patch"
echo "Failed to fetch mxc://$server/$id." echo "Failed to fetch mxc://$server/$id."

View file

@ -101,7 +101,7 @@ query(char *select, HashMap * json)
func = 1; func = 1;
} }
else if (keyName[0] == '!') else if (keyName[0] == '^')
{ {
if (JsonValueType(val) == JSON_OBJECT) if (JsonValueType(val) == JSON_OBJECT)
{ {