Skip to content

Commit 684835e

Browse files
committed
Add entity ranges automatically
1 parent 5bca5e4 commit 684835e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/spectrum/models/content.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class ContentBlock:
6262
type: str
6363
blocks: list[Block] = None
6464
data: Optional[dict] = None
65+
reply_id: int = None
6566

6667
def __post_init__(self):
6768
self.id = int(self.id) if isinstance(self.id, str) and self.id.isnumeric() else self.id

src/spectrum/models/lobby.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from . import message, abc
44
from .. import httpclient
5+
from ..util.entity_ranges import get_entity_ranges
56

67

78
class Lobby(abc.Identifier, abc.Subscription):
@@ -81,12 +82,18 @@ def leader(self):
8182
def __repr__(self):
8283
return f"Lobby(id={repr(self.id)}, name={repr(self.name)})"
8384

84-
async def send(self, content: str):
85+
async def send(self, content: str, add_links=True):
86+
entity_ranges = []
87+
88+
if add_links:
89+
entity_ranges = get_entity_ranges(content)
90+
91+
8592
payload = await self._client._http.send_message({
8693
"lobby_id": self.id,
8794
"content_state": {"blocks": [
8895
{"key": "bpeol", "text": content, "type": "unstyled", "depth": 0,
89-
"inlineStyleRanges": [], "entityRanges": [], "data": {}}], "entityMap": {}},
96+
"inlineStyleRanges": [], "entityRanges": entity_ranges, "data": {}}], "entityMap": {}},
9097
"plaintext": content, "media_id": None, "highlight_role_id": None
9198
})
9299

src/spectrum/util/entity_ranges.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import re
2+
from typing import List
3+
4+
from spectrum.models.content import EntityRange
5+
6+
url_pattern = re.compile(
7+
r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)")
8+
9+
10+
def get_entity_ranges(url: str) -> List[EntityRange]:
11+
ranges = []
12+
for i, match in enumerate(url_pattern.finditer(url)):
13+
ranges.append(EntityRange(match.start(), match.end() - match.start(), i))
14+
15+
return ranges

0 commit comments

Comments
 (0)