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

Commit

Permalink
download message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 25, 2020
1 parent ee3ac97 commit ecd0de0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"debug": False,
"level_path": "data/levels/",
"save_path": "data/saves/",
"command_prefix": "!",
"command_prefix": "/",
"default_priv": 30,
"cache_level_strs": True,
"lang": "en",
Expand Down
39 changes: 39 additions & 0 deletions handlers/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from helpers.timehelper import time_ago
from helpers.auth import auth
from helpers.searchhelper import search_helper
from helpers.lang import lang
from constants import ResponseCodes, Permissions
from cron.cachelb import top_stars, top_cp
from objects.accounts import FriendRequest, Message # To be nicer to PyLint
Expand Down Expand Up @@ -433,3 +434,41 @@ async def message_list_handler(request: aiohttp.web.Response):
response = f"{response[:-1]}#{message_count}:{offset}:10"
logging.debug(response)
return aiohttp.web.Response(text=response)

async def download_message_handler(request: aiohttp.web.Response):
"""Handles fetching a message for the client."""

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)

message_id = int(post_data["messageID"])
sender = int(post_data.get("isSender", 0))

message = await user_helper.get_message(message_id, account_id)

if message is None:
logging.debug(lang.debug("message_not_found", message_id))
return aiohttp.web.Response(text=ResponseCodes.GENERIC_FAIL)

# Get the user object.
user = await user_helper.get_object(account_id if sender else message.target_id)

response = joint_string({
2: user.account_id,
5: message.content_base64,
6: user.username,
1: message.id,
3: user.user_id,
4: message.subject_base64,
8: message.read,
9: sender,
7: time_ago(message.timestamp)
})

await user_helper.mark_message_as_read(message_id)
logging.debug(response)

return aiohttp.web.Response(text=response)
1 change: 1 addition & 0 deletions helpers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def cache_object(self, cache_id : int, cache_obj : object) -> None:
"expire" : get_timestamp() + self._lenght,
"object" : cache_obj
}
self.run_checks()

def remove_cache_object(self, cache_id : int) -> None:
"""Removes an object from cache."""
Expand Down
43 changes: 42 additions & 1 deletion helpers/userhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ async def get_messages(self, account_id : int, get_from : bool = False, page : i
"""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))
await mycursor.execute(f"SELECT userID, userName, body, subject, accID, messageID, toAccountID, timestamp, isNew FROM messages WHERE {'toAccountID' if not get_from else 'accID'} = %s LIMIT 10 OFFSET %s", (account_id, offset))
messages_db = await mycursor.fetchall()

return [
Expand All @@ -580,5 +580,46 @@ async def get_message_count(self, account_id : int, get_from : bool = False) ->
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]

async def get_message(self, message_id: int, filter: int = None) -> Message:
"""Returns a message obj from message_id.
Params:
message_id: The int ID of the message you are trying to fetch.
filters: The int of account ID that the message can be send by or to. This is to prevent users from accessing others' messages."""

async with myconn.conn.cursor() as mycursor:
# We will do filter check
await mycursor.execute("SELECT userID, userName, body, subject, accID, messageID, toAccountID, timestamp, isNew FROM messages WHERE messageID = %s LIMIT 1", (message_id))
message_db = await mycursor.fetchone()

# No message like that exists.
if message_db is None:
return None

# Object creation
obj = Message(
user_id = message_db[0],
username=message_db[1],
content_base64=message_db[2],
subject_base64=message_db[3],
account_id=message_db[4],
id = message_db[5],
target_id=message_db[6],
timestamp=int(message_db[7]),
read= message_db[8]
)

# There is a filter given.
if filter:
if obj.account_id != filter and obj.target_id != filter:
return None

return obj

async def mark_message_as_read(self, message_id: int) -> None:
"""Marks a message as read."""
async with myconn.conn.cursor() as mycursor:
await mycursor.execute("UPDATE messages SET isNew = 1 WHERE messageID = %s LIMIT 1", (message_id,))
await myconn.conn.commit()

user_helper = UserHelper() # This has to be a common class.
3 changes: 2 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"level_scores_gotten" : "Fetched level leaderboards for level {0}.",
"no_score" : "This user has not set a score on this level.",
"overwrite_score" : "Overwriting this score.",
"adding_handler" : "Adding handler: {0} | {1}"
"adding_handler" : "Adding handler: {0} | {1}",
"message_not_found" : "The message with the ID of {0} could not be found! It either does not exist or the user lacks the sufficient privileges to access it."
},
"runtime": {
"shutdown": "Shutting down! Bye!",
Expand Down
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
mod_check_handler,
friends_list_handler,
friend_req_handler,
message_list_handler
message_list_handler,
download_message_handler
)
from handlers.songs import featured_artists_handler, get_songinfo_handler
from handlers.levels import (
Expand Down Expand Up @@ -91,7 +92,8 @@ def config_routes(app: web.Application) -> None:
("/database/accounts/registerGJAccount.php", register_handler),
("/database/accounts/loginGJAccount.php", login_handler),
("/database/getGJFriendRequests20.php", friend_req_handler),
("/database/getGJMessages20.php", message_list_handler)
("/database/getGJMessages20.php", message_list_handler),
("/database/downloadGJMessage20.php", download_message_handler)
]

for r, h in routes:
Expand Down

0 comments on commit ecd0de0

Please sign in to comment.