aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/ws/bot.py
blob: 24af74df26ab340709868ab827c9dfcfcc179286 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import logging

from geventwebsocket.websocket import WebSocket

from pysite.constants import BOT_API_KEY
from pysite.mixins import DBMixin
from pysite.websockets import WS


class BotWebsocket(WS, DBMixin):
    path = "/bot"
    name = "ws.bot"
    table_name = "bot_events"

    do_changefeed = True

    def __init__(self, socket: WebSocket):
        super().__init__(socket)
        self.log = logging.getLogger()

    def on_open(self):
        self.log.debug("Bot | WS opened.")

    def on_message(self, message):
        self.log.debug(f"Bot | Message: {message}")

        try:
            message = json.loads(message)
        except json.JSONDecodeError:
            self.send_json({"error": "Message was not valid JSON"})
            return self.socket.close()

        action = message["action"]

        if action == "login":
            if message["key"] != BOT_API_KEY:
                return self.socket.close()

            self.do_changefeed = True

            for document in self.db.changes(self.table_name, include_initial=True, include_types=True):
                if not self.do_changefeed:
                    break

                if document["type"] not in ["add", "initial"]:
                    continue

                self.send_json({"action": "event", "event": document["new_val"]})
                self.db.delete(self.table_name, document["id"])

        self.send_json({"error": f"Unknown action: {action}"})

    def on_close(self):
        self.log.debug("Bot | WS closed.")
        self.do_changefeed = False

    def send_json(self, data):
        return self.send(json.dumps(data))