!GsmxjHfeAYLsTEQmjS:nixos.org

Matrix Meta (Nix)

670 Members
Discuss your proposals for the Matrix space here, before suggesting them in #matrix-suggestions:nixos.org192 Servers

Load older messages


SenderMessageTime
23 Jul 2021
@grahamc:nixos.org@grahamc:nixos.orgcool14:59:06
@grahamc:nixos.org@grahamc:nixos.orgthanks!14:59:07
@grahamc:nixos.org@grahamc:nixos.orgI don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃14:59:36
@andi:kack.itandi-One of the famous matrix bots has a command/plugin for converting an image to an MXC URI15:07:56
@schnecfk:ruhr-uni-bochum.deCRTifiedRedacted or Malformed Event15:11:15
@schnecfk:ruhr-uni-bochum.deCRTified
In reply to @grahamc:nixos.org
I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃
#!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl

# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here

# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"

# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: image/jpeg" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" | jq '.content_uri')
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $mxc }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"

15:29:53
@grahamc:nixos.org@grahamc:nixos.org🥳15:30:42
@schnecfk:ruhr-uni-bochum.deCRTifiedOh, Content-type might be wrong and needs a fix15:33:09
@schnecfk:ruhr-uni-bochum.deCRTifiedBut it works even if it's wrong15:33:19
@schnecfk:ruhr-uni-bochum.deCRTified
In reply to @grahamc:nixos.org
I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃
*
#!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl

# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here

# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"

# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: image/png" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" | jq '.content_uri')
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $mxc }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"

15:34:10
@schnecfk:ruhr-uni-bochum.deCRTified * Oh, Content-type might be wrong and needs a fix (Edited it to image/png) 15:34:37
@schnecfk:ruhr-uni-bochum.deCRTifiedAnd note that there's no error handling 👀15:35:25
@sumner:sumnerevans.comsumner

Alternative is:

mimetype=$(file --mime-type $avatar_path | sed 's/.*: //g')
15:37:27
@schnecfk:ruhr-uni-bochum.deCRTifiedThat's significantly cleaner15:37:52
@sumner:sumnerevans.comsumner *

Alternative is:

mimetype=$(file --mime-type $FILEPATH | sed 's/.*: //g')
15:38:14
@schnecfk:ruhr-uni-bochum.deCRTified *
#!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl

# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here


# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"

# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')

echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"


15:40:31
@schnecfk:ruhr-uni-bochum.deCRTifiedAdded it :)15:40:45
@schnecfk:ruhr-uni-bochum.deCRTifiedJust the filename that's wrong now :D15:41:15
@leons:is.currently.onlineLeon You should add file as a build input ;) 15:41:21
@schnecfk:ruhr-uni-bochum.deCRTified *
#!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl file

# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here


# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"

# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')

echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"


15:41:29
@schnecfk:ruhr-uni-bochum.deCRTified
In reply to @leons:is.currently.online
You should add file as a build input ;)
you're right. Didn't copy the whole file again as my token is in the local version 🙈
15:41:50
@sumner:sumnerevans.comsumner
In reply to @schnecfk:ruhr-uni-bochum.de
Just the filename that's wrong now :D
filename is optional
15:42:06
@schnecfk:ruhr-uni-bochum.deCRTified *
#!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl file

# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here


# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"

# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')

echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"


15:42:18
@schnecfk:ruhr-uni-bochum.deCRTified In that case, poof :D 15:42:25
@sumner:sumnerevans.comsumner
In reply to @schnecfk:ruhr-uni-bochum.de
Just the filename that's wrong now :D
* filename is optional. Required fields are marked as such.
15:42:37
@sumner:sumnerevans.comsumnerMay be worth uploading that as a file so that it's easy to find in the sidebar.15:43:59
@schnecfk:ruhr-uni-bochum.deCRTifiedIn that case I'll run shellcheck on it15:44:36
@grahamc:nixos.org@grahamc:nixos.orghttps://matrix.org/_matrix/media/r0/download/nixos.org/ab7db8fb43cc002b48b03de4cf04f0ec7c75558515:48:02
@grahamc:nixos.org@grahamc:nixos.orgnice!15:48:25
@grahamc:nixos.org@grahamc:nixos.org

here is what I've put together:


make_avatar() (
  room_id=$1
  roomhash=$(md5sum <<<"$room_id" | cut -d' ' -f1)
  filename="$scratch/$roomhash.avatar"

  curl "https://www.gravatar.com/avatar/${roomhash}?d=identicon&f=y&s=2048" > "$filename"
  echo "$filename"
)

upload_media() (
  FILEPATH="${1}"

  mime=$(file --brief --mime "$FILEPATH")

  # Upload image to image.png
  curl \
    --silent \
    -H "Content-Type: $mime" \
    -H "Authorization: Bearer ${AUTH_TOKEN}" \
    -X POST \
    --data-binary "@${FILEPATH}" \
    "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" \
    | jq '.content_uri'
)

set_room_avatar() (
  ROOMID=$1
  MXC=$2

  # Build m.room.avatar event
  EVENT_SET_AVATAR=$(jq -n '{ url: $mxc }' --arg mxc "$MXC")

  # Push event
  curl \
    --silent \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${AUTH_TOKEN}" \
    -X PUT \
    --data "${EVENT_SET_AVATAR}" \
    "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
)

15:48:52

Show newer messages


Back to Room ListRoom Version: 6