Compare commits
203 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69cb77b8f4 | ||
|
|
9081d23a5f | ||
|
|
78d2ff66b2 | ||
|
|
83dd0eaadc | ||
|
|
41b825c1c7 | ||
|
|
b59f588efd | ||
|
|
b3cf97d102 | ||
|
|
31eb1d92b4 | ||
| b82d3cc726 | |||
|
|
ed807ee645 | ||
|
|
91eb32ff49 | ||
|
|
2d20d60ddb | ||
|
|
3caf1a5ec1 | ||
|
|
82985e0b9a | ||
|
|
42a7fd4214 | ||
|
|
d0cc5a21de | ||
|
|
bcecf61a46 | ||
|
|
f79a0023dc | ||
|
|
c423846756 | ||
|
|
b75f5c88d7 | ||
|
|
9283227074 | ||
|
|
2fd85f045c | ||
|
|
9748062eec | ||
|
|
aaa2b059ff | ||
|
|
0b7417a788 | ||
|
|
c402000f12 | ||
|
|
2489a6f891 | ||
|
|
f977ccf9a8 | ||
|
|
599cdc3f4e | ||
|
|
e27c381405 | ||
|
|
aeebe72ae8 | ||
|
|
8410f15ebd | ||
|
|
106ae5e1c9 | ||
|
|
66741f855d | ||
|
|
36103e36b5 | ||
|
|
8736b576a7 | ||
|
|
f86ce1e835 | ||
|
|
d286207b9f | ||
|
|
73b9d9694e | ||
|
|
a707c842be | ||
|
|
8148de30c2 | ||
|
|
b21efc2d50 | ||
|
|
4c319625d7 | ||
|
|
bfff5db155 | ||
|
|
27300b2530 | ||
|
|
31faf22fd5 | ||
|
|
8c6e954fdf | ||
|
|
027aac3a92 | ||
|
|
e82cb0e10d | ||
|
|
fc16583911 | ||
|
|
d67f294925 | ||
|
|
59a0cef2ca | ||
|
|
781f8c380a | ||
|
|
2e17aee178 | ||
|
|
f0e8e20c24 | ||
|
|
a8dc0b3647 | ||
|
|
a4053e6412 | ||
|
|
bbcd4ca1d9 | ||
|
|
c47223f5e9 | ||
|
|
87724bb7b8 | ||
|
|
2568547384 | ||
|
|
2d892779bf | ||
|
|
aa453954ed | ||
|
|
5a3127dcb0 | ||
|
|
441a2aebbc | ||
|
|
5e2475848e | ||
|
|
c527403868 | ||
|
|
8867f5ba6e | ||
|
|
1512cca858 | ||
|
|
c32c26088f | ||
|
|
69abe9bee1 | ||
|
|
a649640b8c | ||
|
|
4f03591629 | ||
|
|
20cb2836f0 | ||
|
|
36353229c1 | ||
|
|
03b6b815c6 | ||
|
|
dbf3a95bbb | ||
|
|
0cf1c3b8fd | ||
|
|
45c80ca98a | ||
|
|
eb319b68f5 | ||
|
|
4d5eaccde4 | ||
|
|
5ab8647c40 | ||
|
|
4c8140d46a | ||
|
|
aeb9aedf84 | ||
|
|
c729a46a0c | ||
|
|
83653ef23b | ||
|
|
31da4b0e62 | ||
|
|
85a0fc11e5 | ||
|
|
89d229eba9 | ||
|
|
e26e5832ff | ||
|
|
3da103c010 | ||
|
|
e22a8e8d98 | ||
|
|
f6dc60eb08 | ||
|
|
6305a8b0bc | ||
|
|
b8ff61265b | ||
|
|
67a5b60ad5 | ||
|
|
3e36aef9c5 | ||
|
|
c62bfd2a4c | ||
|
|
162390008b | ||
|
|
42a5e09c94 | ||
|
|
0499287555 | ||
|
|
0d09ad1b7e | ||
|
|
d1fb9b4d06 | ||
|
|
e4b78ceec2 | ||
|
|
dd14dfe72e | ||
|
|
39cfada3e8 | ||
|
|
cca8d03988 | ||
|
|
cb7a3a08dc | ||
|
|
a3e8b89128 | ||
|
|
f87bcc43a5 | ||
|
|
0997e449bb | ||
|
|
b0fb643b6a | ||
|
|
2c43e81436 | ||
|
|
e05fdef4ad | ||
|
|
e720336768 | ||
|
|
f93c4ad317 | ||
|
|
a445762a37 | ||
|
|
242f3cf6b8 | ||
|
|
87c2c2f8b7 | ||
|
|
b09cc82544 | ||
|
|
b8c6e893ff | ||
|
|
f62c9f064d | ||
|
|
fe6c9419dd | ||
|
|
43bfdaa1e3 | ||
|
|
3ff4fa7703 | ||
|
|
771d0e72d2 | ||
|
|
8d42c8fdb2 | ||
|
|
c095fa7042 | ||
|
|
06e4465517 | ||
|
|
2f70dc9e8a | ||
|
|
4e0b5ae215 | ||
|
|
486f989494 | ||
|
|
6663e8ed1e | ||
|
|
7688730e4f | ||
|
|
8d86db882d | ||
|
|
7df947f35a | ||
|
|
f13530f043 | ||
|
|
d73b194d1f | ||
|
|
d9ce2f5883 | ||
|
|
1443a61164 | ||
|
|
58ce48b031 | ||
|
|
79653705fe | ||
|
|
7071f0c64a | ||
|
|
9e68be7225 | ||
|
|
a3d6b786e1 | ||
|
|
0b5e242ed0 | ||
|
|
859c11fb1c | ||
|
|
28f7f0f47b | ||
|
|
aab5b7f5d5 | ||
|
|
85d91fc8bf | ||
|
|
7e5c6dd370 | ||
|
|
681117d3ac | ||
|
|
a9fb4317dc | ||
|
|
c620e4bf25 | ||
| 4e4e1203ff | |||
| d50f0e058a | |||
| b0fd810563 | |||
|
|
8f165d15d2 | ||
|
|
5ae3ab33eb | ||
|
|
fc3eb1f7d9 | ||
|
|
be835c7759 | ||
|
|
e9379fd1fb | ||
|
|
c47514d2f0 | ||
|
|
eed88d03db | ||
|
|
f532d6a60f | ||
|
|
bc381e0361 | ||
|
|
c99972a95b | ||
|
|
38d3e9349c | ||
|
|
282808583b | ||
|
|
724777af01 | ||
|
|
12e1311d1f | ||
|
|
8256c22e86 | ||
|
|
5c2187c7b7 | ||
|
|
f901f203b0 | ||
|
|
b5ccd86660 | ||
|
|
6e6cc22d2b | ||
|
|
de5106e9e8 | ||
|
|
422c446039 | ||
|
|
9fd39efba4 | ||
|
|
2bba839112 | ||
|
|
21db95a8e4 | ||
|
|
90143e5e41 | ||
|
|
9413f42020 | ||
|
|
e5c6cb74d1 | ||
|
|
bb3e37ae06 | ||
|
|
302575f129 | ||
|
|
53514d68cb | ||
|
|
73fdb6e5d1 | ||
|
|
60b82f6fb4 | ||
|
|
2a37297ae1 | ||
|
|
4d3e86a4c0 | ||
|
|
6e3e178187 | ||
|
|
9edbeed056 | ||
|
|
9b05288b40 | ||
|
|
d404901583 | ||
|
|
4d743c5e67 | ||
|
|
69706053c6 | ||
|
|
8bbeaa25cf | ||
|
|
b312a8c862 | ||
|
|
7ca5f2d45a | ||
|
|
a53a26f4c0 | ||
|
|
7a2a667aaf | ||
|
|
7aa75317f5 |
883 changed files with 31841 additions and 66889 deletions
11
.gitignore
vendored
11
.gitignore
vendored
|
|
@ -1,6 +1,7 @@
|
||||||
node_modules
|
node_modules
|
||||||
.env
|
.env
|
||||||
.turbo
|
.turbo
|
||||||
|
*.tsbuildinfo
|
||||||
build/**
|
build/**
|
||||||
**/dist/**
|
**/dist/**
|
||||||
.next/**
|
.next/**
|
||||||
|
|
@ -15,12 +16,18 @@ npm-debug.log*
|
||||||
.idea/
|
.idea/
|
||||||
*.szpm
|
*.szpm
|
||||||
.env*
|
.env*
|
||||||
yarn-error.log
|
|
||||||
docker-compose.yml
|
docker-compose.yml
|
||||||
coverage
|
coverage
|
||||||
.pgpass
|
.pgpass
|
||||||
**/dist/**
|
**/dist/**
|
||||||
.metamigo.local.json
|
.bridge.local.json
|
||||||
out/
|
out/
|
||||||
signald-state/*
|
signald-state/*
|
||||||
!./signald-state/.gitkeep
|
!./signald-state/.gitkeep
|
||||||
|
baileys-state
|
||||||
|
signald-state
|
||||||
|
project.org
|
||||||
|
**/.openapi-generator/
|
||||||
|
apps/bridge-worker/scripts/*
|
||||||
|
ENVIRONMENT_VARIABLES_MIGRATION.md
|
||||||
|
local-scripts/*
|
||||||
|
|
|
||||||
124
.gitlab-ci.yml
124
.gitlab-ci.yml
|
|
@ -1,4 +1,4 @@
|
||||||
image: node:20-bullseye-slim
|
image: node:22-bookworm-slim
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- build
|
- build
|
||||||
|
|
@ -8,36 +8,44 @@ stages:
|
||||||
build-all:
|
build-all:
|
||||||
stage: build
|
stage: build
|
||||||
variables:
|
variables:
|
||||||
TURBO_TOKEN: $TURBO_TOKEN
|
TURBO_TOKEN: ${TURBO_TOKEN}
|
||||||
TURBO_TEAM: $TURBO_TEAM
|
TURBO_TEAM: ${TURBO_TEAM}
|
||||||
|
ZAMMAD_URL: ${ZAMMAD_URL}
|
||||||
|
PNPM_HOME: "/pnpm"
|
||||||
script:
|
script:
|
||||||
- npm install -g turbo
|
- export PATH="$PNPM_HOME:$PATH"
|
||||||
- npm ci
|
- corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
- pnpm add -g turbo
|
||||||
|
- pnpm install --frozen-lockfile
|
||||||
- turbo build
|
- turbo build
|
||||||
|
|
||||||
.docker-build:
|
.docker-build:
|
||||||
image: registry.gitlab.com/guardianproject-ops/docker-alpine-git:latest
|
image: registry.gitlab.com/digiresilience/link/link-stack/buildx:main
|
||||||
services:
|
services:
|
||||||
- docker:dind
|
- docker:dind
|
||||||
stage: docker-build
|
stage: docker-build
|
||||||
variables:
|
variables:
|
||||||
|
DOCKER_HOST: tcp://docker:2375
|
||||||
|
DOCKER_TLS_CERTDIR: ""
|
||||||
DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
|
DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
|
||||||
DOCKER_CONTEXT: .
|
BUILD_CONTEXT: .
|
||||||
only:
|
only:
|
||||||
- main
|
- main
|
||||||
- develop
|
- develop
|
||||||
- tags
|
- tags
|
||||||
script:
|
script:
|
||||||
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
||||||
- DOCKER_BUILDKIT=1 docker build --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${DOCKER_CONTEXT}
|
- DOCKER_BUILDKIT=1 docker build --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${BUILD_CONTEXT}
|
||||||
- docker push ${DOCKER_NS}:${DOCKER_TAG}
|
- docker push ${DOCKER_NS}:${DOCKER_TAG}
|
||||||
|
|
||||||
.docker-release:
|
.docker-release:
|
||||||
image: registry.gitlab.com/guardianproject-ops/docker-alpine-git:latest
|
image: registry.gitlab.com/digiresilience/link/link-stack/buildx:main
|
||||||
services:
|
services:
|
||||||
- docker:dind
|
- docker:dind
|
||||||
stage: docker-release
|
stage: docker-release
|
||||||
variables:
|
variables:
|
||||||
|
DOCKER_HOST: tcp://docker:2375
|
||||||
|
DOCKER_TLS_CERTDIR: ""
|
||||||
DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
|
DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
|
||||||
DOCKER_TAG_NEW: ${CI_COMMIT_REF_NAME}
|
DOCKER_TAG_NEW: ${CI_COMMIT_REF_NAME}
|
||||||
only:
|
only:
|
||||||
|
|
@ -50,6 +58,17 @@ build-all:
|
||||||
- docker tag ${DOCKER_NS}:${DOCKER_TAG} ${DOCKER_NS}:${DOCKER_TAG_NEW}
|
- docker tag ${DOCKER_NS}:${DOCKER_TAG} ${DOCKER_NS}:${DOCKER_TAG_NEW}
|
||||||
- docker push ${DOCKER_NS}:${DOCKER_TAG_NEW}
|
- docker push ${DOCKER_NS}:${DOCKER_TAG_NEW}
|
||||||
|
|
||||||
|
buildx-docker-build:
|
||||||
|
extends: .docker-build
|
||||||
|
variables:
|
||||||
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/buildx
|
||||||
|
DOCKERFILE_PATH: ./docker/buildx/Dockerfile
|
||||||
|
|
||||||
|
buildx-docker-release:
|
||||||
|
extends: .docker-release
|
||||||
|
variables:
|
||||||
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/buildx
|
||||||
|
|
||||||
link-docker-build:
|
link-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
variables:
|
variables:
|
||||||
|
|
@ -61,49 +80,49 @@ link-docker-release:
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/link
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/link
|
||||||
|
|
||||||
leafcutter-docker-build:
|
bridge-frontend-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/leafcutter
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-frontend
|
||||||
DOCKERFILE_PATH: ./apps/leafcutter/Dockerfile
|
DOCKERFILE_PATH: ./apps/bridge-frontend/Dockerfile
|
||||||
|
|
||||||
leafcutter-docker-release:
|
bridge-frontend-docker-release:
|
||||||
extends: .docker-release
|
extends: .docker-release
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/leafcutter
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-frontend
|
||||||
|
|
||||||
metamigo-docker-build:
|
bridge-worker-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/metamigo
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-worker
|
||||||
DOCKERFILE_PATH: ./apps/metamigo-cli/Dockerfile
|
DOCKERFILE_PATH: ./apps/bridge-worker/Dockerfile
|
||||||
|
|
||||||
metamigo-docker-release:
|
bridge-worker-docker-release:
|
||||||
extends: .docker-release
|
extends: .docker-release
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/metamigo
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-worker
|
||||||
|
|
||||||
elasticsearch-docker-build:
|
bridge-whatsapp-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/elasticsearch
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-whatsapp
|
||||||
DOCKERFILE_PATH: ./docker/elasticsearch/Dockerfile
|
DOCKERFILE_PATH: ./apps/bridge-whatsapp/Dockerfile
|
||||||
|
|
||||||
elasticsearch-docker-release:
|
bridge-whatsapp-docker-release:
|
||||||
extends: .docker-release
|
extends: .docker-release
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/elasticsearch
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-whatsapp
|
||||||
|
|
||||||
label-studio-docker-build:
|
signal-cli-rest-api-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/label-studio
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/signal-cli-rest-api
|
||||||
DOCKERFILE_PATH: ./docker/label-studio/Dockerfile
|
DOCKERFILE_PATH: ./docker/signal-cli-rest-api/Dockerfile
|
||||||
|
|
||||||
label-studio-docker-release:
|
signal-cli-rest-api-docker-release:
|
||||||
extends: .docker-release
|
extends: .docker-release
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/label-studio
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/signal-cli-rest-api
|
||||||
|
|
||||||
memcached-docker-build:
|
memcached-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
|
|
@ -171,25 +190,48 @@ redis-docker-release:
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/redis
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/redis
|
||||||
|
|
||||||
signald-docker-build:
|
|
||||||
extends: .docker-build
|
|
||||||
variables:
|
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/signald
|
|
||||||
DOCKERFILE_PATH: ./docker/signald/Dockerfile
|
|
||||||
|
|
||||||
signald-docker-release:
|
|
||||||
extends: .docker-release
|
|
||||||
variables:
|
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/signald
|
|
||||||
|
|
||||||
zammad-docker-build:
|
zammad-docker-build:
|
||||||
extends: .docker-build
|
extends: .docker-build
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad
|
||||||
DOCKERFILE_PATH: ./docker/zammad/Dockerfile
|
DOCKERFILE_PATH: ./docker/zammad/Dockerfile
|
||||||
DOCKER_CONTEXT: ./docker/zammad
|
BUILD_CONTEXT: ./docker/zammad
|
||||||
|
PNPM_HOME: "/pnpm"
|
||||||
|
before_script:
|
||||||
|
- export PATH="$PNPM_HOME:$PATH"
|
||||||
|
- corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
script:
|
||||||
|
- pnpm add -g turbo
|
||||||
|
- pnpm install --frozen-lockfile
|
||||||
|
- turbo build --force --filter @link-stack/zammad-addon-*
|
||||||
|
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
||||||
|
- DOCKER_BUILDKIT=1 docker build --build-arg EMBEDDED=true --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${BUILD_CONTEXT}
|
||||||
|
- docker push ${DOCKER_NS}:${DOCKER_TAG}
|
||||||
|
|
||||||
zammad-docker-release:
|
zammad-docker-release:
|
||||||
extends: .docker-release
|
extends: .docker-release
|
||||||
variables:
|
variables:
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad
|
||||||
|
|
||||||
|
zammad-standalone-docker-build:
|
||||||
|
extends: .docker-build
|
||||||
|
variables:
|
||||||
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad-standalone
|
||||||
|
DOCKERFILE_PATH: ./docker/zammad/Dockerfile
|
||||||
|
BUILD_CONTEXT: ./docker/zammad
|
||||||
|
PNPM_HOME: "/pnpm"
|
||||||
|
before_script:
|
||||||
|
- export PATH="$PNPM_HOME:$PATH"
|
||||||
|
- corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
script:
|
||||||
|
- pnpm add -g turbo
|
||||||
|
- pnpm install --frozen-lockfile
|
||||||
|
- turbo build --force --filter @link-stack/zammad-addon-*
|
||||||
|
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
||||||
|
- DOCKER_BUILDKIT=1 docker build --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${BUILD_CONTEXT}
|
||||||
|
- docker push ${DOCKER_NS}:${DOCKER_TAG}
|
||||||
|
|
||||||
|
zammad-standalone-docker-release:
|
||||||
|
extends: .docker-release
|
||||||
|
variables:
|
||||||
|
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad-standalone
|
||||||
|
|
|
||||||
|
|
@ -1,78 +0,0 @@
|
||||||
FROM gitpod/workspace-full
|
|
||||||
|
|
||||||
# install tools we need
|
|
||||||
RUN set -ex; \
|
|
||||||
pyenv global system; \
|
|
||||||
sudo add-apt-repository ppa:ansible/ansible; \
|
|
||||||
sudo add-apt-repository ppa:maxmind/ppa; \
|
|
||||||
curl -s https://helm.baltorepo.com/organization/signing.asc | sudo apt-key add - ; \
|
|
||||||
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash; \
|
|
||||||
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list ; \
|
|
||||||
sudo apt-get update; \
|
|
||||||
sudo apt-get -y upgrade ; \
|
|
||||||
sudo apt-get install -y \
|
|
||||||
ansible \
|
|
||||||
build-essential \
|
|
||||||
httpie \
|
|
||||||
fd-find \
|
|
||||||
ffmpeg \
|
|
||||||
geoipupdate \
|
|
||||||
gitlab-runner \
|
|
||||||
helm \
|
|
||||||
htop \
|
|
||||||
iotop \
|
|
||||||
iptraf \
|
|
||||||
jq \
|
|
||||||
kitty-terminfo \
|
|
||||||
libolm-dev \
|
|
||||||
ncdu \
|
|
||||||
postgresql \
|
|
||||||
pwgen \
|
|
||||||
python3-wheel \
|
|
||||||
ripgrep \
|
|
||||||
rsync \
|
|
||||||
scdaemon \
|
|
||||||
socat \
|
|
||||||
tmux \
|
|
||||||
unrar \
|
|
||||||
unzip \
|
|
||||||
vifm \
|
|
||||||
vim \
|
|
||||||
yamllint \
|
|
||||||
zsh \
|
|
||||||
zsh-syntax-highlighting \
|
|
||||||
; sudo rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
RUN set -ex; \
|
|
||||||
brew install \
|
|
||||||
zoxide \
|
|
||||||
fzf;
|
|
||||||
|
|
||||||
# needed for tailscale
|
|
||||||
RUN sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-nft
|
|
||||||
|
|
||||||
# install npm global packages we need
|
|
||||||
RUN set -ex; \
|
|
||||||
npm install -g \
|
|
||||||
standard-version \
|
|
||||||
turbo \
|
|
||||||
;
|
|
||||||
|
|
||||||
# make a place for all our warez
|
|
||||||
RUN sudo mkdir -p /usr/local/bin
|
|
||||||
|
|
||||||
# install AWS' kubectl
|
|
||||||
# from https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html
|
|
||||||
ARG KUBECTL_URL="https://amazon-eks.s3.us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/kubectl"
|
|
||||||
RUN set -ex; \
|
|
||||||
curl -o kubectl "${KUBECTL_URL}"; \
|
|
||||||
chmod +x kubectl; \
|
|
||||||
sudo mv kubectl /usr/local/bin
|
|
||||||
|
|
||||||
# install cloudflared
|
|
||||||
# from https://github.com/cloudflare/cloudflared/releases
|
|
||||||
ARG CLOUDFLARED_VERSION="2023.2.1"
|
|
||||||
RUN set -ex; \
|
|
||||||
wget --progress=dot:mega https://github.com/cloudflare/cloudflared/releases/download/${CLOUDFLARED_VERSION}/cloudflared-linux-amd64.deb; \
|
|
||||||
sudo dpkg -i cloudflared-linux-amd64.deb; \
|
|
||||||
cloudflared --version
|
|
||||||
63
.gitpod.yml
63
.gitpod.yml
|
|
@ -1,63 +0,0 @@
|
||||||
---
|
|
||||||
# build the docker image for our gitpod from this dockerfile
|
|
||||||
image:
|
|
||||||
file: .gitpod.dockerfile
|
|
||||||
# all init+before are run in prebuilds, and on workspace startup
|
|
||||||
tasks:
|
|
||||||
- name: npm install
|
|
||||||
init: |
|
|
||||||
npm install
|
|
||||||
# extra extensions we share
|
|
||||||
vscode:
|
|
||||||
extensions:
|
|
||||||
- redhat.vscode-yaml
|
|
||||||
- ms-azuretools.vscode-docker
|
|
||||||
- ms-kubernetes-tools.vscode-kubernetes-tools
|
|
||||||
- ms-vscode.makefile-tools
|
|
||||||
- bungcip.better-toml
|
|
||||||
- sleistner.vscode-fileutils
|
|
||||||
- esbenp.prettier-vscode
|
|
||||||
- darkriszty.markdown-table-prettify
|
|
||||||
- VisualStudioExptTeam.vscodeintellicode
|
|
||||||
|
|
||||||
ports:
|
|
||||||
- name: Zammad
|
|
||||||
port: 8001
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Leafcutter Local
|
|
||||||
port: 3001
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Leafcutter
|
|
||||||
port: 8004
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Link
|
|
||||||
port: 8003
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Link Local
|
|
||||||
port: 3000
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
|
|
||||||
- name: Metamigo
|
|
||||||
port: 8002
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Metamigo Local
|
|
||||||
port: 2999
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Metamigo API
|
|
||||||
port: 8004
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Zammad Postgres
|
|
||||||
port: 5432
|
|
||||||
onOpen: notify
|
|
||||||
|
|
||||||
- name: Metamigo Postgres
|
|
||||||
port: 5433
|
|
||||||
onOpen: notify
|
|
||||||
2
.nvmrc
2
.nvmrc
|
|
@ -1 +1 @@
|
||||||
v20.2.0
|
v22.18.0
|
||||||
|
|
|
||||||
83
Makefile
83
Makefile
|
|
@ -1,83 +0,0 @@
|
||||||
CURRENT_UID := $(shell id -u):$(shell id -g)
|
|
||||||
PACKAGE_NAME ?= $(shell jq -r '.name' package.json)
|
|
||||||
PACKAGE_VERSION?= $(shell jq -r '.version' package.json)
|
|
||||||
BUILD_DATE ?=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
||||||
DOCKER_ARGS ?=
|
|
||||||
DOCKER_NS ?= registry.gitlab.com/digiresilience/link/${PACKAGE_NAME}
|
|
||||||
DOCKER_TAG ?= test
|
|
||||||
DOCKER_BUILD := docker build ${DOCKER_ARGS} --build-arg BUILD_DATE=${BUILD_DATE}
|
|
||||||
DOCKER_BUILD_FRESH := ${DOCKER_BUILD} --pull --no-cache
|
|
||||||
DOCKER_BUILD_ARGS := --build-arg VCS_REF=${CI_COMMIT_SHORT_SHA}
|
|
||||||
DOCKER_PUSH := docker push
|
|
||||||
DOCKER_BUILD_TAG := ${DOCKER_NS}:${DOCKER_TAG}
|
|
||||||
|
|
||||||
.PHONY: .npmrc
|
|
||||||
.EXPORT_ALL_VARIABLES:
|
|
||||||
|
|
||||||
.npmrc:
|
|
||||||
ifdef CI_JOB_TOKEN
|
|
||||||
echo '@guardianproject-ops:registry=https://gitlab.com/api/v4/packages/npm/' > .npmrc
|
|
||||||
echo '@digiresilience:registry=https://gitlab.com/api/v4/packages/npm/' >> .npmrc
|
|
||||||
echo '//gitlab.com/api/v4/packages/npm/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
|
|
||||||
echo '//gitlab.com/api/v4/projects/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
|
|
||||||
echo '//gitlab.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
|
|
||||||
endif
|
|
||||||
|
|
||||||
docker/build: .npmrc
|
|
||||||
DOCKER_BUILDKIT=1 ${DOCKER_BUILD} ${DOCKER_BUILD_ARGS} -t ${DOCKER_BUILD_TAG} ${PWD}
|
|
||||||
|
|
||||||
docker/build-fresh: .npmrc
|
|
||||||
DOCKER_BUILDKIT=1 ${DOCKER_BUILD_FRESH} ${DOCKER_BUILD_ARGS} -t ${DOCKER_BUILD_TAG} ${PWD}
|
|
||||||
|
|
||||||
docker/add-tag:
|
|
||||||
docker pull ${DOCKER_NS}:${DOCKER_TAG}
|
|
||||||
docker tag ${DOCKER_NS}:${DOCKER_TAG} ${DOCKER_NS}:${DOCKER_TAG_NEW}
|
|
||||||
docker push ${DOCKER_NS}:${DOCKER_TAG_NEW}
|
|
||||||
|
|
||||||
docker/push:
|
|
||||||
${DOCKER_PUSH} ${DOCKER_BUILD_TAG}
|
|
||||||
|
|
||||||
docker/build-push: docker/build docker/push
|
|
||||||
docker/build-fresh-push: docker/build-fresh docker/push
|
|
||||||
|
|
||||||
# don't use this to generate passwords for production
|
|
||||||
generate-secrets:
|
|
||||||
ZAMMAD_DATABASE_PASSWORD=$(shell openssl rand -hex 16)
|
|
||||||
METAMIGO_DATABASE_ROOT_PASSWORD=$(shell openssl rand -hex 16)
|
|
||||||
METAMIGO_DATABASE_PASSWORD=$(shell openssl rand -hex 16)
|
|
||||||
METAMIGO_DATABASE_AUTHENTICATOR_PASSWORD=$(shell openssl rand -hex 16)
|
|
||||||
NEXTAUTH_AUDIENCE=$(shell openssl rand -hex 16)
|
|
||||||
NEXTAUTH_SECRET=$(shell openssl rand -hex 16)
|
|
||||||
|
|
||||||
generate-keys:
|
|
||||||
docker exec -i $(shell docker ps -aqf "name=metamigo-frontend") bash -c "/opt/metamigo/cli gen-jwks"
|
|
||||||
|
|
||||||
setup-signal:
|
|
||||||
mkdir -p signald
|
|
||||||
|
|
||||||
create-admin-user:
|
|
||||||
docker exec -i $(shell docker ps -aqf "name=metamigo-postgresql") bash < ./scripts/create-admin-user.sh
|
|
||||||
|
|
||||||
|
|
||||||
.env:
|
|
||||||
@test -f .env || echo "You must create .env please refer to the README" && exit 1
|
|
||||||
|
|
||||||
start: .env
|
|
||||||
CURRENT_UID=$(CURRENT_UID) docker-compose up -d
|
|
||||||
|
|
||||||
start-dev: .env
|
|
||||||
CURRENT_UID=$(CURRENT_UID) docker-compose up --build -d
|
|
||||||
|
|
||||||
restart: .env
|
|
||||||
CURRENT_UID=$(CURRENT_UID) docker restart $(shell docker ps -a -q)
|
|
||||||
|
|
||||||
stop:
|
|
||||||
CURRENT_UID=$(CURRENT_UID) docker-compose down
|
|
||||||
|
|
||||||
destroy:
|
|
||||||
docker-compose down
|
|
||||||
docker volume prune
|
|
||||||
|
|
||||||
|
|
||||||
dev-metamigo:
|
|
||||||
CURRENT_UID=$(CURRENT_UID) docker compose up -d metamigo-postgresql signald
|
|
||||||
45
README.md
45
README.md
|
|
@ -1,36 +1,23 @@
|
||||||
# Dev Setup
|
# CDR Link
|
||||||
|
|
||||||
> NOTE: When using Gitpod/Codespaces, use at least 16GB RAM
|
CDR Link is a simple & streamlined helpdesk that lets you tag, assign and respond to your tickets. It is developed by the [Center for Digital Resilience](https://digiresilience.org) and powered by [Zammad](https://zammad.org).
|
||||||
|
|
||||||
Local dev with docker-compose
|
Key differences between CDR Link and a standard Zammad installation:
|
||||||
|
|
||||||
* Create `link-stack/.env` from Bitwarden `.env for root of link-stack`
|
- In addition to the full Zammad interface, CDR Link also provides a simplified 'shell' interface that focuses on the most-commonly-used functionality.
|
||||||
* Run local dev with docker-compose:
|
- Additional channels to communicate with users, including Signal, Whatsapp & Twilio voice messaging.
|
||||||
```
|
- More stringent privacy defaults: ticket data is never sent over email and calls to third-party services are restricted.
|
||||||
git clone ...
|
|
||||||
cd link-stack
|
|
||||||
make start-dev
|
|
||||||
```
|
|
||||||
|
|
||||||
Or for local dev of a single app
|
## Developing
|
||||||
|
|
||||||
* Create `link-stack/apps/link/.env.local` from Bitwarden `.env.local for link-stack/apps/link`
|
This is a monorepo that contains CDR Link and several supporting applications and libraries. It also includes Dockerfiles to build all of the other containers required for an installation. By tagging our own versions of these dependencies, we can make sure that different versions of the supporting containers all work together and are updated in sync.
|
||||||
* Create `link-stack/apps/metamigo-frontend/.metamigo.local.json` from Bitwarden `.metamigo.local.json for link-stack/apps/metamigo/frontend`
|
|
||||||
* Build locally for development:
|
|
||||||
```
|
|
||||||
npm install
|
|
||||||
make dev-metamigo # this starts the containers
|
|
||||||
npm run migrate # this migrates the db
|
|
||||||
npm run dev:metamigo # this runs metamigo frontend and api
|
|
||||||
```
|
|
||||||
|
|
||||||
# TODO
|
We use [Turborepo](https://turbo.build) to manage development and building of the packages. To get started:
|
||||||
|
|
||||||
|
- `npm install` in the root directory
|
||||||
|
- `turbo build` to build all packages
|
||||||
|
|
||||||
|
To run a single package:
|
||||||
|
|
||||||
|
- `turbo dev --filter @link-stack/link`
|
||||||
|
|
||||||
- [ ] Delete old JWT config stuff
|
|
||||||
- [ ] Consolidate config
|
|
||||||
- [ ] Complete react-admin upgrade.. make all the metamigo-frontend stuff work
|
|
||||||
* https://marmelab.com/react-admin/Upgrade.html#no-more-prop-injection-in-page-components
|
|
||||||
- [ ] Get metamigo-worker working
|
|
||||||
- [ ] Migrate off mui/styles
|
|
||||||
* https://mui.com/material-ui/migration/v5-style-changes/
|
|
||||||
* the codemods might help us?
|
|
||||||
3
apps/bridge-frontend/.eslintrc.json
Normal file
3
apps/bridge-frontend/.eslintrc.json
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"extends": "next/core-web-vitals"
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
/node_modules
|
/node_modules
|
||||||
/.pnp
|
/.pnp
|
||||||
.pnp.js
|
.pnp.js
|
||||||
|
.yarn/install-state.gz
|
||||||
|
|
||||||
# testing
|
# testing
|
||||||
/coverage
|
/coverage
|
||||||
|
|
@ -25,16 +26,11 @@ yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
|
|
||||||
# local env files
|
# local env files
|
||||||
.env.local
|
.env*.local
|
||||||
.env.development.local
|
|
||||||
.env.test.local
|
|
||||||
.env.production.local
|
|
||||||
|
|
||||||
# vercel
|
# vercel
|
||||||
.vercel
|
.vercel
|
||||||
|
|
||||||
/storybook-static
|
# typescript
|
||||||
|
*.tsbuildinfo
|
||||||
*.tgz
|
next-env.d.ts
|
||||||
|
|
||||||
.vscode
|
|
||||||
54
apps/bridge-frontend/Dockerfile
Normal file
54
apps/bridge-frontend/Dockerfile
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
FROM node:22-bookworm-slim AS base
|
||||||
|
|
||||||
|
FROM base AS builder
|
||||||
|
ARG APP_DIR=/opt/bridge-frontend
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN mkdir -p ${APP_DIR}/
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
RUN pnpm add -g turbo
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
COPY . .
|
||||||
|
RUN turbo prune --scope=@link-stack/bridge-frontend --scope=@link-stack/bridge-migrations --docker
|
||||||
|
|
||||||
|
FROM base AS installer
|
||||||
|
ARG APP_DIR=/opt/bridge-frontend
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
COPY --from=builder ${APP_DIR}/.gitignore .gitignore
|
||||||
|
COPY --from=builder ${APP_DIR}/out/json/ .
|
||||||
|
COPY --from=builder ${APP_DIR}/out/pnpm-lock.yaml ./pnpm-lock.yaml
|
||||||
|
RUN pnpm install --frozen-lockfile
|
||||||
|
|
||||||
|
COPY --from=builder ${APP_DIR}/out/full/ .
|
||||||
|
RUN pnpm add -g turbo
|
||||||
|
RUN turbo run build --filter=@link-stack/bridge-frontend --filter=@link-stack/bridge-migrations
|
||||||
|
|
||||||
|
FROM base AS runner
|
||||||
|
ARG APP_DIR=/opt/bridge-frontend
|
||||||
|
WORKDIR ${APP_DIR}/
|
||||||
|
ARG BUILD_DATE
|
||||||
|
ARG VERSION
|
||||||
|
LABEL maintainer="Darren Clarke <darren@redaranj.com>"
|
||||||
|
LABEL org.label-schema.build-date=$BUILD_DATE
|
||||||
|
LABEL org.label-schema.version=$VERSION
|
||||||
|
ENV APP_DIR ${APP_DIR}
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
|
||||||
|
apt-get install -y --no-install-recommends \
|
||||||
|
dumb-init
|
||||||
|
RUN mkdir -p ${APP_DIR}
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
COPY --from=installer ${APP_DIR} ./
|
||||||
|
RUN chown -R node:node ${APP_DIR}/
|
||||||
|
WORKDIR ${APP_DIR}/apps/bridge-frontend/
|
||||||
|
RUN chmod +x docker-entrypoint.sh
|
||||||
|
USER node
|
||||||
|
EXPOSE 3000
|
||||||
|
ENV PORT 3000
|
||||||
|
ENV NODE_ENV production
|
||||||
|
ENTRYPOINT ["/opt/bridge-frontend/apps/bridge-frontend/docker-entrypoint.sh"]
|
||||||
133
apps/bridge-frontend/README.md
Normal file
133
apps/bridge-frontend/README.md
Normal file
|
|
@ -0,0 +1,133 @@
|
||||||
|
# Bridge Frontend
|
||||||
|
|
||||||
|
Frontend application for managing communication bridges between various messaging platforms and the CDR Link system.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Bridge Frontend provides a web interface for configuring and managing communication channels including Signal, WhatsApp, Facebook, and Voice integrations. It handles bot registration, webhook configuration, and channel settings.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Channel Management**: Configure Signal, WhatsApp, Facebook, and Voice channels
|
||||||
|
- **Bot Registration**: Register and manage bots for each communication platform
|
||||||
|
- **Webhook Configuration**: Set up webhooks for message routing
|
||||||
|
- **Settings Management**: Configure channel-specific settings and behaviors
|
||||||
|
- **User Authentication**: Secure access with NextAuth.js
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- Node.js >= 20
|
||||||
|
- npm >= 10
|
||||||
|
- PostgreSQL database
|
||||||
|
- Running bridge-worker service
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install dependencies
|
||||||
|
npm install
|
||||||
|
|
||||||
|
# Run database migrations
|
||||||
|
npm run migrate:latest
|
||||||
|
|
||||||
|
# Run development server
|
||||||
|
npm run dev
|
||||||
|
|
||||||
|
# Build for production
|
||||||
|
npm run build
|
||||||
|
|
||||||
|
# Start production server
|
||||||
|
npm run start
|
||||||
|
```
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
Required environment variables:
|
||||||
|
|
||||||
|
- `DATABASE_URL` - PostgreSQL connection string
|
||||||
|
- `DATABASE_HOST` - Database host
|
||||||
|
- `DATABASE_NAME` - Database name
|
||||||
|
- `DATABASE_USER` - Database username
|
||||||
|
- `DATABASE_PASSWORD` - Database password
|
||||||
|
- `NEXTAUTH_URL` - Application URL
|
||||||
|
- `NEXTAUTH_SECRET` - NextAuth.js secret
|
||||||
|
- `GOOGLE_CLIENT_ID` - Google OAuth client ID
|
||||||
|
- `GOOGLE_CLIENT_SECRET` - Google OAuth client secret
|
||||||
|
|
||||||
|
### Available Scripts
|
||||||
|
|
||||||
|
- `npm run dev` - Start development server
|
||||||
|
- `npm run build` - Build for production
|
||||||
|
- `npm run start` - Start production server
|
||||||
|
- `npm run lint` - Run ESLint
|
||||||
|
- `npm run migrate:latest` - Run all pending migrations
|
||||||
|
- `npm run migrate:down` - Rollback last migration
|
||||||
|
- `npm run migrate:up` - Run next migration
|
||||||
|
- `npm run migrate:make` - Create new migration
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### Database Schema
|
||||||
|
|
||||||
|
The application manages the following main entities:
|
||||||
|
|
||||||
|
- **Bots**: Communication channel bot configurations
|
||||||
|
- **Webhooks**: Webhook endpoints for external integrations
|
||||||
|
- **Settings**: Channel-specific configuration settings
|
||||||
|
- **Users**: User accounts with role-based permissions
|
||||||
|
|
||||||
|
### API Routes
|
||||||
|
|
||||||
|
- `/api/auth` - Authentication endpoints
|
||||||
|
- `/api/[service]/bots` - Bot management for each service
|
||||||
|
- `/api/[service]/webhooks` - Webhook configuration
|
||||||
|
|
||||||
|
### Page Structure
|
||||||
|
|
||||||
|
- `/` - Dashboard/home page
|
||||||
|
- `/login` - Authentication page
|
||||||
|
- `/[...segment]` - Dynamic routing for CRUD operations
|
||||||
|
- `@create` - Create new entities
|
||||||
|
- `@detail` - View entity details
|
||||||
|
- `@edit` - Edit existing entities
|
||||||
|
|
||||||
|
## Integration
|
||||||
|
|
||||||
|
### Database Access
|
||||||
|
|
||||||
|
Uses Kysely ORM for type-safe database queries:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { db } from '@link-stack/database'
|
||||||
|
|
||||||
|
const bots = await db
|
||||||
|
.selectFrom('bots')
|
||||||
|
.selectAll()
|
||||||
|
.execute()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
|
||||||
|
Integrated with NextAuth.js using database adapter:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { authOptions } from '@link-stack/auth'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Docker Support
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build image
|
||||||
|
docker build -t link-stack/bridge-frontend .
|
||||||
|
|
||||||
|
# Run with docker-compose
|
||||||
|
docker-compose -f docker/compose/bridge.yml up
|
||||||
|
```
|
||||||
|
|
||||||
|
## Related Services
|
||||||
|
|
||||||
|
- **bridge-worker**: Processes messages from configured channels
|
||||||
|
- **bridge-whatsapp**: WhatsApp-specific integration service
|
||||||
|
- **bridge-migrations**: Database schema management
|
||||||
14
apps/bridge-frontend/app/(login)/login/page.tsx
Normal file
14
apps/bridge-frontend/app/(login)/login/page.tsx
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { Metadata } from "next";
|
||||||
|
import { getSession } from "next-auth/react";
|
||||||
|
import { Login } from "@/app/_components/Login";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: "Login",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page() {
|
||||||
|
const session = await getSession();
|
||||||
|
return <Login session={session} />;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { Create } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<{ segment: string[] }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page({ params }: PageProps) {
|
||||||
|
const { segment } = await params;
|
||||||
|
const service = segment[0];
|
||||||
|
|
||||||
|
return <Create service={service} />;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { serviceConfig, Detail } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<{ segment: string[] }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page({ params }: PageProps) {
|
||||||
|
const { segment } = await params;
|
||||||
|
const service = segment[0];
|
||||||
|
const id = segment?.[1];
|
||||||
|
|
||||||
|
if (!id) return null;
|
||||||
|
|
||||||
|
const {
|
||||||
|
[service]: { table },
|
||||||
|
} = serviceConfig;
|
||||||
|
|
||||||
|
const row = await db
|
||||||
|
.selectFrom(table)
|
||||||
|
.selectAll()
|
||||||
|
.where("id", "=", id)
|
||||||
|
.executeTakeFirst();
|
||||||
|
|
||||||
|
if (!row) return null;
|
||||||
|
|
||||||
|
return <Detail service={service} row={row} />;
|
||||||
|
}
|
||||||
28
apps/bridge-frontend/app/(main)/[...segment]/@edit/page.tsx
Normal file
28
apps/bridge-frontend/app/(main)/[...segment]/@edit/page.tsx
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { serviceConfig, Edit } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<{ segment: string[] }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page({ params }: PageProps) {
|
||||||
|
const { segment } = await params;
|
||||||
|
const service = segment[0];
|
||||||
|
const id = segment?.[1];
|
||||||
|
|
||||||
|
if (!id) return null;
|
||||||
|
|
||||||
|
const {
|
||||||
|
[service]: { table },
|
||||||
|
} = serviceConfig;
|
||||||
|
|
||||||
|
const row = await db
|
||||||
|
.selectFrom(table)
|
||||||
|
.selectAll()
|
||||||
|
.where("id", "=", id)
|
||||||
|
.executeTakeFirst();
|
||||||
|
|
||||||
|
if (!row) return null;
|
||||||
|
|
||||||
|
return <Edit service={service} row={row} />;
|
||||||
|
}
|
||||||
3
apps/bridge-frontend/app/(main)/[...segment]/layout.tsx
Normal file
3
apps/bridge-frontend/app/(main)/[...segment]/layout.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { ServiceLayout } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
export default ServiceLayout;
|
||||||
23
apps/bridge-frontend/app/(main)/[...segment]/page.tsx
Normal file
23
apps/bridge-frontend/app/(main)/[...segment]/page.tsx
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { serviceConfig, List } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
type PageProps = {
|
||||||
|
params: Promise<{
|
||||||
|
segment: string[];
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page({ params }: PageProps) {
|
||||||
|
const { segment } = await params;
|
||||||
|
const service = segment[0];
|
||||||
|
|
||||||
|
if (!service) return null;
|
||||||
|
|
||||||
|
const config = serviceConfig[service];
|
||||||
|
|
||||||
|
if (!config) return null;
|
||||||
|
|
||||||
|
const rows = await db.selectFrom(config.table).selectAll().execute();
|
||||||
|
|
||||||
|
return <List service={service} rows={rows} />;
|
||||||
|
}
|
||||||
9
apps/bridge-frontend/app/(main)/layout.tsx
Normal file
9
apps/bridge-frontend/app/(main)/layout.tsx
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { InternalLayout } from "@/app/_components/InternalLayout";
|
||||||
|
|
||||||
|
export default function Layout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
return <InternalLayout>{children}</InternalLayout>;
|
||||||
|
}
|
||||||
5
apps/bridge-frontend/app/(main)/page.tsx
Normal file
5
apps/bridge-frontend/app/(main)/page.tsx
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { Home } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
return <Home />;
|
||||||
|
}
|
||||||
29
apps/bridge-frontend/app/_components/InternalLayout.tsx
Normal file
29
apps/bridge-frontend/app/_components/InternalLayout.tsx
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { FC, PropsWithChildren, useState } from "react";
|
||||||
|
import { Grid } from "@mui/material";
|
||||||
|
import { CssBaseline } from "@mui/material";
|
||||||
|
import { AppRouterCacheProvider } from "@mui/material-nextjs/v14-appRouter";
|
||||||
|
import { SessionProvider } from "next-auth/react";
|
||||||
|
import { Sidebar } from "./Sidebar";
|
||||||
|
|
||||||
|
export const InternalLayout: FC<PropsWithChildren> = ({ children }) => {
|
||||||
|
const [open, setOpen] = useState(true);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppRouterCacheProvider>
|
||||||
|
<SessionProvider>
|
||||||
|
<CssBaseline />
|
||||||
|
<Grid container direction="row">
|
||||||
|
<Sidebar open={open} setOpen={setOpen} />
|
||||||
|
<Grid
|
||||||
|
item
|
||||||
|
sx={{ ml: open ? "270px" : "70px", width: "100%", height: "100vh" }}
|
||||||
|
>
|
||||||
|
{children as any}
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</SessionProvider>
|
||||||
|
</AppRouterCacheProvider>
|
||||||
|
);
|
||||||
|
};
|
||||||
185
apps/bridge-frontend/app/_components/Login.tsx
Normal file
185
apps/bridge-frontend/app/_components/Login.tsx
Normal file
|
|
@ -0,0 +1,185 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { FC, useState } from "react";
|
||||||
|
import {
|
||||||
|
Box,
|
||||||
|
Grid,
|
||||||
|
Container,
|
||||||
|
IconButton,
|
||||||
|
Typography,
|
||||||
|
TextField,
|
||||||
|
} from "@mui/material";
|
||||||
|
import {
|
||||||
|
Apple as AppleIcon,
|
||||||
|
Google as GoogleIcon,
|
||||||
|
Key as KeyIcon,
|
||||||
|
} from "@mui/icons-material";
|
||||||
|
import { signIn } from "next-auth/react";
|
||||||
|
import Image from "next/image";
|
||||||
|
import LinkLogo from "@/app/_images/link-logo-small.png";
|
||||||
|
import { colors, fonts } from "@link-stack/ui";
|
||||||
|
import { useSearchParams } from "next/navigation";
|
||||||
|
|
||||||
|
type LoginProps = {
|
||||||
|
session: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Login: FC<LoginProps> = ({ session }) => {
|
||||||
|
const origin =
|
||||||
|
typeof window !== "undefined" && window.location.origin
|
||||||
|
? window.location.origin
|
||||||
|
: "";
|
||||||
|
const [email, setEmail] = useState("");
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
const params = useSearchParams();
|
||||||
|
const error = params.get("error");
|
||||||
|
const { darkGray, cdrLinkOrange, white } = colors;
|
||||||
|
const { poppins } = fonts;
|
||||||
|
const buttonStyles = {
|
||||||
|
borderRadius: 500,
|
||||||
|
width: "100%",
|
||||||
|
fontSize: "16px",
|
||||||
|
fontWeight: "bold",
|
||||||
|
backgroundColor: white,
|
||||||
|
"&:hover": {
|
||||||
|
color: white,
|
||||||
|
backgroundColor: cdrLinkOrange,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const fieldStyles = {
|
||||||
|
"& label.Mui-focused": {
|
||||||
|
color: cdrLinkOrange,
|
||||||
|
},
|
||||||
|
"& .MuiInput-underline:after": {
|
||||||
|
borderBottomColor: cdrLinkOrange,
|
||||||
|
},
|
||||||
|
"& .MuiFilledInput-underline:after": {
|
||||||
|
borderBottomColor: cdrLinkOrange,
|
||||||
|
},
|
||||||
|
"& .MuiOutlinedInput-root": {
|
||||||
|
"&.Mui-focused fieldset": {
|
||||||
|
borderColor: cdrLinkOrange,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box sx={{ backgroundColor: darkGray, height: "100vh" }}>
|
||||||
|
<Container maxWidth="md" sx={{ p: 10 }}>
|
||||||
|
<Grid container spacing={2} direction="column" alignItems="center">
|
||||||
|
<Grid
|
||||||
|
item
|
||||||
|
container
|
||||||
|
direction="row"
|
||||||
|
justifyContent="center"
|
||||||
|
alignItems="center"
|
||||||
|
>
|
||||||
|
<Grid item>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
width: "70px",
|
||||||
|
height: "70px",
|
||||||
|
margin: "0 auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Image
|
||||||
|
src={LinkLogo}
|
||||||
|
alt="Link logo"
|
||||||
|
width={70}
|
||||||
|
height={70}
|
||||||
|
style={{
|
||||||
|
objectFit: "cover",
|
||||||
|
filter: "grayscale(100) brightness(100)",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</Grid>
|
||||||
|
<Grid item>
|
||||||
|
<Typography
|
||||||
|
variant="h2"
|
||||||
|
sx={{
|
||||||
|
fontSize: 36,
|
||||||
|
color: "white",
|
||||||
|
fontWeight: 700,
|
||||||
|
mt: 1,
|
||||||
|
ml: 0.5,
|
||||||
|
fontFamily: poppins.style.fontFamily,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
CDR Bridge
|
||||||
|
</Typography>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item sx={{ width: "100%" }}>
|
||||||
|
{!session ? (
|
||||||
|
<Container
|
||||||
|
maxWidth="xs"
|
||||||
|
sx={{
|
||||||
|
p: 3,
|
||||||
|
mt: 3,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Grid
|
||||||
|
container
|
||||||
|
spacing={3}
|
||||||
|
direction="column"
|
||||||
|
alignItems="center"
|
||||||
|
>
|
||||||
|
{error ? (
|
||||||
|
<Grid item sx={{ width: "100%" }}>
|
||||||
|
<Box sx={{ backgroundColor: "red", p: 3 }}>
|
||||||
|
<Typography
|
||||||
|
variant="body1"
|
||||||
|
sx={{
|
||||||
|
fontSize: 18,
|
||||||
|
color: "white",
|
||||||
|
textAlign: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{`${error} error`}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
</Grid>
|
||||||
|
) : null}
|
||||||
|
<Grid item sx={{ width: "100%" }}>
|
||||||
|
<IconButton
|
||||||
|
sx={buttonStyles}
|
||||||
|
onClick={() =>
|
||||||
|
signIn("google", {
|
||||||
|
callbackUrl: `${origin}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<GoogleIcon sx={{ mr: 1 }} />
|
||||||
|
Sign in with Google
|
||||||
|
</IconButton>
|
||||||
|
</Grid>
|
||||||
|
<Grid item sx={{ width: "100%" }}>
|
||||||
|
<IconButton
|
||||||
|
aria-label="Sign in with Apple"
|
||||||
|
sx={buttonStyles}
|
||||||
|
onClick={() =>
|
||||||
|
signIn("apple", {
|
||||||
|
callbackUrl: `${window.location.origin}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<AppleIcon sx={{ mr: 1 }} />
|
||||||
|
Sign in with Apple
|
||||||
|
</IconButton>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Container>
|
||||||
|
) : null}
|
||||||
|
{session ? (
|
||||||
|
<Box component="h4">
|
||||||
|
{` ${session.user.name ?? session.user.email}.`}
|
||||||
|
</Box>
|
||||||
|
) : null}
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Container>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
};
|
||||||
399
apps/bridge-frontend/app/_components/Sidebar.tsx
Normal file
399
apps/bridge-frontend/app/_components/Sidebar.tsx
Normal file
|
|
@ -0,0 +1,399 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { FC } from "react";
|
||||||
|
import {
|
||||||
|
Box,
|
||||||
|
Grid,
|
||||||
|
Typography,
|
||||||
|
List,
|
||||||
|
ListItemButton,
|
||||||
|
ListItemIcon,
|
||||||
|
ListItemText,
|
||||||
|
ListItemSecondaryAction,
|
||||||
|
Drawer,
|
||||||
|
} from "@mui/material";
|
||||||
|
import {
|
||||||
|
ExpandCircleDown as ExpandCircleDownIcon,
|
||||||
|
AccountCircle as AccountCircleIcon,
|
||||||
|
Chat as ChatIcon,
|
||||||
|
PermPhoneMsg as PhoneIcon,
|
||||||
|
WhatsApp as WhatsAppIcon,
|
||||||
|
Facebook as FacebookIcon,
|
||||||
|
AirlineStops as AirlineStopsIcon,
|
||||||
|
Logout as LogoutIcon,
|
||||||
|
} from "@mui/icons-material";
|
||||||
|
import { usePathname } from "next/navigation";
|
||||||
|
import Link from "next/link";
|
||||||
|
import Image from "next/image";
|
||||||
|
import { typography, fonts, Button } from "@link-stack/ui";
|
||||||
|
import LinkLogo from "@/app/_images/link-logo-small.png";
|
||||||
|
import { useSession, signOut } from "next-auth/react";
|
||||||
|
|
||||||
|
const openWidth = 270;
|
||||||
|
const closedWidth = 70;
|
||||||
|
|
||||||
|
const MenuItem = ({
|
||||||
|
name,
|
||||||
|
href,
|
||||||
|
Icon,
|
||||||
|
iconSize,
|
||||||
|
inset = false,
|
||||||
|
selected = false,
|
||||||
|
open = true,
|
||||||
|
badge,
|
||||||
|
target = "_self",
|
||||||
|
}: any) => (
|
||||||
|
<Link href={href} target={target}>
|
||||||
|
<ListItemButton
|
||||||
|
sx={{
|
||||||
|
p: 0,
|
||||||
|
mb: 1,
|
||||||
|
bl: iconSize === 0 ? "1px solid white" : "inherit",
|
||||||
|
}}
|
||||||
|
selected={selected}
|
||||||
|
>
|
||||||
|
{iconSize > 0 ? (
|
||||||
|
<ListItemIcon
|
||||||
|
sx={{
|
||||||
|
color: `white`,
|
||||||
|
minWidth: 0,
|
||||||
|
mr: 2,
|
||||||
|
textAlign: "center",
|
||||||
|
margin: open ? "0 8 0 0" : "0 auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
width: iconSize,
|
||||||
|
height: iconSize,
|
||||||
|
mr: 0.5,
|
||||||
|
mt: "-4px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Icon />
|
||||||
|
</Box>
|
||||||
|
</ListItemIcon>
|
||||||
|
) : (
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
width: 30,
|
||||||
|
height: "28px",
|
||||||
|
position: "relative",
|
||||||
|
ml: "9px",
|
||||||
|
mr: "1px",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
width: "1px",
|
||||||
|
height: "56px",
|
||||||
|
backgroundColor: "white",
|
||||||
|
position: "absolute",
|
||||||
|
left: "3px",
|
||||||
|
top: "-10px",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
width: "42px",
|
||||||
|
height: "42px",
|
||||||
|
position: "absolute",
|
||||||
|
top: "-27px",
|
||||||
|
left: "3px",
|
||||||
|
border: "1px solid #fff",
|
||||||
|
borderColor: "transparent transparent transparent #fff",
|
||||||
|
borderRadius: "60px",
|
||||||
|
rotate: "-35deg",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
{open && (
|
||||||
|
<ListItemText
|
||||||
|
inset={inset}
|
||||||
|
primary={
|
||||||
|
<Typography
|
||||||
|
variant="body1"
|
||||||
|
sx={{
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: "bold",
|
||||||
|
border: 0,
|
||||||
|
textAlign: "left",
|
||||||
|
color: "white",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{name}
|
||||||
|
</Typography>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{badge && badge > 0 ? (
|
||||||
|
<ListItemSecondaryAction>
|
||||||
|
<Typography
|
||||||
|
color="textSecondary"
|
||||||
|
variant="body1"
|
||||||
|
className="badge"
|
||||||
|
sx={{
|
||||||
|
backgroundColor: "#FFB620",
|
||||||
|
color: "black !important",
|
||||||
|
borderRadius: 10,
|
||||||
|
px: 1,
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: "bold",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{badge}
|
||||||
|
</Typography>
|
||||||
|
</ListItemSecondaryAction>
|
||||||
|
) : null}
|
||||||
|
</ListItemButton>
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
|
||||||
|
interface SidebarProps {
|
||||||
|
open: boolean;
|
||||||
|
setOpen: (open: boolean) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Sidebar: FC<SidebarProps> = ({ open, setOpen }) => {
|
||||||
|
const pathname = usePathname();
|
||||||
|
const { poppins } = fonts;
|
||||||
|
const { bodyLarge } = typography;
|
||||||
|
const { data: session } = useSession();
|
||||||
|
const user = session?.user;
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
signOut({ callbackUrl: "/login" });
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Drawer
|
||||||
|
sx={{ width: open ? openWidth : closedWidth, flexShrink: 0 }}
|
||||||
|
variant="permanent"
|
||||||
|
anchor="left"
|
||||||
|
open={open}
|
||||||
|
PaperProps={{
|
||||||
|
sx: {
|
||||||
|
width: open ? openWidth : closedWidth,
|
||||||
|
border: 0,
|
||||||
|
overflow: "visible",
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
position: "absolute",
|
||||||
|
top: 24,
|
||||||
|
right: open ? -8 : -16,
|
||||||
|
color: "#1C75FD",
|
||||||
|
rotate: open ? "90deg" : "-90deg",
|
||||||
|
}}
|
||||||
|
onClick={() => {
|
||||||
|
setOpen!(!open);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ExpandCircleDownIcon
|
||||||
|
sx={{
|
||||||
|
width: 24,
|
||||||
|
height: 24,
|
||||||
|
background: "white",
|
||||||
|
borderRadius: 500,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Grid
|
||||||
|
container
|
||||||
|
direction="column"
|
||||||
|
justifyContent="space-between"
|
||||||
|
wrap="nowrap"
|
||||||
|
spacing={0}
|
||||||
|
sx={{ backgroundColor: "#25272A", height: "100%", p: 2 }}
|
||||||
|
>
|
||||||
|
<Grid item container>
|
||||||
|
<Grid item sx={{ width: open ? "40px" : "100%" }}>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
width: "40px",
|
||||||
|
height: "40px",
|
||||||
|
margin: open ? "0" : "0 auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Image
|
||||||
|
src={LinkLogo}
|
||||||
|
alt="Link logo"
|
||||||
|
width={40}
|
||||||
|
height={40}
|
||||||
|
style={{
|
||||||
|
objectFit: "cover",
|
||||||
|
filter: "grayscale(100) brightness(100)",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
.
|
||||||
|
</Grid>
|
||||||
|
{open && (
|
||||||
|
<Grid item>
|
||||||
|
<Typography
|
||||||
|
variant="h2"
|
||||||
|
sx={{
|
||||||
|
fontSize: 26,
|
||||||
|
color: "white",
|
||||||
|
fontWeight: 700,
|
||||||
|
mt: 1,
|
||||||
|
ml: 0.5,
|
||||||
|
fontFamily: poppins.style.fontFamily,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
CDR Bridge
|
||||||
|
</Typography>
|
||||||
|
</Grid>
|
||||||
|
)}
|
||||||
|
</Grid>
|
||||||
|
<Grid item>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
height: "0.5px",
|
||||||
|
width: "100%",
|
||||||
|
backgroundColor: "#666",
|
||||||
|
mb: 1,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
<Grid
|
||||||
|
item
|
||||||
|
container
|
||||||
|
direction="column"
|
||||||
|
sx={{
|
||||||
|
mt: "6px",
|
||||||
|
overflow: "scroll",
|
||||||
|
scrollbarWidth: "none",
|
||||||
|
msOverflowStyle: "none",
|
||||||
|
"&::-webkit-scrollbar": { display: "none" },
|
||||||
|
}}
|
||||||
|
flexGrow={1}
|
||||||
|
>
|
||||||
|
<List
|
||||||
|
component="nav"
|
||||||
|
sx={{
|
||||||
|
a: {
|
||||||
|
textDecoration: "none",
|
||||||
|
|
||||||
|
".MuiListItemButton-root": {
|
||||||
|
p: 1,
|
||||||
|
borderRadius: 2,
|
||||||
|
"&:hover": {
|
||||||
|
background: "#555",
|
||||||
|
},
|
||||||
|
".MuiTypography-root": {
|
||||||
|
p: {
|
||||||
|
color: "#999 !important",
|
||||||
|
fontSize: 16,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
".badge": {
|
||||||
|
p: { fontSize: 12, color: "black !important" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
".Mui-selected": {
|
||||||
|
background: "#444",
|
||||||
|
color: "#fff !important",
|
||||||
|
".MuiTypography-root": {
|
||||||
|
p: {
|
||||||
|
color: "#fff !important",
|
||||||
|
fontSize: 16,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
".badge": {
|
||||||
|
p: { fontSize: 12, color: "black !important" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<MenuItem
|
||||||
|
name="WhatsApp"
|
||||||
|
href="/whatsapp"
|
||||||
|
selected={pathname.endsWith("/whatsapp")}
|
||||||
|
Icon={WhatsAppIcon}
|
||||||
|
iconSize={20}
|
||||||
|
/>
|
||||||
|
<MenuItem
|
||||||
|
name="Signal"
|
||||||
|
href="/signal"
|
||||||
|
selected={pathname.startsWith("/signal")}
|
||||||
|
Icon={ChatIcon}
|
||||||
|
iconSize={20}
|
||||||
|
/>
|
||||||
|
<MenuItem
|
||||||
|
name="Facebook"
|
||||||
|
href="/facebook"
|
||||||
|
selected={pathname.startsWith("/facebook")}
|
||||||
|
Icon={FacebookIcon}
|
||||||
|
iconSize={20}
|
||||||
|
/>
|
||||||
|
<MenuItem
|
||||||
|
name="Voice"
|
||||||
|
href="/voice"
|
||||||
|
selected={pathname.startsWith("/voice")}
|
||||||
|
Icon={PhoneIcon}
|
||||||
|
iconSize={20}
|
||||||
|
/>
|
||||||
|
<MenuItem
|
||||||
|
name="Webhooks"
|
||||||
|
href="/webhooks"
|
||||||
|
selected={pathname.startsWith("/webhooks")}
|
||||||
|
Icon={AirlineStopsIcon}
|
||||||
|
iconSize={20}
|
||||||
|
/>
|
||||||
|
<MenuItem
|
||||||
|
name="Users"
|
||||||
|
href="/users"
|
||||||
|
selected={pathname.startsWith("/users")}
|
||||||
|
Icon={AccountCircleIcon}
|
||||||
|
iconSize={20}
|
||||||
|
/>
|
||||||
|
</List>
|
||||||
|
</Grid>
|
||||||
|
<Grid
|
||||||
|
item
|
||||||
|
container
|
||||||
|
direction="row"
|
||||||
|
alignItems="center"
|
||||||
|
spacing={1}
|
||||||
|
sx={{
|
||||||
|
borderTop: "1px solid #ffffff33",
|
||||||
|
pt: 0.5,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{user?.image && (
|
||||||
|
<Grid item>
|
||||||
|
<Box sx={{ width: 20, height: 20 }}>
|
||||||
|
<Image
|
||||||
|
src={user?.image ?? ""}
|
||||||
|
alt="Profile image"
|
||||||
|
width={20}
|
||||||
|
height={20}
|
||||||
|
unoptimized
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</Grid>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Grid item>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
...bodyLarge,
|
||||||
|
color: "white",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{user?.email}
|
||||||
|
</Box>
|
||||||
|
</Grid>
|
||||||
|
<Grid item>
|
||||||
|
<Button text="Logout" kind="secondary" onClick={logout} />
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Drawer>
|
||||||
|
);
|
||||||
|
};
|
||||||
BIN
apps/bridge-frontend/app/_images/link-logo-small.png
Normal file
BIN
apps/bridge-frontend/app/_images/link-logo-small.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.6 KiB |
13
apps/bridge-frontend/app/_lib/authentication.ts
Normal file
13
apps/bridge-frontend/app/_lib/authentication.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import GoogleProvider from "next-auth/providers/google";
|
||||||
|
|
||||||
|
export const authOptions = {
|
||||||
|
providers: [
|
||||||
|
GoogleProvider({
|
||||||
|
clientId: process.env.GOOGLE_CLIENT_ID!,
|
||||||
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
session: {
|
||||||
|
strategy: "jwt" as any,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export { receiveMessage as POST } from "@link-stack/bridge-ui";
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export { relinkBot as POST } from "@link-stack/bridge-ui";
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export { getBot as GET } from "@link-stack/bridge-ui";
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export { sendMessage as POST } from "@link-stack/bridge-ui";
|
||||||
3
apps/bridge-frontend/app/api/[service]/webhooks/route.ts
Normal file
3
apps/bridge-frontend/app/api/[service]/webhooks/route.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { handleWebhook } from "@link-stack/bridge-ui";
|
||||||
|
|
||||||
|
export { handleWebhook as GET, handleWebhook as POST };
|
||||||
9
apps/bridge-frontend/app/api/auth/[...nextauth]/route.ts
Normal file
9
apps/bridge-frontend/app/api/auth/[...nextauth]/route.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import NextAuth from "next-auth";
|
||||||
|
import { authOptions } from "@/app/_lib/authentication";
|
||||||
|
|
||||||
|
// Force this route to be dynamic (not statically generated at build time)
|
||||||
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
||||||
|
const handler = NextAuth(authOptions);
|
||||||
|
|
||||||
|
export { handler as GET, handler as POST };
|
||||||
23
apps/bridge-frontend/app/layout.tsx
Normal file
23
apps/bridge-frontend/app/layout.tsx
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import type { Metadata } from "next";
|
||||||
|
import { LicenseInfo } from "@mui/x-license";
|
||||||
|
|
||||||
|
LicenseInfo.setLicenseKey(
|
||||||
|
"2a7dd73ee59e3e028b96b0d2adee1ad8Tz0xMTMwOTUsRT0xNzc5MDYyMzk5MDAwLFM9cHJvLExNPXN1YnNjcmlwdGlvbixQVj1pbml0aWFsLEtWPTI=",
|
||||||
|
);
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: "CDR Bridge",
|
||||||
|
description: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function RootLayout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<html lang="en">
|
||||||
|
<body>{children}</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
}
|
||||||
7
apps/bridge-frontend/docker-entrypoint.sh
Normal file
7
apps/bridge-frontend/docker-entrypoint.sh
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
echo "running migrations"
|
||||||
|
(cd ../bridge-migrations/ && pnpm run migrate:up:all)
|
||||||
|
echo "starting bridge-frontend"
|
||||||
|
exec dumb-init pnpm run start
|
||||||
82
apps/bridge-frontend/middleware.ts
Normal file
82
apps/bridge-frontend/middleware.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
import { withAuth } from "next-auth/middleware";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export default withAuth(
|
||||||
|
function middleware(req) {
|
||||||
|
const isDev = process.env.NODE_ENV === "development";
|
||||||
|
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
|
||||||
|
|
||||||
|
// Allow digiresilience.org for embedding documentation
|
||||||
|
const frameSrcDirective = `frame-src 'self' https://digiresilience.org;`;
|
||||||
|
|
||||||
|
const cspHeader = `
|
||||||
|
default-src 'self';
|
||||||
|
${frameSrcDirective}
|
||||||
|
connect-src 'self';
|
||||||
|
script-src 'self' 'nonce-${nonce}' 'strict-dynamic' ${isDev ? "'unsafe-eval'" : ""};
|
||||||
|
style-src 'self' 'unsafe-inline';
|
||||||
|
img-src 'self' blob: data:;
|
||||||
|
font-src 'self';
|
||||||
|
object-src 'none';
|
||||||
|
base-uri 'self';
|
||||||
|
form-action 'self';
|
||||||
|
frame-ancestors 'self';
|
||||||
|
upgrade-insecure-requests;
|
||||||
|
`;
|
||||||
|
const contentSecurityPolicyHeaderValue = cspHeader
|
||||||
|
.replace(/\s{2,}/g, " ")
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
const requestHeaders = new Headers(req.headers);
|
||||||
|
requestHeaders.set("x-nonce", nonce);
|
||||||
|
requestHeaders.set(
|
||||||
|
"Content-Security-Policy",
|
||||||
|
contentSecurityPolicyHeaderValue,
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = NextResponse.next({
|
||||||
|
request: {
|
||||||
|
headers: requestHeaders,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
response.headers.set(
|
||||||
|
"Content-Security-Policy",
|
||||||
|
contentSecurityPolicyHeaderValue,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Additional security headers
|
||||||
|
response.headers.set("X-Frame-Options", "SAMEORIGIN");
|
||||||
|
response.headers.set("X-Content-Type-Options", "nosniff");
|
||||||
|
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
|
||||||
|
response.headers.set("X-XSS-Protection", "1; mode=block");
|
||||||
|
response.headers.set(
|
||||||
|
"Permissions-Policy",
|
||||||
|
"camera=(), microphone=(), geolocation=()"
|
||||||
|
);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pages: {
|
||||||
|
signIn: `/login`,
|
||||||
|
},
|
||||||
|
callbacks: {
|
||||||
|
authorized: ({ token }) => {
|
||||||
|
if (process.env.SETUP_MODE === "true") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token?.email) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: ["/((?!ws|wss|api|_next/static|_next/image|favicon.ico).*)"],
|
||||||
|
};
|
||||||
7
apps/bridge-frontend/next.config.js
Normal file
7
apps/bridge-frontend/next.config.js
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
/** @type {import('next').NextConfig} */
|
||||||
|
const nextConfig = {
|
||||||
|
transpilePackages: ["@link-stack/ui", "@link-stack/bridge-common", "@link-stack/bridge-ui"],
|
||||||
|
poweredByHeader: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default nextConfig;
|
||||||
40
apps/bridge-frontend/package.json
Normal file
40
apps/bridge-frontend/package.json
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
{
|
||||||
|
"name": "@link-stack/bridge-frontend",
|
||||||
|
"version": "3.3.5",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "next dev",
|
||||||
|
"build": "next build",
|
||||||
|
"start": "next start",
|
||||||
|
"lint": "next lint",
|
||||||
|
"migrate:up:all": "tsx database/migrate.ts up:all",
|
||||||
|
"migrate:up:one": "tsx database/migrate.ts up:one",
|
||||||
|
"migrate:down:all": "tsx database/migrate.ts down:all",
|
||||||
|
"migrate:down:one": "tsx database/migrate.ts down:one"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@auth/kysely-adapter": "^1.10.0",
|
||||||
|
"@mui/icons-material": "^6",
|
||||||
|
"@mui/material": "^6",
|
||||||
|
"@mui/material-nextjs": "^6",
|
||||||
|
"@mui/x-license": "^7",
|
||||||
|
"@link-stack/bridge-common": "workspace:*",
|
||||||
|
"@link-stack/bridge-ui": "workspace:*",
|
||||||
|
"next": "15.5.9",
|
||||||
|
"next-auth": "^4.24.11",
|
||||||
|
"react": "19.2.0",
|
||||||
|
"react-dom": "19.2.0",
|
||||||
|
"sharp": "^0.34.4",
|
||||||
|
"tsx": "^4.20.6",
|
||||||
|
"@link-stack/ui": "workspace:*"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@link-stack/eslint-config": "workspace:*",
|
||||||
|
"@link-stack/typescript-config": "workspace:*",
|
||||||
|
"@types/node": "^24",
|
||||||
|
"@types/pg": "^8.15.5",
|
||||||
|
"@types/react": "^19",
|
||||||
|
"@types/react-dom": "^19",
|
||||||
|
"typescript": "^5"
|
||||||
|
}
|
||||||
|
}
|
||||||
2
apps/bridge-frontend/public/robots.txt
Normal file
2
apps/bridge-frontend/public/robots.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
User-agent: *
|
||||||
|
Disallow: /
|
||||||
41
apps/bridge-frontend/tsconfig.json
Normal file
41
apps/bridge-frontend/tsconfig.json
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"lib": [
|
||||||
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"esnext"
|
||||||
|
],
|
||||||
|
"allowJs": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"strict": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"jsx": "preserve",
|
||||||
|
"incremental": true,
|
||||||
|
"paths": {
|
||||||
|
"@/*": [
|
||||||
|
"./*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"plugins": [
|
||||||
|
{
|
||||||
|
"name": "next"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"target": "ES2017"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"next-env.d.ts",
|
||||||
|
"**/*.ts",
|
||||||
|
"**/*.tsx",
|
||||||
|
".next/types/**/*.ts"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
|
}
|
||||||
158
apps/bridge-migrations/README.md
Normal file
158
apps/bridge-migrations/README.md
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
# Bridge Migrations
|
||||||
|
|
||||||
|
Database migration management for the CDR Link bridge system.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Bridge Migrations handles database schema versioning and migrations for all bridge-related tables using Kysely migration framework. It manages the database structure for authentication, messaging channels, webhooks, and settings.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Schema Versioning**: Track and apply database schema changes
|
||||||
|
- **Up/Down Migrations**: Support for rolling forward and backward
|
||||||
|
- **Type-Safe Migrations**: TypeScript-based migration files
|
||||||
|
- **Migration History**: Track applied migrations in the database
|
||||||
|
- **Multiple Migration Strategies**: Run all, run one, or rollback migrations
|
||||||
|
|
||||||
|
## Migration Files
|
||||||
|
|
||||||
|
Current migrations in order:
|
||||||
|
|
||||||
|
1. **0001-add-next-auth.ts** - NextAuth.js authentication tables
|
||||||
|
2. **0002-add-signal.ts** - Signal messenger integration
|
||||||
|
3. **0003-add-whatsapp.ts** - WhatsApp integration
|
||||||
|
4. **0004-add-voice.ts** - Voice/Twilio integration
|
||||||
|
5. **0005-add-facebook.ts** - Facebook Messenger integration
|
||||||
|
6. **0006-add-webhooks.ts** - Webhook configuration
|
||||||
|
7. **0007-add-settings.ts** - Application settings
|
||||||
|
8. **0008-add-user-role.ts** - User role management
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- Node.js >= 20
|
||||||
|
- npm >= 10
|
||||||
|
- PostgreSQL database
|
||||||
|
- Database connection credentials
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install dependencies
|
||||||
|
npm install
|
||||||
|
|
||||||
|
# Run all pending migrations
|
||||||
|
npm run migrate:latest
|
||||||
|
|
||||||
|
# Check migration status
|
||||||
|
npm run migrate:list
|
||||||
|
```
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
Required environment variables:
|
||||||
|
|
||||||
|
- `DATABASE_URL` - PostgreSQL connection string
|
||||||
|
- `DATABASE_HOST` - Database host
|
||||||
|
- `DATABASE_NAME` - Database name
|
||||||
|
- `DATABASE_USER` - Database username
|
||||||
|
- `DATABASE_PASSWORD` - Database password
|
||||||
|
|
||||||
|
### Available Scripts
|
||||||
|
|
||||||
|
- `npm run migrate:latest` - Run all pending migrations
|
||||||
|
- `npm run migrate:up` - Run next pending migration
|
||||||
|
- `npm run migrate:down` - Rollback last migration
|
||||||
|
- `npm run migrate:up:all` - Run all migrations (alias)
|
||||||
|
- `npm run migrate:up:one` - Run one migration
|
||||||
|
- `npm run migrate:down:all` - Rollback all migrations
|
||||||
|
- `npm run migrate:down:one` - Rollback one migration
|
||||||
|
- `npm run migrate:list` - List migration status
|
||||||
|
- `npm run migrate:make <name>` - Create new migration file
|
||||||
|
|
||||||
|
## Creating New Migrations
|
||||||
|
|
||||||
|
To create a new migration:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run migrate:make add-new-feature
|
||||||
|
```
|
||||||
|
|
||||||
|
This creates a new timestamped migration file in the `migrations/` directory.
|
||||||
|
|
||||||
|
Example migration structure:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Kysely } from 'kysely'
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable('new_table')
|
||||||
|
.addColumn('id', 'serial', (col) => col.primaryKey())
|
||||||
|
.addColumn('name', 'varchar', (col) => col.notNull())
|
||||||
|
.addColumn('created_at', 'timestamp', (col) =>
|
||||||
|
col.defaultTo('now()').notNull()
|
||||||
|
)
|
||||||
|
.execute()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable('new_table').execute()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Database Schema
|
||||||
|
|
||||||
|
### Core Tables
|
||||||
|
|
||||||
|
- **users** - User accounts with roles
|
||||||
|
- **accounts** - OAuth account connections
|
||||||
|
- **sessions** - User sessions
|
||||||
|
- **verification_tokens** - Email verification
|
||||||
|
|
||||||
|
### Communication Tables
|
||||||
|
|
||||||
|
- **bots** - Bot configurations for each service
|
||||||
|
- **signal_messages** - Signal message history
|
||||||
|
- **whatsapp_messages** - WhatsApp message history
|
||||||
|
- **voice_messages** - Voice/call records
|
||||||
|
- **facebook_messages** - Facebook message history
|
||||||
|
|
||||||
|
### Configuration Tables
|
||||||
|
|
||||||
|
- **webhooks** - External webhook endpoints
|
||||||
|
- **settings** - Application settings
|
||||||
|
|
||||||
|
## Best Practices
|
||||||
|
|
||||||
|
1. **Test Migrations**: Always test migrations in development first
|
||||||
|
2. **Backup Database**: Create backups before running migrations in production
|
||||||
|
3. **Review Changes**: Review migration files before applying
|
||||||
|
4. **Atomic Operations**: Keep migrations focused and atomic
|
||||||
|
5. **Rollback Plan**: Ensure down() methods properly reverse changes
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Common Issues
|
||||||
|
|
||||||
|
1. **Migration Failed**: Check error logs and database permissions
|
||||||
|
2. **Locked Migrations**: Check for concurrent migration processes
|
||||||
|
3. **Missing Tables**: Ensure all previous migrations have run
|
||||||
|
4. **Connection Issues**: Verify DATABASE_URL and network access
|
||||||
|
|
||||||
|
### Recovery
|
||||||
|
|
||||||
|
If migrations fail:
|
||||||
|
|
||||||
|
1. Check migration history table
|
||||||
|
2. Manually verify database state
|
||||||
|
3. Run specific migrations as needed
|
||||||
|
4. Use rollback if necessary
|
||||||
|
|
||||||
|
## Integration
|
||||||
|
|
||||||
|
Migrations are used by:
|
||||||
|
- **bridge-frontend** - Requires migrated schema
|
||||||
|
- **bridge-worker** - Depends on message tables
|
||||||
|
- **bridge-whatsapp** - Uses bot configuration tables
|
||||||
96
apps/bridge-migrations/migrate.ts
Normal file
96
apps/bridge-migrations/migrate.ts
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
import * as path from "path";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
import { promises as fs } from "fs";
|
||||||
|
import {
|
||||||
|
Kysely,
|
||||||
|
Migrator,
|
||||||
|
MigrationResult,
|
||||||
|
FileMigrationProvider,
|
||||||
|
PostgresDialect,
|
||||||
|
CamelCasePlugin,
|
||||||
|
} from "kysely";
|
||||||
|
import pkg from "pg";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('bridge-migrations-migrate');
|
||||||
|
const { Pool } = pkg;
|
||||||
|
import * as dotenv from "dotenv";
|
||||||
|
|
||||||
|
interface Database {}
|
||||||
|
|
||||||
|
export const migrate = async (arg: string) => {
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
dotenv.config({ path: path.join(__dirname, "../.env.local") });
|
||||||
|
}
|
||||||
|
const db = new Kysely<Database>({
|
||||||
|
dialect: new PostgresDialect({
|
||||||
|
pool: new Pool({
|
||||||
|
host: process.env.DATABASE_HOST,
|
||||||
|
database: process.env.DATABASE_NAME,
|
||||||
|
port: parseInt(process.env.DATABASE_PORT!),
|
||||||
|
user: process.env.DATABASE_USER,
|
||||||
|
password: process.env.DATABASE_PASSWORD,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
plugins: [new CamelCasePlugin()],
|
||||||
|
});
|
||||||
|
const migrator = new Migrator({
|
||||||
|
db,
|
||||||
|
provider: new FileMigrationProvider({
|
||||||
|
fs,
|
||||||
|
path,
|
||||||
|
migrationFolder: path.join(__dirname, "migrations"),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
let error: any = null;
|
||||||
|
let results: MigrationResult[] = [];
|
||||||
|
|
||||||
|
if (arg === "up:all") {
|
||||||
|
const out = await migrator.migrateToLatest();
|
||||||
|
results = out.results ?? [];
|
||||||
|
error = out.error;
|
||||||
|
} else if (arg === "up:one") {
|
||||||
|
const out = await migrator.migrateUp();
|
||||||
|
results = out.results ?? [];
|
||||||
|
error = out.error;
|
||||||
|
} else if (arg === "down:all") {
|
||||||
|
const migrations = await migrator.getMigrations();
|
||||||
|
for (const _ of migrations) {
|
||||||
|
const out = await migrator.migrateDown();
|
||||||
|
if (out.results) {
|
||||||
|
results = results.concat(out.results);
|
||||||
|
error = out.error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (arg === "down:one") {
|
||||||
|
const out = await migrator.migrateDown();
|
||||||
|
if (out.results) {
|
||||||
|
results = out.results ?? [];
|
||||||
|
error = out.error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results?.forEach((it) => {
|
||||||
|
if (it.status === "Success") {
|
||||||
|
logger.info(
|
||||||
|
`Migration "${it.migrationName} ${it.direction.toLowerCase()}" was executed successfully`,
|
||||||
|
);
|
||||||
|
} else if (it.status === "Error") {
|
||||||
|
logger.error(`Failed to execute migration "${it.migrationName}"`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
logger.error("Failed to migrate");
|
||||||
|
logger.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.destroy();
|
||||||
|
};
|
||||||
|
|
||||||
|
const arg = process.argv.slice(2).pop();
|
||||||
|
migrate(arg as string);
|
||||||
72
apps/bridge-migrations/migrations/0001-add-next-auth.ts
Normal file
72
apps/bridge-migrations/migrations/0001-add-next-auth.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("User")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("name", "text")
|
||||||
|
.addColumn("email", "text", (col) => col.unique().notNull())
|
||||||
|
.addColumn("emailVerified", "timestamptz")
|
||||||
|
.addColumn("image", "text")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createTable("Account")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("userId", "uuid", (col) =>
|
||||||
|
col.references("User.id").onDelete("cascade").notNull(),
|
||||||
|
)
|
||||||
|
.addColumn("type", "text", (col) => col.notNull())
|
||||||
|
.addColumn("provider", "text", (col) => col.notNull())
|
||||||
|
.addColumn("providerAccountId", "text", (col) => col.notNull())
|
||||||
|
.addColumn("refresh_token", "text")
|
||||||
|
.addColumn("access_token", "text")
|
||||||
|
.addColumn("expires_at", "bigint")
|
||||||
|
.addColumn("token_type", "text")
|
||||||
|
.addColumn("scope", "text")
|
||||||
|
.addColumn("id_token", "text")
|
||||||
|
.addColumn("session_state", "text")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createTable("Session")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("userId", "uuid", (col) =>
|
||||||
|
col.references("User.id").onDelete("cascade").notNull(),
|
||||||
|
)
|
||||||
|
.addColumn("sessionToken", "text", (col) => col.notNull().unique())
|
||||||
|
.addColumn("expires", "timestamptz", (col) => col.notNull())
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createTable("VerificationToken")
|
||||||
|
.addColumn("identifier", "text", (col) => col.notNull())
|
||||||
|
.addColumn("token", "text", (col) => col.notNull().unique())
|
||||||
|
.addColumn("expires", "timestamptz", (col) => col.notNull())
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("Account_userId_index")
|
||||||
|
.on("Account")
|
||||||
|
.column("userId")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("Session_userId_index")
|
||||||
|
.on("Session")
|
||||||
|
.column("userId")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("Account").ifExists().execute();
|
||||||
|
await db.schema.dropTable("Session").ifExists().execute();
|
||||||
|
await db.schema.dropTable("User").ifExists().execute();
|
||||||
|
await db.schema.dropTable("VerificationToken").ifExists().execute();
|
||||||
|
}
|
||||||
33
apps/bridge-migrations/migrations/0002-add-signal.ts
Normal file
33
apps/bridge-migrations/migrations/0002-add-signal.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("SignalBot")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("phone_number", "text")
|
||||||
|
.addColumn("token", "text", (col) => col.unique().notNull())
|
||||||
|
.addColumn("user_id", "uuid")
|
||||||
|
.addColumn("name", "text")
|
||||||
|
.addColumn("description", "text")
|
||||||
|
.addColumn("qr_code", "text")
|
||||||
|
.addColumn("verified", "boolean", (col) => col.notNull().defaultTo(false))
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("SignalBotToken")
|
||||||
|
.on("SignalBot")
|
||||||
|
.column("token")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("SignalBot").ifExists().execute();
|
||||||
|
}
|
||||||
33
apps/bridge-migrations/migrations/0003-add-whatsapp.ts
Normal file
33
apps/bridge-migrations/migrations/0003-add-whatsapp.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("WhatsappBot")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("phone_number", "text")
|
||||||
|
.addColumn("token", "text", (col) => col.unique().notNull())
|
||||||
|
.addColumn("user_id", "uuid")
|
||||||
|
.addColumn("name", "text")
|
||||||
|
.addColumn("description", "text")
|
||||||
|
.addColumn("qr_code", "text")
|
||||||
|
.addColumn("verified", "boolean", (col) => col.notNull().defaultTo(false))
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("WhatsappBotToken")
|
||||||
|
.on("WhatsappBot")
|
||||||
|
.column("token")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("WhatsappBot").ifExists().execute();
|
||||||
|
}
|
||||||
77
apps/bridge-migrations/migrations/0004-add-voice.ts
Normal file
77
apps/bridge-migrations/migrations/0004-add-voice.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("VoiceProvider")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("kind", "text", (col) => col.notNull())
|
||||||
|
.addColumn("name", "text", (col) => col.notNull())
|
||||||
|
.addColumn("description", "text")
|
||||||
|
.addColumn("credentials", "jsonb", (col) => col.notNull())
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("VoiceProviderName")
|
||||||
|
.on("VoiceProvider")
|
||||||
|
.column("name")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createTable("VoiceLine")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("provider_id", "uuid", (col) =>
|
||||||
|
col.notNull().references("VoiceProvider.id").onDelete("cascade"),
|
||||||
|
)
|
||||||
|
.addColumn("provider_line_sid", "text", (col) => col.notNull())
|
||||||
|
.addColumn("number", "text", (col) => col.notNull())
|
||||||
|
.addColumn("name", "text", (col) => col.notNull())
|
||||||
|
.addColumn("description", "text")
|
||||||
|
.addColumn("language", "text", (col) => col.notNull())
|
||||||
|
.addColumn("voice", "text", (col) => col.notNull())
|
||||||
|
.addColumn("prompt_text", "text")
|
||||||
|
.addColumn("prompt_audio", "jsonb")
|
||||||
|
.addColumn("audio_prompt_enabled", "boolean", (col) =>
|
||||||
|
col.notNull().defaultTo(false),
|
||||||
|
)
|
||||||
|
.addColumn("audio_converted_at", "timestamptz")
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("VoiceLineProviderId")
|
||||||
|
.on("VoiceLine")
|
||||||
|
.column("provider_id")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("VoiceLineProviderLineSid")
|
||||||
|
.on("VoiceLine")
|
||||||
|
.column("provider_line_sid")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("VoiceLineNumber")
|
||||||
|
.on("VoiceLine")
|
||||||
|
.column("number")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("VoiceLine").ifExists().execute();
|
||||||
|
await db.schema.dropTable("VoiceProvider").ifExists().execute();
|
||||||
|
}
|
||||||
36
apps/bridge-migrations/migrations/0005-add-facebook.ts
Normal file
36
apps/bridge-migrations/migrations/0005-add-facebook.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("FacebookBot")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("name", "text")
|
||||||
|
.addColumn("description", "text")
|
||||||
|
.addColumn("token", "text")
|
||||||
|
.addColumn("page_access_token", "text")
|
||||||
|
.addColumn("app_secret", "text")
|
||||||
|
.addColumn("verify_token", "text")
|
||||||
|
.addColumn("page_id", "text")
|
||||||
|
.addColumn("app_id", "text")
|
||||||
|
.addColumn("user_id", "uuid")
|
||||||
|
.addColumn("verified", "boolean", (col) => col.notNull().defaultTo(false))
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("FacebookBotToken")
|
||||||
|
.on("FacebookBot")
|
||||||
|
.column("token")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("FacebookBot").ifExists().execute();
|
||||||
|
}
|
||||||
41
apps/bridge-migrations/migrations/0006-add-webhooks.ts
Normal file
41
apps/bridge-migrations/migrations/0006-add-webhooks.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("Webhook")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("name", "text", (col) => col.notNull())
|
||||||
|
.addColumn("description", "text")
|
||||||
|
.addColumn("backend_type", "text", (col) => col.notNull())
|
||||||
|
.addColumn("backend_id", "uuid", (col) => col.notNull())
|
||||||
|
.addColumn("endpoint_url", "text", (col) =>
|
||||||
|
col.notNull().check(sql`endpoint_url ~ '^https?://[^/]+'`),
|
||||||
|
)
|
||||||
|
.addColumn("http_method", "text", (col) =>
|
||||||
|
col
|
||||||
|
.notNull()
|
||||||
|
.defaultTo("post")
|
||||||
|
.check(sql`http_method in ('post', 'put')`),
|
||||||
|
)
|
||||||
|
.addColumn("headers", "jsonb")
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("WebhookBackendTypeBackendId")
|
||||||
|
.on("Webhook")
|
||||||
|
.column("backend_type")
|
||||||
|
.column("backend_id")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("Webhook").ifExists().execute();
|
||||||
|
}
|
||||||
28
apps/bridge-migrations/migrations/0007-add-settings.ts
Normal file
28
apps/bridge-migrations/migrations/0007-add-settings.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { Kysely, sql } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable("Setting")
|
||||||
|
.addColumn("id", "uuid", (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||||
|
)
|
||||||
|
.addColumn("name", "text")
|
||||||
|
.addColumn("value", "jsonb")
|
||||||
|
.addColumn("created_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn("updated_at", "timestamptz", (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex("SettingName")
|
||||||
|
.on("Setting")
|
||||||
|
.column("name")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable("Setting").ifExists().execute();
|
||||||
|
}
|
||||||
9
apps/bridge-migrations/migrations/0008-add-user-role.ts
Normal file
9
apps/bridge-migrations/migrations/0008-add-user-role.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { Kysely } from "kysely";
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.alterTable("User").addColumn("role", "text").execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.alterTable("User").dropColumn("role").execute();
|
||||||
|
}
|
||||||
25
apps/bridge-migrations/package.json
Normal file
25
apps/bridge-migrations/package.json
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"name": "@link-stack/bridge-migrations",
|
||||||
|
"version": "3.3.5",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"migrate:up:all": "tsx migrate.ts up:all",
|
||||||
|
"migrate:up:one": "tsx migrate.ts up:one",
|
||||||
|
"migrate:down:all": "tsx migrate.ts down:all",
|
||||||
|
"migrate:down:one": "tsx migrate.ts down:one"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@link-stack/logger": "workspace:*",
|
||||||
|
"dotenv": "^17.2.3",
|
||||||
|
"kysely": "0.27.5",
|
||||||
|
"pg": "^8.16.3",
|
||||||
|
"tsx": "^4.20.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^24",
|
||||||
|
"@types/pg": "^8.15.5",
|
||||||
|
"@link-stack/eslint-config": "workspace:*",
|
||||||
|
"@link-stack/typescript-config": "workspace:*",
|
||||||
|
"typescript": "^5"
|
||||||
|
}
|
||||||
|
}
|
||||||
48
apps/bridge-whatsapp/Dockerfile
Normal file
48
apps/bridge-whatsapp/Dockerfile
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
FROM node:22-bookworm-slim AS base
|
||||||
|
|
||||||
|
FROM base AS builder
|
||||||
|
ARG APP_DIR=/opt/bridge-whatsapp
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN mkdir -p ${APP_DIR}/
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
RUN pnpm add -g turbo
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
COPY . .
|
||||||
|
RUN turbo prune --scope=@link-stack/bridge-whatsapp --docker
|
||||||
|
|
||||||
|
FROM base AS installer
|
||||||
|
ARG APP_DIR=/opt/bridge-whatsapp
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
COPY --from=builder ${APP_DIR}/out/json/ .
|
||||||
|
COPY --from=builder ${APP_DIR}/out/full/ .
|
||||||
|
COPY --from=builder ${APP_DIR}/out/pnpm-lock.yaml ./pnpm-lock.yaml
|
||||||
|
RUN pnpm install --frozen-lockfile
|
||||||
|
RUN pnpm add -g turbo
|
||||||
|
RUN turbo run build --filter=@link-stack/bridge-whatsapp
|
||||||
|
|
||||||
|
FROM base as runner
|
||||||
|
ARG BUILD_DATE
|
||||||
|
ARG VERSION
|
||||||
|
ARG APP_DIR=/opt/bridge-whatsapp
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN mkdir -p ${APP_DIR}/
|
||||||
|
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
|
||||||
|
apt-get install -y --no-install-recommends \
|
||||||
|
dumb-init
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
COPY --from=installer ${APP_DIR} ./
|
||||||
|
RUN chown -R node:node ${APP_DIR}
|
||||||
|
WORKDIR ${APP_DIR}/apps/bridge-whatsapp/
|
||||||
|
RUN chmod +x docker-entrypoint.sh
|
||||||
|
USER node
|
||||||
|
RUN mkdir /home/node/baileys
|
||||||
|
EXPOSE 5000
|
||||||
|
ENV PORT 5000
|
||||||
|
ENV NODE_ENV production
|
||||||
|
ENTRYPOINT ["/opt/bridge-whatsapp/apps/bridge-whatsapp/docker-entrypoint.sh"]
|
||||||
172
apps/bridge-whatsapp/README.md
Normal file
172
apps/bridge-whatsapp/README.md
Normal file
|
|
@ -0,0 +1,172 @@
|
||||||
|
# Bridge WhatsApp
|
||||||
|
|
||||||
|
WhatsApp integration service for the CDR Link communication bridge system.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Bridge WhatsApp provides a REST API for sending and receiving WhatsApp messages using the Baileys library (WhatsApp Web API). It handles bot session management, message routing, and media processing for WhatsApp communication channels.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Bot Management**: Register and manage multiple WhatsApp bot sessions
|
||||||
|
- **Message Handling**: Send and receive text messages with formatting
|
||||||
|
- **Media Support**: Handle images, documents, audio, and video files
|
||||||
|
- **QR Code Authentication**: Web-based WhatsApp authentication
|
||||||
|
- **REST API**: Simple HTTP endpoints for integration
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- Node.js >= 20
|
||||||
|
- npm >= 10
|
||||||
|
- PostgreSQL database (for bot configuration)
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install dependencies
|
||||||
|
npm install
|
||||||
|
|
||||||
|
# Build TypeScript
|
||||||
|
npm run build
|
||||||
|
|
||||||
|
# Run development server
|
||||||
|
npm run dev
|
||||||
|
|
||||||
|
# Start production server
|
||||||
|
npm run start
|
||||||
|
```
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
- `PORT` - Server port (default: 5000)
|
||||||
|
- `DATABASE_URL` - PostgreSQL connection string (optional)
|
||||||
|
- Additional WhatsApp-specific configuration as needed
|
||||||
|
|
||||||
|
### Available Scripts
|
||||||
|
|
||||||
|
- `npm run build` - Compile TypeScript
|
||||||
|
- `npm run dev` - Development mode with auto-reload
|
||||||
|
- `npm run start` - Start production server
|
||||||
|
|
||||||
|
## API Endpoints
|
||||||
|
|
||||||
|
### Bot Management
|
||||||
|
|
||||||
|
- `POST /api/bots/:token` - Register/initialize a bot
|
||||||
|
- `GET /api/bots/:token` - Get bot status and QR code
|
||||||
|
|
||||||
|
### Messaging
|
||||||
|
|
||||||
|
- `POST /api/bots/:token/send` - Send a message
|
||||||
|
- `POST /api/bots/:token/receive` - Webhook for incoming messages
|
||||||
|
|
||||||
|
### Request/Response Format
|
||||||
|
|
||||||
|
#### Send Message
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"to": "1234567890@s.whatsapp.net",
|
||||||
|
"message": "Hello World",
|
||||||
|
"media": {
|
||||||
|
"url": "https://example.com/image.jpg",
|
||||||
|
"type": "image"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Receive Message Webhook
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"from": "1234567890@s.whatsapp.net",
|
||||||
|
"message": "Hello",
|
||||||
|
"timestamp": "2024-01-01T00:00:00Z",
|
||||||
|
"media": {
|
||||||
|
"url": "https://...",
|
||||||
|
"type": "image",
|
||||||
|
"mimetype": "image/jpeg"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### Server Framework
|
||||||
|
|
||||||
|
Built with Hapi.js for:
|
||||||
|
|
||||||
|
- Route validation
|
||||||
|
- Plugin architecture
|
||||||
|
- Error handling
|
||||||
|
- Request lifecycle
|
||||||
|
|
||||||
|
### WhatsApp Integration
|
||||||
|
|
||||||
|
Uses @whiskeysockets/baileys:
|
||||||
|
|
||||||
|
- WhatsApp Web protocol
|
||||||
|
- Multi-device support
|
||||||
|
- Message encryption
|
||||||
|
- Media handling
|
||||||
|
|
||||||
|
### Session Management
|
||||||
|
|
||||||
|
- File-based session storage
|
||||||
|
- Automatic reconnection
|
||||||
|
- QR code regeneration
|
||||||
|
- Session cleanup
|
||||||
|
|
||||||
|
## Media Handling
|
||||||
|
|
||||||
|
Supported media types:
|
||||||
|
|
||||||
|
- **Images**: JPEG, PNG, GIF
|
||||||
|
- **Documents**: PDF, DOC, DOCX
|
||||||
|
- **Audio**: MP3, OGG, WAV
|
||||||
|
- **Video**: MP4, AVI
|
||||||
|
|
||||||
|
Media is processed and uploaded before sending.
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
- Connection errors trigger reconnection
|
||||||
|
- Invalid sessions regenerate QR codes
|
||||||
|
- API errors return appropriate HTTP status codes
|
||||||
|
- Comprehensive logging for debugging
|
||||||
|
|
||||||
|
## Security
|
||||||
|
|
||||||
|
- Token-based bot authentication
|
||||||
|
- Message validation
|
||||||
|
- Rate limiting (configurable)
|
||||||
|
- Secure session storage
|
||||||
|
|
||||||
|
## Integration
|
||||||
|
|
||||||
|
Designed to work with:
|
||||||
|
|
||||||
|
- **bridge-worker**: Processes WhatsApp message jobs
|
||||||
|
- **bridge-frontend**: Manages bot configuration
|
||||||
|
- External webhooks for message routing
|
||||||
|
|
||||||
|
## Docker Support
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build image
|
||||||
|
docker build -t link-stack/bridge-whatsapp .
|
||||||
|
|
||||||
|
# Run container
|
||||||
|
docker run -p 5000:5000 link-stack/bridge-whatsapp
|
||||||
|
```
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
While test configuration exists (jest.config.json), tests should be implemented for:
|
||||||
|
|
||||||
|
- API endpoint validation
|
||||||
|
- Message processing logic
|
||||||
|
- Session management
|
||||||
|
- Error scenarios
|
||||||
5
apps/bridge-whatsapp/docker-entrypoint.sh
Normal file
5
apps/bridge-whatsapp/docker-entrypoint.sh
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
echo "starting bridge-whatsapp"
|
||||||
|
exec dumb-init pnpm run start
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"preset": "jest-config-link",
|
"preset": "jest-config",
|
||||||
"setupFiles": ["<rootDir>/src/setup.test.ts"]
|
"setupFiles": ["<rootDir>/src/setup.test.ts"]
|
||||||
}
|
}
|
||||||
33
apps/bridge-whatsapp/package.json
Normal file
33
apps/bridge-whatsapp/package.json
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
{
|
||||||
|
"name": "@link-stack/bridge-whatsapp",
|
||||||
|
"version": "3.3.5",
|
||||||
|
"main": "build/main/index.js",
|
||||||
|
"author": "Darren Clarke <darren@redaranj.com>",
|
||||||
|
"license": "AGPL-3.0-or-later",
|
||||||
|
"dependencies": {
|
||||||
|
"@adiwajshing/keyed-db": "0.2.4",
|
||||||
|
"@hapi/hapi": "^21.4.3",
|
||||||
|
"@hapipal/schmervice": "^3.0.0",
|
||||||
|
"@hapipal/toys": "^4.0.0",
|
||||||
|
"@link-stack/bridge-common": "workspace:*",
|
||||||
|
"@link-stack/logger": "workspace:*",
|
||||||
|
"@whiskeysockets/baileys": "6.7.21",
|
||||||
|
"hapi-pino": "^13.0.0",
|
||||||
|
"link-preview-js": "^3.1.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@link-stack/eslint-config": "workspace:*",
|
||||||
|
"@link-stack/jest-config": "workspace:*",
|
||||||
|
"@link-stack/typescript-config": "workspace:*",
|
||||||
|
"@types/long": "^5",
|
||||||
|
"@types/node": "*",
|
||||||
|
"dotenv-cli": "^10.0.0",
|
||||||
|
"tsx": "^4.20.6",
|
||||||
|
"typescript": "^5.9.3"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc -p tsconfig.json",
|
||||||
|
"dev": "dotenv -- tsx src/index.ts",
|
||||||
|
"start": "node build/main/index.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
42
apps/bridge-whatsapp/src/index.ts
Normal file
42
apps/bridge-whatsapp/src/index.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import * as Hapi from "@hapi/hapi";
|
||||||
|
import hapiPino from "hapi-pino";
|
||||||
|
import Schmervice from "@hapipal/schmervice";
|
||||||
|
import WhatsappService from "./service.js";
|
||||||
|
import {
|
||||||
|
RegisterBotRoute,
|
||||||
|
UnverifyBotRoute,
|
||||||
|
GetBotRoute,
|
||||||
|
SendMessageRoute,
|
||||||
|
ReceiveMessageRoute,
|
||||||
|
} from "./routes.js";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('bridge-whatsapp-index');
|
||||||
|
|
||||||
|
const server = Hapi.server({ port: 5000 });
|
||||||
|
|
||||||
|
const startServer = async () => {
|
||||||
|
await server.register({ plugin: hapiPino });
|
||||||
|
|
||||||
|
server.route(RegisterBotRoute);
|
||||||
|
server.route(UnverifyBotRoute);
|
||||||
|
server.route(GetBotRoute);
|
||||||
|
server.route(SendMessageRoute);
|
||||||
|
server.route(ReceiveMessageRoute);
|
||||||
|
|
||||||
|
await server.register(Schmervice);
|
||||||
|
server.registerService(WhatsappService);
|
||||||
|
|
||||||
|
await server.start();
|
||||||
|
|
||||||
|
return server;
|
||||||
|
};
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
await startServer();
|
||||||
|
};
|
||||||
|
|
||||||
|
main().catch((err) => {
|
||||||
|
logger.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
131
apps/bridge-whatsapp/src/routes.ts
Normal file
131
apps/bridge-whatsapp/src/routes.ts
Normal file
|
|
@ -0,0 +1,131 @@
|
||||||
|
import * as Hapi from "@hapi/hapi";
|
||||||
|
import Toys from "@hapipal/toys";
|
||||||
|
import WhatsappService from "./service";
|
||||||
|
|
||||||
|
const withDefaults = Toys.withRouteDefaults({
|
||||||
|
options: {
|
||||||
|
cors: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const getService = (request: Hapi.Request): WhatsappService => {
|
||||||
|
const { whatsappService } = request.services();
|
||||||
|
|
||||||
|
return whatsappService as WhatsappService;
|
||||||
|
};
|
||||||
|
|
||||||
|
interface MessageRequest {
|
||||||
|
phoneNumber: string;
|
||||||
|
message: string;
|
||||||
|
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SendMessageRoute = withDefaults({
|
||||||
|
method: "post",
|
||||||
|
path: "/api/bots/{id}/send",
|
||||||
|
options: {
|
||||||
|
description: "Send a message",
|
||||||
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||||
|
const { id } = request.params;
|
||||||
|
const { phoneNumber, message, attachments } =
|
||||||
|
request.payload as MessageRequest;
|
||||||
|
const whatsappService = getService(request);
|
||||||
|
await whatsappService.send(
|
||||||
|
id,
|
||||||
|
phoneNumber,
|
||||||
|
message as string,
|
||||||
|
attachments,
|
||||||
|
);
|
||||||
|
request.logger.info(
|
||||||
|
{
|
||||||
|
id,
|
||||||
|
attachmentCount: attachments?.length || 0,
|
||||||
|
},
|
||||||
|
"Sent a message at %s",
|
||||||
|
new Date().toISOString(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return _h
|
||||||
|
.response({
|
||||||
|
result: {
|
||||||
|
recipient: phoneNumber,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
source: id,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.code(200);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ReceiveMessageRoute = withDefaults({
|
||||||
|
method: "get",
|
||||||
|
path: "/api/bots/{id}/receive",
|
||||||
|
options: {
|
||||||
|
description: "Receive messages",
|
||||||
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||||
|
const { id } = request.params;
|
||||||
|
const whatsappService = getService(request);
|
||||||
|
const date = new Date();
|
||||||
|
const twoDaysAgo = new Date(date.getTime());
|
||||||
|
twoDaysAgo.setDate(date.getDate() - 2);
|
||||||
|
request.logger.info({ id }, "Received messages at %s", new Date().toISOString());
|
||||||
|
|
||||||
|
return whatsappService.receive(id, twoDaysAgo);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const RegisterBotRoute = withDefaults({
|
||||||
|
method: "post",
|
||||||
|
path: "/api/bots/{id}/register",
|
||||||
|
options: {
|
||||||
|
description: "Register a bot",
|
||||||
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||||
|
const { id } = request.params;
|
||||||
|
const whatsappService = getService(request);
|
||||||
|
|
||||||
|
await whatsappService.register(id);
|
||||||
|
/*
|
||||||
|
, (error: string) => {
|
||||||
|
if (error) {
|
||||||
|
return _h.response(error).code(500);
|
||||||
|
}
|
||||||
|
request.logger.info({ id }, "Register bot at %s", new Date());
|
||||||
|
|
||||||
|
return _h.response().code(200);
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
return _h.response().code(200);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const UnverifyBotRoute = withDefaults({
|
||||||
|
method: "post",
|
||||||
|
path: "/api/bots/{id}/unverify",
|
||||||
|
options: {
|
||||||
|
description: "Unverify bot",
|
||||||
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||||
|
const { id } = request.params;
|
||||||
|
const whatsappService = getService(request);
|
||||||
|
|
||||||
|
return whatsappService.unverify(id);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const GetBotRoute = withDefaults({
|
||||||
|
method: "get",
|
||||||
|
path: "/api/bots/{id}",
|
||||||
|
options: {
|
||||||
|
description: "Get bot info",
|
||||||
|
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||||
|
const { id } = request.params;
|
||||||
|
const whatsappService = getService(request);
|
||||||
|
|
||||||
|
return whatsappService.getBot(id);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
421
apps/bridge-whatsapp/src/service.ts
Normal file
421
apps/bridge-whatsapp/src/service.ts
Normal file
|
|
@ -0,0 +1,421 @@
|
||||||
|
import { Server } from "@hapi/hapi";
|
||||||
|
import { Service } from "@hapipal/schmervice";
|
||||||
|
import makeWASocket, {
|
||||||
|
DisconnectReason,
|
||||||
|
proto,
|
||||||
|
downloadContentFromMessage,
|
||||||
|
MediaType,
|
||||||
|
fetchLatestBaileysVersion,
|
||||||
|
isJidBroadcast,
|
||||||
|
isJidStatusBroadcast,
|
||||||
|
useMultiFileAuthState,
|
||||||
|
} from "@whiskeysockets/baileys";
|
||||||
|
import fs from "fs";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import {
|
||||||
|
getMaxAttachmentSize,
|
||||||
|
getMaxTotalAttachmentSize,
|
||||||
|
MAX_ATTACHMENTS,
|
||||||
|
} from "@link-stack/bridge-common";
|
||||||
|
|
||||||
|
const logger = createLogger("bridge-whatsapp-service");
|
||||||
|
|
||||||
|
export type AuthCompleteCallback = (error?: string) => void;
|
||||||
|
|
||||||
|
export default class WhatsappService extends Service {
|
||||||
|
connections: { [key: string]: any } = {};
|
||||||
|
loginConnections: { [key: string]: any } = {};
|
||||||
|
|
||||||
|
static browserDescription: [string, string, string] = ["Bridge", "Chrome", "2.0"];
|
||||||
|
|
||||||
|
constructor(server: Server, options: never) {
|
||||||
|
super(server, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
getBaseDirectory(): string {
|
||||||
|
return `/home/node/baileys`;
|
||||||
|
}
|
||||||
|
|
||||||
|
getBotDirectory(id: string): string {
|
||||||
|
// Validate that ID contains only safe characters (alphanumeric, dash, underscore)
|
||||||
|
if (!/^[a-zA-Z0-9_-]+$/.test(id)) {
|
||||||
|
throw new Error(`Invalid bot ID format: ${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent path traversal by checking for suspicious patterns
|
||||||
|
if (id.includes("..") || id.includes("/") || id.includes("\\")) {
|
||||||
|
throw new Error(`Path traversal detected in bot ID: ${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const botPath = `${this.getBaseDirectory()}/${id}`;
|
||||||
|
|
||||||
|
// Ensure the resolved path is still within the base directory
|
||||||
|
if (!botPath.startsWith(this.getBaseDirectory())) {
|
||||||
|
throw new Error(`Invalid bot path: ${botPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return botPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAuthDirectory(id: string): string {
|
||||||
|
return `${this.getBotDirectory(id)}/auth`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async initialize(): Promise<void> {
|
||||||
|
this.updateConnections();
|
||||||
|
}
|
||||||
|
|
||||||
|
async teardown(): Promise<void> {
|
||||||
|
this.resetConnections();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async sleep(ms: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async resetConnections() {
|
||||||
|
for (const connection of Object.values(this.connections)) {
|
||||||
|
try {
|
||||||
|
connection.end(null);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error({ error }, "Connection reset error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.connections = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async createConnection(
|
||||||
|
botID: string,
|
||||||
|
server: Server,
|
||||||
|
options: any,
|
||||||
|
authCompleteCallback?: any,
|
||||||
|
) {
|
||||||
|
const authDirectory = this.getAuthDirectory(botID);
|
||||||
|
const { state, saveCreds } = await useMultiFileAuthState(authDirectory);
|
||||||
|
const msgRetryCounterMap: any = {};
|
||||||
|
const socket = makeWASocket({
|
||||||
|
...options,
|
||||||
|
auth: state,
|
||||||
|
generateHighQualityLinkPreview: false,
|
||||||
|
msgRetryCounterMap,
|
||||||
|
shouldIgnoreJid: (jid) => isJidBroadcast(jid) || isJidStatusBroadcast(jid),
|
||||||
|
});
|
||||||
|
let pause = 5000;
|
||||||
|
|
||||||
|
socket.ev.process(async (events) => {
|
||||||
|
if (events["connection.update"]) {
|
||||||
|
const update = events["connection.update"];
|
||||||
|
const { connection: connectionState, lastDisconnect, qr, isNewLogin } = update;
|
||||||
|
if (qr) {
|
||||||
|
logger.info("got qr code");
|
||||||
|
const botDirectory = this.getBotDirectory(botID);
|
||||||
|
const qrPath = `${botDirectory}/qr.txt`;
|
||||||
|
fs.writeFileSync(qrPath, qr, "utf8");
|
||||||
|
} else if (isNewLogin) {
|
||||||
|
logger.info("got new login");
|
||||||
|
const botDirectory = this.getBotDirectory(botID);
|
||||||
|
const verifiedFile = `${botDirectory}/verified`;
|
||||||
|
fs.writeFileSync(verifiedFile, "");
|
||||||
|
} else if (connectionState === "open") {
|
||||||
|
logger.info("opened connection");
|
||||||
|
} else if (connectionState === "close") {
|
||||||
|
logger.info({ lastDisconnect }, "connection closed");
|
||||||
|
const disconnectStatusCode = (lastDisconnect?.error as any)?.output?.statusCode;
|
||||||
|
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
||||||
|
logger.info("reconnecting after got new login");
|
||||||
|
await this.createConnection(botID, server, options);
|
||||||
|
authCompleteCallback?.();
|
||||||
|
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
||||||
|
logger.info("reconnecting");
|
||||||
|
await this.sleep(pause);
|
||||||
|
pause *= 2;
|
||||||
|
this.createConnection(botID, server, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (events["creds.update"]) {
|
||||||
|
logger.info("creds update");
|
||||||
|
await saveCreds();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (events["messages.upsert"]) {
|
||||||
|
logger.info("messages upsert");
|
||||||
|
const upsert = events["messages.upsert"];
|
||||||
|
const { messages } = upsert;
|
||||||
|
if (messages) {
|
||||||
|
await this.queueUnreadMessages(botID, messages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.connections[botID] = { socket, msgRetryCounterMap };
|
||||||
|
}
|
||||||
|
|
||||||
|
private async updateConnections() {
|
||||||
|
this.resetConnections();
|
||||||
|
|
||||||
|
const baseDirectory = this.getBaseDirectory();
|
||||||
|
const botIDs = fs.readdirSync(baseDirectory);
|
||||||
|
|
||||||
|
for await (const botID of botIDs) {
|
||||||
|
const directory = this.getBotDirectory(botID);
|
||||||
|
const verifiedFile = `${directory}/verified`;
|
||||||
|
if (fs.existsSync(verifiedFile)) {
|
||||||
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
||||||
|
logger.info({ version: version.join("."), isLatest }, "using WA version");
|
||||||
|
|
||||||
|
await this.createConnection(botID, this.server, {
|
||||||
|
browser: WhatsappService.browserDescription,
|
||||||
|
printQRInTerminal: true,
|
||||||
|
version,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async queueMessage(botID: string, webMessageInfo: proto.IWebMessageInfo) {
|
||||||
|
const {
|
||||||
|
key: { id, fromMe, remoteJid },
|
||||||
|
message,
|
||||||
|
messageTimestamp,
|
||||||
|
} = webMessageInfo;
|
||||||
|
logger.info("Message type debug");
|
||||||
|
for (const key in message) {
|
||||||
|
logger.info(
|
||||||
|
{ key, exists: !!message[key as keyof proto.IMessage] },
|
||||||
|
"Message field",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const isValidMessage = message && remoteJid !== "status@broadcast" && !fromMe;
|
||||||
|
if (isValidMessage) {
|
||||||
|
const { audioMessage, documentMessage, imageMessage, videoMessage } = message;
|
||||||
|
const isMediaMessage =
|
||||||
|
audioMessage || documentMessage || imageMessage || videoMessage;
|
||||||
|
|
||||||
|
const messageContent = Object.values(message)[0];
|
||||||
|
let messageType: MediaType;
|
||||||
|
let attachment: string | null | undefined;
|
||||||
|
let filename: string | null | undefined;
|
||||||
|
let mimeType: string | null | undefined;
|
||||||
|
if (isMediaMessage) {
|
||||||
|
if (audioMessage) {
|
||||||
|
messageType = "audio";
|
||||||
|
const extension = audioMessage.mimetype?.split("/").pop() || "audio";
|
||||||
|
filename = `${id}.${extension}`;
|
||||||
|
mimeType = audioMessage.mimetype;
|
||||||
|
} else if (documentMessage) {
|
||||||
|
messageType = "document";
|
||||||
|
filename = documentMessage.fileName || `${id}.bin`;
|
||||||
|
mimeType = documentMessage.mimetype;
|
||||||
|
} else if (imageMessage) {
|
||||||
|
messageType = "image";
|
||||||
|
const extension = imageMessage.mimetype?.split("/").pop() || "jpg";
|
||||||
|
filename = `${id}.${extension}`;
|
||||||
|
mimeType = imageMessage.mimetype;
|
||||||
|
} else if (videoMessage) {
|
||||||
|
messageType = "video";
|
||||||
|
const extension = videoMessage.mimetype?.split("/").pop() || "mp4";
|
||||||
|
filename = `${id}.${extension}`;
|
||||||
|
mimeType = videoMessage.mimetype;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = await downloadContentFromMessage(
|
||||||
|
messageContent,
|
||||||
|
// @ts-ignore
|
||||||
|
messageType,
|
||||||
|
);
|
||||||
|
let buffer = Buffer.from([]);
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
buffer = Buffer.concat([buffer, chunk]);
|
||||||
|
}
|
||||||
|
attachment = buffer.toString("base64");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messageContent || attachment) {
|
||||||
|
const conversation = message?.conversation;
|
||||||
|
const extendedTextMessage = message?.extendedTextMessage?.text;
|
||||||
|
const imageMessage = message?.imageMessage?.caption;
|
||||||
|
const videoMessage = message?.videoMessage?.caption;
|
||||||
|
const messageText = [
|
||||||
|
conversation,
|
||||||
|
extendedTextMessage,
|
||||||
|
imageMessage,
|
||||||
|
videoMessage,
|
||||||
|
].find((text) => text && text !== "");
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
to: botID,
|
||||||
|
from: remoteJid?.split("@")[0],
|
||||||
|
messageId: id,
|
||||||
|
sentAt: new Date((messageTimestamp as number) * 1000).toISOString(),
|
||||||
|
message: messageText,
|
||||||
|
attachment,
|
||||||
|
filename,
|
||||||
|
mimeType,
|
||||||
|
};
|
||||||
|
|
||||||
|
await fetch(
|
||||||
|
`${process.env.BRIDGE_FRONTEND_URL}/api/whatsapp/bots/${botID}/receive`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async queueUnreadMessages(botID: string, messages: proto.IWebMessageInfo[]) {
|
||||||
|
for await (const message of messages) {
|
||||||
|
await this.queueMessage(botID, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getBot(botID: string): Record<string, any> {
|
||||||
|
const botDirectory = this.getBotDirectory(botID);
|
||||||
|
const qrPath = `${botDirectory}/qr.txt`;
|
||||||
|
const verifiedFile = `${botDirectory}/verified`;
|
||||||
|
const qr = fs.existsSync(qrPath) ? fs.readFileSync(qrPath, "utf8") : null;
|
||||||
|
const verified = fs.existsSync(verifiedFile);
|
||||||
|
|
||||||
|
return { qr, verified };
|
||||||
|
}
|
||||||
|
|
||||||
|
async unverify(botID: string): Promise<void> {
|
||||||
|
// Step 1: Close and remove the active connection if it exists
|
||||||
|
const connection = this.connections[botID];
|
||||||
|
if (connection?.socket) {
|
||||||
|
try {
|
||||||
|
// Properly close the WebSocket connection
|
||||||
|
await connection.socket.logout();
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn({ botID, error }, "Error during logout, forcing disconnect");
|
||||||
|
try {
|
||||||
|
connection.socket.end(undefined);
|
||||||
|
} catch (endError) {
|
||||||
|
logger.warn({ botID, endError }, "Error ending socket connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Remove from in-memory connections
|
||||||
|
delete this.connections[botID];
|
||||||
|
|
||||||
|
// Step 3: Remove the bot directory (auth state, QR code, verified marker)
|
||||||
|
const botDirectory = this.getBotDirectory(botID);
|
||||||
|
if (fs.existsSync(botDirectory)) {
|
||||||
|
fs.rmSync(botDirectory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async register(botID: string, callback?: AuthCompleteCallback): Promise<void> {
|
||||||
|
const { version } = await fetchLatestBaileysVersion();
|
||||||
|
await this.createConnection(
|
||||||
|
botID,
|
||||||
|
this.server,
|
||||||
|
{ version, browser: WhatsappService.browserDescription },
|
||||||
|
callback,
|
||||||
|
);
|
||||||
|
callback?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
async send(
|
||||||
|
botID: string,
|
||||||
|
phoneNumber: string,
|
||||||
|
message: string,
|
||||||
|
attachments?: Array<{ data: string; filename: string; mime_type: string }>,
|
||||||
|
): Promise<void> {
|
||||||
|
const connection = this.connections[botID]?.socket;
|
||||||
|
const digits = phoneNumber.replace(/\D+/g, "");
|
||||||
|
// LIDs are 15+ digits, phone numbers with country code are typically 10-14 digits
|
||||||
|
const suffix = digits.length > 14 ? "@lid" : "@s.whatsapp.net";
|
||||||
|
const recipient = `${digits}${suffix}`;
|
||||||
|
|
||||||
|
// Send text message if provided
|
||||||
|
if (message) {
|
||||||
|
await connection.sendMessage(recipient, { text: message });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send attachments if provided with size validation
|
||||||
|
if (attachments && attachments.length > 0) {
|
||||||
|
const MAX_ATTACHMENT_SIZE = getMaxAttachmentSize();
|
||||||
|
const MAX_TOTAL_SIZE = getMaxTotalAttachmentSize();
|
||||||
|
|
||||||
|
if (attachments.length > MAX_ATTACHMENTS) {
|
||||||
|
throw new Error(
|
||||||
|
`Too many attachments: ${attachments.length} (max ${MAX_ATTACHMENTS})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let totalSize = 0;
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
// Calculate size before converting to buffer
|
||||||
|
const estimatedSize = (attachment.data.length * 3) / 4;
|
||||||
|
|
||||||
|
if (estimatedSize > MAX_ATTACHMENT_SIZE) {
|
||||||
|
logger.warn(
|
||||||
|
{
|
||||||
|
filename: attachment.filename,
|
||||||
|
size: estimatedSize,
|
||||||
|
maxSize: MAX_ATTACHMENT_SIZE,
|
||||||
|
},
|
||||||
|
"Attachment exceeds size limit, skipping",
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
totalSize += estimatedSize;
|
||||||
|
if (totalSize > MAX_TOTAL_SIZE) {
|
||||||
|
logger.warn(
|
||||||
|
{
|
||||||
|
totalSize,
|
||||||
|
maxTotalSize: MAX_TOTAL_SIZE,
|
||||||
|
},
|
||||||
|
"Total attachment size exceeds limit, skipping remaining",
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = Buffer.from(attachment.data, "base64");
|
||||||
|
|
||||||
|
if (attachment.mime_type.startsWith("image/")) {
|
||||||
|
await connection.sendMessage(recipient, {
|
||||||
|
image: buffer,
|
||||||
|
caption: attachment.filename,
|
||||||
|
});
|
||||||
|
} else if (attachment.mime_type.startsWith("video/")) {
|
||||||
|
await connection.sendMessage(recipient, {
|
||||||
|
video: buffer,
|
||||||
|
caption: attachment.filename,
|
||||||
|
});
|
||||||
|
} else if (attachment.mime_type.startsWith("audio/")) {
|
||||||
|
await connection.sendMessage(recipient, {
|
||||||
|
audio: buffer,
|
||||||
|
mimetype: attachment.mime_type,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await connection.sendMessage(recipient, {
|
||||||
|
document: buffer,
|
||||||
|
fileName: attachment.filename,
|
||||||
|
mimetype: attachment.mime_type,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async receive(
|
||||||
|
botID: string,
|
||||||
|
_lastReceivedDate: Date,
|
||||||
|
): Promise<proto.IWebMessageInfo[]> {
|
||||||
|
const connection = this.connections[botID]?.socket;
|
||||||
|
const messages = await connection.loadAllUnreadMessages();
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
apps/bridge-whatsapp/src/types.ts
Normal file
8
apps/bridge-whatsapp/src/types.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import type WhatsappService from "./service.js";
|
||||||
|
|
||||||
|
declare module "@hapipal/schmervice" {
|
||||||
|
interface SchmerviceDecorator {
|
||||||
|
(namespace: "whatsapp"): WhatsappService;
|
||||||
|
}
|
||||||
|
type ServiceFunctionalInterface = { name: string };
|
||||||
|
}
|
||||||
17
apps/bridge-whatsapp/tsconfig.json
Normal file
17
apps/bridge-whatsapp/tsconfig.json
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"extends": "@link-stack/typescript-config/tsconfig.node.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "commonjs",
|
||||||
|
"target": "es2018",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"outDir": "build/main",
|
||||||
|
"rootDir": "src",
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"types": ["node"],
|
||||||
|
"lib": ["es2020", "DOM"],
|
||||||
|
"composite": true
|
||||||
|
},
|
||||||
|
"include": ["src/**/*.ts", "src/**/.*.ts"],
|
||||||
|
"exclude": ["node_modules/**"]
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,5 @@
|
||||||
**/.env*
|
**/.env*
|
||||||
**/coverage
|
**/coverage
|
||||||
**/.next
|
**/.next
|
||||||
**/amigo.*.json
|
|
||||||
**/.yalc
|
|
||||||
**/cypress/videos
|
**/cypress/videos
|
||||||
**/cypress/screenshots
|
**/cypress/screenshots
|
||||||
45
apps/bridge-worker/Dockerfile
Normal file
45
apps/bridge-worker/Dockerfile
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
FROM node:22-bookworm-slim AS base
|
||||||
|
|
||||||
|
FROM base AS builder
|
||||||
|
ARG APP_DIR=/opt/bridge-worker
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN mkdir -p ${APP_DIR}/
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
RUN pnpm add -g turbo
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
COPY . .
|
||||||
|
RUN turbo prune --scope=@link-stack/bridge-worker --docker
|
||||||
|
|
||||||
|
FROM base AS installer
|
||||||
|
ARG APP_DIR=/opt/bridge-worker
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
COPY --from=builder ${APP_DIR}/out/json/ .
|
||||||
|
COPY --from=builder ${APP_DIR}/out/full/ .
|
||||||
|
COPY --from=builder ${APP_DIR}/out/pnpm-lock.yaml ./pnpm-lock.yaml
|
||||||
|
RUN pnpm install --frozen-lockfile
|
||||||
|
RUN pnpm add -g turbo
|
||||||
|
RUN turbo run build --filter=@link-stack/bridge-worker
|
||||||
|
|
||||||
|
FROM base as runner
|
||||||
|
ARG BUILD_DATE
|
||||||
|
ARG VERSION
|
||||||
|
ARG APP_DIR=/opt/bridge-worker
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
|
||||||
|
RUN mkdir -p ${APP_DIR}/
|
||||||
|
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
|
||||||
|
apt-get install -y --no-install-recommends \
|
||||||
|
dumb-init
|
||||||
|
WORKDIR ${APP_DIR}
|
||||||
|
COPY --from=installer ${APP_DIR} ./
|
||||||
|
RUN chown -R node:node ${APP_DIR}
|
||||||
|
WORKDIR ${APP_DIR}/apps/bridge-worker/
|
||||||
|
RUN chmod +x docker-entrypoint.sh
|
||||||
|
USER node
|
||||||
|
ENV NODE_ENV production
|
||||||
|
ENTRYPOINT ["/opt/bridge-worker/apps/bridge-worker/docker-entrypoint.sh"]
|
||||||
144
apps/bridge-worker/README.md
Normal file
144
apps/bridge-worker/README.md
Normal file
|
|
@ -0,0 +1,144 @@
|
||||||
|
# Bridge Worker
|
||||||
|
|
||||||
|
Background job processor for handling asynchronous tasks in the CDR Link communication bridge system.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Bridge Worker uses Graphile Worker to process queued jobs for message handling, media conversion, webhook notifications, and scheduled tasks. It manages the flow of messages between various communication channels (Signal, WhatsApp, Facebook, Voice) and the Zammad ticketing system.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Message Processing**: Handle incoming/outgoing messages for all supported channels
|
||||||
|
- **Media Conversion**: Convert audio/video files between formats
|
||||||
|
- **Webhook Notifications**: Notify external systems of events
|
||||||
|
- **Scheduled Tasks**: Cron-based job scheduling
|
||||||
|
- **Job Queue Management**: Reliable job processing with retries
|
||||||
|
- **Multi-Channel Support**: Signal, WhatsApp, Facebook, Voice (Twilio)
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- Node.js >= 20
|
||||||
|
- npm >= 10
|
||||||
|
- PostgreSQL database
|
||||||
|
- Redis (for caching)
|
||||||
|
- FFmpeg (for media conversion)
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install dependencies
|
||||||
|
npm install
|
||||||
|
|
||||||
|
# Build TypeScript
|
||||||
|
npm run build
|
||||||
|
|
||||||
|
# Run development server with auto-reload
|
||||||
|
npm run dev
|
||||||
|
|
||||||
|
# Start production worker
|
||||||
|
npm run start
|
||||||
|
```
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
Required environment variables:
|
||||||
|
|
||||||
|
- `DATABASE_URL` - PostgreSQL connection string
|
||||||
|
- `GRAPHILE_WORKER_CONCURRENCY` - Number of concurrent jobs (default: 10)
|
||||||
|
- `GRAPHILE_WORKER_POLL_INTERVAL` - Job poll interval in ms (default: 1000)
|
||||||
|
- `ZAMMAD_URL` - Zammad instance URL
|
||||||
|
- `ZAMMAD_API_TOKEN` - Zammad API token
|
||||||
|
- `TWILIO_ACCOUNT_SID` - Twilio account SID
|
||||||
|
- `TWILIO_AUTH_TOKEN` - Twilio auth token
|
||||||
|
- `SIGNAL_CLI_URL` - Signal CLI REST API URL
|
||||||
|
- `WHATSAPP_SERVICE_URL` - WhatsApp bridge service URL
|
||||||
|
- `FACEBOOK_APP_SECRET` - Facebook app secret
|
||||||
|
- `FACEBOOK_PAGE_ACCESS_TOKEN` - Facebook page token
|
||||||
|
|
||||||
|
### Available Scripts
|
||||||
|
|
||||||
|
- `npm run build` - Compile TypeScript
|
||||||
|
- `npm run dev` - Development mode with watch
|
||||||
|
- `npm run start` - Start production worker
|
||||||
|
|
||||||
|
## Task Types
|
||||||
|
|
||||||
|
### Signal Tasks
|
||||||
|
- `receive-signal-message` - Process incoming Signal messages
|
||||||
|
- `send-signal-message` - Send outgoing Signal messages
|
||||||
|
- `fetch-signal-messages` - Fetch messages from Signal CLI
|
||||||
|
|
||||||
|
### WhatsApp Tasks
|
||||||
|
- `receive-whatsapp-message` - Process incoming WhatsApp messages
|
||||||
|
- `send-whatsapp-message` - Send outgoing WhatsApp messages
|
||||||
|
|
||||||
|
### Facebook Tasks
|
||||||
|
- `receive-facebook-message` - Process incoming Facebook messages
|
||||||
|
- `send-facebook-message` - Send outgoing Facebook messages
|
||||||
|
|
||||||
|
### Voice Tasks
|
||||||
|
- `receive-voice-message` - Process incoming voice calls/messages
|
||||||
|
- `send-voice-message` - Send voice messages via Twilio
|
||||||
|
- `twilio-recording` - Handle Twilio call recordings
|
||||||
|
- `voice-line-audio-update` - Update voice line audio
|
||||||
|
- `voice-line-delete` - Delete voice line
|
||||||
|
- `voice-line-provider-update` - Update voice provider settings
|
||||||
|
|
||||||
|
### Common Tasks
|
||||||
|
- `notify-webhooks` - Send webhook notifications
|
||||||
|
- `import-label-studio` - Import Label Studio annotations
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### Job Processing
|
||||||
|
|
||||||
|
Jobs are queued in PostgreSQL using Graphile Worker:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await addJob('send-signal-message', {
|
||||||
|
to: '+1234567890',
|
||||||
|
message: 'Hello world'
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cron Schedule
|
||||||
|
|
||||||
|
Scheduled tasks are configured in `crontab`:
|
||||||
|
- Periodic message fetching
|
||||||
|
- Cleanup tasks
|
||||||
|
- Health checks
|
||||||
|
|
||||||
|
### Error Handling
|
||||||
|
|
||||||
|
- Automatic retries with exponential backoff
|
||||||
|
- Dead letter queue for failed jobs
|
||||||
|
- Comprehensive logging with winston
|
||||||
|
|
||||||
|
## Media Handling
|
||||||
|
|
||||||
|
Supports conversion between formats:
|
||||||
|
- Audio: MP3, OGG, WAV, M4A
|
||||||
|
- Uses fluent-ffmpeg for processing
|
||||||
|
- Automatic format detection
|
||||||
|
|
||||||
|
## Integration Points
|
||||||
|
|
||||||
|
- **Zammad**: Creates/updates tickets via API
|
||||||
|
- **Signal CLI**: REST API for Signal messaging
|
||||||
|
- **WhatsApp Bridge**: HTTP API for WhatsApp
|
||||||
|
- **Twilio**: Voice and SMS capabilities
|
||||||
|
- **Facebook**: Graph API for Messenger
|
||||||
|
|
||||||
|
## Docker Support
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build image
|
||||||
|
docker build -t link-stack/bridge-worker .
|
||||||
|
|
||||||
|
# Run with docker-compose
|
||||||
|
docker-compose -f docker/compose/bridge.yml up
|
||||||
|
```
|
||||||
|
|
||||||
|
The worker includes cron support via built-in crontab.
|
||||||
2
apps/bridge-worker/crontab
Normal file
2
apps/bridge-worker/crontab
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
*/1 * * * * fetch-signal-messages ?max=1&id=fetchSignalMessagesCron {"scheduleTasks": "true"}
|
||||||
|
*/2 * * * * check-group-membership ?max=1&id=checkGroupMembershipCron {}
|
||||||
5
apps/bridge-worker/docker-entrypoint.sh
Normal file
5
apps/bridge-worker/docker-entrypoint.sh
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
echo "starting bridge-worker"
|
||||||
|
exec dumb-init pnpm run start
|
||||||
16
apps/bridge-worker/graphile.config.ts
Normal file
16
apps/bridge-worker/graphile.config.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import type {} from "graphile-worker";
|
||||||
|
|
||||||
|
const preset: any = {
|
||||||
|
worker: {
|
||||||
|
connectionString: process.env.DATABASE_URL,
|
||||||
|
maxPoolSize: process.env.BRIDGE_WORKER_POOL_SIZE
|
||||||
|
? parseInt(process.env.BRIDGE_WORKER_POOL_SIZE, 10)
|
||||||
|
: 10,
|
||||||
|
pollInterval: process.env.BRIDGE_WORKER_POLL_INTERVAL
|
||||||
|
? parseInt(process.env.BRIDGE_WORKER_POLL_INTERVAL, 10)
|
||||||
|
: 2000,
|
||||||
|
fileExtensions: [".ts"],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default preset;
|
||||||
46
apps/bridge-worker/index.ts
Normal file
46
apps/bridge-worker/index.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { run } from "graphile-worker";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import * as path from "path";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
|
||||||
|
const logger = createLogger("bridge-worker");
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
const startWorker = async () => {
|
||||||
|
logger.info("Starting worker...");
|
||||||
|
|
||||||
|
await run({
|
||||||
|
connectionString: process.env.DATABASE_URL,
|
||||||
|
noHandleSignals: false,
|
||||||
|
concurrency: process.env.BRIDGE_WORKER_CONCURRENCY
|
||||||
|
? parseInt(process.env.BRIDGE_WORKER_CONCURRENCY, 10)
|
||||||
|
: 10,
|
||||||
|
maxPoolSize: process.env.BRIDGE_WORKER_POOL_SIZE
|
||||||
|
? parseInt(process.env.BRIDGE_WORKER_POOL_SIZE, 10)
|
||||||
|
: 10,
|
||||||
|
pollInterval: process.env.BRIDGE_WORKER_POLL_INTERVAL
|
||||||
|
? parseInt(process.env.BRIDGE_WORKER_POLL_INTERVAL, 10)
|
||||||
|
: 1000,
|
||||||
|
taskDirectory: `${__dirname}/tasks`,
|
||||||
|
crontabFile: `${__dirname}/crontab`,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
await startWorker();
|
||||||
|
};
|
||||||
|
|
||||||
|
main().catch((err) => {
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
error: err,
|
||||||
|
message: err.message,
|
||||||
|
stack: err.stack,
|
||||||
|
name: err.name,
|
||||||
|
},
|
||||||
|
"Worker failed to start",
|
||||||
|
);
|
||||||
|
console.error("Full error:", err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
20
apps/bridge-worker/lib/common.ts
Normal file
20
apps/bridge-worker/lib/common.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
// import { SavedVoiceProvider } from "@digiresilience/bridge-db";
|
||||||
|
import Twilio from "twilio";
|
||||||
|
|
||||||
|
type SavedVoiceProvider = any;
|
||||||
|
|
||||||
|
export const twilioClientFor = (
|
||||||
|
provider: SavedVoiceProvider,
|
||||||
|
): Twilio.Twilio => {
|
||||||
|
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
||||||
|
if (!accountSid || !apiKeySid || !apiKeySecret)
|
||||||
|
throw new Error(
|
||||||
|
`twilio provider ${provider.name} does not have credentials`,
|
||||||
|
);
|
||||||
|
|
||||||
|
return Twilio(apiKeySid, apiKeySecret, {
|
||||||
|
accountSid,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
|
/*
|
||||||
import pgPromise from "pg-promise";
|
import pgPromise from "pg-promise";
|
||||||
import * as pgMonitor from "pg-monitor";
|
import * as pgMonitor from "pg-monitor";
|
||||||
import { dbInitOptions, IRepositories, AppDatabase } from "@digiresilience/metamigo-db";
|
import {
|
||||||
import config from "@digiresilience/metamigo-config";
|
dbInitOptions,
|
||||||
|
IRepositories,
|
||||||
|
AppDatabase,
|
||||||
|
} from "@digiresilience/bridge-db";
|
||||||
|
import config from "@digiresilience/bridge-config";
|
||||||
import type { IInitOptions } from "pg-promise";
|
import type { IInitOptions } from "pg-promise";
|
||||||
|
|
||||||
export const initDiagnostics = (
|
export const initDiagnostics = (
|
||||||
logSql: boolean,
|
logSql: boolean,
|
||||||
initOpts: IInitOptions<IRepositories>
|
initOpts: IInitOptions<IRepositories>,
|
||||||
): void => {
|
): void => {
|
||||||
if (logSql) {
|
if (logSql) {
|
||||||
pgMonitor.attach(initOpts);
|
pgMonitor.attach(initOpts);
|
||||||
|
|
@ -33,8 +38,12 @@ const initDb = (): AppDatabase => {
|
||||||
export const stopDb = async (db: AppDatabase): Promise<void> => {
|
export const stopDb = async (db: AppDatabase): Promise<void> => {
|
||||||
return db.$pool.end();
|
return db.$pool.end();
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type AppDatabase = any;
|
||||||
|
|
||||||
export const withDb = <T>(f: (db: AppDatabase) => Promise<T>): Promise<T> => {
|
export const withDb = <T>(f: (db: AppDatabase) => Promise<T>): Promise<T> => {
|
||||||
|
/*
|
||||||
const db = initDb();
|
const db = initDb();
|
||||||
initDiagnostics(config.logging.sql, pgpInitOptions);
|
initDiagnostics(config.logging.sql, pgpInitOptions);
|
||||||
try {
|
try {
|
||||||
|
|
@ -42,6 +51,6 @@ export const withDb = <T>(f: (db: AppDatabase) => Promise<T>): Promise<T> => {
|
||||||
} finally {
|
} finally {
|
||||||
stopDiagnostics();
|
stopDiagnostics();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
return f(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
export type { AppDatabase } from "@digiresilience/metamigo-db";
|
|
||||||
272
apps/bridge-worker/lib/formstack-field-mapping.ts
Normal file
272
apps/bridge-worker/lib/formstack-field-mapping.ts
Normal file
|
|
@ -0,0 +1,272 @@
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('formstack-field-mapping');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Field mapping configuration for Formstack to Zammad integration
|
||||||
|
*
|
||||||
|
* This configuration is completely flexible - you define your own internal field names
|
||||||
|
* and map them to both Formstack source fields and Zammad custom fields.
|
||||||
|
*/
|
||||||
|
export interface FieldMappingConfig {
|
||||||
|
/**
|
||||||
|
* Map internal field keys to Formstack field names
|
||||||
|
*
|
||||||
|
* Required keys (system):
|
||||||
|
* - formId: The Formstack Form ID field
|
||||||
|
* - uniqueId: The Formstack submission unique ID field
|
||||||
|
*
|
||||||
|
* Optional keys with special behavior:
|
||||||
|
* - email: Used for user lookup/creation (if provided)
|
||||||
|
* - phone: Used for user lookup/creation (if provided)
|
||||||
|
* - signalAccount: Used for Signal-based user lookup (tried first before phone)
|
||||||
|
* - name: User's full name (can be nested object with first/last, used in user creation)
|
||||||
|
* - organization: Used in ticket title template placeholder {organization}
|
||||||
|
* - typeOfSupport: Used in ticket title template placeholder {typeOfSupport}
|
||||||
|
* - descriptionOfIssue: Used as article subject (defaults to "Support Request" if not provided)
|
||||||
|
*
|
||||||
|
* All other keys are completely arbitrary and defined by your form.
|
||||||
|
*/
|
||||||
|
sourceFields: Record<string, string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map Zammad custom field names to internal field keys (from sourceFields)
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* {
|
||||||
|
* "us_state": "state", // Zammad field "us_state" gets value from sourceFields["state"]
|
||||||
|
* "zip_code": "zipCode", // Zammad field "zip_code" gets value from sourceFields["zipCode"]
|
||||||
|
* "custom_field": "myField" // Any custom field mapping
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* The values in this object must correspond to keys in sourceFields.
|
||||||
|
*/
|
||||||
|
zammadFields: Record<string, string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for ticket creation
|
||||||
|
*/
|
||||||
|
ticket: {
|
||||||
|
/** Zammad group name to assign tickets to */
|
||||||
|
group: string;
|
||||||
|
|
||||||
|
/** Article type name (e.g., "note", "cdr_signal", "email") */
|
||||||
|
defaultArticleType: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template for ticket title
|
||||||
|
* Supports placeholders: {name}, {organization}, {typeOfSupport}
|
||||||
|
* Placeholders reference internal field keys from sourceFields
|
||||||
|
*/
|
||||||
|
titleTemplate?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for extracting nested field values
|
||||||
|
*/
|
||||||
|
nestedFields?: {
|
||||||
|
/**
|
||||||
|
* How to extract first/last name from a nested Name field
|
||||||
|
* Example: { firstNamePath: "first", lastNamePath: "last" }
|
||||||
|
* for a field like { "Name": { "first": "John", "last": "Doe" } }
|
||||||
|
*/
|
||||||
|
name?: {
|
||||||
|
firstNamePath?: string;
|
||||||
|
lastNamePath?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let cachedMapping: FieldMappingConfig | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load field mapping configuration from environment variable (REQUIRED)
|
||||||
|
*/
|
||||||
|
export function loadFieldMapping(): FieldMappingConfig {
|
||||||
|
if (cachedMapping) {
|
||||||
|
return cachedMapping;
|
||||||
|
}
|
||||||
|
|
||||||
|
const configJson = process.env.FORMSTACK_FIELD_MAPPING;
|
||||||
|
|
||||||
|
if (!configJson) {
|
||||||
|
throw new Error(
|
||||||
|
'FORMSTACK_FIELD_MAPPING environment variable is required. ' +
|
||||||
|
'Please set it to a JSON string containing your field mapping configuration.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info('Loading Formstack field mapping from environment variable');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const config = JSON.parse(configJson) as FieldMappingConfig;
|
||||||
|
|
||||||
|
// Validate required sections exist
|
||||||
|
if (!config.sourceFields || typeof config.sourceFields !== 'object') {
|
||||||
|
throw new Error('Invalid field mapping configuration: sourceFields must be an object');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.zammadFields || typeof config.zammadFields !== 'object') {
|
||||||
|
throw new Error('Invalid field mapping configuration: zammadFields must be an object');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.ticket || typeof config.ticket !== 'object') {
|
||||||
|
throw new Error('Invalid field mapping configuration: ticket must be an object');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate required ticket fields
|
||||||
|
if (!config.ticket.group) {
|
||||||
|
throw new Error('Invalid field mapping configuration: ticket.group is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.ticket.defaultArticleType) {
|
||||||
|
throw new Error('Invalid field mapping configuration: ticket.defaultArticleType is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate required source fields
|
||||||
|
const systemRequiredFields = ['formId', 'uniqueId'];
|
||||||
|
for (const field of systemRequiredFields) {
|
||||||
|
if (!config.sourceFields[field]) {
|
||||||
|
throw new Error(`Invalid field mapping configuration: sourceFields.${field} is required (system field)`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate zammadFields reference valid sourceFields
|
||||||
|
for (const [zammadField, sourceKey] of Object.entries(config.zammadFields)) {
|
||||||
|
if (!config.sourceFields[sourceKey]) {
|
||||||
|
logger.warn(
|
||||||
|
{ zammadField, sourceKey },
|
||||||
|
'Zammad field maps to non-existent source field key'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info('Successfully loaded Formstack field mapping configuration');
|
||||||
|
cachedMapping = config;
|
||||||
|
return cachedMapping;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logger.error({
|
||||||
|
error: error instanceof Error ? error.message : error,
|
||||||
|
jsonLength: configJson.length
|
||||||
|
}, 'Failed to parse field mapping configuration');
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
`Failed to parse Formstack field mapping JSON: ${error instanceof Error ? error.message : error}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a field value from formData using the source field name mapping
|
||||||
|
*/
|
||||||
|
export function getFieldValue(
|
||||||
|
formData: any,
|
||||||
|
internalFieldKey: string,
|
||||||
|
mapping?: FieldMappingConfig
|
||||||
|
): any {
|
||||||
|
const config = mapping || loadFieldMapping();
|
||||||
|
const sourceFieldName = config.sourceFields[internalFieldKey];
|
||||||
|
if (!sourceFieldName) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return formData[sourceFieldName];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a nested field value (e.g., Name.first)
|
||||||
|
*/
|
||||||
|
export function getNestedFieldValue(
|
||||||
|
fieldValue: any,
|
||||||
|
path: string | undefined
|
||||||
|
): any {
|
||||||
|
if (!path || !fieldValue) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = path.split('.');
|
||||||
|
let current = fieldValue;
|
||||||
|
|
||||||
|
for (const part of parts) {
|
||||||
|
if (current && typeof current === 'object') {
|
||||||
|
current = current[part];
|
||||||
|
} else {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format field value (handle arrays, objects, etc.)
|
||||||
|
*/
|
||||||
|
export function formatFieldValue(value: any): string | undefined {
|
||||||
|
if (value === null || value === undefined || value === '') {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
return value.join(', ');
|
||||||
|
}
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
return JSON.stringify(value);
|
||||||
|
}
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build ticket title from template and data
|
||||||
|
* Replaces placeholders like {name}, {organization}, {typeOfSupport} with provided values
|
||||||
|
*/
|
||||||
|
export function buildTicketTitle(
|
||||||
|
mapping: FieldMappingConfig,
|
||||||
|
data: Record<string, string | undefined>
|
||||||
|
): string {
|
||||||
|
const template = mapping.ticket.titleTemplate || '{name}';
|
||||||
|
|
||||||
|
let title = template;
|
||||||
|
|
||||||
|
// Replace all placeholders in the template
|
||||||
|
for (const [key, value] of Object.entries(data)) {
|
||||||
|
const placeholder = `{${key}}`;
|
||||||
|
if (title.includes(placeholder)) {
|
||||||
|
if (value) {
|
||||||
|
title = title.replace(placeholder, value);
|
||||||
|
} else {
|
||||||
|
// Remove empty placeholder and surrounding separators
|
||||||
|
title = title.replace(` - ${placeholder}`, '').replace(`${placeholder} - `, '').replace(placeholder, '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return title.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all Zammad field values from form data using the mapping
|
||||||
|
* Returns an object with Zammad field names as keys and formatted values
|
||||||
|
*/
|
||||||
|
export function getZammadFieldValues(
|
||||||
|
formData: any,
|
||||||
|
mapping?: FieldMappingConfig
|
||||||
|
): Record<string, string> {
|
||||||
|
const config = mapping || loadFieldMapping();
|
||||||
|
const result: Record<string, string> = {};
|
||||||
|
|
||||||
|
for (const [zammadFieldName, sourceKey] of Object.entries(config.zammadFields)) {
|
||||||
|
const value = getFieldValue(formData, sourceKey, config);
|
||||||
|
const formatted = formatFieldValue(value);
|
||||||
|
if (formatted !== undefined) {
|
||||||
|
result[zammadFieldName] = formatted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset cached mapping (useful for testing)
|
||||||
|
*/
|
||||||
|
export function resetMappingCache(): void {
|
||||||
|
cachedMapping = null;
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
import ffmpeg from "fluent-ffmpeg";
|
import ffmpeg from "fluent-ffmpeg";
|
||||||
import * as R from "remeda";
|
import * as R from "remeda";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('bridge-worker-media-convert');
|
||||||
|
|
||||||
const requiredCodecs = ["mp3", "webm", "wav"];
|
const requiredCodecs = ["mp3", "webm", "wav"];
|
||||||
|
|
||||||
|
|
@ -25,7 +28,7 @@ const defaultAudioConvertOpts = {
|
||||||
**/
|
**/
|
||||||
export const convert = (
|
export const convert = (
|
||||||
input: Buffer,
|
input: Buffer,
|
||||||
opts?: AudioConvertOpts
|
opts?: AudioConvertOpts,
|
||||||
): Promise<Buffer> => {
|
): Promise<Buffer> => {
|
||||||
const settings = { ...defaultAudioConvertOpts, ...opts };
|
const settings = { ...defaultAudioConvertOpts, ...opts };
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
@ -35,12 +38,8 @@ export const convert = (
|
||||||
.audioCodec(settings.audioCodec)
|
.audioCodec(settings.audioCodec)
|
||||||
.audioBitrate(settings.bitrate)
|
.audioBitrate(settings.bitrate)
|
||||||
.toFormat(settings.format)
|
.toFormat(settings.format)
|
||||||
.on("error", (err, stdout, stderr) => {
|
.on("error", (err, _stdout, _stderr) => {
|
||||||
console.error(err.message);
|
logger.error({ error: err }, 'FFmpeg conversion error');
|
||||||
console.log("FFMPEG OUTPUT");
|
|
||||||
console.log(stdout);
|
|
||||||
console.log("FFMPEG ERROR");
|
|
||||||
console.log(stderr);
|
|
||||||
reject(err);
|
reject(err);
|
||||||
})
|
})
|
||||||
.on("end", () => {
|
.on("end", () => {
|
||||||
|
|
@ -62,12 +61,16 @@ export const selfCheck = (): Promise<boolean> => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
ffmpeg.getAvailableFormats((err, codecs) => {
|
ffmpeg.getAvailableFormats((err, codecs) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("FFMPEG error:", err);
|
logger.error({ error: err }, 'FFMPEG error');
|
||||||
resolve(false);
|
resolve(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
const preds = R.map(requiredCodecs, (codec) => (available: any) =>
|
const preds = R.map(
|
||||||
available[codec] && available[codec].canDemux && available[codec].canMux
|
requiredCodecs,
|
||||||
|
(codec) => (available: any) =>
|
||||||
|
available[codec] &&
|
||||||
|
available[codec].canDemux &&
|
||||||
|
available[codec].canMux,
|
||||||
);
|
);
|
||||||
|
|
||||||
resolve(R.allPass(codecs, preds));
|
resolve(R.allPass(codecs, preds));
|
||||||
|
|
@ -79,6 +82,6 @@ export const assertFfmpegAvailable = async (): Promise<void> => {
|
||||||
const r = await selfCheck();
|
const r = await selfCheck();
|
||||||
if (!r)
|
if (!r)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`ffmpeg is not installed, could not be located, or does not support the required codecs: ${requiredCodecs}`
|
`ffmpeg is not installed, could not be located, or does not support the required codecs: ${requiredCodecs}`,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import * as Worker from "graphile-worker";
|
import * as Worker from "graphile-worker";
|
||||||
import { defState } from "@digiresilience/montar";
|
// import { defState } from "@digiresilience/montar";
|
||||||
import config from "@digiresilience/metamigo-config";
|
//import config from "@digiresilience/bridge-config";
|
||||||
|
|
||||||
|
/*
|
||||||
const startWorkerUtils = async (): Promise<Worker.WorkerUtils> => {
|
const startWorkerUtils = async (): Promise<Worker.WorkerUtils> => {
|
||||||
const workerUtils = await Worker.makeWorkerUtils({
|
const workerUtils = await Worker.makeWorkerUtils({
|
||||||
connectionString: config.worker.connection,
|
connectionString: config.worker.connection,
|
||||||
|
|
@ -18,4 +19,8 @@ const workerUtils = defState("workerUtils", {
|
||||||
stop: stopWorkerUtils,
|
stop: stopWorkerUtils,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const workerUtils: any = {};
|
||||||
export default workerUtils;
|
export default workerUtils;
|
||||||
0
apps/bridge-worker/lib/whatsapp.ts
Normal file
0
apps/bridge-worker/lib/whatsapp.ts
Normal file
171
apps/bridge-worker/lib/zammad.ts
Normal file
171
apps/bridge-worker/lib/zammad.ts
Normal file
|
|
@ -0,0 +1,171 @@
|
||||||
|
/* eslint-disable camelcase,@typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any */
|
||||||
|
import querystring from "querystring";
|
||||||
|
import Wreck from "@hapi/wreck";
|
||||||
|
|
||||||
|
export interface User {
|
||||||
|
id: number;
|
||||||
|
firstname?: string;
|
||||||
|
lastname?: string;
|
||||||
|
email?: string;
|
||||||
|
phone?: string;
|
||||||
|
}
|
||||||
|
export interface Ticket {
|
||||||
|
id: number;
|
||||||
|
title?: string;
|
||||||
|
group_id?: number;
|
||||||
|
customer_id?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ZammadClient {
|
||||||
|
ticket: {
|
||||||
|
create: (data: any) => Promise<Ticket>;
|
||||||
|
update: (id: number, data: any) => Promise<Ticket>;
|
||||||
|
};
|
||||||
|
user: {
|
||||||
|
search: (data: any) => Promise<User[]>;
|
||||||
|
create: (data: any) => Promise<User>;
|
||||||
|
};
|
||||||
|
get: (path: string) => Promise<any>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ZammadCredentials =
|
||||||
|
| { username: string; password: string }
|
||||||
|
| { token: string };
|
||||||
|
|
||||||
|
export interface ZammadClientOpts {
|
||||||
|
headers?: Record<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const formatAuth = (credentials: any) => {
|
||||||
|
if (credentials.username) {
|
||||||
|
return (
|
||||||
|
"Basic " +
|
||||||
|
Buffer.from(`${credentials.username}:${credentials.password}`).toString(
|
||||||
|
"base64",
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (credentials.token) {
|
||||||
|
return `Token ${credentials.token}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("invalid zammad credentials type");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Zammad = (
|
||||||
|
credentials: ZammadCredentials,
|
||||||
|
host: string,
|
||||||
|
opts?: ZammadClientOpts,
|
||||||
|
): ZammadClient => {
|
||||||
|
const extraHeaders = (opts && opts.headers) || {};
|
||||||
|
|
||||||
|
const wreck = Wreck.defaults({
|
||||||
|
baseUrl: `${host}/api/v1/`,
|
||||||
|
headers: {
|
||||||
|
authorization: formatAuth(credentials),
|
||||||
|
...extraHeaders,
|
||||||
|
},
|
||||||
|
json: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
ticket: {
|
||||||
|
create: async (payload) => {
|
||||||
|
const { payload: result } = await wreck.post("tickets", { payload });
|
||||||
|
return result as Ticket;
|
||||||
|
},
|
||||||
|
update: async (id, payload) => {
|
||||||
|
const { payload: result } = await wreck.put(`tickets/${id}`, {
|
||||||
|
payload,
|
||||||
|
});
|
||||||
|
return result as Ticket;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
search: async (query) => {
|
||||||
|
const qp = querystring.stringify({ query });
|
||||||
|
const { payload: result } = await wreck.get(`users/search?${qp}`);
|
||||||
|
return result as User[];
|
||||||
|
},
|
||||||
|
create: async (payload) => {
|
||||||
|
const { payload: result } = await wreck.post("users", { payload });
|
||||||
|
return result as User;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
get: async (path) => {
|
||||||
|
const { payload: result } = await wreck.get(path);
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitizes phone number to E.164 format: +15554446666
|
||||||
|
* Strips all non-digit characters except +, ensures + prefix
|
||||||
|
* @param phoneNumber - Raw phone number (e.g., "(555) 444-6666", "5554446666", "+1 555 444 6666")
|
||||||
|
* @returns E.164 formatted phone number (e.g., "+15554446666")
|
||||||
|
* @throws Error if phone number is invalid
|
||||||
|
*/
|
||||||
|
export const sanitizePhoneNumber = (phoneNumber: string): string => {
|
||||||
|
// Remove all characters except digits and +
|
||||||
|
let cleaned = phoneNumber.replace(/[^\d+]/g, "");
|
||||||
|
|
||||||
|
// Ensure it starts with +
|
||||||
|
if (!cleaned.startsWith("+")) {
|
||||||
|
// Assume US/Canada if no country code (11 digits starting with 1, or 10 digits)
|
||||||
|
if (cleaned.length === 10) {
|
||||||
|
cleaned = "+1" + cleaned;
|
||||||
|
} else if (cleaned.length === 11 && cleaned.startsWith("1")) {
|
||||||
|
cleaned = "+" + cleaned;
|
||||||
|
} else if (cleaned.length >= 10) {
|
||||||
|
// International number without +, add it
|
||||||
|
cleaned = "+" + cleaned;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate E.164 format: + followed by 10-15 digits
|
||||||
|
if (!/^\+\d{10,15}$/.test(cleaned)) {
|
||||||
|
throw new Error(`Invalid phone number format: ${phoneNumber}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cleaned;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getUser = async (zammad: ZammadClient, phoneNumber: string) => {
|
||||||
|
// Sanitize to E.164 format
|
||||||
|
const sanitized = sanitizePhoneNumber(phoneNumber);
|
||||||
|
|
||||||
|
// Remove + for Zammad search query
|
||||||
|
const searchNumber = sanitized.replace("+", "");
|
||||||
|
|
||||||
|
// Try sanitized format first (e.g., "6464229653" for "+16464229653")
|
||||||
|
let results = await zammad.user.search(`phone:${searchNumber}`);
|
||||||
|
if (results.length > 0) return results[0];
|
||||||
|
|
||||||
|
// Fall back to searching for original input (handles legacy formatted numbers)
|
||||||
|
// This ensures we can find users with "(646) 422-9653" format in database
|
||||||
|
const originalCleaned = phoneNumber.replace(/[^\d+]/g, "").replace("+", "");
|
||||||
|
if (originalCleaned !== searchNumber) {
|
||||||
|
results = await zammad.user.search(`phone:${originalCleaned}`);
|
||||||
|
if (results.length > 0) return results[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getOrCreateUser = async (
|
||||||
|
zammad: ZammadClient,
|
||||||
|
phoneNumber: string,
|
||||||
|
) => {
|
||||||
|
const customer = await getUser(zammad, phoneNumber);
|
||||||
|
if (customer) return customer;
|
||||||
|
|
||||||
|
// Sanitize phone number to E.164 format before storing
|
||||||
|
const sanitized = sanitizePhoneNumber(phoneNumber);
|
||||||
|
|
||||||
|
return zammad.user.create({
|
||||||
|
phone: sanitized,
|
||||||
|
note: "User created from incoming voice call",
|
||||||
|
});
|
||||||
|
};
|
||||||
30
apps/bridge-worker/package.json
Normal file
30
apps/bridge-worker/package.json
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"name": "@link-stack/bridge-worker",
|
||||||
|
"version": "3.3.5",
|
||||||
|
"type": "module",
|
||||||
|
"main": "build/main/index.js",
|
||||||
|
"author": "Darren Clarke <darren@redaranj.com>",
|
||||||
|
"license": "AGPL-3.0-or-later",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc -p tsconfig.json && cp crontab build/main/crontab",
|
||||||
|
"dev": "dotenv -- graphile-worker",
|
||||||
|
"start": "node build/main/index.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@hapi/wreck": "^18.1.0",
|
||||||
|
"@link-stack/bridge-common": "workspace:*",
|
||||||
|
"@link-stack/logger": "workspace:*",
|
||||||
|
"@link-stack/signal-api": "workspace:*",
|
||||||
|
"fluent-ffmpeg": "^2.1.3",
|
||||||
|
"graphile-worker": "^0.16.6",
|
||||||
|
"remeda": "^2.32.0",
|
||||||
|
"twilio": "^5.10.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/fluent-ffmpeg": "^2.1.27",
|
||||||
|
"dotenv-cli": "^10.0.0",
|
||||||
|
"@link-stack/eslint-config": "workspace:*",
|
||||||
|
"@link-stack/typescript-config": "workspace:*",
|
||||||
|
"typescript": "^5.9.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
121
apps/bridge-worker/tasks/check-group-membership.ts
Normal file
121
apps/bridge-worker/tasks/check-group-membership.ts
Normal file
|
|
@ -0,0 +1,121 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
/**
|
||||||
|
* Check Signal group membership status and update Zammad tickets
|
||||||
|
*
|
||||||
|
* This task queries the Signal CLI API to check if users have joined
|
||||||
|
* their assigned groups. When a user joins (moves from pendingInvites to members),
|
||||||
|
* it updates the ticket's group_joined flag in Zammad.
|
||||||
|
*
|
||||||
|
* Note: This task sends webhooks for all group members every time it runs.
|
||||||
|
* The Zammad webhook handler is idempotent and will ignore duplicate notifications
|
||||||
|
* if group_joined is already true.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import * as signalApi from "@link-stack/signal-api";
|
||||||
|
|
||||||
|
const logger = createLogger("check-group-membership");
|
||||||
|
|
||||||
|
const { Configuration, GroupsApi } = signalApi;
|
||||||
|
|
||||||
|
interface CheckGroupMembershipTaskOptions {
|
||||||
|
// Optional: Check specific group. If not provided, checks all groups with group_joined=false
|
||||||
|
groupId?: string;
|
||||||
|
botToken?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkGroupMembershipTask = async (
|
||||||
|
options: CheckGroupMembershipTaskOptions = {},
|
||||||
|
): Promise<void> => {
|
||||||
|
const config = new Configuration({
|
||||||
|
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||||
|
});
|
||||||
|
const groupsClient = new GroupsApi(config);
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
|
||||||
|
// Get all Signal bots
|
||||||
|
const bots = await db.selectFrom("SignalBot").selectAll().execute();
|
||||||
|
|
||||||
|
for (const bot of bots) {
|
||||||
|
try {
|
||||||
|
logger.debug(
|
||||||
|
{ botId: bot.id, phoneNumber: bot.phoneNumber },
|
||||||
|
"Checking groups for bot",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get all groups for this bot
|
||||||
|
const groups = await groupsClient.v1GroupsNumberGet({
|
||||||
|
number: bot.phoneNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{ botId: bot.id, groupCount: groups.length },
|
||||||
|
"Retrieved groups from Signal CLI",
|
||||||
|
);
|
||||||
|
|
||||||
|
// For each group, check if we have tickets waiting for members to join
|
||||||
|
for (const group of groups) {
|
||||||
|
if (!group.id || !group.internalId) {
|
||||||
|
logger.debug({ groupName: group.name }, "Skipping group without ID");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log info about each group temporarily for debugging
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
groupId: group.id,
|
||||||
|
groupName: group.name,
|
||||||
|
membersCount: group.members?.length || 0,
|
||||||
|
members: group.members,
|
||||||
|
pendingInvitesCount: group.pendingInvites?.length || 0,
|
||||||
|
pendingInvites: group.pendingInvites,
|
||||||
|
pendingRequestsCount: group.pendingRequests?.length || 0,
|
||||||
|
},
|
||||||
|
"Checking group membership",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Notify Zammad about each member who has joined
|
||||||
|
// This handles both cases:
|
||||||
|
// 1. New contacts who must accept invite (they move from pendingInvites to members)
|
||||||
|
// 2. Existing contacts who are auto-added (they appear directly in members)
|
||||||
|
if (group.members && group.members.length > 0) {
|
||||||
|
for (const memberPhone of group.members) {
|
||||||
|
// Check if this member was previously pending
|
||||||
|
// We'll send the webhook and let Zammad decide if it needs to update
|
||||||
|
await worker.addJob("common/notify-webhooks", {
|
||||||
|
backendId: bot.id,
|
||||||
|
payload: {
|
||||||
|
event: "group_member_joined",
|
||||||
|
group_id: group.id,
|
||||||
|
member_phone: memberPhone,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
groupId: group.id,
|
||||||
|
memberPhone,
|
||||||
|
},
|
||||||
|
"Notified Zammad about group member",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
botId: bot.id,
|
||||||
|
error: error.message,
|
||||||
|
stack: error.stack,
|
||||||
|
},
|
||||||
|
"Error checking group membership for bot",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Completed group membership check");
|
||||||
|
};
|
||||||
|
|
||||||
|
export default checkGroupMembershipTask;
|
||||||
72
apps/bridge-worker/tasks/common/notify-webhooks.ts
Normal file
72
apps/bridge-worker/tasks/common/notify-webhooks.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('notify-webhooks');
|
||||||
|
|
||||||
|
export interface NotifyWebhooksOptions {
|
||||||
|
backendId: string;
|
||||||
|
payload: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const notifyWebhooksTask = async (
|
||||||
|
options: NotifyWebhooksOptions,
|
||||||
|
): Promise<void> => {
|
||||||
|
const { backendId, payload } = options;
|
||||||
|
|
||||||
|
logger.debug({
|
||||||
|
backendId,
|
||||||
|
payloadKeys: Object.keys(payload),
|
||||||
|
}, 'Processing webhook notification');
|
||||||
|
|
||||||
|
const webhooks = await db
|
||||||
|
.selectFrom("Webhook")
|
||||||
|
.selectAll()
|
||||||
|
.where("backendId", "=", backendId)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
logger.debug({ count: webhooks.length, backendId }, 'Found webhooks');
|
||||||
|
|
||||||
|
for (const webhook of webhooks) {
|
||||||
|
const { endpointUrl, httpMethod, headers } = webhook;
|
||||||
|
const finalHeaders = { "Content-Type": "application/json", ...headers };
|
||||||
|
const body = JSON.stringify(payload);
|
||||||
|
|
||||||
|
logger.debug({
|
||||||
|
url: endpointUrl,
|
||||||
|
method: httpMethod,
|
||||||
|
bodyLength: body.length,
|
||||||
|
headerKeys: Object.keys(finalHeaders),
|
||||||
|
}, 'Sending webhook');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await fetch(endpointUrl, {
|
||||||
|
method: httpMethod,
|
||||||
|
headers: finalHeaders,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug({
|
||||||
|
url: endpointUrl,
|
||||||
|
status: result.status,
|
||||||
|
statusText: result.statusText,
|
||||||
|
ok: result.ok,
|
||||||
|
}, 'Webhook response');
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
const responseText = await result.text();
|
||||||
|
logger.error({
|
||||||
|
url: endpointUrl,
|
||||||
|
status: result.status,
|
||||||
|
responseSample: responseText.substring(0, 500),
|
||||||
|
}, 'Webhook error response');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error({
|
||||||
|
url: endpointUrl,
|
||||||
|
error: error instanceof Error ? error.message : error,
|
||||||
|
}, 'Webhook request failed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default notifyWebhooksTask;
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
|
||||||
|
interface ReceiveFacebookMessageTaskOptions {
|
||||||
|
message: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const receiveFacebookMessageTask = async ({
|
||||||
|
message,
|
||||||
|
}: ReceiveFacebookMessageTaskOptions): Promise<void> => {
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
|
||||||
|
for (const entry of message.entry) {
|
||||||
|
for (const messaging of entry.messaging) {
|
||||||
|
const pageId = messaging.recipient.id;
|
||||||
|
const row = await db
|
||||||
|
.selectFrom("FacebookBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("pageId", "=", pageId)
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
const backendId = row.id;
|
||||||
|
const payload = {
|
||||||
|
to: pageId,
|
||||||
|
from: messaging.sender.id,
|
||||||
|
sent_at: new Date(messaging.timestamp).toISOString(),
|
||||||
|
message: messaging.message.text,
|
||||||
|
message_id: messaging.message.mid,
|
||||||
|
};
|
||||||
|
|
||||||
|
await worker.addJob("common/notify-webhooks", { backendId, payload });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default receiveFacebookMessageTask;
|
||||||
43
apps/bridge-worker/tasks/facebook/send-facebook-message.ts
Normal file
43
apps/bridge-worker/tasks/facebook/send-facebook-message.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('bridge-worker-send-facebook-message');
|
||||||
|
|
||||||
|
interface SendFacebookMessageTaskOptions {
|
||||||
|
token: string;
|
||||||
|
to: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sendFacebookMessageTask = async (
|
||||||
|
options: SendFacebookMessageTaskOptions,
|
||||||
|
): Promise<void> => {
|
||||||
|
const { token, to, message } = options;
|
||||||
|
const { pageId, pageAccessToken } = await db
|
||||||
|
.selectFrom("FacebookBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("token", "=", token)
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
|
||||||
|
const endpoint = `https://graph.facebook.com/v19.0/${pageId}/messages`;
|
||||||
|
|
||||||
|
const outgoingMessage = {
|
||||||
|
recipient: { id: to },
|
||||||
|
message: { text: message },
|
||||||
|
messaging_type: "RESPONSE",
|
||||||
|
access_token: pageAccessToken,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(outgoingMessage),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error({ error });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default sendFacebookMessageTask;
|
||||||
258
apps/bridge-worker/tasks/fetch-signal-messages.ts
Normal file
258
apps/bridge-worker/tasks/fetch-signal-messages.ts
Normal file
|
|
@ -0,0 +1,258 @@
|
||||||
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import * as signalApi from "@link-stack/signal-api";
|
||||||
|
|
||||||
|
const logger = createLogger("fetch-signal-messages");
|
||||||
|
|
||||||
|
const { Configuration, MessagesApi, AttachmentsApi } = signalApi;
|
||||||
|
const config = new Configuration({
|
||||||
|
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetchAttachments = async (attachments: any[] | undefined) => {
|
||||||
|
const formattedAttachments = [];
|
||||||
|
|
||||||
|
if (attachments) {
|
||||||
|
const attachmentsClient = new AttachmentsApi(config);
|
||||||
|
|
||||||
|
for (const att of attachments) {
|
||||||
|
const { id, contentType, filename: name } = att;
|
||||||
|
|
||||||
|
const blob = await attachmentsClient.v1AttachmentsAttachmentGet({
|
||||||
|
attachment: id,
|
||||||
|
});
|
||||||
|
const arrayBuffer = await blob.arrayBuffer();
|
||||||
|
const base64Attachment = Buffer.from(arrayBuffer).toString("base64");
|
||||||
|
|
||||||
|
// Generate default filename if not provided by Signal API
|
||||||
|
let defaultFilename = name;
|
||||||
|
if (!defaultFilename) {
|
||||||
|
// Check if id already has an extension
|
||||||
|
const hasExtension = id.includes(".");
|
||||||
|
if (hasExtension) {
|
||||||
|
// ID already includes extension
|
||||||
|
defaultFilename = id;
|
||||||
|
} else {
|
||||||
|
// Add extension based on content type
|
||||||
|
const extension = contentType?.split("/")[1] || "bin";
|
||||||
|
defaultFilename = `${id}.${extension}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const formattedAttachment = {
|
||||||
|
filename: defaultFilename,
|
||||||
|
mimeType: contentType,
|
||||||
|
attachment: base64Attachment,
|
||||||
|
};
|
||||||
|
|
||||||
|
formattedAttachments.push(formattedAttachment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return formattedAttachments;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ProcessMessageArgs = {
|
||||||
|
id: string;
|
||||||
|
phoneNumber: string;
|
||||||
|
message: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
const processMessage = async ({
|
||||||
|
id,
|
||||||
|
phoneNumber,
|
||||||
|
message: msg,
|
||||||
|
}: ProcessMessageArgs): Promise<Record<string, any>[]> => {
|
||||||
|
const { envelope } = msg;
|
||||||
|
const { source, sourceUuid, dataMessage, syncMessage, receiptMessage, typingMessage } =
|
||||||
|
envelope;
|
||||||
|
|
||||||
|
// Log all envelope types to understand what events we're receiving
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
source,
|
||||||
|
sourceUuid,
|
||||||
|
hasDataMessage: !!dataMessage,
|
||||||
|
hasSyncMessage: !!syncMessage,
|
||||||
|
hasReceiptMessage: !!receiptMessage,
|
||||||
|
hasTypingMessage: !!typingMessage,
|
||||||
|
envelopeKeys: Object.keys(envelope),
|
||||||
|
},
|
||||||
|
"Received Signal envelope",
|
||||||
|
);
|
||||||
|
|
||||||
|
const isGroup = !!(
|
||||||
|
dataMessage?.groupV2 ||
|
||||||
|
dataMessage?.groupContext ||
|
||||||
|
dataMessage?.groupInfo
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check if this is a group membership change event
|
||||||
|
const groupInfo = dataMessage?.groupInfo;
|
||||||
|
if (groupInfo) {
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
type: groupInfo.type,
|
||||||
|
groupId: groupInfo.groupId,
|
||||||
|
source,
|
||||||
|
groupInfoKeys: Object.keys(groupInfo),
|
||||||
|
fullGroupInfo: groupInfo,
|
||||||
|
},
|
||||||
|
"Received group info event",
|
||||||
|
);
|
||||||
|
|
||||||
|
// If user joined the group, notify Zammad
|
||||||
|
if (groupInfo.type === "JOIN" || groupInfo.type === "JOINED") {
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
const groupId = groupInfo.groupId
|
||||||
|
? `group.${Buffer.from(groupInfo.groupId).toString("base64")}`
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (groupId) {
|
||||||
|
await worker.addJob("common/notify-webhooks", {
|
||||||
|
backendId: id,
|
||||||
|
payload: {
|
||||||
|
event: "group_member_joined",
|
||||||
|
group_id: groupId,
|
||||||
|
member_phone: source,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
groupId,
|
||||||
|
memberPhone: source,
|
||||||
|
},
|
||||||
|
"User joined Signal group, notifying Zammad",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dataMessage) return [];
|
||||||
|
|
||||||
|
const { attachments } = dataMessage;
|
||||||
|
const rawTimestamp = dataMessage?.timestamp;
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
sourceUuid,
|
||||||
|
source,
|
||||||
|
rawTimestamp,
|
||||||
|
hasGroupV2: !!dataMessage?.groupV2,
|
||||||
|
hasGroupContext: !!dataMessage?.groupContext,
|
||||||
|
hasGroupInfo: !!dataMessage?.groupInfo,
|
||||||
|
isGroup,
|
||||||
|
groupV2Id: dataMessage?.groupV2?.id,
|
||||||
|
groupContextType: dataMessage?.groupContext?.type,
|
||||||
|
groupInfoType: dataMessage?.groupInfo?.type,
|
||||||
|
},
|
||||||
|
"Processing message",
|
||||||
|
);
|
||||||
|
const timestamp = new Date(rawTimestamp);
|
||||||
|
|
||||||
|
const formattedAttachments = await fetchAttachments(attachments);
|
||||||
|
const primaryAttachment = formattedAttachments[0] ?? {};
|
||||||
|
const additionalAttachments = formattedAttachments.slice(1);
|
||||||
|
|
||||||
|
const groupId =
|
||||||
|
dataMessage?.groupV2?.id ||
|
||||||
|
dataMessage?.groupContext?.id ||
|
||||||
|
dataMessage?.groupInfo?.groupId;
|
||||||
|
const toRecipient = groupId
|
||||||
|
? `group.${Buffer.from(groupId).toString("base64")}`
|
||||||
|
: phoneNumber;
|
||||||
|
|
||||||
|
const primaryMessage = {
|
||||||
|
token: id,
|
||||||
|
to: toRecipient,
|
||||||
|
from: source,
|
||||||
|
messageId: `${sourceUuid}-${rawTimestamp}`,
|
||||||
|
message: dataMessage?.message,
|
||||||
|
sentAt: timestamp.toISOString(),
|
||||||
|
attachment: primaryAttachment.attachment,
|
||||||
|
filename: primaryAttachment.filename,
|
||||||
|
mimeType: primaryAttachment.mimeType,
|
||||||
|
isGroup,
|
||||||
|
};
|
||||||
|
const formattedMessages = [primaryMessage];
|
||||||
|
|
||||||
|
let count = 1;
|
||||||
|
for (const attachment of additionalAttachments) {
|
||||||
|
const additionalMessage = {
|
||||||
|
...primaryMessage,
|
||||||
|
...attachment,
|
||||||
|
message: attachment.filename,
|
||||||
|
messageId: `${sourceUuid}-${count}-${rawTimestamp}`,
|
||||||
|
};
|
||||||
|
formattedMessages.push(additionalMessage);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return formattedMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
interface FetchSignalMessagesTaskOptions {
|
||||||
|
scheduleTasks: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchSignalMessagesTask = async ({
|
||||||
|
scheduleTasks = "false",
|
||||||
|
}: FetchSignalMessagesTaskOptions): Promise<void> => {
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
|
||||||
|
if (scheduleTasks === "true") {
|
||||||
|
// because cron only has minimum 1 minute resolution
|
||||||
|
for (const offset of [15000, 30000, 45000]) {
|
||||||
|
await worker.addJob(
|
||||||
|
"fetch-signal-messages",
|
||||||
|
{ scheduleTasks: "false" },
|
||||||
|
{
|
||||||
|
maxAttempts: 1,
|
||||||
|
runAt: new Date(Date.now() + offset),
|
||||||
|
jobKey: `fetchSignalMessages-${offset}`,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const messagesClient = new MessagesApi(config);
|
||||||
|
const rows = await db.selectFrom("SignalBot").selectAll().execute();
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
const { id, phoneNumber } = row;
|
||||||
|
const messages = await messagesClient.v1ReceiveNumberGet({
|
||||||
|
number: phoneNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug({ botId: id, phoneNumber }, "Fetching messages for bot");
|
||||||
|
|
||||||
|
for (const message of messages) {
|
||||||
|
const formattedMessages = await processMessage({
|
||||||
|
id,
|
||||||
|
phoneNumber,
|
||||||
|
message,
|
||||||
|
});
|
||||||
|
for (const formattedMessage of formattedMessages) {
|
||||||
|
if (formattedMessage.to !== formattedMessage.from) {
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
messageId: formattedMessage.messageId,
|
||||||
|
from: formattedMessage.from,
|
||||||
|
to: formattedMessage.to,
|
||||||
|
isGroup: formattedMessage.isGroup,
|
||||||
|
hasMessage: !!formattedMessage.message,
|
||||||
|
hasAttachment: !!formattedMessage.attachment,
|
||||||
|
},
|
||||||
|
"Creating job for message",
|
||||||
|
);
|
||||||
|
|
||||||
|
await worker.addJob("signal/receive-signal-message", formattedMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default fetchSignalMessagesTask;
|
||||||
436
apps/bridge-worker/tasks/formstack/create-ticket-from-form.ts
Normal file
436
apps/bridge-worker/tasks/formstack/create-ticket-from-form.ts
Normal file
|
|
@ -0,0 +1,436 @@
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { Zammad, getUser, sanitizePhoneNumber } from "../../lib/zammad.js";
|
||||||
|
import {
|
||||||
|
loadFieldMapping,
|
||||||
|
getFieldValue,
|
||||||
|
getNestedFieldValue,
|
||||||
|
formatFieldValue,
|
||||||
|
buildTicketTitle,
|
||||||
|
getZammadFieldValues,
|
||||||
|
type FieldMappingConfig,
|
||||||
|
} from "../../lib/formstack-field-mapping.js";
|
||||||
|
|
||||||
|
const logger = createLogger("create-ticket-from-form");
|
||||||
|
|
||||||
|
export interface CreateTicketFromFormOptions {
|
||||||
|
formData: any;
|
||||||
|
receivedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const createTicketFromFormTask = async (
|
||||||
|
options: CreateTicketFromFormOptions,
|
||||||
|
): Promise<void> => {
|
||||||
|
const { formData, receivedAt } = options;
|
||||||
|
|
||||||
|
// Load field mapping configuration
|
||||||
|
const mapping = loadFieldMapping();
|
||||||
|
|
||||||
|
// Log only non-PII metadata using configured field names
|
||||||
|
const formId = getFieldValue(formData, "formId", mapping);
|
||||||
|
const uniqueId = getFieldValue(formData, "uniqueId", mapping);
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
formId,
|
||||||
|
uniqueId,
|
||||||
|
receivedAt,
|
||||||
|
fieldCount: Object.keys(formData).length,
|
||||||
|
},
|
||||||
|
"Processing Formstack form submission",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Extract fields using dynamic mapping
|
||||||
|
const nameField = getFieldValue(formData, "name", mapping);
|
||||||
|
const firstName = mapping.nestedFields?.name?.firstNamePath
|
||||||
|
? getNestedFieldValue(nameField, mapping.nestedFields.name.firstNamePath) || ""
|
||||||
|
: "";
|
||||||
|
const lastName = mapping.nestedFields?.name?.lastNamePath
|
||||||
|
? getNestedFieldValue(nameField, mapping.nestedFields.name.lastNamePath) || ""
|
||||||
|
: "";
|
||||||
|
const fullName =
|
||||||
|
firstName && lastName
|
||||||
|
? `${firstName} ${lastName}`.trim()
|
||||||
|
: firstName || lastName || "Unknown";
|
||||||
|
|
||||||
|
// Extract well-known fields used for special logic (all optional)
|
||||||
|
const email = getFieldValue(formData, "email", mapping);
|
||||||
|
const rawPhone = getFieldValue(formData, "phone", mapping);
|
||||||
|
const rawSignalAccount = getFieldValue(formData, "signalAccount", mapping);
|
||||||
|
const organization = getFieldValue(formData, "organization", mapping);
|
||||||
|
const typeOfSupport = getFieldValue(formData, "typeOfSupport", mapping);
|
||||||
|
const descriptionOfIssue = getFieldValue(formData, "descriptionOfIssue", mapping);
|
||||||
|
|
||||||
|
// Sanitize phone numbers to E.164 format (+15554446666)
|
||||||
|
let phone: string | undefined;
|
||||||
|
if (rawPhone) {
|
||||||
|
try {
|
||||||
|
phone = sanitizePhoneNumber(rawPhone);
|
||||||
|
logger.info({ rawPhone, sanitized: phone }, "Sanitized phone number");
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.warn({ rawPhone, error: error.message }, "Invalid phone number format, ignoring");
|
||||||
|
phone = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let signalAccount: string | undefined;
|
||||||
|
if (rawSignalAccount) {
|
||||||
|
try {
|
||||||
|
signalAccount = sanitizePhoneNumber(rawSignalAccount);
|
||||||
|
logger.info({ rawSignalAccount, sanitized: signalAccount }, "Sanitized signal account");
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.warn({ rawSignalAccount, error: error.message }, "Invalid signal account format, ignoring");
|
||||||
|
signalAccount = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate that at least one contact method is provided
|
||||||
|
if (!email && !phone && !signalAccount) {
|
||||||
|
logger.error(
|
||||||
|
{ formId, uniqueId },
|
||||||
|
"No contact information provided - at least one of email, phone, or signalAccount is required",
|
||||||
|
);
|
||||||
|
throw new Error(
|
||||||
|
"At least one contact method (email, phone, or signalAccount) is required for ticket creation",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build ticket title using configured template
|
||||||
|
// Pass all potentially used fields - the template determines which are actually used
|
||||||
|
const title = buildTicketTitle(mapping, {
|
||||||
|
name: fullName,
|
||||||
|
organization: formatFieldValue(organization),
|
||||||
|
typeOfSupport: formatFieldValue(typeOfSupport),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Build article body - format all fields as HTML
|
||||||
|
const formatAllFields = (data: any): string => {
|
||||||
|
let html = "";
|
||||||
|
|
||||||
|
// Add formatted name field first if we have it
|
||||||
|
if (fullName && fullName !== "Unknown") {
|
||||||
|
html += `<strong>Name:</strong><br>${fullName}<br>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(data)) {
|
||||||
|
// Skip metadata fields and name field (we already formatted it above)
|
||||||
|
const skipFields = [
|
||||||
|
mapping.sourceFields.formId,
|
||||||
|
mapping.sourceFields.uniqueId,
|
||||||
|
mapping.sourceFields.name, // Skip raw name field
|
||||||
|
"HandshakeKey",
|
||||||
|
].filter(Boolean);
|
||||||
|
|
||||||
|
if (skipFields.includes(key)) continue;
|
||||||
|
if (value === null || value === undefined || value === "") continue;
|
||||||
|
|
||||||
|
const displayValue = Array.isArray(value)
|
||||||
|
? value.join(", ")
|
||||||
|
: typeof value === "object"
|
||||||
|
? JSON.stringify(value)
|
||||||
|
: value;
|
||||||
|
html += `<strong>${key}:</strong><br>${displayValue}<br>`;
|
||||||
|
}
|
||||||
|
return html;
|
||||||
|
};
|
||||||
|
|
||||||
|
const body = formatAllFields(formData);
|
||||||
|
|
||||||
|
// Get Zammad configuration from environment
|
||||||
|
const zammadUrl = process.env.ZAMMAD_URL || "http://zammad-nginx:8080";
|
||||||
|
const zammadToken = process.env.ZAMMAD_API_TOKEN;
|
||||||
|
|
||||||
|
if (!zammadToken) {
|
||||||
|
logger.error("ZAMMAD_API_TOKEN environment variable is not configured");
|
||||||
|
throw new Error("ZAMMAD_API_TOKEN is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
const zammad = Zammad({ token: zammadToken }, zammadUrl);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Look up the configured article type
|
||||||
|
let articleTypeId: number | undefined;
|
||||||
|
try {
|
||||||
|
const articleTypes = await zammad.get("ticket_article_types");
|
||||||
|
const configuredType = articleTypes.find(
|
||||||
|
(t: any) => t.name === mapping.ticket.defaultArticleType,
|
||||||
|
);
|
||||||
|
articleTypeId = configuredType?.id;
|
||||||
|
if (articleTypeId) {
|
||||||
|
logger.info(
|
||||||
|
{ articleTypeId, typeName: mapping.ticket.defaultArticleType },
|
||||||
|
"Found configured article type",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
{ typeName: mapping.ticket.defaultArticleType },
|
||||||
|
"Configured article type not found, ticket will use default type",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.warn({ error: error.message }, "Failed to look up article type");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get or create user
|
||||||
|
// Try to find existing user by: phone -> email
|
||||||
|
// Note: We can't search by Signal account since Signal group IDs aren't phone numbers
|
||||||
|
let customer;
|
||||||
|
|
||||||
|
// Try phone if provided
|
||||||
|
if (phone) {
|
||||||
|
customer = await getUser(zammad, phone);
|
||||||
|
if (customer) {
|
||||||
|
logger.info(
|
||||||
|
{ customerId: customer.id, method: "phone" },
|
||||||
|
"Found existing user by phone",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to email if no customer found yet
|
||||||
|
if (!customer && email) {
|
||||||
|
// Validate email format before using in search
|
||||||
|
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||||
|
if (emailRegex.test(email)) {
|
||||||
|
const emailResults = await zammad.user.search(`email:${email}`);
|
||||||
|
if (emailResults.length > 0) {
|
||||||
|
customer = emailResults[0];
|
||||||
|
logger.info(
|
||||||
|
{ customerId: customer.id, method: "email" },
|
||||||
|
"Found existing user by email",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn({ email }, "Invalid email format provided, skipping email search");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!customer) {
|
||||||
|
// Create new user
|
||||||
|
logger.info("Creating new user from form submission");
|
||||||
|
|
||||||
|
// Build user data with whatever contact info we have
|
||||||
|
const userData: any = {
|
||||||
|
firstname: firstName,
|
||||||
|
lastname: lastName,
|
||||||
|
roles: ["Customer"],
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add contact info only if provided
|
||||||
|
if (email) {
|
||||||
|
userData.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use phone number if provided (don't use Signal group ID as phone)
|
||||||
|
if (phone) {
|
||||||
|
userData.phone = phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
customer = await zammad.user.create(userData);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
customerId: customer.id,
|
||||||
|
email: customer.email,
|
||||||
|
},
|
||||||
|
"Using customer for ticket",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Look up the configured group
|
||||||
|
const groups = await zammad.get("groups");
|
||||||
|
const targetGroup = groups.find((g: any) => g.name === mapping.ticket.group);
|
||||||
|
|
||||||
|
if (!targetGroup) {
|
||||||
|
logger.error({ groupName: mapping.ticket.group }, "Configured group not found");
|
||||||
|
throw new Error(`Zammad group "${mapping.ticket.group}" not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{ groupId: targetGroup.id, groupName: targetGroup.name },
|
||||||
|
"Using configured group",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Build custom fields using Zammad field mapping
|
||||||
|
// This dynamically maps all configured fields without hardcoding
|
||||||
|
const customFields = getZammadFieldValues(formData, mapping);
|
||||||
|
|
||||||
|
// Check if this is a Signal ticket
|
||||||
|
let signalArticleType = null;
|
||||||
|
let signalChannelId = null;
|
||||||
|
let signalBotToken = null;
|
||||||
|
|
||||||
|
if (signalAccount) {
|
||||||
|
try {
|
||||||
|
logger.info({ signalAccount }, "Looking up Signal channel and article type");
|
||||||
|
|
||||||
|
// Look up Signal channels from Zammad (admin-only endpoint)
|
||||||
|
// Note: bot_token is NOT included in this response for security reasons
|
||||||
|
const channels = await zammad.get("cdr_signal_channels");
|
||||||
|
if (channels.length > 0) {
|
||||||
|
const zammadChannel = channels[0]; // Use first active Signal channel
|
||||||
|
signalChannelId = zammadChannel.id;
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
channelId: zammadChannel.id,
|
||||||
|
phoneNumber: zammadChannel.phone_number,
|
||||||
|
},
|
||||||
|
"Found active Signal channel from Zammad",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Look up the bot_token from our own cdr database using the phone number
|
||||||
|
const signalBot = await db
|
||||||
|
.selectFrom("SignalBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("phoneNumber", "=", zammadChannel.phone_number)
|
||||||
|
.executeTakeFirst();
|
||||||
|
|
||||||
|
if (signalBot) {
|
||||||
|
signalBotToken = signalBot.token;
|
||||||
|
logger.info(
|
||||||
|
{ botId: signalBot.id, phoneNumber: signalBot.phoneNumber },
|
||||||
|
"Found Signal bot token from cdr database",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
{ phoneNumber: zammadChannel.phone_number },
|
||||||
|
"Signal bot not found in cdr database",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn("No active Signal channels found");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look up cdr_signal article type
|
||||||
|
const articleTypes = await zammad.get("ticket_article_types");
|
||||||
|
signalArticleType = articleTypes.find((t: any) => t.name === "cdr_signal");
|
||||||
|
|
||||||
|
if (!signalArticleType) {
|
||||||
|
logger.warn("Signal article type (cdr_signal) not found, using default type");
|
||||||
|
} else {
|
||||||
|
logger.info(
|
||||||
|
{ articleTypeId: signalArticleType.id },
|
||||||
|
"Found Signal article type",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.warn(
|
||||||
|
{ error: error.message },
|
||||||
|
"Failed to look up Signal article type, creating regular ticket",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the ticket
|
||||||
|
const articleData: any = {
|
||||||
|
subject: descriptionOfIssue || "Support Request",
|
||||||
|
body,
|
||||||
|
content_type: "text/html",
|
||||||
|
internal: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Use Signal article type if available, otherwise use configured default
|
||||||
|
if (signalArticleType) {
|
||||||
|
articleData.type_id = signalArticleType.id;
|
||||||
|
logger.info({ typeId: signalArticleType.id }, "Using Signal article type");
|
||||||
|
|
||||||
|
// IMPORTANT: Set sender to "Customer" for Signal tickets created from Formstack
|
||||||
|
// This prevents the article from being echoed back to the user via Signal
|
||||||
|
// (enqueue_communicate_cdr_signal_job only sends if sender != 'Customer')
|
||||||
|
articleData.sender = "Customer";
|
||||||
|
} else if (articleTypeId) {
|
||||||
|
articleData.type_id = articleTypeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ticketData: any = {
|
||||||
|
title,
|
||||||
|
group_id: targetGroup.id,
|
||||||
|
customer_id: customer.id,
|
||||||
|
article: articleData,
|
||||||
|
...customFields,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add Signal preferences if we have Signal channel and article type
|
||||||
|
// Note: signalAccount from Formstack is the phone number the user typed in
|
||||||
|
// Groups are added later via update_group webhook from bridge-worker
|
||||||
|
if (signalChannelId && signalBotToken && signalArticleType && signalAccount) {
|
||||||
|
ticketData.preferences = {
|
||||||
|
channel_id: signalChannelId,
|
||||||
|
cdr_signal: {
|
||||||
|
bot_token: signalBotToken,
|
||||||
|
chat_id: signalAccount, // Use Signal phone number as chat_id
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
channelId: signalChannelId,
|
||||||
|
chatId: signalAccount,
|
||||||
|
},
|
||||||
|
"Adding Signal preferences to ticket",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
title,
|
||||||
|
groupId: targetGroup.id,
|
||||||
|
customerId: customer.id,
|
||||||
|
hasArticleType: !!articleTypeId || !!signalArticleType,
|
||||||
|
isSignalTicket: !!signalArticleType && !!signalAccount,
|
||||||
|
customFieldCount: Object.keys(customFields).length,
|
||||||
|
},
|
||||||
|
"Creating ticket",
|
||||||
|
);
|
||||||
|
|
||||||
|
const ticket = await zammad.ticket.create(ticketData);
|
||||||
|
|
||||||
|
// Set create_article_type_id for Signal tickets to enable proper replies
|
||||||
|
if (signalArticleType && signalChannelId) {
|
||||||
|
try {
|
||||||
|
await zammad.ticket.update(ticket.id, {
|
||||||
|
create_article_type_id: signalArticleType.id,
|
||||||
|
});
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
ticketId: ticket.id,
|
||||||
|
articleTypeId: signalArticleType.id,
|
||||||
|
},
|
||||||
|
"Set create_article_type_id for Signal ticket",
|
||||||
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.warn(
|
||||||
|
{
|
||||||
|
error: error.message,
|
||||||
|
ticketId: ticket.id,
|
||||||
|
},
|
||||||
|
"Failed to set create_article_type_id, ticket may not support Signal replies",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
ticketId: ticket.id,
|
||||||
|
ticketNumber: ticket.id,
|
||||||
|
title,
|
||||||
|
isSignalTicket: !!signalChannelId,
|
||||||
|
},
|
||||||
|
"Successfully created ticket from Formstack submission",
|
||||||
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
error: error.message,
|
||||||
|
stack: error.stack,
|
||||||
|
formId,
|
||||||
|
uniqueId,
|
||||||
|
},
|
||||||
|
"Failed to create ticket from Formstack submission",
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default createTicketFromFormTask;
|
||||||
227
apps/bridge-worker/tasks/signal/receive-signal-message.ts
Normal file
227
apps/bridge-worker/tasks/signal/receive-signal-message.ts
Normal file
|
|
@ -0,0 +1,227 @@
|
||||||
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import * as signalApi from "@link-stack/signal-api";
|
||||||
|
const { Configuration, GroupsApi } = signalApi;
|
||||||
|
|
||||||
|
const logger = createLogger('bridge-worker-receive-signal-message');
|
||||||
|
|
||||||
|
interface ReceiveSignalMessageTaskOptions {
|
||||||
|
token: string;
|
||||||
|
to: string;
|
||||||
|
from: string;
|
||||||
|
messageId: string;
|
||||||
|
sentAt: string;
|
||||||
|
message: string;
|
||||||
|
attachment?: string;
|
||||||
|
filename?: string;
|
||||||
|
mimeType?: string;
|
||||||
|
isGroup?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const receiveSignalMessageTask = async ({
|
||||||
|
token,
|
||||||
|
to,
|
||||||
|
from,
|
||||||
|
messageId,
|
||||||
|
sentAt,
|
||||||
|
message,
|
||||||
|
attachment,
|
||||||
|
filename,
|
||||||
|
mimeType,
|
||||||
|
isGroup,
|
||||||
|
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
|
||||||
|
logger.debug({
|
||||||
|
messageId,
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
isGroup,
|
||||||
|
hasMessage: !!message,
|
||||||
|
hasAttachment: !!attachment,
|
||||||
|
token,
|
||||||
|
}, 'Processing incoming message');
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
const row = await db
|
||||||
|
.selectFrom("SignalBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("id", "=", token)
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
|
||||||
|
const backendId = row.id;
|
||||||
|
let finalTo = to;
|
||||||
|
let createdInternalId: string | undefined;
|
||||||
|
|
||||||
|
// Check if auto-group creation is enabled and this is NOT already a group message
|
||||||
|
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
|
||||||
|
|
||||||
|
logger.debug({
|
||||||
|
enableAutoGroups,
|
||||||
|
isGroup,
|
||||||
|
shouldCreateGroup: enableAutoGroups && !isGroup && from && to,
|
||||||
|
}, 'Auto-groups config');
|
||||||
|
|
||||||
|
// If this is already a group message and auto-groups is enabled,
|
||||||
|
// use group provided in 'to'
|
||||||
|
if (enableAutoGroups && isGroup && to) {
|
||||||
|
// Signal sends the internal ID (base64) in group messages
|
||||||
|
// We should NOT add "group." prefix - that's for sending messages, not receiving
|
||||||
|
logger.debug('Message is from existing group with internal ID');
|
||||||
|
|
||||||
|
finalTo = to;
|
||||||
|
} else if (enableAutoGroups && !isGroup && from && to) {
|
||||||
|
try {
|
||||||
|
const config = new Configuration({
|
||||||
|
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||||
|
});
|
||||||
|
const groupsClient = new GroupsApi(config);
|
||||||
|
|
||||||
|
// Always create a new group for direct messages to the helpdesk
|
||||||
|
// This ensures each conversation gets its own group/ticket
|
||||||
|
logger.info({ from }, 'Creating new group for user');
|
||||||
|
|
||||||
|
// Include timestamp to make each group unique
|
||||||
|
const timestamp = new Date()
|
||||||
|
.toISOString()
|
||||||
|
.replace(/[:.]/g, "-")
|
||||||
|
.substring(0, 19);
|
||||||
|
const groupName = `Support: ${from} (${timestamp})`;
|
||||||
|
|
||||||
|
// Create new group for this conversation
|
||||||
|
const createGroupResponse = await groupsClient.v1GroupsNumberPost({
|
||||||
|
number: row.phoneNumber,
|
||||||
|
data: {
|
||||||
|
name: groupName,
|
||||||
|
members: [from],
|
||||||
|
description: "Private support conversation",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug({ createGroupResponse }, 'Group creation response from Signal API');
|
||||||
|
|
||||||
|
if (createGroupResponse.id) {
|
||||||
|
// The createGroupResponse.id already contains the full group identifier (group.BASE64)
|
||||||
|
finalTo = createGroupResponse.id;
|
||||||
|
|
||||||
|
// Fetch the group details to get the actual internalId
|
||||||
|
// The base64 part of the ID is NOT the same as the internalId!
|
||||||
|
try {
|
||||||
|
logger.debug('Fetching group details to get internalId');
|
||||||
|
const groups = await groupsClient.v1GroupsNumberGet({
|
||||||
|
number: row.phoneNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug({ groupsSample: groups.slice(0, 3) }, 'Groups for bot');
|
||||||
|
|
||||||
|
const createdGroup = groups.find((g) => g.id === finalTo);
|
||||||
|
if (createdGroup) {
|
||||||
|
logger.debug({ createdGroup }, 'Found created group details');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (createdGroup && createdGroup.internalId) {
|
||||||
|
createdInternalId = createdGroup.internalId;
|
||||||
|
logger.debug({ createdInternalId }, 'Got actual internalId');
|
||||||
|
} else {
|
||||||
|
// Fallback: extract base64 part from ID
|
||||||
|
if (finalTo.startsWith("group.")) {
|
||||||
|
createdInternalId = finalTo.substring(6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (fetchError) {
|
||||||
|
logger.debug('Could not fetch group details, using ID base64 part');
|
||||||
|
// Fallback: extract base64 part from ID
|
||||||
|
if (finalTo.startsWith("group.")) {
|
||||||
|
createdInternalId = finalTo.substring(6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug({
|
||||||
|
fullGroupId: finalTo,
|
||||||
|
internalId: createdInternalId,
|
||||||
|
}, 'Group created successfully');
|
||||||
|
logger.debug({
|
||||||
|
groupId: finalTo,
|
||||||
|
internalId: createdInternalId,
|
||||||
|
groupName,
|
||||||
|
forPhoneNumber: from,
|
||||||
|
botNumber: row.phoneNumber,
|
||||||
|
response: createGroupResponse,
|
||||||
|
}, 'Created new Signal group');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now handle notifications and message forwarding for both new and existing groups
|
||||||
|
if (finalTo && finalTo.startsWith("group.")) {
|
||||||
|
// Forward the user's initial message to the group using quote feature
|
||||||
|
try {
|
||||||
|
logger.debug('Forwarding initial message to group using quote feature');
|
||||||
|
|
||||||
|
const attributionMessage = `Message from ${from}:\n"${message}"\n\n---\nSupport team: Your request has been received. An agent will respond shortly.`;
|
||||||
|
|
||||||
|
await worker.addJob("signal/send-signal-message", {
|
||||||
|
token: row.token,
|
||||||
|
to: finalTo,
|
||||||
|
message: attributionMessage,
|
||||||
|
conversationId: null,
|
||||||
|
quoteMessage: message,
|
||||||
|
quoteAuthor: from,
|
||||||
|
quoteTimestamp: Date.parse(sentAt),
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug({ finalTo }, 'Successfully forwarded initial message to group');
|
||||||
|
} catch (forwardError) {
|
||||||
|
logger.error({ error: forwardError }, 'Error forwarding message to group');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send a response to the original DM informing about the group
|
||||||
|
try {
|
||||||
|
logger.debug('Sending group notification to original DM');
|
||||||
|
|
||||||
|
const dmNotification = `Hello! A private support group has been created for your conversation.\n\nGroup name: ${groupName}\n\nPlease look for the new group in your Signal app to continue the conversation. Our support team will respond there shortly.\n\nThank you for contacting support!`;
|
||||||
|
|
||||||
|
await worker.addJob("signal/send-signal-message", {
|
||||||
|
token: row.token,
|
||||||
|
to: from,
|
||||||
|
message: dmNotification,
|
||||||
|
conversationId: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug('Successfully sent group notification to user DM');
|
||||||
|
} catch (dmError) {
|
||||||
|
logger.error({ error: dmError }, 'Error sending DM notification');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
// Check if error is because group already exists
|
||||||
|
const errorMessage =
|
||||||
|
error?.response?.data?.error || error?.message || error;
|
||||||
|
const isAlreadyExists =
|
||||||
|
errorMessage?.toString().toLowerCase().includes("already") ||
|
||||||
|
errorMessage?.toString().toLowerCase().includes("exists");
|
||||||
|
|
||||||
|
if (isAlreadyExists) {
|
||||||
|
logger.debug({ from }, 'Group might already exist, continuing with original recipient');
|
||||||
|
} else {
|
||||||
|
logger.error({
|
||||||
|
error: errorMessage,
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
botNumber: row.phoneNumber,
|
||||||
|
}, 'Error creating Signal group');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
to: finalTo,
|
||||||
|
from,
|
||||||
|
message_id: messageId,
|
||||||
|
sent_at: sentAt,
|
||||||
|
message,
|
||||||
|
attachment,
|
||||||
|
filename,
|
||||||
|
mime_type: mimeType,
|
||||||
|
is_group: finalTo.startsWith("group"),
|
||||||
|
};
|
||||||
|
|
||||||
|
await worker.addJob("common/notify-webhooks", { backendId, payload });
|
||||||
|
};
|
||||||
|
|
||||||
|
export default receiveSignalMessageTask;
|
||||||
313
apps/bridge-worker/tasks/signal/send-signal-message.ts
Normal file
313
apps/bridge-worker/tasks/signal/send-signal-message.ts
Normal file
|
|
@ -0,0 +1,313 @@
|
||||||
|
import {
|
||||||
|
db,
|
||||||
|
getWorkerUtils,
|
||||||
|
getMaxAttachmentSize,
|
||||||
|
getMaxTotalAttachmentSize,
|
||||||
|
MAX_ATTACHMENTS,
|
||||||
|
buildSignalGroupName,
|
||||||
|
} from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
import * as signalApi from "@link-stack/signal-api";
|
||||||
|
const { Configuration, MessagesApi, GroupsApi } = signalApi;
|
||||||
|
|
||||||
|
const logger = createLogger("bridge-worker-send-signal-message");
|
||||||
|
|
||||||
|
interface SendSignalMessageTaskOptions {
|
||||||
|
token: string;
|
||||||
|
to: string;
|
||||||
|
message: any;
|
||||||
|
conversationId?: string; // Zammad ticket/conversation ID for callback
|
||||||
|
quoteMessage?: string; // Optional: message text to quote
|
||||||
|
quoteAuthor?: string; // Optional: author of quoted message (phone number)
|
||||||
|
quoteTimestamp?: number; // Optional: timestamp of quoted message in milliseconds
|
||||||
|
attachments?: Array<{
|
||||||
|
data: string; // base64
|
||||||
|
filename: string;
|
||||||
|
mime_type: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sendSignalMessageTask = async ({
|
||||||
|
token,
|
||||||
|
to,
|
||||||
|
message,
|
||||||
|
conversationId,
|
||||||
|
quoteMessage,
|
||||||
|
quoteAuthor,
|
||||||
|
quoteTimestamp,
|
||||||
|
attachments,
|
||||||
|
}: SendSignalMessageTaskOptions): Promise<void> => {
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
token,
|
||||||
|
to,
|
||||||
|
conversationId,
|
||||||
|
messageLength: message?.length,
|
||||||
|
},
|
||||||
|
"Processing outgoing message",
|
||||||
|
);
|
||||||
|
const bot = await db
|
||||||
|
.selectFrom("SignalBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("token", "=", token)
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
|
||||||
|
const { phoneNumber: number } = bot;
|
||||||
|
const config = new Configuration({
|
||||||
|
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||||
|
});
|
||||||
|
const messagesClient = new MessagesApi(config);
|
||||||
|
const groupsClient = new GroupsApi(config);
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
|
||||||
|
let finalTo = to;
|
||||||
|
let groupCreated = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check if 'to' is a group ID (UUID format, group.base64 format, or base64) vs phone number
|
||||||
|
const isUUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
||||||
|
to,
|
||||||
|
);
|
||||||
|
const isGroupPrefix = to.startsWith("group.");
|
||||||
|
const isBase64 = /^[A-Za-z0-9+/]+=*$/.test(to) && to.length > 20; // Base64 internal_id
|
||||||
|
const isGroupId = isUUID || isGroupPrefix || isBase64;
|
||||||
|
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
to,
|
||||||
|
isGroupId,
|
||||||
|
enableAutoGroups,
|
||||||
|
shouldCreateGroup: enableAutoGroups && !isGroupId && to && conversationId,
|
||||||
|
},
|
||||||
|
"Recipient analysis",
|
||||||
|
);
|
||||||
|
|
||||||
|
// If sending to a phone number and auto-groups is enabled, create a group first
|
||||||
|
if (enableAutoGroups && !isGroupId && to && conversationId) {
|
||||||
|
try {
|
||||||
|
const groupName = buildSignalGroupName(conversationId);
|
||||||
|
const createGroupResponse = await groupsClient.v1GroupsNumberPost({
|
||||||
|
number: bot.phoneNumber,
|
||||||
|
data: {
|
||||||
|
name: groupName,
|
||||||
|
members: [to],
|
||||||
|
description: "Private support conversation",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (createGroupResponse.id) {
|
||||||
|
// The createGroupResponse.id already contains the full group identifier (group.BASE64)
|
||||||
|
finalTo = createGroupResponse.id;
|
||||||
|
groupCreated = true;
|
||||||
|
|
||||||
|
// Fetch the group details to get the actual internalId
|
||||||
|
let internalId: string | undefined;
|
||||||
|
try {
|
||||||
|
const groups = await groupsClient.v1GroupsNumberGet({
|
||||||
|
number: bot.phoneNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
const createdGroup = groups.find((g) => g.id === finalTo);
|
||||||
|
if (createdGroup && createdGroup.internalId) {
|
||||||
|
internalId = createdGroup.internalId;
|
||||||
|
logger.debug({ internalId }, "Got actual internalId");
|
||||||
|
} else {
|
||||||
|
// Fallback: extract base64 part from ID
|
||||||
|
if (finalTo.startsWith("group.")) {
|
||||||
|
internalId = finalTo.substring(6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (fetchError) {
|
||||||
|
logger.debug("Could not fetch group details, using ID base64 part");
|
||||||
|
// Fallback: extract base64 part from ID
|
||||||
|
if (finalTo.startsWith("group.")) {
|
||||||
|
internalId = finalTo.substring(6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
groupId: finalTo,
|
||||||
|
internalId,
|
||||||
|
groupName,
|
||||||
|
conversationId,
|
||||||
|
originalRecipient: to,
|
||||||
|
botNumber: bot.phoneNumber,
|
||||||
|
},
|
||||||
|
"Created new Signal group",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Notify Zammad about the new group ID via webhook
|
||||||
|
// Set group_joined: false initially - will be updated when user accepts invitation
|
||||||
|
await worker.addJob("common/notify-webhooks", {
|
||||||
|
backendId: bot.id,
|
||||||
|
payload: {
|
||||||
|
event: "group_created",
|
||||||
|
conversation_id: conversationId,
|
||||||
|
original_recipient: to,
|
||||||
|
group_id: finalTo,
|
||||||
|
internal_group_id: internalId,
|
||||||
|
group_joined: false,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (groupError) {
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
error: groupError instanceof Error ? groupError.message : groupError,
|
||||||
|
to,
|
||||||
|
conversationId,
|
||||||
|
},
|
||||||
|
"Error creating Signal group",
|
||||||
|
);
|
||||||
|
// Continue with original recipient if group creation fails
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
fromNumber: number,
|
||||||
|
toRecipient: finalTo,
|
||||||
|
originalTo: to,
|
||||||
|
recipientChanged: to !== finalTo,
|
||||||
|
groupCreated,
|
||||||
|
isGroupRecipient: finalTo.startsWith("group."),
|
||||||
|
},
|
||||||
|
"Sending message via API",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Build the message data with optional quote parameters
|
||||||
|
const messageData: signalApi.ApiSendMessageV2 = {
|
||||||
|
number,
|
||||||
|
recipients: [finalTo],
|
||||||
|
message,
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
number,
|
||||||
|
recipients: [finalTo],
|
||||||
|
messageLength: message?.length,
|
||||||
|
hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp),
|
||||||
|
},
|
||||||
|
"Message data being sent",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add quote parameters if all are provided
|
||||||
|
if (quoteMessage && quoteAuthor && quoteTimestamp) {
|
||||||
|
messageData.quoteTimestamp = quoteTimestamp;
|
||||||
|
messageData.quoteAuthor = quoteAuthor;
|
||||||
|
messageData.quoteMessage = quoteMessage;
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
quoteAuthor,
|
||||||
|
quoteMessageLength: quoteMessage?.length,
|
||||||
|
quoteTimestamp,
|
||||||
|
},
|
||||||
|
"Including quote in message",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add attachments if provided with size validation
|
||||||
|
if (attachments && attachments.length > 0) {
|
||||||
|
const MAX_ATTACHMENT_SIZE = getMaxAttachmentSize();
|
||||||
|
const MAX_TOTAL_SIZE = getMaxTotalAttachmentSize();
|
||||||
|
|
||||||
|
if (attachments.length > MAX_ATTACHMENTS) {
|
||||||
|
throw new Error(
|
||||||
|
`Too many attachments: ${attachments.length} (max ${MAX_ATTACHMENTS})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let totalSize = 0;
|
||||||
|
const validatedAttachments = [];
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
// Calculate size from base64 string (rough estimate: length * 3/4)
|
||||||
|
const estimatedSize = (attachment.data.length * 3) / 4;
|
||||||
|
|
||||||
|
if (estimatedSize > MAX_ATTACHMENT_SIZE) {
|
||||||
|
logger.warn(
|
||||||
|
{
|
||||||
|
filename: attachment.filename,
|
||||||
|
size: estimatedSize,
|
||||||
|
maxSize: MAX_ATTACHMENT_SIZE,
|
||||||
|
},
|
||||||
|
"Attachment exceeds size limit, skipping",
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
totalSize += estimatedSize;
|
||||||
|
if (totalSize > MAX_TOTAL_SIZE) {
|
||||||
|
logger.warn(
|
||||||
|
{
|
||||||
|
totalSize,
|
||||||
|
maxTotalSize: MAX_TOTAL_SIZE,
|
||||||
|
},
|
||||||
|
"Total attachment size exceeds limit, skipping remaining",
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
validatedAttachments.push(attachment.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (validatedAttachments.length > 0) {
|
||||||
|
messageData.base64Attachments = validatedAttachments;
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
attachmentCount: validatedAttachments.length,
|
||||||
|
attachmentNames: attachments
|
||||||
|
.slice(0, validatedAttachments.length)
|
||||||
|
.map((att) => att.filename),
|
||||||
|
totalSizeBytes: totalSize,
|
||||||
|
},
|
||||||
|
"Including attachments in message",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await messagesClient.v2SendPost({
|
||||||
|
data: messageData,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
to: finalTo,
|
||||||
|
groupCreated,
|
||||||
|
response: response?.timestamp || "no timestamp",
|
||||||
|
},
|
||||||
|
"Message sent successfully",
|
||||||
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
// Try to get the actual error message from the response
|
||||||
|
if (error.response) {
|
||||||
|
try {
|
||||||
|
const errorBody = await error.response.text();
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
status: error.response.status,
|
||||||
|
statusText: error.response.statusText,
|
||||||
|
body: errorBody,
|
||||||
|
sentTo: finalTo,
|
||||||
|
messageDetails: {
|
||||||
|
fromNumber: number,
|
||||||
|
toRecipients: [finalTo],
|
||||||
|
hasQuote: !!quoteMessage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Signal API error",
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Could not parse error response");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error({ error }, "Full error details");
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default sendSignalMessageTask;
|
||||||
11
apps/bridge-worker/tasks/voice/receive-voice-message.ts
Normal file
11
apps/bridge-worker/tasks/voice/receive-voice-message.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
// import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
|
||||||
|
interface ReceiveVoiceMessageTaskOptions {
|
||||||
|
message: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const receiveVoiceMessageTask = async ({
|
||||||
|
message,
|
||||||
|
}: ReceiveVoiceMessageTaskOptions): Promise<void> => {};
|
||||||
|
|
||||||
|
export default receiveVoiceMessageTask;
|
||||||
11
apps/bridge-worker/tasks/voice/send-voice-message.ts
Normal file
11
apps/bridge-worker/tasks/voice/send-voice-message.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
// import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
|
||||||
|
interface SendVoiceMessageTaskOptions {
|
||||||
|
message: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sendVoiceMessageTask = async ({
|
||||||
|
message,
|
||||||
|
}: SendVoiceMessageTaskOptions): Promise<void> => {};
|
||||||
|
|
||||||
|
export default sendVoiceMessageTask;
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
import Wreck from "@hapi/wreck";
|
import Wreck from "@hapi/wreck";
|
||||||
import { withDb, AppDatabase } from "../db";
|
import { withDb, AppDatabase } from "../../lib/db.js";
|
||||||
import { twilioClientFor } from "../common";
|
import { twilioClientFor } from "../../lib/common.js";
|
||||||
import { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
|
import { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
|
||||||
import workerUtils from "../utils";
|
import workerUtils from "../../lib/utils.js";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger('bridge-worker-twilio-recording');
|
||||||
|
|
||||||
interface WebhookPayload {
|
interface WebhookPayload {
|
||||||
startTime: string;
|
startTime: string;
|
||||||
|
|
@ -20,14 +23,14 @@ const getTwilioRecording = async (url: string) => {
|
||||||
const { payload } = await Wreck.get(url);
|
const { payload } = await Wreck.get(url);
|
||||||
return { recording: payload as Buffer };
|
return { recording: payload as Buffer };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(error.output);
|
logger.error(error.output);
|
||||||
return { error: error.output };
|
return { error: error.output };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatPayload = (
|
const formatPayload = (
|
||||||
call: CallInstance,
|
call: CallInstance,
|
||||||
recording: Buffer
|
recording: Buffer,
|
||||||
): WebhookPayload => {
|
): WebhookPayload => {
|
||||||
return {
|
return {
|
||||||
startTime: call.startTime.toISOString(),
|
startTime: call.startTime.toISOString(),
|
||||||
|
|
@ -45,12 +48,12 @@ const notifyWebhooks = async (
|
||||||
db: AppDatabase,
|
db: AppDatabase,
|
||||||
voiceLineId: string,
|
voiceLineId: string,
|
||||||
call: CallInstance,
|
call: CallInstance,
|
||||||
recording: Buffer
|
recording: Buffer,
|
||||||
) => {
|
) => {
|
||||||
const webhooks = await db.webhooks.findAllByBackendId("voice", voiceLineId);
|
const webhooks = await db.webhooks.findAllByBackendId("voice", voiceLineId);
|
||||||
if (webhooks && webhooks.length === 0) return;
|
if (webhooks && webhooks.length === 0) return;
|
||||||
|
|
||||||
webhooks.forEach(({ id }) => {
|
webhooks.forEach(({ id }: any) => {
|
||||||
const payload = formatPayload(call, recording);
|
const payload = formatPayload(call, recording);
|
||||||
workerUtils.addJob(
|
workerUtils.addJob(
|
||||||
"notify-webhook",
|
"notify-webhook",
|
||||||
|
|
@ -61,7 +64,7 @@ const notifyWebhooks = async (
|
||||||
{
|
{
|
||||||
// this de-depuplicates the job
|
// this de-depuplicates the job
|
||||||
jobKey: `webhook-${id}-call-${call.sid}`,
|
jobKey: `webhook-${id}-call-${call.sid}`,
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -74,7 +77,7 @@ interface TwilioRecordingTaskOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
const twilioRecordingTask = async (
|
const twilioRecordingTask = async (
|
||||||
options: TwilioRecordingTaskOptions
|
options: TwilioRecordingTaskOptions,
|
||||||
): Promise<void> =>
|
): Promise<void> =>
|
||||||
withDb(async (db: AppDatabase) => {
|
withDb(async (db: AppDatabase) => {
|
||||||
const { voiceLineId, accountSid, callSid, recordingSid } = options;
|
const { voiceLineId, accountSid, callSid, recordingSid } = options;
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { createHash } from "crypto";
|
import { createHash } from "crypto";
|
||||||
import { withDb, AppDatabase } from "../db";
|
import { withDb, AppDatabase } from "../../lib/db.js";
|
||||||
import { convert } from "../lib/media-convert";
|
import { convert } from "../../lib/media-convert.js";
|
||||||
|
|
||||||
interface VoiceLineAudioUpdateTaskOptions {
|
interface VoiceLineAudioUpdateTaskOptions {
|
||||||
voiceLineId: string;
|
voiceLineId: string;
|
||||||
|
|
@ -13,7 +13,7 @@ const sha1sum = (v: any) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const voiceLineAudioUpdateTask = async (
|
const voiceLineAudioUpdateTask = async (
|
||||||
payload: VoiceLineAudioUpdateTaskOptions
|
payload: VoiceLineAudioUpdateTaskOptions,
|
||||||
): Promise<void> =>
|
): Promise<void> =>
|
||||||
withDb(async (db: AppDatabase) => {
|
withDb(async (db: AppDatabase) => {
|
||||||
const { voiceLineId } = payload;
|
const { voiceLineId } = payload;
|
||||||
|
|
@ -41,7 +41,7 @@ const voiceLineAudioUpdateTask = async (
|
||||||
"audio/mpeg": mp3.toString("base64"),
|
"audio/mpeg": mp3.toString("base64"),
|
||||||
checksum: webmSha1,
|
checksum: webmSha1,
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import Twilio from "twilio";
|
import Twilio from "twilio";
|
||||||
import config from "@digiresilience/metamigo-config";
|
// import config from "@digiresilience/bridge-config";
|
||||||
import { withDb, AppDatabase } from "../db";
|
import { withDb, AppDatabase } from "../../lib/db.js";
|
||||||
|
|
||||||
|
const config: any = {};
|
||||||
|
|
||||||
interface VoiceLineDeleteTaskOptions {
|
interface VoiceLineDeleteTaskOptions {
|
||||||
voiceLineId: string;
|
voiceLineId: string;
|
||||||
|
|
@ -9,7 +11,7 @@ interface VoiceLineDeleteTaskOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
const voiceLineDeleteTask = async (
|
const voiceLineDeleteTask = async (
|
||||||
payload: VoiceLineDeleteTaskOptions
|
payload: VoiceLineDeleteTaskOptions,
|
||||||
): Promise<void> =>
|
): Promise<void> =>
|
||||||
withDb(async (db: AppDatabase) => {
|
withDb(async (db: AppDatabase) => {
|
||||||
const { voiceLineId, providerId, providerLineSid } = payload;
|
const { voiceLineId, providerId, providerLineSid } = payload;
|
||||||
|
|
@ -19,7 +21,7 @@ const voiceLineDeleteTask = async (
|
||||||
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
||||||
if (!accountSid || !apiKeySid || !apiKeySecret)
|
if (!accountSid || !apiKeySid || !apiKeySecret)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`twilio provider ${provider.name} does not have credentials`
|
`twilio provider ${provider.name} does not have credentials`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const client = Twilio(apiKeySid, apiKeySecret, {
|
const client = Twilio(apiKeySid, apiKeySecret, {
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
import Twilio from "twilio";
|
import Twilio from "twilio";
|
||||||
import config from "@digiresilience/metamigo-config";
|
// import config from "@digiresilience/bridge-config";
|
||||||
import { withDb, AppDatabase } from "../db";
|
import { withDb, AppDatabase } from "../../lib/db.js";
|
||||||
|
|
||||||
|
const config: any = {};
|
||||||
|
|
||||||
interface VoiceLineUpdateTaskOptions {
|
interface VoiceLineUpdateTaskOptions {
|
||||||
voiceLineId: string;
|
voiceLineId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const voiceLineUpdateTask = async (
|
const voiceLineUpdateTask = async (
|
||||||
payload: VoiceLineUpdateTaskOptions
|
payload: VoiceLineUpdateTaskOptions,
|
||||||
): Promise<void> =>
|
): Promise<void> =>
|
||||||
withDb(async (db: AppDatabase) => {
|
withDb(async (db: AppDatabase) => {
|
||||||
const { voiceLineId } = payload;
|
const { voiceLineId } = payload;
|
||||||
|
|
@ -22,7 +24,7 @@ const voiceLineUpdateTask = async (
|
||||||
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;
|
||||||
if (!accountSid || !apiKeySid || !apiKeySecret)
|
if (!accountSid || !apiKeySid || !apiKeySecret)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`twilio provider ${provider.name} does not have credentials`
|
`twilio provider ${provider.name} does not have credentials`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const client = Twilio(apiKeySid, apiKeySecret, {
|
const client = Twilio(apiKeySid, apiKeySecret, {
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||||
|
|
||||||
|
interface ReceiveWhatsappMessageTaskOptions {
|
||||||
|
token: string;
|
||||||
|
to: string;
|
||||||
|
from: string;
|
||||||
|
messageId: string;
|
||||||
|
sentAt: string;
|
||||||
|
message: string;
|
||||||
|
attachment?: string;
|
||||||
|
filename?: string;
|
||||||
|
mimeType?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const receiveWhatsappMessageTask = async ({
|
||||||
|
token,
|
||||||
|
to,
|
||||||
|
from,
|
||||||
|
messageId,
|
||||||
|
sentAt,
|
||||||
|
message,
|
||||||
|
attachment,
|
||||||
|
filename,
|
||||||
|
mimeType,
|
||||||
|
}: ReceiveWhatsappMessageTaskOptions): Promise<void> => {
|
||||||
|
const worker = await getWorkerUtils();
|
||||||
|
const row = await db
|
||||||
|
.selectFrom("WhatsappBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("id", "=", token)
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
const backendId = row.id;
|
||||||
|
const payload = {
|
||||||
|
to,
|
||||||
|
from,
|
||||||
|
message_id: messageId,
|
||||||
|
sent_at: sentAt,
|
||||||
|
message,
|
||||||
|
attachment,
|
||||||
|
filename,
|
||||||
|
mime_type: mimeType,
|
||||||
|
};
|
||||||
|
|
||||||
|
await worker.addJob("common/notify-webhooks", { backendId, payload });
|
||||||
|
};
|
||||||
|
|
||||||
|
export default receiveWhatsappMessageTask;
|
||||||
68
apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts
Normal file
68
apps/bridge-worker/tasks/whatsapp/send-whatsapp-message.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
import { db } from "@link-stack/bridge-common";
|
||||||
|
import { createLogger } from "@link-stack/logger";
|
||||||
|
|
||||||
|
const logger = createLogger("bridge-worker-send-whatsapp-message");
|
||||||
|
|
||||||
|
interface SendWhatsappMessageTaskOptions {
|
||||||
|
token: string;
|
||||||
|
to: string;
|
||||||
|
message: any;
|
||||||
|
attachments?: Array<{
|
||||||
|
data: string;
|
||||||
|
filename: string;
|
||||||
|
mime_type: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sendWhatsappMessageTask = async ({
|
||||||
|
message,
|
||||||
|
to,
|
||||||
|
token,
|
||||||
|
attachments,
|
||||||
|
}: SendWhatsappMessageTaskOptions): Promise<void> => {
|
||||||
|
const bot = await db
|
||||||
|
.selectFrom("WhatsappBot")
|
||||||
|
.selectAll()
|
||||||
|
.where("token", "=", token)
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
|
||||||
|
const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`;
|
||||||
|
const params: any = { message, phoneNumber: to };
|
||||||
|
|
||||||
|
if (attachments && attachments.length > 0) {
|
||||||
|
params.attachments = attachments;
|
||||||
|
logger.debug(
|
||||||
|
{
|
||||||
|
attachmentCount: attachments.length,
|
||||||
|
attachmentNames: attachments.map((att) => att.filename),
|
||||||
|
},
|
||||||
|
"Sending WhatsApp message with attachments",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await fetch(url, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(params),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
const errorText = await result.text();
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
status: result.status,
|
||||||
|
errorText,
|
||||||
|
url,
|
||||||
|
},
|
||||||
|
"WhatsApp send failed",
|
||||||
|
);
|
||||||
|
throw new Error(`Failed to send message: ${result.status}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error({ error });
|
||||||
|
throw new Error("Failed to send message");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default sendWhatsappMessageTask;
|
||||||
8
apps/bridge-worker/tsconfig.json
Normal file
8
apps/bridge-worker/tsconfig.json
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"extends": "@link-stack/typescript-config/tsconfig.node.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "build/main"
|
||||||
|
},
|
||||||
|
"include": ["**/*.ts", "**/.*.ts"],
|
||||||
|
"exclude": ["node_modules", "build"]
|
||||||
|
}
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
.git
|
|
||||||
.idea
|
|
||||||
**/node_modules
|
|
||||||
!/node_modules
|
|
||||||
**/build
|
|
||||||
**/dist
|
|
||||||
**/tmp
|
|
||||||
**/.env*
|
|
||||||
**/coverage
|
|
||||||
**/.next
|
|
||||||
**/amigo.*.json
|
|
||||||
**/.yalc
|
|
||||||
**/cypress/videos
|
|
||||||
**/cypress/screenshots
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
leafcutter-docker-build:
|
|
||||||
image: registry.gitlab.com/guardianproject-ops/docker-alpine-git:latest
|
|
||||||
services:
|
|
||||||
- docker:dind
|
|
||||||
stage: docker-build
|
|
||||||
variables:
|
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/leafcutter
|
|
||||||
DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
|
|
||||||
script:
|
|
||||||
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
|
||||||
- cd apps/leafcutter && make docker/build-fresh-push
|
|
||||||
|
|
||||||
leafcutter-docker-release:
|
|
||||||
image: registry.gitlab.com/guardianproject-ops/docker-alpine-git:latest
|
|
||||||
services:
|
|
||||||
- docker:dind
|
|
||||||
stage: docker-release
|
|
||||||
variables:
|
|
||||||
DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/leafcutter
|
|
||||||
DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
|
|
||||||
DOCKER_TAG_NEW: ${CI_COMMIT_REF_NAME}
|
|
||||||
only:
|
|
||||||
- main
|
|
||||||
- develop
|
|
||||||
script:
|
|
||||||
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
|
||||||
- cd apps/leafcutter && make docker/add-tag
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
const path = require("path");
|
|
||||||
const toPath = (filePath) => path.join(process.cwd(), filePath);
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
stories: [
|
|
||||||
"../stories/**/*.stories.mdx",
|
|
||||||
"../stories/**/*.stories.@(js|jsx|ts|tsx)",
|
|
||||||
],
|
|
||||||
addons: [
|
|
||||||
"@storybook/addon-docs",
|
|
||||||
"@storybook/addon-links",
|
|
||||||
"@storybook/addon-essentials",
|
|
||||||
],
|
|
||||||
webpackFinal: async (config) => {
|
|
||||||
return {
|
|
||||||
...config,
|
|
||||||
resolve: {
|
|
||||||
...config.resolve,
|
|
||||||
alias: {
|
|
||||||
...config.resolve.alias,
|
|
||||||
"@emotion/core": toPath("node_modules/@emotion/react"),
|
|
||||||
"emotion-theming": toPath("node_modules/@emotion/react"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
import { AppProvider } from "../components/common/AppProvider";
|
|
||||||
import { I18n } from "react-polyglot";
|
|
||||||
import en from "../locales/en.json";
|
|
||||||
import "@fontsource/roboto/400.css";
|
|
||||||
import "@fontsource/poppins/400.css";
|
|
||||||
import "@fontsource/poppins/700.css";
|
|
||||||
import "@fontsource/playfair-display/900.css";
|
|
||||||
|
|
||||||
const locale = "en";
|
|
||||||
const messages = { en };
|
|
||||||
|
|
||||||
export const decorators = [
|
|
||||||
(Story) => (
|
|
||||||
<I18n locale={locale} messages={messages[locale]}>
|
|
||||||
<AppProvider>
|
|
||||||
<Story />
|
|
||||||
</AppProvider>
|
|
||||||
</I18n>
|
|
||||||
),
|
|
||||||
];
|
|
||||||
|
|
||||||
export const parameters = {
|
|
||||||
actions: { argTypesRegex: "^on[A-Z].*" },
|
|
||||||
controls: {
|
|
||||||
matchers: {
|
|
||||||
color: /(background|color)$/i,
|
|
||||||
date: /Date$/,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
@ -1,52 +0,0 @@
|
||||||
|
|
||||||
FROM node:20 AS base
|
|
||||||
|
|
||||||
FROM base AS builder
|
|
||||||
ARG APP_DIR=/opt/leafcutter
|
|
||||||
RUN mkdir -p ${APP_DIR}/
|
|
||||||
RUN npm i -g turbo
|
|
||||||
WORKDIR ${APP_DIR}
|
|
||||||
COPY . .
|
|
||||||
RUN turbo prune --scope=leafcutter --docker
|
|
||||||
|
|
||||||
FROM base AS installer
|
|
||||||
ARG APP_DIR=/opt/leafcutter
|
|
||||||
WORKDIR ${APP_DIR}
|
|
||||||
COPY .gitignore .gitignore
|
|
||||||
COPY --from=builder ${APP_DIR}/out/json/ .
|
|
||||||
COPY --from=builder ${APP_DIR}/out/package-lock.json ./package-lock.json
|
|
||||||
RUN npm ci --omit=dev
|
|
||||||
|
|
||||||
COPY --from=builder ${APP_DIR}/out/full/ .
|
|
||||||
ARG LINK_EMBEDDED=true
|
|
||||||
RUN npm i -g turbo
|
|
||||||
RUN turbo run build --filter=leafcutter
|
|
||||||
|
|
||||||
FROM base AS runner
|
|
||||||
ARG APP_DIR=/opt/leafcutter
|
|
||||||
WORKDIR ${APP_DIR}/
|
|
||||||
ARG BUILD_DATE
|
|
||||||
ARG VERSION
|
|
||||||
LABEL maintainer="Darren Clarke <darren@redaranj.com>"
|
|
||||||
LABEL org.label-schema.build-date=$BUILD_DATE
|
|
||||||
LABEL org.label-schema.version=$VERSION
|
|
||||||
ENV APP_DIR ${APP_DIR}
|
|
||||||
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
|
|
||||||
apt-get install -y --no-install-recommends \
|
|
||||||
dumb-init
|
|
||||||
RUN mkdir -p ${APP_DIR}
|
|
||||||
RUN chown -R node ${APP_DIR}/
|
|
||||||
|
|
||||||
USER node
|
|
||||||
WORKDIR ${APP_DIR}
|
|
||||||
COPY --from=installer ${APP_DIR}/node_modules/ ./node_modules/
|
|
||||||
COPY --from=installer ${APP_DIR}/apps/leafcutter/ ./apps/leafcutter/
|
|
||||||
COPY --from=installer ${APP_DIR}/package.json ./package.json
|
|
||||||
USER root
|
|
||||||
WORKDIR ${APP_DIR}/apps/leafcutter/
|
|
||||||
RUN chmod +x docker-entrypoint.sh
|
|
||||||
USER node
|
|
||||||
EXPOSE 3000
|
|
||||||
ENV PORT 3000
|
|
||||||
ENV NODE_ENV production
|
|
||||||
ENTRYPOINT ["/opt/leafcutter/apps/leafcutter/docker-entrypoint.sh"]
|
|
||||||
|
|
@ -1,616 +0,0 @@
|
||||||
### GNU AFFERO GENERAL PUBLIC LICENSE
|
|
||||||
|
|
||||||
Version 3, 19 November 2007
|
|
||||||
|
|
||||||
Copyright (C) 2007 Free Software Foundation, Inc.
|
|
||||||
<https://fsf.org/>
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim copies of this
|
|
||||||
license document, but changing it is not allowed.
|
|
||||||
|
|
||||||
### Preamble
|
|
||||||
|
|
||||||
The GNU Affero General Public License is a free, copyleft license for
|
|
||||||
software and other kinds of works, specifically designed to ensure
|
|
||||||
cooperation with the community in the case of network server software.
|
|
||||||
|
|
||||||
The licenses for most software and other practical works are designed
|
|
||||||
to take away your freedom to share and change the works. By contrast,
|
|
||||||
our General Public Licenses are intended to guarantee your freedom to
|
|
||||||
share and change all versions of a program--to make sure it remains
|
|
||||||
free software for all its users.
|
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not
|
|
||||||
price. Our General Public Licenses are designed to make sure that you
|
|
||||||
have the freedom to distribute copies of free software (and charge for
|
|
||||||
them if you wish), that you receive source code or can get it if you
|
|
||||||
want it, that you can change the software or use pieces of it in new
|
|
||||||
free programs, and that you know you can do these things.
|
|
||||||
|
|
||||||
Developers that use our General Public Licenses protect your rights
|
|
||||||
with two steps: (1) assert copyright on the software, and (2) offer
|
|
||||||
you this License which gives you legal permission to copy, distribute
|
|
||||||
and/or modify the software.
|
|
||||||
|
|
||||||
A secondary benefit of defending all users' freedom is that
|
|
||||||
improvements made in alternate versions of the program, if they
|
|
||||||
receive widespread use, become available for other developers to
|
|
||||||
incorporate. Many developers of free software are heartened and
|
|
||||||
encouraged by the resulting cooperation. However, in the case of
|
|
||||||
software used on network servers, this result may fail to come about.
|
|
||||||
The GNU General Public License permits making a modified version and
|
|
||||||
letting the public access it on a server without ever releasing its
|
|
||||||
source code to the public.
|
|
||||||
|
|
||||||
The GNU Affero General Public License is designed specifically to
|
|
||||||
ensure that, in such cases, the modified source code becomes available
|
|
||||||
to the community. It requires the operator of a network server to
|
|
||||||
provide the source code of the modified version running there to the
|
|
||||||
users of that server. Therefore, public use of a modified version, on
|
|
||||||
a publicly accessible server, gives the public access to the source
|
|
||||||
code of the modified version.
|
|
||||||
|
|
||||||
An older license, called the Affero General Public License and
|
|
||||||
published by Affero, was designed to accomplish similar goals. This is
|
|
||||||
a different license, not a version of the Affero GPL, but Affero has
|
|
||||||
released a new version of the Affero GPL which permits relicensing
|
|
||||||
under this license.
|
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and
|
|
||||||
modification follow.
|
|
||||||
|
|
||||||
### TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
#### 0. Definitions.
|
|
||||||
|
|
||||||
"This License" refers to version 3 of the GNU Affero General Public
|
|
||||||
License.
|
|
||||||
|
|
||||||
"Copyright" also means copyright-like laws that apply to other kinds
|
|
||||||
of works, such as semiconductor masks.
|
|
||||||
|
|
||||||
"The Program" refers to any copyrightable work licensed under this
|
|
||||||
License. Each licensee is addressed as "you". "Licensees" and
|
|
||||||
"recipients" may be individuals or organizations.
|
|
||||||
|
|
||||||
To "modify" a work means to copy from or adapt all or part of the work
|
|
||||||
in a fashion requiring copyright permission, other than the making of
|
|
||||||
an exact copy. The resulting work is called a "modified version" of
|
|
||||||
the earlier work or a work "based on" the earlier work.
|
|
||||||
|
|
||||||
A "covered work" means either the unmodified Program or a work based
|
|
||||||
on the Program.
|
|
||||||
|
|
||||||
To "propagate" a work means to do anything with it that, without
|
|
||||||
permission, would make you directly or secondarily liable for
|
|
||||||
infringement under applicable copyright law, except executing it on a
|
|
||||||
computer or modifying a private copy. Propagation includes copying,
|
|
||||||
distribution (with or without modification), making available to the
|
|
||||||
public, and in some countries other activities as well.
|
|
||||||
|
|
||||||
To "convey" a work means any kind of propagation that enables other
|
|
||||||
parties to make or receive copies. Mere interaction with a user
|
|
||||||
through a computer network, with no transfer of a copy, is not
|
|
||||||
conveying.
|
|
||||||
|
|
||||||
An interactive user interface displays "Appropriate Legal Notices" to
|
|
||||||
the extent that it includes a convenient and prominently visible
|
|
||||||
feature that (1) displays an appropriate copyright notice, and (2)
|
|
||||||
tells the user that there is no warranty for the work (except to the
|
|
||||||
extent that warranties are provided), that licensees may convey the
|
|
||||||
work under this License, and how to view a copy of this License. If
|
|
||||||
the interface presents a list of user commands or options, such as a
|
|
||||||
menu, a prominent item in the list meets this criterion.
|
|
||||||
|
|
||||||
#### 1. Source Code.
|
|
||||||
|
|
||||||
The "source code" for a work means the preferred form of the work for
|
|
||||||
making modifications to it. "Object code" means any non-source form of
|
|
||||||
a work.
|
|
||||||
|
|
||||||
A "Standard Interface" means an interface that either is an official
|
|
||||||
standard defined by a recognized standards body, or, in the case of
|
|
||||||
interfaces specified for a particular programming language, one that
|
|
||||||
is widely used among developers working in that language.
|
|
||||||
|
|
||||||
The "System Libraries" of an executable work include anything, other
|
|
||||||
than the work as a whole, that (a) is included in the normal form of
|
|
||||||
packaging a Major Component, but which is not part of that Major
|
|
||||||
Component, and (b) serves only to enable use of the work with that
|
|
||||||
Major Component, or to implement a Standard Interface for which an
|
|
||||||
implementation is available to the public in source code form. A
|
|
||||||
"Major Component", in this context, means a major essential component
|
|
||||||
(kernel, window system, and so on) of the specific operating system
|
|
||||||
(if any) on which the executable work runs, or a compiler used to
|
|
||||||
produce the work, or an object code interpreter used to run it.
|
|
||||||
|
|
||||||
The "Corresponding Source" for a work in object code form means all
|
|
||||||
the source code needed to generate, install, and (for an executable
|
|
||||||
work) run the object code and to modify the work, including scripts to
|
|
||||||
control those activities. However, it does not include the work's
|
|
||||||
System Libraries, or general-purpose tools or generally available free
|
|
||||||
programs which are used unmodified in performing those activities but
|
|
||||||
which are not part of the work. For example, Corresponding Source
|
|
||||||
includes interface definition files associated with source files for
|
|
||||||
the work, and the source code for shared libraries and dynamically
|
|
||||||
linked subprograms that the work is specifically designed to require,
|
|
||||||
such as by intimate data communication or control flow between those
|
|
||||||
subprograms and other parts of the work.
|
|
||||||
|
|
||||||
The Corresponding Source need not include anything that users can
|
|
||||||
regenerate automatically from other parts of the Corresponding Source.
|
|
||||||
|
|
||||||
The Corresponding Source for a work in source code form is that same
|
|
||||||
work.
|
|
||||||
|
|
||||||
#### 2. Basic Permissions.
|
|
||||||
|
|
||||||
All rights granted under this License are granted for the term of
|
|
||||||
copyright on the Program, and are irrevocable provided the stated
|
|
||||||
conditions are met. This License explicitly affirms your unlimited
|
|
||||||
permission to run the unmodified Program. The output from running a
|
|
||||||
covered work is covered by this License only if the output, given its
|
|
||||||
content, constitutes a covered work. This License acknowledges your
|
|
||||||
rights of fair use or other equivalent, as provided by copyright law.
|
|
||||||
|
|
||||||
You may make, run and propagate covered works that you do not convey,
|
|
||||||
without conditions so long as your license otherwise remains in force.
|
|
||||||
You may convey covered works to others for the sole purpose of having
|
|
||||||
them make modifications exclusively for you, or provide you with
|
|
||||||
facilities for running those works, provided that you comply with the
|
|
||||||
terms of this License in conveying all material for which you do not
|
|
||||||
control copyright. Those thus making or running the covered works for
|
|
||||||
you must do so exclusively on your behalf, under your direction and
|
|
||||||
control, on terms that prohibit them from making any copies of your
|
|
||||||
copyrighted material outside their relationship with you.
|
|
||||||
|
|
||||||
Conveying under any other circumstances is permitted solely under the
|
|
||||||
conditions stated below. Sublicensing is not allowed; section 10 makes
|
|
||||||
it unnecessary.
|
|
||||||
|
|
||||||
#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
|
||||||
|
|
||||||
No covered work shall be deemed part of an effective technological
|
|
||||||
measure under any applicable law fulfilling obligations under article
|
|
||||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
|
||||||
similar laws prohibiting or restricting circumvention of such
|
|
||||||
measures.
|
|
||||||
|
|
||||||
When you convey a covered work, you waive any legal power to forbid
|
|
||||||
circumvention of technological measures to the extent such
|
|
||||||
circumvention is effected by exercising rights under this License with
|
|
||||||
respect to the covered work, and you disclaim any intention to limit
|
|
||||||
operation or modification of the work as a means of enforcing, against
|
|
||||||
the work's users, your or third parties' legal rights to forbid
|
|
||||||
circumvention of technological measures.
|
|
||||||
|
|
||||||
#### 4. Conveying Verbatim Copies.
|
|
||||||
|
|
||||||
You may convey verbatim copies of the Program's source code as you
|
|
||||||
receive it, in any medium, provided that you conspicuously and
|
|
||||||
appropriately publish on each copy an appropriate copyright notice;
|
|
||||||
keep intact all notices stating that this License and any
|
|
||||||
non-permissive terms added in accord with section 7 apply to the code;
|
|
||||||
keep intact all notices of the absence of any warranty; and give all
|
|
||||||
recipients a copy of this License along with the Program.
|
|
||||||
|
|
||||||
You may charge any price or no price for each copy that you convey,
|
|
||||||
and you may offer support or warranty protection for a fee.
|
|
||||||
|
|
||||||
#### 5. Conveying Modified Source Versions.
|
|
||||||
|
|
||||||
You may convey a work based on the Program, or the modifications to
|
|
||||||
produce it from the Program, in the form of source code under the
|
|
||||||
terms of section 4, provided that you also meet all of these
|
|
||||||
conditions:
|
|
||||||
|
|
||||||
- a) The work must carry prominent notices stating that you modified
|
|
||||||
it, and giving a relevant date.
|
|
||||||
- b) The work must carry prominent notices stating that it is
|
|
||||||
released under this License and any conditions added under
|
|
||||||
section 7. This requirement modifies the requirement in section 4
|
|
||||||
to "keep intact all notices".
|
|
||||||
- c) You must license the entire work, as a whole, under this
|
|
||||||
License to anyone who comes into possession of a copy. This
|
|
||||||
License will therefore apply, along with any applicable section 7
|
|
||||||
additional terms, to the whole of the work, and all its parts,
|
|
||||||
regardless of how they are packaged. This License gives no
|
|
||||||
permission to license the work in any other way, but it does not
|
|
||||||
invalidate such permission if you have separately received it.
|
|
||||||
- d) If the work has interactive user interfaces, each must display
|
|
||||||
Appropriate Legal Notices; however, if the Program has interactive
|
|
||||||
interfaces that do not display Appropriate Legal Notices, your
|
|
||||||
work need not make them do so.
|
|
||||||
|
|
||||||
A compilation of a covered work with other separate and independent
|
|
||||||
works, which are not by their nature extensions of the covered work,
|
|
||||||
and which are not combined with it such as to form a larger program,
|
|
||||||
in or on a volume of a storage or distribution medium, is called an
|
|
||||||
"aggregate" if the compilation and its resulting copyright are not
|
|
||||||
used to limit the access or legal rights of the compilation's users
|
|
||||||
beyond what the individual works permit. Inclusion of a covered work
|
|
||||||
in an aggregate does not cause this License to apply to the other
|
|
||||||
parts of the aggregate.
|
|
||||||
|
|
||||||
#### 6. Conveying Non-Source Forms.
|
|
||||||
|
|
||||||
You may convey a covered work in object code form under the terms of
|
|
||||||
sections 4 and 5, provided that you also convey the machine-readable
|
|
||||||
Corresponding Source under the terms of this License, in one of these
|
|
||||||
ways:
|
|
||||||
|
|
||||||
- a) Convey the object code in, or embodied in, a physical product
|
|
||||||
(including a physical distribution medium), accompanied by the
|
|
||||||
Corresponding Source fixed on a durable physical medium
|
|
||||||
customarily used for software interchange.
|
|
||||||
- b) Convey the object code in, or embodied in, a physical product
|
|
||||||
(including a physical distribution medium), accompanied by a
|
|
||||||
written offer, valid for at least three years and valid for as
|
|
||||||
long as you offer spare parts or customer support for that product
|
|
||||||
model, to give anyone who possesses the object code either (1) a
|
|
||||||
copy of the Corresponding Source for all the software in the
|
|
||||||
product that is covered by this License, on a durable physical
|
|
||||||
medium customarily used for software interchange, for a price no
|
|
||||||
more than your reasonable cost of physically performing this
|
|
||||||
conveying of source, or (2) access to copy the Corresponding
|
|
||||||
Source from a network server at no charge.
|
|
||||||
- c) Convey individual copies of the object code with a copy of the
|
|
||||||
written offer to provide the Corresponding Source. This
|
|
||||||
alternative is allowed only occasionally and noncommercially, and
|
|
||||||
only if you received the object code with such an offer, in accord
|
|
||||||
with subsection 6b.
|
|
||||||
- d) Convey the object code by offering access from a designated
|
|
||||||
place (gratis or for a charge), and offer equivalent access to the
|
|
||||||
Corresponding Source in the same way through the same place at no
|
|
||||||
further charge. You need not require recipients to copy the
|
|
||||||
Corresponding Source along with the object code. If the place to
|
|
||||||
copy the object code is a network server, the Corresponding Source
|
|
||||||
may be on a different server (operated by you or a third party)
|
|
||||||
that supports equivalent copying facilities, provided you maintain
|
|
||||||
clear directions next to the object code saying where to find the
|
|
||||||
Corresponding Source. Regardless of what server hosts the
|
|
||||||
Corresponding Source, you remain obligated to ensure that it is
|
|
||||||
available for as long as needed to satisfy these requirements.
|
|
||||||
- e) Convey the object code using peer-to-peer transmission,
|
|
||||||
provided you inform other peers where the object code and
|
|
||||||
Corresponding Source of the work are being offered to the general
|
|
||||||
public at no charge under subsection 6d.
|
|
||||||
|
|
||||||
A separable portion of the object code, whose source code is excluded
|
|
||||||
from the Corresponding Source as a System Library, need not be
|
|
||||||
included in conveying the object code work.
|
|
||||||
|
|
||||||
A "User Product" is either (1) a "consumer product", which means any
|
|
||||||
tangible personal property which is normally used for personal,
|
|
||||||
family, or household purposes, or (2) anything designed or sold for
|
|
||||||
incorporation into a dwelling. In determining whether a product is a
|
|
||||||
consumer product, doubtful cases shall be resolved in favor of
|
|
||||||
coverage. For a particular product received by a particular user,
|
|
||||||
"normally used" refers to a typical or common use of that class of
|
|
||||||
product, regardless of the status of the particular user or of the way
|
|
||||||
in which the particular user actually uses, or expects or is expected
|
|
||||||
to use, the product. A product is a consumer product regardless of
|
|
||||||
whether the product has substantial commercial, industrial or
|
|
||||||
non-consumer uses, unless such uses represent the only significant
|
|
||||||
mode of use of the product.
|
|
||||||
|
|
||||||
"Installation Information" for a User Product means any methods,
|
|
||||||
procedures, authorization keys, or other information required to
|
|
||||||
install and execute modified versions of a covered work in that User
|
|
||||||
Product from a modified version of its Corresponding Source. The
|
|
||||||
information must suffice to ensure that the continued functioning of
|
|
||||||
the modified object code is in no case prevented or interfered with
|
|
||||||
solely because modification has been made.
|
|
||||||
|
|
||||||
If you convey an object code work under this section in, or with, or
|
|
||||||
specifically for use in, a User Product, and the conveying occurs as
|
|
||||||
part of a transaction in which the right of possession and use of the
|
|
||||||
User Product is transferred to the recipient in perpetuity or for a
|
|
||||||
fixed term (regardless of how the transaction is characterized), the
|
|
||||||
Corresponding Source conveyed under this section must be accompanied
|
|
||||||
by the Installation Information. But this requirement does not apply
|
|
||||||
if neither you nor any third party retains the ability to install
|
|
||||||
modified object code on the User Product (for example, the work has
|
|
||||||
been installed in ROM).
|
|
||||||
|
|
||||||
The requirement to provide Installation Information does not include a
|
|
||||||
requirement to continue to provide support service, warranty, or
|
|
||||||
updates for a work that has been modified or installed by the
|
|
||||||
recipient, or for the User Product in which it has been modified or
|
|
||||||
installed. Access to a network may be denied when the modification
|
|
||||||
itself materially and adversely affects the operation of the network
|
|
||||||
or violates the rules and protocols for communication across the
|
|
||||||
network.
|
|
||||||
|
|
||||||
Corresponding Source conveyed, and Installation Information provided,
|
|
||||||
in accord with this section must be in a format that is publicly
|
|
||||||
documented (and with an implementation available to the public in
|
|
||||||
source code form), and must require no special password or key for
|
|
||||||
unpacking, reading or copying.
|
|
||||||
|
|
||||||
#### 7. Additional Terms.
|
|
||||||
|
|
||||||
"Additional permissions" are terms that supplement the terms of this
|
|
||||||
License by making exceptions from one or more of its conditions.
|
|
||||||
Additional permissions that are applicable to the entire Program shall
|
|
||||||
be treated as though they were included in this License, to the extent
|
|
||||||
that they are valid under applicable law. If additional permissions
|
|
||||||
apply only to part of the Program, that part may be used separately
|
|
||||||
under those permissions, but the entire Program remains governed by
|
|
||||||
this License without regard to the additional permissions.
|
|
||||||
|
|
||||||
When you convey a copy of a covered work, you may at your option
|
|
||||||
remove any additional permissions from that copy, or from any part of
|
|
||||||
it. (Additional permissions may be written to require their own
|
|
||||||
removal in certain cases when you modify the work.) You may place
|
|
||||||
additional permissions on material, added by you to a covered work,
|
|
||||||
for which you have or can give appropriate copyright permission.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, for material you
|
|
||||||
add to a covered work, you may (if authorized by the copyright holders
|
|
||||||
of that material) supplement the terms of this License with terms:
|
|
||||||
|
|
||||||
- a) Disclaiming warranty or limiting liability differently from the
|
|
||||||
terms of sections 15 and 16 of this License; or
|
|
||||||
- b) Requiring preservation of specified reasonable legal notices or
|
|
||||||
author attributions in that material or in the Appropriate Legal
|
|
||||||
Notices displayed by works containing it; or
|
|
||||||
- c) Prohibiting misrepresentation of the origin of that material,
|
|
||||||
or requiring that modified versions of such material be marked in
|
|
||||||
reasonable ways as different from the original version; or
|
|
||||||
- d) Limiting the use for publicity purposes of names of licensors
|
|
||||||
or authors of the material; or
|
|
||||||
- e) Declining to grant rights under trademark law for use of some
|
|
||||||
trade names, trademarks, or service marks; or
|
|
||||||
- f) Requiring indemnification of licensors and authors of that
|
|
||||||
material by anyone who conveys the material (or modified versions
|
|
||||||
of it) with contractual assumptions of liability to the recipient,
|
|
||||||
for any liability that these contractual assumptions directly
|
|
||||||
impose on those licensors and authors.
|
|
||||||
|
|
||||||
All other non-permissive additional terms are considered "further
|
|
||||||
restrictions" within the meaning of section 10. If the Program as you
|
|
||||||
received it, or any part of it, contains a notice stating that it is
|
|
||||||
governed by this License along with a term that is a further
|
|
||||||
restriction, you may remove that term. If a license document contains
|
|
||||||
a further restriction but permits relicensing or conveying under this
|
|
||||||
License, you may add to a covered work material governed by the terms
|
|
||||||
of that license document, provided that the further restriction does
|
|
||||||
not survive such relicensing or conveying.
|
|
||||||
|
|
||||||
If you add terms to a covered work in accord with this section, you
|
|
||||||
must place, in the relevant source files, a statement of the
|
|
||||||
additional terms that apply to those files, or a notice indicating
|
|
||||||
where to find the applicable terms.
|
|
||||||
|
|
||||||
Additional terms, permissive or non-permissive, may be stated in the
|
|
||||||
form of a separately written license, or stated as exceptions; the
|
|
||||||
above requirements apply either way.
|
|
||||||
|
|
||||||
#### 8. Termination.
|
|
||||||
|
|
||||||
You may not propagate or modify a covered work except as expressly
|
|
||||||
provided under this License. Any attempt otherwise to propagate or
|
|
||||||
modify it is void, and will automatically terminate your rights under
|
|
||||||
this License (including any patent licenses granted under the third
|
|
||||||
paragraph of section 11).
|
|
||||||
|
|
||||||
However, if you cease all violation of this License, then your license
|
|
||||||
from a particular copyright holder is reinstated (a) provisionally,
|
|
||||||
unless and until the copyright holder explicitly and finally
|
|
||||||
terminates your license, and (b) permanently, if the copyright holder
|
|
||||||
fails to notify you of the violation by some reasonable means prior to
|
|
||||||
60 days after the cessation.
|
|
||||||
|
|
||||||
Moreover, your license from a particular copyright holder is
|
|
||||||
reinstated permanently if the copyright holder notifies you of the
|
|
||||||
violation by some reasonable means, this is the first time you have
|
|
||||||
received notice of violation of this License (for any work) from that
|
|
||||||
copyright holder, and you cure the violation prior to 30 days after
|
|
||||||
your receipt of the notice.
|
|
||||||
|
|
||||||
Termination of your rights under this section does not terminate the
|
|
||||||
licenses of parties who have received copies or rights from you under
|
|
||||||
this License. If your rights have been terminated and not permanently
|
|
||||||
reinstated, you do not qualify to receive new licenses for the same
|
|
||||||
material under section 10.
|
|
||||||
|
|
||||||
#### 9. Acceptance Not Required for Having Copies.
|
|
||||||
|
|
||||||
You are not required to accept this License in order to receive or run
|
|
||||||
a copy of the Program. Ancillary propagation of a covered work
|
|
||||||
occurring solely as a consequence of using peer-to-peer transmission
|
|
||||||
to receive a copy likewise does not require acceptance. However,
|
|
||||||
nothing other than this License grants you permission to propagate or
|
|
||||||
modify any covered work. These actions infringe copyright if you do
|
|
||||||
not accept this License. Therefore, by modifying or propagating a
|
|
||||||
covered work, you indicate your acceptance of this License to do so.
|
|
||||||
|
|
||||||
#### 10. Automatic Licensing of Downstream Recipients.
|
|
||||||
|
|
||||||
Each time you convey a covered work, the recipient automatically
|
|
||||||
receives a license from the original licensors, to run, modify and
|
|
||||||
propagate that work, subject to this License. You are not responsible
|
|
||||||
for enforcing compliance by third parties with this License.
|
|
||||||
|
|
||||||
An "entity transaction" is a transaction transferring control of an
|
|
||||||
organization, or substantially all assets of one, or subdividing an
|
|
||||||
organization, or merging organizations. If propagation of a covered
|
|
||||||
work results from an entity transaction, each party to that
|
|
||||||
transaction who receives a copy of the work also receives whatever
|
|
||||||
licenses to the work the party's predecessor in interest had or could
|
|
||||||
give under the previous paragraph, plus a right to possession of the
|
|
||||||
Corresponding Source of the work from the predecessor in interest, if
|
|
||||||
the predecessor has it or can get it with reasonable efforts.
|
|
||||||
|
|
||||||
You may not impose any further restrictions on the exercise of the
|
|
||||||
rights granted or affirmed under this License. For example, you may
|
|
||||||
not impose a license fee, royalty, or other charge for exercise of
|
|
||||||
rights granted under this License, and you may not initiate litigation
|
|
||||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
||||||
any patent claim is infringed by making, using, selling, offering for
|
|
||||||
sale, or importing the Program or any portion of it.
|
|
||||||
|
|
||||||
#### 11. Patents.
|
|
||||||
|
|
||||||
A "contributor" is a copyright holder who authorizes use under this
|
|
||||||
License of the Program or a work on which the Program is based. The
|
|
||||||
work thus licensed is called the contributor's "contributor version".
|
|
||||||
|
|
||||||
A contributor's "essential patent claims" are all patent claims owned
|
|
||||||
or controlled by the contributor, whether already acquired or
|
|
||||||
hereafter acquired, that would be infringed by some manner, permitted
|
|
||||||
by this License, of making, using, or selling its contributor version,
|
|
||||||
but do not include claims that would be infringed only as a
|
|
||||||
consequence of further modification of the contributor version. For
|
|
||||||
purposes of this definition, "control" includes the right to grant
|
|
||||||
patent sublicenses in a manner consistent with the requirements of
|
|
||||||
this License.
|
|
||||||
|
|
||||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
|
||||||
patent license under the contributor's essential patent claims, to
|
|
||||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
|
||||||
propagate the contents of its contributor version.
|
|
||||||
|
|
||||||
In the following three paragraphs, a "patent license" is any express
|
|
||||||
agreement or commitment, however denominated, not to enforce a patent
|
|
||||||
(such as an express permission to practice a patent or covenant not to
|
|
||||||
sue for patent infringement). To "grant" such a patent license to a
|
|
||||||
party means to make such an agreement or commitment not to enforce a
|
|
||||||
patent against the party.
|
|
||||||
|
|
||||||
If you convey a covered work, knowingly relying on a patent license,
|
|
||||||
and the Corresponding Source of the work is not available for anyone
|
|
||||||
to copy, free of charge and under the terms of this License, through a
|
|
||||||
publicly available network server or other readily accessible means,
|
|
||||||
then you must either (1) cause the Corresponding Source to be so
|
|
||||||
available, or (2) arrange to deprive yourself of the benefit of the
|
|
||||||
patent license for this particular work, or (3) arrange, in a manner
|
|
||||||
consistent with the requirements of this License, to extend the patent
|
|
||||||
license to downstream recipients. "Knowingly relying" means you have
|
|
||||||
actual knowledge that, but for the patent license, your conveying the
|
|
||||||
covered work in a country, or your recipient's use of the covered work
|
|
||||||
in a country, would infringe one or more identifiable patents in that
|
|
||||||
country that you have reason to believe are valid.
|
|
||||||
|
|
||||||
If, pursuant to or in connection with a single transaction or
|
|
||||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
|
||||||
covered work, and grant a patent license to some of the parties
|
|
||||||
receiving the covered work authorizing them to use, propagate, modify
|
|
||||||
or convey a specific copy of the covered work, then the patent license
|
|
||||||
you grant is automatically extended to all recipients of the covered
|
|
||||||
work and works based on it.
|
|
||||||
|
|
||||||
A patent license is "discriminatory" if it does not include within the
|
|
||||||
scope of its coverage, prohibits the exercise of, or is conditioned on
|
|
||||||
the non-exercise of one or more of the rights that are specifically
|
|
||||||
granted under this License. You may not convey a covered work if you
|
|
||||||
are a party to an arrangement with a third party that is in the
|
|
||||||
business of distributing software, under which you make payment to the
|
|
||||||
third party based on the extent of your activity of conveying the
|
|
||||||
work, and under which the third party grants, to any of the parties
|
|
||||||
who would receive the covered work from you, a discriminatory patent
|
|
||||||
license (a) in connection with copies of the covered work conveyed by
|
|
||||||
you (or copies made from those copies), or (b) primarily for and in
|
|
||||||
connection with specific products or compilations that contain the
|
|
||||||
covered work, unless you entered into that arrangement, or that patent
|
|
||||||
license was granted, prior to 28 March 2007.
|
|
||||||
|
|
||||||
Nothing in this License shall be construed as excluding or limiting
|
|
||||||
any implied license or other defenses to infringement that may
|
|
||||||
otherwise be available to you under applicable patent law.
|
|
||||||
|
|
||||||
#### 12. No Surrender of Others' Freedom.
|
|
||||||
|
|
||||||
If conditions are imposed on you (whether by court order, agreement or
|
|
||||||
otherwise) that contradict the conditions of this License, they do not
|
|
||||||
excuse you from the conditions of this License. If you cannot convey a
|
|
||||||
covered work so as to satisfy simultaneously your obligations under
|
|
||||||
this License and any other pertinent obligations, then as a
|
|
||||||
consequence you may not convey it at all. For example, if you agree to
|
|
||||||
terms that obligate you to collect a royalty for further conveying
|
|
||||||
from those to whom you convey the Program, the only way you could
|
|
||||||
satisfy both those terms and this License would be to refrain entirely
|
|
||||||
from conveying the Program.
|
|
||||||
|
|
||||||
#### 13. Remote Network Interaction; Use with the GNU General Public License.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, if you modify the
|
|
||||||
Program, your modified version must prominently offer all users
|
|
||||||
interacting with it remotely through a computer network (if your
|
|
||||||
version supports such interaction) an opportunity to receive the
|
|
||||||
Corresponding Source of your version by providing access to the
|
|
||||||
Corresponding Source from a network server at no charge, through some
|
|
||||||
standard or customary means of facilitating copying of software. This
|
|
||||||
Corresponding Source shall include the Corresponding Source for any
|
|
||||||
work covered by version 3 of the GNU General Public License that is
|
|
||||||
incorporated pursuant to the following paragraph.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, you have
|
|
||||||
permission to link or combine any covered work with a work licensed
|
|
||||||
under version 3 of the GNU General Public License into a single
|
|
||||||
combined work, and to convey the resulting work. The terms of this
|
|
||||||
License will continue to apply to the part which is the covered work,
|
|
||||||
but the work with which it is combined will remain governed by version
|
|
||||||
3 of the GNU General Public License.
|
|
||||||
|
|
||||||
#### 14. Revised Versions of this License.
|
|
||||||
|
|
||||||
The Free Software Foundation may publish revised and/or new versions
|
|
||||||
of the GNU Affero General Public License from time to time. Such new
|
|
||||||
versions will be similar in spirit to the present version, but may
|
|
||||||
differ in detail to address new problems or concerns.
|
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the Program
|
|
||||||
specifies that a certain numbered version of the GNU Affero General
|
|
||||||
Public License "or any later version" applies to it, you have the
|
|
||||||
option of following the terms and conditions either of that numbered
|
|
||||||
version or of any later version published by the Free Software
|
|
||||||
Foundation. If the Program does not specify a version number of the
|
|
||||||
GNU Affero General Public License, you may choose any version ever
|
|
||||||
published by the Free Software Foundation.
|
|
||||||
|
|
||||||
If the Program specifies that a proxy can decide which future versions
|
|
||||||
of the GNU Affero General Public License can be used, that proxy's
|
|
||||||
public statement of acceptance of a version permanently authorizes you
|
|
||||||
to choose that version for the Program.
|
|
||||||
|
|
||||||
Later license versions may give you additional or different
|
|
||||||
permissions. However, no additional obligations are imposed on any
|
|
||||||
author or copyright holder as a result of your choosing to follow a
|
|
||||||
later version.
|
|
||||||
|
|
||||||
#### 15. Disclaimer of Warranty.
|
|
||||||
|
|
||||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
|
||||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
|
||||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
|
|
||||||
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
|
|
||||||
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
|
|
||||||
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
|
|
||||||
CORRECTION.
|
|
||||||
|
|
||||||
#### 16. Limitation of Liability.
|
|
||||||
|
|
||||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
|
||||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
|
|
||||||
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
|
||||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
|
||||||
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
|
|
||||||
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
|
|
||||||
LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
|
|
||||||
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
|
|
||||||
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
||||||
|
|
||||||
#### 17. Interpretation of Sections 15 and 16.
|
|
||||||
|
|
||||||
If the disclaimer of warranty and limitation of liability provided
|
|
||||||
above cannot be given local legal effect according to their terms,
|
|
||||||
reviewing courts shall apply local law that most closely approximates
|
|
||||||
an absolute waiver of all civil liability in connection with the
|
|
||||||
Program, unless a warranty or assumption of liability accompanies a
|
|
||||||
copy of the Program in return for a fee.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
First, run the development server:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run dev
|
|
||||||
# or
|
|
||||||
yarn dev
|
|
||||||
```
|
|
||||||
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
|
||||||
|
|
||||||
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
|
|
||||||
|
|
||||||
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
|
|
||||||
|
|
||||||
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
|
|
||||||
|
|
||||||
## Learn More
|
|
||||||
|
|
||||||
To learn more about Next.js, take a look at the following resources:
|
|
||||||
|
|
||||||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
|
||||||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
|
||||||
|
|
||||||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
|
|
||||||
|
|
||||||
## Deploy on Vercel
|
|
||||||
|
|
||||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
|
||||||
|
|
||||||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue