hedgedoc / cli

A tiny CLI for HedgeDoc
GNU Affero General Public License v3.0
150 stars 37 forks source link

Find out how socket.io works in order to edit notes. #10

Open Fumesover opened 5 years ago

Fumesover commented 5 years ago

I'm trying to figure out how to edit a note directly from the command line, unfortunately I can't delete text successfully. So I leave here what I found, and if someone can make it work, it would be a great addition to this client.

First of all, let's authentificate cf #9. (Idk if it's necessary)

codimd login --email user@email.com password

Then, connecting to the socket.

NOTEID=3_jUrZCYRTOtcEAJG6Oywg

# The socket url
URL=$CODIMD_SERVER/socket.io/?noteId=$NOTEID\&EIO=3\&transport=polling

# get the 'io' token
curl -c /tmp/codimd-cookies.txt \
    -b /tmp/codimd-cookies.txt \
    $URL\&t=$(python yeast.py) \
    &> /dev/null

Then, all messages must be passed to $SERVER/socket.io/?noteId=$NOTEID&EIO=3&transport=polling&t=$(python yeast.py)&sid\=$TOKEN, TOKEN is the token we just picked up.

To do so:

TOKEN=$(cat /tmp/codimd-cookies.txt | grep io | awk '{print $7}')

curl -b /tmp/codimd-cookies.txt \
    $URL\&t\=$(python yeast.py)\&sid\=$TOKEN \
    -H "Content-Type: text/plain;charset=UTF-8" \
    -d @msg_0

This will send the data from the file msg_0 to the socket.

As an example, on a fresh new note:

#!/bin/bash

SERVER=https://xxxxxxxxxxxxxx
NOTEID=xxxxxxxxxxxxxxxxxxxxxx

URL=$SERVER/socket.io/?noteId=$NOTEID\&EIO=3\&transport=polling

curl -c /tmp/codimd-cookies.txt \
    -b /tmp/codimd-cookies.txt \
    $URL\&t=$(python yeast.py) \
    &> /dev/null

TOKEN=$(cat /tmp/codimd-cookies.txt | grep io | awk '{print $7}')

curl -b /tmp/codimd-cookies.txt \
    $URL\&t\=$(python yeast.py)\&sid\=$TOKEN \
    -H "Content-Type: text/plain;charset=UTF-8" \
    -d @msg_4 

With msg_4 containing 61:42["operation",0,["test"],{"ranges":[{"anchor":4,"head":4}]}]

Then, by analyzing the different possible messages I think the format is as follows:

$1:42["operation",$2,["$3",$4],{"ranges":[{"anchor":$5,"head":$5}]}]

For example:

This payloads are working when I'm inspecting the network on my browser, but I can't reproduce it (text deletion) by using curl. Even if the socket returns ok to me every time.

I hope I can help, if someone can figure out how to do it. This is the last thing before being able to edit notes from codimd-cli.

PS: here you can find the source file

SISheogorath commented 5 years ago

@Fumesover You are might interested in joining the CodiMD community channel to work with us on the new API. Your input would be very welcome :+1:

As the server currently provides and manages the note state anyway, we should find a better way than reimplementing OT in order to update notes.

pirate commented 4 years ago

If it makes it easier @SISheogorath, the CLI only needs a way to update the entire note contents at once (so no need for OT or individual line/diff-based edits).