switched to sqlite, added support for streamer_ vals

dev
Anton Franzluebbers 2022-06-23 22:27:28 -04:00
parent 39e840b62a
commit a855d8ac65
8 changed files with 273 additions and 262 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ velconnect_backup.sql
discord_bot/graph.png discord_bot/graph.png
discord_bot/config.py discord_bot/config.py
env_win/ env_win/
velconnect.db

View File

@ -9,11 +9,9 @@ CREATE TABLE `Room` (
`modified_by` VARCHAR(64), `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`)),
`tv_url` VARCHAR(1024), `tv_url` VARCHAR(1024),
`carpet_color` VARCHAR(9), `carpet_color` VARCHAR(9),
`room_details` JSON, `room_details` JSON
CHECK (JSON_VALID(`room_details`))
); );
DROP TABLE IF EXISTS `Headset`; DROP TABLE IF EXISTS `Headset`;
CREATE TABLE `Headset` ( CREATE TABLE `Headset` (
@ -35,8 +33,7 @@ CREATE TABLE `Headset` (
-- Stuff like player color, nickname, whiteboard state -- Stuff like player color, nickname, whiteboard state
`user_details` JSON, `user_details` JSON,
`streamer_stream_id` VARCHAR(64), `streamer_stream_id` VARCHAR(64),
`streamer_control_id` VARCHAR(64), `streamer_control_id` VARCHAR(64)
CHECK (JSON_VALID(`user_details`))
); );
DROP TABLE IF EXISTS `APIKey`; DROP TABLE IF EXISTS `APIKey`;
CREATE TABLE `APIKey` ( CREATE TABLE `APIKey` (

View File

@ -26,7 +26,7 @@ oauth2_scheme = OAuth2PasswordBearer(
def api_key_auth(api_key: str = Depends(oauth2_scheme)): def api_key_auth(api_key: str = Depends(oauth2_scheme)):
return True return True
values = query( values = query(
"SELECT * FROM `APIKey` WHERE `key`=%(key)s;", {'key': api_key}) "SELECT * FROM `APIKey` WHERE `key`=:key;", {'key': api_key})
if not (len(values) > 0 and values['auth_level'] < 0): if not (len(values) > 0 and values['auth_level'] < 0):
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
@ -71,7 +71,7 @@ def get_all_headsets():
@router.get('/pair_headset/{pairing_code}') @router.get('/pair_headset/{pairing_code}')
def pair_headset(pairing_code: str): def pair_headset(pairing_code: str):
values = query("SELECT * FROM `Headset` WHERE `pairing_code`=%(pairing_code)s;", values = query("SELECT * FROM `Headset` WHERE `pairing_code`=:pairing_code;",
{'pairing_code': pairing_code}) {'pairing_code': pairing_code})
if len(values) == 1: if len(values) == 1:
print(values[0]['hw_id']) print(values[0]['hw_id'])
@ -81,36 +81,33 @@ def pair_headset(pairing_code: str):
class UpdatePairingCode(BaseModel): class UpdatePairingCode(BaseModel):
hw_id: str hw_id: str
pairing_code: str pairing_code: int
@router.post('/update_pairing_code') @router.post('/update_pairing_code')
def update_paring_code(data: UpdatePairingCode): def update_pairing_code(data: UpdatePairingCode):
"""This also creates a headset if it doesn't exist""" """This also creates a headset if it doesn't exist"""
if 'hw_id' not in data: print("Update pairing code")
return 'Must supply hw_id', 400 print(data)
if 'pairing_code' not in data:
return 'Must supply pairing_code', 400 create_headset(data.hw_id)
insert(""" insert("""
INSERT INTO `Headset`( UPDATE `Headset`
`hw_id`, SET `pairing_code`=:pairing_code, `last_used`=CURRENT_TIMESTAMP
`pairing_code`, WHERE `hw_id`=:hw_id;
`last_used` """, data.dict())
) VALUES (
%(hw_id)s,
%(pairing_code)s,
CURRENT_TIMESTAMP
)
ON DUPLICATE KEY UPDATE
pairing_code=%(pairing_code)s,
last_used=CURRENT_TIMESTAMP;
""", data)
return {'success': True} return {'success': True}
def create_headset(hw_id: str):
insert("""
INSERT OR IGNORE INTO Headset(hw_id) VALUES (:hw_id);
""", {'hw_id': hw_id})
@router.get('/get_state/{hw_id}') @router.get('/get_state/{hw_id}')
def get_headset_details(hw_id: str): def get_headset_details(hw_id: str):
data = get_headset_details_db(hw_id) data = get_headset_details_db(hw_id)
@ -122,7 +119,7 @@ def get_headset_details(hw_id: str):
def get_headset_details_db(hw_id): def get_headset_details_db(hw_id):
headsets = query(""" headsets = query("""
SELECT * FROM `Headset` WHERE `hw_id`=%(hw_id)s; SELECT * FROM `Headset` WHERE `hw_id`=:hw_id;
""", {'hw_id': hw_id}) """, {'hw_id': hw_id})
if len(headsets) == 0: if len(headsets) == 0:
return None return None
@ -134,8 +131,11 @@ def get_headset_details_db(hw_id):
@router.post('/set_headset_details/{hw_id}') @router.post('/set_headset_details/{hw_id}')
def set_headset_details_generic(hw_id: str, data: dict): def set_headset_details_generic(hw_id: str, data: dict):
print("Data:")
print(data) print(data)
# create_headset(hw_id)
allowed_keys = [ allowed_keys = [
'current_room', 'current_room',
'pairing_code', 'pairing_code',
@ -143,13 +143,15 @@ def set_headset_details_generic(hw_id: str, data: dict):
'user_name', 'user_name',
'avatar_url', 'avatar_url',
'user_details', 'user_details',
'streamer_stream_id',
'streamer_control_id',
] ]
for key in data: for key in data:
if key in allowed_keys: if key in allowed_keys:
if key == 'current_room': if key == 'current_room':
create_room(data['current_room']) create_room(data['current_room'])
insert("UPDATE `Headset` SET " + key + insert(f"UPDATE `Headset` SET {key}=:value, modified_by=:sender_id WHERE `hw_id`=:hw_id;", {
"=%(value)s, modified_by=%(sender_id)s WHERE `hw_id`=%(hw_id)s;", {'value': data[key], 'hw_id': hw_id, 'sender_id': data['sender_id']}) 'value': data[key], 'hw_id': hw_id, 'sender_id': data['sender_id']})
return {'success': True} return {'success': True}
@ -167,7 +169,7 @@ def set_room_details_generic(room_id: str, data: dict):
for key in data: for key in data:
if key in allowed_keys: if key in allowed_keys:
insert("UPDATE `Room` SET " + key + insert("UPDATE `Room` SET " + key +
"=%(value)s, modified_by=%(sender_id)s WHERE `room_id`=%(room_id)s;", {'value': data[key], 'room_id': room_id, 'sender_id': data['sender_id']}) "=:value, modified_by=:sender_id WHERE `room_id`=:room_id;", {'value': data[key], 'room_id': room_id, 'sender_id': data['sender_id']})
return {'success': True} return {'success': True}
@ -178,7 +180,7 @@ def get_room_details(room_id: str):
def get_room_details_db(room_id): def get_room_details_db(room_id):
values = query(""" values = query("""
SELECT * FROM `Room` WHERE room_id=%(room_id)s; SELECT * FROM `Room` WHERE room_id=:room_id;
""", {'room_id': room_id}) """, {'room_id': room_id})
if len(values) == 1: if len(values) == 1:
return values[0] return values[0]
@ -188,9 +190,9 @@ def get_room_details_db(room_id):
def create_room(room_id): def create_room(room_id):
insert(""" insert("""
INSERT IGNORE INTO `Room`(room_id) INSERT OR IGNORE INTO `Room`(room_id)
VALUES( VALUES(
%(room_id)s :room_id
); );
""", {'room_id': room_id}) """, {'room_id': room_id})
return {'room_id': room_id} return {'room_id': room_id}
@ -202,12 +204,12 @@ def update_user_count(data: dict):
REPLACE INTO `UserCount` REPLACE INTO `UserCount`
VALUES( VALUES(
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,
%(hw_id)s, :hw_id,
%(room_id)s, :room_id,
%(total_users)s, :total_users,
%(room_users)s, :room_users,
%(version)s, :version,
%(platform)s :platform
); );
""", data) """, data)
return {'success': True} return {'success': True}

View File

@ -1,47 +1,50 @@
# from config import * import sqlite3
import pymysql import os
from pymysql import converters import traceback
from config_mysql import *
def connectToDB(): def create_or_connect():
conv = converters.conversions.copy() db_name = 'velconnect.db'
conv[246] = float # convert decimals to floats create = False
conn = pymysql.connect( if not os.path.exists(db_name, ):
host=MYSQL_DATABASE_HOST, create = True
user=MYSQL_DATABASE_USER,
password=MYSQL_DATABASE_PASSWORD, conn = sqlite3.connect(db_name)
db=MYSQL_DATABASE_DB, conn.row_factory = sqlite3.Row
cursorclass=pymysql.cursors.DictCursor,
conv=conv,
ssl={"fake_flag_to_enable_tls": True},
)
curr = conn.cursor() curr = conn.cursor()
if create:
# create the db
with open('CreateDB.sql', 'r') as f:
curr.executescript(f.read())
conn.set_trace_callback(print)
return conn, curr return conn, curr
def query(query: str, data: dict = None) -> list: def query(query: str, data: dict = None) -> list:
try: try:
conn, curr = connectToDB() conn, curr = create_or_connect()
if data is not None:
curr.execute(query, data) curr.execute(query, data)
values = [dict(row) for row in curr.fetchall()] else:
curr.close() curr.execute(query)
values = curr.fetchall()
conn.close()
return values return values
except Exception: except:
print(curr._last_executed) print(traceback.print_exc())
curr.close() conn.close()
raise raise
def insert(query: str, data: dict = None) -> bool: def insert(query: str, data: dict = None) -> bool:
try: try:
conn, curr = connectToDB() conn, curr = create_or_connect()
curr.execute(query, data) curr.execute(query, data)
conn.commit() conn.commit()
curr.close() conn.close()
return True return True
except Exception: except:
print(curr._last_executed) print(traceback.print_exc())
curr.close() conn.close()
raise raise

View File

@ -1,19 +1,14 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 charecters --> <meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 charecters -->
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script> <script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
</head> </head>
<body>
<rapi-doc
render-style = "read"
primary-color = "#bc1f2d"
show-header = "false"
show-info = "true"
spec-url = "https://connect.vel.workers.dev/api/api_spec.json"
default-schema-tab = 'example'
> <body>
<rapi-doc render-style="read" primary-color="#bc1f2d" show-header="false" show-info="true"
spec-url="https://connect.vel.workers.dev/api/api_spec.json" default-schema-tab='example'>
<div slot="nav-logo" style="display: flex; align-items: center; justify-content: center;"> <div slot="nav-logo" style="display: flex; align-items: center; justify-content: center;">
@ -21,4 +16,5 @@
</div> </div>
</rapi-doc> </rapi-doc>
</body> </body>
</html> </html>

View File

@ -12,7 +12,10 @@
<title>VEL Connect</title> <title>VEL Connect</title>
<link rel="stylesheet" href="/static/css/spectre.min.css"> <link rel="stylesheet" type="text/css" href="/static/css/spectre.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/spectre-exp.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/spectre-icons.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/coloris.min.css">
<script src="/static/js/util.js"></script> <script src="/static/js/util.js"></script>
<style> <style>
.container { .container {
@ -38,14 +41,17 @@
<body> <body>
<div class="container"> <div class="container">
<div id="loading"><br><br><div class="loading loading-lg"></div></div> <div id="loading"><br><br>
<div class="loading loading-lg"></div>
</div>
<div id="failure" style="display: none;"><br><br><br>☹️</div> <div id="failure" style="display: none;"><br><br><br>☹️</div>
<div id="headset_details" style="display: none;"> <div id="headset_details" style="display: none;">
<div class="panel card"> <div class="panel card">
<div class="panel-header text-center"> <div class="panel-header text-center">
<figure class="avatar avatar-lg"><img src="/static/favicons/android-chrome-192x192.png" alt="Avatar"></figure> <figure class="avatar avatar-lg"><img src="/static/favicons/android-chrome-192x192.png"
alt="Avatar"></figure>
<div class="panel-title h5 mt-10">Headset Info</div> <div class="panel-title h5 mt-10">Headset Info</div>
<div class="panel-subtitle hw_id">---</div> <div class="panel-subtitle hw_id">---</div>
</div> </div>
@ -79,7 +85,8 @@
<div class="tile-content"> <div class="tile-content">
<div class="tile-title text-bold">User Name</div> <div class="tile-title text-bold">User Name</div>
<input class="btn user_name" type="text" id="user_name" placeholder="----"> <input class="btn user_name" type="text" id="user_name" placeholder="----">
<button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_user_name" data-tooltip="">Set</button> <button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_user_name"
data-tooltip="">Set</button>
</div> </div>
<div class="tile-action"> <div class="tile-action">
</div> </div>
@ -89,7 +96,8 @@
<div class="tile-content"> <div class="tile-content">
<div class="tile-title text-bold">TV URL</div> <div class="tile-title text-bold">TV URL</div>
<input class="btn tv_url" type="text" id="tv_url" placeholder="----"> <input class="btn tv_url" type="text" id="tv_url" placeholder="----">
<button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_tv_url" data-tooltip="">Set</button> <button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_tv_url"
data-tooltip="">Set</button>
</div> </div>
<div class="tile-action"> <div class="tile-action">
</div> </div>
@ -99,7 +107,8 @@
<div class="tile-content"> <div class="tile-content">
<div class="tile-title text-bold">User Color</div> <div class="tile-title text-bold">User Color</div>
<input class="btn user_color coloris" type="text" id="user_color" placeholder="#ffffff"> <input class="btn user_color coloris" type="text" id="user_color" placeholder="#ffffff">
<button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_user_color" data-tooltip="Set User Color">Set</button> <button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_user_color"
data-tooltip="Set User Color">Set</button>
</div> </div>
<div class="tile-action"> <div class="tile-action">
</div> </div>
@ -109,7 +118,8 @@
<div class="tile-content"> <div class="tile-content">
<div class="tile-title text-bold">Carpet Color</div> <div class="tile-title text-bold">Carpet Color</div>
<input class="btn carpet_color coloris" type="text" id="carpet_color" placeholder="#ffffff"> <input class="btn carpet_color coloris" type="text" id="carpet_color" placeholder="#ffffff">
<button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_carpet_color" data-tooltip="Set Carpet Color">Set</button> <button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_carpet_color"
data-tooltip="Set Carpet Color">Set</button>
</div> </div>
<div class="tile-action"> <div class="tile-action">
</div> </div>
@ -136,7 +146,7 @@
// check cookie // check cookie
let hw_id = getCookie('hw_id'); let hw_id = getCookie('hw_id');
if (hw_id != "") { if (hw_id !== "" && hw_id !== undefined && hw_id !== "undefined") {
httpGetAsync('/api/get_state/' + hw_id, (resp) => { httpGetAsync('/api/get_state/' + hw_id, (resp) => {
console.log(resp); console.log(resp);
@ -160,15 +170,17 @@
failure.style.display = "block"; failure.style.display = "block";
}); });
function setUserData(endpoint, data) { function setUserData(data) {
httpPostAsync('/api/set_headset_details/' + hw_id + '/' + endpoint, data["sender_id"] = "web";
httpPostAsync('/api/set_headset_details/' + hw_id,
data, data,
(resp) => { console.log('success'); }, (resp) => { console.log('success'); },
(status) => { console.log('fail'); } (status) => { console.log('fail'); }
); );
} }
function setRoomData(endpoint, data) { function setRoomData(data) {
httpPostAsync('/api/set_room_details/' + current_room.value + '/' + endpoint, data["sender_id"] = "web";
httpPostAsync('/api/set_room_details/' + current_room.value,
data, data,
(resp) => { console.log('success'); }, (resp) => { console.log('success'); },
(status) => { console.log('fail'); } (status) => { console.log('fail'); }
@ -176,19 +188,19 @@
} }
set_room_id.addEventListener('click', () => { set_room_id.addEventListener('click', () => {
setUserData('current_room', { "current_room": current_room.value }); setUserData({ "current_room": current_room.value });
}); });
document.getElementById('set_user_color').addEventListener('click', () => { document.getElementById('set_user_color').addEventListener('click', () => {
setUserData('user_color', { "user_color": document.getElementById('user_color').value }); setUserData({ "user_color": document.getElementById('user_color').value });
}); });
document.getElementById('set_user_name').addEventListener('click', () => { document.getElementById('set_user_name').addEventListener('click', () => {
setUserData('user_name', { "user_name": document.getElementById('user_name').value }); setUserData({ "user_name": document.getElementById('user_name').value });
}); });
document.getElementById('set_tv_url').addEventListener('click', () => { document.getElementById('set_tv_url').addEventListener('click', () => {
setRoomData('tv_url', { "tv_url": document.getElementById('tv_url').value }); setRoomData({ "tv_url": document.getElementById('tv_url').value });
}); });
document.getElementById('set_carpet_color').addEventListener('click', () => { document.getElementById('set_carpet_color').addEventListener('click', () => {
setRoomData('carpet_color', { "carpet_color": document.getElementById('carpet_color').value }); setRoomData({ "carpet_color": document.getElementById('carpet_color').value });
}); });
} else { } else {

View File

@ -2,11 +2,11 @@
<head> <head>
<link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-touch-icon.png"> <link rel="apple-touch-icon" sizes="180x180" href="/static/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="32x32" href="/static/favicons/static/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png"> <link rel="icon" type="image/png" sizes="16x16" href="/static/favicons/static/favicon-16x16.png">
<link rel="manifest" href="/favicons/site.webmanifest"> <link rel="manifest" href="/static/favicons/site.webmanifest">
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#5bbad5"> <link rel="mask-icon" href="/static/favicons/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#b91d47"> <meta name="msapplication-TileColor" content="#b91d47">
<meta name="theme-color" content="#ffffff"> <meta name="theme-color" content="#ffffff">