!GsmxjHfeAYLsTEQmjS:nixos.org

Matrix Meta (Nix)

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

Load older messages


SenderMessageTime
23 Jul 2021
@grahamc:nixos.org@grahamc:nixos.orgyeah11:45:13
@schnecfk:ruhr-uni-bochum.deCRTifiedhttps://matrix.org/_matrix/media/r0/download/nixos.org/f99b43034eae3a82706e86aca15300fda8210b7911:45:41
@schnecfk:ruhr-uni-bochum.deCRTifiedYou can just plug it in the end of that URL11:45:49
@grahamc:nixos.org@grahamc:nixos.orgoh cool11:45:53
@schnecfk:ruhr-uni-bochum.deCRTifiedIn the end, its basically all HTTP11:46:16
@grahamc:nixos.org@grahamc:nixos.orgyeah :) with some manipulation I suppose11:46:25
@schnecfk:ruhr-uni-bochum.deCRTifiedYou can also swap out the host and use the same URL on different homeservers, it will fetch (and cache) the download from the homeserver in the URL11:47:06
@grahamc:nixos.org@grahamc:nixos.org
$ synadm -o json room details '!VjfUzaKsXokUdnQcvP:nixos.org' | jq .
{
  "room_id": "!VjfUzaKsXokUdnQcvP:nixos.org",
  "name": "Nix Python",
  "canonical_alias": "#python:nixos.org",
  "joined_members": 50,
  "joined_local_members": 2,
  "version": "6",
  "creator": "@grahamc:nixos.org",
  "encryption": null,
  "federatable": true,
  "public": true,
  "join_rules": "public",
  "guest_access": null,
  "history_visibility": "world_readable",
  "state_events": 62,
  "avatar": null,
  "topic": "Anything regarding using Python with Nix.",
  "joined_local_devices": 12
}

so iterating over each room id, filtering out rooms with a null avatar, and then figuring out how to upload media and update the room id

11:47:54
@kevincox:matrix.orgkevincoxIf you post an image Element will also let you view source and see the mxc URL. But that isn't too convenient for automation.11:47:54
@grahamc:nixos.org@grahamc:nixos.orgyeah, I wonder what the API is like to upload the image automatically13:24:19
@grahamc:nixos.org@grahamc:nixos.org maybe sumner has ideas here 13:24:25
@kevincox:matrix.orgkevincox

It looks like you could combine:

13:28:30
@sumner:sumnerevans.comsumner
In reply to @grahamc:nixos.org
yeah, I wonder what the API is like to upload the image automatically
Just send it bytes I think: https://matrix.org/docs/spec/client_server/r0.4.0#post-matrix-media-r0-upload
14:18:55
@sumner:sumnerevans.comsumner It gives you the MXC URI as a response. Normally clients will then turn around right away and send it in a message, but in this case you just need to add it to a m.room.avatar state event (https://matrix.org/docs/spec/client_server/r0.4.0#m-room-avatar) which can be created with this endpoint: https://matrix.org/docs/spec/client_server/r0.4.0#put-matrix-client-r0-rooms-roomid-state-eventtype 14:22:37
@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

There are no newer messages yet.


Back to Room ListRoom Version: 6