Пример #1
0
  def comment(self):
    commented_content = self.models.content.get(
        username=self.get_argument('local_user'),
        name=self.get_argument('local_name'))[0]
    self.display["user"] = commented_user = self.models.users.get(
        username=commented_content.username)[0]
    comment = self.get_argument('comment')
    sanitized_comment = content_remote.sanitize(comment)

    is_spam = spam.guess(comment, self.application.settings["private_path"],
        commented_content.username)

    post_remote = self.models.content_remote()
    post_remote.to_username = commented_content.username
    post_remote.from_user = self.current_user["email"]
    post_remote.username = self.current_user["email"].split('@')[0]
    local_url = ('/' + commented_content.username +
        '/remote-comments/comment-' + str(uuid.uuid4()))
    post_remote.post_id = ('tag:' + self.request.host + ',' +
        self.display["tag_date"] + ':' + local_url)
    post_remote.link = 'http://' + self.request.host + local_url
    from_username = post_remote.username
    if self.current_user["user"]:
      profile = self.get_author_user()
      post_remote.avatar = profile.logo if profile.logo else profile.favicon
    else:
      post_remote.avatar = ('http://www.gravatar.com/avatar/' +
          hashlib.md5(self.current_user["email"].lower()).hexdigest())
    post_remote.date_created = datetime.datetime.utcnow()
    if is_spam:
      post_remote.is_spam = True
    else:
      spam.train_ham(comment, self.application.settings["private_path"],
          commented_content.username)
    post_remote.type = 'comment'
    post_remote.local_content_name = commented_content.name
    post_remote.view = sanitized_comment
    post_remote.save()

    commented_content.comments_count += 1
    commented_content.comments_updated = datetime.datetime.utcnow()
    commented_content.save()

    cache.remove(self, self.content_url(commented_content))
    socialize.socialize(self, commented_content)
    smtp.comment(self, from_username, post_remote.from_user,
        commented_user.email, self.content_url(commented_content, host=True),
        sanitized_comment)
Пример #2
0
  def spam(self):
    local_id = self.get_argument('local_id')
    not_spam = self.get_argument('not_spam')
    is_remote = int(self.get_argument('is_remote'))

    content = (self.models.content_remote.get(local_id) if
        is_remote else self.models.content.get(local_id))
    if not content:
      raise tornado.web.HTTPError(400)

    profile = self.get_author_user()
    self.check_ownership(content, is_remote)

    content.is_spam = not int(not_spam)
    content.save()

    if not_spam:
      spam.train_ham(content.view, self.application.settings["private_path"],
          profile.username)
    else:
      spam.train_spam(content.view, self.application.settings["private_path"],
          profile.username)
Пример #3
0
    def handle_activity_verb(self, env, signer_uri):
        salmon_doc = BeautifulSoup(env)
        activity_verb = salmon_doc.find(re.compile('.+:verb$')).string
        user_remote = self.models.users_remote.get(
            local_username=self.display["user"].username,
            profile_url=signer_uri)[0]

        if (activity_verb == 'http://activitystrea.ms/schema/1.0/follow'):
            user = users.get_remote_user_info(self, signer_uri,
                                              user_remote.local_username)
            user.follower = 1
            user.save()
            smtp.follow(self, user.username, self.display["user"].email,
                        user.profile_url)
        elif (activity_verb == 'http://ostatus.org/schema/1.0/unfollow'
              or activity_verb
              == 'http://activitystrea.ms/schema/1.0/stop-following'):
            user_remote.follower = 0
            user_remote.save()
        elif (activity_verb == 'http://activitystrea.ms/schema/1.0/favorite'):
            activity_object = salmon_doc.find(re.compile('.+:object$'))
            atom_id = activity_object.find('atom:id').string
            local_url = atom_id.split(':')[2]
            content_url = url_factory.load_basic_parameters(self,
                                                            url=local_url)

            already_favorited = self.models.content_remote.get(
                to_username=self.display["user"].username,
                from_user=signer_uri,
                type='favorite',
                local_content_name=content_url['name'])[0]
            if already_favorited:
                return

            content = self.models.content.get(
                username=self.display["user"].username,
                name=content_url['name'])[0]
            if not content:
                raise tornado.web.HTTPError(400)
            content.favorites += 1
            content.save()

            favorite_record = self.models.content_remote()
            favorite_record.to_username = self.display["user"].username
            favorite_record.from_user = signer_uri
            favorite_record.username = user_remote.username
            favorite_record.date_created = datetime.datetime.strptime(
                salmon_doc.find('atom:updated').string[:-6],
                '%Y-%m-%dT%H:%M:%S')
            favorite_record.type = 'favorite'
            favorite_record.local_content_name = content.name
            favorite_record.save()
        elif (activity_verb == 'http://activitystrea.ms/schema/1.0/unfavorite'
              or activity_verb == 'http://ostatus.org/schema/1.0/unfavorite'):
            # TODO no activity object at least with ostatus??
            pass
        elif (activity_verb == 'http://activitystrea.ms/schema/1.0/share'):
            # TODO
            pass
        elif (activity_verb == 'http://activitystrea.ms/schema/1.0/post'):
            atom_content = salmon_doc.find('atom:content').string
            sanitized_atom_content = content_remote.sanitize(
                tornado.escape.xhtml_unescape(atom_content))

            existing_content = self.models.content_remote.get(
                to_username=self.display["user"].username,
                from_user=signer_uri,
                view=sanitized_atom_content)[0]

            thread = salmon_doc.find('thr:in-reply-to')
            ref = ''
            if thread:
                try:
                    local_url = thread['ref'].split(':')[2]
                    content_url = url_factory.load_basic_parameters(
                        self, url=local_url)
                    content = self.models.content.get(
                        username=self.display["user"].username,
                        name=content_url['name'])[0]
                    if not content:
                        raise tornado.web.HTTPError(400)
                    ref = content_url['name']
                    content.comments_count += 1
                    content.comments_updated = datetime.datetime.utcnow()
                    content.save()
                except Exception as ex:
                    import logging
                    logging.error("something wrong with thread")
                    logging.error(ex)

            replies = salmon_doc.find('thr:replies')
            comments_count = 0
            comments_updated = None
            if replies:
                if replies.has_key('count'):
                    comments_count = int(replies['count'])
                if replies.has_key('updated'):
                    comments_updated = replies['updated']
                comments_response = urllib2.urlopen(replies['href'])
                content_remote.parse_feed(self.models,
                                          user_remote,
                                          comments_response.read(),
                                          remote_comments=True)

            mentioned = salmon_doc.findAll('atom:link', rel='mentioned')
            if not mentioned:
                mentioned = salmon_doc.findAll('atom:link',
                                               rel='ostatus:attention')

            this_user_mentioned = False
            if mentioned:
                this_user_url = self.nav_url(
                    host=True, username=self.display["user"].username)
                for mentions in mentioned:
                    if mentions['href'] == this_user_url:
                        # hey, you've been mentioned. cool.
                        this_user_mentioned = True
                        break

            is_spam = spam.guess(atom_content,
                                 self.application.settings["private_path"],
                                 self.display["user"].username)

            if existing_content:
                # possible that it's picked up via feed, before we get the salmon call
                post_remote = existing_content
            else:
                post_remote = self.models.content_remote()
            post_remote.to_username = self.display["user"].username
            post_remote.from_user = signer_uri
            post_remote.username = user_remote.username
            post_remote.avatar = user_remote.avatar
            post_remote.date_created = datetime.datetime.strptime(
                salmon_doc.find('atom:published').string[:-6],
                '%Y-%m-%dT%H:%M:%S')
            post_remote.date_updated = datetime.datetime.strptime(
                salmon_doc.find('atom:updated').string[:-6],
                '%Y-%m-%dT%H:%M:%S')
            post_remote.comments_count = comments_count
            if comments_updated:
                post_remote.comments_updated = datetime.datetime.strptime(
                    comments_updated[:-6], '%Y-%m-%dT%H:%M:%S')
            else:
                post_remote.comments_updated = None
            if is_spam:
                post_remote.is_spam = True
            else:
                spam.train_ham(atom_content,
                               self.application.settings["private_path"],
                               self.display["user"].username)
            post_remote.type = 'comment' if ref or (
                existing_content
                and existing_content.type == 'comment') else 'post'
            post_remote.title = salmon_doc.find('atom:title').string
            post_remote.post_id = salmon_doc.find('atom:id').string
            post_remote.link = salmon_doc.find('atom:link',
                                               rel='alternate')['href']
            post_remote.local_content_name = content.name if ref else ''
            post_remote.view = sanitized_atom_content
            post_remote.save()

            if ref:
                socialize.socialize(self, content)

            if ref:
                smtp.comment(self, post_remote.username, post_remote.from_user,
                             self.display["user"].email,
                             self.content_url(content, host=True),
                             sanitized_atom_content)
            elif this_user_mentioned:
                smtp.comment(self,
                             post_remote.username,
                             post_remote.from_user,
                             self.display["user"].email,
                             post_remote.link,
                             sanitized_atom_content,
                             this_user_mentioned=True)
Пример #4
0
  def handle_activity_verb(self, env, signer_uri):
    salmon_doc = BeautifulSoup(env)
    activity_verb = salmon_doc.find(re.compile('.+:verb$')).string
    user_remote = self.models.users_remote.get(
        local_username=self.display["user"].username,
        profile_url=signer_uri)[0]

    if (activity_verb == 'http://activitystrea.ms/schema/1.0/follow'):
      user = users.get_remote_user_info(self, signer_uri,
          user_remote.local_username)
      user.follower = 1
      user.save()
      smtp.follow(self, user.username, self.display["user"].email,
          user.profile_url)
    elif (activity_verb == 'http://ostatus.org/schema/1.0/unfollow' or
        activity_verb == 'http://activitystrea.ms/schema/1.0/stop-following'):
      user_remote.follower = 0
      user_remote.save()
    elif (activity_verb == 'http://activitystrea.ms/schema/1.0/favorite'):
      activity_object = salmon_doc.find(re.compile('.+:object$'))
      atom_id = activity_object.find('atom:id').string
      local_url = atom_id.split(':')[2]
      content_url = url_factory.load_basic_parameters(self, url=local_url)

      already_favorited = self.models.content_remote.get(
          to_username=self.display["user"].username,
          from_user=signer_uri,
          type='favorite',
          local_content_name=content_url['name'])[0]
      if already_favorited:
        return

      content = self.models.content.get(username=self.display["user"].username,
                                        name=content_url['name'])[0]
      if not content:
        raise tornado.web.HTTPError(400)
      content.favorites += 1
      content.save()

      favorite_record = self.models.content_remote()
      favorite_record.to_username = self.display["user"].username
      favorite_record.from_user = signer_uri
      favorite_record.username = user_remote.username
      favorite_record.date_created = datetime.datetime.strptime(
          salmon_doc.find('atom:updated').string[:-6], '%Y-%m-%dT%H:%M:%S')
      favorite_record.type = 'favorite'
      favorite_record.local_content_name = content.name
      favorite_record.save()
    elif (activity_verb == 'http://activitystrea.ms/schema/1.0/unfavorite' or
        activity_verb == 'http://ostatus.org/schema/1.0/unfavorite'):
      # TODO no activity object at least with ostatus??
      pass
    elif (activity_verb == 'http://activitystrea.ms/schema/1.0/share'):
      # TODO
      pass
    elif (activity_verb == 'http://activitystrea.ms/schema/1.0/post'):
      atom_content = salmon_doc.find('atom:content').string
      sanitized_atom_content = content_remote.sanitize(
          tornado.escape.xhtml_unescape(atom_content))

      existing_content = self.models.content_remote.get(
          to_username=self.display["user"].username,
          from_user=signer_uri,
          view=sanitized_atom_content)[0]

      thread = salmon_doc.find('thr:in-reply-to')
      ref = ''
      if thread:
        try:
          local_url = thread['ref'].split(':')[2]
          content_url = url_factory.load_basic_parameters(self, url=local_url)
          content = self.models.content.get(
              username=self.display["user"].username,
              name=content_url['name'])[0]
          if not content:
            raise tornado.web.HTTPError(400)
          ref = content_url['name']
          content.comments_count += 1
          content.comments_updated = datetime.datetime.utcnow()
          content.save()
        except Exception as ex:
          import logging
          logging.error("something wrong with thread")
          logging.error(ex)

      replies = salmon_doc.find('thr:replies')
      comments_count = 0
      comments_updated = None
      if replies:
        if replies.has_key('count'):
          comments_count = int(replies['count'])
        if replies.has_key('updated'):
          comments_updated = replies['updated']
        comments_response = urllib2.urlopen(replies['href'])
        content_remote.parse_feed(self.models, user_remote,
            comments_response.read(), remote_comments=True)

      mentioned = salmon_doc.findAll('atom:link', rel='mentioned')
      if not mentioned:
        mentioned = salmon_doc.findAll('atom:link', rel='ostatus:attention')

      this_user_mentioned = False
      if mentioned:
        this_user_url = self.nav_url(host=True,
            username=self.display["user"].username)
        for mentions in mentioned:
          if mentions['href'] == this_user_url:
            # hey, you've been mentioned. cool.
            this_user_mentioned = True
            break

      is_spam = spam.guess(atom_content,
          self.application.settings["private_path"],
          self.display["user"].username)

      if existing_content:
        # possible that it's picked up via feed, before we get the salmon call
        post_remote = existing_content
      else:
        post_remote = self.models.content_remote()
      post_remote.to_username = self.display["user"].username
      post_remote.from_user = signer_uri
      post_remote.username = user_remote.username
      post_remote.avatar = user_remote.avatar
      post_remote.date_created = datetime.datetime.strptime(
          salmon_doc.find('atom:published').string[:-6], '%Y-%m-%dT%H:%M:%S')
      post_remote.date_updated = datetime.datetime.strptime(
          salmon_doc.find('atom:updated').string[:-6], '%Y-%m-%dT%H:%M:%S')
      post_remote.comments_count = comments_count
      if comments_updated:
        post_remote.comments_updated = datetime.datetime.strptime(
            comments_updated[:-6], '%Y-%m-%dT%H:%M:%S')
      else:
        post_remote.comments_updated = None
      if is_spam:
        post_remote.is_spam = True
      else:
        spam.train_ham(atom_content, self.application.settings["private_path"],
            self.display["user"].username)
      post_remote.type = 'comment' if ref or (existing_content and
          existing_content.type == 'comment') else 'post'
      post_remote.title = salmon_doc.find('atom:title').string
      post_remote.post_id = salmon_doc.find('atom:id').string
      post_remote.link = salmon_doc.find('atom:link', rel='alternate')['href']
      post_remote.local_content_name = content.name if ref else ''
      post_remote.view = sanitized_atom_content
      post_remote.save()

      if ref:
        socialize.socialize(self, content)

      if ref:
        smtp.comment(self, post_remote.username, post_remote.from_user,
            self.display["user"].email, self.content_url(content, host=True),
            sanitized_atom_content)
      elif this_user_mentioned:
        smtp.comment(self, post_remote.username, post_remote.from_user,
            self.display["user"].email, post_remote.link,
            sanitized_atom_content, this_user_mentioned=True)