def process_event(self, store, comment):
        comment_desc = rtip.receiver_serialize_comment(comment)

        context_desc = admin.admin_serialize_context(store,
                                                     comment.internaltip.context,
                                                     self.language)

        # for every comment, iterate on the associated receiver(s)
        log.debug("Comments from %s - Receiver(s) %d" % \
                  (comment.author, comment.internaltip.receivers.count()))

        for receiver in comment.internaltip.receivers:
            if comment.type == u'receiver' and comment.author == receiver.name:
                log.debug("Receiver is the Author (%s): skipped" % receiver.user.username)
                return

            receivertip = store.find(models.ReceiverTip,
                                     (models.ReceiverTip.internaltip_id == comment.internaltip_id,
                                      models.ReceiverTip.receiver_id == receiver.id)).one()

            tip_desc = serialize_receivertip(store, receivertip, self.language)

            do_mail, receiver_desc = self.import_receiver(receiver)

            self.events.append(Event(type=self.template_type,
                                     trigger=self.trigger,
                                     node_info={},
                                     receiver_info=receiver_desc,
                                     context_info=context_desc,
                                     tip_info=tip_desc,
                                     subevent_info=comment_desc,
                                     do_mail=do_mail))
    def db_load(self, store):
        not_notified_comments = store.find(models.Comment,
                                           models.Comment.new == True)

        if not_notified_comments.count():
            log.debug("Comments found to be notified: %d" %
                      not_notified_comments.count())

        for comment in not_notified_comments:
            comment_desc = rtip.receiver_serialize_comment(comment)

            context_desc = admin.admin_serialize_context(store,
                                                         comment.internaltip.context,
                                                         self.language)

            steps_desc = admin.db_get_context_steps(store,
                                                    context_desc['id'],
                                                    self.language)

            # for every comment, iterate on the associated receiver(s)
            log.debug("Comments from %s - Receiver(s) %d" % \
                      (comment.author, comment.internaltip.receivers.count()))

            for receiver in comment.internaltip.receivers:
                if comment.type == u'receiver' and comment.author == receiver.name:
                    log.debug("Receiver is the Author (%s): skipped" % receiver.user.username)
                    continue

                receivertip = store.find(models.ReceiverTip,
                                         (models.ReceiverTip.internaltip_id == comment.internaltip_id,
                                         models.ReceiverTip.receiver_id == receiver.id)).one()

                tip_desc = serialize_receivertip(receivertip)

                do_mail, receiver_desc = self.import_receiver(receiver)

                self.events.append(Event(type=self.template_type,
                                         trigger=self.trigger,
                                         node_info={},
                                         receiver_info=receiver_desc,
                                         context_info=context_desc,
                                         steps_info=steps_desc,
                                         tip_info=tip_desc,
                                         subevent_info=comment_desc,
                                         do_mail=do_mail))

            comment.new = False
Exemple #3
0
    def load_comments(self, store):

        not_notified_comments = store.find(models.Comment,
            models.Comment.mark == u'not notified'
        )

        if not_notified_comments.count():
            log.debug("Comments found to be notified: %d" %
                      not_notified_comments.count())

        for comment in not_notified_comments:

            if not self.context_desc.has_key('id') or \
                            self.context_desc['id'] != comment.internaltip.context_id:
                self.context_desc = admin.admin_serialize_context(store,
                                                                  comment.internaltip.context,
                                                                  self.language)

                self.steps_info_desc = admin.db_get_context_steps(store,
                                                                  self.context_desc['id'],
                                                                  self.language)

            comment_desc = rtip.receiver_serialize_comment(comment)
            comment.mark = u'notified'

            # for every comment, iterate on the associated receiver(s)
            log.debug("Comments from %s - Receiver(s) %d" % \
                      (comment.author, comment.internaltip.receivers.count()))

            for receiver in comment.internaltip.receivers:

                self.do_mail = self.import_receiver(receiver)

                if comment.type == u'receiver' and comment.author == receiver.name:
                    log.debug("Receiver is the Author (%s): skipped" % receiver.user.username)
                    continue

                receivertip = store.find(models.ReceiverTip,
                    (models.ReceiverTip.internaltip_id == comment.internaltip_id,
                     models.ReceiverTip.receiver_id == receiver.id)).one()

                tip_desc = serialize_receivertip(receivertip)

                self.append_event(tip_info=tip_desc,
                                  subevent_info=comment_desc)
    def create_comment_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added comments.

        Returns:
            events: a list of tuples containing ((comment_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).


        """
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_comments = store.find(
            models.Comment, models.Comment.mark == models.Comment._marker[0])

        node_desc = admin.db_admin_serialize_node(
            store, GLSetting.memory_copy.default_language)

        if not_notified_comments.count():
            log.debug("Comments found to be notified: %d" %
                      not_notified_comments.count())

        for comment in not_notified_comments:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug(
                    "Notification counter has reached the suggested limit: %d (comment)"
                    % notification_counter)
                break

            if comment.internaltip is None or comment.internaltip.receivers is None:
                log.err(
                    "Comment %s has internaltip or receivers broken reference"
                    % comment.id)
                comment.mark = models.Comment._marker[2]  # 'unable to notify'
                continue

            # for every comment, iter on the associated receiver
            log.debug("Comments receiver: %d" %
                      comment.internaltip.receivers.count())

            comment_desc = rtip.receiver_serialize_comment(comment)

            if not comment.internaltip.context:
                log.err(
                    "(comment_notification) Integrity check failure Context")
                continue

            context_desc = admin.admin_serialize_context(
                comment.internaltip.context,
                GLSetting.memory_copy.default_language)

            # XXX BUG! All notification is marked as correctly send,
            # This can't be managed by callback, and can't be managed by actual DB design
            comment.mark = models.Comment._marker[1]  # 'notified'

            for receiver in comment.internaltip.receivers:

                receiver_desc = admin.admin_serialize_receiver(
                    receiver, GLSetting.memory_copy.default_language)
                if not receiver_desc.has_key('mail_address'):
                    log.err("Receiver %s lack of email address!" %
                            receiver.name)
                    continue

                # if the comment author is the one to be notified: skip the notification
                # ----- BUG, remind,
                # if two receiver has the same name, and one has notification disabled
                # also the homonymous would get the notification dropped.
                if comment.type == models.Comment._types[
                        0] and comment.author == receiver.name:
                    log.debug("Receiver is the Author (%s): skipped" %
                              receiver.user.username)
                    continue

                # check if the receiver has the Comment notification enabled or not
                if not receiver.comment_notification:
                    log.debug(
                        "Receiver %s has comment notification disabled: skipped [source: %s]"
                        % (receiver.user.username, comment.author))
                    continue

                receivertip = store.find(
                    models.ReceiverTip,
                    (models.ReceiverTip.internaltip_id
                     == comment.internaltip_id, models.ReceiverTip.receiver_id
                     == receiver.id)).one()

                tip_desc = serialize_receivertip(receivertip)

                if receiver_desc[
                        'gpg_key_status'] == u'Enabled':  # Receiver._gpg_types[1]
                    template_type = u'encrypted_comment'
                else:
                    template_type = u'plaintext_comment'

                event = Event(type=template_type,
                              trigger='Comment',
                              notification_settings=self.notification_settings,
                              trigger_info=comment_desc,
                              trigger_parent=tip_desc,
                              node_info=node_desc,
                              receiver_info=receiver_desc,
                              context_info=context_desc,
                              plugin=plugin)

                events.append(
                    ((unicode(comment.id), unicode(receiver.id)), event))

        return events, notification_counter
    def create_comment_notification_events(self, store, notification_counter):
        """
        Creates events for performing notification of newly added comments.

        Returns:
            events: a list of tuples containing ((comment_id, receiver_id), an instance of
                :class:`globaleaks.plugins.base.Event`).


        """
        language = GLSetting.memory_copy.default_language
        events = []
        cplugin = GLSetting.notification_plugins[0]

        plugin = getattr(notification, cplugin)()

        not_notified_comments = store.find(models.Comment,
            models.Comment.mark == models.Comment._marker[0]
        )

        node_desc = admin.db_admin_serialize_node(store, language)

        if not_notified_comments.count():
            log.debug("Comments found to be notified: %d" % not_notified_comments.count() )

        for comment in not_notified_comments:

            notification_counter += 1
            if notification_counter >= GLSetting.notification_limit:
                log.debug("Notification counter has reached the suggested limit: %d (comment)" %
                          notification_counter)
                store.remove(comment)
                break

            if comment.internaltip is None or comment.internaltip.receivers is None:
                log.err("Comment %s has internaltip or receivers broken reference" % comment.id)
                comment.mark = models.Comment._marker[2] # 'unable to notify'
                continue

            # for every comment, iter on the associated receiver
            log.debug("Comments receiver: %d" % comment.internaltip.receivers.count())

            comment_desc = rtip.receiver_serialize_comment(comment)

            if not comment.internaltip.context:
                log.err("(comment_notification) Integrity check failure Context")
                store.remove(comment)
                continue

            context_desc = admin.admin_serialize_context(store, comment.internaltip.context, language)

            # XXX BUG! All notification is marked as correctly send,
            # This can't be managed by callback, and can't be managed by actual DB design
            comment.mark = models.Comment._marker[1] # 'notified'

            for receiver in comment.internaltip.receivers:

                receiver_desc = admin.admin_serialize_receiver(receiver, language)
                if not receiver_desc.has_key('mail_address'):
                    log.err("Receiver %s lack of email address!" % receiver.name)
                    continue

                # if the comment author is the one to be notified: skip the notification
                # ----- BUG, remind,
                # if two receiver has the same name, and one has notification disabled
                # also the homonymous would get the notification dropped.
                if comment.type == models.Comment._types[0] and comment.author == receiver.name:
                    log.debug("Receiver is the Author (%s): skipped" % receiver.user.username)
                    continue

                # check if the receiver has the Comment notification enabled or not
                if not receiver.comment_notification:
                    log.debug("Receiver %s has comment notification disabled: skipped [source: %s]" % (
                        receiver.user.username, comment.author))
                    continue

                receivertip = store.find(models.ReceiverTip,
                    (models.ReceiverTip.internaltip_id == comment.internaltip_id,
                     models.ReceiverTip.receiver_id == receiver.id)).one()

                tip_desc = serialize_receivertip(receivertip)

                if  receiver_desc['gpg_key_status'] == u'Enabled': # Receiver._gpg_types[1]
                    template_type = u'encrypted_comment'
                else:
                    template_type = u'plaintext_comment'

                notification_settings = self._get_notification_settings(store, receiver_desc['language'])

                event = Event(type=template_type, trigger='Comment',
                    notification_settings=notification_settings,
                    trigger_info=comment_desc,
                    trigger_parent=tip_desc,
                    node_info=node_desc,
                    receiver_info=receiver_desc,
                    context_info=context_desc,
                    steps_info = admin.db_get_context_steps(store,
                                                            context_desc['id'],
                                                            language),
                    plugin=plugin)

                events.append(((unicode(comment.id), unicode(receiver.id)), event))

        return events, notification_counter