def get_post(post_id): """Given a post id, return a dictionary with the body and avatar url.""" post = redis.get('post:%s' % (post_id,)) if post: user, post = post.split('|', 1) post = unicode(post, errors='ignore') return dict(body=post, user=get_gravatar(user, size=16))
def index(): if 'email' not in session: return redirect(url_for('.login')) email = session['email'] gravatar = get_gravatar(email, size=32) post_ids = redis.lrange('global:timeline', 0, 10) posts = [get_post(post_id) for post_id in post_ids] posts = filter(lambda x: x not in ['', None], posts) return render_template('index.html', avatar=gravatar, email=email, posts=posts)
def set_post(post): """Store a post and push it onto the global timeline.""" if 'email' not in session: return redirect(url_for('.login')) post_id = redis.incr('last-post-id') post = post[:140] # silently truncate! redis.set('post:%d' % (post_id,), '%s|%s' % (session['email'], post)) redis.lpush('global:timeline', post_id) redis.ltrim('global:timeline', 0, 10) publisher.publish(post, get_gravatar(session['email'], size=16))