Esempio n. 1
0
def view(_user):
    """
    Show the logged-in user their own feed.
    """
    from pyaspora.diaspora.models import MessageQueue
    if MessageQueue.has_pending_items(_user):
        return redirect(url_for('diaspora.run_queue', _external=True))

    limit = int(request.args.get('limit', 99))
    friend_ids = [f.id for f in _user.contact.friends()]
    clauses = [Post.Queries.shared_with_contact(_user.contact)]
    if friend_ids:
        clauses.append(
            Post.Queries.authored_by_contacts_and_public(friend_ids))
    tag_ids = [t.id for t in _user.contact.interests]
    if tag_ids:
        clauses.append(Tag.Queries.public_posts_for_tags(tag_ids))
    feed_query = or_(*clauses)
    feed = db.session.query(Share).join(Post). \
        outerjoin(PostTag).outerjoin(Tag). \
        filter(feed_query). \
        order_by(desc(Post.thread_modified_at)). \
        group_by(Post.id). \
        options(contains_eager(Share.post)). \
        limit(limit)

    data = {
        'feed': json_posts([(s.post, s) for s in feed], _user, True)
    }

    add_logged_in_user_to_data(data, _user)

    return render_response('feed.tpl', data)
Esempio n. 2
0
def view(_user):
    """
    Show the logged-in user their own feed.
    """
    from pyaspora.diaspora.models import MessageQueue
    if MessageQueue.has_pending_items(_user):
        return redirect(url_for('diaspora.run_queue', _external=True))

    limit = int(request.args.get('limit', 99))
    friend_ids = [f.id for f in _user.contact.friends()]
    clauses = [Post.Queries.shared_with_contact(_user.contact)]
    if friend_ids:
        clauses.append(
            Post.Queries.authored_by_contacts_and_public(friend_ids))
    tag_ids = [t.id for t in _user.contact.interests]
    if tag_ids:
        clauses.append(Tag.Queries.public_posts_for_tags(tag_ids))
    feed_query = or_(*clauses)
    feed = db.session.query(Share).join(Post). \
        outerjoin(PostTag).outerjoin(Tag). \
        filter(feed_query). \
        order_by(desc(Post.thread_modified_at)). \
        group_by(Post.id). \
        options(contains_eager(Share.post)). \
        limit(limit)

    data = {'feed': json_posts([(s.post, s) for s in feed], _user, True)}

    add_logged_in_user_to_data(data, _user)

    return render_response('feed.tpl', data)
Esempio n. 3
0
def view(_user):
    """
    Show the logged-in user their own feed.
    """
    from pyaspora.diaspora.models import MessageQueue
    if MessageQueue.has_pending_items(_user):
        return redirect(url_for('diaspora.run_queue', _external=True))

    limit = int(request.args.get('limit', 10))
    friend_ids = [f.id for f in _user.contact.friends()]
    clauses = [Post.Queries.shared_with_contact(_user.contact)]
    if friend_ids:
        clauses.append(
            Post.Queries.authored_by_contacts_and_public(friend_ids))
    tag_ids = [t.id for t in _user.contact.interests]
    if tag_ids:
        clauses.append(Tag.Queries.public_posts_for_tags(tag_ids))
    feed_query = or_(*clauses)
    my_share = aliased(Share)
    feed = db.session.query(Share).join(Post). \
        outerjoin(  # Stuff user hasn't hidden
            my_share,
            and_(
                Post.id == my_share.post_id,
                my_share.contact == _user.contact
            )
        ). \
        outerjoin(PostTag).outerjoin(Tag). \
        filter(feed_query). \
        filter(or_(my_share.hidden == None, not_(my_share.hidden))). \
        filter(Post.parent == None). \
        order_by(desc(Post.thread_modified_at)). \
        group_by(Post.id). \
        options(contains_eager(Share.post)). \
        options(joinedload(Share.post, Post.diasp)). \
        limit(limit)

    data = {
        'feed': json_posts([(s.post, s) for s in feed], _user, True),
        'limit': limit,
        'actions': {},
    }

    if len(data['feed']) >= limit:
        data['actions']['more'] = url_for('feed.view', limit=limit + 10, _external=True)

    add_logged_in_user_to_data(data, _user)

    return render_response('feed.tpl', data)
Esempio n. 4
0
def view(_user):
    """
    Show the logged-in user their own feed.
    """
    from pyaspora.diaspora.models import MessageQueue
    if MessageQueue.has_pending_items(_user):
        return redirect(url_for('diaspora.run_queue', _external=True))

    limit = int(request.args.get('limit', 10))
    friend_ids = [f.id for f in _user.contact.friends()]
    clauses = [Post.Queries.shared_with_contact(_user.contact)]
    if friend_ids:
        clauses.append(
            Post.Queries.authored_by_contacts_and_public(friend_ids))
    tag_ids = [t.id for t in _user.contact.interests]
    if tag_ids:
        clauses.append(Tag.Queries.public_posts_for_tags(tag_ids))
    feed_query = or_(*clauses)
    my_share = aliased(Share)
    feed = db.session.query(Share).join(Post). \
        outerjoin(  # Stuff user hasn't hidden
            my_share,
            and_(
                Post.id == my_share.post_id,
                my_share.contact == _user.contact
            )
        ). \
        outerjoin(PostTag).outerjoin(Tag). \
        filter(feed_query). \
        filter(or_(my_share.hidden == None, not_(my_share.hidden))). \
        filter(Post.parent == None). \
        order_by(desc(Post.thread_modified_at)). \
        group_by(Post.id). \
        options(contains_eager(Share.post)). \
        options(joinedload(Share.post, Post.diasp)). \
        limit(limit)

    data = {
        'feed': json_posts([(s.post, s) for s in feed], _user, True),
        'limit': limit,
    }
    if len(data['feed']) >= limit:
        data['actions'] = {
            'more': url_for('feed.view', limit=limit + 10, _external=True)
        }

    add_logged_in_user_to_data(data, _user)

    return render_response('feed.tpl', data)
Esempio n. 5
0
def run_queue(_user):
    start = datetime.now()
    retry = True
    processed = int(request.args.get('processed', 0))
    while datetime.now() < start + timedelta(seconds=3):
        if not MessageQueue.has_pending_items(_user):
            retry = False
            break
        MessageQueue.process_incoming_queue(_user, max_items=1)
        processed += 1

    data = {
        'count': processed,
        'next': url_for('.run_queue', processed=processed, _external=True)
    }
    add_logged_in_user_to_data(data, _user)

    if retry:
        resp = make_response(render_response('diaspora_queue.tpl', data))
        resp.headers['Refresh'] = '1;{0}'.format(data['next'])
        return resp
    else:
        return redirect(url_for('feed.view'))
Esempio n. 6
0
def run_queue(_user):
    start = datetime.now()
    retry = True
    processed = int(request.args.get('processed', 0))
    while datetime.now() < start + timedelta(seconds=3):
        if not MessageQueue.has_pending_items(_user):
            retry = False
            break
        MessageQueue.process_incoming_queue(_user, max_items=1)
        processed += 1

    data = {
        'count': processed,
        'next': url_for('.run_queue', processed=processed, _external=True)
    }
    add_logged_in_user_to_data(data, _user)

    if retry:
        resp = make_response(render_response('diaspora_queue.tpl', data))
        resp.headers['Refresh'] = '1;{0}'.format(data['next'])
        return resp
    else:
        return redirect(url_for('feed.view'))