def _create_message(self, data, channel): """ Create message with corresponding possible metadata, like reactions, files etc. """ if data['type'] != 'message': logging.info("Skipping message of type `%s'.", data['type']) return logging.debug('Message data: %s', json.dumps(data)) user = self._get_user(data) if not any((data.get('attachments'), data['text'].strip(), data.get('files'))): logging.info("Skipping message from `%s' since it's empty", user.name) return message = o.Message(data) message.channel = channel message.user = user if data.get('is_starred'): message.is_starred = True if 'reactions' in data: for reaction_data in data['reactions']: message.reactions.append(o.Reaction(reaction_data)) if data.get('files'): for fdata in data['files']: if (self._url_file_to_attachment and fdata.get('is_external')): logging.info('got external file') message.text = (message.text.split('shared a file:')[0] + 'shared a file: ') logging.debug("Found external file `%s'. Saving as " "attachment.", fdata['url_private']) self._att_data(message, [{'title': fdata['name'], 'text': fdata['url_private'], 'fallback': ''}]) else: self._file_data(message, fdata) # TODO(gryf): subtype pinned_item coexistsing with pinned_info message # 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'): self._att_data(message, data['attachments']) elif data.get('item'): self._file_data(message, data['item']) elif data.get('attachments'): self._att_data(message, data['attachments']) self.session.add(message)
def test_file_data(self, md, dl): dl.side_effect = ['some_path'] url = INTERNAL_DATA['files'][0]['url_private_download'] cl = client.Client(FakeArgs()) cl.downloader._download = mock.MagicMock(return_value=url) cl.session = mock.MagicMock() msg = o.Message() cl._file_data(msg, INTERNAL_DATA['files'][0]) self.assertIsNotNone(msg.files) self.assertEqual(msg.files[0].filepath, 'some_path')
def test_starred_file_data(self, md, dl): dl.side_effect = ['some_path'] data = copy.deepcopy(INTERNAL_DATA) data['files'][0]['is_starred'] = True url = data['files'][0]['url_private_download'] cl = client.Client(FakeArgs()) cl.downloader._download = mock.MagicMock(return_value=url) cl.session = mock.MagicMock() msg = o.Message() cl._file_data(msg, data['files'][0]) self.assertTrue(msg.is_starred)
def _create_message(self, data, channel): """ Create message with corresponding possible metadata, like reactions, files etc. """ if data['type'] != 'message': logging.info("Skipping message of type `%s'.", data['type']) return logging.debug('Message data: %s', json.dumps(data)) try: user = self.q(o.User).\ filter(o.User.slackid == data['user']).one() except KeyError: user = self.q(o.User).\ filter(o.User.slackid == data['comment']['user']).one() if not data['text'].strip(): logging.info("Skipping message from `%s' since it's empty", user.name) return message = o.Message(data) message.channel = channel message.user = user if data.get('is_starred'): message.is_starred = True if 'reactions' in data: for reaction_data in data['reactions']: message.reactions.append(o.Reaction(reaction_data)) if data.get('subtype') == 'file_share': self._file_data(message, data['file'], data['file']['is_external']) elif data.get('subtype') == 'pinned_item': if data.get('attachments'): self._att_data(message, data['attachments']) elif data.get('item'): self._file_data(message, data['item'], data['item']['is_external']) elif data.get('attachments'): self._att_data(message, data['attachments']) self.session.add(message)
def test_external_file_data(self, md, dl, uuid): uuid.side_effect = ['aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'] dl.side_effect = ['some_path'] cl = client.Client(FakeArgs()) cl.session = mock.MagicMock() # pretend, that we are authorized cl.downloader._authorized = True msg = o.Message() expexted_line = ('https://docs.google.com/spreadsheets/d/xxx/edit?' 'usp=drivesdk --> ' 'assets/files/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\n') cl._file_data(msg, EXTERNAL_DATA['files'][0]) file_ = cl.session.add.call_args[0][0] self.assertEqual(cl._dldata, [expexted_line]) self.assertEqual(file_.filepath, 'assets/files/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee') dl.assert_not_called()