Make Event.inline_content & links pure attrs

This commit is contained in:
miruka 2020-02-11 17:32:22 -04:00
parent 8a29143b60
commit e982de1c61
3 changed files with 34 additions and 31 deletions

View File

@ -499,15 +499,21 @@ class MatrixClient(nio.AsyncClient):
our_info = self.models[self.user_id, room_id, "members"][self.user_id] our_info = self.models[self.user_id, room_id, "members"][self.user_id]
content = event_fields.get("content", "").strip()
if content and "inline_content" not in event_fields:
event_fields["inline_content"] = HTML.filter(content, inline=True)
event = Event( event = Event(
id = f"echo-{transaction_id}", id = f"echo-{transaction_id}",
event_id = "", event_id = "",
event_type = event_type, event_type = event_type,
date = datetime.now(), date = datetime.now(),
sender_id = self.user_id, sender_id = self.user_id,
sender_name = our_info.display_name, sender_name = our_info.display_name,
sender_avatar = our_info.avatar_url, sender_avatar = our_info.avatar_url,
is_local_echo = True, is_local_echo = True,
links = Event.parse_links(content),
**event_fields, **event_fields,
) )
@ -1021,6 +1027,11 @@ class MatrixClient(nio.AsyncClient):
await self.get_member_name_avatar(room.room_id, target_id) \ await self.get_member_name_avatar(room.room_id, target_id) \
if target_id else ("", "") if target_id else ("", "")
content = fields.get("content", "").strip()
if content and "inline_content" not in fields:
fields["inline_content"] = HTML.filter(content, inline=True)
# Create Event ModelItem # Create Event ModelItem
item = Event( item = Event(
id = ev.event_id, id = ev.event_id,
@ -1034,6 +1045,7 @@ class MatrixClient(nio.AsyncClient):
target_id = target_id, target_id = target_id,
target_name = target_name, target_name = target_name,
target_avatar = target_avatar, target_avatar = target_avatar,
links = Event.parse_links(content),
**fields, **fields,
) )

View File

@ -174,9 +174,10 @@ class Event(ModelItem):
sender_name: str = field() sender_name: str = field()
sender_avatar: str = field() sender_avatar: str = field()
content: str = "" content: str = ""
inline_content: str = "" inline_content: str = ""
reason: str = "" reason: str = ""
links: List[Dict[str, Any]] = field(default_factory=list)
type_specifier: TypeSpecifier = TypeSpecifier.Unset type_specifier: TypeSpecifier = TypeSpecifier.Unset
@ -201,32 +202,19 @@ class Event(ModelItem):
thumbnail_height: int = 0 thumbnail_height: int = 0
thumbnail_crypt_dict: Dict[str, Any] = field(default_factory=dict) thumbnail_crypt_dict: Dict[str, Any] = field(default_factory=dict)
def __post_init__(self) -> None:
if not self.inline_content:
self.inline_content = \
HTML_PROCESSOR.filter(self.content, inline=True)
def __lt__(self, other: "Event") -> bool: def __lt__(self, other: "Event") -> bool:
"""Sort by date in descending order, from newest to oldest.""" """Sort by date in descending order, from newest to oldest."""
return self.date > other.date return self.date > other.date
@property @staticmethod
def links(self) -> List[Dict[str, Any]]: def parse_links(text: str) -> List[Dict[str, Any]]:
"""List of URLs (`<a href=...>` tags) present in the event content.""" """Return list of URLs (`<a href=...>` tags) present in the text."""
urls: List[Dict[str, Any]] = [] if not text.strip():
return []
if self.content.strip(): return [{"url": link[2]} for link in lxml.html.iterlinks(text)]
urls += [
{"url": link[2]} for link in lxml.html.iterlinks(self.content)
]
if self.media_url:
urls.append({"url": self.media_url})
return urls
@property @property
def serialized(self) -> Dict[str, Any]: def serialized(self) -> Dict[str, Any]:

View File

@ -172,7 +172,10 @@ HRowLayout {
HRepeater { HRepeater {
id: linksRepeater id: linksRepeater
model: JSON.parse(eventDelegate.currentModel.links) model: [
eventDelegate.currentModel.media_url,
...JSON.parse(eventDelegate.currentModel.links),
]
EventMediaLoader { EventMediaLoader {
singleMediaInfo: eventDelegate.currentModel singleMediaInfo: eventDelegate.currentModel