Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add pagination response fields to /api/v1/users/connections.
Add response field function to other criteria filters.
  • Loading branch information
Misterblue committed Nov 3, 2020
1 parent e4a58ca commit 44a371d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/Entities/EntityFilters/AccountFilterInfo.ts
Expand Up @@ -130,6 +130,11 @@ export class AccountFilterInfo extends CriteriaFilter {
}
};

// Add any parameters to the response
public addResponseFields(pRequest: Request) {
return;
};

// Passed (what should be) an AccountEntity, test if the filters
// think it's passable.
// Return 'true' of this account fits the search criteria.
Expand Down
5 changes: 5 additions & 0 deletions src/Entities/EntityFilters/AccountScopeFilter.ts
Expand Up @@ -60,6 +60,11 @@ export class AccountScopeFilter extends CriteriaFilter {
};
};

// Add any parameters to the response
public addResponseFields(pRequest: Request) {
return;
};

// Return if we've found admin enabling parameters
public AsAdmin(): boolean {
return this._asAdmin;
Expand Down
3 changes: 3 additions & 0 deletions src/Entities/EntityFilters/CriteriaFilter.ts
Expand Up @@ -20,6 +20,9 @@ export abstract class CriteriaFilter {
// Take a request and extract filter parameters
abstract parametersFromRequest(pRequest: Request): void;

// Adds additional fields to the response
abstract addResponseFields(pRequest: Request): void;

// Test a thing and return 'true' if it should be included in the set
abstract criteriaTest(pThingy: any): boolean;

Expand Down
5 changes: 5 additions & 0 deletions src/Entities/EntityFilters/GenericFilter.ts
Expand Up @@ -34,6 +34,11 @@ export class GenericFilter extends CriteriaFilter {
return;
};

// Add any parameters to the response
public addResponseFields(pRequest: Request) {
return;
};

// Test a thing and return 'true' if it should be included in the set
public criteriaTest(pThingy: any): boolean {
return true;
Expand Down
15 changes: 15 additions & 0 deletions src/Entities/EntityFilters/PaginationInfo.ts
Expand Up @@ -23,6 +23,8 @@ import { Logger } from '@Tools/Logging';
export class PaginationInfo extends CriteriaFilter {
public PageNum: number = 1;
public PerPage: number = 20;
public TotalPages: number = 1;
public TotalEntries: number = 20;

// results from as filter operation
private _currentPage: number = 1;
Expand All @@ -35,6 +37,8 @@ export class PaginationInfo extends CriteriaFilter {
super();
this.PageNum = Clamp(pPageNum, 1, 1000);
this.PerPage = Clamp(pPerPage, 1, 1000);
this.TotalPages = this.PageNum;
this.TotalEntries = 0;
}

public parametersFromRequest(pRequest: Request) : void {
Expand All @@ -44,6 +48,8 @@ export class PaginationInfo extends CriteriaFilter {
if (pRequest.query.per_page) {
this.PerPage = Clamp(Number(pRequest.query.per_page), 1, 1000);
};
this.TotalPages = this.PageNum;
this.TotalEntries = 0;
// Logger.debug(`PaginstationInfo: pageNum=${this.PageNum}, perPage=${this.PerPage}`);
}

Expand All @@ -52,9 +58,18 @@ export class PaginationInfo extends CriteriaFilter {
// "total_pages": num,
// "per_page": num,
// "total_entries": num
public addResponseFields(pRequest: Request): void {
if (pRequest.vRestResp) {
pRequest.vRestResp.addAdditionalField('current_page', this.PageNum);
pRequest.vRestResp.addAdditionalField('per_page', this.PerPage);
pRequest.vRestResp.addAdditionalField('total_pages', this.TotalPages);
pRequest.vRestResp.addAdditionalField('total_entries', this.TotalEntries);
};
};

public criteriaTest(pThingy: any): boolean {
if (! this._doingQuery) {
this.TotalEntries++;
if (++this._currentItem > this.PerPage) {
this._currentItem = 1;
++this._currentPage;
Expand Down
5 changes: 5 additions & 0 deletions src/Entities/EntityFilters/RequestScopeFilter.ts
Expand Up @@ -72,6 +72,11 @@ export class RequestScopeFilter extends CriteriaFilter {
};
};

// Add any parameters to the response
public addResponseFields(pRequest: Request) {
return;
};

// Return if we've found admin enabling parameters
public AsAdmin(): boolean {
return this._asAdmin;
Expand Down
8 changes: 4 additions & 4 deletions src/Entities/Sessions.ts
Expand Up @@ -17,7 +17,7 @@ import { Config } from '@Base/config';

import { SessionEntity } from '@Entities/SessionEntity';

import { PaginationInfo } from '@Entities/EntityFilters/PaginationInfo';
import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter';

import { VKeyedCollection } from '@Tools/vTypes';
import { GenUUID, genRandomString, IsNullOrEmpty } from '@Tools/Misc';
Expand Down Expand Up @@ -87,10 +87,10 @@ export const Sessions = {
clearCounts(pSession: SessionEntity): void {
pSession.countReference = 0;
},
*enumerate(pPager?: PaginationInfo): Generator<SessionEntity> {
*enumerate(pFilter?: CriteriaFilter): Generator<SessionEntity> {
for (const sess of _currentSessions.values()) {
if (pPager) {
if (pPager.criteriaTest(sess)) {
if (pFilter) {
if (pFilter.criteriaTest(sess)) {
yield sess;
};
}
Expand Down
1 change: 1 addition & 0 deletions src/routes/api/v1/accounts.ts
Expand Up @@ -47,6 +47,7 @@ const procGetAccounts: RequestHandler = async (req: Request, resp: Response, nex
req.vRestResp.Data = {
accounts: accts
};
pager.addResponseFields(req);
}
else {
req.vRestResp.respondFailure('No account specified');
Expand Down
34 changes: 19 additions & 15 deletions src/routes/api/v1/users/connections.ts
Expand Up @@ -43,26 +43,30 @@ const procGetUsersConnections: RequestHandler = async (req: Request, resp: Respo
Logger.debug(`procGetUsersConnections: user=${req.vAuthAccount.username}, connections=${JSON.stringify(connections)}`);
const connectionInfo: any[] = [];
for (const connectionUsername of connections) {
const aAccount = await Accounts.getAccountWithUsername(connectionUsername);
if (aAccount) {
connectionInfo.push( {
'username': connectionUsername,
'connection': SArray.has(req.vAuthAccount.friends, connectionUsername) ? 'is_friend' : 'is_connection',
'images': await buildImageInfo(aAccount),
'location': await buildLocationInfo(aAccount)
});
}
else {
Logger.error(`procGetUsersConnections: connection name with no account. acct=${req.vAuthAccount.id}, name=${connectionUsername}`);
connectionInfo.push( {
'username': connectionUsername,
'connection': 'unknown'
});
if (pager.criteriaTest(connectionUsername)) {
const aAccount = await Accounts.getAccountWithUsername(connectionUsername);
if (aAccount) {
connectionInfo.push( {
'username': connectionUsername,
'connection': SArray.has(req.vAuthAccount.friends, connectionUsername) ? 'is_friend' : 'is_connection',
'images': await buildImageInfo(aAccount),
'location': await buildLocationInfo(aAccount)
});
}
else {
Logger.error(`procGetUsersConnections: connection name with no account. acct=${req.vAuthAccount.id}, name=${connectionUsername}`);
connectionInfo.push( {
'username': connectionUsername,
'connection': 'unknown'
});
};
};
};
req.vRestResp.Data = {
'users': connectionInfo
};
// Add the 'current_page' and 'total_pages' to the response
pager.addResponseFields(req);
}
else {
req.vRestResp.respondFailure('unauthorized');
Expand Down
2 changes: 2 additions & 0 deletions src/routes/explore.ts
Expand Up @@ -68,6 +68,8 @@ const procGetExplore: RequestHandler = async (req: Request, resp: Response, next
};
req.vRestResp.Data = allPlaces;

pager.addResponseFields(req);

next();
};

Expand Down

0 comments on commit 44a371d

Please sign in to comment.