updated db schema to include modifier, simplified modifying endpoint

dev
Anton Franzluebbers 2022-01-30 23:04:58 -05:00
parent a605c872f1
commit c86e43010f
4 changed files with 44 additions and 43 deletions

View File

@ -5,6 +5,8 @@ CREATE TABLE `Room` (
`last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- Can be null if no owner -- Can be null if no owner
`owner` VARCHAR(64), `owner` VARCHAR(64),
-- The last source to change this object
`modified_by` VARCHAR(64),
-- array of hw_ids of users allowed. Always includes the owner. Null for public -- array of hw_ids of users allowed. Always includes the owner. Null for public
`whitelist` JSON, `whitelist` JSON,
CHECK (JSON_VALID(`whitelist`)), CHECK (JSON_VALID(`whitelist`)),
@ -16,6 +18,8 @@ CREATE TABLE `Room` (
DROP TABLE IF EXISTS `Headset`; DROP TABLE IF EXISTS `Headset`;
CREATE TABLE `Headset` ( CREATE TABLE `Headset` (
`hw_id` VARCHAR(64) NOT NULL PRIMARY KEY, `hw_id` VARCHAR(64) NOT NULL PRIMARY KEY,
-- The last source to change this object
`modified_by` VARCHAR(64),
-- The room_id of the owned room -- The room_id of the owned room
`owned_room` VARCHAR(64), `owned_room` VARCHAR(64),
-- The room_id of the current room. Can be null if room not specified -- The room_id of the current room. Can be null if room not specified
@ -44,9 +48,12 @@ CREATE TABLE `APIKey` (
); );
DROP TABLE IF EXISTS `UserCount`; DROP TABLE IF EXISTS `UserCount`;
CREATE TABLE `UserCount` ( CREATE TABLE `UserCount` (
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP PRIMARY KEY, `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`hw_id` VARCHAR(64) NOT NULL, `hw_id` VARCHAR(64) NOT NULL,
`room_id` VARCHAR(64) NOT NULL, `room_id` VARCHAR(64) NOT NULL,
`total_users` INT NOT NULL DEFAULT 0, `total_users` INT NOT NULL DEFAULT 0,
`room_users` INT NOT NULL DEFAULT 0 `room_users` INT NOT NULL DEFAULT 0,
`version` VARCHAR(32) DEFAULT "0",
`platform` VARCHAR(64) DEFAULT "none",
PRIMARY KEY (`timestamp`, `hw_id`)
); );

View File

@ -5,7 +5,6 @@ from velconnect.logger import logger
from time import strftime from time import strftime
import traceback import traceback
def create_app(): def create_app():
app = Flask( app = Flask(
__name__, __name__,

View File

@ -122,8 +122,6 @@ def get_headset_details_db(hw_id):
return {'user': headsets[0], 'room': room} return {'user': headsets[0], 'room': room}
@bp.route('/set_headset_details/<hw_id>', methods=['POST']) @bp.route('/set_headset_details/<hw_id>', methods=['POST'])
@require_api_key(10) @require_api_key(10)
def set_headset_details_generic(hw_id): def set_headset_details_generic(hw_id):
@ -131,24 +129,24 @@ def set_headset_details_generic(hw_id):
logger.error(data) logger.error(data)
conn, curr = connectToDB() conn, curr = connectToDB()
allowed_keys = [
'current_room',
'pairing_code',
'user_color',
'user_name',
'avatar_url',
'user_details',
]
try: try:
for key in data: for key in data:
# protected keys if key in allowed_keys:
if key == 'hw_id' \ if key == 'current_room':
or key == 'owned_room' \ create_room(data['current_room'])
or key == 'date_created' \ query = "UPDATE `Headset` SET " + key + "=%(value)s, modified_by=%(sender_id)s WHERE `hw_id`=%(hw_id)s;"
or key == 'last_used' \ curr.execute(query, {'value': data[key], 'hw_id': hw_id, 'sender_id': data['sender_id']})
: conn.commit()
continue
if key == 'current_room':
create_room(data['current_room'])
query = """
UPDATE `Headset` SET `%(key)s`=%(value)s WHERE `hw_id`=%(hw_id)s;
"""
curr.execute(query, {'key': key, 'value': data[key], 'hw_id': hw_id})
conn.commit()
except Exception as e: except Exception as e:
logger.error(curr._last_executed)
curr.close() curr.close()
logger.error(e) logger.error(e)
return 'Error', 400 return 'Error', 400
@ -160,8 +158,6 @@ def set_headset_details_generic(hw_id):
return response return response
@bp.route('/set_room_details/<room_id>', methods=['POST']) @bp.route('/set_room_details/<room_id>', methods=['POST'])
@require_api_key(10) @require_api_key(10)
def set_room_details_generic(room_id): def set_room_details_generic(room_id):
@ -169,22 +165,21 @@ def set_room_details_generic(room_id):
logger.error(data) logger.error(data)
conn, curr = connectToDB() conn, curr = connectToDB()
allowed_keys = [
'modified_by',
'whitelist',
'tv_url',
'carpet_color',
'room_details',
]
try: try:
for key in data: for key in data:
# protected keys if key in allowed_keys:
if key == 'room_id' \ query = "UPDATE `Room` SET " + key + "=%(value)s, modified_by=%(sender_id)s WHERE `room_id`=%(room_id)s;"
or key == 'date_created' \ curr.execute(query, {'value': data[key], 'room_id': room_id, 'sender_id': data['sender_id']})
or key == 'last_modified' \ conn.commit()
or key == 'owner' \
:
continue
query = """
UPDATE `Room` SET `%(key)s`=%(value)s WHERE `room_id`=%(room_id)s;
"""
curr.execute(query, {'key': key, 'value': data[key], 'room_id': room_id})
conn.commit()
except Exception as e: except Exception as e:
logger.error(curr._last_executed)
curr.close() curr.close()
logger.error(e) logger.error(e)
return 'Error', 400 return 'Error', 400
@ -413,8 +408,6 @@ def set_room_details_carpet_color(room_id):
return response return response
@bp.route('/update_user_count', methods=['POST']) @bp.route('/update_user_count', methods=['POST'])
@require_api_key(10) @require_api_key(10)
def update_user_count(): def update_user_count():
@ -426,7 +419,9 @@ def update_user_count():
%(hw_id)s, %(hw_id)s,
%(room_id)s, %(room_id)s,
%(total_users)s, %(total_users)s,
%(room_users)s %(room_users)s,
%(version)s,
%(platform)s
); );
""" """
data = request.json data = request.json
@ -438,8 +433,6 @@ def update_user_count():
return response return response
@bp.route('/get_user_count', methods=['GET']) @bp.route('/get_user_count', methods=['GET'])
def get_user_count(): def get_user_count():
hours = request.args.get('hours', 24) hours = request.args.get('hours', 24)

View File

@ -86,7 +86,7 @@
<div class="tile tile-centered"> <div class="tile tile-centered">
<div class="tile-content"> <div class="tile-content">
<div class="tile-title text-bold">Avatar URL</div> <div class="tile-title text-bold">Avatar URL</div>
<div class="tile-subtitle"><a href="https://vr.readyplayer.me" target="blank">Create New Avatar</a></div> <div class="tile-subtitle"><a href="https://convrged.readyplayer.me" target="blank">Create New Avatar</a></div>
<input class="btn avatar_url" type="text" id="avatar_url" placeholder="----"> <input class="btn avatar_url" type="text" id="avatar_url" placeholder="----">
</div> </div>
<div class="tile-action"> <div class="tile-action">
@ -202,6 +202,7 @@
}); });
function setUserData(data) { function setUserData(data) {
data["sender_id"] = Math.floor(Math.random()*10000000);
httpPostAsync('{% include "api_url.html" %}/api/set_headset_details/' + hw_id, httpPostAsync('{% include "api_url.html" %}/api/set_headset_details/' + hw_id,
data, data,
(resp) => { console.log('success'); }, (resp) => { console.log('success'); },
@ -209,6 +210,7 @@
); );
} }
function setRoomData(data) { function setRoomData(data) {
data["sender_id"] = Math.floor(Math.random()*10000000);
httpPostAsync('{% include "api_url.html" %}/api/set_room_details/' + current_room.value, httpPostAsync('{% include "api_url.html" %}/api/set_room_details/' + current_room.value,
data, data,
(resp) => { console.log('success'); }, (resp) => { console.log('success'); },