From 81c9042439b2d58a3b3989b404fe7d2e0547d0f3 Mon Sep 17 00:00:00 2001 From: khorshuheng Date: Sat, 28 Sep 2024 16:16:15 +0800 Subject: [PATCH] feat: send email to workspace owner when user request access --- .github/workflows/integration_test.yml | 5 +- ...386ea1566e45e31acd5fb51886aabcd98479.json} | 4 +- ...cdfa3a642d2b7276c2a81f877a6217a0d473.json} | 20 ++- ...0f1e56a62016246c5a522423102a9e9b6dae.json} | 20 ++- ...12694e530b64a105e03a040c65b779c9d91e.json} | 20 ++- .../build_production/access_request.html | 123 +++++++++++++++++ deploy.env | 2 +- dev.env | 2 +- docker-compose-ci.yml | 1 + email_template/config.js | 13 +- email_template/config.production.js | 8 +- .../src/templates/access_request.html | 129 ++++++++++++++++++ libs/database-entity/src/dto.rs | 1 + libs/database/src/access_request.rs | 1 + libs/database/src/pg_row.rs | 4 + libs/database/src/workspace.rs | 34 +++-- src/api/access_request.rs | 19 ++- src/biz/access_request/ops.rs | 44 +++++- src/biz/workspace/ops.rs | 2 +- src/mailer.rs | 100 ++++++++++++-- 20 files changed, 484 insertions(+), 68 deletions(-) rename .sqlx/{query-343cdf36e68c8333ecc6b778789d8de543c15f2aa0318dac2d10c5f1ef0f0232.json => query-0c3ae560880e82218d13c5992540386ea1566e45e31acd5fb51886aabcd98479.json} (57%) rename .sqlx/{query-29a6f76da0baf71c215b69078cce66d55f43d63f5c1c9e6786a4e80b52b4c5df.json => query-cf7b8baaba35e74671911e13f1efcdfa3a642d2b7276c2a81f877a6217a0d473.json} (59%) rename .sqlx/{query-2ebeb1af741d6866849af544be78ab44a44f9800265e49adf156b8b40b2d0f46.json => query-dbebcabe81603dca27ad9fc5a5df0f1e56a62016246c5a522423102a9e9b6dae.json} (64%) rename .sqlx/{query-04eb046efaa45999587db62cb32fa314f61997652c070870b44d23753ad48b5c.json => query-f448ae1b28ef69f884040016072b12694e530b64a105e03a040c65b779c9d91e.json} (68%) create mode 100644 assets/mailer_templates/build_production/access_request.html create mode 100644 email_template/src/templates/access_request.html diff --git a/.github/workflows/integration_test.yml b/.github/workflows/integration_test.yml index 347ca952..faf17c83 100644 --- a/.github/workflows/integration_test.yml +++ b/.github/workflows/integration_test.yml @@ -51,7 +51,7 @@ jobs: echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login --username appflowyinc --password-stdin docker push appflowyinc/appflowy_cloud:${GITHUB_SHA} docker push appflowyinc/appflowy_history:${GITHUB_SHA} - APPFLOWY_HISTORY_VERSION=${GITHUB_SHA} + APPFLOWY_HISTORY_VERSION=${GITHUB_SHA} APPFLOWY_CLOUD_VERSION=0.1.1 test: @@ -93,6 +93,7 @@ jobs: sed -i "s|LOCAL_AI_AWS_SECRET_ACCESS_KEY=.*|LOCAL_AI_AWS_SECRET_ACCESS_KEY=${{ secrets.LOCAL_AI_AWS_SECRET_ACCESS_KEY }}|" .env sed -i 's|APPFLOWY_INDEXER_REDIS_URL=.*|APPFLOWY_INDEXER_REDIS_URL=redis://localhost:6379|' .env sed -i 's|APPFLOWY_INDEXER_DATABASE_URL=.*|APPFLOWY_INDEXER_DATABASE_URL=postgres://postgres:password@localhost:5432/postgres|' .env + sed -i 's|APPFLOWY_WEB_URL=.*|APPFLOWY_WEB_URL=http://localhost:3000|' .env shell: bash - name: Update Nginx Configuration @@ -125,4 +126,4 @@ jobs: - name: Remove Docker Images from Docker Hub run: | TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "appflowyinc", "password": "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}"}' https://hub.docker.com/v2/users/login/ | jq -r .token) - curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/appflowyinc/${{ matrix.test_service }}/tags/${GITHUB_SHA}/ \ No newline at end of file + curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/appflowyinc/${{ matrix.test_service }}/tags/${GITHUB_SHA}/ diff --git a/.sqlx/query-343cdf36e68c8333ecc6b778789d8de543c15f2aa0318dac2d10c5f1ef0f0232.json b/.sqlx/query-0c3ae560880e82218d13c5992540386ea1566e45e31acd5fb51886aabcd98479.json similarity index 57% rename from .sqlx/query-343cdf36e68c8333ecc6b778789d8de543c15f2aa0318dac2d10c5f1ef0f0232.json rename to .sqlx/query-0c3ae560880e82218d13c5992540386ea1566e45e31acd5fb51886aabcd98479.json index c86517ca..bfc48c6b 100644 --- a/.sqlx/query-343cdf36e68c8333ecc6b778789d8de543c15f2aa0318dac2d10c5f1ef0f0232.json +++ b/.sqlx/query-0c3ae560880e82218d13c5992540386ea1566e45e31acd5fb51886aabcd98479.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH request_id_workspace_member_count AS (\n SELECT\n request_id,\n COUNT(*) AS member_count\n FROM af_access_request\n JOIN af_workspace_member USING (workspace_id)\n WHERE request_id = $1\n GROUP BY request_id\n )\n SELECT\n request_id,\n view_id,\n (\n workspace_id,\n af_workspace.database_storage_id,\n af_workspace.owner_uid,\n owner_profile.name,\n af_workspace.created_at,\n af_workspace.workspace_type,\n af_workspace.deleted_at,\n af_workspace.workspace_name,\n af_workspace.icon,\n request_id_workspace_member_count.member_count\n ) AS \"workspace!: AFWorkspaceWithMemberCountRow\",\n (\n af_user.uuid,\n af_user.name,\n af_user.email,\n af_user.metadata ->> 'icon_url'\n ) AS \"requester!: AFAccessRequesterColumn\",\n status AS \"status: AFAccessRequestStatusColumn\",\n af_access_request.created_at AS created_at\n FROM af_access_request\n JOIN af_user USING (uid)\n JOIN af_workspace USING (workspace_id)\n JOIN af_user AS owner_profile ON af_workspace.owner_uid = owner_profile.uid\n JOIN request_id_workspace_member_count USING (request_id)\n WHERE request_id = $1\n ", + "query": "\n WITH request_id_workspace_member_count AS (\n SELECT\n request_id,\n COUNT(*) AS member_count\n FROM af_access_request\n JOIN af_workspace_member USING (workspace_id)\n WHERE request_id = $1\n GROUP BY request_id\n )\n SELECT\n request_id,\n view_id,\n (\n workspace_id,\n af_workspace.database_storage_id,\n af_workspace.owner_uid,\n owner_profile.name,\n owner_profile.email,\n af_workspace.created_at,\n af_workspace.workspace_type,\n af_workspace.deleted_at,\n af_workspace.workspace_name,\n af_workspace.icon,\n request_id_workspace_member_count.member_count\n ) AS \"workspace!: AFWorkspaceWithMemberCountRow\",\n (\n af_user.uuid,\n af_user.name,\n af_user.email,\n af_user.metadata ->> 'icon_url'\n ) AS \"requester!: AFAccessRequesterColumn\",\n status AS \"status: AFAccessRequestStatusColumn\",\n af_access_request.created_at AS created_at\n FROM af_access_request\n JOIN af_user USING (uid)\n JOIN af_workspace USING (workspace_id)\n JOIN af_user AS owner_profile ON af_workspace.owner_uid = owner_profile.uid\n JOIN request_id_workspace_member_count USING (request_id)\n WHERE request_id = $1\n ", "describe": { "columns": [ { @@ -48,5 +48,5 @@ false ] }, - "hash": "343cdf36e68c8333ecc6b778789d8de543c15f2aa0318dac2d10c5f1ef0f0232" + "hash": "0c3ae560880e82218d13c5992540386ea1566e45e31acd5fb51886aabcd98479" } diff --git a/.sqlx/query-29a6f76da0baf71c215b69078cce66d55f43d63f5c1c9e6786a4e80b52b4c5df.json b/.sqlx/query-cf7b8baaba35e74671911e13f1efcdfa3a642d2b7276c2a81f877a6217a0d473.json similarity index 59% rename from .sqlx/query-29a6f76da0baf71c215b69078cce66d55f43d63f5c1c9e6786a4e80b52b4c5df.json rename to .sqlx/query-cf7b8baaba35e74671911e13f1efcdfa3a642d2b7276c2a81f877a6217a0d473.json index c2ce5ae7..4fa9b15e 100644 --- a/.sqlx/query-29a6f76da0baf71c215b69078cce66d55f43d63f5c1c9e6786a4e80b52b4c5df.json +++ b/.sqlx/query-cf7b8baaba35e74671911e13f1efcdfa3a642d2b7276c2a81f877a6217a0d473.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n INSERT INTO public.af_workspace (owner_uid, workspace_name)\n VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2)\n RETURNING\n workspace_id,\n database_storage_id,\n owner_uid,\n (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name,\n created_at,\n workspace_type,\n deleted_at,\n workspace_name,\n icon\n ;\n ", + "query": "\n WITH new_workspace AS (\n INSERT INTO public.af_workspace (owner_uid, workspace_name)\n VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2)\n RETURNING *\n )\n SELECT\n workspace_id,\n database_storage_id,\n owner_uid,\n owner_profile.name AS owner_name,\n owner_profile.email AS owner_email,\n new_workspace.created_at,\n workspace_type,\n new_workspace.deleted_at,\n workspace_name,\n icon\n FROM new_workspace\n JOIN public.af_user AS owner_profile ON new_workspace.owner_uid = owner_profile.uid;\n ", "describe": { "columns": [ { @@ -25,26 +25,31 @@ }, { "ordinal": 4, + "name": "owner_email", + "type_info": "Text" + }, + { + "ordinal": 5, "name": "created_at", "type_info": "Timestamptz" }, { - "ordinal": 5, + "ordinal": 6, "name": "workspace_type", "type_info": "Int4" }, { - "ordinal": 6, + "ordinal": 7, "name": "deleted_at", "type_info": "Timestamptz" }, { - "ordinal": 7, + "ordinal": 8, "name": "workspace_name", "type_info": "Text" }, { - "ordinal": 8, + "ordinal": 9, "name": "icon", "type_info": "Text" } @@ -59,7 +64,8 @@ false, false, false, - null, + false, + false, true, false, true, @@ -67,5 +73,5 @@ false ] }, - "hash": "29a6f76da0baf71c215b69078cce66d55f43d63f5c1c9e6786a4e80b52b4c5df" + "hash": "cf7b8baaba35e74671911e13f1efcdfa3a642d2b7276c2a81f877a6217a0d473" } diff --git a/.sqlx/query-2ebeb1af741d6866849af544be78ab44a44f9800265e49adf156b8b40b2d0f46.json b/.sqlx/query-dbebcabe81603dca27ad9fc5a5df0f1e56a62016246c5a522423102a9e9b6dae.json similarity index 64% rename from .sqlx/query-2ebeb1af741d6866849af544be78ab44a44f9800265e49adf156b8b40b2d0f46.json rename to .sqlx/query-dbebcabe81603dca27ad9fc5a5df0f1e56a62016246c5a522423102a9e9b6dae.json index b94bd630..5ab3dbf6 100644 --- a/.sqlx/query-2ebeb1af741d6866849af544be78ab44a44f9800265e49adf156b8b40b2d0f46.json +++ b/.sqlx/query-dbebcabe81603dca27ad9fc5a5df0f1e56a62016246c5a522423102a9e9b6dae.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n w.workspace_id,\n w.database_storage_id,\n w.owner_uid,\n (SELECT name FROM public.af_user WHERE uid = w.owner_uid) AS owner_name,\n w.created_at,\n w.workspace_type,\n w.deleted_at,\n w.workspace_name,\n w.icon\n FROM af_workspace w\n JOIN af_workspace_member wm ON w.workspace_id = wm.workspace_id\n WHERE wm.uid = (\n SELECT uid FROM public.af_user WHERE uuid = $1\n );\n ", + "query": "\n SELECT\n w.workspace_id,\n w.database_storage_id,\n w.owner_uid,\n u.name AS owner_name,\n u.email AS owner_email,\n w.created_at,\n w.workspace_type,\n w.deleted_at,\n w.workspace_name,\n w.icon\n FROM af_workspace w\n JOIN af_workspace_member wm ON w.workspace_id = wm.workspace_id\n JOIN public.af_user u ON w.owner_uid = u.uid\n WHERE wm.uid = (\n SELECT uid FROM public.af_user WHERE uuid = $1\n );\n ", "describe": { "columns": [ { @@ -25,26 +25,31 @@ }, { "ordinal": 4, + "name": "owner_email", + "type_info": "Text" + }, + { + "ordinal": 5, "name": "created_at", "type_info": "Timestamptz" }, { - "ordinal": 5, + "ordinal": 6, "name": "workspace_type", "type_info": "Int4" }, { - "ordinal": 6, + "ordinal": 7, "name": "deleted_at", "type_info": "Timestamptz" }, { - "ordinal": 7, + "ordinal": 8, "name": "workspace_name", "type_info": "Text" }, { - "ordinal": 8, + "ordinal": 9, "name": "icon", "type_info": "Text" } @@ -58,7 +63,8 @@ false, false, false, - null, + false, + false, true, false, true, @@ -66,5 +72,5 @@ false ] }, - "hash": "2ebeb1af741d6866849af544be78ab44a44f9800265e49adf156b8b40b2d0f46" + "hash": "dbebcabe81603dca27ad9fc5a5df0f1e56a62016246c5a522423102a9e9b6dae" } diff --git a/.sqlx/query-04eb046efaa45999587db62cb32fa314f61997652c070870b44d23753ad48b5c.json b/.sqlx/query-f448ae1b28ef69f884040016072b12694e530b64a105e03a040c65b779c9d91e.json similarity index 68% rename from .sqlx/query-04eb046efaa45999587db62cb32fa314f61997652c070870b44d23753ad48b5c.json rename to .sqlx/query-f448ae1b28ef69f884040016072b12694e530b64a105e03a040c65b779c9d91e.json index 93cd3969..5b7e7844 100644 --- a/.sqlx/query-04eb046efaa45999587db62cb32fa314f61997652c070870b44d23753ad48b5c.json +++ b/.sqlx/query-f448ae1b28ef69f884040016072b12694e530b64a105e03a040c65b779c9d91e.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n workspace_id,\n database_storage_id,\n owner_uid,\n (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name,\n created_at,\n workspace_type,\n deleted_at,\n workspace_name,\n icon\n FROM public.af_workspace WHERE workspace_id = $1\n ", + "query": "\n SELECT\n workspace_id,\n database_storage_id,\n owner_uid,\n owner_profile.name as owner_name,\n owner_profile.email as owner_email,\n af_workspace.created_at,\n workspace_type,\n af_workspace.deleted_at,\n workspace_name,\n icon\n FROM public.af_workspace\n JOIN public.af_user owner_profile ON af_workspace.owner_uid = owner_profile.uid\n WHERE workspace_id = $1\n ", "describe": { "columns": [ { @@ -25,26 +25,31 @@ }, { "ordinal": 4, + "name": "owner_email", + "type_info": "Text" + }, + { + "ordinal": 5, "name": "created_at", "type_info": "Timestamptz" }, { - "ordinal": 5, + "ordinal": 6, "name": "workspace_type", "type_info": "Int4" }, { - "ordinal": 6, + "ordinal": 7, "name": "deleted_at", "type_info": "Timestamptz" }, { - "ordinal": 7, + "ordinal": 8, "name": "workspace_name", "type_info": "Text" }, { - "ordinal": 8, + "ordinal": 9, "name": "icon", "type_info": "Text" } @@ -58,7 +63,8 @@ false, false, false, - null, + false, + false, true, false, true, @@ -66,5 +72,5 @@ false ] }, - "hash": "04eb046efaa45999587db62cb32fa314f61997652c070870b44d23753ad48b5c" + "hash": "f448ae1b28ef69f884040016072b12694e530b64a105e03a040c65b779c9d91e" } diff --git a/assets/mailer_templates/build_production/access_request.html b/assets/mailer_templates/build_production/access_request.html new file mode 100644 index 00000000..960352d3 --- /dev/null +++ b/assets/mailer_templates/build_production/access_request.html @@ -0,0 +1,123 @@ + + + + + + + + + + + Request to join the workspace + + + +
+ Approve a user's request to join the workspace. +  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏ +
+
+
+ + + + + + + +
+
+ {{ username }} +
+

+ {{ username }} + has requested access to + {{ workspace_name }} +

+
+ + + + + +
+
+ {{ workspace_name }} +
+
+
{{ workspace_name }}
+
+ {{ workspace_member_count }} members +
+
+ +
+ By clicking "Approve request" above, the user will be added to the + workspace. +
+
+
+

+ + + +

+

+ Bring projects, knowledge, and teams together with the power of AI. +

+

+ + Maizzle + + + Maizzle + + + Maizzle + + + Maizzle + +

+
+
+
+ + \ No newline at end of file diff --git a/deploy.env b/deploy.env index e161ffb3..b823976d 100644 --- a/deploy.env +++ b/deploy.env @@ -141,4 +141,4 @@ APPFLOWY_COLLABORATE_MULTI_THREAD=false APPFLOWY_COLLABORATE_REMOVE_BATCH_SIZE=100 # AppFlowy Web -APPFLOWY_WEB_URL= +APPFLOWY_WEB_URL=http://localhost:3000 diff --git a/dev.env b/dev.env index bbec40c5..c6cbfbee 100644 --- a/dev.env +++ b/dev.env @@ -127,4 +127,4 @@ APPFLOWY_COLLABORATE_MULTI_THREAD=false APPFLOWY_COLLABORATE_REMOVE_BATCH_SIZE=100 # AppFlowy Web -APPFLOWY_WEB_URL= +APPFLOWY_WEB_URL=http://localhost:3000 diff --git a/docker-compose-ci.yml b/docker-compose-ci.yml index 96c570e1..dff77e04 100644 --- a/docker-compose-ci.yml +++ b/docker-compose-ci.yml @@ -113,6 +113,7 @@ services: - APPFLOWY_DATABASE_MAX_CONNECTIONS=20 - APPFLOWY_AI_SERVER_HOST=${APPFLOWY_AI_SERVER_HOST} - APPFLOWY_AI_SERVER_PORT=${APPFLOWY_AI_SERVER_PORT} + - APPFLOWY_WEB_URL=${APPFLOWY_WEB_URL} build: context: . dockerfile: Dockerfile diff --git a/email_template/config.js b/email_template/config.js index 3e6ed1aa..4338d118 100644 --- a/email_template/config.js +++ b/email_template/config.js @@ -14,23 +14,24 @@ module.exports = { build: { templates: { - source: 'src/templates', + source: "src/templates", destination: { - path: 'build_local', + path: "build_local", }, assets: { - source: 'src/images', - destination: 'images', + source: "src/images", + destination: "images", }, }, }, locals: { - cdnBaseUrl: '', + cdnBaseUrl: "", userIconUrl: "https://cdn-icons-png.flaticon.com/512/1077/1077012.png", userName: "John Doe", acceptUrl: "https://appflowy.io", + approveUrl: "https://appflowy.io", workspaceName: "AppFlowy", workspaceMembersCount: "100", workspaceIconURL: "https://cdn-icons-png.flaticon.com/512/1078/1078013.png", }, -} +}; diff --git a/email_template/config.production.js b/email_template/config.production.js index 0b37b317..832821eb 100644 --- a/email_template/config.production.js +++ b/email_template/config.production.js @@ -15,15 +15,17 @@ module.exports = { build: { templates: { destination: { - path: '../assets/mailer_templates/build_production', + path: "../assets/mailer_templates/build_production", }, }, }, locals: { - cdnBaseUrl: 'https://raw.githubusercontent.com/AppFlowy-IO/AppFlowy-Cloud/main/assets/mailer_templates/build_production/', + cdnBaseUrl: + "https://raw.githubusercontent.com/AppFlowy-IO/AppFlowy-Cloud/main/assets/mailer_templates/build_production/", userIconUrl: "{{ user_icon_url }}", userName: "{{ username }}", acceptUrl: "{{ accept_url }}", + approveUrl: "{{ approve_url }}", workspaceName: "{{ workspace_name }}", workspaceMembersCount: "{{ workspace_member_count }}", workspaceIconURL: "{{ workspace_icon_url }}", @@ -32,4 +34,4 @@ module.exports = { removeUnusedCSS: true, shorthandCSS: true, prettify: true, -} +}; diff --git a/email_template/src/templates/access_request.html b/email_template/src/templates/access_request.html new file mode 100644 index 00000000..f8aa5612 --- /dev/null +++ b/email_template/src/templates/access_request.html @@ -0,0 +1,129 @@ +--- +title: "Request to join the workspace" +preheader: "Approve a user's request to join the workspace." +bodyClass: bg-purple-50 +--- + + +
+ + + + + + + +
+
+ {{ userName }} +
+

+ {{ userName }} + has requested access to + {{ workspaceName }} +

+ + + + + + +
+
+ {{ workspaceName }} +
+
+
{{ workspaceName }}
+
+ {{ workspaceMembersCount }} members +
+
+ +
Approve request
+
+
+ By clicking "Approve request" above, the user will be added to the + workspace. +
+ +
+

+ + + +

+

+ Bring projects, knowledge, and teams together with the power of AI. +

+ +

+ + Maizzle + + + Maizzle + + + Maizzle + + + Maizzle + +

+
+
+
diff --git a/libs/database-entity/src/dto.rs b/libs/database-entity/src/dto.rs index f53625dd..5c618f8a 100644 --- a/libs/database-entity/src/dto.rs +++ b/libs/database-entity/src/dto.rs @@ -527,6 +527,7 @@ pub struct AFWorkspace { pub database_storage_id: Uuid, pub owner_uid: i64, pub owner_name: String, + pub owner_email: String, pub workspace_type: i32, pub workspace_name: String, pub created_at: DateTime, diff --git a/libs/database/src/access_request.rs b/libs/database/src/access_request.rs index 4650fd1e..9b77a709 100644 --- a/libs/database/src/access_request.rs +++ b/libs/database/src/access_request.rs @@ -71,6 +71,7 @@ pub async fn select_access_request_by_request_id<'a, E: Executor<'a, Database = af_workspace.database_storage_id, af_workspace.owner_uid, owner_profile.name, + owner_profile.email, af_workspace.created_at, af_workspace.workspace_type, af_workspace.deleted_at, diff --git a/libs/database/src/pg_row.rs b/libs/database/src/pg_row.rs index 357bd2b2..28e04e52 100644 --- a/libs/database/src/pg_row.rs +++ b/libs/database/src/pg_row.rs @@ -19,6 +19,7 @@ pub struct AFWorkspaceRow { pub database_storage_id: Option, pub owner_uid: Option, pub owner_name: Option, + pub owner_email: Option, pub created_at: Option>, pub workspace_type: i32, pub deleted_at: Option>, @@ -46,6 +47,7 @@ impl TryFrom for AFWorkspace { database_storage_id, owner_uid, owner_name: value.owner_name.unwrap_or_default(), + owner_email: value.owner_email.unwrap_or_default(), workspace_type: value.workspace_type, workspace_name, created_at, @@ -61,6 +63,7 @@ pub struct AFWorkspaceWithMemberCountRow { pub database_storage_id: Option, pub owner_uid: Option, pub owner_name: Option, + pub owner_email: Option, pub created_at: Option>, pub workspace_type: i32, pub deleted_at: Option>, @@ -89,6 +92,7 @@ impl TryFrom for AFWorkspace { database_storage_id, owner_uid, owner_name: value.owner_name.unwrap_or_default(), + owner_email: value.owner_email.unwrap_or_default(), workspace_type: value.workspace_type, workspace_name, created_at, diff --git a/libs/database/src/workspace.rs b/libs/database/src/workspace.rs index 2c90b108..d6e15a7f 100644 --- a/libs/database/src/workspace.rs +++ b/libs/database/src/workspace.rs @@ -39,19 +39,24 @@ pub async fn insert_user_workspace( let workspace = sqlx::query_as!( AFWorkspaceRow, r#" - INSERT INTO public.af_workspace (owner_uid, workspace_name) - VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2) - RETURNING + WITH new_workspace AS ( + INSERT INTO public.af_workspace (owner_uid, workspace_name) + VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2) + RETURNING * + ) + SELECT workspace_id, database_storage_id, owner_uid, - (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name, - created_at, + owner_profile.name AS owner_name, + owner_profile.email AS owner_email, + new_workspace.created_at, workspace_type, - deleted_at, + new_workspace.deleted_at, workspace_name, icon - ; + FROM new_workspace + JOIN public.af_user AS owner_profile ON new_workspace.owner_uid = owner_profile.uid; "#, user_uuid, workspace_name, @@ -670,13 +675,16 @@ pub async fn select_workspace<'a, E: Executor<'a, Database = Postgres>>( workspace_id, database_storage_id, owner_uid, - (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name, - created_at, + owner_profile.name as owner_name, + owner_profile.email as owner_email, + af_workspace.created_at, workspace_type, - deleted_at, + af_workspace.deleted_at, workspace_name, icon - FROM public.af_workspace WHERE workspace_id = $1 + FROM public.af_workspace + JOIN public.af_user owner_profile ON af_workspace.owner_uid = owner_profile.uid + WHERE workspace_id = $1 "#, workspace_id ) @@ -718,7 +726,8 @@ pub async fn select_all_user_workspaces<'a, E: Executor<'a, Database = Postgres> w.workspace_id, w.database_storage_id, w.owner_uid, - (SELECT name FROM public.af_user WHERE uid = w.owner_uid) AS owner_name, + u.name AS owner_name, + u.email AS owner_email, w.created_at, w.workspace_type, w.deleted_at, @@ -726,6 +735,7 @@ pub async fn select_all_user_workspaces<'a, E: Executor<'a, Database = Postgres> w.icon FROM af_workspace w JOIN af_workspace_member wm ON w.workspace_id = wm.workspace_id + JOIN public.af_user u ON w.owner_uid = u.uid WHERE wm.uid = ( SELECT uid FROM public.af_user WHERE uuid = $1 ); diff --git a/src/api/access_request.rs b/src/api/access_request.rs index f8b87e67..ad577fc4 100644 --- a/src/api/access_request.rs +++ b/src/api/access_request.rs @@ -2,6 +2,8 @@ use actix_web::{ web::{self, Data, Json}, Result, Scope, }; +use anyhow::anyhow; +use app_error::AppError; use authentication::jwt::UserUuid; use database_entity::dto::{ AccessRequestMinimal, ApproveAccessRequestParams, CreateAccessRequestParams, @@ -52,7 +54,22 @@ async fn post_access_request_handler( let uid = state.user_cache.get_user_uid(&uuid).await?; let workspace_id = create_access_request_params.workspace_id; let view_id = create_access_request_params.view_id; - let request_id = create_access_request(&state.pg_pool, workspace_id, view_id, uid).await?; + let appflowy_web_url = state + .config + .appflowy_web_url + .clone() + .ok_or(AppError::Internal(anyhow!( + "AppFlowy web url has not been set" + )))?; + let request_id = create_access_request( + &state.pg_pool, + state.mailer.clone(), + &appflowy_web_url, + workspace_id, + view_id, + uid, + ) + .await?; let access_request = AccessRequestMinimal { request_id, workspace_id, diff --git a/src/biz/access_request/ops.rs b/src/biz/access_request/ops.rs index 723e7a18..bb3472b7 100644 --- a/src/biz/access_request/ops.rs +++ b/src/biz/access_request/ops.rs @@ -16,18 +16,56 @@ use shared_entity::dto::access_request_dto::{AccessRequest, AccessRequestView}; use sqlx::PgPool; use uuid::Uuid; -use crate::biz::collab::{ - folder_view::{to_dto_view_icon, to_view_layout}, - ops::get_latest_collab_folder, +use crate::{ + biz::collab::{ + folder_view::{to_dto_view_icon, to_view_layout}, + ops::get_latest_collab_folder, + }, + mailer::{Mailer, WorkspaceAccessRequestMailerParam}, }; pub async fn create_access_request( pg_pool: &PgPool, + mailer: Mailer, + appflowy_web_url: &str, workspace_id: Uuid, view_id: Uuid, uid: i64, ) -> Result { let request_id = insert_new_access_request(pg_pool, workspace_id, view_id, uid).await?; + let access_request = select_access_request_by_request_id(pg_pool, request_id).await?; + let cloned_mailer = mailer.clone(); + let approve_url = format!( + "{}/app/approve-request?request_id={}", + appflowy_web_url, request_id + ); + let email = access_request.workspace.owner_email.clone(); + let recipient_name = access_request.workspace.owner_name.clone(); + // use default icon until we have workspace icon + let workspace_icon_url = + "https://miro.medium.com/v2/resize:fit:2400/1*mTPfm7CwU31-tLhtLNkyJw.png".to_string(); + let user_icon_url = + "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" + .to_string(); + tokio::spawn(async move { + if let Err(err) = cloned_mailer + .send_workspace_access_request( + &recipient_name, + &email, + WorkspaceAccessRequestMailerParam { + user_icon_url, + username: access_request.requester.name, + workspace_name: access_request.workspace.workspace_name, + workspace_icon_url, + workspace_member_count: access_request.workspace.member_count.unwrap_or(0), + approve_url: approve_url.to_string(), + }, + ) + .await + { + tracing::error!("Failed to send access request email: {:?}", err); + }; + }); Ok(request_id) } diff --git a/src/biz/workspace/ops.rs b/src/biz/workspace/ops.rs index b82f7cf6..9fb55165 100644 --- a/src/biz/workspace/ops.rs +++ b/src/biz/workspace/ops.rs @@ -439,7 +439,7 @@ pub async fn invite_workspace_members( tokio::spawn(async move { if let Err(err) = cloned_mailer .send_workspace_invite( - invitation.email, + &invitation.email, WorkspaceInviteMailerParam { user_icon_url, username: inviter_name, diff --git a/src/mailer.rs b/src/mailer.rs index 8d4a797b..1f07ea9f 100644 --- a/src/mailer.rs +++ b/src/mailer.rs @@ -4,6 +4,7 @@ use lettre::transport::smtp::authentication::Credentials; use lettre::Address; use lettre::AsyncSmtpTransport; use lettre::AsyncTransport; +use std::collections::HashMap; use std::sync::Arc; use std::sync::RwLock; @@ -18,6 +19,9 @@ pub struct Mailer { smtp_username: String, } +pub const WORKSPACE_INVITE_TEMPLATE_NAME: &str = "workspace_invite"; +pub const WORKSPACE_ACCESS_REQUEST_TEMPLATE_NAME: &str = "workspace_access_request"; + impl Mailer { pub async fn new( smtp_username: String, @@ -33,12 +37,25 @@ impl Mailer { let workspace_invite_template = include_str!("../assets/mailer_templates/build_production/workspace_invitation.html"); + let access_request_template = + include_str!("../assets/mailer_templates/build_production/access_request.html"); + let template_strings = HashMap::from([ + (WORKSPACE_INVITE_TEMPLATE_NAME, workspace_invite_template), + ( + WORKSPACE_ACCESS_REQUEST_TEMPLATE_NAME, + access_request_template, + ), + ]); - HANDLEBARS - .write() - .map_err(|err| anyhow::anyhow!(format!("Failed to write handlebars: {}", err)))? - .register_template_string("workspace_invite", workspace_invite_template) - .map_err(|err| anyhow::anyhow!(format!("Failed to register handlebars template: {}", err)))?; + for (template_name, template_string) in template_strings { + HANDLEBARS + .write() + .map_err(|err| anyhow::anyhow!(format!("Failed to write handlebars: {}", err)))? + .register_template_string(template_name, template_string) + .map_err(|err| { + anyhow::anyhow!(format!("Failed to register handlebars template: {}", err)) + })?; + } Ok(Self { smtp_transport, @@ -46,13 +63,19 @@ impl Mailer { }) } - pub async fn send_workspace_invite( + async fn send_email_template( &self, - email: String, - param: WorkspaceInviteMailerParam, - ) -> Result<(), anyhow::Error> { + recipient_name: Option, + email: &str, + template_name: &str, + param: T, + subject: &str, + ) -> Result<(), anyhow::Error> + where + T: serde::Serialize, + { let rendered = match HANDLEBARS.read() { - Ok(registory) => registory.render("workspace_invite", ¶m)?, + Ok(registory) => registory.render(template_name, ¶m)?, Err(err) => anyhow::bail!(format!("Failed to render handlebars template: {}", err)), }; @@ -62,19 +85,56 @@ impl Mailer { self.smtp_username.parse::
()?, )) .to(lettre::message::Mailbox::new( - Some(param.username.clone()), + recipient_name, email.parse()?, )) - .subject(format!( - "Action required: {} invited you to {} in AppFlowy", - param.username, param.workspace_name - )) + .subject(subject) .header(ContentType::TEXT_HTML) .body(rendered)?; AsyncTransport::send(&self.smtp_transport, email).await?; Ok(()) } + pub async fn send_workspace_invite( + &self, + email: &str, + param: WorkspaceInviteMailerParam, + ) -> Result<(), anyhow::Error> { + let subject = format!( + "Action required: {} invited you to {} in AppFlowy", + param.username, param.workspace_name + ); + self + .send_email_template( + Some(param.username.clone()), + email, + WORKSPACE_INVITE_TEMPLATE_NAME, + param, + &subject, + ) + .await + } + + pub async fn send_workspace_access_request( + &self, + recipient_name: &str, + email: &str, + param: WorkspaceAccessRequestMailerParam, + ) -> Result<(), anyhow::Error> { + let subject = format!( + "Action required: {} requested access to {} in AppFlowy", + param.username, param.workspace_name + ); + self + .send_email_template( + Some(recipient_name.to_string()), + email, + WORKSPACE_ACCESS_REQUEST_TEMPLATE_NAME, + param, + &subject, + ) + .await + } } #[derive(serde::Serialize)] @@ -86,3 +146,13 @@ pub struct WorkspaceInviteMailerParam { pub workspace_member_count: String, pub accept_url: String, } + +#[derive(serde::Serialize)] +pub struct WorkspaceAccessRequestMailerParam { + pub user_icon_url: String, + pub username: String, + pub workspace_name: String, + pub workspace_icon_url: String, + pub workspace_member_count: i64, + pub approve_url: String, +}