Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unauth Home page] Sort Explore Spaces section on most activity #4578

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/domain/space/space/dto/explore.spaces.dto.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class ExploreSpacesInput {
@Field(() => Number, {
nullable: true,
description: 'Amount of Spaces returned.',
defaultValue: 30,
})
limit!: number;

@Field(() => Number, {
nullable: true,
description: 'Take into account only the activity in the past X days.',
defaultValue: 30,
})
daysOld!: number;
}
12 changes: 12 additions & 0 deletions src/domain/space/space/space.resolver.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UUID_NAMEID } from '@domain/common/scalars';
import { SpaceService } from './space.service';
import { ISpace } from './space.interface';
import { SpacesQueryArgs } from './dto/space.args.query.spaces';
import { ExploreSpacesInput } from './dto/explore.spaces.dto.input';
import { InnovationHub } from '@domain/innovation-hub/types';
import { GraphqlGuard } from '@core/authorization';
import { PaginatedSpaces, PaginationArgs } from '@core/pagination';
Expand Down Expand Up @@ -64,4 +65,15 @@ export class SpaceResolverQueries {
});
return space;
}

@UseGuards(GraphqlGuard)
@Query(() => [ISpace], {
nullable: false,
description: 'Active Spaces only, order by most active in the past X days.',
})
public exploreSpaces(
@Args('options', { nullable: true }) options?: ExploreSpacesInput
): Promise<ISpace[]> {
return this.spaceService.getExploreSpaces(options?.limit, options?.daysOld);
}
}
29 changes: 29 additions & 0 deletions src/domain/space/space/space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ import { templatesSetDefaults } from '../space.defaults/definitions/space.defaul
import { InputCreatorService } from '@services/api/input-creator/input.creator.service';
import { RoleSetService } from '@domain/access/role-set/role.set.service';
import { IRoleSet } from '@domain/access/role-set/role.set.interface';
import { Activity } from '@platform/activity';

const EXPLORE_SPACES_LIMIT = 30;
const EXPLORE_SPACES_ACTIVITY_DAYS_OLD = 30;

@Injectable()
export class SpaceService {
Expand Down Expand Up @@ -726,6 +730,31 @@ export class SpaceService {
return space;
}

public getExploreSpaces(
limit = EXPLORE_SPACES_LIMIT,
daysOld = EXPLORE_SPACES_ACTIVITY_DAYS_OLD
): Promise<ISpace[]> {
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - daysOld);

return (
this.spaceRepository
.createQueryBuilder('s')
.leftJoinAndSelect('s.authorization', 'authorization') // eager load the authorization
.innerJoin(Activity, 'a', 's.collaborationId = a.collaborationID')
.where({
level: SpaceLevel.SPACE,
visibility: SpaceVisibility.ACTIVE,
})
// activities in the past "daysOld" days
.andWhere('a.createdDate >= :daysAgo', { daysAgo })
.groupBy('s.id')
.orderBy('COUNT(a.id)', 'DESC')
.limit(limit)
.getMany()
);
}

async getSpace(
spaceID: string,
options?: FindOneOptions<Space>
Expand Down