-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add get all places endpoint (#560)
* feat: Add task to fetch worlds live users * feat: Add endpoint to return merged Places and Worlds scene info * feat: Add tests * fix: Use max limit constant * feat: Add utils tests * feat: Add env var WORLDS_LIVE_DATA
- Loading branch information
Showing
15 changed files
with
1,211 additions
and
55 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { WorldLiveDataProps } from "../entities/World/types" | ||
|
||
export const worldsLiveData: WorldLiveDataProps = { | ||
perWorld: [{ worldName: "test.dcl.eth", users: 30 }], | ||
totalUsers: 30, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { withAuthOptional } from "decentraland-gatsby/dist/entities/Auth/routes/withDecentralandAuth" | ||
import Context from "decentraland-gatsby/dist/entities/Route/wkc/context/Context" | ||
import ApiResponse from "decentraland-gatsby/dist/entities/Route/wkc/response/ApiResponse" | ||
import Router from "decentraland-gatsby/dist/entities/Route/wkc/routes/Router" | ||
import { | ||
bool, | ||
numeric, | ||
oneOf, | ||
} from "decentraland-gatsby/dist/entities/Schema/utils" | ||
|
||
import PlaceModel from "../../Place/model" | ||
import { PlaceListOrderBy } from "../../Place/types" | ||
import { getHotScenes } from "../../RealmProvider/utils" | ||
import { getSceneStats } from "../../SceneStats/utils" | ||
import { getWorldsLiveData } from "../../World/utils" | ||
import { getAllPlacesListQuerySchema } from "../schemas" | ||
import { | ||
DEFAULT_MAX_LIMIT, | ||
FindAllPlacesWithAggregatesOptions, | ||
GetAllPlaceListQuery, | ||
} from "../types" | ||
import { allPlacesWithAggregates } from "../utils" | ||
import { getAllPlacesMostActiveList } from "./getAllPlacesMostActiveList" | ||
|
||
export const validateGetPlaceListQuery = Router.validator<GetAllPlaceListQuery>( | ||
getAllPlacesListQuerySchema | ||
) | ||
|
||
export const getAllPlacesList = Router.memo( | ||
async (ctx: Context<{}, "url" | "request">) => { | ||
if (ctx.url.searchParams.get("order_by") === PlaceListOrderBy.MOST_ACTIVE) { | ||
return getAllPlacesMostActiveList(ctx) | ||
} | ||
|
||
const query = await validateGetPlaceListQuery({ | ||
offset: ctx.url.searchParams.get("offset"), | ||
limit: ctx.url.searchParams.get("limit"), | ||
only_favorites: ctx.url.searchParams.get("only_favorites"), | ||
only_featured: ctx.url.searchParams.get("only_featured"), | ||
only_highlighted: ctx.url.searchParams.get("only_highlighted"), | ||
positions: ctx.url.searchParams.getAll("positions"), | ||
names: ctx.url.searchParams.getAll("names"), | ||
order_by: | ||
oneOf(ctx.url.searchParams.get("order_by"), [ | ||
PlaceListOrderBy.LIKE_SCORE_BEST, | ||
PlaceListOrderBy.UPDATED_AT, | ||
PlaceListOrderBy.CREATED_AT, | ||
]) || PlaceListOrderBy.LIKE_SCORE_BEST, | ||
order: | ||
oneOf(ctx.url.searchParams.get("order"), ["asc", "desc"]) || "desc", | ||
with_realms_detail: ctx.url.searchParams.get("with_realms_detail"), | ||
search: ctx.url.searchParams.get("search"), | ||
categories: ctx.url.searchParams.getAll("categories"), | ||
}) | ||
|
||
const userAuth = await withAuthOptional(ctx) | ||
|
||
if (bool(query.only_favorites) && !userAuth?.address) { | ||
return new ApiResponse([], { total: 0 }) | ||
} | ||
|
||
const options: FindAllPlacesWithAggregatesOptions = { | ||
user: userAuth?.address, | ||
offset: numeric(query.offset, { min: 0 }) ?? 0, | ||
limit: | ||
numeric(query.limit, { min: 0, max: DEFAULT_MAX_LIMIT }) ?? | ||
DEFAULT_MAX_LIMIT, | ||
only_favorites: !!bool(query.only_favorites), | ||
only_highlighted: !!bool(query.only_highlighted), | ||
positions: query.positions, | ||
names: query.names, | ||
order_by: query.order_by, | ||
order: query.order, | ||
search: query.search, | ||
categories: query.categories, | ||
} | ||
|
||
const hotScenes = getHotScenes() | ||
const worldsLiveData = getWorldsLiveData() | ||
|
||
const [data, total, sceneStats] = await Promise.all([ | ||
PlaceModel.findAllPlacesWithAggregates(options), | ||
PlaceModel.countAllPlaces(options), | ||
getSceneStats(), | ||
]) | ||
|
||
const response = allPlacesWithAggregates( | ||
data, | ||
hotScenes, | ||
sceneStats, | ||
worldsLiveData, | ||
{ | ||
withRealmsDetail: !!bool(query.with_realms_detail), | ||
} | ||
) | ||
|
||
return new ApiResponse(response, { total }) | ||
} | ||
) |
Oops, something went wrong.