-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
330 lines (299 loc) · 13.6 KB
/
app.py
File metadata and controls
330 lines (299 loc) · 13.6 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
import uuid
import datetime
from flask import Flask, render_template, redirect, url_for, request, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_user, logout_user, login_required, current_user, UserMixin
from flask_socketio import SocketIO, join_room, emit
from werkzeug.security import generate_password_hash, check_password_hash
print("Made by Shaurya Singh(Willoper)\n","Thanks for using site! I hope you will like it and rate it on Github\n","Once Again Thanks for using")
print("Please Wait Server is Starting........")
# Create extension instances
db = SQLAlchemy()
login_manager = LoginManager()
socketio = SocketIO(manage_session=True)
def create_app():
app = Flask(__name__)
# Configuration
app.config['SECRET_KEY'] = 'secret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['UPLOAD_FOLDER'] = os.path.join(app.root_path, 'static', 'uploads')
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
db.init_app(app)
login_manager.init_app(app)
socketio.init_app(app)
# ---------------------------
# Database Models
# ---------------------------
contacts = db.Table('contacts',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('contact_id', db.Integer, db.ForeignKey('user.id'))
)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.String(10), unique=True, nullable=False)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
chat_list = db.relationship('User', secondary=contacts,
primaryjoin=(contacts.c.user_id == id),
secondaryjoin=(contacts.c.contact_id == id),
backref='contacts')
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
sender = db.relationship('User', foreign_keys=[sender_id])
receiver_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
group_id = db.Column(db.Integer, db.ForeignKey('chat_group.id'), nullable=True)
content = db.Column(db.Text, nullable=True)
timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
filename = db.Column(db.String(200), nullable=True)
read = db.Column(db.Boolean, default=False)
class ChatGroup(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
admin_id = db.Column(db.Integer, db.ForeignKey('user.id'))
members = db.relationship('User', secondary='group_members', backref='groups')
group_members = db.Table('group_members',
db.Column('group_id', db.Integer, db.ForeignKey('chat_group.id')),
db.Column('user_id', db.Integer, db.ForeignKey('user.id'))
)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# ---------------------------
# Routes for Authentication & Profile
# ---------------------------
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
if User.query.filter_by(username=username).first():
flash("Username already exists. Please choose a different username.")
return redirect(url_for('register'))
uid = str(uuid.uuid4())[:8]
new_user = User(username=username, email=email, uid=uid,
password_hash=generate_password_hash(password))
db.session.add(new_user)
db.session.commit()
flash('Registered successfully. Please login.')
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password_hash, password):
login_user(user)
return redirect(url_for('dashboard'))
else:
flash('Invalid username or password.')
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('dashboard.html', uid=current_user.uid)
@app.route('/profile')
@login_required
def profile():
return render_template('profile.html', user=current_user)
@app.route('/change_password', methods=['GET', 'POST'])
@login_required
def change_password():
if request.method == 'POST':
current_password = request.form['current_password']
new_password = request.form['new_password']
confirm_password = request.form['confirm_password']
if not check_password_hash(current_user.password_hash, current_password):
flash('Current password is incorrect.')
elif new_password != confirm_password:
flash('New passwords do not match.')
else:
current_user.password_hash = generate_password_hash(new_password)
db.session.commit()
flash('Password updated successfully.')
return redirect(url_for('profile'))
return render_template('change_password.html')
# ---------------------------
# Private Chat & Group Chat Routes
# ---------------------------
@app.route('/private_chat', methods=['GET', 'POST'])
@login_required
def private_chat():
if request.method == 'POST':
uid_search = request.form['uid']
user = User.query.filter_by(uid=uid_search).first()
if user and user != current_user:
if user not in current_user.chat_list:
current_user.chat_list.append(user)
db.session.commit()
flash('User added to your chat list.')
else:
flash('User not found.')
return render_template('private_chat.html', contacts=current_user.chat_list)
@app.route('/group_chat')
@login_required
def group_chat():
groups = current_user.groups
return render_template('group_chat.html', groups=groups)
@app.route('/create_group', methods=['GET', 'POST'])
@login_required
def create_group():
if request.method == 'POST':
group_name = request.form['group_name']
group = ChatGroup(name=group_name, admin_id=current_user.id)
group.members.append(current_user)
db.session.add(group)
db.session.commit()
flash('Group created successfully.')
return redirect(url_for('group_chat'))
return render_template('create_group.html')
@app.route('/add_to_group/<int:group_id>', methods=['GET', 'POST'])
@login_required
def add_to_group(group_id):
group = ChatGroup.query.get_or_404(group_id)
if group.admin_id != current_user.id:
flash('Only the group admin can add users.')
return redirect(url_for('group_chat'))
if request.method == 'POST':
uid_search = request.form['uid']
user = User.query.filter_by(uid=uid_search).first()
if user and (user in current_user.chat_list) and (user not in group.members):
group.members.append(user)
db.session.commit()
flash('User added to group.')
else:
flash('User not found or not in your chat list.')
return render_template('add_to_group.html', group=group)
# New route: Delete Group (only for group admin)
@app.route('/delete_group/<int:group_id>', methods=['POST'])
@login_required
def delete_group(group_id):
group = ChatGroup.query.get_or_404(group_id)
if group.admin_id != current_user.id:
flash("You are not authorized to delete this group.")
return redirect(url_for('group_chat'))
db.session.delete(group)
db.session.commit()
flash("Group deleted successfully.")
return redirect(url_for('group_chat'))
@app.route('/chat/<chat_type>/<int:chat_id>')
@login_required
def chat(chat_type, chat_id):
if chat_type == 'private':
other_user = User.query.get_or_404(chat_id)
messages = Message.query.filter(
((Message.sender_id == current_user.id) & (Message.receiver_id == other_user.id)) |
((Message.sender_id == other_user.id) & (Message.receiver_id == current_user.id))
).order_by(Message.timestamp).all()
return render_template('chat.html', messages=messages, chat_type='private', chat_id=other_user.id)
elif chat_type == 'group':
group = ChatGroup.query.get_or_404(chat_id)
if current_user not in group.members:
flash('You are not a member of this group.')
return redirect(url_for('dashboard'))
messages = Message.query.filter_by(group_id=group.id).order_by(Message.timestamp).all()
return render_template('chat.html', messages=messages, chat_type='group', chat_id=group.id)
else:
flash('Invalid chat type.')
return redirect(url_for('dashboard'))
@app.route('/clear_history/<chat_type>/<int:chat_id>', methods=['POST'])
@login_required
def clear_history(chat_type, chat_id):
if chat_type == 'private':
other_user = User.query.get_or_404(chat_id)
Message.query.filter(
((Message.sender_id == current_user.id) & (Message.receiver_id == other_user.id)) |
((Message.sender_id == other_user.id) & (Message.receiver_id == current_user.id))
).delete(synchronize_session=False)
elif chat_type == 'group':
Message.query.filter_by(group_id=chat_id).delete(synchronize_session=False)
db.session.commit()
flash('Chat history cleared.')
return redirect(url_for('chat', chat_type=chat_type, chat_id=chat_id))
@app.route('/upload', methods=['POST'])
@login_required
def upload():
file = request.files.get('file')
if file:
filename = str(uuid.uuid4()) + "_" + file.filename
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
return filename
return ''
# ---------------------------
# Socket.IO Events
# ---------------------------
@socketio.on('join')
def handle_join(data):
room = data['room']
join_room(room)
emit('status', {'msg': current_user.username + ' has entered the room.'}, room=room)
@socketio.on('text')
def handle_text(data):
room = data['room']
msg = data['msg']
chat_type = data['chat_type']
chat_id = data['chat_id']
filename = data.get('filename')
message = Message(sender_id=current_user.id, content=msg, filename=filename)
if chat_type == 'private':
message.receiver_id = chat_id
elif chat_type == 'group':
message.group_id = chat_id
db.session.add(message)
db.session.commit()
emit('message', {
'username': current_user.username,
'timestamp': message.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
'content': msg,
'filename': filename,
'message_id': message.id,
'read': message.read
}, room=room)
@socketio.on('delete_message')
def handle_delete_message(data):
message_id = data.get('message_id')
room = data.get('room')
msg_obj = Message.query.get(message_id)
if msg_obj and str(msg_obj.sender_id) == str(current_user.get_id()):
db.session.delete(msg_obj)
db.session.commit()
emit('message_deleted', {'message_id': message_id}, room=room)
else:
emit('error', {'msg': 'You are not authorized to delete this message.'}, room=room)
# New event: Mark all messages as read in a private chat when the chat window opens
@socketio.on('mark_read_all')
def handle_mark_read_all(data):
room = data.get('room')
other_user = data.get('other_user')
unread_messages = Message.query.filter(
Message.sender_id == other_user,
Message.receiver_id == current_user.id,
Message.read == False
).all()
for msg in unread_messages:
msg.read = True
db.session.commit()
# Optionally, notify the room that these messages are now read
emit('messages_marked_read', {'message_ids': [msg.id for msg in unread_messages]}, room=room)
return app
if __name__ == '__main__':
app = create_app()
with app.app_context():
db.create_all()
socketio.run(app, debug=True)