| 23 Jul 2021 |
CRTified | That's significantly cleaner | 15:37:52 |
sumner | * Alternative is:
mimetype=$(file --mime-type $FILEPATH | sed 's/.*: //g')
| 15:38:14 |
CRTified | * #!/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 |
CRTified | Added it :) | 15:40:45 |
CRTified | Just the filename that's wrong now :D | 15:41:15 |
Leon | You should add file as a build input ;) | 15:41:21 |
CRTified | * #!/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 |
CRTified | 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 | In reply to @schnecfk:ruhr-uni-bochum.de Just the filename that's wrong now :D filename is optional | 15:42:06 |
CRTified | * #!/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 |
CRTified | In that case, poof :D | 15:42:25 |
sumner | 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 | May be worth uploading that as a file so that it's easy to find in the sidebar. | 15:43:59 |
CRTified | In that case I'll run shellcheck on it | 15:44:36 |
@grahamc:nixos.org | https://matrix.org/_matrix/media/r0/download/nixos.org/ab7db8fb43cc002b48b03de4cf04f0ec7c755585 | 15:48:02 |
@grahamc:nixos.org | nice! | 15:48:25 |
@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 |
| Jez (he/him) left the room. | 15:49:04 |
CRTified | That's a clean style for a shell script 👀 | 15:50:20 |
CRTified | looks good to go | 15:50:39 |
CRTified | grahamc (he/him): maybe keep a list of the rooms that you'll assign a generated image for. That way, we could replace them easier with something prettier (autogenerated) afterwards. I was thinking about doing something with the nix logo, e.g. assigning colors from the hash of the room name or something like this | 15:57:24 |
@grahamc:nixos.org | that is a cool idea | 15:58:25 |
CRTified | I'll try something this evening (so in 2-3 hours probably) | 15:59:12 |
CRTified | * I'll try something this evening (so in 2-3 hours probably), but that might help to keep the nix "branding" present in the room avatars 😁 | 15:59:39 |
@grahamc:nixos.org | that would be cool | 16:03:18 |
@grahamc:nixos.org | I'm not going to roll it out widely yet anyway, I want to give some people time to think about it | 16:03:29 |
@grahamc:nixos.org | applied to !gKfPYWNBcIabzMfEvn:nixos.org
!pbdtvoHxUGLhcEvnlu:nixos.org
| 16:04:32 |
@grahamc:nixos.org | hrm I tried to add a URL parameter to the community room's avatar but it breaks the rendering: "avatar": "mxc://nixos.org/721c442bc81f9fedaeada3ad89af693666619889?autogenerated=1" | 16:11:43 |
CRTified | In reply to @grahamc:nixos.org hrm I tried to add a URL parameter to the community room's avatar but it breaks the rendering: "avatar": "mxc://nixos.org/721c442bc81f9fedaeada3ad89af693666619889?autogenerated=1" I wonder if the mimetype is preserved - in that case you could use a parameter there? 🤔 | 16:29:12 |
CRTified | Looks like it is preserved - both in the event (where you can supply it under the info->mimetype key), and when uploading the image (in that case it's the Content-Type header) | 16:36:09 |