-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
239 lines (206 loc) · 7.64 KB
/
app.py
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
from website import create_app, db, Workspace, User, Channel, Chats
from flask_socketio import SocketIO, send, emit, join_room
from flask import session
from flask_login import login_user, logout_user, login_required, current_user
import random
import string
from flask import Blueprint, render_template, session, redirect, request
import cloudinary as Cloud
from cloudinary import uploader
from cloudinary.utils import cloudinary_url
app = create_app(db)
socketio = SocketIO(app, logger=True, engineio_logger=True)
Cloud.config(
cloud_name="ddyxpvamf",
api_key="326629223956868",
api_secret="RfIUcGp-j8_Xz4YbBoLUTNn5Mc4"
)
@socketio.on('sendimage')
def sendimage(data):
print("hello")
if Chats.query.filter_by(id=session['imageid']).count() == 1:
i = Chats.query.filter_by(id=session['imageid']).first()
c = Chats.query.filter_by(message=i.message).first()
session['imageid'] = -1
data = {
'id': c.id,
'message': c.message,
'username': c.username,
'wid': c.wid,
'channel_id': c.channel_id,
'image': 1
}
room = Workspace.query.filter_by(id=c.wid).first()
join_room(room.name)
emit('receiveMessage', data, broadcast=True, room=room.name)
request.environ.get('socketio').emit('reloadPage') # Emitting custom event to trigger page reload
# emit('receiveimage', data, room = session['name'] )
@socketio.on('reloadPage') # Event handler for custom event
def reload_page():
emit('reload', broadcast=True) # Emitting 'reload' event to trigger page reload on the client side
@socketio.on('message')
def handle_message(data):
print(data)
if session.get("USERNAME") is None:
username = current_user.name
else:
username = session['username']
user = User.query.filter_by(name=username).first()
if user.workspace_list:
wlist = user.workspace_list.split()
wid = int(wlist[0])
room = Workspace.query.filter_by(id=wid).first()
join_room(room.name)
send({"msg": data['data'], "wid": "1", "channel_d": "2"})
@socketio.on('createWorkspace')
def handle_createWorkspace(data):
print(data)
w = Workspace()
w.admin_username = data['username']
w.name = data['name']
joining_code = random_string(4, 2)
w.joining_code = joining_code
db.session.add(w)
db.session.commit()
room = Workspace.query.filter_by(name=data['name']).first()
user = User.query.filter_by(name=data['username']).first()
if user.workspace_list:
user.workspace_list = user.workspace_list + str(room.id) + " "
else:
user.workspace_list = str(room.id) + " "
db.session.commit()
print("hello", user.workspace_list)
join_room(room.name)
data = {
"name": data['name'],
"admin_username": data['username'],
"id": room.id,
"joining_code": joining_code,
}
emit('createWorkspaceJS', data, broadcast=True)
@socketio.on('createChannel')
def handle_createChannel(data):
c = Channel()
c.admin_username = data['username']
c.name = data['name']
c.wid = data['wid']
room = Workspace.query.filter_by(id=data['wid']).first()
db.session.add(c)
db.session.commit()
channel = Channel.query.filter_by(name=data['name']).first()
data = {
"name": data['name'],
"admin_username": data['username'],
"id": channel.id,
"wid": data['wid'],
}
emit('createChannelJS', data, room=room.name, broadcast=True)
@socketio.on('join')
def joinRoom(data):
if (data['wid']):
room = Workspace.query.filter_by(id=data['wid']).first()
join_room(room.name)
elif (data['name']):
join_room(data['name'])
@socketio.on('getChannels')
def sendChannels(data):
wid = data['wid']
room = Workspace.query.filter_by(id=wid).first()
Channels = Channel.query.filter_by(wid=wid).all()
ch = []
ChannelCount = Channel.query.filter_by(wid=wid).count()
i = 0
for c in Channels:
ch.append({i: {
'id': c.id,
'name': c.name,
'admin_username': c.admin_username,
'wid': c.wid
}})
i = i + 1
emit('getChannelsJS', {"channels": ch, "channelCount": ChannelCount, "name": room.name})
@socketio.on('getWorkspaceName')
def get_workspaceName(data):
wid = data['wid']
print(wid)
room = Workspace.query.filter_by(id=wid).first()
print("hello", room.joining_code)
emit('changeWorkspaceName', {"name": room.name, "joining_id": room.joining_code})
@socketio.on('chatmsg')
def chat_msg(data):
c = Chats()
c.message = data['msg']
c.username = data['username']
c.wid = data['wid']
c.channel_id = data['channel_id']
c.image = 0
data['image'] = 0
db.session.add(c)
db.session.commit()
print(c)
wid = data['wid']
room = Workspace.query.filter_by(id=wid).first()
join_room(room.name)
emit('receiveMessage', data, broadcast=True, room=room.name)
@socketio.on('getMessages')
def sendMessages(data):
chats = Chats.query.filter_by(wid=data['wid'], channel_id=data['channel_id']).all()
channel = Channel.query.filter_by(id=data['channel_id']).first()
chatscount = len(chats)
i = 0
ch = []
wid = data['wid']
room = Workspace.query.filter_by(id=wid).first()
join_room(room.name)
for c in chats:
ch.append({i: {
'id': c.id,
'message': c.message,
'username': c.username,
'wid': c.wid,
'channel_id': c.channel_id,
'image': c.image
}})
i = i + 1
emit('receiveMessageJS', {"chats": ch, "channel_id": data['channel_id'], "name": channel.name}, broadcast=True,
room=room.name)
@socketio.on('joinWorkspace')
def addWorkspace(data):
user = User.query.filter_by(name=data['username']).first()
if Workspace.query.filter_by(name=data['name'], joining_code=data['code']).count() == 1:
join_room(data['name'])
if user.workspace_list:
room = Workspace.query.filter_by(name=data['name'], ).first()
join_room(room.name)
wlist = user.workspace_list.split()
wlist = [int(i) for i in wlist]
wid = room.id
if wid in wlist:
emit('error', {"msg": "You have already joined the workspace!", "username": data['username']},
room=room.name)
else:
user.workspace_list = user.workspace_list + str(room.id) + " "
emit('workspaceJoined', {"wid": room.id, "username": data['username'], "name": room.name, },
room=room.name)
else:
room = Workspace.query.filter_by(name=data['name'], ).first()
user.workspace_list = str(room.id) + " "
emit('workspaceJoined', {"wid": room.id, "name": room.name, "username": data['username']}, room=room.name)
db.session.commit()
def random_string(letter_count, digit_count):
str1 = ''.join((random.choice(string.ascii_letters) for x in range(letter_count)))
str1 += ''.join((random.choice(string.digits) for x in range(digit_count)))
sam_list = list(str1) # it converts the string to list.
random.shuffle(sam_list) # It uses a random.shuffle() function to shuffle the string.
final_string = ''.join(sam_list)
return final_string
@app.errorhandler(405)
# inbuilt function which takes error as parameter
def not_found(e):
return render_template("/views/404.html")
@app.errorhandler(404)
# inbuilt function which takes error as parameter
def not_found(e):
return render_template("/views/404.html")
if __name__ == '__main__':
socketio.run(app, debug=True, port=5000)