def verify_auth_token(token): import app.cache as Cache import app.cache.redis_keys as KEYS """ Get current User from token """ token_key = KEYS.user_token.format(token) data = rd.get(token_key) if data is not None: return Cache.get_user(data.decode()) s = Serializer('auth' + current_app.config['SECRET_KEY']) try: data = s.loads(token) rd.set(token_key, data['id'], KEYS.user_token_expire) return Cache.get_user(data['id']) except: return None
def user_first_created(u): developer = Cache.get_user(1) developer.followers.append(u) db.session.add(u) for a in OfficialAccount.query: a.subscribers.append(u) db.session.add(a)
def get_user_followed(): id = request.args.get('id', -1, type=int) offset = request.args.get('offset', 0, type=int) limit = request.args.get('limit', 10, type=int) user = Cache.get_user(id) if user is None: return not_found('找不到该用户') users = user.followed.offset(offset).limit(limit) users = [u.to_json() for u in users] return jsonify(users)
def delete_user_followed(): id = request.args.get('id', -1, type=int) user = Cache.get_user(id) if user is None: return not_found('找不到该用户') if user not in g.user.followed: return jsonify({'message': 'already unfollowed'}) g.user.followed.remove(user) db.session.add(g.user) db.session.commit() return jsonify({'message': 'unfollow success'})
def user_first_created(u): """ 用户刚注册时,添加订阅公众号信息 :param u: :return: """ developer = Cache.get_user(1) developer.followers.append(u) db.session.add(u) for a in OfficialAccount.query: a.subscribers.append(u) db.session.add(a)
def create_user_followed(): id = request.json.get('id', -1) user = Cache.get_user(id) if user is None: return not_found('找不到该用户') if user == g.user: return bad_request('不可以关注自己哦') if user in g.user.followed: return jsonify({'message': 'already followed'}) g.user.followed.append(user) db.session.add(g.user) db.session.commit() return jsonify({'message': 'followed success'})
def get_user_groups(): id = request.args.get('id', -1, type=int) offset = request.args.get('offset', 0, type=int) limit = request.args.get('limit', 10, type=int) user = Cache.get_user(id) if user is None: return not_found('找不到该用户') groups = user.groups[offset:offset + limit] json = [] for group in groups: item = group.to_json() item['title'] = group.get_user_title(user) json.append(item) return jsonify(json)
def get_user(): ''' 根据id获取用户信息 Note: id不存在时返回当前登录用户信息 ''' id = request.args.get('id', -1, type=int) username = request.args.get('username', '') keyword = request.args.get('keyword', '') offset = request.args.get('offset', 0, type=int) limit = request.args.get('limit', 10, type=int) if id != -1: user = Cache.get_user(id) if user == None: return not_found("找不到该用户") return jsonify(user.to_json()) if username != '': user = User.query.filter_by(username=username).first_or_404() return jsonify(user.to_json()) if keyword != '': users = User.query.filter(User.username.like('%' + keyword + '%')) users = users.order_by(func.char_length(User.username)) users = users.offset(offset).limit(limit) users = [u.to_json() for u in users] return jsonify(users) return jsonify(g.user.to_json())
def get_status(): """ 获取动态 分类: 1. 获取某条特定动态, 团体微博, 团体帖子 params = { id } 3. 获取个人微博(时间序) params = { type: user, user_id: } 2. 获取团体微博(时间序) params = { type: group_status, group_id: } 2. 获取用户动态(时间序) params = { type: status, } 3. 获取团体帖子(热门序) params = { type: post, group_id = Integer } 4. 获取推荐(热门序) params = { type: timeline, } 5. 获取关注微博(时间序) params = { type: followed, } 6. 获取话题下的微博: params = { type: topic, topic_name: // } 公共参数 limit: 可选, default=10 Note: 1. 根据offset排序的小问题: a. 当某条内容上升到offset之前, 用户可能错过, 忽略不计, b. 当某条内容下降到offset之后, 用户可能重新刷到, 客户端需要处理重叠 2. 当返回空数组时, 代表没有更多内容, 客户端自行处理 """ id = request.args.get('id', -1, type=int) type = request.args.get('type', '') user_id = request.args.get('user_id', -1, type=int) group_id = request.args.get('group_id', -1, type=int) only_with_comment = request.args.get('only_with_comment', "") topic = request.args.get('topic', "") offset = request.args.get('offset', 0, type=int) limit = request.args.get('limit', 10, type=int) if id != -1: return jsonify(Cache.get_status_json(id)) if type == 'user': u = Cache.get_user(user_id) if u is None: return not_found('找不到该用户') ss = Status.query.filter_by(user=u) ss = ss.order_by(Status.timestamp.desc()) ss = ss.offset(offset).limit(limit) ss = [s.to_json() for s in ss] return jsonify(ss) if type == 'group_status': group = Group.query.get(group_id) if group is None: return not_found('找不到该团体') ss = Status.query.filter_by(group=group, type_id=Status.TYPES['GROUP_STATUS']) ss = ss.order_by(Status.timestamp.desc()) ss = ss.offset(offset).limit(limit) ss = [s.to_json() for s in ss] return jsonify(ss) if type == "status": ss = Status.query.filter_by(type_id=Status.TYPES['USER_STATUS']) ss = ss.order_by(Status.id.desc()) ss = ss.offset(offset).limit(limit) ss = [s.to_json() for s in ss] return jsonify(ss) if type == 'post': ss = Status.query.filter_by(type_id=Status.TYPES['GROUP_POST']) if group_id != -1: ss = ss.filter_by(group_id=group_id) ss = ss.order_by(Status.timestamp.desc()) ss = ss.offset(offset).limit(limit) ss = [s.to_json() for s in ss] return jsonify(ss) if type == 'hot': ss = Status.query.order_by(Status.timestamp.desc()) ss = ss.offset(offset).limit(limit) ss = [s.to_json() for s in ss] rank.get_fresh() return jsonify(ss) if type == 'timeline': ### TODO: with entities might be useful here if not hasattr(g, 'user'): return jsonify([]) sql2 = """ select * from ( select 0 as kind, id, timestamp from statuses where user_id=:UID or user_id in ( select followed_id from user_follows as F where F.follower_id=:UID ) union select 1 as kind, id, timestamp from articles where official_account_id in ( select official_account_id from subscriptions as S where S.users_id=:UID ) ) as t order by timestamp DESC limit :LIMIT offset :OFFSET; """ # `S.users_id` because there is a typo in column name result = db.engine.execute(text(sql2), UID=g.user.id, LIMIT=limit, OFFSET=offset) result = list(result) status_ids = [ item['id'] for item in result if item['kind'] == 0] article_ids = [ item['id'] for item in result if item['kind'] == 1 ] statuses = Status.query.filter(Status.id.in_( status_ids)).all() articles = Article.query.filter(Article.id.in_( article_ids)).all() res = statuses + articles res = sorted(res, key=lambda x: x.timestamp, reverse=True) res = [item.to_json() for item in res] return jsonify(res) #if type == "trending": #ids = rank.get_mixed()[offset:offset+limit] #ss = [Status.query.get(id).to_json() for id in ids] #return jsonify(ss) if type == 'topic': key = Keys.topic_id.format(topic_name=topic) data = rd.get(key) if data != None: topic_id = data.decode() else: t = Topic.query.filter_by(topic=topic).first() if t is None: return jsonify([]) topic_id = t.id rd.set(key, topic_id, Keys.topic_id_expire) sql = """ select status_id from status_topic where topic_id=:TOPIC_ID order by status_id DESC limit :LIMIT offset :OFFSET; """ result = db.engine.execute(text(sql), TOPIC_ID=topic_id, OFFSET=offset, LIMIT=limit) result = list(result) status_ids = [item['status_id'] for item in result] statuses = Cache.multiget_status_json(status_ids) res_map = {} for s in statuses: res_map[s['id']] = s res = [ res_map[id] for id in status_ids ] return jsonify(res) return bad_request('参数有误')