from collective.ftw.upgrade import UpgradeStep
from logging import getLogger
from plone import api


class RevertPublishedNewsInDefaultStream(UpgradeStep):
    """Revert published news in default stream."""

    def __call__(self):
        tool = api.portal.get_tool("ploneintranet_microblog")
        count = 0
        # narrow the set of candiates to those that are both human and content
        for id in tool._is_human_mapping & tool._is_content_mapping:
            status_update = tool._get(id)
            if not status_update.is_human_update:
                tool._unidx_is_human(status_update)
                count += 1
        getLogger(__name__).info(
            "Removed %s published news items from default stream.", count
        )
