-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
116 lines (110 loc) · 4.77 KB
/
message.py
File metadata and controls
116 lines (110 loc) · 4.77 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
from flask import Blueprint, current_app, request, render_template, redirect, session, flash, g
from tools import get_date_fashion, filter_sql
bp = Blueprint('message', __name__)
def create_message_item(row, conn):
from_user = conn.execute('select id,`name`,avatar from user where id=%d' % row.from_id).first()
to_user = conn.execute('select id,`name`,avatar from user where id=%d' % row.to_id).first()
return {
'id':row.id,
'content':row.content,
'from_user':{
'id':from_user.id,
'name':from_user.name,
'avatar':from_user.avatar
},
'to_user':{
'id':to_user.id,
'name':to_user.name,
'avatar':to_user.avatar
},
'send_date': get_date_fashion(row.send_date)
}
@bp.route('/msgbox')
def read_messages():
if 'ol_user' not in session:
return redirect('/login?back=/msgbox')
user = session['ol_user']
conn = current_app.connect()
read_messages = conn.execute('select * from message where to_id=%d and readed=0 and (first_del_id is null or first_del_id<>%d)' % (user['id'], user['id'])).fetchall()
conn.execute('update message set readed=1 where to_id=%d and readed=0' % user['id'])
messages = []
for message in read_messages:
messages.append(create_message_item(message, conn))
conn.close()
messages.reverse()
return render_template(g.tperfix + 'msgbox.html', receive = True, messages = messages, title='未读信息 • <a href="/msgbox/all">收件箱</a> <a href="/msgbox/send">发件箱</a>')
@bp.route('/msgbox/all')
def all_messages():
if 'ol_user' not in session:
return redirect('/login?back=/msgbox/all')
user = session['ol_user']
conn = current_app.connect()
all_messages = conn.execute('select * from message where to_id=%d and (first_del_id is null or first_del_id<>%d)' % (user['id'], user['id'])).fetchall()
messages = []
for message in all_messages:
messages.append(create_message_item(message, conn))
conn.close()
messages.reverse()
return render_template(g.tperfix + 'msgbox.html', receive = True, messages = messages, title='收件箱 • <a href="/msgbox">未读信息</a> <a href="/msgbox/send">发件箱</a>')
@bp.route('/msgbox/send')
def send_messages():
if 'ol_user' not in session:
return redirect('/login?back=/msgbox/all')
user = session['ol_user']
conn = current_app.connect()
send_messages = conn.execute('select * from message where from_id=%d and (first_del_id is null or first_del_id<>%d)' % (user['id'], user['id'])).fetchall()
messages = []
for message in send_messages:
messages.append(create_message_item(message, conn))
conn.close()
messages.reverse()
return render_template(g.tperfix + 'msgbox.html', noreply = True, send = True, messages = messages, title = '发件箱 • <a href="/msgbox">未读信息</a> <a href="/msgbox/all">收件箱</a>')
@bp.route('/msgsend', methods = ['POST', 'GET'])
def send_message():
if 'ol_user' not in session:
return redirect('/login?back=/msgsend')
if request.method == 'GET':
to_user = ''
if request.args.get('to'):
to_user = request.args.get('to')
import urllib
to_user = urllib.parse.unquote(to_user)
if "'" in to_user:
return render_template(g.tperfix + '404.html')
return render_template(g.tperfix + 'msgsend.html', to_user = to_user)
user = session['ol_user']
to_user = request.form['username']
msg_content = request.form['content']
if to_user is None or msg_content is None or len(msg_content) < 1:
return render_template(g.tperfix + 'msgsend.html', message = '接收者不能为空,信息内容不能为空!')
if to_user == user['name']:
return render_template(g.tperfix + 'msgsend.html', message = '不能给自己发信息!')
to_user, msg_content = filter_sql([to_user, msg_content])
conn = current_app.connect()
to_id_row = conn.execute("select id from user where name='%s'" % to_user).first()
if not to_id_row:
message = '接收者不存在!'
else:
count = conn.execute("insert into message(content, from_id, to_id) values('%s', %d, %d)" % (msg_content, user['id'], to_id_row.id)).rowcount
if count == 1:
message = '已发送!'
else:
message = 'Error.'
conn.close()
return render_template(g.tperfix + 'msgsend.html', message = message)
@bp.route('/msgdel/<int:id>')
def msgdel(id):
if 'ol_user' not in session:
return redirect('/login')
user = session['ol_user']
conn = current_app.connect()
first_del_id = conn.execute('select first_del_id from message where id=%d and (from_id=%d or to_id=%d)' % (id, user['id'], user['id'])).first()[0]
if first_del_id and (first_del_id != user['id']):
conn.execute('delete from message where id=%d' % id)
else:
conn.execute('update message set first_del_id=%d where id=%d and (from_id=%d or to_id=%d)' % (user['id'], id, user['id'], user['id']))
flash('已删除!')
backurl = request.args.get('back')
if backurl:
return redirect(backurl)
return redirect('/msgbox/all')