This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
forked from meower-media/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
169 lines (144 loc) · 5.91 KB
/
files.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
from pymongo import MongoClient, ASCENDING, DESCENDING, TEXT
import time
from uuid import uuid4
import os
from dotenv import load_dotenv
"""
Meower Files Module
This module provides filesystem functionality and a primitive JSON-file based database interface.
This file should be modified/refactored to interact with a JSON-friendly database server instead of filesystem directories and files.
"""
load_dotenv()
class Files:
def __init__(self, logger, errorhandler):
self.log = logger
self.errorhandler = errorhandler
mongo_uri = os.getenv("MONGO_URI", "mongodb://127.0.0.1:27017")
self.log("Connecting to database...\n(If it seems like the server is stuck or the server randomly crashes, it probably means it couldn't connect to the database)")
self.db = MongoClient(mongo_uri)["meowerserver"]
# Check connection status
if self.db.client.get_database("meowerserver") == None:
self.log("Failed to connect to MongoDB database!")
else:
self.log("Connected to database")
# Create database collections
for item in ["config", "usersv0", "netlog", "posts", "chats", "reports"]:
if not item in self.db.list_collection_names():
self.log("Creating collection {0}".format(item))
self.db.create_collection(name=item)
# Create collection indexes
try:
self.db["netlog"].create_index([("users", ASCENDING)], name="users")
except: pass
try:
self.db["usersv0"].create_index([("lower_username", ASCENDING), ("created", DESCENDING)], name="lower_username")
except: pass
try:
self.db["posts"].create_index([("post_origin", ASCENDING), ("isDeleted", ASCENDING), ("t.e", DESCENDING), ("u", ASCENDING)], name="default", partialFilterExpression={"isDeleted": False})
except: pass
try:
self.db["posts"].create_index([("post_origin", ASCENDING), ("isDeleted", ASCENDING), ("t.e", DESCENDING), ("p", TEXT)], name="content_search", partialFilterExpression={"isDeleted": False})
except: pass
try:
self.db["posts"].create_index([("post_origin", ASCENDING), ("isDeleted", ASCENDING), ("u", ASCENDING), ("t.e", DESCENDING)], name="user_search", partialFilterExpression={"isDeleted": False})
except: pass
try:
self.db["chats"].create_index([("members", ASCENDING), ("last_active", DESCENDING)], name="user_chats")
except: pass
# Create reserved accounts
for username in ["Server", "Deleted", "Meower", "Admin", "username"]:
self.create_item("usersv0", username, {
"lower_username": username.lower(),
"created": int(time.time()),
"uuid": str(uuid4()),
"unread_inbox": False,
"theme": "",
"mode": None,
"sfx": None,
"debug": None,
"bgm": None,
"bgm_song": None,
"layout": None,
"pfp_data": None,
"quote": None,
"email": None,
"pswd": None,
"tokens": [],
"lvl": None,
"banned": False,
"last_ip": None
})
# Create IP banlist file
self.create_item("config", "IPBanlist", {
"wildcard": [],
"users": {}
})
# Create Version support file
self.create_item("config", "supported_versions", {
"index": [
"scratch-beta-5-r7",
]
})
# Create Trust Keys file
self.create_item("config", "trust_keys", {
"index": [
"meower",
]
})
# Create Filter file
self.create_item("config", "filter", {
"whitelist": [],
"blacklist": []
})
# Create status file
self.create_item("config", "status", {
"repair_mode": False,
"is_deprecated": False
})
self.log("Files initialized!")
def does_item_exist(self, collection, id):
if self.db[collection].count_documents({"_id": id}) > 0:
return True
else:
return False
def create_item(self, collection, id, data):
if collection in self.db.list_collection_names():
if not self.does_item_exist(collection, id):
data["_id"] = id
self.db[collection].insert_one(data)
return True
else:
self.log("{0} already exists in {1}".format(id, collection))
return False
else:
self.log("{0} collection doesn't exist".format(collection))
return False
def update_item(self, collection, id, data):
if self.does_item_exist(collection, id):
self.db[collection].update_one({"_id": id}, {"$set": data})
return True
else:
return False
def write_item(self, collection, id, data):
if self.does_item_exist(collection, id):
data["_id"] = id
self.db[collection].find_one_and_replace({"_id": id}, data)
return True
else:
return False
def load_item(self, collection, id):
item = self.db[collection].find_one({"_id": id})
if item:
return True, item
else:
return False, None
def find_items(self, collection, query):
return [item["_id"] for item in self.db[collection].find(query, projection={"_id": 1})]
def count_items(self, collection, query):
return self.db[collection].count_documents(query)
def delete_item(self, collection, id):
if self.does_item_exist(collection, id):
self.db[collection].delete_one({"_id": id})
return True
else:
return False