-
Notifications
You must be signed in to change notification settings - Fork 4
/
post.py
192 lines (176 loc) · 5.41 KB
/
post.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
from flask_login import current_user
from utils import _
from utils import *
from pipeline import (
pipeline, split_lines, process_code_block, process_line_without_format,
join_lines
)
from models import (
db, fn, User, Topic, Post, DeleteRecord, Message, Image
)
from config import NOTIFICATION_SIGN, IMAGE_SIGN, SUMMARY_LENGTH
from validation import REGEX_SHA256_PART
def filter_images(lines, images):
found = {}
def process_segment(segment):
if (
segment.startswith(IMAGE_SIGN)
and REGEX_SHA256_PART.fullmatch(segment[len(IMAGE_SIGN):])
):
image_query = (
Image
.select()
.where(
Image.sha256.startswith(
segment[len(IMAGE_SIGN):]
)
)
)
if image_query:
image = image_query.get()
if not found.get(image.sha256):
images.append(image)
found[image.sha256] = True
return _('[Image]')
return segment
for line in lines:
if isinstance(line, str):
yield (
' '.join(process_segment(segment) for segment in line.split(' '))
)
else:
yield str(line)
def gen_summary(content):
images = []
def get_images(lines):
return filter_images(lines, images)
content_processor = pipeline(
split_lines, process_code_block, get_images, join_lines
)
processed = content_processor(content)
if len(processed) > SUMMARY_LENGTH:
summary = processed[:SUMMARY_LENGTH-3] + '...'
else:
summary = processed
summary_images = ','.join(image.sha256[:10] for image in images[:3])
return (summary, summary_images)
def filter_at_messages(lines, callees):
called = {}
def process_segment(segment):
if segment.startswith(NOTIFICATION_SIGN):
callee_name = segment[1:]
callee = find_record(User, name=callee_name)
if (
callee
and callee.id != current_user.id
and not called.get(callee.id)
):
callees.append(callee)
called[callee.id] = True
# use "@@" to indicate this call is valid
return NOTIFICATION_SIGN + segment
return segment
text = ''
for line in lines:
if isinstance(line, str):
yield ''.join(
snippet
for snippet in process_line_without_format(
line, process_segment
)
)
else:
yield str(line)
def create_post(topic, parent, content, add_reply_count=True, is_sys_msg=False, is_pm=False, author=None):
if not is_sys_msg:
if not author:
author = find_record(User, id=current_user.id)
callees = []
def process_at(lines):
return filter_at_messages(lines, callees)
content_processor = pipeline(
split_lines, process_code_block, process_at, join_lines
)
if not is_sys_msg and not is_pm:
content = content_processor(content)
if parent:
parent_path = parent.path
parent_sort_path = parent.sort_path
else:
parent_path = ''
parent_sort_path = ''
total = Post.select(fn.COUNT(Post.id)).where(
Post.topic == topic,
Post.parent == parent
)
new_post = Post.create(
topic = topic,
parent = parent,
ordinal = total,
content = content,
date = now(),
author = author
)
new_post.path = '%s/%d' % (parent_path, new_post.id)
new_post.sort_path = (
'%s/%s-%d' % (
parent_sort_path,
new_post.date.isoformat(),
random.randrange(0, 10)
)
)
new_post.save()
if topic:
topic.last_reply_date = new_post.date
topic.last_reply_author = new_post.author
topic.save()
if add_reply_count:
(
Topic
.update(reply_count = Topic.reply_count+1)
.where(Topic.id == topic.id)
).execute()
for callee in callees:
# self-to-self already filtered
# use try_to_create() to update unread count
Message.try_to_create(
msg_type = 'at',
post = new_post,
caller = author,
callee = callee
)
p = parent
while p:
Message.try_to_create(
msg_type = 'reply',
post = new_post,
caller = author,
callee = p.author
)
p = p.parent
if not is_sys_msg and not is_pm:
Message.try_to_create(
msg_type = 'reply',
post = new_post,
caller = author,
callee = topic.author
)
return new_post
def create_system_message(content, target):
post = create_post(None, None, content, is_sys_msg=True)
message = Message.try_to_create(
msg_type = 'sys',
post = post,
caller = None,
callee = target
)
return message
def create_pm(content, target):
post = create_post(None, None, content, is_pm=True)
message = Message.try_to_create(
msg_type = 'pm',
post = post,
caller = find_record(User, id=current_user.id),
callee = target
)
return message