Ejemplo 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)
Ejemplo 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)
Ejemplo n.º 3
0
def feed(tag_name, _user):
    """
    Display recent public posts on a particular topic (Tag).
    """
    from pyaspora.post.models import Post, Share
    from pyaspora.post.views import json_posts

    tag = Tag.get_by_name(tag_name, create=False)
    if not tag:
        abort(404, 'No such tag')

    data = json_tag(tag)

    posts = db.session.query(Post). \
        join(PostTag). \
        join(Tag). \
        join(Share). \
        filter(Tag.Queries.public_posts_for_tags([tag.id])). \
        order_by(desc(Post.thread_modified_at)). \
        group_by(Post.id). \
        limit(100)

    data['feed'] = json_posts([(p, None) for p in posts])

    add_logged_in_user_to_data(data, _user)

    return render_response('tags_feed.tpl', data)
Ejemplo 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,
        '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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def _profile_base(contact_id, public=False):
    """
    Standard data for profile-alike pages, including the profile page and feed
    pages.
    """
    from pyaspora.post.models import Post, Share
    from pyaspora.post.views import json_posts

    contact = Contact.get(contact_id)
    if not contact:
        abort(404, 'No such contact', force_status=True)

    viewing_as = None if public else logged_in_user()

    data = json_contact(contact, viewing_as)
    limit = int(request.args.get('limit', 25))

    if viewing_as and request.args.get('refresh', False) and contact.diasp:
        try:
            contact.diasp.import_public_posts()
            db.session.commit()
        except:
            current_app.logger.debug(format_exc())

    # If not local, we don't have a proper feed
    if viewing_as or contact.user:
        # user put it on their public wall
        feed_query = Post.Queries.public_wall_for_contact(contact)
        if viewing_as:
            # Also include things this user has shared with us
            shared_query = Post.Queries.author_shared_with(
                contact, viewing_as)
            feed_query = or_(feed_query, shared_query)

        feed = db.session.query(Share). \
            join(Post). \
            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], viewing_as)

    add_logged_in_user_to_data(data, viewing_as)
    return data, contact
Ejemplo n.º 7
0
def _profile_base(contact_id, public=False):
    """
    Standard data for profile-alike pages, including the profile page and feed
    pages.
    """
    from pyaspora.post.models import Post, Share
    from pyaspora.post.views import json_posts

    contact = Contact.get(contact_id)
    if not contact:
        abort(404, 'No such contact', force_status=True)

    viewing_as = None if public else logged_in_user()

    data = json_contact(contact, viewing_as)
    limit = int(request.args.get('limit', 25))

    if viewing_as and request.args.get('refresh', False) and contact.diasp:
        try:
            contact.diasp.import_public_posts()
            db.session.commit()
        except:
            current_app.logger.debug(format_exc())

    # If not local, we don't have a proper feed
    if viewing_as or contact.user:
        # user put it on their public wall
        feed_query = Post.Queries.public_wall_for_contact(contact)
        if viewing_as:
            # Also include things this user has shared with us
            shared_query = Post.Queries.author_shared_with(contact, viewing_as)
            feed_query = or_(feed_query, shared_query)

        feed = db.session.query(Share). \
            join(Post). \
            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], viewing_as)

    add_logged_in_user_to_data(data, viewing_as)
    return data, contact