-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.mu
executable file
·140 lines (129 loc) · 7.25 KB
/
view.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
#!/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 main
def print_fields():
print(f"Post ID: `B444`<post_id`{post_id}>`b")
print(f"`Ff22`_`[Visit post`:{main.page_path}/view.mu`*]`_`f")
def display_comment(parent: str, indent: int):
comments = main.query_database(f"SELECT comment_id, username, content, datetime(changed, 'unixepoch'), edited FROM comments WHERE post_id = '{post_id}' AND parent = '{parent}'")
for comment_data in comments:
if comment_data[1] != "[DELETED]":
display_name = main.query_database(f"SELECT display_name FROM users WHERE username = '{comment_data[1]}'")[0][0]
username_styled = f"{display_name}```B888 (`Ff22`_`[Profile`:{main.page_path}/profile.mu`username={comment_data[1]}]`f`_)"
else:
username_styled = "[DELETED]"
edited_status_message = ""
if comment_data[4] == 1:
edited_status_message = " [edited]"
print(">" * (2 + indent))
print(f"`B888{username_styled}: ({comment_data[3]} (UTC)){edited_status_message}")
print("``")
print(comment_data[2])
print("``")
comment_link, delete_link, edit_link = "", "", ""
if not comments_locked:
comment_link = f"`Ff22`_`[Add a comment`:{main.page_path}/comment.mu`post_id={post_id}|parent={comment_data[0]}]`_`f"
query_result = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")
if len(query_result) == 1:
if main.check_admin(link_id) or comment_data[1] == query_result[0][0]:
delete_link = f" `Ff22`_`[Delete`:{main.page_path}/delete_content.mu`content_id={comment_data[0]}|type=comment]`_`f"
if comment_data[1] == query_result[0][0]:
edit_link = f" `Ff22`_`[Edit`:{main.page_path}/comment.mu`edit=true|edit_id={comment_data[0]}|post_id={post_id}|parent={parent}]`_`f"
print(f"{comment_link}{delete_link}{edit_link}")
display_comment(comment_data[0], indent + 1)
try:
link_id, remote_identity = main.handle_ids()
main.print_header(link_id)
post_id = ""
action = ""
for env_variable in os.environ:
if env_variable == "var_post_id":
post_id = os.environ[env_variable]
elif env_variable == "var_action":
action = os.environ[env_variable]
elif env_variable == "field_post_id":
post_id = os.environ[env_variable]
if not main.check_uuid(post_id):
print("invalid id")
print_fields()
elif post_id == "":
print_fields()
elif len(main.query_database(f"SELECT numeric_id FROM posts WHERE post_id = '{post_id}'")) == 0:
print("post not found")
print_fields()
else:
username = ""
query_result = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")
if len(query_result) == 1:
username = query_result[0][0]
if action == "subscribe" and main.verify_link_id():
if len(main.query_database(f"SELECT sub_id FROM subscriptions WHERE post_id = '{post_id}' AND username = '{username}'")) == 0:
main.execute_sql(f"INSERT INTO subscriptions (username, post_id) VALUES ('{username}', '{post_id}')")
elif action == "unsubscribe" and main.verify_link_id():
main.execute_sql(f"DELETE FROM subscriptions WHERE post_id = '{post_id}' AND username = '{username}'")
elif action == "lock" and main.check_admin(link_id) and main.verify_link_id():
main.execute_sql(f"UPDATE posts SET locked = 1 WHERE locked = 0 AND post_id = '{post_id}'")
elif action == "unlock" and main.check_admin(link_id) and main.verify_link_id():
main.execute_sql(f"UPDATE posts SET locked = 0 WHERE locked = 1 AND post_id = '{post_id}'")
post_data = main.query_database(f"SELECT username, title, content, datetime(changed, 'unixepoch'), edited FROM posts WHERE post_id = '{post_id}'")[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]"
edited_status_message = ""
if post_data[4] == 1:
edited_status_message = " [edited]"
print(f"{username_styled}: ({post_data[3]} (UTC)){edited_status_message}")
print("-")
print(f"{post_data[2]}")
print("``")
print(">")
print("-")
if len(username) >= 4 and main.notifications_enabled:
if len(main.query_database(f"SELECT sub_id FROM subscriptions WHERE post_id = '{post_id}' AND username = '{username}'")) == 0:
print(f"`Ff22`_`[Subscribe`:{main.page_path}/view.mu`post_id={post_id}|action=subscribe|source_link_id={link_id}]`_`f")
else:
print(f"`Ff22`_`[Unsubscribe`:{main.page_path}/view.mu`post_id={post_id}|action=unsubscribe|source_link_id={link_id}]`_`f")
lock_link, delete_link, edit_link = "", "", ""
if main.query_database(f"SELECT locked FROM posts WHERE post_id = '{post_id}'")[0][0] == 0:
comments_locked = False
comment_link = f"`Ff22`_`[Add comment`:{main.page_path}/comment.mu`post_id={post_id}|parent=post]`_`f"
else:
comments_locked = True
comment_link = "[LOCKED]"
if main.check_admin(link_id):
if comments_locked:
lock_link = f" `Ff22`_`[Unlock`:{main.page_path}/view.mu`post_id={post_id}|action=unlock|source_link_id={link_id}]`_`f"
else:
lock_link = f" `Ff22`_`[Lock`:{main.page_path}/view.mu`post_id={post_id}|action=lock|source_link_id={link_id}]`_`f"
query_result = main.query_database(f"SELECT username FROM users WHERE link_id = '{link_id}'")
if len(query_result) == 1:
if main.check_admin(link_id) or post_data[0] == query_result[0][0]:
delete_link = f" `Ff22`_`[Delete`:{main.page_path}/delete_content.mu`content_id={post_id}|type=post]`_`f"
if post_data[0] == query_result[0][0]:
edit_link = f" `Ff22`_`[Edit`:{main.page_path}/post.mu`edit=true|edit_id={post_id}]`_`f"
print(f"Comments {comment_link}{lock_link}{delete_link}{edit_link}")
# view comments
display_comment("post", 0)
main.close_database()
except:
print("An error occured")