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/config.py
env_win/
velconnect.db

View File

@ -9,11 +9,9 @@ CREATE TABLE `Room` (
`modified_by` VARCHAR(64),
-- array of hw_ids of users allowed. Always includes the owner. Null for public
`whitelist` JSON,
CHECK (JSON_VALID(`whitelist`)),
`tv_url` VARCHAR(1024),
`carpet_color` VARCHAR(9),
`room_details` JSON,
CHECK (JSON_VALID(`room_details`))
`room_details` JSON
);
DROP TABLE IF EXISTS `Headset`;
CREATE TABLE `Headset` (
@ -35,8 +33,7 @@ CREATE TABLE `Headset` (
-- Stuff like player color, nickname, whiteboard state
`user_details` JSON,
`streamer_stream_id` VARCHAR(64),
`streamer_control_id` VARCHAR(64),
CHECK (JSON_VALID(`user_details`))
`streamer_control_id` VARCHAR(64)
);
DROP TABLE IF EXISTS `APIKey`;
CREATE TABLE `APIKey` (

View File

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

View File

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

View File

@ -1,24 +1,20 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 charecters -->
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
</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'
<html>
>
<head>
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 charecters -->
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
</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'>
<div slot="nav-logo" style="display: flex; align-items: center; justify-content: center;">
<img src = "/static/favicons/android-chrome-256x256.png" style="width:10em; margin: auto;" />
</div>
</rapi-doc>
</body>
</html>
<div slot="nav-logo" style="display: flex; align-items: center; justify-content: center;">
<img src="/static/favicons/android-chrome-256x256.png" style="width:10em; margin: auto;" />
</div>
</rapi-doc>
</body>
</html>

View File

@ -12,7 +12,10 @@
<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>
<style>
.container {
@ -38,188 +41,197 @@
<body>
<div class="container">
<div id="loading"><br><br><div class="loading loading-lg"></div></div>
<div id="failure" style="display: none;"><br><br><br>☹️</div>
<div id="headset_details" style="display: none;">
<div class="panel card">
<div class="panel-header text-center">
<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-subtitle hw_id">---</div>
<div id="loading"><br><br>
<div class="loading loading-lg"></div>
</div>
<div class="panel-body">
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">Current Room</div>
<input class="btn current_room" type="text" id="current_room" placeholder="----">
<div id="failure" style="display: none;"><br><br><br>☹️</div>
<div id="headset_details" style="display: none;">
<div class="panel card">
<div class="panel-header text-center">
<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-subtitle hw_id">---</div>
</div>
<div class="tile-action">
<button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_room_id"
data-tooltip="Set Room ID">Set</button>
<div class="panel-body">
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">Current Room</div>
<input class="btn current_room" type="text" id="current_room" placeholder="----">
</div>
<div class="tile-action">
<button class="btn btn-primary btn-lg tooltip tooltip-left" id="set_room_id"
data-tooltip="Set Room ID">Set</button>
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">First Seen</div>
<div class="tile-subtitle date_created">---</div>
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">Last Login</div>
<div class="tile-subtitle last_used">---</div>
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">User Name</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">TV URL</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">User Color</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">Carpet Color</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<br>
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">First Seen</div>
<div class="tile-subtitle date_created">---</div>
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">Last Login</div>
<div class="tile-subtitle last_used">---</div>
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">User Name</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">TV URL</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">User Color</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<div class="tile tile-centered">
<div class="tile-content">
<div class="tile-title text-bold">Carpet Color</div>
<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>
</div>
<div class="tile-action">
</div>
</div>
<br>
<br>
</div>
</div>
</div>
<script type="text/javascript" src="/static/js/coloris.min.js"></script>
<script>
<script type="text/javascript" src="/static/js/coloris.min.js"></script>
<script>
let submit_button = document.getElementById('submit_pairing_code');
let pair_code_input = document.getElementById('pair_code');
let loading = document.getElementById('loading');
let enter_pairing_id = document.getElementById('enter_pairing_id');
let headset_details = document.getElementById('headset_details');
let hw_id_field = document.getElementById('hw_id');
let failure = document.getElementById('failure');
let set_room_id = document.getElementById('set_room_id');
let current_room = document.getElementById('current_room');
let submit_button = document.getElementById('submit_pairing_code');
let pair_code_input = document.getElementById('pair_code');
let loading = document.getElementById('loading');
let enter_pairing_id = document.getElementById('enter_pairing_id');
let headset_details = document.getElementById('headset_details');
let hw_id_field = document.getElementById('hw_id');
let failure = document.getElementById('failure');
let set_room_id = document.getElementById('set_room_id');
let current_room = document.getElementById('current_room');
// check cookie
let hw_id = getCookie('hw_id');
if (hw_id != "") {
// check cookie
let hw_id = getCookie('hw_id');
if (hw_id !== "" && hw_id !== undefined && hw_id !== "undefined") {
httpGetAsync('/api/get_state/' + hw_id, (resp) => {
console.log(resp);
let respData = JSON.parse(resp);
httpGetAsync('/api/get_state/' + hw_id, (resp) => {
console.log(resp);
let respData = JSON.parse(resp);
writeClass('hw_id', respData['user']['hw_id']);
writeValue('current_room', respData['user']['current_room']);
writeClass('date_created', respData['user']['date_created'] + "<br>" + timeSinceString(respData['user']['date_created']) + " ago");
writeClass('last_used', respData['user']['last_used'] + "<br>" + timeSinceString(respData['user']['last_used']) + " ago");
writeValue('user_color', respData['user']['user_color']);
writeValue('user_name', respData['user']['user_name']);
if (respData['room']) {
writeValue('tv_url', respData['room']['tv_url']);
writeValue('carpet_color', respData['room']['carpet_color']);
writeClass('hw_id', respData['user']['hw_id']);
writeValue('current_room', respData['user']['current_room']);
writeClass('date_created', respData['user']['date_created'] + "<br>" + timeSinceString(respData['user']['date_created']) + " ago");
writeClass('last_used', respData['user']['last_used'] + "<br>" + timeSinceString(respData['user']['last_used']) + " ago");
writeValue('user_color', respData['user']['user_color']);
writeValue('user_name', respData['user']['user_name']);
if (respData['room']) {
writeValue('tv_url', respData['room']['tv_url']);
writeValue('carpet_color', respData['room']['carpet_color']);
}
loading.style.display = "none";
headset_details.style.display = "block";
}, (status) => {
loading.style.display = "none";
failure.style.display = "block";
});
function setUserData(data) {
data["sender_id"] = "web";
httpPostAsync('/api/set_headset_details/' + hw_id,
data,
(resp) => { console.log('success'); },
(status) => { console.log('fail'); }
);
}
function setRoomData(data) {
data["sender_id"] = "web";
httpPostAsync('/api/set_room_details/' + current_room.value,
data,
(resp) => { console.log('success'); },
(status) => { console.log('fail'); }
);
}
set_room_id.addEventListener('click', () => {
setUserData({ "current_room": current_room.value });
});
document.getElementById('set_user_color').addEventListener('click', () => {
setUserData({ "user_color": document.getElementById('user_color').value });
});
document.getElementById('set_user_name').addEventListener('click', () => {
setUserData({ "user_name": document.getElementById('user_name').value });
});
document.getElementById('set_tv_url').addEventListener('click', () => {
setRoomData({ "tv_url": document.getElementById('tv_url').value });
});
document.getElementById('set_carpet_color').addEventListener('click', () => {
setRoomData({ "carpet_color": document.getElementById('carpet_color').value });
});
} else {
window.location.href = "/pair";
}
loading.style.display = "none";
headset_details.style.display = "block";
}, (status) => {
loading.style.display = "none";
failure.style.display = "block";
});
function setUserData(endpoint, data) {
httpPostAsync('/api/set_headset_details/' + hw_id + '/' + endpoint,
data,
(resp) => { console.log('success'); },
(status) => { console.log('fail'); }
);
}
function setRoomData(endpoint, data) {
httpPostAsync('/api/set_room_details/' + current_room.value + '/' + endpoint,
data,
(resp) => { console.log('success'); },
(status) => { console.log('fail'); }
);
}
set_room_id.addEventListener('click', () => {
setUserData('current_room', { "current_room": current_room.value });
});
document.getElementById('set_user_color').addEventListener('click', () => {
setUserData('user_color', { "user_color": document.getElementById('user_color').value });
});
document.getElementById('set_user_name').addEventListener('click', () => {
setUserData('user_name', { "user_name": document.getElementById('user_name').value });
});
document.getElementById('set_tv_url').addEventListener('click', () => {
setRoomData('tv_url', { "tv_url": document.getElementById('tv_url').value });
});
document.getElementById('set_carpet_color').addEventListener('click', () => {
setRoomData('carpet_color', { "carpet_color": document.getElementById('carpet_color').value });
});
} else {
window.location.href = "/pair";
}
Coloris({
el: '.coloris',
swatches: [
'#264653',
'#2a9d8f',
'#e9c46a',
'#f4a261',
'#e76f51',
'#d62828',
'#023e8a',
'#0077b6',
'#0096c7',
'#00b4d8',
'#48cae4',
]
});
Coloris({
el: '.coloris',
swatches: [
'#264653',
'#2a9d8f',
'#e9c46a',
'#f4a261',
'#e76f51',
'#d62828',
'#023e8a',
'#0077b6',
'#0096c7',
'#00b4d8',
'#48cae4',
]
});
</script>
</div>
</script>
</div>
</body>
</html>

View File

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