Skip to content

Commit

Permalink
use auth_users.public_keys.is_primary in all relevant queries (#3589)
Browse files Browse the repository at this point in the history
  • Loading branch information
wentokay authored Apr 7, 2023
1 parent 26ca764 commit 7d51e40
Show file tree
Hide file tree
Showing 25 changed files with 967 additions and 209 deletions.
2 changes: 2 additions & 0 deletions .actrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-P ubuntu-latest=catthehacker/ubuntu:full-latest
--container-architecture linux/amd64
44 changes: 44 additions & 0 deletions .github/actions/backend-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Backend tests
description: Runs backend tests using pm2 and docker-compose

runs:
using: composite
steps:
- name: "Install pm2 to run node backend services for tests"
shell: bash
run: npm install pm2 --global

- name: "Start backend/reef/docker-compose.yml containers for backend services"
shell: bash
run: cd backend/reef && docker compose up -d --build --wait

- name: "Run xnft-api-server"
shell: bash
run: pm2 start backend/native/xnft-api-server/dist/index.js -- --port 8080

- name: "Test xnft-api-server"
shell: bash
run: cd backend/native/xnft-api-server && npx vitest

- if: always()
name: "Stop xnft-api-server"
shell: bash
run: pm2 kill

- name: "Run backpack-api"
shell: bash
run: pm2 start backend/native/backpack-api/dist/index.js -- --port 8080

- name: "Test backpack-api"
shell: bash
run: cd backend/native/backpack-api && npx vitest

- if: always()
name: "Stop backpack-api"
shell: bash
run: pm2 kill

- if: always()
name: "Stop docker containers"
shell: bash
run: cd backend/reef && docker compose down --remove-orphans --volumes
11 changes: 11 additions & 0 deletions .github/workflows/pull_requests_and_merges.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ jobs:

- run: yarn test

- name: "Check if any backend code has changed, to know if we should run backend tests"
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
backend:
- 'backend/**'
- if: steps.changes.outputs.backend == 'true'
uses: ./.github/actions/backend-tests

##########################################################################
# GitHub Pages (mobile app and hardware wallet support)
##########################################################################
Expand Down
3 changes: 2 additions & 1 deletion backend/native/backpack-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"devDependencies": {
"@types/passport": "^1.0.12",
"@types/passport-twitter": "^1.0.37",
"eslint-config-custom": "*"
"eslint-config-custom": "*",
"vitest": "^0.29.8"
},
"scripts": {
"build": "esbuild ./src/index.js --bundle --platform=node --outfile=dist/index.js",
Expand Down
152 changes: 48 additions & 104 deletions backend/native/backpack-api/src/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,90 +56,36 @@ export const getUsers = async (
// hotfix: empty array returns all records
if (userIds.filter(Boolean).length === 0) return [];

const response = await chain("query")(
const { auth_users } = await chain("query")(
{
auth_users: [
{
where: { id: { _in: userIds } },
where: {
id: { _in: userIds },
public_keys: { is_primary: { _eq: true } },
},
},
{
id: true,
username: true,
},
],
auth_public_keys: [
{
where: { user_id: { _in: userIds } },
},
{
public_key: true,
id: true,
blockchain: true,
user_id: true,
},
],
auth_user_active_publickey_mapping: [
{
where: {
user_id: {
_in: userIds,
public_keys: [
{
where: { is_primary: { _eq: true } },
},
},
},
{
user_id: true,
public_key_id: true,
{
public_key: true,
id: true,
blockchain: true,
is_primary: true,
},
],
},
],
},
{ operationName: "getUsers" }
);

const publicKeyMapping: { [public_key_id: string]: true } = {};
const userToPublicKeyMapping: {
[user_id: string]: {
public_key: string;
id: number;
blockchain: string;
}[];
} = {};

response.auth_user_active_publickey_mapping.map((x) => {
publicKeyMapping[x.public_key_id] = true;
});

response.auth_public_keys.map((x) => {
//@ts-ignore
const userId: string = x.user_id;
if (!userToPublicKeyMapping[userId]) {
userToPublicKeyMapping[userId] = [];
}
userToPublicKeyMapping[userId].push({
public_key: x.public_key,
blockchain: x.blockchain,
id: x.id,
});
});

const transformUserResponseInput = response.auth_users.map(
(userResponse) => ({
id: userResponse.id,
username: userResponse.username,
public_keys:
userToPublicKeyMapping[userResponse.id as string].map((x) => ({
blockchain: x.blockchain,
public_key: x.public_key,
user_active_publickey_mappings: publicKeyMapping[x.id]
? [
{
user_id: userResponse.id as string,
},
]
: undefined,
})) || [],
})
);
return transformUsers(transformUserResponseInput, true);
return auth_users.map((x) => transformUser(x, true));
};

/**
Expand Down Expand Up @@ -189,34 +135,37 @@ export const getUsersByPublicKeys = async (
* Get a user by their username.
*/
export const getUserByUsername = async (username: string) => {
const response = await chain("query")(
const { auth_users } = await chain("query")(
{
auth_users: [
{
limit: 1,
where: { username: { _eq: username } },
where: {
username: { _eq: username },
public_keys: { is_primary: { _eq: true } },
},
},
{
id: true,
username: true,
public_keys: [
{},
{ where: { is_primary: { _eq: true } } },
{
blockchain: true,
id: true,
blockchain: true,
public_key: true,
user_active_publickey_mappings: [{}, { user_id: true }],
is_primary: true,
},
],
},
],
},
{ operationName: "getUserByUsername" }
);
if (!response.auth_users[0]) {
if (!auth_users[0]) {
throw new Error("user not found");
}
return transformUser(response.auth_users[0]);
return transformUser(auth_users[0], true);
};

/**
Expand All @@ -238,7 +187,7 @@ export const getUser = async (id: string, onlyActiveKeys?: boolean) => {
blockchain: true,
id: true,
public_key: true,
user_active_publickey_mappings: [{}, { user_id: true }],
is_primary: true,
},
],
},
Expand Down Expand Up @@ -272,20 +221,6 @@ export const getReferrer = async (userId: string) => {
return auth_users_by_pk?.referrer;
};

const transformUsers = (
users: {
id: unknown;
username: unknown;
public_keys: Array<{
blockchain: string;
public_key: string;
user_active_publickey_mappings?: { user_id: string }[];
}>;
}[],
onlyActiveKeys?: boolean
) => {
return users.map((x) => transformUser(x, onlyActiveKeys));
};
/**
* Utility method to format a user for responses from a raw user object.
*/
Expand All @@ -296,7 +231,7 @@ const transformUser = (
public_keys: Array<{
blockchain: string;
public_key: string;
user_active_publickey_mappings?: { user_id: string }[];
is_primary?: boolean;
}>;
},
onlyActiveKeys?: boolean
Expand All @@ -309,8 +244,7 @@ const transformUser = (
.map((k) => ({
blockchain: k.blockchain as Blockchain,
publicKey: k.public_key,
primary:
k.user_active_publickey_mappings?.length || 0 >= 1 ? true : false,
primary: Boolean(k.is_primary),
}))
.filter((x) => {
if (onlyActiveKeys && !x.primary) {
Expand Down Expand Up @@ -402,27 +336,35 @@ export async function getUsersByPrefix({
usernamePrefix: string;
uuid: string;
limit?: number;
}): Promise<{ username: string; id: string }[]> {
const response = await chain("query")(
}) {
const { auth_users } = await chain("query")(
{
auth_users: [
{
where: {
username: { _like: `${usernamePrefix}%` },
id: { _neq: uuid },
public_keys: { is_primary: { _eq: true } },
},
limit: limit || 25,
},
{
id: true,
username: true,
public_keys: [
{ where: { is_primary: { _eq: true } } },
{
blockchain: true,
public_key: true,
},
],
},
],
},
{ operationName: "getUsersByPrefix" }
);

return response.auth_users || [];
return auth_users;
}

/**
Expand Down Expand Up @@ -555,18 +497,20 @@ export const getUserByPublicKeyAndChain = async (
public_keys: {
blockchain: { _eq: blockchain },
public_key: { _eq: publicKey },
user_active_publickey_mappings: {
blockchain: { _eq: blockchain },
public_key: {
public_key: { _eq: publicKey },
},
},
is_primary: { _eq: true },
},
},
},
{
id: true,
username: true,
public_keys: [
{ where: { is_primary: { _eq: true } } },
{
blockchain: true,
public_key: true,
},
],
},
],
},
Expand Down
Loading

1 comment on commit 7d51e40

@vercel
Copy link

@vercel vercel bot commented on 7d51e40 Apr 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.