blueprints: add dummy attachment blueprint

- storage: add message attachment fetching (not working)
This commit is contained in:
Luna 2018-12-08 23:12:43 -03:00
parent ec062f75a8
commit 9ec8577b7f
3 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,29 @@
"""
Litecord
Copyright (C) 2018 Luna Mendes
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, version 3 of the License.
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 <http://www.gnu.org/licenses/>.
"""
from quart import Blueprint
bp = Blueprint(__name__)
@bp.route('/<channel_id:int>/<message_id:int>/<filename:int>.<ext>',
methods=['GET'])
async def _get_attachment(channel_id: int, message_id: int,
filename: str, ext: str):
# TODO: get the attachment id with given metadata
return

View File

@ -388,6 +388,8 @@ async def _add_attachment(message_id: int, attachment_file) -> int:
attachment_id, attachment_file.filename, file_size, attachment_id, attachment_file.filename, file_size,
is_image, img_width, img_height) is_image, img_width, img_height)
# TODO: save a file
# add the newly created attachment to the message # add the newly created attachment to the message
await app.db.execute(""" await app.db.execute("""
INSERT INTO message_attachments (message_id, attachment_id) INSERT INTO message_attachments (message_id, attachment_id)

View File

@ -31,6 +31,7 @@ from litecord.blueprints.user.billing import PLAN_ID_TO_TYPE
from litecord.types import timestamp_ from litecord.types import timestamp_
from litecord.utils import pg_set_json from litecord.utils import pg_set_json
from litecord.embed.sanitizer import proxify
log = Logger(__name__) log = Logger(__name__)
@ -636,6 +637,34 @@ class Storage:
# they were defined in the first loop. # they were defined in the first loop.
return list(map(react_stats.get, emoji)) return list(map(react_stats.get, emoji))
async def get_attachments(self, message_id: int) -> List[Dict[str, Any]]:
"""Get a list of attachment objects tied to the message."""
attachment_ids = await self.db.fetch("""
SELECT attachment_id
FROM message_attachments
WHERE message_id = $1
""", message_id)
res = []
for attachment_id in attachment_ids:
row = await self.db.fetchrow("""
SELECT id::text, filename, filesize, image, height, width
FROM attachments
WHERE id = $1
""", attachment_id)
drow = dict(row)
drow['size'] = drow['filesize']
drow.pop('size')
# TODO: url, proxy_url
res.append(drow)
return res
async def get_message(self, message_id: int, user_id=None) -> Dict: async def get_message(self, message_id: int, user_id=None) -> Dict:
"""Get a single message's payload.""" """Get a single message's payload."""
row = await self.fetchrow_with_json(""" row = await self.fetchrow_with_json("""
@ -703,7 +732,7 @@ class Storage:
res.pop('author_id') res.pop('author_id')
# TODO: res['attachments'] # TODO: res['attachments']
res['attachments'] = [] res['attachments'] = await self.get_attachments(message_id)
# TODO: res['member'] for partial member data # TODO: res['member'] for partial member data
# of the author # of the author