Onionr/onionr/static-data/default-plugins/chat/controlapi.py

71 lines
2.2 KiB
Python
Raw Normal View History

2019-03-27 17:38:46 +00:00
'''
2019-06-20 07:59:32 +00:00
Onionr - Private P2P Communication
2019-03-27 17:38:46 +00:00
HTTP endpoints for controlling IMs
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
2019-03-31 04:50:54 +00:00
import json
from flask import Response, request, redirect, Blueprint, send_from_directory
2019-07-20 00:41:15 +00:00
import deadsimplekv as simplekv
import filepaths
2019-07-27 21:56:06 +00:00
flask_blueprint = Blueprint('chat_control', __name__)
2019-07-20 06:02:30 +00:00
key_store = simplekv.DeadSimpleKV(filepaths.cached_storage, refresh_seconds=5)
2019-07-27 21:56:06 +00:00
@flask_blueprint.route('/chatapi/ping')
2019-03-27 17:38:46 +00:00
def ping():
return 'pong!'
2019-03-31 04:50:54 +00:00
2019-07-27 21:56:06 +00:00
@flask_blueprint.route('/chatapi/send/<peer>', methods=['POST'])
2019-03-31 04:50:54 +00:00
def send_message(peer):
data = request.get_json(force=True)
2019-07-20 00:41:15 +00:00
key_store.refresh()
existing = key_store.get('s' + peer)
2019-03-31 04:50:54 +00:00
if existing is None:
existing = []
existing.append(data)
2019-07-20 00:41:15 +00:00
key_store.put('s' + peer, existing)
key_store.flush()
2019-03-31 04:50:54 +00:00
return Response('success')
2019-07-27 21:56:06 +00:00
@flask_blueprint.route('/chatapi/gets/<peer>')
2019-03-31 04:50:54 +00:00
def get_sent(peer):
2019-07-20 00:41:15 +00:00
sent = key_store.get('s' + peer)
2019-03-31 04:50:54 +00:00
if sent is None:
sent = []
return Response(json.dumps(sent))
2019-07-27 21:56:06 +00:00
@flask_blueprint.route('/chatapi/addrec/<peer>', methods=['POST'])
2019-03-31 04:50:54 +00:00
def add_rec(peer):
data = request.get_json(force=True)
2019-07-20 00:41:15 +00:00
key_store.refresh()
existing = key_store.get('r' + peer)
2019-03-31 04:50:54 +00:00
if existing is None:
existing = []
existing.append(data)
2019-07-20 00:41:15 +00:00
key_store.put('r' + peer, existing)
key_store.flush()
2019-03-31 04:50:54 +00:00
return Response('success')
2019-07-27 21:56:06 +00:00
@flask_blueprint.route('/chatapi/getrec/<peer>')
2019-03-31 04:50:54 +00:00
def get_messages(peer):
2019-07-20 00:41:15 +00:00
key_store.refresh()
existing = key_store.get('r' + peer)
2019-03-31 04:50:54 +00:00
if existing is None:
existing = []
else:
existing = list(existing)
2019-07-20 00:41:15 +00:00
key_store.delete('r' + peer)
return Response(json.dumps(existing))