Skip to content

Commit

Permalink
[IMP] add collaboration capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilien Durieu (edu) committed Apr 16, 2021
1 parent 310ea2a commit 226f205
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 198 deletions.
2 changes: 2 additions & 0 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@

<!-- add mocha -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha384-cPwlPLvBTa3sKAgddT6krw0cJat7egBga3DJepJyrLl4Q9/5WLra3rrnMcyTyOnh" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/10.0.0/sinon.min.js"></script>
<script>
mocha.setup('bdd');
</script>
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 21 additions & 43 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
#!/usr/bin/env python3

from flask import Flask, send_from_directory, request
from flask import Flask, send_from_directory
import flask_restful as restful
from flask_cors import CORS

import time
from flask_socketio import SocketIO, emit

app = Flask(__name__)
CORS(app)
api = restful.Api(app)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

history = [1] # TODO: use an ordered dict instead
history_patch = {1: {
'cursor': {},
'dom': [
{
'type': 'add', 'append': 1, 'id': 1, 'node':
{
'nodeType': 1, 'oid': 1873262997,
'tagName': 'H1',
'children': [{
'nodeType': 3, 'oid': 1550618946,
'textValue': 'A Collaborative Title'
}],
'attributes': {}
}
}
],
'id': 1
}}


@app.route('/')
def index():
return open('dev/index.html').read()
Expand All @@ -43,29 +23,27 @@ def send_js(path):
return send_from_directory('dev', path)


@app.route('/history-push', methods=['POST'])
def history_push():
data = request.get_json()
print(data)
history.append(data['id'])
history_patch[data['id']] = data
return {'status': 200}


class history_get(restful.Resource):
def get(self, oid=0):
index = 0
if oid:
index = history.index(oid) + 1
while index == len(history):
time.sleep(0.1)
history = []

result = [history_patch[x] for x in history[index:]]
print('Get After', oid, ':', [x for x in history[index:]])
return result
@socketio.on('step')
def on_history_step(step):
step_index = len(history)
step['index'] = step_index
history.append(step)
emit('step', step, broadcast=True, json=True)

@socketio.on('init')
def on_init(incoming_history):
if len(history) == 0:
history.extend(incoming_history)
else:
emit('synchronize', history, json=True)

api.add_resource(history_get, '/history-get/<int:oid>')
@socketio.on('needSync')
def on_need_sync():
emit('synchronize', history, json=True)

if __name__ == '__main__':
app.run(port=8000, debug=True)
socketio.run(app)
Loading

0 comments on commit 226f205

Please sign in to comment.