Skip to content

Commit

Permalink
Implement POST /api/v1/user/friends so a user can make a connection i…
Browse files Browse the repository at this point in the history
…nto a friend.
  • Loading branch information
Misterblue committed Nov 3, 2020
1 parent 44a371d commit 292681a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/Entities/Accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export const Accounts = {
},
// The contents of this entity have been updated
async updateEntityFields(pEntity: AccountEntity, pFields: VKeyedCollection): Promise<AccountEntity> {
return updateObjectFields(accountCollection,
new GenericFilter({ 'id': pEntity.id }), pFields);
return updateObjectFields(accountCollection, new GenericFilter({ 'id': pEntity.id }), pFields);
},
// Return the number of accounts that match the criteria
async accountCount(pCriteria: CriteriaFilter): Promise<number> {
Expand Down
27 changes: 23 additions & 4 deletions src/routes/api/v1/user/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getAccountField, setAccountField } from '@Entities/AccountEntity';

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

import { VKeyedCollection } from '@Tools/vTypes';
import { SArray, VKeyedCollection } from '@Tools/vTypes';
import { IsNullOrEmpty, IsNotNullOrEmpty } from '@Tools/Misc';

// Get the friends of the logged in account
Expand All @@ -41,15 +41,34 @@ const procGetUserFriends: RequestHandler = async (req: Request, resp: Response,
};
}
else {
req.vRestResp.respondFailure('account token did not work');
req.vRestResp.respondFailure('unauthorized');
};
next();
};

// Upgrade a connection to a friend.
// Not implemented as something needs to be done with request_connection, etc
const procPostUserFriends: RequestHandler = async (req: Request, resp: Response, next: NextFunction) => {
req.vRestResp.respondFailure('cannot add friends this way');
if (req.vAuthAccount) {
if (req.body.username && typeof(req.body.username) === 'string') {
const newFriend = req.body.username;
// Verify the username is a connection.
const connections: string[] = await getAccountField(req.vAuthToken, req.vAuthAccount, 'connections', req.vAuthAccount) ?? [];
if (SArray.hasNoCase(connections, newFriend)) {
const updates: VKeyedCollection = {};
await setAccountField(req.vAuthToken, req.vAuthAccount, 'friends', { "add": newFriend }, req.vAuthAccount, updates);
await Accounts.updateEntityFields(req.vAuthAccount, updates);
}
else {
req.vRestResp.respondFailure('cannot add friend who is not a connection');
};
}
else {
req.vRestResp.respondFailure('badly formed request');
};
}
else {
req.vRestResp.respondFailure('unauthorized');
};
next();
};

Expand Down

0 comments on commit 292681a

Please sign in to comment.