Skip to content
Snippets Groups Projects
Commit 0fa89b7b authored by Tulir Asokan's avatar Tulir Asokan :cat2:
Browse files

Add timestamp massaging for messages from Facebook

parent 722f32d2
No related branches found
No related tags found
No related merge requests found
...@@ -618,7 +618,8 @@ class Portal(BasePortal): ...@@ -618,7 +618,8 @@ class Portal(BasePortal):
if self.is_direct: if self.is_direct:
self.log.info(f"{user.mxid} left private chat portal with {self.fbid}") self.log.info(f"{user.mxid} left private chat portal with {self.fbid}")
if user.fbid == self.fb_receiver: if user.fbid == self.fb_receiver:
self.log.info(f"{user.mxid} was the recipient of this portal. Cleaning up and deleting...") self.log.info(f"{user.mxid} was the recipient of this portal. "
"Cleaning up and deleting...")
await self.cleanup_and_delete() await self.cleanup_and_delete()
else: else:
self.log.debug(f"{user.mxid} left portal to {self.fbid}") self.log.debug(f"{user.mxid} left portal to {self.fbid}")
...@@ -663,12 +664,12 @@ class Portal(BasePortal): ...@@ -663,12 +664,12 @@ class Portal(BasePortal):
intent = sender.intent_for(self) intent = sender.intent_for(self)
event_ids = [] event_ids = []
if message.sticker: if message.sticker:
event_ids = [await self._handle_facebook_sticker(intent, message.sticker, event_ids = [await self._handle_facebook_sticker(
message.reply_to_id)] intent, message.sticker, message.reply_to_id, message.created_at)]
elif len(message.attachments) > 0: elif len(message.attachments) > 0:
attach_ids = await asyncio.gather( attach_ids = await asyncio.gather(
*[self._handle_facebook_attachment(source.client, intent, attachment, *[self._handle_facebook_attachment(source.client, intent, attachment,
message.reply_to_id) message.reply_to_id, message.created_at)
for attachment in message.attachments]) for attachment in message.attachments])
event_ids += [attach_id for attach_id in attach_ids if attach_id] event_ids += [attach_id for attach_id in attach_ids if attach_id]
if not event_ids: if not event_ids:
...@@ -715,10 +716,10 @@ class Portal(BasePortal): ...@@ -715,10 +716,10 @@ class Portal(BasePortal):
) -> EventID: ) -> EventID:
content = facebook_to_matrix(message) content = facebook_to_matrix(message)
await self._add_facebook_reply(content, message.reply_to_id) await self._add_facebook_reply(content, message.reply_to_id)
return await self._send_message(intent, content) return await self._send_message(intent, content, timestamp=message.created_at)
async def _handle_facebook_sticker(self, intent: IntentAPI, sticker: fbchat.Sticker, async def _handle_facebook_sticker(self, intent: IntentAPI, sticker: fbchat.Sticker,
reply_to: str) -> EventID: reply_to: str, timestamp: datetime) -> EventID:
width, height = sticker.image.width, sticker.image.height width, height = sticker.image.width, sticker.image.height
if sticker.is_animated and Image and convert_cmd: if sticker.is_animated and Image and convert_cmd:
async def convert(data: bytes) -> bytes: async def convert(data: bytes) -> bytes:
...@@ -738,18 +739,19 @@ class Portal(BasePortal): ...@@ -738,18 +739,19 @@ class Portal(BasePortal):
msgtype=MessageType.STICKER, body=sticker.label or "", msgtype=MessageType.STICKER, body=sticker.label or "",
info=ImageInfo(width=width, size=size, info=ImageInfo(width=width, size=size,
height=height, mimetype=mime), height=height, mimetype=mime),
relates_to=self._get_facebook_reply(reply_to))) relates_to=self._get_facebook_reply(reply_to)),
timestamp=timestamp)
async def _handle_facebook_attachment(self, source: fbchat.Client, intent: IntentAPI, async def _handle_facebook_attachment(self, source: fbchat.Client, intent: IntentAPI,
attachment: fbchat.Attachment, reply_to: str attachment: fbchat.Attachment, reply_to: str,
) -> Optional[EventID]: timestamp: datetime) -> Optional[EventID]:
if isinstance(attachment, fbchat.AudioAttachment): if isinstance(attachment, fbchat.AudioAttachment):
mxc, mime, size, decryption_info = await self._reupload_fb_file( mxc, mime, size, decryption_info = await self._reupload_fb_file(
attachment.url, intent, attachment.filename, encrypt=self.encrypted) attachment.url, intent, attachment.filename, encrypt=self.encrypted)
event_id = await self._send_message(intent, MediaMessageEventContent( event_id = await self._send_message(intent, MediaMessageEventContent(
url=mxc, file=decryption_info, msgtype=MessageType.AUDIO, body=attachment.filename, url=mxc, file=decryption_info, msgtype=MessageType.AUDIO, body=attachment.filename,
info=AudioInfo(size=size, mimetype=mime, duration=attachment.duration.seconds), info=AudioInfo(size=size, mimetype=mime, duration=attachment.duration.seconds),
relates_to=self._get_facebook_reply(reply_to))) relates_to=self._get_facebook_reply(reply_to)), timestamp=timestamp)
# elif isinstance(attachment, fbchat.VideoAttachment): # elif isinstance(attachment, fbchat.VideoAttachment):
# TODO # TODO
elif isinstance(attachment, fbchat.FileAttachment): elif isinstance(attachment, fbchat.FileAttachment):
...@@ -758,7 +760,7 @@ class Portal(BasePortal): ...@@ -758,7 +760,7 @@ class Portal(BasePortal):
event_id = await self._send_message(intent, MediaMessageEventContent( event_id = await self._send_message(intent, MediaMessageEventContent(
url=mxc, file=decryption_info, msgtype=MessageType.FILE, body=attachment.name, url=mxc, file=decryption_info, msgtype=MessageType.FILE, body=attachment.name,
info=FileInfo(size=size, mimetype=mime), info=FileInfo(size=size, mimetype=mime),
relates_to=self._get_facebook_reply(reply_to))) relates_to=self._get_facebook_reply(reply_to)), timestamp=timestamp)
elif isinstance(attachment, fbchat.ImageAttachment): elif isinstance(attachment, fbchat.ImageAttachment):
mxc, mime, size, decryption_info = await self._reupload_fb_file( mxc, mime, size, decryption_info = await self._reupload_fb_file(
await source.fetch_image_url(attachment.id), intent, encrypt=self.encrypted) await source.fetch_image_url(attachment.id), intent, encrypt=self.encrypted)
...@@ -767,11 +769,11 @@ class Portal(BasePortal): ...@@ -767,11 +769,11 @@ class Portal(BasePortal):
body=f"image.{attachment.original_extension}", body=f"image.{attachment.original_extension}",
info=ImageInfo(size=size, mimetype=mime, width=attachment.width, info=ImageInfo(size=size, mimetype=mime, width=attachment.width,
height=attachment.height), height=attachment.height),
relates_to=self._get_facebook_reply(reply_to))) relates_to=self._get_facebook_reply(reply_to)), timestamp=timestamp)
elif isinstance(attachment, fbchat.LocationAttachment): elif isinstance(attachment, fbchat.LocationAttachment):
content = await self._convert_facebook_location(intent, attachment) content = await self._convert_facebook_location(intent, attachment)
content.relates_to = self._get_facebook_reply(reply_to) content.relates_to = self._get_facebook_reply(reply_to)
event_id = await self._send_message(intent, content) event_id = await self._send_message(intent, content, timestamp=timestamp)
elif isinstance(attachment, fbchat.ShareAttachment): elif isinstance(attachment, fbchat.ShareAttachment):
# These are handled in the text formatter # These are handled in the text formatter
return None return None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment