Exemple #1
0
 def test_surrounding_characters(self):
     """Can parse objects be in parenthesis"""
     create_account('user1', '*****@*****.**', 'Password1')
     links, mentions, hashtags = parse_post('(@user1), (pjuu.com), (#hash)')
     self.assertEqual(links[0]['link'], 'http://pjuu.com')
     self.assertEqual(mentions[0]['username'], 'user1')
     self.assertEqual(hashtags[0]['hashtag'], 'hash')
Exemple #2
0
 def test_unicode_character(self):
     """Do unicode characters break things."""
     create_account('user1', '*****@*****.**', 'Password1')
     links, mentions, hashtags = parse_post('၍ @user1, ☂pjuu.com, 㒅 #hash')
     self.assertEqual(links[0]['link'], 'http://pjuu.com')
     self.assertEqual(mentions[0]['username'], 'user1')
     self.assertEqual(hashtags[0]['hashtag'], 'hash')
Exemple #3
0
 def test_surrounding_characters(self):
     """Can parse objects be in parenthesis"""
     user1 = create_account('user1', '*****@*****.**', 'Password1')
     activate(user1)
     links, mentions, hashtags = parse_post('(@user1), (pjuu.com), (#hash)')
     self.assertEqual(links[0]['link'], 'http://pjuu.com')
     self.assertEqual(mentions[0]['username'], 'user1')
     self.assertEqual(hashtags[0]['hashtag'], 'hash')
Exemple #4
0
 def test_unicode_character(self):
     """Do unicode characters break things."""
     user1 = create_account('user1', '*****@*****.**', 'Password1')
     activate(user1)
     links, mentions, hashtags = parse_post('၍ @user1, ☂pjuu.com, 㒅 #hash')
     self.assertEqual(links[0]['link'], 'http://pjuu.com')
     self.assertEqual(mentions[0]['username'], 'user1')
     self.assertEqual(hashtags[0]['hashtag'], 'hash')
Exemple #5
0
def create_post(user_id, username, body, reply_to=None, upload=None,
                permission=k.PERM_PUBLIC):
    """Creates a new post

    This handled both posts and what used to be called comments. If the
    reply_to field is not None then the post will be treat as a comment.
    You will need to make sure the reply_to post exists.

    :param user_id: The user id of the user posting the post
    :type user_id: str
    :param username: The user name of the user posting (saves a lookup)
    :type username: str
    :param body: The content of the post
    :type body: str
    :param reply_to: The post id of the post this is a reply to if any
    :type reply_to: str
    :param upload:
    :returns: The post id of the new post
    :param permission: Who can see/interact with the post you are posting
    :type permission: int
    :rtype: str or None

    """
    # Get a new UUID for the post_id ("_id" in MongoDB)
    post_id = get_uuid()
    # Get the timestamp, we will use this to populate users feeds
    post_time = timestamp()

    post = {
        '_id': post_id,             # Newly created post id
        'user_id': user_id,         # User id of the poster
        'username': username,       # Username of the poster
        'body': body,               # Body of the post
        'created': post_time,       # Unix timestamp for this moment in time
        'score': 0,                 # Atomic score counter
    }

    if reply_to is not None:
        # If the is a reply it must have this property
        post['reply_to'] = reply_to
    else:
        # Replies don't need a comment count
        post['comment_count'] = 0
        # Set the permission a user needs to view
        post['permission'] = permission

    # TODO: Make the upload process better at dealing with issues
    if upload:
        # If there is an upload along with this post it needs to go for
        # processing.
        # process_upload() can throw an Exception of UploadError. We will let
        # it fall through as a 500 is okay I think.
        # TODO: Turn this in to a Queue task at some point
        filename = process_upload(upload)

        if filename is not None:
            # If the upload process was okay attach the filename to the doc
            post['upload'] = filename
        else:
            # Stop the image upload process here if something went wrong.
            return None

    # Process everything thats needed in a post
    links, mentions, hashtags = parse_post(body)

    # Only add the fields if we need too.
    if links:
        post['links'] = links

    if mentions:
        post['mentions'] = mentions

    if hashtags:
        post['hashtags'] = hashtags

    # Add the post to the database
    # If the post isn't stored, result will be None
    result = m.db.posts.insert(post)

    # Only carry out the rest of the actions if the insert was successful
    if result:
        if reply_to is None:
            # Add post to authors feed
            r.zadd(k.USER_FEED.format(user_id), post_time, post_id)
            # Ensure the feed does not grow to large
            r.zremrangebyrank(k.USER_FEED.format(user_id), 0, -1000)

            # Subscribe the poster to there post
            subscribe(user_id, post_id, SubscriptionReasons.POSTER)

            # Alert everyone tagged in the post
            alert_tagees(mentions, user_id, post_id)

            # Append to all followers feeds or approved followers based
            # on the posts permission
            if permission < k.PERM_APPROVED:
                populate_followers_feeds(user_id, post_id, post_time)
            else:
                populate_approved_followers_feeds(user_id, post_id, post_time)

        else:
            # To reduce database look ups on the read path we will increment
            # the reply_to's comment count.
            m.db.posts.update({'_id': reply_to},
                              {'$inc': {'comment_count': 1}})

            # Alert all subscribers to the post that a new comment has been
            # added. We do this before subscribing anyone new
            alert = CommentingAlert(user_id, reply_to)

            subscribers = []
            # Iterate through subscribers and let them know about the comment
            for subscriber_id in get_subscribers(reply_to):
                # Ensure we don't get alerted for our own comments
                if subscriber_id != user_id:
                    subscribers.append(subscriber_id)

            # Push the comment alert out to all subscribers
            AlertManager().alert(alert, subscribers)

            # Subscribe the user to the post, will not change anything if they
            # are already subscribed
            subscribe(user_id, reply_to, SubscriptionReasons.COMMENTER)

            # Alert everyone tagged in the post
            alert_tagees(mentions, user_id, reply_to)

        return post_id

    # If there was a problem putting the post in to Mongo we will return None
    return None  # pragma: no cover
Exemple #6
0
 def test_urls_and_hashtags(self):
     """Hashtags intermixed with urls"""
     links, mentions, hashtags = parse_post('pjuu.com/#bottom #plop')
     self.assertEqual(links[0]['link'], 'http://pjuu.com/#bottom')
     self.assertEqual(hashtags[0]['hashtag'], 'plop')
from pjuu import create_app  # noqa
from pjuu.lib.parser import parse_post  # noqa


m = pymongo.MongoClient(host='localhost')


if __name__ == '__main__':
    # Set up Flask environment
    app = create_app()
    ctx = app.app_context()
    ctx.push()

    for post in m.pjuu.posts.find():
        # Parse the posts body like it has just come in
        if post.get('links') is None and post.get('mentions') is None and \
                post.get('hashtags') is None:

            links, mentions, hashtags = parse_post(post.get('body'))

            if links:
                post['links'] = links
            if mentions:
                post['mentions'] = mentions
            if hashtags:
                post['hashtags'] = hashtags

            m.pjuu.posts.update({'_id': post.get('_id')},
                                post)
Exemple #8
0
 def test_urls_and_hashtags(self):
     """Hashtags intermixed with urls"""
     links, mentions, hashtags = parse_post('pjuu.com/#bottom #plop')
     self.assertEqual(links[0]['link'], 'http://pjuu.com/#bottom')
     self.assertEqual(hashtags[0]['hashtag'], 'plop')