-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
134 lines (110 loc) · 4.85 KB
/
database.py
File metadata and controls
134 lines (110 loc) · 4.85 KB
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
from supabase import create_client, Client
import os
from uuid import uuid4
from datetime import datetime
import random
from werkzeug.datastructures import FileStorage
from config import Config
class Database:
def __init__(self):
url: str = Config.SUPABASE_URL
key: str = Config.SUPABASE_KEY
self.supabase: Client = create_client(url, key)
self.users = self.supabase.table("zap_users")
self.posts = self.supabase.table("zap_posts")
def fetch_user(self, key, value):
res = self.users.select("*").eq(key, value).execute().model_dump()['data']
if len(res) == 0:
return None
return res[0]
def create_user(self, email, name):
if self.fetch_user("email", email) is not None:
return False
user_obj = {
"_id": str(uuid4()),
"name": name,
"email": email,
"username": email.split("@")[0] + "".join([str(random.randint(0,9)) for _ in range(5)]),
"bio": "",
"posts": [],
"history": [],
"followers": [],
"following": []
}
self.users.insert(user_obj).execute()
return user_obj
def edit_profile(self, email, username, name, bio):
self.users.update({"username": username, "name": name, "bio": bio}).eq("email", email).execute()
def upload_file(self, location, file, content_type):
key = self.supabase.storage.from_("Zap")\
.upload(file=file,
path=location,
file_options={"content-type": content_type}).json()['Key']
return f"{Config.SUPABASE_URL}/storage/v1/object/public/{key}"
def create_post(self, user, caption, img:FileStorage, tags):
post_id = str(uuid4())
mime = img.content_type
url = self.upload_file(f"Posts/{post_id}.{mime.split('/')[1]}", img.read(), mime)
post = {
"_id": post_id,
"caption": caption,
"img_url": url,
"user": user,
"likes": [],
"comments": [],
"tags": tags
}
self.posts.insert(post).execute()
posts = self.users.select("posts").eq("_id", user).execute().model_dump()['data'][0]['posts']
posts.append(post_id)
self.users.update({"posts": posts}).eq("_id", user).execute()
return post
def get_posts(self, *post_ids):
post = self.posts.select("*").in_("_id", post_ids).execute().model_dump()['data']
return post
def add_comment(self, user_id, post_id, comment):
user = self.fetch_user("_id", user_id)
post = self.get_posts(post_id)
if user is None or len(post) == 0 or comment.strip() == "":
return False
post = post[0]
post['comments'].append({"userid": user_id, "username": user['name'], "comment": comment, "on": datetime.now().timestamp()})
self.posts.update({"comments": post['comments']}).eq("_id", post_id).execute()
return True
def toggle_like(self, user_id, post_id):
user = self.fetch_user("_id", user_id)
post = self.get_posts(post_id)
if user is None or len(post) == 0:
return False
post = post[0]
if user_id in post['likes']:
post['likes'].remove(user_id)
else:
post['likes'].append(user_id)
self.posts.update({"likes": post['likes']}).eq("_id", post_id).execute()
return True
def attach_username(self, posts):
users = [i['user'] for i in posts]
user_objs = self.supabase.table("zap_users").select("*").in_("_id", users).execute().model_dump()['data']
new_posts = []
for post in posts:
post['username'] = {i['_id']: i['username'] for i in user_objs}[post['user']]
new_posts.append(post)
return new_posts
def random_posts(self):
return self.attach_username(self.supabase.table("random_posts").select("*").limit(5).execute().model_dump()['data'])
def toggle_follow(self, follower, following):
user = self.fetch_user("_id", follower)
other = self.fetch_user("_id", following)
if following not in user['following']:
user['following'].append(following)
other['followers'].append(follower)
else:
user['following'].remove(following)
other['followers'].remove(follower)
self.users.update({"following": user['following']}).eq("_id", follower).execute()
self.users.update({"followers": other['followers']}).eq("_id", following).execute()
return True
def get_followed_content(self, user_id):
users = self.fetch_user("_id", user_id)['following']
return self.attach_username(self.supabase.table("random_posts").select("*").in_("user", users).limit(5).execute().model_dump()['data'])