from ploneintranet.core import _
from ploneintranet.microblog.notifications import messages
from ploneintranet.notifications.adapters.category_provider import BaseCategory
from ploneintranet.notifications.adapters.category_provider import BaseCategoryProvider


class NewsPublishedCategory(BaseCategory):
    name = "news_published"
    default_channels = {"desktop"}
    title = _("label_new_news_articles", default="New news items")
    description = ""
    _app_id = "news"

    @property
    def recipient_ids(self):
        return self.all_intranet_user_ids

    def get_message_for(self, notification, with_text=True, highlight=False):
        if notification.logged_event.zope_event == "newsitem_published":
            context = notification.logged_event.context
            mapping = {
                "actor": self.logged_event.actor_fullname,
                "object": self.maybe_process_title(context.Title(), highlight),
                "section": context.aq_parent.Title(),
            }
            return messages.message_published_a_news_item(mapping=mapping)
        return super().get_message_for(notification)


class NewsItemCategoryProvider(BaseCategoryProvider):

    category_factories = [NewsPublishedCategory]
