Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
add messages list and friend req list handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 24, 2020
1 parent 88073d5 commit ee3ac97
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
2 changes: 1 addition & 1 deletion handlers/levelextras.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async def level_scores_handler(request: aiohttp.web.Request) -> aiohttp.web.Resp
account_id = int(
post_data["accountID"]
) # Lets declare this as we will re-use it later
if not auth.check_gjp(account_id, post_data["gjp"]):
if not await auth.check_gjp(account_id, post_data["gjp"]):
return aiohttp.web.Response(text=ResponseCodes.GENERIC_FAIL)

# Creating the current score object.
Expand Down
88 changes: 85 additions & 3 deletions handlers/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
from objects.accounts import (
Account,
) # Makes it WAY easier to work with the objects inside VSCode
from helpers.generalhelper import create_offsets_from_page, joint_string, pipe_string
from helpers.generalhelper import (
create_offsets_from_page,
joint_string,
pipe_string,
string_bool,
paginate_list,
create_offsets_from_page
)
from helpers.timehelper import time_ago
from helpers.auth import auth
from helpers.searchhelper import search_helper
from constants import ResponseCodes, Permissions
from cron.cachelb import top_stars, top_cp
from objects.accounts import FriendRequest, Message # To be nicer to PyLint
import aiohttp
import logging

Expand Down Expand Up @@ -313,7 +321,7 @@ async def friends_list_handler(request: aiohttp.web.Response):
account_id = int(post_data["accountID"])
friends_type = int(post_data["type"])

if not auth.check_gjp(account_id, post_data["gjp"]):
if not await auth.check_gjp(account_id, post_data["gjp"]):
return aiohttp.web.Response(text=ResponseCodes.GENERIC_FAIL)

id_function = { # Coro to get a list of friends ids.
Expand Down Expand Up @@ -350,4 +358,78 @@ async def friends_list_handler(request: aiohttp.web.Response):
return aiohttp.web.Response(text=response)

async def friend_req_handler(request: aiohttp.web.Response):
"""Handles friend requests ."""
"""Handles friend requests."""

post_data = await request.post()
account_id = int(post_data["accountID"])

if not await auth.check_gjp(account_id, post_data["gjp"]):
return aiohttp.web.Response(text=ResponseCodes.GENERIC_FAIL)

sent = string_bool(post_data.get("getSent", "0"))
page = int(post_data["page"])
offset = create_offsets_from_page(page, 10)
requests = []
response = ""

if sent:
requests = await user_helper.get_friend_requests_from(account_id)
else:
requests = await user_helper.get_friend_requests_to(account_id)

for req in paginate_list(requests, page, 10):
req : FriendRequest
user = await user_helper.get_object(req.target_id if sent else req.account_id)
response += joint_string({
1: user.username,
2: user.user_id,
9: user.icon,
10: user.colour1,
11: user.colour2,
14: user.icon_type,
15: 0,
16: user.account_id,
32: req.id,
35: req.content_base64,
37: req.timestamp,
41: req.new
}) + "|"

response = f"{response[:-1]}#{len(requests)}:{offset}:10"
logging.debug(response)
return aiohttp.web.Response(text=response)

async def message_list_handler(request: aiohttp.web.Response):
"""Handles message list."""

# Looks familiar huh??
post_data = await request.post()
account_id = int(post_data["accountID"])

if not await auth.check_gjp(account_id, post_data["gjp"]):
return aiohttp.web.Response(text=ResponseCodes.GENERIC_FAIL)

sent = string_bool(post_data.get("getSent", "0"))
page = int(post_data["page"])
offset = create_offsets_from_page(page, 10)
messages = await user_helper.get_messages(account_id, sent, page)
message_count = await user_helper.get_message_count(account_id, sent)
response = ""

for msg in messages:
msg: Message
user = await user_helper.get_object(msg.target_id if not sent else msg.account_id)
response += joint_string({
1: msg.id,
2: user.account_id,
3: user.user_id,
4: msg.subject_base64,
6: user.username,
7: time_ago(msg.timestamp),
8: msg.read,
9: int(sent)
}) + "|"

response = f"{response[:-1]}#{message_count}:{offset}:10"
logging.debug(response)
return aiohttp.web.Response(text=response)
30 changes: 29 additions & 1 deletion helpers/userhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from helpers.timehelper import get_timestamp, Timer
from helpers.crypthelper import decode_base64, hash_bcrypt, encode_base64
from helpers.lang import lang
from objects.accounts import Account, AccountExtras, FriendRequest
from objects.accounts import Account, AccountExtras, FriendRequest, Message
from objects.comments import AccountComment
from conn.mysql import myconn
from constants import Permissions, Relationships
Expand Down Expand Up @@ -552,5 +552,33 @@ async def gdpr_delete(self, account_id : int) -> None:

await mycursor.execute("DELETE FROM comments WHERE userID = %s", (user.user_id,))
await myconn.conn.commit()

async def get_messages(self, account_id : int, get_from : bool = False, page : int = 0):
"""Gets list of message objects for user"""
offset = page * 10
async with myconn.conn.cursor() as mycursor:
await mycursor.execute(f"SELECT userID, userName, body, subject, accID, messageID, toAccountID, timestamp, isNew FROM messages WHERE {'accID' if not get_from else 'toAccountID'} = %s LIMIT 10 OFFSET %s", (account_id, offset))
messages_db = await mycursor.fetchall()

return [
Message(
user_id = i[0],
username=i[1],
content_base64=i[2],
subject_base64=i[3],
account_id=i[4],
id = i[5],
target_id=i[6],
timestamp=int(i[7]),
read= i[8]
)
for i in messages_db
]

async def get_message_count(self, account_id : int, get_from : bool = False) -> int:
"""Returns the number of messages a user has."""
async with myconn.conn.cursor() as mycursor:
await mycursor.execute(f"SELECT COUNT(*) FROM messages WHERE {'accID' if not get_from else 'toAccountID'} = %s", (account_id,))
return (await mycursor.fetchall())[0]

user_helper = UserHelper() # This has to be a common class.
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
leaderboards_handler,
mod_check_handler,
friends_list_handler,
friend_req_handler,
message_list_handler
)
from handlers.songs import featured_artists_handler, get_songinfo_handler
from handlers.levels import (
Expand Down Expand Up @@ -88,6 +90,8 @@ def config_routes(app: web.Application) -> None:
("/database/getGJAccountComments20.php", profile_comment_handler),
("/database/accounts/registerGJAccount.php", register_handler),
("/database/accounts/loginGJAccount.php", login_handler),
("/database/getGJFriendRequests20.php", friend_req_handler),
("/database/getGJMessages20.php", message_list_handler)
]

for r, h in routes:
Expand Down
22 changes: 22 additions & 0 deletions objects/accounts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from helpers.crypthelper import decode_base64, encode_base64


@dataclass
Expand Down Expand Up @@ -63,3 +64,24 @@ class FriendRequest:
content: str
timestamp: int
new: bool

@dataclass
class Message:
"""Message dataclass."""
user_id : int
username : str
content_base64 : str
subject_base64 : str
account_id : int
id : int
target_id : int
timestamp : int
read : bool

@property
def content(self) -> str:
return decode_base64(self.content_base64)

@content.setter
def content_setter(self, set_to : str):
self.content_base64 = encode_base64(set_to)

0 comments on commit ee3ac97

Please sign in to comment.