💾 Archived View for cadence.moe › documents › matrix-start-chat.fish captured on 2023-05-24 at 17:58:43.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

function matrix-start-chat
    set other_handles $argv
    set my_handle @cadence:cadence.moe
    set server https://matrix.cadence.moe
    set access_token '' # YOU NEED TO GET A MATRIX ACCESS TOKEN FOR YOUR ACCOUNT AND PUT IT HERE. You can find an access token in Element's settings -> about -> access token.
    set auth "Authorization: Bearer $access_token"

    echo -n 'Creating chat... '
    # do the creation
    set create_room_result (curl -Ss -H 'Content-Type: application/json' -H $auth "$server/_matrix/client/v3/createRoom" --data-binary '{"invite":['(string join , \"$other_handles\")'],"is_direct":true,"preset":"trusted_private_chat"}'); or return
    # figure out whether the request was successful
    # if it was, the server will give back the ID of the new room
    set room_id (string sub -- $create_room_result | jq -r .room_id); or return
    test $room_id != null; or return
    echo $room_id

    # the room is created. now, update own account data

    echo 'Querying account data...'
    set m_direct (curl -Ss -H $auth "$server/_matrix/client/v3/user/$my_handle/account_data/m.direct")

    echo 'Marking room as direct on this side...'
    # for each user
    for handle in $other_handles
        # make the new list of rooms that are directs with the user
        set new_list (string sub -- $m_direct | jq -c (printf '.["%s"] + ["%s"]' $handle $room_id)); or return
        # update the m_direct variable
        set m_direct (string sub -- $m_direct | jq -c (printf '. + {"%s":%s}' $handle $new_list)); or return
    end

    # check we didn't erase the entire account data
    if test (string length -- $m_direct) -le 20
        echo 'Oops. Looks like the entire account data would have been erased. This would have been written:'
        string sub -- $m_direct
        echo 'Better to not upload that.'
        return 1
    end

    echo 'Saving...'
    curl -o /dev/null -Ss -H 'Content-Type: application/json' -H $auth "$server/_matrix/client/v3/user/$my_handle/account_data/m.direct" -X PUT --data-binary (string sub -- $m_direct | string collect); or return

    echo 'Ok, seems like it worked!'
end