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

Add non-persistent deduplication and other stuff

parent 2847b9c7
No related branches found
No related tags found
No related merge requests found
...@@ -49,6 +49,9 @@ class Portal: ...@@ -49,6 +49,9 @@ class Portal:
photo: str photo: str
avatar_uri: ContentURI avatar_uri: ContentURI
messages_by_fbid: Dict[str, EventID]
messages_by_mxid: Dict[EventID, str]
_main_intent: Optional[IntentAPI] _main_intent: Optional[IntentAPI]
def __init__(self, fbid: str, fb_type: ThreadType, mxid: Optional[RoomID] = None, def __init__(self, fbid: str, fb_type: ThreadType, mxid: Optional[RoomID] = None,
...@@ -63,6 +66,9 @@ class Portal: ...@@ -63,6 +66,9 @@ class Portal:
self._main_intent = None self._main_intent = None
self.messages_by_fbid = {}
self.messages_by_mxid = {}
self.name = name self.name = name
self.photo = photo self.photo = photo
self.avatar_uri = avatar_uri self.avatar_uri = avatar_uri
...@@ -113,7 +119,7 @@ class Portal: ...@@ -113,7 +119,7 @@ class Portal:
await self.main_intent.set_room_name(self.mxid, self.name) await self.main_intent.set_room_name(self.mxid, self.name)
async def _update_photo(self, photo: str) -> None: async def _update_photo(self, photo: str) -> None:
if self.photo != photo: if self.photo != photo or len(self.avatar_uri) == 0:
self.photo = photo self.photo = photo
if self.mxid and not self.is_direct: if self.mxid and not self.is_direct:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
...@@ -123,11 +129,11 @@ class Portal: ...@@ -123,11 +129,11 @@ class Portal:
await self.main_intent.set_room_avatar(self.mxid, self.avatar_uri) await self.main_intent.set_room_avatar(self.mxid, self.avatar_uri)
async def _update_participants(self, source: 'u.User', info: ThreadClass) -> None: async def _update_participants(self, source: 'u.User', info: ThreadClass) -> None:
if not self.mxid: if self.is_direct:
return
elif self.is_direct:
await p.Puppet.get(info.uid).update_info(source=source, info=info) await p.Puppet.get(info.uid).update_info(source=source, info=info)
return return
elif not self.mxid:
return
users = await source.fetchAllUsersFromThreads(info) users = await source.fetchAllUsersFromThreads(info)
puppets = {user: p.Puppet.get(user.uid) for user in users} puppets = {user: p.Puppet.get(user.uid) for user in users}
await asyncio.gather(*[puppet.update_info(source=source, info=user) await asyncio.gather(*[puppet.update_info(source=source, info=user)
...@@ -154,11 +160,34 @@ class Portal: ...@@ -154,11 +160,34 @@ class Portal:
if not self.mxid: if not self.mxid:
raise Exception("Failed to create room") raise Exception("Failed to create room")
self.by_mxid[self.mxid] = self self.by_mxid[self.mxid] = self
await self._update_participants(source, info) if not self.is_direct:
await self._update_participants(source, info)
async def handle_matrix_message(self, sender: 'u.User', message: MessageEventContent, async def handle_matrix_message(self, sender: 'u.User', message: MessageEventContent,
event_id: EventID): event_id: EventID) -> None:
await sender.send(FBMessage(text=message.body), self.fbid, self.fb_type) if event_id in self.messages_by_mxid:
print(f"handle_matrix_message({event_id}) --> cancelled")
return
print(f"handle_matrix_message({event_id}) --> sending")
fbid = await sender.send(FBMessage(text=message.body), self.fbid, self.fb_type)
print(f"handle_matrix_message({event_id}) --> sent")
self.messages_by_fbid[fbid] = event_id
self.messages_by_mxid[event_id] = fbid
print(f"handle_matrix_message({event_id}) --> mapped to {fbid}")
async def handle_facebook_message(self, source: 'u.User', sender: 'p.Puppet',
message: FBMessage) -> None:
if message.uid in self.messages_by_fbid:
print(f"handle_facebook_message({message.uid}) --> cancelled")
return
print(f"handle_facebook_message({message.uid}) --> sending")
if not self.mxid:
await self.create_matrix_room(source)
event_id = await sender.intent.send_text(self.mxid, message.text)
print(f"handle_facebook_message({message.uid}) --> sent")
self.messages_by_mxid[event_id] = message.uid
self.messages_by_fbid[message.uid] = event_id
print(f"handle_facebook_message({message.uid}) --> mapped to {event_id}")
@classmethod @classmethod
def get_by_mxid(cls, mxid: RoomID) -> Optional['Portal']: def get_by_mxid(cls, mxid: RoomID) -> Optional['Portal']:
......
...@@ -85,7 +85,7 @@ class Puppet: ...@@ -85,7 +85,7 @@ class Puppet:
await self.intent.set_displayname(self.name) await self.intent.set_displayname(self.name)
async def _update_photo(self, photo: str) -> None: async def _update_photo(self, photo: str) -> None:
if photo != self.photo: if photo != self.photo or len(self.avatar_uri) == 0:
self.photo = photo self.photo = photo
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
resp = await session.get(self.photo) resp = await session.get(self.photo)
......
...@@ -172,11 +172,17 @@ class User(Client): ...@@ -172,11 +172,17 @@ class User(Client):
:type message_object: models.Message :type message_object: models.Message
:type thread_type: models.ThreadType :type thread_type: models.ThreadType
""" """
if author_id == self.uid:
self.log.debug(f"Ignoring message from self ({mid}, {author_id}, {message}, "
f"{thread_id}, {thread_type})")
return
self.log.debug(f"onMessage({mid}, {author_id}, {message}, {thread_id}, {thread_type})")
portal = po.Portal.get_by_fbid(thread_id, thread_type) portal = po.Portal.get_by_fbid(thread_id, thread_type)
puppet = pu.Puppet.get(author_id) puppet = pu.Puppet.get(author_id)
if not portal.mxid: if not puppet.name:
await portal.create_matrix_room(self) await puppet.update_info(self)
await puppet.intent.send_text(portal.mxid, message) message_object.uid = mid
await portal.handle_facebook_message(self, puppet, message_object)
async def onColorChange(self, mid=None, author_id=None, new_color=None, thread_id=None, async def onColorChange(self, mid=None, author_id=None, new_color=None, thread_id=None,
thread_type=ThreadType.USER, ts=None, metadata=None, msg=None): thread_type=ThreadType.USER, ts=None, metadata=None, msg=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