from plone import api
from ploneintranet import api as pi_api
from ploneintranet.layout.browser.base import BaseView


class IncomingBaseView(BaseView):
    @property
    def userid(self):
        return pi_api.userprofile.get_current().getId()

    def ping(self):
        raise NotImplementedError

    def __call__(self):
        """Test view that allows to push notifications to the connected user"""
        if self.is_posting():
            self.ping()
        return super().__call__()


class IncomingMessageView(IncomingBaseView):
    def ping(self):
        userid = self.userid
        with api.env.adopt_user("admin"):
            pi_api.messaging.send_message(userid, "Hello World!")


class IncomingNotificationView(IncomingBaseView):
    def ping(self):
        userid = self.userid
        with api.env.adopt_user("admin"):
            pi_api.microblog.statusupdate.create("Test", mention_ids=[userid])
