forked from techwithtim/Chat-Web-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b958a7e
commit 508bb29
Showing
20 changed files
with
205 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
TESTING=False | ||
DEBUG=True | ||
SECRET_KEY=youwontguessthiskey |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from client import Client | ||
from old_msg_server.client import Client | ||
import time | ||
from threading import Thread | ||
|
||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy() | ||
from os import environ | ||
|
||
|
||
def create_app(): | ||
"""Construct the core application.""" | ||
app = Flask(__name__, instance_relative_config=False) | ||
db.init_app(app) | ||
app.config.from_object('config.Config') | ||
app.secret_key = "no" | ||
|
||
# APP ROUTES | ||
|
||
with app.app_context(): | ||
# Imports | ||
from .views import view | ||
from .filters import filter | ||
from .models import Message | ||
from .database import DataBase | ||
|
||
app.register_blueprint(view, url_prefix="/") | ||
app.register_blueprint(filter, url_prefix="/filter") | ||
|
||
# Create tables for our models | ||
db.create_all() | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import sqlite3 | ||
from sqlite3 import Error | ||
import os | ||
from datetime import datetime | ||
import pickle | ||
|
||
|
||
cwd = os.getcwd() | ||
|
||
FILE = "messages.db" | ||
PLAYLIST_TABLE = "Messages" | ||
|
||
|
||
class DataBase: | ||
def __init__(self): | ||
self.conn = None | ||
try: | ||
self.conn = sqlite3.connect(FILE) | ||
except Error as e: | ||
print(e) | ||
|
||
self.cursor = self.conn.cursor() | ||
self._create_table() | ||
|
||
def close(self): | ||
self.conn.close() | ||
|
||
def _create_table(self): | ||
query = f"""CREATE TABLE IF NOT EXISTS {PLAYLIST_TABLE} | ||
(name TEXT, content TEXT, time Date, id INTEGER PRIMARY KEY AUTOINCREMENT)""" | ||
self.cursor.execute(query) | ||
self.conn.commit() | ||
|
||
def get_all_messages(self, limit=100): | ||
""" | ||
returns all messages | ||
""" | ||
query = f"SELECT * FROM {PLAYLIST_TABLE} LIMIT {limit}" | ||
self.cursor.execute(query) | ||
result = self.cursor.fetchall() | ||
|
||
results = [] | ||
for r in result: | ||
name, content, date, _id = r | ||
data = {"name":name, "message":content, "time":date} | ||
results.append(data) | ||
|
||
return results | ||
|
||
def save_message(self, name, msg, time): | ||
""" | ||
saves the given message | ||
:param name: str | ||
:param msg: str | ||
:param time: datetime | ||
:return: | ||
""" | ||
query = f"INSERT INTO {PLAYLIST_TABLE} VALUES (?, ?, ?, ?)" | ||
self.cursor.execute(query, (name, msg,time, None)) | ||
self.conn.commit() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
async function load_name(){ | ||
return await fetch('/get_name') | ||
.then(async function (response) { | ||
return await response.json(); | ||
}).then(function (text) { | ||
return text["name"] | ||
}); | ||
}; | ||
|
||
async function load_messages() { | ||
return await fetch('/get_messages') | ||
.then(async function (response) { | ||
return await response.json(); | ||
}).then(function (text) { | ||
console.log(text) | ||
return text | ||
}); | ||
} | ||
|
||
var socket = io.connect('http://' + document.domain + ':' + location.port); | ||
socket.on( 'connect', function() { | ||
socket.emit( 'event', { | ||
data: 'User Connected' | ||
} ) | ||
var form = $( 'form#msgForm' ).on( 'submit', async function( e ) { | ||
e.preventDefault() | ||
|
||
// get input from message box | ||
let msg_input = document.getElementById("msg") | ||
let user_input = msg_input.value | ||
let user_name = await load_name() | ||
|
||
// clear msg box value | ||
msg_input.value = "" | ||
|
||
// send message to other users | ||
socket.emit( 'event', { | ||
message : user_input, | ||
name: user_name | ||
} ) | ||
} ) | ||
} ) | ||
socket.on( 'message response', function( msg ) { | ||
add_messages(msg) | ||
}) | ||
|
||
window.onload = async function() { | ||
var msgs = await load_messages() | ||
for (msg of msgs){ | ||
add_messages(msg) | ||
} | ||
} | ||
|
||
async function add_messages(msg){ | ||
if( typeof msg.name !== 'undefined' ) { | ||
var d = new Date() | ||
var n = d.toLocaleTimeString() | ||
if ( typeof msg.time !== "undefined") { | ||
} | ||
var global_name = await load_name() | ||
|
||
var content = '<div class="container">' + '<b style="color:#000" class="right">'+msg.name+'</b><p>' + msg.message +'</p><span class="time-right">' + n + '</span></div>' | ||
if (global_name == msg.name){ | ||
content = '<div class="container darker">' + '<b style="color:#000" class="left">'+msg.name+'</b><p>' + msg.message +'</p><span class="time-left">' + n + '</span></div>' | ||
} | ||
// update div | ||
var messageDiv = document.getElementById("messages") | ||
messageDiv.innerHTML += content | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.