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
recipe_build
fi

View file

@ -23,34 +23,57 @@ fi
matrix_send() {
msg="$1"
if [ ! -z "$msg" ]; then
jq --null-input \
--arg body "$msg" \
--arg formatted_body "$msg" \
--arg format "org.matrix.custom.html" \
--arg msgtype "m.text" \
'{"body":$body,"formatted_body":$formatted_body,"format":$format,"msgtype":$msgtype}' |
$CURL -X PUT -d @- "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN"
(
printf '{'
printf '"body":'
json -e "$msg"
printf ',"formatted_body":'
json -e "$msg"
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
}
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
"ingress")
matrix_get_files | while IFS="," read -r file size url; do
server=$(echo "$url" | cut -d '/' -f 3)
id=$(echo "$url" | cut -d '/' -f 4)
ext=$(echo "$file" | rev | cut -d '.' -f 1 | rev)
timeline="/tmp/timeline.json"
$CURL "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" |
json -s "rooms->join->${PATCHES_ROOM}->timeline" >"$timeline"
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
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
rm "$TP_DIR/ingress/$id.patch"
echo "Failed to fetch mxc://$server/$id."

View file

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