-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.mu
executable file
·257 lines (244 loc) · 12.4 KB
/
comment.mu
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
#!/usr/bin/env python3
# nomadForum - a forum on the NomadNetwork
# Copyright (C) 2023-2024 AutumnSpark1226
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import uuid
import sys
import main
import notify
def edit_check_data():
if not main.check_uuid(edit_id):
print("data not found")
main.close_database(write_changes=False)
sys.exit(0)
op_username = ""
comment_data = main.query_database(f"SELECT username FROM comments WHERE comment_id = '{edit_id}'")
if len(comment_data) != 1:
print("data not found")
main.close_database(write_changes=False)
sys.exit(0)
op_username = comment_data[0][0]
if username != op_username:
print("You can only edit your own posts.")
main.close_database(write_changes=False)
sys.exit(0)
def print_fields() -> None:
if parent == "post":
post_data = main.query_database(f"SELECT username, title, content, datetime(changed, 'unixepoch') FROM posts WHERE post_id = '{post_id}'")
if len(post_data) != 1:
print("You found a bug!")
main.close_database(write_changes=False)
exit(0)
post_data = post_data[0]
print('`F222`B999')
print(f"`!{post_data[1]}`!")
print("``")
if post_data[0] != "[DELETED]":
display_name = main.query_database(f"SELECT display_name FROM users WHERE username = '{post_data[0]}'")[0][0]
username_styled = f"{display_name} ``(`Ff22`_`[Profile`:{main.page_path}/profile.mu`username={post_data[0]}]`f`_)"
else:
username_styled = "[DELETED]"
print(f"{username_styled}: ({post_data[3]} (UTC))")
print("-")
print(f"{post_data[2]}")
print("``")
print(">")
elif main.check_uuid(parent) and len(main.query_database(f"SELECT numeric_id FROM comments WHERE comment_id = '{parent}' and post_id = '{post_id}'")) == 1:
comment_data = main.query_database(f"SELECT username, content, datetime(changed, 'unixepoch'), post_id FROM comments WHERE comment_id = '{parent}'")
if len(comment_data) != 1:
print("You found a bug!")
main.close_database(write_changes=False)
exit(0)
comment_data = comment_data[0]
post_title = main.query_database(f"SELECT title FROM posts WHERE post_id = '{comment_data[3]}'")[0][0]
print(f">>Comment on {post_title}\n")
if comment_data[0] != "[DELETED]":
display_name = main.query_database(f"SELECT display_name FROM users WHERE username = '{comment_data[0]}'")[0][0]
username_styled = f"{display_name} ```B888(`Ff22`_`[Profile`:{main.page_path}/profile.mu`username={comment_data[0]}]`f`_)"
else:
username_styled = "[DELETED]"
print(">>>")
print(f"`B888{username_styled}: ({comment_data[2]} (UTC))")
print("``")
print(comment_data[1])
print("``")
print(">")
print()
username = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")
if len(username) != 1:
print("Congratulations, you found a bug!")
main.close_database(write_changes=False)
exit(0)
username = username[0][0]
global content
if content == "":
content = main.query_database(f"SELECT default_styling FROM users WHERE username = '{username}'")[0][0]
if content == 'None':
content = ""
content = content.replace("\n", "$newline$")
print(f"Comment: `B444`<70|content`{content}>`b")
print(f"`Ff22`_`[Post comment`:{main.page_path}/comment.mu`*|post_id={post_id}|parent={parent}|source_link_id={link_id}]`_`f")
def print_fields_edit() -> None:
global content, username
if parent == "post":
post_data = main.query_database(f"SELECT username, title, content, datetime(changed, 'unixepoch') FROM posts WHERE post_id = '{post_id}'")
if len(post_data) != 1:
print("You found a bug!")
main.close_database(write_changes=False)
exit(0)
post_data = post_data[0]
print('`F222`B999')
print(f"`!{post_data[1]}`!")
print("``")
if post_data[0] != "[DELETED]":
display_name = main.query_database(f"SELECT display_name FROM users WHERE username = '{post_data[0]}'")[0][0]
username_styled = f"{display_name} ``(`Ff22`_`[Profile`:{main.page_path}/profile.mu`username={post_data[0]}]`f`_)"
else:
username_styled = "[DELETED]"
print(f"{username_styled}: ({post_data[3]} (UTC))")
print("-")
print(f"{post_data[2]}")
print("``")
print(">")
elif main.check_uuid(parent) and len(main.query_database(f"SELECT numeric_id FROM comments WHERE comment_id = '{parent}' and post_id = '{post_id}'")) == 1:
comment_data = main.query_database(f"SELECT username, content, datetime(changed, 'unixepoch'), post_id FROM comments WHERE comment_id = '{parent}'")
if len(comment_data) != 1:
print("You found a bug!")
main.close_database(write_changes=False)
exit(0)
comment_data = comment_data[0]
post_title = main.query_database(f"SELECT title FROM posts WHERE post_id = '{comment_data[3]}'")[0][0]
print(f">>Comment on {post_title}\n")
if comment_data[0] != "[DELETED]":
display_name = main.query_database(f"SELECT display_name FROM users WHERE username = '{comment_data[0]}'")[0][0]
username_styled = f"{display_name} ```B888(`Ff22`_`[Profile`:{main.page_path}/profile.mu`username={comment_data[0]}]`f`_)"
else:
username_styled = "[DELETED]"
print(">>>")
print(f"`B888{username_styled}: ({comment_data[2]} (UTC))")
print("``")
print(comment_data[1])
print("``")
print(">")
print()
username = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")
if len(username) != 1:
print("Congratulations, you found a bug!")
main.close_database(write_changes=False)
exit(0)
username = username[0][0]
edit_check_data()
content = main.query_database(f"SELECT content FROM comments WHERE comment_id = '{edit_id}'")[0][0]
content = content.replace("\n", "$newline$")
print(f"Comment: `B444`<70|content`{content}>`b")
print(f"`Ff22`_`[Update comment`:{main.page_path}/comment.mu`*|edit=true|edit_id={edit_id}|post_id={post_id}|parent={parent}|source_link_id={link_id}]`_`f")
try:
link_id, remote_identity = main.handle_ids()
main.print_header(link_id, reload=True)
content = ""
post_id = ""
parent = ""
edit = False
edit_id = ""
for env_variable in os.environ:
if env_variable == "field_content":
content = os.environ[env_variable]
elif env_variable == "var_post_id":
post_id = os.environ[env_variable]
elif env_variable == "var_parent":
parent = os.environ[env_variable]
elif env_variable == "var_edit" and os.environ[env_variable] == "true":
edit = True
elif env_variable == "var_edit_id":
edit_id = os.environ[env_variable]
if len(main.query_database(f"SELECT user_id FROM users WHERE link_id = '{link_id}'")) == 0:
print(">Please log in first.")
elif post_id == "" or parent == "":
print(">something went wrong...")
main.close_database(write_changes=False)
sys.exit(0)
elif not main.check_uuid(post_id):
print(">something went wrong...")
main.close_database(write_changes=False)
sys.exit(0)
elif not (main.check_uuid(parent) or parent == "post"):
print(">something went wrong...")
main.close_database(write_changes=False)
sys.exit(0)
elif len(main.query_database(f"SELECT numeric_id FROM posts WHERE post_id = '{post_id}'")) == 0:
print(">The requested post was not found.")
elif main.query_database(f"SELECT locked FROM posts WHERE post_id = '{post_id}'")[0][0] != 0:
print("This post is locked. You aren't allowed to leave a comment.")
main.close_database(write_changes=False)
sys.exit(0)
elif not (parent == "post" or len(main.query_database(f"SELECT numeric_id FROM comments WHERE comment_id = '{parent}' AND post_id = '{post_id}'")) == 1):
print(">The requested comment was not found")
# prevent huge comments
elif content == "" or len(content) >= 4096:
content = ""
if edit:
print_fields_edit()
else:
print_fields()
elif main.verify_link_id():
content = content.replace("$newline$", "\n")
content = main.prepare_content(content)
username = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")[0][0]
if main.query_database(f"SELECT enabled FROM users WHERE username = '{username}'")[0][0] != 1:
print(">Your account is disabled.")
main.close_database(write_changes=False)
sys.exit(0)
elif len(main.query_database(f"SELECT numeric_id FROM comments WHERE content = '{content}' AND username = '{username}' AND unixepoch() < (changed + 600)")) != 0:
print(">Spam protection triggered!")
main.close_database(write_changes=False)
sys.exit(0)
elif len(main.query_database(f"SELECT numeric_id FROM comments WHERE username = '{username}' AND unixepoch() < (changed + 15)")) != 0:
print(">Spam protection triggered!")
main.close_database(write_changes=False)
sys.exit(0)
if edit:
edit_check_data()
main.execute_sql(f"UPDATE comments SET content = '{content}', changed = unixepoch(), edited = 1 WHERE comment_id = '{edit_id}' AND username = '{username}'")
main.execute_sql(f"UPDATE posts SET last_action = unixepoch() WHERE post_id = '{post_id}'")
print(">Your comment has been edited.")
print()
print(f"`Ff22`_`[Visit`:{main.page_path}/view.mu`post_id={post_id}]`_`f")
else:
comment_id = str(uuid.uuid4())
# this should not be very probable
while len(main.query_database(f"SELECT numeric_id FROM comments WHERE comment_id = '{comment_id}'")) != 0:
comment_id = str(uuid.uuid4())
main.execute_sql(f"INSERT INTO comments (comment_id, post_id, parent, username, content, created, changed) VALUES ('{comment_id}', '{post_id}', '{parent}', '{username}', '{content}', unixepoch(), unixepoch())")
main.execute_sql(f"UPDATE posts SET last_action = unixepoch() WHERE post_id = '{post_id}'")
print(">Your comment has been added.")
print()
print(f"`Ff22`_`[Visit`:{main.page_path}/view.mu`post_id={post_id}]`_`f")
if main.notifications_enabled:
try:
notification_recipients = main.query_database(f"SELECT username FROM subscriptions WHERE post_id = '{post_id}'")
post_title = main.query_database(f"SELECT title FROM posts WHERE post_id = '{post_id}'")[0][0]
content = content.replace("\'\'", "\'")
display_name = main.remove_micron(main.query_database(f"SELECT display_name FROM users WHERE username = '{username}'")[0][0])
notification_text = f"{display_name} commented on {post_title}:\n{main.remove_micron(content)}"
for recipient in notification_recipients:
destinations = main.query_database(f"SELECT remote_id FROM connections WHERE username = '{recipient[0]}' AND send_notifications = 1")
for destination in destinations:
notify.add_notification([main.decrypt(destination[0]), notification_text])
except:
print("An error occured. No notifications were sent. Please contact an admin.")
main.close_database()
except:
print("An error occured")