Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ingawei committed Apr 4, 2024
2 parents 6ef42c1 + fb8c4c7 commit 0b3e29b
Show file tree
Hide file tree
Showing 905 changed files with 30,401 additions and 43,953 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
cache: 'yarn'
- name: Install dependencies
run: yarn install --prefer-offline --frozen-lockfile
working-directory: ./common
- name: Run lint
run: yarn lint
- name: Run unit tests
run: yarn test
working-directory: ./common
1 change: 0 additions & 1 deletion Manifold Dev.session.sql

This file was deleted.

19 changes: 19 additions & 0 deletions backend/api/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,24 @@ module.exports = {
process.platform === 'win32' ? 'windows' : 'unix',
],
'lodash/import-scope': [2, 'member'],
'no-restricted-imports': [
'error',
{
paths: [
{
name: 'openai',
importNames: ['APIError'],
message:
'Please import APIError from `api/helpers/endpoint` instead',
},
{
name: 'openai/error',
importNames: ['APIError'],
message:
'Please import APIError from `api/helpers/endpoint` instead',
},
],
},
],
},
}
41 changes: 32 additions & 9 deletions backend/api/deploy-api.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/bin/bash

# Uncomment this line if you don't want to install Docker locally!
# MANIFOLD_CLOUD_BUILD=1

set -e

if [ -z "$1" ]; then
echo "Usage: the first argument should be 'dev' or 'prod'"
exit 1
fi

# set to true to spin up all the resources for the first time
INITIALIZE=false

SERVICE_NAME="api"
SERVICE_GROUP="${SERVICE_NAME}-group"
REGION="us-central1"
Expand All @@ -32,13 +33,9 @@ esac
GIT_REVISION=$(git rev-parse --short HEAD)
TIMESTAMP=$(date +"%s")
IMAGE_TAG="${TIMESTAMP}-${GIT_REVISION}"
IMAGE_NAME="gcr.io/${GCLOUD_PROJECT}/${SERVICE_NAME}"
IMAGE_URL="${IMAGE_NAME}:${IMAGE_TAG}"
TEMPLATE_NAME="${SERVICE_NAME}-${IMAGE_TAG}"
GROUP_PAGE_URL="https://console.cloud.google.com/compute/instanceGroups/details/${ZONE}/${SERVICE_GROUP}?project=${GCLOUD_PROJECT}"

# steps to deploy new version to GCP:
# 1. build new docker image & upload to GCR
# 1. build new docker image & upload to Google
# 2. create a new GCP instance template with the new docker image
# 3. tell the GCP 'backend service' for the API to update to the new template
# 4. a. GCP creates a new instance with the new template
Expand All @@ -47,7 +44,33 @@ GROUP_PAGE_URL="https://console.cloud.google.com/compute/instanceGroups/details/
# d. delete the old instance

yarn build
gcloud builds submit . --tag ${IMAGE_URL} --project ${GCLOUD_PROJECT}

if [ -z "${MANIFOLD_CLOUD_BUILD}" ]; then
if ! command -v docker &> /dev/null
then
echo "Docker not found. You should install Docker for local builds. https://docs.docker.com/engine/install/"
echo
echo "After installing docker, run:"
echo " gcloud auth configure-docker us-central1-docker.pkg.dev"
echo "to authenticate Docker to push to Google Artifact Registry."
echo
echo "If you really don't want to figure out how to install Docker, you can set MANIFOLD_CLOUD_BUILD=1."
echo "Then it will do remote builds like before, at the cost of it being slow, like before."
exit 1
fi
IMAGE_NAME="us-central1-docker.pkg.dev/${GCLOUD_PROJECT}/builds/${SERVICE_NAME}"
IMAGE_URL="${IMAGE_NAME}:${IMAGE_TAG}"
docker build . --tag ${IMAGE_URL} --platform linux/amd64
docker push ${IMAGE_URL}
else
# not really any reason to do this other than if you have been too lazy to install docker
IMAGE_NAME="gcr.io/${GCLOUD_PROJECT}/${SERVICE_NAME}"
IMAGE_URL="${IMAGE_NAME}:${IMAGE_TAG}"
gcloud builds submit . --tag ${IMAGE_URL} --project ${GCLOUD_PROJECT}
fi

TEMPLATE_NAME="${SERVICE_NAME}-${IMAGE_TAG}"
GROUP_PAGE_URL="https://console.cloud.google.com/compute/instanceGroups/details/${ZONE}/${SERVICE_GROUP}?project=${GCLOUD_PROJECT}"

echo
echo "Creating new instance template ${TEMPLATE_NAME} using Docker image https://${IMAGE_URL}..."
Expand Down
9 changes: 6 additions & 3 deletions backend/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"private": true,
"scripts": {
"build": "yarn compile && yarn --cwd=../../common alias && yarn --cwd=../shared alias && yarn alias && yarn dist",
"dev": "nodemon -r tsconfig-paths/register --watch src -e ts --watch ../../common/src --watch ../shared/src --exec \"yarn build && node lib/serve.js | node lib/pretty-gcp-logs.js\"",
"debug": "nodemon -r tsconfig-paths/register --watch src -e ts --watch ../../common/src --watch ../shared/src --exec \"yarn build && node --inspect-brk lib/serve.js | node lib/pretty-gcp-logs.js\"",
"dev": "nodemon -r tsconfig-paths/register --watch src -e ts --watch ../../common/src --watch ../shared/src --exec \"yarn build && node lib/serve.js\"",
"debug": "nodemon -r tsconfig-paths/register --watch src -e ts --watch ../../common/src --watch ../shared/src --exec \"yarn build && node --inspect-brk lib/serve.js\"",
"dist": "yarn dist:prepare && yarn dist:copy",
"dist:prepare": "rm -rf dist && mkdir -p dist/common/lib dist/backend/shared/lib dist/backend/api/lib",
"dist:copy": "cp -R ../../common/lib/* dist/common/lib && cp -R ../shared/lib/* dist/backend/shared/lib && cp -R ./lib/* dist/backend/api/lib && cp ../../yarn.lock dist && cp package.json dist",
Expand All @@ -33,6 +33,7 @@
"@tiptap/html": "2.0.0-beta.204",
"@tiptap/starter-kit": "2.0.0-beta.204",
"@tiptap/suggestion": "2.0.0-beta.204",
"colors": "1.4.0",
"cors": "2.8.5",
"dayjs": "1.11.4",
"expo-server-sdk": "3.6.0",
Expand All @@ -45,14 +46,16 @@
"marked": "4.1.1",
"openai": "4.16.1",
"pg-promise": "11.4.1",
"sharp": "0.32.6",
"string-similarity": "4.0.4",
"stripe": "8.194.0",
"twilio": "5.0.1",
"twitter-api-v2": "1.15.0",
"zod": "3.21.4"
},
"devDependencies": {
"@types/mailgun-js": "0.22.12",
"@types/marked": "4.0.7",
"colors": "1.4.0"
"@types/twilio": "3.19.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { createSupabaseClient } from 'shared/supabase/init'
import {
createSupabaseClient,
createSupabaseDirectClient,
} from 'shared/supabase/init'
import { APIError, type APIHandler } from './helpers/endpoint'
import { convertContract } from 'common/supabase/contracts'
import {
addGroupToContract,
removeGroupFromContract,
canUserAddGroupToMarket,
} from 'shared/update-group-contracts-internal'
import { MAX_GROUPS_PER_MARKET } from 'common/group'
import { revalidateContractStaticProps } from 'shared/utils'
import { DAY_MS } from 'common/util/time'
import { addContractToFeed } from 'shared/create-feed'
import { upsertGroupEmbedding } from 'shared/helpers/embeddings'

export const addOrRemoveGroupFromContract: APIHandler<
export const addOrRemoveTopicFromContract: APIHandler<
'market/:contractId/group'
> = async (props, auth) => {
const { contractId, groupId, remove } = props
Expand All @@ -25,7 +33,7 @@ export const addOrRemoveGroupFromContract: APIHandler<

const contractQuery = await db
.from('contracts')
.select()
.select('data, importance_score')
.eq('id', contractId)
.single()

Expand All @@ -41,6 +49,13 @@ export const addOrRemoveGroupFromContract: APIHandler<
throw new APIError(403, `private groups can't be tagged or untagged`)
}

if (!remove && (contract.groupLinks?.length ?? 0) > MAX_GROUPS_PER_MARKET) {
throw new APIError(
403,
`A question can only have up to ${MAX_GROUPS_PER_MARKET} topic tags.`
)
}

const canUpdate = canUserAddGroupToMarket({
userId: auth.uid,
group,
Expand All @@ -57,4 +72,31 @@ export const addOrRemoveGroupFromContract: APIHandler<
} else {
await addGroupToContract(contract, group, auth.uid)
}

const continuation = async () => {
await revalidateContractStaticProps(contract)

// Adding a contract to a group is ~similar~ to creating a new contract in that group
if (
!remove &&
contract.createdTime > Date.now() - 2 * DAY_MS &&
contract.visibility === 'public'
) {
await addContractToFeed(
contract,
['contract_in_group_you_are_in'],
'new_contract',
[contract.creatorId],
{
idempotencyKey: contract.id + '_new_contract',
}
)
}
await upsertGroupEmbedding(createSupabaseDirectClient(), groupId)
}

return {
result: { success: true },
continue: continuation,
}
}
Loading

0 comments on commit 0b3e29b

Please sign in to comment.