-
Notifications
You must be signed in to change notification settings - Fork 43
/
message.py
executable file
·116 lines (92 loc) · 4.18 KB
/
message.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
from flask import request, Response, render_template, session, url_for, redirect
from crysadm import app, r_session
from auth import requires_admin, requires_auth
from datetime import datetime, timedelta
import util
import json
import uuid
@app.route('/messagebox')
@requires_auth
def messagebox():
user = session.get('user_info')
err_msg = util.get_message()
msgs_key = 'user_messages:%s' % user.get('username')
msg_box = list()
show_read_all = False
for b_msg_id in r_session.lrange(msgs_key, 0, -1):
msg_key = 'user_message:%s' % b_msg_id.decode('utf-8')
b_msg = r_session.get(msg_key)
if b_msg is None:
r_session.lrem(msgs_key, msg_key)
continue
msg = json.loads(b_msg.decode('utf-8'))
if show_read_all or not msg.get('is_read'):
show_read_all = True
msg_box.append(msg)
return render_template('messages.html', err_msg=err_msg, messages=msg_box, show_read_all=show_read_all)
@app.route('/message/action', methods=['POST'])
@requires_auth
def message_action():
user = session.get('user_info')
if request.form['btn'] is None:
util.set_message('参数错误')
return redirect(url_for('messagebox'))
msgs_key = 'user_messages:%s' % user.get('username')
all_message = r_session.lrange(msgs_key, 0, -1)
for val in request.form:
if len(val) < 4 or val[0:3] != 'msg':
continue
msg_id = val[4:]
if bytes(msg_id, 'utf-8') not in all_message:
continue
if request.form['btn'] == 'mark_as_read':
msg_key = 'user_message:%s' % msg_id
msg = json.loads(r_session.get(msg_key).decode('utf-8'))
msg['is_read'] = True
r_session.set(msg_key, json.dumps(msg))
elif request.form['btn'] == 'delete':
r_session.lrem(msgs_key, msg_id)
msg_key = 'user_message:%s' % msg_id
r_session.delete(msg_key)
return redirect(url_for('messagebox'))
@app.route('/add_msg')
@requires_admin
def add_msg():
return '功能已关闭'
i = 0
for b_username in r_session.smembers('users'):
i += 1
if i > 10000:
break
send_msg(b_username.decode('utf-8'), '新域名通知 crysadm.com!', '最好看的矿场监工有新的访问姿势:crysadm.com <br /> <br />'
'''<table class="table table-bordered">
<tbody>
<tr>
<td>国内用户</td>
<td><a href="https://crysadm.com">crysadm.com</a></td>
</tr>
<tr>
<td>海外用户</td>
<td><a href="https://os.crysadm.com">os.crysadm.com</a></td>
</tr>
</tbody>
</table>
''', expire=3600 * 24)
return '发送成功'
@app.route('/delall_msg')
@requires_admin
def del_all_msg():
for k in r_session.keys('user_messages:*'):
r_session.delete(k.decode('utf-8'))
return '删除成功'
def send_msg(username, subject, content, expire=3600 * 24 * 7):
if bytes(username, 'utf-8') not in r_session.smembers('users'):
return '找不到该用户。'
msgs_key = 'user_messages:%s' % username
msg_id = str(uuid.uuid1())
msg = dict(id=msg_id, subject=subject, content=content,
is_read=False, time=datetime.now().strftime('%Y-%m-%d %H:%M'))
msg_key = 'user_message:%s' % msg_id
r_session.setex(msg_key, json.dumps(msg), expire)
r_session.lpush(msgs_key, msg_id)
return '发送成功'