From 11de1e12136ee13e606cca785a3ecaa33014f99a Mon Sep 17 00:00:00 2001 From: Simone Robutti Date: Wed, 16 Feb 2022 13:51:00 +0100 Subject: [PATCH] fixed facebook format (#142) --- .../publishers/platforms/facebook.py | 6 ++ .../publishers/templates/facebook.tmpl.j2 | 2 + tests/formatting/test_output_format.py | 71 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/formatting/test_output_format.py diff --git a/mobilizon_reshare/publishers/platforms/facebook.py b/mobilizon_reshare/publishers/platforms/facebook.py index 4546b4d..d7d9c2d 100644 --- a/mobilizon_reshare/publishers/platforms/facebook.py +++ b/mobilizon_reshare/publishers/platforms/facebook.py @@ -5,6 +5,7 @@ import pkg_resources from facebook import GraphAPIError from mobilizon_reshare.event.event import MobilizonEvent +from mobilizon_reshare.formatting.description import html_to_plaintext from mobilizon_reshare.publishers.abstract import ( AbstractPlatform, AbstractEventFormatter, @@ -38,6 +39,11 @@ class FacebookFormatter(AbstractEventFormatter): def _validate_message(self, message) -> None: pass + def _preprocess_event(self, event: MobilizonEvent): + event.description = html_to_plaintext(event.description) + event.name = html_to_plaintext(event.name) + return event + class FacebookPlatform(AbstractPlatform): """ diff --git a/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 b/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 index 06a4062..903b0a2 100644 --- a/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 @@ -7,3 +7,5 @@ {% endif %} {{ description }} + +Link: {{mobilizon_link}} \ No newline at end of file diff --git a/tests/formatting/test_output_format.py b/tests/formatting/test_output_format.py new file mode 100644 index 0000000..1dfccb5 --- /dev/null +++ b/tests/formatting/test_output_format.py @@ -0,0 +1,71 @@ +from datetime import datetime, timedelta, timezone +from uuid import UUID + +import arrow +import pytest + +from mobilizon_reshare.event.event import MobilizonEvent +from mobilizon_reshare.publishers.platforms.platform_mapping import get_formatter_class + + +@pytest.fixture() +def event() -> MobilizonEvent: + begin_date = arrow.get( + datetime( + year=2021, + month=1, + day=1, + hour=11, + minute=30, + tzinfo=timezone(timedelta(hours=1)), + ) + ) + return MobilizonEvent( + name="test event", + description="

description of the event

", + begin_datetime=begin_date, + end_datetime=begin_date.shift(hours=1), + mobilizon_link="http://some_link.com/123", + mobilizon_id=UUID(int=12345), + thumbnail_link="http://some_link.com/123.jpg", + location="location", + last_update_time=begin_date, + ) + + +@pytest.mark.parametrize( + "publisher_name,expected_output", + [ + [ + "facebook", + """# + +🕒 01 January, 11:30 - 01 January, 12:30 + + +📍 location + + +description of the event + +Link: http://some_link.com/123 +""", + ], + [ + "telegram", + """test event + +🕒 01 January, 11:30 - 01 January, 12:30 +📍 location + +description of the event + +Link""", + ], + ], +) +def test_output_format(event, publisher_name, expected_output): + assert ( + get_formatter_class(publisher_name)().get_message_from_event(event).strip() + == expected_output.strip() + )