def get_timeline(): """ 获取个人Timeline 参数: limit: 可选,default=10 offset: 可选,default=10 """ limit = request.args.get('limit', 10, type=int) offset = request.args.get('offset', 0, type=int) key = Keys.user_timeline.format(g.user.id) print("View:", key) if not rd.exists(key): # A long time no-logged in user comes back #rd.rpush(Keys.timeline_events_queue, # Keys.user_returned.format(g.user.id)) _load_user_timeline(g.user.id) """element: type:id, type=s|a""" datas = rd.zrevrange(key, offset, offset + limit - 1) rd.expire(key, Keys.user_timeline_expire) items = [d.decode() for d in datas] print("Timeline items:", items) status_ids = [] article_ids = [] for item in items: if item[0] == Keys.timeline_status_prefix: status_ids.append(item[2:]) elif item[0] == Keys.timeline_article_prefix: article_ids.append(item[2:]) else: raise Exception("No such type") statuses = Cache.multiget_status_json(status_ids) articles = Cache.multiget_article_json(article_ids) res_map = {} for s in statuses: item_key = Keys.timeline_status_item.format(s['id']) res_map[item_key] = s for a in articles: item_key = Keys.timeline_article_item.format(a['id']) res_map[item_key] = a res = [res_map[t] for t in items if t in res_map] return jsonify(res)
def is_user_followed_by(id: IntLike, other_id: IntLike) -> bool: key = Keys.user_followers.format(id) if not rd.exists(key): _cache_followers(id) rd.expire(key, Keys.user_followers_expire) return rd.sismember(key, other_id)
def is_status_liked_by(id: IntLike, other_id: IntLike) -> bool: key = KEYS.status_liked_users.format(id) if not rd.exists(key): _cache_liked_users(id) return rd.sismember(key, other_id)
def is_account_followed_by(id: IntLike, user_id: IntLike) -> bool: key = KEYS.official_account_subscribers.format(id) if not rd.exists(key): _cache_account_subscribers(id) return rd.sismember(key, user_id)