File size: 4,241 Bytes
f38d374
 
 
 
 
 
f3f7821
f38d374
 
c91a847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f38d374
c91a847
f38d374
f3f7821
500ffbd
 
f3f7821
 
 
 
500ffbd
 
 
f3f7821
d8bd52e
500ffbd
c28382a
500ffbd
 
 
 
 
 
 
 
d8bd52e
 
 
 
 
 
 
 
 
 
 
 
830fb2e
97be64b
830fb2e
 
 
052d770
 
712b502
052d770
 
 
 
 
 
 
 
 
712b502
052d770
8e6cd59
 
 
 
712b502
 
 
052d770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef89e5c
052d770
 
 
 
 
 
 
830fb2e
f38d374
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
  <head>
    <title>Inter</title>
    <link href="/static/styles/app.css" rel="stylesheet">
    <script src="/static/scripts/app.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
  </head>
  <body>
    <div id="page">
      <div id="servers"></div>
      <div id="channels"></div>
      <div id="chat">
        <div id="zone">
          {% for msg in history %}
            {% if msg.local %}
              <div class="R-Local MSG-Row">
                <p class="UL-Local UserLabel">{{ msg.user }}</p>
                <p class="M-Local Message">{{ msg.message }}</p>
              </div>
            {% elif not msg.local %}
              <div class="MSG-Row">
                <p class="UserLabel">{{ msg.user }}</p>
                <p class="Message">{{ msg.message }}</p>
              </div>
            {% endif %}
          {% endfor %}
        </div>
        <div class="input">
          <input id="inputText" placeholder="Message"/>
        </div>
      </div>
      <div id="users"></div>
    </div>
    <script type="text/javascript" charset="utf-8">
      let log_events = true; // you can turn this on, it won't change anything, other than log everything in the console. Debugging purposes
      let user = "{{ session['username'] }}";
      let latest_msg_id = {{ latestID }};
      let socket = io()

      socket.on('connect', function() {
        if (log_events == true) {
          console.log("Socket connected to server");
        }
      });

      socket.on('msg', function(data) {
        if (data.author == user) {
          return;
        }
        if (log_events === true) {
          console.log("New message received!");
        }
        addMsg2View(data);
      })
      
      document.addEventListener("DOMContentLoaded",(event) => {

        document.addEventListener("keydown", (event) => {
          if (event.key == "Enter") {
            let input = document.getElementById("inputText");
            if (input.value == "") {
              return;
            }
            sendMessage(input.value);
          }
        })
      })
      
      function checkForMessage(payload) { // I don't trust you losers with something like message data, You just get to check with an api lmao
        return;
      }

      function sendMessage(msgContent) {
        let heads = new Headers();
        let creds = window.btoa("{{ session['username'] }}" + ":" + "{{ session['password'] }}");
        heads.append("Authorization", `Basic ${creds}`);
        heads.append("Content-Type", "application/json");
        fetch("/api/messages", {
          method: "POST",
          headers: heads,
          body: JSON.stringify({ content: msgContent })
        })
          .then(response => {
            if (!response.ok) {
              throw new Error("Response did not return ok");
            }
            return response.json();
          })
          .then((response) => {
            addMsg2View(response);
          })
          .catch(error => {
            console.error("Error - ", error.message);
          })
      }

      function addMsg2View(data) {
        let author = data.author;
        let content = data.content;
        let local = data.local;
        let id = data.id;
        if (id > latest_msg_id) {
          latest_msg_id = id;
        }
        let row = document.createElement("div");
        let authorTxt = document.createElement("p");
        let contentTxt = document.createElement("p");
        let zone = document.getElementById("zone")

        row.classList.add("MSG-Row");
        authorTxt.classList.add("UserLabel");
        contentTxt.classList.add("Message");
        if (local == true) {
          row.classList.add("R-Local");
          authorTxt.classList.add("UL-Local");
          contentTxt.classList.add("M-Local");
        }
        authorTxt.textContent = author;
        contentTxt.textContent = content;
        row.appendChild(authorTxt);
        row.appendChild(contentTxt);
        zone.appendChild(row);
      }
    </script>
  </body>
</html>