Skip to content

Commit 8985dab

Browse files
committed
Fix compatibility mode for outbound messages
1 parent e2c2fa5 commit 8985dab

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

shinobu/beacon/discord/driver.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,6 @@ def sanitize_outbound(self, content: str) -> str:
531531

532532
return content
533533

534-
def sanitize_inbound(self, content: str) -> str:
535-
# Nothing to sanitize
536-
return content
537-
538534
# Beacon driver functions
539535
def get_user(self, user_id: str):
540536
user = self.bot.get_user(int(user_id))

shinobu/beacon/models/driver.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,19 @@ async def _purge(self, messages: list[beacon_message.BeaconMessage]):
145145

146146
def sanitize_inbound(self, content: str) -> str:
147147
"""Sanitizes content to be friendly with the driver's platform."""
148-
raise BeaconDriverUnsupported()
148+
return content
149149

150150
def sanitize_outbound(self, content: str) -> str:
151151
"""Sanitizes content to be friendly with other platforms."""
152-
raise BeaconDriverUnsupported()
152+
return content
153+
154+
def sanitize_inbound_compat(self, content: str) -> str:
155+
"""Sanitizes content to be friendly with other platforms with compatibility mode enabled."""
156+
return content
157+
158+
def sanitize_outbound_compat(self, content: str) -> str:
159+
"""Sanitizes content to be friendly with driver's platform with compatibility mode enabled."""
160+
return content
153161

154162
# The following properties and methods are already implemented but can be overwritten
155163
# for custom behavior.

shinobu/beacon/stoat/driver.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ async def _to_stoat_content(self, content: beacon_message.BeaconMessageContent,
198198
replies.append(reply_obj)
199199

200200
# Get final content
201-
final_content: str = "\n".join(text_components)
201+
final_content: str = self.sanitize_inbound("\n".join(text_components))
202+
202203
if compatibility:
203-
final_content = self._sanitize_inbound_compat(final_content)
204+
final_content = self.sanitize_inbound_compat(final_content)
204205

205206
# Assemble to StoatMessageContent
206207
return StoatMessageContent(
@@ -275,8 +276,20 @@ def sanitize_inbound(self, content: str) -> str:
275276
# Return content
276277
return content
277278

278-
@staticmethod
279-
def _sanitize_inbound_compat(content: str) -> str:
279+
def sanitize_outbound_compat(self, content: str) -> str:
280+
# Detect spoilered content
281+
components: list[str] = content.split('!!||')
282+
283+
if len(components) < 2:
284+
return content
285+
286+
for index in range(1, len(components)):
287+
if "||!!" in components[index]:
288+
components[index] = "||" + components[index].replace("||!!", "||", 1)
289+
290+
return ''.join(components)
291+
292+
def sanitize_inbound_compat(self, content: str) -> str:
280293
# Detect spoilered content
281294
components: list[str] = content.split('||')
282295

shinobu/beacon/stoat/parent.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ def register_driver(self):
5454
async def add_extensions(self):
5555
await self.load_extension("shinobu.beacon.stoat.modules.frontend")
5656

57-
async def _to_beacon_content(self, message: stoat.Message | stoat.PartialMessage) -> beacon_message.BeaconMessageContent:
57+
async def _to_beacon_content(self, message: stoat.Message | stoat.PartialMessage, compatibility: bool = False
58+
) -> beacon_message.BeaconMessageContent:
5859
# Create text content block
60+
final_content: str = self._driver.sanitize_outbound(str(message.content))
61+
if compatibility:
62+
final_content = self._driver.sanitize_outbound_compat(final_content)
63+
5964
text_content: beacon_content.BeaconContentText = beacon_content.BeaconContentText(
60-
content=self._driver.sanitize_outbound(str(message.content))
65+
content=final_content
6166
)
6267

6368
# Create embed blocks

0 commit comments

Comments
 (0)