diff --git a/models.py b/models.py index 6721d78..ad80716 100644 --- a/models.py +++ b/models.py @@ -1,29 +1,33 @@ +from datetime import datetime, timezone + from fastapi import Query -from pydantic import BaseModel +from pydantic import BaseModel, Field class CreateCopilotData(BaseModel): - user: str = Query(None) - title: str = Query(None) - lnurl_toggle: int = Query(0) - wallet: str = Query(None) - animation1: str = Query(None) - animation2: str = Query(None) - animation3: str = Query(None) - animation1threshold: int = Query(0) - animation2threshold: int = Query(0) - animation3threshold: int = Query(0) - animation1webhook: str = Query(None) - animation2webhook: str = Query(None) - animation3webhook: str = Query(None) - lnurl_title: str = Query(None) - show_message: int = Query(0) - show_ack: int = Query(0) - show_price: str = Query(None) - amount_made: int = Query(0) - timestamp: int = Query(0) - fullscreen_cam: int = Query(0) - iframe_url: str = Query(None) + user: str | None = None + title: str | None = None + lnurl_toggle: int | None = 0 + wallet: str | None = None + animation1: str | None = None + animation2: str | None = None + animation3: str | None = None + animation1threshold: int | None = 0 + animation2threshold: int | None = 0 + animation3threshold: int | None = 0 + animation1webhook: str | None = None + animation2webhook: str | None = None + animation3webhook: str | None = None + lnurl_title: str | None = None + show_message: int | None = 0 + show_ack: int | None = 0 + show_price: str | None = None + amount_made: int | None = 0 + timestamp: datetime | None = Field( + default_factory=lambda: datetime.now(timezone.utc) + ) + fullscreen_cam: int | None = 0 + iframe_url: str | None = None class Copilot(BaseModel): @@ -46,6 +50,6 @@ class Copilot(BaseModel): show_ack: int show_price: str | None amount_made: int - timestamp: int + timestamp: datetime fullscreen_cam: int iframe_url: str | None diff --git a/templates/copilot/chat.html b/templates/copilot/chat.html index 5efd4f1..520ae55 100644 --- a/templates/copilot/chat.html +++ b/templates/copilot/chat.html @@ -79,31 +79,21 @@ initTrollBox() { this.troll_box = !this.troll_box - if (location.protocol !== 'http:') { - this.chatUrl = - 'wss://' + - document.domain + - ':' + - location.port + - '/api/v1/ws/' + - this.chat_id - } else { - this.chatUrl = - 'ws://' + - document.domain + - ':' + - location.port + - '/api/v1/ws/' + - this.chat_id - } - this.connectionchat = new WebSocket(this.chatUrl) + const url = new URL(window.location) + url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' + url.pathname = `/api/v1/ws/${chatId}` + + this.connectionchat = new WebSocket(url) + this.chatUrl = url.toString() this.connectionchat.onmessage = e => { + console.log(e.data) this.trollbox.push(e.data) } this.connectionchat.onopen = () => this.launch } }, created() { + this.chatId = '{{ chat_id }}' this.initTrollBox() } }) diff --git a/templates/copilot/compose.html b/templates/copilot/compose.html index 4e41b29..ac1b9c3 100644 --- a/templates/copilot/compose.html +++ b/templates/copilot/compose.html @@ -22,7 +22,7 @@
- +
Trollbox
@@ -75,12 +75,12 @@ :url="url" :show-buttons="false" > -
+

{ res = e.data.split('-') if (res[0].length < 1 || res[1].length < 1) { @@ -335,27 +323,34 @@ return data => (pending = run(data)) })() - if (location.protocol !== 'http:') { - localUrl = - 'wss://' + - document.domain + - ':' + - location.port + - '/api/v1/ws/' + - this.copilot.id - } else { - localUrl = - 'ws://' + - document.domain + - ':' + - location.port + - '/api/v1/ws/' + - this.copilot.id - } + const localUrl = new URL(window.location) + localUrl.protocol = localUrl.protocol === 'https:' ? 'wss:' : 'ws:' + localUrl.pathname = `/api/v1/ws/${this.copilot.id}` + + // if (location.protocol !== 'http:') { + // localUrl = + // 'wss://' + + // document.domain + + // ':' + + // location.port + + // '/api/v1/ws/' + + // this.copilot.id + // } else { + // localUrl = + // 'ws://' + + // document.domain + + // ':' + + // location.port + + // '/api/v1/ws/' + + // this.copilot.id + // } this.connection = new WebSocket(localUrl) this.connection.onmessage = e => { console.log(e) - res = e.data.replace(' ', '').split('-') + res = e.data + .replace(' ', '') + .split('-') + .map(s => s.trim()) if (res[0] == 'rocket') { addTask(['40%', '/copilot/static/rocket.gif', res[1]]) } @@ -376,7 +371,7 @@ } if (res[0] == 'trollbox') { this.chat_id = res[1] - this.chatLink = '/copilot/chat/' + this.chat_id + this.chatLink = `/copilot/chat/${this.chat_id}` this.initTrollBox() } if (res[0] == 'trollboxchat') { diff --git a/templates/copilot/panel.html b/templates/copilot/panel.html index 7821da9..fccf93e 100644 --- a/templates/copilot/panel.html +++ b/templates/copilot/panel.html @@ -92,7 +92,7 @@ - + @@ -153,11 +153,7 @@ LNbits.api .request( 'GET', - '/copilot/api/v1/copilot/ws/' + - this.copilot.id + - '/' + - this.chatId + - '/trollbox' + `/copilot/api/v1/copilot/ws/${this.copilot.id}/${this.chatId}/trollbox` ) .then(response1 => { Quasar.Notify.create({