Exemplo n.º 1
0
    def get(self, params):
        # 查询首页顶部用户
        if params["position"] == 1:
            return self.home_top_user_request(params)

        # 查询首页下部推荐用户中的活跃用户
        active_user_list = []

        # 查询所有缓存的活跃用户
        if params["refresh"] == 0 and (params["last_id"] != 0
                                       or params["offset"] != 0):
            cache_user_list = cache.get(self.home_active_user_cache_key)
            if cache_user_list:
                active_user_list = cache_user_list

        # 如果没有缓存的活跃用户,则查询数据库,并重新缓存结果
        if not active_user_list and params["refresh"] == 0:
            active_user_list = HomeRecommendModel.query_home_active_users(
                24, params)
            if active_user_list:
                cache.set(self.home_active_user_cache_key, active_user_list,
                          self.home_active_user_cache_time)

        # 从缓存的活跃用户中分页获取要显示的用户列表
        offset = params["offset"]
        if offset == 0:
            result_list = active_user_list[params["last_id"]:offset +
                                           self.per_page]
            if not result_list:
                result_list = []
        else:
            result_list = []

        # 如果缓存用户不足一页,则从home_recommend表中随机查询其它的用户
        if result_list and len(result_list) < self.per_page:
            params["active_ids"] = array_column(result_list, "user_id")

        params["limit"] = self.per_page - len(result_list)
        if params["limit"] > 0:
            params["active_list"] = array_column(active_user_list, "user_id")

            if not params["offset"]:
                db_count = db.session.query(func.count(
                    HomeRecommendModel.id)).scalar()
                params["offset"] = randint(1, db_count) if db_count else 0
                params["last_id"] = 0

            result_list_add = HomeRecommendModel.query_home_random_users(
                params)
        else:
            result_list_add = []

        # 合并活跃用户和普通用户
        home_recommend = result_list + result_list_add

        if not home_recommend:
            return json_success_response([])

        return json_success_response(
            HomeRecommendHandler.format_home_users(home_recommend))
Exemplo n.º 2
0
 def query_relation_follows(user_id, follow_list=()):
     follow_list = FollowModel.query.filter_by(
         user_id=user_id,
         status=1).filter(FollowModel.follow_id.in_(follow_list)).all()
     if not follow_list:
         follow_list = []
     return array_column(follow_list, "follow_id")
Exemplo n.º 3
0
    def get(self, params):
        params["limit"] = self.per_page

        user_list = []
        field = ""
        # 关注
        if params["type"] == 2:
            user_list = FollowModel.query_follow_list(params)
            field = "follow_id"
        # 粉丝
        elif params["type"] == 3:
            user_list = FollowModel.query_fans_list(params)
            field = "user_id"
        # 好友
        elif params["type"] == 4:
            user_list = FollowModel.query_friends_list(params)
            field = "follow_id"
        if not user_list:
            return json_success_response(user_list)

        other_user_id_list = array_column(user_list, field)
        user_relations_dict = FollowModel.query_relation_to_user_list(g.account["user_id"], other_user_id_list)

        result = list()
        for follow_model in user_list:
            item = dict()
            item["id"] = follow_model.id
            user_id = getattr(follow_model, field)
            item["relation_type"] = user_relations_dict.get(user_id, 0)
            user_model = UserInfoModel.query_user_model_by_id(user_id)
            item["user_info"] = UserInfoModel.format_user_info(user_model)
            result.append(item)

        return json_success_response(result)
Exemplo n.º 4
0
    def format_home_users(user_list=list()):
        """
        格式化首页的用户
        """
        result = []

        if not user_list:
            return result

        image_list = ImageModel.query_image_by_image_id_list(
            array_column(user_list, "image_id"))
        image_list = array_column_key(image_list, "user_id")

        for user in user_list:

            item = dict()

            # 用户信息
            user_info = UserInfoModel.query_user_model_by_id(user.user_id)
            item["user_info"] = UserInfoModel.format_user_info(user_info)

            image_model = image_list.get(user.user_id, None)
            item["cover"] = ImageModel.generate_image_url(
                image_model, "c") if image_model else ""
            item["last_id"] = 0
            item["offset"] = 0
            item["relation_type"] = 0

            result.append(item)

        return result
Exemplo n.º 5
0
    def get(self, params):
        offset = IndexHandler.per_page * params["limit"]
        user_black_list = UserBlackModel.query_user_black_list(
            g.account["user_id"], 1, offset, IndexHandler.per_page)

        user_id_list = array_column(user_black_list, "black_user_id")

        result = list()

        for user_id in user_id_list:

            user_info = UserInfoModel.query_user_model_by_id(user_id)
            if not user_info:
                continue
            item = dict()
            item["user_id"] = user_info.user_id
            item["nickname"] = user_info.nickname

            item["avatar"] = UserInfoModel.format_user_avatar(user_info)
            user_id_relation = UserIdRelationModel.query_user_id_relation(
                user_info.user_id)
            item[
                "huanxin_uid"] = user_id_relation.str_id if user_id_relation else ""
            result.append(item)

        return json_success_response(result)