Skip to content

Commit

Permalink
Fixed chat timestamp, added chat timestamp to the /chats/user url
Browse files Browse the repository at this point in the history
  • Loading branch information
etrian-dev committed Feb 27, 2022
1 parent b2dfc9d commit b11ef32
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
13 changes: 10 additions & 3 deletions chatroom/Chat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import db

from time import time
from datetime import datetime
from sqlite3 import Connection, Cursor, DatabaseError

from flask import (
Expand Down Expand Up @@ -54,8 +55,8 @@ def home_user(user_id: int):
# create a chat object
ch = Chat(row['participant1'], row['participant2'])
ch.chat_id = row['chat_id']
ch.creation_time = row['creation_tm']
ch.last_activity = row['last_mod_tm']
ch.creation_time = datetime.strptime(row['creation_tm'], "%Y-%m-%d %H:%M:%S").isoformat()
ch.last_activity = datetime.strptime(row['last_mod_tm'], "%Y-%m-%d %H:%M:%S").isoformat()
# insert that into the user_data dict
if 'chats' not in user_data:
user_data['chats'] = [ch]
Expand Down Expand Up @@ -87,7 +88,8 @@ def create_chat(creator):
if match is not None:
cursor.execute('''
INSERT INTO Chats(participant1, participant2, creation_tm, last_mod_tm)
VALUES (?,?,?,?)''', [creator, match['user_id'], 'CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP'])
VALUES (?,?,datetime(),datetime());
''', [creator, match['user_id']])
db_conn.commit()
cursor.close()

Expand Down Expand Up @@ -124,6 +126,11 @@ def display_chat(user, other):
''', [chat['chat_id']])
msgs_encoded = cur.fetchall()
messages = []
# build breadcrumb
breadcrumb = dict()
breadcrumb['home'] = url_for('chat.home_user', user_id=user)
breadcrumb[chat_info['other_user']] = url_for('chat.display_chat', user=user, other=other)
chat_info['breadcrumb'] = breadcrumb
#decode messages
for msg in msgs_encoded:
sender = None
Expand Down
7 changes: 5 additions & 2 deletions chatroom/Msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def __init__(self, data: str, stamp: int):

from . import db
from sqlite3 import Connection, Cursor, DatabaseError
from json import loads

from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
Expand All @@ -24,7 +25,6 @@ def get_message(sender, receiver, msg_id):


@blueprint.route('/<int:sender>/<int:recipient>/', methods=['POST'])
# TODO: post
def send_message(sender, recipient):
db_conn = db.get_db()
try:
Expand Down Expand Up @@ -55,8 +55,11 @@ def send_message(sender, recipient):


#TODO: PUT the message trough jQuery
@blueprint.route('/<int:msg_id>', methods=['GET', 'POST'])
@blueprint.route('/<int:msg_id>', methods=['GET', 'PUT'])
def edit_message(msg_id):
if request.method == 'PUT':
print(json.loads(request.get_json()))
return '',404
msg_data = dict()
msg_data['msg_id'] = msg_id
db_conn = db.get_db()
Expand Down
4 changes: 2 additions & 2 deletions chatroom/db_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ CREATE TABLE Chats (
chat_id INTEGER PRIMARY KEY AUTOINCREMENT,
participant1 INTEGER REFERENCES Users(user_id),
participant2 INTEGER REFERENCES Users(user_id),
creation_tm INTEGER,
last_mod_tm INTEGER,
creation_tm TEXT,
last_mod_tm TEXT,
CHECK (participant1 != participant2),
UNIQUE(participant1, participant2)
);
Expand Down
5 changes: 1 addition & 4 deletions chatroom/templates/chats.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@
{{ user.participant1 }}
</a>
{% endif %}

(created at: {{ user.last_activity }})
</li>
{% endfor %}
</ul>
<div>
<form action="{{ url_for('chat.create_chat', creator=user_data['user_id']) }}" id='chat-creation' method='POST'>
<span class='form-label'>Chat with: </span>
<input class='form-input' type='text' name='username' placeholder='username'>
<select class='form-select'>
<option></option>
</select>
<input class='btn btn-primary' type='submit' value='Create'>
</form>
</div>
Expand Down
5 changes: 2 additions & 3 deletions chatroom/templates/edit_message.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
</head>
<body>
<div class="card">
<form action="{{ url_for('msg.edit_message', msg_id=msg_id) }}"
id='msg-edit' method='POST'>
<form action="{{ url_for('msg.edit_message', msg_id=msg_id) }}" id='msg-edit-form'>
<input class='form-input' type='textarea' name='message' value='{{ old_msg }}'>
<input class='btn btn-primary' type='submit' value='Edit'>
<button class='btn btn-primary' id="msg-edit-sumbit" type='submit'>Ok</button>
</form>
</div>
<body>
Expand Down
9 changes: 9 additions & 0 deletions chatroom/templates/messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<title>RSA chatroom - {{ other_user|title }}</title>
</head>
<body>
<ul class="breadcrumb">
<li class="breadcrumb-item">
<a href="{{ breadcrumb['home'] }}"}>Home</a>
</li>
<li class="breadcrumb-item">
<a href="{{ breadcrumb[other_user] }}"}>{{ other_user|title }}</a>
</li>
</ul>

{% for msg in messages %}
<div class="container columns">
{% if msg['sender'] is eq this_user %}
Expand Down

0 comments on commit b11ef32

Please sign in to comment.