mirror of
https://github.com/gryf/slack-backup.git
synced 2025-12-17 11:30:25 +01:00
Adapt to Slack file atachement API changes
Couple of months ago, file attachments was treated differently, than now - API provides attached files as a list, instead of single object, and is directly coupled with a message object. This change provides support for such cases.
This commit is contained in:
@@ -238,7 +238,8 @@ class Client(object):
|
|||||||
|
|
||||||
user = self._get_user(data)
|
user = self._get_user(data)
|
||||||
|
|
||||||
if not any((data.get('attachments'), data['text'].strip())):
|
if not any((data.get('attachments'), data['text'].strip(),
|
||||||
|
data.get('files'))):
|
||||||
logging.info("Skipping message from `%s' since it's empty",
|
logging.info("Skipping message from `%s' since it's empty",
|
||||||
user.name)
|
user.name)
|
||||||
return
|
return
|
||||||
@@ -254,22 +255,25 @@ class Client(object):
|
|||||||
for reaction_data in data['reactions']:
|
for reaction_data in data['reactions']:
|
||||||
message.reactions.append(o.Reaction(reaction_data))
|
message.reactions.append(o.Reaction(reaction_data))
|
||||||
|
|
||||||
if data.get('subtype') == 'file_share':
|
if data.get('files'):
|
||||||
if (self._url_file_to_attachment and
|
for fdata in data['files']:
|
||||||
data['file'].get('is_external')):
|
if (self._url_file_to_attachment and fdata.get('is_external')):
|
||||||
fdata = data['file']
|
logging.info('got external file')
|
||||||
# change message type from file_share to default
|
message.text = (message.text.split('shared a file:')[0] +
|
||||||
message.type = ''
|
'shared a file: ')
|
||||||
message.text = (message.text.split('shared a file:')[0] +
|
logging.debug("Found external file `%s'. Saving as "
|
||||||
'shared a file: ')
|
"attachment.", fdata['url_private'])
|
||||||
logging.debug("Found external file `%s'. Saving as "
|
self._att_data(message, [{'title': fdata['name'],
|
||||||
"attachment.", fdata['url_private'])
|
'text': fdata['url_private'],
|
||||||
self._att_data(message, [{'title': fdata['name'],
|
'fallback': ''}])
|
||||||
'text': fdata['url_private'],
|
else:
|
||||||
'fallback': ''}])
|
self._file_data(message, fdata)
|
||||||
else:
|
|
||||||
self._file_data(message, data['file'])
|
# TODO(gryf): subtype pinned_item coexistsing with pinned_info message
|
||||||
elif data.get('subtype') == 'pinned_item':
|
# key :C
|
||||||
|
# pinned_info however is just a mark, which point to the channlel
|
||||||
|
# where it is pinned to, who did that and when. To be resolved.
|
||||||
|
if data.get('subtype') == 'pinned_item':
|
||||||
if data.get('attachments'):
|
if data.get('attachments'):
|
||||||
self._att_data(message, data['attachments'])
|
self._att_data(message, data['attachments'])
|
||||||
elif data.get('item'):
|
elif data.get('item'):
|
||||||
@@ -284,26 +288,34 @@ class Client(object):
|
|||||||
Process file data. Could be either represented as 'file' object or
|
Process file data. Could be either represented as 'file' object or
|
||||||
'item' object in case of pinned items
|
'item' object in case of pinned items
|
||||||
"""
|
"""
|
||||||
message.file = o.File(data)
|
_file = o.File(data)
|
||||||
|
message.files.append(_file)
|
||||||
|
|
||||||
|
if data.get('mode') == 'tombstone':
|
||||||
|
_file.title = 'This file was deleted'
|
||||||
|
return
|
||||||
|
|
||||||
if data.get('is_starred'):
|
if data.get('is_starred'):
|
||||||
message.is_starred = True
|
message.is_starred = True
|
||||||
|
|
||||||
if data.get('is_external'):
|
if data.get('is_external'):
|
||||||
# Create a link and corresponding file name for manual download
|
# Create a link and corresponding file name for manual download
|
||||||
fname = str(uuid.uuid4())
|
fname = str(uuid.uuid4())
|
||||||
message.file.filepath = self.downloader.get_filepath(fname, 'file')
|
_file.filepath = self.downloader.get_filepath(fname, 'file')
|
||||||
logging.info("Please, manually download an external file from "
|
logging.info("Please, manually download an external file from "
|
||||||
"URL `%s' to `%s'", data['url_private'],
|
"URL `%s' to `%s'", data['url_private'],
|
||||||
message.file.filepath)
|
_file.filepath)
|
||||||
self._dldata.append('%s --> %s\n' % (data['url_private'],
|
self._dldata.append('%s --> %s\n' % (data['url_private'],
|
||||||
message.file.filepath))
|
_file.filepath))
|
||||||
message.file.url = data['url_private']
|
_file.url = data['url_private']
|
||||||
else:
|
else:
|
||||||
logging.debug("Found internal file `%s'",
|
logging.debug("Found internal file `%s'",
|
||||||
data['url_private_download'])
|
data['url_private_download'])
|
||||||
priv_url = data['url_private_download']
|
priv_url = data['url_private_download']
|
||||||
message.file.filepath = self.downloader.download(priv_url, 'file')
|
_file.filepath = self.downloader.download(priv_url, 'file',
|
||||||
self.session.add(message.file)
|
data.get('filetype'))
|
||||||
|
|
||||||
|
self.session.add(_file)
|
||||||
|
|
||||||
def _att_data(self, message, data):
|
def _att_data(self, message, data):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ class Message(Base):
|
|||||||
channel = relationship("Channel", back_populates="messages")
|
channel = relationship("Channel", back_populates="messages")
|
||||||
|
|
||||||
reactions = relationship("Reaction", back_populates="message")
|
reactions = relationship("Reaction", back_populates="message")
|
||||||
file = relationship("File", uselist=False, back_populates="message")
|
files = relationship("File", back_populates="message")
|
||||||
attachments = relationship("Attachment", back_populates="message")
|
attachments = relationship("Attachment", back_populates="message")
|
||||||
|
|
||||||
def __init__(self, data_dict=None):
|
def __init__(self, data_dict=None):
|
||||||
@@ -255,7 +255,7 @@ class File(Base):
|
|||||||
filepath = Column(Text)
|
filepath = Column(Text)
|
||||||
|
|
||||||
message_id = Column(Integer, ForeignKey('messages.id'))
|
message_id = Column(Integer, ForeignKey('messages.id'))
|
||||||
message = relationship('Message', back_populates='file')
|
message = relationship('Message', back_populates='files')
|
||||||
|
|
||||||
def __init__(self, data_dict=None):
|
def __init__(self, data_dict=None):
|
||||||
self.update(data_dict)
|
self.update(data_dict)
|
||||||
|
|||||||
Reference in New Issue
Block a user