Esempio n. 1
0
 def __init__(self, data):
     """
     """
     self.data = data
     self.processFields()
     self.data['content'] = formatMessageEntities(self.data['content'])
     hashtags = findHashtags(self.data['content'])
     if hashtags:
         self.data['_hashtags'] = hashtags
     self.data['_keywords'] = findKeywords(self.data['content'])
     self.update(self.data)
Esempio n. 2
0
def processTweet(twitter_username, content, tweetID='---'):
    """ Process inbound tweet
    """
    logger.info("(INFO) Processing tweet %s from @%s with content: %s" % (str(tweetID), twitter_username, content))

    conn = pymongo.Connection(mongodb_url)
    db = conn[mongodb_db_name]
    users = MADMaxCollection(db.users)
    contexts = MADMaxCollection(db.contexts)

    contexts_with_twitter_username = contexts.search({"twitterUsernameId": {"$exists": True}})
    readable_follow_list = [users_to_follow.get('twitterUsername', '').lower() for users_to_follow in contexts_with_twitter_username]
    twitter_username = twitter_username.lower()
    # If we have a tweet from a followed user
    if twitter_username in readable_follow_list:
        # Find the context
        maxcontext = contexts.search({"twitterUsername": twitter_username})

        # Watch for the case when two or more context share twitterUsername
        for context in maxcontext:
            url_hash = context.get("urlHash")
            context_url = context.get("url")

            # Construct the payload with the activity information
            newactivity = {
                "object": {
                    "objectType": "note",
                    "content": content
                },
                "contexts": [
                    context_url,
                ],
                "generator": twitter_generator_name
            }

            re = requests.post('%s/admin/contexts/%s/activities' % (max_server_url, url_hash), json.dumps(newactivity), auth=('admin', 'admin'), verify=False)
            if re.status_code == 201:
                # 200: Successful tweet from context
                logger.info("(201) Successfully posted tweet %s from @%s as context %s" % (str(tweetID), twitter_username, context_url))
                return "(201) %s tweet from context %s" % (twitter_username, context_url)
            else:
                # 500: Error accessing the MAX API
                logger.info("(404) Discarding tweet %s from @%s : There's no MAX user with that twitter username." % (str(tweetID), twitter_username))
                return "(404) No Such Max User"

        return "Should not see me..."

    # If we have a tweet from a tracked hashtag
    # Parse text and determine the second or nth hashtag
    possible_hastags = findHashtags(content)
    # Prepare query with normalized possible_hastags

    query = [dict(twitterHashtag=hashtag.lower()) for hashtag in possible_hastags]

    if debug_hashtag in possible_hastags:
        logger.info("%s Debug hashtag detected!" % content)
        return "%s Debug hashtag detected! %s" % content

    # Check if twitter_username is a registered for a valid MAX username
    # if not, discard it
    maxuser = users.search({"twitterUsername": twitter_username})
    if maxuser:
        maxuser = maxuser[0]
    else:
        logger.info("(404) Discarding tweet %s from @%s : There's no MAX user with that twitter username." % (str(tweetID), twitter_username))
        return "(404) %s: No such MAX user." % twitter_username

    # Check if hashtag is registered for a valid MAX context
    # if not, discard it
    successful_tweets = 0
    maxcontext = contexts.search({"$or": query})
    if maxcontext:
        for context in maxcontext:
            # Check if MAX username has permission to post to the MAX context
            # if not, discard it
            try:
                can_write = canWriteInContexts(maxuser, [context.url])
            except:
                can_write = False
                logger.info("(401) Failure posting tweet %s: User %s can't write to %s" % (str(tweetID), maxuser.username, context['url']))

            if can_write:

                # Construct the payload with the activity information
                newactivity = {
                    "object": {
                        "objectType": "note",
                        "content": content
                    },
                    "contexts": [
                        context.url,
                    ],
                    "generator": twitter_generator_name
                }

                # Use the restricted REST endpoint for create a new activity in the specified
                # MAX context in name of the specified MAX username
                re = requests.post('%s/admin/people/%s/activities' % (max_server_url, maxuser.username), json.dumps(newactivity), auth=('admin', 'admin'), verify=False)
                if re.status_code == 201:
                    logger.info("(201) Successfully posted tweet %s from @%s as %s on context %s" % (str(tweetID), twitter_username, maxuser['username'], context.url))
                    successful_tweets += 1
                    #return "(201) Successful tweet from user %s in context %s" % (maxuser, context.url)
                else:
                    logger.info("(500) Error accessing the MAX API at %s" % max_server_url)
                    #return "(500) MAX API error"

        error_message = len(maxcontext) != successful_tweets and " We weren't able to send the tweet to all contexts. See above for more information." or ""
        logger.info("(INFO) Processed tweet %s to %d of %d possible contexts.%s" % (str(tweetID), successful_tweets, len(maxcontext), error_message))
        if error_message:
            return "(401) Some posts not sent"
        else:
            return "(200) All posts sent"
    else:
        logger.info("(404) Discarding tweet %s from @%s: There are no MAX context with any of those hashtags" % (str(tweetID), twitter_username))
        return "(404) %s: Not such MAX context %s" % (maxuser.username, maxcontext)

    return "Should not see mee"
Esempio n. 3
0
def processTweet(twitter_username, content, tweetID='---'):
    """ Process inbound tweet
    """
    logger.info("(INFO) Processing tweet %s from @%s with content: %s" %
                (str(tweetID), twitter_username, content))

    conn = pymongo.Connection(mongodb_url)
    db = conn[mongodb_db_name]
    users = MADMaxCollection(db.users)
    contexts = MADMaxCollection(db.contexts)

    contexts_with_twitter_username = contexts.search(
        {"twitterUsernameId": {
            "$exists": True
        }})
    readable_follow_list = [
        users_to_follow.get('twitterUsername', '').lower()
        for users_to_follow in contexts_with_twitter_username
    ]
    twitter_username = twitter_username.lower()
    # If we have a tweet from a followed user
    if twitter_username in readable_follow_list:
        # Find the context
        maxcontext = contexts.search({"twitterUsername": twitter_username})

        # Watch for the case when two or more context share twitterUsername
        for context in maxcontext:
            url_hash = context.get("urlHash")
            context_url = context.get("url")

            # Construct the payload with the activity information
            newactivity = {
                "object": {
                    "objectType": "note",
                    "content": content
                },
                "contexts": [
                    context_url,
                ],
                "generator": twitter_generator_name
            }

            re = requests.post('%s/admin/contexts/%s/activities' %
                               (max_server_url, url_hash),
                               json.dumps(newactivity),
                               auth=('admin', 'admin'),
                               verify=False)
            if re.status_code == 201:
                # 200: Successful tweet from context
                logger.info(
                    "(201) Successfully posted tweet %s from @%s as context %s"
                    % (str(tweetID), twitter_username, context_url))
                return "(201) %s tweet from context %s" % (twitter_username,
                                                           context_url)
            else:
                # 500: Error accessing the MAX API
                logger.info(
                    "(404) Discarding tweet %s from @%s : There's no MAX user with that twitter username."
                    % (str(tweetID), twitter_username))
                return "(404) No Such Max User"

        return "Should not see me..."

    # If we have a tweet from a tracked hashtag
    # Parse text and determine the second or nth hashtag
    possible_hastags = findHashtags(content)
    # Prepare query with normalized possible_hastags

    query = [
        dict(twitterHashtag=hashtag.lower()) for hashtag in possible_hastags
    ]

    if debug_hashtag in possible_hastags:
        logger.info("%s Debug hashtag detected!" % content)
        return "%s Debug hashtag detected! %s" % content

    # Check if twitter_username is a registered for a valid MAX username
    # if not, discard it
    maxuser = users.search({"twitterUsername": twitter_username})
    if maxuser:
        maxuser = maxuser[0]
    else:
        logger.info(
            "(404) Discarding tweet %s from @%s : There's no MAX user with that twitter username."
            % (str(tweetID), twitter_username))
        return "(404) %s: No such MAX user." % twitter_username

    # Check if hashtag is registered for a valid MAX context
    # if not, discard it
    successful_tweets = 0
    maxcontext = contexts.search({"$or": query})
    if maxcontext:
        for context in maxcontext:
            # Check if MAX username has permission to post to the MAX context
            # if not, discard it
            try:
                can_write = canWriteInContexts(maxuser, [context.url])
            except:
                can_write = False
                logger.info(
                    "(401) Failure posting tweet %s: User %s can't write to %s"
                    % (str(tweetID), maxuser.username, context['url']))

            if can_write:

                # Construct the payload with the activity information
                newactivity = {
                    "object": {
                        "objectType": "note",
                        "content": content
                    },
                    "contexts": [
                        context.url,
                    ],
                    "generator": twitter_generator_name
                }

                # Use the restricted REST endpoint for create a new activity in the specified
                # MAX context in name of the specified MAX username
                re = requests.post('%s/admin/people/%s/activities' %
                                   (max_server_url, maxuser.username),
                                   json.dumps(newactivity),
                                   auth=('admin', 'admin'),
                                   verify=False)
                if re.status_code == 201:
                    logger.info(
                        "(201) Successfully posted tweet %s from @%s as %s on context %s"
                        % (str(tweetID), twitter_username, maxuser['username'],
                           context.url))
                    successful_tweets += 1
                    #return "(201) Successful tweet from user %s in context %s" % (maxuser, context.url)
                else:
                    logger.info("(500) Error accessing the MAX API at %s" %
                                max_server_url)
                    #return "(500) MAX API error"

        error_message = len(
            maxcontext
        ) != successful_tweets and " We weren't able to send the tweet to all contexts. See above for more information." or ""
        logger.info(
            "(INFO) Processed tweet %s to %d of %d possible contexts.%s" %
            (str(tweetID), successful_tweets, len(maxcontext), error_message))
        if error_message:
            return "(401) Some posts not sent"
        else:
            return "(200) All posts sent"
    else:
        logger.info(
            "(404) Discarding tweet %s from @%s: There are no MAX context with any of those hashtags"
            % (str(tweetID), twitter_username))
        return "(404) %s: Not such MAX context %s" % (maxuser.username,
                                                      maxcontext)

    return "Should not see mee"
Esempio n. 4
0
def configView(context, request):

    # XXX TODO Refactor this into a maintenance webservice in MAX

    page_title = "BIG MAX Server Config"
    api = TemplateAPI(context, request, page_title)
    success = False
    message = ''
    if request.params.get('form.rebuildKeywords', None) is not None:
        db = context.db
        activities = db.activity.find({'object.content': {'$exists': True}})
        for activity in activities:

            keywords = findKeywords(activity['object']['content'])
            hashtags = findHashtags(activity['object']['content'])

            replies = activity.get('replies', {}).get('items', [])
            for reply in replies:
                keywords.extend(findKeywords(reply.get('content', u'')))
                hashtags.extend(findHashtags(reply.get('content', u'')))

            keywords = list(set(keywords))
            hashtags = list(set(hashtags))

            db.activity.update({'_id': activity['_id']}, {'$set': {'object._keywords': keywords, 'object._hashtags': hashtags}})
        success = True
        message = 'Keywords rebuilded!'

    if request.params.get('form.resetPermissions', None) is not None:
        db = context.db
        contexts = {context['url']: context for context in db.contexts.find()}
        users = db.users.find()
        for user in users:
            subscriptions = user.get('subscribedTo', {})
            items = subscriptions.get('items', [])
            if items:
                for item in items:
                    curl = item.get('url')
                    permissions = ['read']
                    context = contexts.get(curl)
                    if context:
                        if context['permissions']['write'] == 'subscribed':
                            permissions.append('write')
                        item['permissions'] = permissions
                        for field in ['displayName']:
                            if context.get(field, None):
                                item[field] = context[field]

                #Purge subscriptions without context
                items = [item for item in items if item.get('permissions', None)]

                db.users.update({'_id': user['_id']}, {'$set': {'subscribedTo.items': items}})

        success = True
        message = 'Permissions Reseted to contexts defaults'

    return dict(api=api,
                url='%s/maintenance' % api.application_url,
                success=success,
                message=message)
    return {}