Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: Integrate supabase (#2551)

Lucas.Xud842f228e883
diff --git a/frontend/appflowy_flutter/.gitignore b/frontend/appflowy_flutter/.gitignore
index 82c35e91cd..a0886f8dbc 100644
--- a/frontend/appflowy_flutter/.gitignore
+++ b/frontend/appflowy_flutter/.gitignore
@@ -68,3 +68,5 @@ windows/flutter/dart_ffi/
**/**/Brewfile.lock.json
**/.sandbox
**/.vscode/
+
+*.env
diff --git a/frontend/appflowy_flutter/assets/images/common/open_folder.svg b/frontend/appflowy_flutter/assets/images/common/open_folder.svg
new file mode 100644
index 0000000000..cd81df9271
--- /dev/null
+++ b/frontend/appflowy_flutter/assets/images/common/open_folder.svg
@@ -0,0 +1,3 @@
+<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path opacity="0.7" d="M14.1579 9L14.6673 8.57546C14.5413 8.42426 14.3547 8.33684 14.1579 8.33684V9ZM16 11.2105L15.4905 11.6351C15.6165 11.7863 15.8032 11.8737 16 11.8737V11.2105ZM9.66316 12.6843C9.66316 12.318 9.36625 12.0211 9 12.0211C8.63375 12.0211 8.33684 12.318 8.33684 12.6843H9.66316ZM8.33684 17.1053C8.33684 17.4716 8.63375 17.7685 9 17.7685C9.36625 17.7685 9.66316 17.4716 9.66316 17.1053H8.33684ZM23.6632 12.6843C23.6632 12.318 23.3663 12.0211 23 12.0211C22.6337 12.0211 22.3368 12.318 22.3368 12.6843H23.6632ZM22.3368 17.1053C22.3368 17.4716 22.6337 17.7685 23 17.7685C23.3663 17.7685 23.6632 17.4716 23.6632 17.1053H22.3368ZM9.66316 9.73684C9.66316 9.69614 9.69614 9.66316 9.73684 9.66316V8.33684C8.96364 8.33684 8.33684 8.96364 8.33684 9.73684H9.66316ZM9.73684 9.66316H14.1579V8.33684H9.73684V9.66316ZM13.6484 9.42454L15.4905 11.6351L16.5094 10.786L14.6673 8.57546L13.6484 9.42454ZM16 11.8737H22.2631V10.5474H16V11.8737ZM22.2631 11.8737C22.3038 11.8737 22.3368 11.9067 22.3368 11.9474H23.6631C23.6631 11.1742 23.0363 10.5474 22.2631 10.5474V11.8737ZM22.3368 11.9474V21.5263H23.6631V11.9474H22.3368ZM22.3368 21.5263C22.3368 21.567 22.3038 21.6 22.2631 21.6V22.9263C23.0363 22.9263 23.6631 22.2995 23.6631 21.5263H22.3368ZM22.2631 21.6H9.73684V22.9263H22.2631V21.6ZM9.73684 21.6C9.69614 21.6 9.66316 21.567 9.66316 21.5263H8.33684C8.33684 22.2995 8.96365 22.9263 9.73684 22.9263V21.6ZM9.66316 21.5263V9.73684H8.33684V21.5263H9.66316ZM22.9999 14.2317H9V15.558H22.9999V14.2317ZM8.33684 12.6843V17.1053H9.66316V12.6843H8.33684ZM22.3368 12.6843V17.1053H23.6632V12.6843H22.3368Z" fill="#333333"/>
+</svg>
diff --git a/frontend/appflowy_flutter/assets/images/common/recover.svg b/frontend/appflowy_flutter/assets/images/common/recover.svg
new file mode 100644
index 0000000000..38d77b51de
--- /dev/null
+++ b/frontend/appflowy_flutter/assets/images/common/recover.svg
@@ -0,0 +1,4 @@
+<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path fill-rule="evenodd" clip-rule="evenodd" d="M21.7874 10.1975C22.48 10.8859 23.0235 11.6885 23.4041 12.5811C23.7967 13.5077 24 14.4863 24 15.499C24 16.5117 23.7987 17.4943 23.4061 18.4189C23.0256 19.3115 22.482 20.1141 21.7894 20.8025C21.0968 21.491 20.2895 22.0314 19.3915 22.4096C18.4614 22.8019 17.4729 23 16.4541 23C15.4354 23 14.4469 22.7999 13.5167 22.4096C12.6188 22.0314 11.8114 21.491 11.1189 20.8025C10.8833 20.5684 10.8833 20.1881 11.1189 19.954C11.3544 19.7198 11.7369 19.7198 11.9725 19.954C13.1684 21.1448 14.7589 21.7992 16.4521 21.7992C18.1453 21.7992 19.7338 21.1428 20.9317 19.954C22.1296 18.7652 22.792 17.1801 22.792 15.499C22.792 13.8179 22.1316 12.2368 20.9357 11.046C19.7398 9.85524 18.1493 9.2008 16.4561 9.2008C15.6971 9.2008 14.9562 9.33089 14.2516 9.59106C13.5711 9.84323 12.9429 10.2055 12.3852 10.6718C11.6041 11.3242 10.996 12.1508 10.6075 13.0734L11.2618 12.7231C11.5557 12.565 11.9222 12.6751 12.0792 12.9673C12.2383 13.2595 12.1275 13.6237 11.8336 13.7799L9.92094 14.8005C9.92094 14.8025 9.91892 14.8025 9.91892 14.8025C9.82832 14.8506 9.72967 14.8746 9.63303 14.8746C9.44378 14.8746 9.26258 14.7885 9.14581 14.6304C9.14455 14.6267 9.14249 14.6237 9.14064 14.621C9.13954 14.6194 9.13851 14.6179 9.13776 14.6164C9.13575 14.6124 9.13323 14.6089 9.13071 14.6054C9.1282 14.6019 9.12568 14.5984 9.12367 14.5944C9.11561 14.5824 9.10756 14.5704 9.10152 14.5584L8.07272 12.6551C7.91367 12.3629 8.0244 11.9987 8.31834 11.8426C8.61228 11.6845 8.97871 11.7945 9.13575 12.0867L9.45989 12.6871C9.91892 11.5584 10.6558 10.5457 11.6061 9.75317C12.2705 9.1988 13.0174 8.76451 13.8288 8.46631C14.6663 8.1561 15.5501 8 16.4521 8C17.4708 8 18.4594 8.20013 19.3895 8.59039C20.2875 8.96865 21.0948 9.50901 21.7874 10.1975ZM15.52 12.1206C15.52 11.7884 15.7898 11.5202 16.124 11.5202C16.4582 11.5202 16.728 11.7884 16.728 12.1206V15.2747H19.8244C20.1587 15.2747 20.4284 15.5429 20.4284 15.8751C20.4284 16.2073 20.1587 16.4755 19.8244 16.4755H16.1481C15.8441 16.4755 15.5925 16.2514 15.5502 15.9612C15.5301 15.9031 15.52 15.8411 15.52 15.7751V12.1206Z" fill="#333333" fill-opacity="0.7"/>
+<path d="M23.4041 12.5811L23.4435 12.5643L23.4435 12.5642L23.4041 12.5811ZM21.7874 10.1975L21.7572 10.2279L21.7874 10.1975ZM23.4061 18.4189L23.4455 18.4358L23.4455 18.4357L23.4061 18.4189ZM19.3915 22.4096L19.3749 22.3701L19.3749 22.3701L19.3915 22.4096ZM13.5167 22.4096L13.5001 22.4491L13.5001 22.4491L13.5167 22.4096ZM11.1189 20.8025L11.1491 20.7721L11.1189 20.8025ZM11.1189 19.954L11.1491 19.9844L11.1189 19.954ZM11.9725 19.954L12.0027 19.9236L12.0027 19.9236L11.9725 19.954ZM20.9317 19.954L20.9619 19.9844L20.9317 19.954ZM20.9357 11.046L20.9055 11.0764L20.9357 11.046ZM14.2516 9.59106L14.2367 9.55086L14.2367 9.55087L14.2516 9.59106ZM12.3852 10.6718L12.4127 10.7047L12.4127 10.7047L12.3852 10.6718ZM10.6075 13.0734L10.568 13.0567L10.521 13.1683L10.6277 13.1112L10.6075 13.0734ZM11.2618 12.7231L11.282 12.7609L11.2821 12.7609L11.2618 12.7231ZM12.0792 12.9673L12.0415 12.9876L12.0416 12.9878L12.0792 12.9673ZM11.8336 13.7799L11.8135 13.742L11.8134 13.742L11.8336 13.7799ZM9.92094 14.8005L9.90076 14.7627L9.87808 14.7748V14.8005H9.92094ZM9.91892 14.8025L9.9189 14.7597L9.90825 14.7597L9.89885 14.7647L9.91892 14.8025ZM9.14581 14.6304L9.10311 14.6447L9.11134 14.6559L9.14581 14.6304ZM9.14064 14.621L9.10537 14.6453L9.10537 14.6453L9.14064 14.621ZM9.13776 14.6164L9.17605 14.5972L9.17604 14.5972L9.13776 14.6164ZM9.13071 14.6054L9.09591 14.6304L9.09591 14.6304L9.13071 14.6054ZM9.12367 14.5944L9.16225 14.575L9.15926 14.5705L9.12367 14.5944ZM9.10152 14.5584L9.13982 14.5391L9.13922 14.538L9.10152 14.5584ZM8.07272 12.6551L8.11042 12.6347L8.11036 12.6346L8.07272 12.6551ZM8.31834 11.8426L8.33844 11.8804L8.33864 11.8803L8.31834 11.8426ZM9.13575 12.0867L9.09799 12.107L9.09803 12.1071L9.13575 12.0867ZM9.45989 12.6871L9.42218 12.7075L9.46535 12.7875L9.49959 12.7033L9.45989 12.6871ZM11.6061 9.75317L11.6335 9.78608L11.6335 9.78608L11.6061 9.75317ZM13.8288 8.46631L13.8436 8.50654L13.8437 8.5065L13.8288 8.46631ZM19.3895 8.59039L19.4062 8.5509L19.4061 8.55087L19.3895 8.59039ZM16.728 15.2747H16.6851V15.3176H16.728V15.2747ZM15.5502 15.9612L15.5934 15.9549L15.5907 15.9471L15.5502 15.9612ZM23.4435 12.5642C23.0608 11.6666 22.5141 10.8594 21.8176 10.1671L21.7572 10.2279C22.4458 10.9124 22.9863 11.7104 23.3646 12.5979L23.4435 12.5642ZM24.0429 15.499C24.0429 14.4805 23.8383 13.4961 23.4435 12.5643L23.3646 12.5978C23.755 13.5192 23.9571 14.4922 23.9571 15.499H24.0429ZM23.4455 18.4357C23.8404 17.5058 24.0429 16.5174 24.0429 15.499H23.9571C23.9571 16.5059 23.757 17.4829 23.3666 18.4022L23.4455 18.4357ZM21.8196 20.8329C22.5161 20.1406 23.0628 19.3334 23.4455 18.4358L23.3667 18.4021C22.9883 19.2896 22.4478 20.0876 21.7592 20.7721L21.8196 20.8329ZM19.4082 22.4491C20.3112 22.0687 21.1231 21.5253 21.8196 20.8329L21.7592 20.7721C21.0705 21.4567 20.2678 21.994 19.3749 22.3701L19.4082 22.4491ZM16.4541 23.0429C17.4785 23.0429 18.4727 22.8436 19.4082 22.4491L19.3749 22.3701C18.4501 22.7601 17.4672 22.9571 16.4541 22.9571V23.0429ZM13.5001 22.4491C14.4355 22.8416 15.4297 23.0429 16.4541 23.0429V22.9571C15.4411 22.9571 14.4582 22.7581 13.5333 22.3701L13.5001 22.4491ZM11.0886 20.8329C11.7851 21.5253 12.5971 22.0687 13.5001 22.4491L13.5333 22.3701C12.6405 21.994 11.8377 21.4567 11.1491 20.7721L11.0886 20.8329ZM11.0886 19.9236C10.8362 20.1745 10.8362 20.582 11.0886 20.8329L11.1491 20.7721C10.9304 20.5547 10.9304 20.2018 11.1491 19.9844L11.0886 19.9236ZM12.0027 19.9236C11.7504 19.6728 11.3409 19.6728 11.0886 19.9236L11.1491 19.9844C11.3679 19.7668 11.7234 19.7668 11.9423 19.9844L12.0027 19.9236ZM16.4521 21.7563C14.7702 21.7563 13.1906 21.1064 12.0027 19.9236L11.9423 19.9843C13.1462 21.1832 14.7476 21.8421 16.4521 21.8421V21.7563ZM20.9015 19.9235C19.7116 21.1044 18.134 21.7563 16.4521 21.7563V21.8421C18.1566 21.8421 19.756 21.1811 20.9619 19.9844L20.9015 19.9235ZM22.7492 15.499C22.7492 17.1686 22.0914 18.7428 20.9015 19.9235L20.9619 19.9844C22.1679 18.7876 22.8349 17.1916 22.8349 15.499H22.7492ZM20.9055 11.0764C22.0934 12.2592 22.7492 13.8294 22.7492 15.499H22.8349C22.8349 13.8064 22.1699 12.2144 20.966 11.0157L20.9055 11.0764ZM16.4561 9.24366C18.1381 9.24366 19.7177 9.89365 20.9055 11.0764L20.966 11.0157C19.762 9.81683 18.1606 9.15794 16.4561 9.15794V9.24366ZM14.2664 9.63127C14.9662 9.37289 15.7021 9.24366 16.4561 9.24366V9.15794C15.6922 9.15794 14.9462 9.28889 14.2367 9.55086L14.2664 9.63127ZM12.4127 10.7047C12.9666 10.2415 13.5905 9.88174 14.2665 9.63125L14.2367 9.55087C13.5517 9.80472 12.9192 10.1694 12.3577 10.6389L12.4127 10.7047ZM10.647 13.09C11.0329 12.1737 11.6368 11.3527 12.4127 10.7047L12.3578 10.6389C11.5713 11.2957 10.9592 12.1278 10.568 13.0567L10.647 13.09ZM11.2416 12.6854L10.5872 13.0356L10.6277 13.1112L11.282 12.7609L11.2416 12.6854ZM12.117 12.947C11.9487 12.6339 11.5562 12.5161 11.2415 12.6854L11.2821 12.7609C11.5553 12.614 11.8957 12.7164 12.0415 12.9876L12.117 12.947ZM11.8537 13.8177C12.1685 13.6505 12.2874 13.2601 12.1168 12.9468L12.0416 12.9878C12.1891 13.2589 12.0865 13.597 11.8135 13.742L11.8537 13.8177ZM9.94111 14.8383L11.8538 13.8177L11.8134 13.742L9.90076 14.7627L9.94111 14.8383ZM9.91892 14.8025C9.91894 14.8454 9.91896 14.8454 9.91898 14.8454C9.91899 14.8454 9.91901 14.8454 9.91903 14.8454C9.91905 14.8454 9.91908 14.8454 9.91911 14.8454C9.91917 14.8454 9.91922 14.8454 9.91928 14.8454C9.9194 14.8454 9.91952 14.8454 9.91964 14.8454C9.91987 14.8454 9.92012 14.8454 9.92038 14.8454C9.9209 14.8454 9.92146 14.8453 9.92206 14.8453C9.92325 14.8452 9.92467 14.845 9.92626 14.8448C9.92924 14.8443 9.93387 14.8432 9.93902 14.8407C9.94433 14.838 9.95098 14.8334 9.95622 14.8256C9.96164 14.8175 9.96379 14.8086 9.96379 14.8005H9.87808C9.87808 14.7935 9.87998 14.7853 9.88502 14.7778C9.88988 14.7706 9.89603 14.7663 9.90084 14.7639C9.90549 14.7616 9.90961 14.7607 9.91221 14.7602C9.91361 14.76 9.91488 14.7599 9.91595 14.7598C9.9165 14.7597 9.91702 14.7597 9.91751 14.7597C9.91775 14.7597 9.91799 14.7597 9.91822 14.7597C9.91834 14.7597 9.91845 14.7597 9.91857 14.7597C9.91862 14.7597 9.91868 14.7597 9.91873 14.7597C9.91876 14.7597 9.91879 14.7597 9.91882 14.7597C9.91883 14.7597 9.91885 14.7597 9.91886 14.7597C9.91888 14.7597 9.9189 14.7597 9.91892 14.8025ZM9.63303 14.9174C9.73673 14.9174 9.84227 14.8917 9.939 14.8404L9.89885 14.7647C9.81438 14.8095 9.72261 14.8317 9.63303 14.8317V14.9174ZM9.11134 14.6559C9.23651 14.8254 9.43073 14.9174 9.63303 14.9174V14.8317C9.45684 14.8317 9.28866 14.7517 9.18029 14.605L9.11134 14.6559ZM9.10537 14.6453C9.10749 14.6484 9.10609 14.6468 9.10518 14.644L9.18645 14.6168C9.183 14.6065 9.17749 14.5989 9.17591 14.5966L9.10537 14.6453ZM9.09947 14.6357C9.10181 14.6403 9.10468 14.6443 9.10537 14.6453L9.17591 14.5966L9.17605 14.5972L9.09947 14.6357ZM9.09591 14.6304C9.0987 14.6343 9.09921 14.6352 9.09947 14.6357L9.17604 14.5972C9.17228 14.5897 9.16776 14.5835 9.16552 14.5804L9.09591 14.6304ZM9.08538 14.6137C9.08915 14.6211 9.09367 14.6273 9.09591 14.6304L9.16552 14.5804C9.16272 14.5765 9.16221 14.5757 9.16195 14.5751L9.08538 14.6137ZM9.06323 14.5776C9.0707 14.5925 9.08035 14.6068 9.08807 14.6183L9.15926 14.5705C9.15087 14.558 9.14442 14.5483 9.13981 14.5391L9.06323 14.5776ZM8.03502 12.6755L9.06382 14.5788L9.13922 14.538L8.11042 12.6347L8.03502 12.6755ZM8.29824 11.8047C7.98338 11.9719 7.86455 12.3623 8.03508 12.6756L8.11036 12.6346C7.96278 12.3635 8.06541 12.0254 8.33844 11.8804L8.29824 11.8047ZM9.1735 12.0664C9.0052 11.7533 8.61274 11.6355 8.29804 11.8048L8.33864 11.8803C8.61183 11.7334 8.95222 11.8358 9.09799 12.107L9.1735 12.0664ZM9.4976 12.6668L9.17346 12.0664L9.09803 12.1071L9.42218 12.7075L9.4976 12.6668ZM11.5786 9.72026C10.6229 10.5173 9.88187 11.5357 9.42019 12.671L9.49959 12.7033C9.95598 11.581 10.6886 10.5741 11.6335 9.78608L11.5786 9.72026ZM13.814 8.42608C12.9979 8.72601 12.2467 9.1628 11.5786 9.72026L11.6335 9.78608C12.2942 9.2348 13.0369 8.80301 13.8436 8.50654L13.814 8.42608ZM16.4521 7.95714C15.5452 7.95714 14.6563 8.11412 13.8139 8.42612L13.8437 8.5065C14.6763 8.19809 15.5551 8.04286 16.4521 8.04286V7.95714ZM19.4061 8.55087C18.4707 8.15841 17.4765 7.95714 16.4521 7.95714V8.04286C17.4651 8.04286 18.448 8.24185 19.3729 8.62991L19.4061 8.55087ZM21.8176 10.1671C21.1211 9.47472 20.3092 8.93128 19.4062 8.5509L19.3729 8.62989C20.2658 9.00601 21.0685 9.54329 21.7572 10.2279L21.8176 10.1671ZM16.124 11.4774C15.7663 11.4774 15.4771 11.7645 15.4771 12.1206H15.5628C15.5628 11.8123 15.8132 11.5631 16.124 11.5631V11.4774ZM16.7708 12.1206C16.7708 11.7645 16.4816 11.4774 16.124 11.4774V11.5631C16.4348 11.5631 16.6851 11.8123 16.6851 12.1206H16.7708ZM16.7708 15.2747V12.1206H16.6851V15.2747H16.7708ZM19.8244 15.2319H16.728V15.3176H19.8244V15.2319ZM20.4713 15.8751C20.4713 15.519 20.1821 15.2319 19.8244 15.2319V15.3176C20.1352 15.3176 20.3856 15.5668 20.3856 15.8751H20.4713ZM19.8244 16.5184C20.1821 16.5184 20.4713 16.2313 20.4713 15.8751H20.3856C20.3856 16.1834 20.1352 16.4327 19.8244 16.4327V16.5184ZM16.1481 16.5184H19.8244V16.4327H16.1481V16.5184ZM15.5078 15.9674C15.5531 16.2784 15.8227 16.5184 16.1481 16.5184V16.4327C15.8656 16.4327 15.6318 16.2243 15.5926 15.955L15.5078 15.9674ZM15.4771 15.7751C15.4771 15.8455 15.4879 15.9123 15.5097 15.9752L15.5907 15.9471C15.5722 15.8939 15.5628 15.8367 15.5628 15.7751H15.4771ZM15.4771 12.1206V15.7751H15.5628V12.1206H15.4771Z" fill="#333333"/>
+</svg>
diff --git a/frontend/appflowy_flutter/assets/images/editor/duplicate.svg b/frontend/appflowy_flutter/assets/images/editor/duplicate.svg
new file mode 100644
index 0000000000..40b5ed5a95
--- /dev/null
+++ b/frontend/appflowy_flutter/assets/images/editor/duplicate.svg
@@ -0,0 +1,4 @@
+<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path d="M11.974 6.33301H7.35865C6.7922 6.33301 6.33301 6.7922 6.33301 7.35865V11.974C6.33301 12.5405 6.7922 12.9997 7.35865 12.9997H11.974C12.5405 12.9997 12.9997 12.5405 12.9997 11.974V7.35865C12.9997 6.7922 12.5405 6.33301 11.974 6.33301Z" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M4.53846 9.66667H4.02564C3.75362 9.66667 3.49275 9.55861 3.3004 9.36626C3.10806 9.17392 3 8.91304 3 8.64103V4.02564C3 3.75362 3.10806 3.49275 3.3004 3.3004C3.49275 3.10806 3.75362 3 4.02564 3H8.64103C8.91304 3 9.17392 3.10806 9.36626 3.3004C9.55861 3.49275 9.66667 3.75362 9.66667 4.02564V4.53846" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
+</svg>
diff --git a/frontend/appflowy_flutter/assets/images/login/discord-mark.svg b/frontend/appflowy_flutter/assets/images/login/discord-mark.svg
new file mode 100644
index 0000000000..640e06af95
--- /dev/null
+++ b/frontend/appflowy_flutter/assets/images/login/discord-mark.svg
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="127px" height="96px" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g><path style="opacity:0.982" fill="#5864f2" d="M 40.5,-0.5 C 42.5,-0.5 44.5,-0.5 46.5,-0.5C 46.7534,3.03777 48.5868,5.03777 52,5.5C 59.3333,4.16667 66.6667,4.16667 74,5.5C 77.4132,5.03777 79.2466,3.03777 79.5,-0.5C 81.8333,-0.5 84.1667,-0.5 86.5,-0.5C 93.6784,1.84095 100.845,4.50762 108,7.5C 117.556,21.9281 123.723,37.5947 126.5,54.5C 126.5,63.1667 126.5,71.8333 126.5,80.5C 117.234,86.2996 107.567,91.2996 97.5,95.5C 95.8333,95.5 94.1667,95.5 92.5,95.5C 90.2583,92.0205 88.2583,88.3538 86.5,84.5C 89.9621,82.9349 93.2954,81.1016 96.5,79C 95.8333,78.8333 95.1667,78.6667 94.5,78.5C 80.6757,84.0674 66.3424,85.9007 51.5,84C 44.0284,82.4993 36.6951,80.8327 29.5,79C 32.7046,81.1016 36.0379,82.9349 39.5,84.5C 37.7417,88.3538 35.7417,92.0205 33.5,95.5C 31.8333,95.5 30.1667,95.5 28.5,95.5C 18.4329,91.2996 8.76625,86.2996 -0.5,80.5C -0.5,71.8333 -0.5,63.1667 -0.5,54.5C 2.27734,37.5947 8.44401,21.9281 18,7.5C 25.419,4.30685 32.919,1.64019 40.5,-0.5 Z M 38.5,40.5 C 51.8373,41.4956 55.6706,48.1623 50,60.5C 42.3899,66.5776 36.3899,65.2442 32,56.5C 30.062,49.3891 32.2287,44.0558 38.5,40.5 Z M 80.5,40.5 C 93.8373,41.4956 97.6706,48.1623 92,60.5C 84.3899,66.5776 78.3899,65.2442 74,56.5C 72.062,49.3891 74.2287,44.0558 80.5,40.5 Z"/></g>
+</svg>
diff --git a/frontend/appflowy_flutter/assets/images/login/github-mark.svg b/frontend/appflowy_flutter/assets/images/login/github-mark.svg
new file mode 100644
index 0000000000..d5e6491854
--- /dev/null
+++ b/frontend/appflowy_flutter/assets/images/login/github-mark.svg
@@ -0,0 +1 @@
+<svg width="98" height="96" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" fill="#fff"/></svg>
\ No newline at end of file
diff --git a/frontend/appflowy_flutter/assets/images/login/google-mark.svg b/frontend/appflowy_flutter/assets/images/login/google-mark.svg
new file mode 100644
index 0000000000..5a65dae32d
--- /dev/null
+++ b/frontend/appflowy_flutter/assets/images/login/google-mark.svg
@@ -0,0 +1 @@
+<svg xmlns:xlink="http://www.w3.org/1999/xlink" data-e2e="" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" width="1" height="1" ><path fill-rule="evenodd" clip-rule="evenodd" d="M43 24.4313C43 23.084 42.8767 21.7885 42.6475 20.5449H24.3877V27.8945H34.8219C34.3724 30.2695 33.0065 32.2818 30.9532 33.6291V38.3964H37.2189C40.885 35.0886 43 30.2177 43 24.4313Z" fill="#4285F4"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M24.3872 43.001C29.6219 43.001 34.0107 41.2996 37.2184 38.3978L30.9527 33.6305C29.2165 34.7705 26.9958 35.4441 24.3872 35.4441C19.3375 35.4441 15.0633 32.1018 13.5388 27.6108H7.06152V32.5337C10.2517 38.7433 16.8082 43.001 24.3872 43.001Z" fill="#34A853"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M13.5395 27.6094C13.1516 26.4695 12.9313 25.2517 12.9313 23.9994C12.9313 22.7472 13.1516 21.5295 13.5395 20.3894V15.4668H7.06217C5.74911 18.0318 5 20.9336 5 23.9994C5 27.0654 5.74911 29.9673 7.06217 32.5323L13.5395 27.6094Z" fill="#FBBC04"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M24.3872 12.5568C27.2336 12.5568 29.7894 13.5155 31.7987 15.3982L37.3595 9.94866C34.0018 6.88281 29.6131 5 24.3872 5C16.8082 5 10.2517 9.25777 7.06152 15.4674L13.5388 20.39C15.0633 15.8991 19.3375 12.5568 24.3872 12.5568Z" fill="#EA4335"></path></svg>
\ No newline at end of file
diff --git a/frontend/appflowy_flutter/assets/translations/en.json b/frontend/appflowy_flutter/assets/translations/en.json
index a43c32e849..0c2b62ea5d 100644
--- a/frontend/appflowy_flutter/assets/translations/en.json
+++ b/frontend/appflowy_flutter/assets/translations/en.json
@@ -21,6 +21,7 @@
"signIn": {
"loginTitle": "Login to @:appName",
"loginButtonText": "Login",
+ "loginAsGuestButtonText": "Get Started",
"buttonText": "Sign In",
"forgotPassword": "Forgot Password?",
"emailHint": "Email",
@@ -184,7 +185,8 @@
"theme": "Theme"
},
"files": {
- "defaultLocation": "Where your data is stored now",
+ "copy": "Copy",
+ "defaultLocation": "Read files and data storage location",
"doubleTapToCopy": "Double tap to copy the path",
"restoreLocation": "Restore to AppFlowy default path",
"customizeLocation": "Open another folder",
@@ -203,7 +205,11 @@
"create": "Create",
"folderPath": "Path to store your folder",
"locationCannotBeEmpty": "Path cannot be empty",
- "pathCopiedSnackbar": "File storage path copied to clipboard!"
+ "pathCopiedSnackbar": "File storage path copied to clipboard!",
+ "changeLocationTooltips": "Change the files read the data directory",
+ "change": "Change",
+ "openLocationTooltips": "Open the files read the data directory",
+ "recoverLocationTooltips": "Recover the files read the data directory"
},
"user": {
"name": "Name",
diff --git a/frontend/appflowy_flutter/integration_test/util/base.dart b/frontend/appflowy_flutter/integration_test/util/base.dart
index 3525fc5871..acc4b64e7a 100644
--- a/frontend/appflowy_flutter/integration_test/util/base.dart
+++ b/frontend/appflowy_flutter/integration_test/util/base.dart
@@ -1,8 +1,8 @@
import 'dart:io';
+import 'package:appflowy/core/config/kv_keys.dart';
import 'package:appflowy/main.dart' as app;
import 'package:appflowy/startup/tasks/prelude.dart';
-import 'package:appflowy/workspace/application/settings/settings_location_cubit.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -21,7 +21,7 @@ class TestFolder {
static Future<void> setTestLocation(String? name) async {
final location = await testLocation(name);
SharedPreferences.setMockInitialValues({
- kSettingsLocationDefaultLocation: location.path,
+ KVKeys.pathLocation: location.path,
});
return;
}
@@ -36,7 +36,7 @@ class TestFolder {
/// Get current using location.
static Future<String> currentLocation() async {
final prefs = await SharedPreferences.getInstance();
- return prefs.getString(kSettingsLocationDefaultLocation)!;
+ return prefs.getString(KVKeys.pathLocation)!;
}
/// Get default location under development environment.
diff --git a/frontend/appflowy_flutter/integration_test/util/data.dart b/frontend/appflowy_flutter/integration_test/util/data.dart
index 0dd0961ee2..6725f2958b 100644
--- a/frontend/appflowy_flutter/integration_test/util/data.dart
+++ b/frontend/appflowy_flutter/integration_test/util/data.dart
@@ -1,6 +1,6 @@
import 'dart:io';
-import 'package:appflowy/workspace/application/settings/settings_location_cubit.dart';
+import 'package:appflowy/core/config/kv_keys.dart';
import 'package:archive/archive_io.dart';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as p;
@@ -51,8 +51,7 @@ class TestWorkspaceService {
Future<void> setUpAll() async {
SharedPreferences.setMockInitialValues(
{
- kSettingsLocationDefaultLocation:
- await workspace.root.then((value) => value.path),
+ KVKeys.pathLocation: await workspace.root.then((value) => value.path),
},
);
}
diff --git a/frontend/appflowy_flutter/lib/core/config/config.dart b/frontend/appflowy_flutter/lib/core/config/config.dart
index ad06ecb860..19c771c793 100644
--- a/frontend/appflowy_flutter/lib/core/config/config.dart
+++ b/frontend/appflowy_flutter/lib/core/config/config.dart
@@ -4,13 +4,15 @@ import 'package:appflowy_backend/protobuf/flowy-config/entities.pb.dart';
class Config {
static Future<void> setSupabaseConfig({
required String url,
+ required String anonKey,
required String key,
required String secret,
}) async {
await ConfigEventSetSupabaseConfig(
SupabaseConfigPB.create()
..supabaseUrl = url
- ..supabaseKey = key
+ ..key = key
+ ..anonKey = anonKey
..jwtSecret = secret,
).send();
}
diff --git a/frontend/appflowy_flutter/lib/core/config/kv.dart b/frontend/appflowy_flutter/lib/core/config/kv.dart
index 692c691b57..4c0901cb8c 100644
--- a/frontend/appflowy_flutter/lib/core/config/kv.dart
+++ b/frontend/appflowy_flutter/lib/core/config/kv.dart
@@ -2,11 +2,61 @@ import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-config/entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:dartz/dartz.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+abstract class KeyValueStorage {
+ Future<void> set(String key, String value);
+ Future<Either<FlowyError, String>> get(String key);
+ Future<void> remove(String key);
+ Future<void> clear();
+}
+
+class DartKeyValue implements KeyValueStorage {
+ SharedPreferences? _sharedPreferences;
+ SharedPreferences get sharedPreferences => _sharedPreferences!;
+
+ @override
+ Future<Either<FlowyError, String>> get(String key) async {
+ await _initSharedPreferencesIfNeeded();
+
+ final value = sharedPreferences.getString(key);
+ if (value != null) {
+ return Right(value);
+ }
+ return Left(FlowyError());
+ }
+
+ @override
+ Future<void> remove(String key) async {
+ await _initSharedPreferencesIfNeeded();
+
+ await sharedPreferences.remove(key);
+ }
+
+ @override
+ Future<void> set(String key, String value) async {
+ await _initSharedPreferencesIfNeeded();
+
+ await sharedPreferences.setString(key, value);
+ }
+
+ @override
+ Future<void> clear() async {
+ await _initSharedPreferencesIfNeeded();
+
+ await sharedPreferences.clear();
+ }
+
+ Future<void> _initSharedPreferencesIfNeeded() async {
+ _sharedPreferences ??= await SharedPreferences.getInstance();
+ }
+}
/// Key-value store
/// The data is stored in the local storage of the device.
-class KeyValue {
- static Future<void> set(String key, String value) async {
+class RustKeyValue implements KeyValueStorage {
+ @override
+ Future<void> set(String key, String value) async {
await ConfigEventSetKeyValue(
KeyValuePB.create()
..key = key
@@ -14,20 +64,23 @@ class KeyValue {
).send();
}
- static Future<Either<String, FlowyError>> get(String key) {
- return ConfigEventGetKeyValue(
- KeyPB.create()..key = key,
- ).send().then(
- (result) => result.fold(
- (pb) => left(pb.value),
- (error) => right(error),
- ),
- );
+ @override
+ Future<Either<FlowyError, String>> get(String key) async {
+ final payload = KeyPB.create()..key = key;
+ final response = await ConfigEventGetKeyValue(payload).send();
+ return response.swap().map((r) => r.value);
}
- static Future<void> remove(String key) async {
+ @override
+ Future<void> remove(String key) async {
await ConfigEventRemoveKeyValue(
KeyPB.create()..key = key,
).send();
}
+
+ @override
+ Future<void> clear() {
+ // TODO: implement clear
+ throw UnimplementedError();
+ }
}
diff --git a/frontend/appflowy_flutter/lib/core/config/kv_keys.dart b/frontend/appflowy_flutter/lib/core/config/kv_keys.dart
new file mode 100644
index 0000000000..192a0e6960
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/core/config/kv_keys.dart
@@ -0,0 +1,15 @@
+class KVKeys {
+ const KVKeys._();
+
+ static const String prefix = 'io.appflowy.appflowy_flutter';
+
+ /// The key for the path location of the local data for the whole app.
+ static const String pathLocation = '$prefix.path_location';
+
+ /// The key for the last time login type.
+ ///
+ /// The value is one of the following:
+ /// - local
+ /// - supabase
+ static const String loginType = '$prefix.login_type';
+}
diff --git a/frontend/appflowy_flutter/lib/env/env.dart b/frontend/appflowy_flutter/lib/env/env.dart
new file mode 100644
index 0000000000..311df8a3ce
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/env/env.dart
@@ -0,0 +1,38 @@
+// lib/env/env.dart
+import 'package:envied/envied.dart';
+
+part 'env.g.dart';
+
+@Envied(path: '.env')
+abstract class Env {
+ @EnviedField(
+ obfuscate: true,
+ varName: 'SUPABASE_URL',
+ defaultValue: '',
+ )
+ static final supabaseUrl = _Env.supabaseUrl;
+ @EnviedField(
+ obfuscate: true,
+ varName: 'SUPABASE_ANON_KEY',
+ defaultValue: '',
+ )
+ static final supabaseAnonKey = _Env.supabaseAnonKey;
+ @EnviedField(
+ obfuscate: true,
+ varName: 'SUPABASE_KEY',
+ defaultValue: '',
+ )
+ static final supabaseKey = _Env.supabaseKey;
+ @EnviedField(
+ obfuscate: true,
+ varName: 'SUPABASE_JWT_SECRET',
+ defaultValue: '',
+ )
+ static final supabaseJwtSecret = _Env.supabaseJwtSecret;
+}
+
+bool get isSupabaseEnable =>
+ Env.supabaseUrl.isNotEmpty &&
+ Env.supabaseAnonKey.isNotEmpty &&
+ Env.supabaseKey.isNotEmpty &&
+ Env.supabaseJwtSecret.isNotEmpty;
diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart b/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart
index 4fe2db1b07..2f7c0d78ce 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart
@@ -65,7 +65,6 @@ extension on InsertOperation {
final parentId =
node.parent?.id ?? editorState.getNodeAtPath(path.parent)?.id ?? '';
final prevId = previousNode?.id ??
- node.previous?.id ??
editorState.getNodeAtPath(path.previous)?.id ??
'';
assert(parentId.isNotEmpty && prevId.isNotEmpty);
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/actions/option_action.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/actions/option_action.dart
index eb6e861919..2da5eccc0d 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/actions/option_action.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/actions/option_action.dart
@@ -37,7 +37,7 @@ class ColorOptionAction extends PopoverActionCell {
@override
Widget? leftIcon(Color iconColor) {
return svgWidget(
- 'editor/delete', // todo: add color icon
+ 'editor/color_formatter',
color: iconColor,
);
}
@@ -100,10 +100,10 @@ class ColorOptionAction extends PopoverActionCell {
}
class OptionActionWrapper extends ActionCell {
- final OptionAction inner;
-
OptionActionWrapper(this.inner);
+ final OptionAction inner;
+
@override
Widget? leftIcon(Color iconColor) {
var name = '';
@@ -125,15 +125,13 @@ class OptionActionWrapper extends ActionCell {
name = 'editor/move_down';
break;
case OptionAction.color:
- name = 'editor/color';
- break;
+ throw UnimplementedError();
case OptionAction.divider:
throw UnimplementedError();
}
if (name.isEmpty) {
return null;
}
- name = 'editor/delete';
return svgWidget(
name,
color: iconColor,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/cover/cover_image_picker_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/cover/cover_image_picker_bloc.dart
index 3c375ad7ee..43993d3f47 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/cover/cover_image_picker_bloc.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/cover/cover_image_picker_bloc.dart
@@ -130,7 +130,7 @@ class CoverImagePickerBloc
}
Future<String> _coverPath() async {
- final directory = await getIt<SettingsLocationCubit>().fetchLocation();
+ final directory = await getIt<LocalFileStorage>().getPath();
return Directory(p.join(directory, 'covers'))
.create(recursive: true)
.then((value) => value.path);
diff --git a/frontend/appflowy_flutter/lib/startup/deps_resolver.dart b/frontend/appflowy_flutter/lib/startup/deps_resolver.dart
index d060aabab1..5186646f53 100644
--- a/frontend/appflowy_flutter/lib/startup/deps_resolver.dart
+++ b/frontend/appflowy_flutter/lib/startup/deps_resolver.dart
@@ -1,3 +1,4 @@
+import 'package:appflowy/core/config/kv.dart';
import 'package:appflowy/core/network_monitor.dart';
import 'package:appflowy/plugins/database_view/application/field/field_action_sheet_bloc.dart';
import 'package:appflowy/plugins/database_view/application/field/field_controller.dart';
@@ -5,6 +6,8 @@ import 'package:appflowy/plugins/database_view/application/field/field_service.d
import 'package:appflowy/plugins/database_view/application/setting/property_bloc.dart';
import 'package:appflowy/plugins/database_view/grid/application/grid_header_bloc.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/service/openai_client.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
+import 'package:appflowy/user/application/auth/supabase_auth_service.dart';
import 'package:appflowy/user/application/user_listener.dart';
import 'package:appflowy/user/application/user_service.dart';
import 'package:appflowy/util/file_picker/file_picker_impl.dart';
@@ -45,7 +48,10 @@ class DependencyResolver {
}
void _resolveCommonService(GetIt getIt) async {
+ // getIt.registerFactory<KeyValueStorage>(() => RustKeyValue());
+ getIt.registerFactory<KeyValueStorage>(() => DartKeyValue());
getIt.registerFactory<FilePickerService>(() => FilePicker());
+ getIt.registerFactory<LocalFileStorage>(() => LocalFileStorage());
getIt.registerFactoryAsync<OpenAIRepository>(
() async {
@@ -66,11 +72,17 @@ void _resolveCommonService(GetIt getIt) async {
}
void _resolveUserDeps(GetIt getIt) {
- getIt.registerFactory<AuthService>(() => AuthService());
+ // getIt.registerFactory<AuthService>(() => AppFlowyAuthService());
+ getIt.registerFactory<AuthService>(() => SupabaseAuthService());
+
getIt.registerFactory<AuthRouter>(() => AuthRouter());
- getIt.registerFactory<SignInBloc>(() => SignInBloc(getIt<AuthService>()));
- getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthService>()));
+ getIt.registerFactory<SignInBloc>(
+ () => SignInBloc(getIt<AuthService>()),
+ );
+ getIt.registerFactory<SignUpBloc>(
+ () => SignUpBloc(getIt<AuthService>()),
+ );
getIt.registerFactory<SplashRoute>(() => SplashRoute());
getIt.registerFactory<EditPanelBloc>(() => EditPanelBloc());
@@ -131,11 +143,6 @@ void _resolveFolderDeps(GetIt getIt) {
(user, _) => SettingsDialogBloc(user),
);
- // Location
- getIt.registerFactory<SettingsLocationCubit>(
- () => SettingsLocationCubit(),
- );
-
//User
getIt.registerFactoryParam<SettingsUserViewBloc, UserProfilePB, void>(
(user, _) => SettingsUserViewBloc(user),
diff --git a/frontend/appflowy_flutter/lib/startup/startup.dart b/frontend/appflowy_flutter/lib/startup/startup.dart
index 0fa50120e8..aa7cd9d4a5 100644
--- a/frontend/appflowy_flutter/lib/startup/startup.dart
+++ b/frontend/appflowy_flutter/lib/startup/startup.dart
@@ -1,33 +1,17 @@
import 'dart:io';
+import 'package:appflowy/env/env.dart';
+import 'package:appflowy/workspace/application/settings/settings_location_cubit.dart';
import 'package:appflowy_backend/appflowy_backend.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
-import '../workspace/application/settings/settings_location_cubit.dart';
import 'deps_resolver.dart';
import 'launch_configuration.dart';
import 'plugin/plugin.dart';
import 'tasks/prelude.dart';
-// [[diagram: flowy startup flow]]
-// ┌──────────┐
-// │ FlowyApp │
-// └──────────┘
-// │ impl
-// ▼
-// ┌────────┐ 1.run ┌──────────┐
-// │ System │───┬───▶│EntryPoint│
-// └────────┘ │ └──────────┘ ┌─────────────────┐
-// │ ┌──▶ │ RustSDKInitTask │
-// │ ┌───────────┐ │ └─────────────────┘
-// └──▶ │AppLauncher│───┤
-// 2.launch └───────────┘ │ ┌─────────────┐ ┌──────────────────┐ ┌───────────────┐
-// └───▶│AppWidgetTask│────────▶│ApplicationWidget │─────▶│ SplashScreen │
-// └─────────────┘ └──────────────────┘ └───────────────┘
-//
-// 3.build MaterialApp
final getIt = GetIt.instance;
abstract class EntryPoint {
@@ -48,10 +32,12 @@ class FlowyRunner {
final env = integrationEnv();
initGetIt(getIt, env, f, config);
- final directory = getIt<SettingsLocationCubit>()
- .fetchLocation()
+ final directory = await getIt<LocalFileStorage>()
+ .getPath()
.then((value) => Directory(value));
+ // final directory = await appFlowyDocumentDirectory();
+
// add task
final launcher = getIt<AppLauncher>();
launcher.addTasks(
@@ -71,6 +57,12 @@ class FlowyRunner {
// ignore in test mode
if (!env.isTest()) ...[
const HotKeyTask(),
+ InitSupabaseTask(
+ url: Env.supabaseUrl,
+ anonKey: Env.supabaseAnonKey,
+ key: Env.supabaseKey,
+ jwtSecret: Env.supabaseJwtSecret,
+ ),
const InitAppWidgetTask(),
const InitPlatformServiceTask()
],
diff --git a/frontend/appflowy_flutter/lib/startup/tasks/prelude.dart b/frontend/appflowy_flutter/lib/startup/tasks/prelude.dart
index 208712b81a..d7d7993af7 100644
--- a/frontend/appflowy_flutter/lib/startup/tasks/prelude.dart
+++ b/frontend/appflowy_flutter/lib/startup/tasks/prelude.dart
@@ -6,3 +6,4 @@ export 'hot_key.dart';
export 'platform_error_catcher.dart';
export 'windows.dart';
export 'localization.dart';
+export 'supabase_task.dart';
diff --git a/frontend/appflowy_flutter/lib/startup/tasks/rust_sdk.dart b/frontend/appflowy_flutter/lib/startup/tasks/rust_sdk.dart
index 135012c298..7e389c491c 100644
--- a/frontend/appflowy_flutter/lib/startup/tasks/rust_sdk.dart
+++ b/frontend/appflowy_flutter/lib/startup/tasks/rust_sdk.dart
@@ -12,30 +12,23 @@ class InitRustSDKTask extends LaunchTask {
});
// Customize the RustSDK initialization path
- final Future<Directory>? directory;
+ final Directory? directory;
@override
LaunchTaskType get type => LaunchTaskType.dataProcessing;
@override
Future<void> initialize(LaunchContext context) async {
- // use the custom directory
- if (directory != null) {
- return directory!.then((directory) async {
- await context.getIt<FlowySDK>().init(directory);
- });
- } else {
- return appFlowyDocumentDirectory().then((directory) async {
- await context.getIt<FlowySDK>().init(directory);
- });
- }
+ final dir = directory ?? await appFlowyDocumentDirectory();
+ await context.getIt<FlowySDK>().init(dir);
}
}
Future<Directory> appFlowyDocumentDirectory() async {
switch (integrationEnv()) {
case IntegrationMode.develop:
- Directory documentsDir = await getApplicationDocumentsDirectory();
+ Directory documentsDir = await getApplicationDocumentsDirectory()
+ ..create();
return Directory(path.join(documentsDir.path, 'data_dev')).create();
case IntegrationMode.release:
Directory documentsDir = await getApplicationDocumentsDirectory();
diff --git a/frontend/appflowy_flutter/lib/startup/tasks/supabase_task.dart b/frontend/appflowy_flutter/lib/startup/tasks/supabase_task.dart
new file mode 100644
index 0000000000..e7369cd331
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/startup/tasks/supabase_task.dart
@@ -0,0 +1,46 @@
+import 'package:appflowy/core/config/config.dart';
+import 'package:appflowy_backend/log.dart';
+import 'package:supabase_flutter/supabase_flutter.dart';
+
+import '../startup.dart';
+
+bool isSupabaseEnable = false;
+bool isSupabaseInitialized = false;
+
+class InitSupabaseTask extends LaunchTask {
+ const InitSupabaseTask({
+ required this.url,
+ required this.anonKey,
+ required this.key,
+ required this.jwtSecret,
+ });
+
+ final String url;
+ final String anonKey;
+ final String key;
+ final String jwtSecret;
+
+ @override
+ Future<void> initialize(LaunchContext context) async {
+ if (url.isEmpty || anonKey.isEmpty || jwtSecret.isEmpty || key.isEmpty) {
+ isSupabaseEnable = false;
+ Log.info('Supabase config is empty, skip init supabase.');
+ return;
+ }
+ if (isSupabaseInitialized) {
+ return;
+ }
+ await Supabase.initialize(
+ url: url,
+ anonKey: anonKey,
+ );
+ await Config.setSupabaseConfig(
+ url: url,
+ key: key,
+ secret: jwtSecret,
+ anonKey: anonKey,
+ );
+ isSupabaseEnable = true;
+ isSupabaseInitialized = true;
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/startup/tasks/windows.dart b/frontend/appflowy_flutter/lib/startup/tasks/windows.dart
index 4fabfd962b..fcb80ecfe0 100644
--- a/frontend/appflowy_flutter/lib/startup/tasks/windows.dart
+++ b/frontend/appflowy_flutter/lib/startup/tasks/windows.dart
@@ -7,7 +7,7 @@ import 'package:window_manager/window_manager.dart';
class InitAppWindowTask extends LaunchTask {
const InitAppWindowTask({
- this.minimumSize = const Size(600, 400),
+ this.minimumSize = const Size(800, 600),
this.title = 'AppFlowy',
});
diff --git a/frontend/appflowy_flutter/lib/user/application/auth/appflowy_auth_service.dart b/frontend/appflowy_flutter/lib/user/application/auth/appflowy_auth_service.dart
new file mode 100644
index 0000000000..302d31a75c
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/user/application/auth/appflowy_auth_service.dart
@@ -0,0 +1,87 @@
+import 'package:appflowy/user/application/auth/auth_service.dart';
+import 'package:appflowy/user/application/user_service.dart';
+import 'package:appflowy_backend/protobuf/flowy-user/auth.pb.dart';
+import 'package:dartz/dartz.dart';
+import 'package:easy_localization/easy_localization.dart';
+import 'package:flowy_infra/uuid.dart';
+import 'package:appflowy_backend/dispatch/dispatch.dart';
+import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
+import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
+ show SignInPayloadPB, SignUpPayloadPB, UserProfilePB;
+
+import '../../../generated/locale_keys.g.dart';
+
+class AppFlowyAuthService implements AuthService {
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signIn({
+ required String email,
+ required String password,
+ AuthTypePB authType = AuthTypePB.Local,
+ Map<String, String> map = const {},
+ }) async {
+ final request = SignInPayloadPB.create()
+ ..email = email
+ ..password = password
+ ..authType = authType;
+ final response = UserEventSignIn(request).send();
+ return response.then((value) => value.swap());
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signUp({
+ required String name,
+ required String email,
+ required String password,
+ AuthTypePB authType = AuthTypePB.Local,
+ Map<String, String> map = const {},
+ }) async {
+ final request = SignUpPayloadPB.create()
+ ..name = name
+ ..email = email
+ ..password = password
+ ..authType = authType;
+ final response = await UserEventSignUp(request).send().then(
+ (value) => value.swap(),
+ );
+ return response;
+ }
+
+ @override
+ Future<void> signOut({
+ AuthTypePB authType = AuthTypePB.Local,
+ Map<String, String> map = const {},
+ }) async {
+ final payload = SignOutPB()..authType = authType;
+ await UserEventSignOut(payload).send();
+ return;
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
+ AuthTypePB authType = AuthTypePB.Local,
+ Map<String, String> map = const {},
+ }) {
+ const password = "AppFlowy123@";
+ final uid = uuid();
+ final userEmail = "[email protected]";
+ return signUp(
+ name: LocaleKeys.defaultUsername.tr(),
+ password: password,
+ email: userEmail,
+ );
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
+ required String platform,
+ AuthTypePB authType = AuthTypePB.Local,
+ Map<String, String> map = const {},
+ }) {
+ throw UnimplementedError();
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> getUser() async {
+ return UserBackendService.getCurrentUserProfile();
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/user/application/auth/auth_error.dart b/frontend/appflowy_flutter/lib/user/application/auth/auth_error.dart
new file mode 100644
index 0000000000..f9629e1547
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/user/application/auth/auth_error.dart
@@ -0,0 +1,19 @@
+import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
+
+class AuthError {
+ static final supabaseSignInError = FlowyError()
+ ..msg = 'supabase sign in error'
+ ..code = -10001;
+
+ static final supabaseSignUpError = FlowyError()
+ ..msg = 'supabase sign up error'
+ ..code = -10002;
+
+ static final supabaseSignInWithOauthError = FlowyError()
+ ..msg = 'supabase sign in with oauth error'
+ ..code = -10003;
+
+ static final supabaseGetUserError = FlowyError()
+ ..msg = 'supabase sign in with oauth error'
+ ..code = -10003;
+}
diff --git a/frontend/appflowy_flutter/lib/user/application/auth/auth_service.dart b/frontend/appflowy_flutter/lib/user/application/auth/auth_service.dart
new file mode 100644
index 0000000000..70c7fd73f9
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/user/application/auth/auth_service.dart
@@ -0,0 +1,51 @@
+import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
+import 'package:appflowy_backend/protobuf/flowy-user/auth.pb.dart';
+import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart';
+import 'package:dartz/dartz.dart';
+
+class AuthServiceMapKeys {
+ const AuthServiceMapKeys._();
+
+ // for supabase auth use only.
+ static const String uuid = 'uuid';
+}
+
+abstract class AuthService {
+ /// Returns [UserProfilePB] if the user is authenticated, otherwise returns [FlowyError].
+ Future<Either<FlowyError, UserProfilePB>> signIn({
+ required String email,
+ required String password,
+ AuthTypePB authType,
+ Map<String, String> map,
+ });
+
+ /// Returns [UserProfilePB] if the user is authenticated, otherwise returns [FlowyError].
+ Future<Either<FlowyError, UserProfilePB>> signUp({
+ required String name,
+ required String email,
+ required String password,
+ AuthTypePB authType,
+ Map<String, String> map,
+ });
+
+ ///
+ Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
+ required String platform,
+ AuthTypePB authType,
+ Map<String, String> map,
+ });
+
+ /// Returns a default [UserProfilePB]
+ Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
+ AuthTypePB authType,
+ Map<String, String> map,
+ });
+
+ ///
+ Future<void> signOut({
+ AuthTypePB authType,
+ });
+
+ /// Returns [UserProfilePB] if the user has sign in, otherwise returns null.
+ Future<Either<FlowyError, UserProfilePB>> getUser();
+}
diff --git a/frontend/appflowy_flutter/lib/user/application/auth/supabase_auth_service.dart b/frontend/appflowy_flutter/lib/user/application/auth/supabase_auth_service.dart
new file mode 100644
index 0000000000..6a28a44981
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/user/application/auth/supabase_auth_service.dart
@@ -0,0 +1,221 @@
+import 'dart:async';
+
+import 'package:appflowy/core/config/kv.dart';
+import 'package:appflowy/core/config/kv_keys.dart';
+import 'package:appflowy/startup/startup.dart';
+import 'package:appflowy/startup/tasks/prelude.dart';
+import 'package:appflowy/user/application/auth/appflowy_auth_service.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
+import 'package:appflowy_backend/dispatch/dispatch.dart';
+import 'package:appflowy_backend/log.dart';
+import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
+import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
+import 'package:dartz/dartz.dart';
+import 'package:supabase_flutter/supabase_flutter.dart';
+import 'auth_error.dart';
+
+class SupabaseAuthService implements AuthService {
+ SupabaseAuthService();
+
+ SupabaseClient get _client => Supabase.instance.client;
+ GoTrueClient get _auth => _client.auth;
+
+ final AppFlowyAuthService _appFlowyAuthService = AppFlowyAuthService();
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signUp({
+ required String name,
+ required String email,
+ required String password,
+ AuthTypePB authType = AuthTypePB.Supabase,
+ Map<String, String> map = const {},
+ }) async {
+ if (!isSupabaseEnable) {
+ return _appFlowyAuthService.signUp(
+ name: name,
+ email: email,
+ password: password,
+ );
+ }
+
+ // fetch the uuid from supabase.
+ final response = await _auth.signUp(
+ email: email,
+ password: password,
+ );
+ final uuid = response.user?.id;
+ if (uuid == null) {
+ return left(AuthError.supabaseSignUpError);
+ }
+ // assign the uuid to our backend service.
+ // and will transfer this logic to backend later.
+ return _appFlowyAuthService.signUp(
+ name: name,
+ email: email,
+ password: password,
+ authType: authType,
+ map: {
+ AuthServiceMapKeys.uuid: uuid,
+ },
+ );
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signIn({
+ required String email,
+ required String password,
+ AuthTypePB authType = AuthTypePB.Supabase,
+ Map<String, String> map = const {},
+ }) async {
+ if (!isSupabaseEnable) {
+ return _appFlowyAuthService.signIn(
+ email: email,
+ password: password,
+ );
+ }
+
+ try {
+ final response = await _auth.signInWithPassword(
+ email: email,
+ password: password,
+ );
+ final uuid = response.user?.id;
+ if (uuid == null) {
+ return Left(AuthError.supabaseSignInError);
+ }
+ return _appFlowyAuthService.signIn(
+ email: email,
+ password: password,
+ authType: authType,
+ map: {
+ AuthServiceMapKeys.uuid: uuid,
+ },
+ );
+ } on AuthException catch (e) {
+ Log.error(e);
+ return Left(AuthError.supabaseSignInError);
+ }
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
+ required String platform,
+ AuthTypePB authType = AuthTypePB.Supabase,
+ Map<String, String> map = const {},
+ }) async {
+ if (!isSupabaseEnable) {
+ return _appFlowyAuthService.signUpWithOAuth(
+ platform: platform,
+ );
+ }
+ final provider = platform.toProvider();
+ final completer = Completer<Either<FlowyError, UserProfilePB>>();
+ late final StreamSubscription<AuthState> subscription;
+ subscription = _auth.onAuthStateChange.listen((event) async {
+ if (event.event != AuthChangeEvent.signedIn) {
+ completer.complete(left(AuthError.supabaseSignInWithOauthError));
+ } else {
+ final user = await getSupabaseUser();
+ final Either<FlowyError, UserProfilePB> response = await user.fold(
+ (l) => left(l),
+ (r) async => await setupAuth(map: {AuthServiceMapKeys.uuid: r.id}),
+ );
+ completer.complete(response);
+ }
+ subscription.cancel();
+ });
+ final Map<String, String> query = {};
+ if (provider == Provider.google) {
+ query['access_type'] = 'offline';
+ query['prompt'] = 'consent';
+ }
+ final response = await _auth.signInWithOAuth(
+ provider,
+ queryParams: query,
+ redirectTo:
+ 'io.appflowy.appflowy-flutter://login-callback', // can't use underscore here.
+ );
+ if (!response) {
+ completer.complete(left(AuthError.supabaseSignInWithOauthError));
+ }
+ return completer.future;
+ }
+
+ @override
+ Future<void> signOut({
+ AuthTypePB authType = AuthTypePB.Supabase,
+ }) async {
+ if (!isSupabaseEnable) {
+ return _appFlowyAuthService.signOut();
+ }
+ await _auth.signOut();
+ await _appFlowyAuthService.signOut(
+ authType: authType,
+ );
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
+ AuthTypePB authType = AuthTypePB.Supabase,
+ Map<String, String> map = const {},
+ }) async {
+ // supabase don't support guest login.
+ // so, just forward to our backend.
+ return _appFlowyAuthService.signUpAsGuest();
+ }
+
+ @override
+ Future<Either<FlowyError, UserProfilePB>> getUser() async {
+ final loginType = await getIt<KeyValueStorage>()
+ .get(KVKeys.loginType)
+ .then((value) => value.toOption().toNullable());
+ if (!isSupabaseEnable || (loginType != null && loginType != 'supabase')) {
+ return _appFlowyAuthService.getUser();
+ }
+ final user = await getSupabaseUser();
+ return user.map((r) => r.toUserProfile());
+ }
+
+ Future<Either<FlowyError, User>> getSupabaseUser() async {
+ final user = _auth.currentUser;
+ if (user == null) {
+ return left(AuthError.supabaseGetUserError);
+ }
+ return Right(user);
+ }
+
+ Future<Either<FlowyError, UserProfilePB>> setupAuth({
+ required Map<String, String> map,
+ }) async {
+ final payload = ThirdPartyAuthPB(
+ authType: AuthTypePB.Supabase,
+ map: map,
+ );
+ return UserEventThirdPartyAuth(payload)
+ .send()
+ .then((value) => value.swap());
+ }
+}
+
+extension on User {
+ UserProfilePB toUserProfile() {
+ return UserProfilePB()
+ ..email = email ?? ''
+ ..token = this.id;
+ }
+}
+
+extension on String {
+ Provider toProvider() {
+ switch (this) {
+ case 'github':
+ return Provider.github;
+ case 'google':
+ return Provider.google;
+ case 'discord':
+ return Provider.discord;
+ default:
+ throw UnimplementedError();
+ }
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/user/application/auth_service.dart b/frontend/appflowy_flutter/lib/user/application/auth_service.dart
deleted file mode 100644
index c6e684d611..0000000000
--- a/frontend/appflowy_flutter/lib/user/application/auth_service.dart
+++ /dev/null
@@ -1,63 +0,0 @@
-import 'package:dartz/dartz.dart';
-import 'package:easy_localization/easy_localization.dart';
-import 'package:flowy_infra/uuid.dart';
-import 'package:appflowy_backend/dispatch/dispatch.dart';
-import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
-import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
- show SignInPayloadPB, SignUpPayloadPB, UserProfilePB;
-
-import '../../generated/locale_keys.g.dart';
-
-class AuthService {
- Future<Either<UserProfilePB, FlowyError>> signIn({
- required String? email,
- required String? password,
- }) {
- //
- final request = SignInPayloadPB.create()
- ..email = email ?? ''
- ..password = password ?? '';
-
- return UserEventSignIn(request).send();
- }
-
- Future<Either<UserProfilePB, FlowyError>> signUp({
- required String? name,
- required String? password,
- required String? email,
- }) {
- final request = SignUpPayloadPB.create()
- ..email = email ?? ''
- ..name = name ?? ''
- ..password = password ?? '';
-
- return UserEventSignUp(request).send();
-
- // return UserEventSignUp(request).send().then((result) {
- // return result.fold((userProfile) async {
- // return await FolderEventCreateDefaultWorkspace().send().then((result) {
- // return result.fold((workspaceIdentifier) {
- // return left(Tuple2(userProfile, workspaceIdentifier.workspaceId));
- // }, (error) {
- // throw UnimplementedError;
- // });
- // });
- // }, (error) => right(error));
- // });
- }
-
- Future<Either<Unit, FlowyError>> signOut() {
- return UserEventSignOut().send();
- }
-
- Future<Either<UserProfilePB, FlowyError>> autoSignUp() {
- const password = "AppFlowy123@";
- final uid = uuid();
- final userEmail = "[email protected]";
- return signUp(
- name: LocaleKeys.defaultUsername.tr(),
- password: password,
- email: userEmail,
- );
- }
-}
diff --git a/frontend/appflowy_flutter/lib/user/application/prelude.dart b/frontend/appflowy_flutter/lib/user/application/prelude.dart
index 74b644808e..ddacb654d3 100644
--- a/frontend/appflowy_flutter/lib/user/application/prelude.dart
+++ b/frontend/appflowy_flutter/lib/user/application/prelude.dart
@@ -1,4 +1,4 @@
-export './auth_service.dart';
+export 'auth/appflowy_auth_service.dart';
export './sign_in_bloc.dart';
export './sign_up_bloc.dart';
export './splash_bloc.dart';
diff --git a/frontend/appflowy_flutter/lib/user/application/sign_in_bloc.dart b/frontend/appflowy_flutter/lib/user/application/sign_in_bloc.dart
index 69b0fe7a62..a95530a5c0 100644
--- a/frontend/appflowy_flutter/lib/user/application/sign_in_bloc.dart
+++ b/frontend/appflowy_flutter/lib/user/application/sign_in_bloc.dart
@@ -1,4 +1,4 @@
-import 'package:appflowy/user/application/auth_service.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
@@ -20,6 +20,16 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
emit,
);
},
+ signedInWithOAuth: (value) async =>
+ await _performActionOnSignInWithOAuth(
+ state,
+ emit,
+ value.platform,
+ ),
+ signedInAsGuest: (value) async => await _performActionOnSignInAsGuest(
+ state,
+ emit,
+ ),
emailChanged: (EmailChanged value) async {
emit(
state.copyWith(
@@ -45,6 +55,26 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
Future<void> _performActionOnSignIn(
SignInState state,
Emitter<SignInState> emit,
+ ) async {
+ final result = await authService.signIn(
+ email: state.email ?? '',
+ password: state.password ?? '',
+ );
+ emit(
+ result.fold(
+ (error) => stateFromCode(error),
+ (userProfile) => state.copyWith(
+ isSubmitting: false,
+ successOrFail: some(left(userProfile)),
+ ),
+ ),
+ );
+ }
+
+ Future<void> _performActionOnSignInWithOAuth(
+ SignInState state,
+ Emitter<SignInState> emit,
+ String platform,
) async {
emit(
state.copyWith(
@@ -55,17 +85,41 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
),
);
- final result = await authService.signIn(
- email: state.email,
- password: state.password,
+ final result = await authService.signUpWithOAuth(
+ platform: platform,
);
emit(
result.fold(
+ (error) => stateFromCode(error),
(userProfile) => state.copyWith(
isSubmitting: false,
successOrFail: some(left(userProfile)),
),
+ ),
+ );
+ }
+
+ Future<void> _performActionOnSignInAsGuest(
+ SignInState state,
+ Emitter<SignInState> emit,
+ ) async {
+ emit(
+ state.copyWith(
+ isSubmitting: true,
+ emailError: none(),
+ passwordError: none(),
+ successOrFail: none(),
+ ),
+ );
+
+ final result = await authService.signUpAsGuest();
+ emit(
+ result.fold(
(error) => stateFromCode(error),
+ (userProfile) => state.copyWith(
+ isSubmitting: false,
+ successOrFail: some(left(userProfile)),
+ ),
),
);
}
@@ -97,6 +151,9 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
class SignInEvent with _$SignInEvent {
const factory SignInEvent.signedInWithUserEmailAndPassword() =
SignedInWithUserEmailAndPassword;
+ const factory SignInEvent.signedInWithOAuth(String platform) =
+ SignedInWithOAuth;
+ const factory SignInEvent.signedInAsGuest() = SignedInAsGuest;
const factory SignInEvent.emailChanged(String email) = EmailChanged;
const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
}
diff --git a/frontend/appflowy_flutter/lib/user/application/sign_up_bloc.dart b/frontend/appflowy_flutter/lib/user/application/sign_up_bloc.dart
index bb3e99f51c..ca884cdecc 100644
--- a/frontend/appflowy_flutter/lib/user/application/sign_up_bloc.dart
+++ b/frontend/appflowy_flutter/lib/user/application/sign_up_bloc.dart
@@ -1,4 +1,4 @@
-import 'package:appflowy/user/application/auth_service.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:dartz/dartz.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
@@ -100,12 +100,13 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
);
final result = await authService.signUp(
- name: state.email,
- password: state.password,
- email: state.email,
+ name: state.email ?? '',
+ password: state.password ?? '',
+ email: state.email ?? '',
);
emit(
result.fold(
+ (error) => stateFromCode(error),
(profile) => state.copyWith(
isSubmitting: false,
successOrFail: some(left(profile)),
@@ -113,7 +114,6 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
passwordError: none(),
repeatPasswordError: none(),
),
- (error) => stateFromCode(error),
),
);
}
diff --git a/frontend/appflowy_flutter/lib/user/application/splash_bloc.dart b/frontend/appflowy_flutter/lib/user/application/splash_bloc.dart
index 56448ad538..f603e1cf53 100644
--- a/frontend/appflowy_flutter/lib/user/application/splash_bloc.dart
+++ b/frontend/appflowy_flutter/lib/user/application/splash_bloc.dart
@@ -1,5 +1,6 @@
+import 'package:appflowy/startup/startup.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:appflowy/user/domain/auth_state.dart';
-import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -10,16 +11,11 @@ class SplashBloc extends Bloc<SplashEvent, SplashState> {
on<SplashEvent>((event, emit) async {
await event.map(
getUser: (val) async {
- final result = await UserEventCheckUser().send();
- final authState = result.fold(
- (userProfile) {
- return AuthState.authenticated(userProfile);
- },
- (error) {
- return AuthState.unauthenticated(error);
- },
+ final response = await getIt<AuthService>().getUser();
+ final authState = response.fold(
+ (error) => AuthState.unauthenticated(error),
+ (user) => AuthState.authenticated(user),
);
-
emit(state.copyWith(auth: authState));
},
);
diff --git a/frontend/appflowy_flutter/lib/user/application/user_listener.dart b/frontend/appflowy_flutter/lib/user/application/user_listener.dart
index 91185b8f45..a06e398bc5 100644
--- a/frontend/appflowy_flutter/lib/user/application/user_listener.dart
+++ b/frontend/appflowy_flutter/lib/user/application/user_listener.dart
@@ -40,7 +40,7 @@ class UserListener {
}
_userParser = UserNotificationParser(
- id: _userProfile.token,
+ id: _userProfile.id.toString(),
callback: _userNotificationCallback,
);
_subscription = RustStreamReceiver.listen((observable) {
diff --git a/frontend/appflowy_flutter/lib/user/application/user_service.dart b/frontend/appflowy_flutter/lib/user/application/user_service.dart
index eacb938de1..60ec1e26f8 100644
--- a/frontend/appflowy_flutter/lib/user/application/user_service.dart
+++ b/frontend/appflowy_flutter/lib/user/application/user_service.dart
@@ -1,10 +1,10 @@
import 'dart:async';
+import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
-import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart';
import 'package:fixnum/fixnum.dart';
class UserBackendService {
@@ -58,8 +58,9 @@ class UserBackendService {
throw UnimplementedError();
}
- Future<Either<Unit, FlowyError>> signOut() {
- return UserEventSignOut().send();
+ Future<Either<Unit, FlowyError>> signOut(AuthTypePB authType) {
+ final payload = SignOutPB()..authType = authType;
+ return UserEventSignOut(payload).send();
}
Future<Either<Unit, FlowyError>> initUser() async {
diff --git a/frontend/appflowy_flutter/lib/user/presentation/folder/folder_widget.dart b/frontend/appflowy_flutter/lib/user/presentation/folder/folder_widget.dart
index 4033d59e13..20eb65fbcd 100644
--- a/frontend/appflowy_flutter/lib/user/presentation/folder/folder_widget.dart
+++ b/frontend/appflowy_flutter/lib/user/presentation/folder/folder_widget.dart
@@ -61,9 +61,9 @@ class _FolderWidgetState extends State<FolderWidget> {
}
Future<void> _openFolder() async {
- final directory = await getIt<FilePickerService>().getDirectoryPath();
- if (directory != null) {
- await getIt<SettingsLocationCubit>().setLocation(directory);
+ final path = await getIt<FilePickerService>().getDirectoryPath();
+ if (path != null) {
+ await getIt<LocalFileStorage>().setPath(path);
await widget.createFolderCallback();
}
}
@@ -188,7 +188,7 @@ class CreateFolderWidgetState extends State<CreateFolderWidget> {
LocaleKeys.settings_files_locationCannotBeEmpty.tr(),
);
} else {
- await getIt<SettingsLocationCubit>().setLocation(_path);
+ await getIt<LocalFileStorage>().setPath(_path);
await widget.onPressedCreate();
}
},
diff --git a/frontend/appflowy_flutter/lib/user/presentation/router.dart b/frontend/appflowy_flutter/lib/user/presentation/router.dart
index b03f3ef042..d0845d41d7 100644
--- a/frontend/appflowy_flutter/lib/user/presentation/router.dart
+++ b/frontend/appflowy_flutter/lib/user/presentation/router.dart
@@ -1,5 +1,5 @@
import 'package:appflowy/startup/startup.dart';
-import 'package:appflowy/user/application/auth_service.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:appflowy/user/presentation/sign_in_screen.dart';
import 'package:appflowy/user/presentation/sign_up_screen.dart';
import 'package:appflowy/user/presentation/skip_log_in_screen.dart';
@@ -28,7 +28,7 @@ class AuthRouter {
);
}
- void pushHomeScreen(
+ void pushHomeScreenWithWorkSpace(
BuildContext context,
UserProfilePB profile,
WorkspaceSettingPB workspaceSetting,
@@ -45,6 +45,21 @@ class AuthRouter {
),
);
}
+
+ Future<void> pushHomeScreen(
+ BuildContext context,
+ UserProfilePB userProfile,
+ ) async {
+ final result = await FolderEventReadCurrentWorkspace().send();
+ result.fold(
+ (workspaceSettingPB) => pushHomeScreenWithWorkSpace(
+ context,
+ userProfile,
+ workspaceSettingPB,
+ ),
+ (r) => pushWelcomeScreen(context, userProfile),
+ );
+ }
}
class SplashRoute {
diff --git a/frontend/appflowy_flutter/lib/user/presentation/sign_in_screen.dart b/frontend/appflowy_flutter/lib/user/presentation/sign_in_screen.dart
index 44b339a31e..fef663f104 100644
--- a/frontend/appflowy_flutter/lib/user/presentation/sign_in_screen.dart
+++ b/frontend/appflowy_flutter/lib/user/presentation/sign_in_screen.dart
@@ -1,13 +1,15 @@
+import 'package:appflowy/core/config/kv.dart';
+import 'package:appflowy/core/config/kv_keys.dart';
+import 'package:appflowy/core/frameless_window.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/user/application/sign_in_bloc.dart';
import 'package:appflowy/user/presentation/router.dart';
import 'package:appflowy/user/presentation/widgets/background.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/size.dart';
-import 'package:flowy_infra_ui/style_widget/text.dart';
+import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flowy_infra_ui/widget/rounded_button.dart';
import 'package:flowy_infra_ui/widget/rounded_input_field.dart';
-import 'package:flowy_infra_ui/widget/spacing.dart';
import 'package:flowy_infra_ui/style_widget/snap_bar.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
@@ -19,21 +21,29 @@ import 'package:flowy_infra/image.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
class SignInScreen extends StatelessWidget {
+ const SignInScreen({
+ super.key,
+ required this.router,
+ });
+
final AuthRouter router;
- const SignInScreen({Key? key, required this.router}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<SignInBloc>(),
- child: BlocListener<SignInBloc, SignInState>(
+ child: BlocConsumer<SignInBloc, SignInState>(
listener: (context, state) {
state.successOrFail.fold(
() => null,
(result) => _handleSuccessOrFail(result, context),
);
},
- child: Scaffold(
+ builder: (_, __) => Scaffold(
+ appBar: const PreferredSize(
+ preferredSize: Size(double.infinity, 60),
+ child: MoveWindowDetector(),
+ ),
body: SignInForm(router: router),
),
),
@@ -45,18 +55,19 @@ class SignInScreen extends StatelessWidget {
BuildContext context,
) {
result.fold(
- (user) => router.pushWelcomeScreen(context, user),
+ (user) => router.pushHomeScreen(context, user),
(error) => showSnapBar(context, error.msg),
);
}
}
class SignInForm extends StatelessWidget {
- final AuthRouter router;
const SignInForm({
- Key? key,
+ super.key,
required this.router,
- }) : super(key: key);
+ });
+
+ final AuthRouter router;
@override
Widget build(BuildContext context) {
@@ -64,22 +75,42 @@ class SignInForm extends StatelessWidget {
alignment: Alignment.center,
child: AuthFormContainer(
children: [
+ // Email.
FlowyLogoTitle(
title: LocaleKeys.signIn_loginTitle.tr(),
logoSize: const Size(60, 60),
),
const VSpace(30),
- const EmailTextField(),
- const PasswordTextField(),
- ForgetPasswordButton(router: router),
- const VSpace(30),
- const LoginButton(),
+ // Email and password. don't support yet.
+ /*
+ ...[
+ const EmailTextField(),
+ const VSpace(5),
+ const PasswordTextField(),
+ const VSpace(20),
+ const LoginButton(),
+ const VSpace(10),
+
+ const VSpace(10),
+ SignUpPrompt(router: router),
+ ],
+ */
+
+ const SignInAsGuestButton(),
+
+ // third-party sign in.
+ const VSpace(20),
+ const OrContinueWith(),
const VSpace(10),
- SignUpPrompt(router: router),
+ const ThirdPartySignInButtons(),
+ const VSpace(20),
+
+ // loading status
if (context.read<SignInBloc>().state.isSubmitting) ...[
const SizedBox(height: 8),
const LinearProgressIndicator(value: null),
- ]
+ const VSpace(20),
+ ],
],
),
);
@@ -113,6 +144,7 @@ class SignUpPrompt extends StatelessWidget {
style: TextStyle(color: Theme.of(context).colorScheme.primary),
),
),
+ ForgetPasswordButton(router: router),
],
);
}
@@ -136,6 +168,25 @@ class LoginButton extends StatelessWidget {
}
}
+class SignInAsGuestButton extends StatelessWidget {
+ const SignInAsGuestButton({
+ Key? key,
+ }) : super(key: key);
+
+ @override
+ Widget build(BuildContext context) {
+ return RoundedTextButton(
+ title: LocaleKeys.signIn_loginAsGuestButtonText.tr(),
+ height: 48,
+ borderRadius: Corners.s6Border,
+ onPressed: () {
+ getIt<KeyValueStorage>().set(KVKeys.loginType, 'local');
+ context.read<SignInBloc>().add(const SignInEvent.signedInAsGuest());
+ },
+ );
+ }
+}
+
class ForgetPasswordButton extends StatelessWidget {
const ForgetPasswordButton({
Key? key,
@@ -150,7 +201,9 @@ class ForgetPasswordButton extends StatelessWidget {
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.bodyMedium,
),
- onPressed: () => router.pushForgetPasswordScreen(context),
+ onPressed: () {
+ throw UnimplementedError();
+ },
child: Text(
LocaleKeys.signIn_forgotPassword.tr(),
style: TextStyle(color: Theme.of(context).colorScheme.primary),
@@ -214,3 +267,98 @@ class EmailTextField extends StatelessWidget {
);
}
}
+
+class OrContinueWith extends StatelessWidget {
+ const OrContinueWith({super.key});
+
+ @override
+ Widget build(BuildContext context) {
+ return Row(
+ mainAxisAlignment: MainAxisAlignment.spaceEvenly,
+ children: const [
+ Flexible(
+ child: Divider(
+ color: Colors.white,
+ height: 10,
+ ),
+ ),
+ FlowyText.regular(' Or continue with '),
+ Flexible(
+ child: Divider(
+ color: Colors.white,
+ height: 10,
+ ),
+ ),
+ ],
+ );
+ }
+}
+
+class ThirdPartySignInButton extends StatelessWidget {
+ const ThirdPartySignInButton({
+ Key? key,
+ required this.icon,
+ required this.onPressed,
+ }) : super(key: key);
+
+ final String icon;
+ final VoidCallback onPressed;
+
+ @override
+ Widget build(BuildContext context) {
+ return FlowyIconButton(
+ height: 48,
+ width: 48,
+ iconPadding: const EdgeInsets.all(8.0),
+ radius: Corners.s10Border,
+ onPressed: onPressed,
+ icon: svgWidget(
+ icon,
+ ),
+ );
+ }
+}
+
+class ThirdPartySignInButtons extends StatelessWidget {
+ const ThirdPartySignInButtons({
+ super.key,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ return Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ ThirdPartySignInButton(
+ icon: 'login/google-mark',
+ onPressed: () {
+ getIt<KeyValueStorage>().set(KVKeys.loginType, 'supabase');
+ context
+ .read<SignInBloc>()
+ .add(const SignInEvent.signedInWithOAuth('google'));
+ },
+ ),
+ const SizedBox(width: 20),
+ ThirdPartySignInButton(
+ icon: 'login/github-mark',
+ onPressed: () {
+ getIt<KeyValueStorage>().set(KVKeys.loginType, 'supabase');
+ context
+ .read<SignInBloc>()
+ .add(const SignInEvent.signedInWithOAuth('github'));
+ },
+ ),
+ const SizedBox(width: 20),
+ ThirdPartySignInButton(
+ icon: 'login/discord-mark',
+ onPressed: () {
+ getIt<KeyValueStorage>().set(KVKeys.loginType, 'supabase');
+ context
+ .read<SignInBloc>()
+ .add(const SignInEvent.signedInWithOAuth('discord'));
+ },
+ ),
+ ],
+ );
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/user/presentation/sign_up_screen.dart b/frontend/appflowy_flutter/lib/user/presentation/sign_up_screen.dart
index 7b8b796fd1..95f9c2fef8 100644
--- a/frontend/appflowy_flutter/lib/user/presentation/sign_up_screen.dart
+++ b/frontend/appflowy_flutter/lib/user/presentation/sign_up_screen.dart
@@ -18,8 +18,12 @@ import 'package:flowy_infra/image.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
class SignUpScreen extends StatelessWidget {
+ const SignUpScreen({
+ super.key,
+ required this.router,
+ });
+
final AuthRouter router;
- const SignUpScreen({Key? key, required this.router}) : super(key: key);
@override
Widget build(BuildContext context) {
@@ -65,7 +69,9 @@ class SignUpForm extends StatelessWidget {
),
const VSpace(30),
const EmailTextField(),
+ const VSpace(5),
const PasswordTextField(),
+ const VSpace(5),
const RepeatPasswordTextField(),
const VSpace(30),
const SignUpButton(),
diff --git a/frontend/appflowy_flutter/lib/user/presentation/skip_log_in_screen.dart b/frontend/appflowy_flutter/lib/user/presentation/skip_log_in_screen.dart
index fb9662bc8c..8f69763603 100644
--- a/frontend/appflowy_flutter/lib/user/presentation/skip_log_in_screen.dart
+++ b/frontend/appflowy_flutter/lib/user/presentation/skip_log_in_screen.dart
@@ -1,5 +1,6 @@
import 'package:appflowy/core/frameless_window.dart';
import 'package:appflowy/startup/entry_point.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:dartz/dartz.dart' as dartz;
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/size.dart';
@@ -15,7 +16,6 @@ import 'package:url_launcher/url_launcher.dart';
import '../../generated/locale_keys.g.dart';
import '../../startup/launch_configuration.dart';
import '../../startup/startup.dart';
-import '../application/auth_service.dart';
import 'folder/folder_widget.dart';
import 'router.dart';
import 'widgets/background.dart';
@@ -120,16 +120,16 @@ class _SkipLogInScreenState extends State<SkipLogInScreen> {
}
Future<void> _autoRegister(BuildContext context) async {
- final result = await widget.authService.autoSignUp();
+ final result = await widget.authService.signUpAsGuest();
result.fold(
+ (error) {
+ Log.error(error);
+ },
(user) {
FolderEventReadCurrentWorkspace().send().then((result) {
_openCurrentWorkspace(context, user, result);
});
},
- (error) {
- Log.error(error);
- },
);
}
@@ -140,7 +140,8 @@ class _SkipLogInScreenState extends State<SkipLogInScreen> {
) {
workspacesOrError.fold(
(workspaceSetting) {
- widget.router.pushHomeScreen(context, user, workspaceSetting);
+ widget.router
+ .pushHomeScreenWithWorkSpace(context, user, workspaceSetting);
},
(error) {
Log.error(error);
diff --git a/frontend/appflowy_flutter/lib/user/presentation/splash_screen.dart b/frontend/appflowy_flutter/lib/user/presentation/splash_screen.dart
index e2c60447ce..aefcada89d 100644
--- a/frontend/appflowy_flutter/lib/user/presentation/splash_screen.dart
+++ b/frontend/appflowy_flutter/lib/user/presentation/splash_screen.dart
@@ -1,10 +1,11 @@
+import 'package:appflowy/startup/tasks/supabase_task.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../startup/startup.dart';
-import '../application/auth_service.dart';
import '../application/splash_bloc.dart';
import '../domain/auth_state.dart';
import 'router.dart';
@@ -64,33 +65,40 @@ class SplashScreen extends StatelessWidget {
);
}
- void _handleAuthenticated(BuildContext context, Authenticated result) {
- final userProfile = result.userProfile;
- FolderEventReadCurrentWorkspace().send().then(
- (result) {
- return result.fold(
- (workspaceSetting) {
- getIt<SplashRoute>()
- .pushHomeScreen(context, userProfile, workspaceSetting);
- },
- (error) async {
- Log.error(error);
- getIt<SplashRoute>().pushWelcomeScreen(context, userProfile);
- },
+ Future<void> _handleAuthenticated(
+ BuildContext context,
+ Authenticated authenticated,
+ ) async {
+ final userProfile = authenticated.userProfile;
+ final result = await FolderEventReadCurrentWorkspace().send();
+ result.fold(
+ (workspaceSetting) {
+ getIt<SplashRoute>().pushHomeScreen(
+ context,
+ userProfile,
+ workspaceSetting,
);
},
+ (error) async {
+ Log.error(error);
+ getIt<SplashRoute>().pushWelcomeScreen(context, userProfile);
+ },
);
}
void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
- // getIt<SplashRoute>().pushSignInScreen(context);
- getIt<SplashRoute>().pushSkipLoginScreen(context);
+ // if the env is not configured, we will skip to the 'skip login screen'.
+ if (isSupabaseEnable) {
+ getIt<SplashRoute>().pushSignInScreen(context);
+ } else {
+ getIt<SplashRoute>().pushSkipLoginScreen(context);
+ }
}
Future<void> _registerIfNeeded() async {
final result = await UserEventCheckUser().send();
if (!result.isLeft()) {
- await getIt<AuthService>().autoSignUp();
+ await getIt<AuthService>().signUpAsGuest();
}
}
}
diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/settings_location_cubit.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/settings_location_cubit.dart
index 44b32dfe5d..344ffc2887 100644
--- a/frontend/appflowy_flutter/lib/workspace/application/settings/settings_location_cubit.dart
+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/settings_location_cubit.dart
@@ -1,71 +1,83 @@
import 'dart:io';
-import 'package:bloc/bloc.dart';
-import 'package:flutter/material.dart';
-import 'package:shared_preferences/shared_preferences.dart';
+import 'package:appflowy/core/config/kv.dart';
+import 'package:appflowy/core/config/kv_keys.dart';
+import 'package:appflowy/startup/startup.dart';
+import 'package:appflowy_backend/log.dart';
+import 'package:flutter/foundation.dart';
+import 'package:flutter_bloc/flutter_bloc.dart';
+import 'package:freezed_annotation/freezed_annotation.dart';
import '../../../startup/tasks/prelude.dart';
-@visibleForTesting
-const String kSettingsLocationDefaultLocation =
- 'kSettingsLocationDefaultLocation';
+part 'settings_location_cubit.freezed.dart';
-class SettingsLocation {
- SettingsLocation({
- String? path,
- }) : _path = path;
-
- String? _path;
+@freezed
+class SettingsLocationState with _$SettingsLocationState {
+ const factory SettingsLocationState.initial() = _Initial;
+ const factory SettingsLocationState.didReceivedPath(String path) =
+ _DidReceivedPath;
+}
- set path(String? path) {
- _path = path;
+class SettingsLocationCubit extends Cubit<SettingsLocationState> {
+ SettingsLocationCubit() : super(const SettingsLocationState.initial()) {
+ _init();
}
- String? get path {
- if (Platform.isMacOS) {
- // remove the prefix `/Volumes/*`
- return _path?.replaceFirst(RegExp(r'^/Volumes/[^/]+'), '');
- } else if (Platform.isWindows) {
- return _path?.replaceAll("/", "\\");
- }
- return _path;
+ Future<void> setPath(String path) async {
+ await getIt<LocalFileStorage>().setPath(path);
+ emit(SettingsLocationState.didReceivedPath(path));
}
- SettingsLocation copyWith({String? path}) {
- return SettingsLocation(
- path: path ?? this.path,
- );
+ Future<void> _init() async {
+ final path = await getIt<LocalFileStorage>().getPath();
+ emit(SettingsLocationState.didReceivedPath(path));
}
}
-class SettingsLocationCubit extends Cubit<SettingsLocation> {
- SettingsLocationCubit() : super(SettingsLocation(path: null));
+class LocalFileStorage {
+ LocalFileStorage();
+ String? _cachePath;
- /// Returns a path that used to store user data
- Future<String> fetchLocation() async {
- final prefs = await SharedPreferences.getInstance();
+ Future<void> setPath(String path) async {
+ if (kIsWeb || Platform.isAndroid || Platform.isIOS) {
+ Log.info('LocalFileStorage is not supported on this platform.');
+ return;
+ }
- /// Use the [appFlowyDocumentDirectory] instead if there is no user
- /// preference location
- final path = prefs.getString(kSettingsLocationDefaultLocation) ??
- (await appFlowyDocumentDirectory()).path;
+ if (Platform.isMacOS) {
+ // remove the prefix `/Volumes/*`
+ path = path.replaceFirst(RegExp(r'^/Volumes/[^/]+'), '');
+ } else if (Platform.isWindows) {
+ path = path.replaceAll('/', '\\');
+ }
- emit(state.copyWith(path: path));
- return Future.value(path);
+ await getIt<KeyValueStorage>().set(KVKeys.pathLocation, path);
+ // clear the cache path, and not set the cache path to the new path because the set path may be invalid
+ _cachePath = null;
}
- /// Saves the user preference local data store location
- Future<void> setLocation(String? path) async {
- path = path ?? (await appFlowyDocumentDirectory()).path;
+ Future<String> getPath() async {
+ if (_cachePath != null) {
+ return _cachePath!;
+ }
+
+ final response = await getIt<KeyValueStorage>().get(KVKeys.pathLocation);
+ final String path = await response.fold(
+ (error) async {
+ // return the default path if the path is not set
+ final directory = await appFlowyDocumentDirectory();
+ return directory.path;
+ },
+ (path) => path,
+ );
+ _cachePath = path;
- assert(path.isNotEmpty);
- if (path.isEmpty) {
- path = (await appFlowyDocumentDirectory()).path;
+ // if the path is not exists means the path is invalid, so we should clear the kv store
+ if (!Directory(path).existsSync()) {
+ await getIt<KeyValueStorage>().clear();
}
- final prefs = await SharedPreferences.getInstance();
- prefs.setString(kSettingsLocationDefaultLocation, path);
- await Directory(path).create(recursive: true);
- emit(state.copyWith(path: path));
+ return path;
}
}
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_customize_location_view.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_customize_location_view.dart
index 5ed0a07f13..8a7e9af1f7 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_customize_location_view.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_customize_location_view.dart
@@ -1,154 +1,258 @@
+import 'dart:io';
+
import 'package:appflowy/startup/entry_point.dart';
import 'package:appflowy/util/file_picker/file_picker_service.dart';
import 'package:appflowy/workspace/application/settings/settings_location_cubit.dart';
+import 'package:flowy_infra/image.dart';
+import 'package:flowy_infra_ui/style_widget/hover.dart';
+import 'package:flowy_infra_ui/widget/buttons/secondary_button.dart';
import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
-import 'package:flutter/services.dart';
-
+import 'package:styled_widget/styled_widget.dart';
+import 'package:url_launcher/url_launcher.dart';
import '../../../../generated/locale_keys.g.dart';
import '../../../../startup/launch_configuration.dart';
import '../../../../startup/startup.dart';
import '../../../../startup/tasks/prelude.dart';
-class SettingsFileLocationCustomzier extends StatefulWidget {
- const SettingsFileLocationCustomzier({
+class SettingsFileLocationCustomizer extends StatefulWidget {
+ const SettingsFileLocationCustomizer({
super.key,
- required this.cubit,
});
- final SettingsLocationCubit cubit;
-
@override
- State<SettingsFileLocationCustomzier> createState() =>
- SettingsFileLocationCustomzierState();
+ State<SettingsFileLocationCustomizer> createState() =>
+ SettingsFileLocationCustomizerState();
}
@visibleForTesting
-class SettingsFileLocationCustomzierState
- extends State<SettingsFileLocationCustomzier> {
+class SettingsFileLocationCustomizerState
+ extends State<SettingsFileLocationCustomizer> {
@override
Widget build(BuildContext context) {
- return BlocProvider<SettingsLocationCubit>.value(
- value: widget.cubit,
- child: BlocBuilder<SettingsLocationCubit, SettingsLocation>(
+ return BlocProvider<SettingsLocationCubit>(
+ create: (_) => SettingsLocationCubit(),
+ child: BlocBuilder<SettingsLocationCubit, SettingsLocationState>(
builder: (context, state) {
- return ListTile(
- title: FlowyText.medium(
- LocaleKeys.settings_files_defaultLocation.tr(),
- overflow: TextOverflow.ellipsis,
+ return state.when(
+ initial: () => const Center(
+ child: CircularProgressIndicator(),
),
- subtitle: Tooltip(
- message: LocaleKeys.settings_files_doubleTapToCopy.tr(),
- child: GestureDetector(
- onDoubleTap: () {
- Clipboard.setData(
- ClipboardData(
- text: state.path,
+ didReceivedPath: (path) {
+ return Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ // display file paths.
+ Flexible(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ mainAxisAlignment: MainAxisAlignment.spaceEvenly,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ FlowyText.medium(
+ LocaleKeys.settings_files_defaultLocation.tr(),
+ fontSize: 13,
+ overflow: TextOverflow.visible,
+ ).padding(horizontal: 5),
+ const VSpace(5),
+ _CopyableText(
+ usingPath: path,
+ ),
+ ],
),
- ).then((_) {
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: FlowyText(
- LocaleKeys.settings_files_pathCopiedSnackbar.tr(),
- color: Theme.of(context).colorScheme.onSurface,
- ),
+ ),
+
+ // display the icons
+ Flexible(
+ child: Row(
+ mainAxisAlignment: MainAxisAlignment.end,
+ children: [
+ _ChangeStoragePathButton(
+ usingPath: path,
),
- );
- }
- });
- },
- child: FlowyText.regular(
- state.path ?? '',
- overflow: TextOverflow.ellipsis,
+ const HSpace(10),
+ _OpenStorageButton(
+ usingPath: path,
+ ),
+ _RecoverDefaultStorageButton(
+ usingPath: path,
+ ),
+ ],
+ ),
+ ),
+ ],
+ );
+ },
+ );
+ },
+ ),
+ );
+ }
+}
+
+class _CopyableText extends StatelessWidget {
+ const _CopyableText({
+ required this.usingPath,
+ });
+
+ final String usingPath;
+
+ @override
+ Widget build(BuildContext context) {
+ return FlowyHover(
+ builder: (_, onHover) {
+ return GestureDetector(
+ onTap: () {
+ Clipboard.setData(ClipboardData(text: usingPath));
+ ScaffoldMessenger.of(context).showSnackBar(
+ SnackBar(
+ content: FlowyText(
+ LocaleKeys.settings_files_pathCopiedSnackbar.tr(),
+ color: Theme.of(context).colorScheme.onSurface,
),
),
- ),
- trailing: Row(
+ );
+ },
+ child: Container(
+ height: 20,
+ padding: const EdgeInsets.symmetric(horizontal: 5),
+ child: Row(
mainAxisSize: MainAxisSize.min,
children: [
- Tooltip(
- message: LocaleKeys.settings_files_restoreLocation.tr(),
- child: FlowyIconButton(
- height: 40,
- width: 40,
- icon: const Icon(Icons.restore_outlined),
- hoverColor:
- Theme.of(context).colorScheme.secondaryContainer,
- onPressed: () async {
- final result = await appFlowyDocumentDirectory();
- await _setCustomLocation(result.path);
- await FlowyRunner.run(
- FlowyApp(),
- config: const LaunchConfiguration(
- autoRegistrationSupported: true,
- ),
- );
- if (mounted) {
- Navigator.of(context).pop();
- }
- },
+ Flexible(
+ child: FlowyText.regular(
+ usingPath,
+ fontSize: 12,
+ overflow: TextOverflow.ellipsis,
),
),
- const SizedBox(
- width: 5,
- ),
- Tooltip(
- message: LocaleKeys.settings_files_customizeLocation.tr(),
- child: FlowyIconButton(
- height: 40,
- width: 40,
- icon: const Icon(Icons.folder_open_outlined),
- hoverColor:
- Theme.of(context).colorScheme.secondaryContainer,
- onPressed: () async {
- final result =
- await getIt<FilePickerService>().getDirectoryPath();
- if (result != null) {
- await _setCustomLocation(result);
- await reloadApp();
- }
- },
- ),
- )
+ if (onHover)
+ FlowyText.regular(
+ LocaleKeys.settings_files_copy.tr(),
+ fontSize: 12,
+ color: Theme.of(context).colorScheme.primary,
+ )
],
),
+ ),
+ );
+ },
+ );
+ }
+}
+
+class _ChangeStoragePathButton extends StatefulWidget {
+ const _ChangeStoragePathButton({
+ required this.usingPath,
+ });
+
+ final String usingPath;
+
+ @override
+ State<_ChangeStoragePathButton> createState() =>
+ _ChangeStoragePathButtonState();
+}
+
+class _ChangeStoragePathButtonState extends State<_ChangeStoragePathButton> {
+ @override
+ Widget build(BuildContext context) {
+ return Tooltip(
+ message: LocaleKeys.settings_files_changeLocationTooltips.tr(),
+ child: SecondaryTextButton(
+ LocaleKeys.settings_files_change.tr(),
+ mode: SecondaryTextButtonMode.small,
+ onPressed: () async {
+ // pick the new directory and reload app
+ final path = await getIt<FilePickerService>().getDirectoryPath();
+ if (path == null || !mounted || widget.usingPath == path) {
+ return;
+ }
+ await context.read<SettingsLocationCubit>().setPath(path);
+ await FlowyRunner.run(
+ FlowyApp(),
+ config: const LaunchConfiguration(
+ autoRegistrationSupported: true,
+ ),
);
+ if (mounted) {
+ Navigator.of(context).pop();
+ }
},
),
);
}
+}
+
+class _OpenStorageButton extends StatelessWidget {
+ const _OpenStorageButton({
+ required this.usingPath,
+ });
- Future<void> _setCustomLocation(String? path) async {
- // Using default location if path equals null.
- final location = path ?? (await appFlowyDocumentDirectory()).path;
- if (mounted) {
- widget.cubit.setLocation(location);
- }
-
- // The location could not save into the KV db, because the db initialize is later than the rust sdk initialize.
- /*
- final prefs = await SharedPreferences.getInstance();
- if (mounted) {
- context
- .read<AppearanceSettingsCubit>()
- .setKeyValue(AppearanceKeys.defaultLocation, location);
- }
- */
+ final String usingPath;
+
+ @override
+ Widget build(BuildContext context) {
+ return FlowyIconButton(
+ hoverColor: Theme.of(context).colorScheme.secondaryContainer,
+ tooltipText: LocaleKeys.settings_files_openLocationTooltips.tr(),
+ icon: svgWidget(
+ 'common/open_folder',
+ color: Theme.of(context).iconTheme.color,
+ ),
+ onPressed: () async {
+ final uri = Directory(usingPath).uri;
+ if (await canLaunchUrl(uri)) {
+ launchUrl(uri);
+ }
+ },
+ );
}
+}
+
+class _RecoverDefaultStorageButton extends StatefulWidget {
+ const _RecoverDefaultStorageButton({
+ required this.usingPath,
+ });
+
+ final String usingPath;
- Future<void> reloadApp() async {
- await FlowyRunner.run(
- FlowyApp(),
- config: const LaunchConfiguration(
- autoRegistrationSupported: true,
+ @override
+ State<_RecoverDefaultStorageButton> createState() =>
+ _RecoverDefaultStorageButtonState();
+}
+
+class _RecoverDefaultStorageButtonState
+ extends State<_RecoverDefaultStorageButton> {
+ @override
+ Widget build(BuildContext context) {
+ return FlowyIconButton(
+ hoverColor: Theme.of(context).colorScheme.secondaryContainer,
+ tooltipText: LocaleKeys.settings_files_recoverLocationTooltips.tr(),
+ icon: svgWidget(
+ 'common/recover',
+ color: Theme.of(context).iconTheme.color,
),
+ onPressed: () async {
+ // reset to the default directory and reload app
+ final directory = await appFlowyDocumentDirectory();
+ final path = directory.path;
+ if (!mounted || widget.usingPath == path) {
+ return;
+ }
+ await context.read<SettingsLocationCubit>().setPath(path);
+ await FlowyRunner.run(
+ FlowyApp(),
+ config: const LaunchConfiguration(
+ autoRegistrationSupported: true,
+ ),
+ );
+ if (mounted) {
+ Navigator.of(context).pop();
+ }
+ },
);
- if (mounted) {
- Navigator.of(context).pop();
- }
- return;
}
}
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_system_view.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_system_view.dart
index c90793fbfd..507efbe37b 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_system_view.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/settings_file_system_view.dart
@@ -1,8 +1,6 @@
import 'package:appflowy/workspace/presentation/settings/widgets/settings_file_customize_location_view.dart';
import 'package:flutter/material.dart';
-import '../../../application/settings/settings_location_cubit.dart';
-
class SettingsFileSystemView extends StatefulWidget {
const SettingsFileSystemView({
super.key,
@@ -13,16 +11,15 @@ class SettingsFileSystemView extends StatefulWidget {
}
class _SettingsFileSystemViewState extends State<SettingsFileSystemView> {
- final _locationCubit = SettingsLocationCubit()..fetchLocation();
-
@override
Widget build(BuildContext context) {
+ // return Column(
+ // children: [],
+ // );
return ListView.separated(
itemBuilder: (context, index) {
if (index == 0) {
- return SettingsFileLocationCustomzier(
- cubit: _locationCubit,
- );
+ return const SettingsFileLocationCustomizer();
} else if (index == 1) {
// return _buildExportDatabaseButton();
}
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart b/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart
index 2e3c8b0622..5b62a726bc 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart
@@ -227,7 +227,7 @@ class OkCancelButton extends StatelessWidget {
SecondaryTextButton(
cancelTitle ?? LocaleKeys.button_Cancel.tr(),
onPressed: onCancelPressed,
- bigMode: true,
+ mode: SecondaryTextButtonMode.big,
),
HSpace(Insets.m),
if (onOkPressed != null)
diff --git a/frontend/appflowy_flutter/macos/Runner/Info.plist b/frontend/appflowy_flutter/macos/Runner/Info.plist
index 4628404f6e..6c91d53468 100644
--- a/frontend/appflowy_flutter/macos/Runner/Info.plist
+++ b/frontend/appflowy_flutter/macos/Runner/Info.plist
@@ -40,5 +40,16 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
+ <key>CFBundleURLTypes</key>
+ <array>
+ <dict>
+ <key>CFBundleURLName</key>
+ <string></string>
+ <key>CFBundleURLSchemes</key>
+ <array>
+ <string>io.appflowy.appflowy-flutter</string>
+ </array>
+ </dict>
+ </array>
</dict>
</plist>
diff --git a/frontend/appflowy_flutter/packages/appflowy_popover/pubspec.yaml b/frontend/appflowy_flutter/packages/appflowy_popover/pubspec.yaml
index 80e808f890..7f83c6aee8 100644
--- a/frontend/appflowy_flutter/packages/appflowy_popover/pubspec.yaml
+++ b/frontend/appflowy_flutter/packages/appflowy_popover/pubspec.yaml
@@ -4,8 +4,8 @@ version: 0.0.1
homepage:
environment:
- sdk: ">=2.17.0 <3.0.0"
- flutter: ">=1.17.0"
+ sdk: ">=2.19.0 <3.0.0"
+ flutter: ">=3.7.0"
dependencies:
flutter:
diff --git a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/text.dart b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/text.dart
index 4dceb85710..972a330ef2 100644
--- a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/text.dart
+++ b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/text.dart
@@ -65,9 +65,12 @@ class FlowyText extends StatelessWidget {
@override
Widget build(BuildContext context) {
+ final text = overflow == TextOverflow.ellipsis
+ ? title.replaceAll('', '\u200B')
+ : title;
if (selectable) {
return SelectableText(
- title,
+ text,
maxLines: maxLines,
textAlign: textAlign,
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
@@ -79,7 +82,7 @@ class FlowyText extends StatelessWidget {
);
} else {
return Text(
- title,
+ text,
maxLines: maxLines,
textAlign: textAlign,
overflow: overflow ?? TextOverflow.clip,
diff --git a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/buttons/secondary_button.dart b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/buttons/secondary_button.dart
index eae3c58a2a..d45e6affbd 100644
--- a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/buttons/secondary_button.dart
+++ b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/buttons/secondary_button.dart
@@ -4,19 +4,50 @@ import 'package:flowy_infra/size.dart';
import 'base_styled_button.dart';
+enum SecondaryTextButtonMode {
+ normal,
+ big,
+ small;
+
+ Size get size {
+ switch (this) {
+ case SecondaryTextButtonMode.normal:
+ return const Size(80, 38);
+ case SecondaryTextButtonMode.big:
+ return const Size(100, 40);
+ case SecondaryTextButtonMode.small:
+ return const Size(100, 30);
+ }
+ }
+
+ BorderRadius get borderRadius {
+ switch (this) {
+ case SecondaryTextButtonMode.normal:
+ return Corners.s8Border;
+ case SecondaryTextButtonMode.big:
+ return Corners.s12Border;
+ case SecondaryTextButtonMode.small:
+ return Corners.s6Border;
+ }
+ }
+}
+
class SecondaryTextButton extends StatelessWidget {
+ const SecondaryTextButton(
+ this.label, {
+ super.key,
+ this.onPressed,
+ this.mode = SecondaryTextButtonMode.normal,
+ });
+
final String label;
final VoidCallback? onPressed;
- final bool bigMode;
-
- const SecondaryTextButton(this.label,
- {Key? key, this.onPressed, this.bigMode = false})
- : super(key: key);
+ final SecondaryTextButtonMode mode;
@override
Widget build(BuildContext context) {
return SecondaryButton(
- bigMode: bigMode,
+ mode: mode,
onPressed: onPressed,
child: FlowyText.regular(
label,
@@ -27,23 +58,27 @@ class SecondaryTextButton extends StatelessWidget {
}
class SecondaryButton extends StatelessWidget {
+ const SecondaryButton({
+ super.key,
+ required this.child,
+ this.onPressed,
+ this.mode = SecondaryTextButtonMode.normal,
+ });
+
final Widget child;
final VoidCallback? onPressed;
- final bool bigMode;
-
- const SecondaryButton(
- {Key? key, required this.child, this.onPressed, this.bigMode = false})
- : super(key: key);
+ final SecondaryTextButtonMode mode;
@override
Widget build(BuildContext context) {
+ final size = mode.size;
return BaseStyledButton(
- minWidth: bigMode ? 100 : 80,
- minHeight: bigMode ? 40 : 38,
+ minWidth: size.width,
+ minHeight: size.height,
contentPadding: EdgeInsets.zero,
bgColor: Theme.of(context).colorScheme.surface,
outlineColor: Theme.of(context).colorScheme.primary,
- borderRadius: bigMode ? Corners.s12Border : Corners.s8Border,
+ borderRadius: mode.borderRadius,
onPressed: onPressed,
child: child,
);
diff --git a/frontend/appflowy_flutter/packages/flowy_infra_ui/pubspec.yaml b/frontend/appflowy_flutter/packages/flowy_infra_ui/pubspec.yaml
index 59d0c48b7e..03547676a8 100644
--- a/frontend/appflowy_flutter/packages/flowy_infra_ui/pubspec.yaml
+++ b/frontend/appflowy_flutter/packages/flowy_infra_ui/pubspec.yaml
@@ -5,8 +5,8 @@ homepage:
publish_to: "none"
environment:
- sdk: ">=2.12.0 <3.0.0"
- flutter: ">=1.20.0"
+ sdk: ">=2.19.0 <3.0.0"
+ flutter: ">=3.7.0"
dependencies:
flutter:
diff --git a/frontend/appflowy_flutter/pubspec.lock b/frontend/appflowy_flutter/pubspec.lock
index 99cecdcfac..065c49f0ac 100644
--- a/frontend/appflowy_flutter/pubspec.lock
+++ b/frontend/appflowy_flutter/pubspec.lock
@@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
- sha256: "4897882604d919befd350648c7f91926a9d5de99e67b455bf0917cc2362f4bb8"
+ sha256: "405666cd3cf0ee0a48d21ec67e65406aad2c726d9fa58840d3375e7bdcd32a07"
url: "https://pub.dev"
source: hosted
- version: "47.0.0"
+ version: "60.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
- sha256: "690e335554a8385bc9d787117d9eb52c0c03ee207a607e593de3c9d71b1cfe80"
+ sha256: "1952250bd005bacb895a01bf1b4dc00e3ba1c526cf47dca54dfe24979c65f5b3"
url: "https://pub.dev"
source: hosted
- version: "4.7.0"
+ version: "5.12.0"
animations:
dependency: transitive
description:
@@ -149,10 +149,10 @@ packages:
dependency: "direct dev"
description:
name: build_runner
- sha256: "93f05c041932674be039b0a2323d6cf57e5f2bbf884a3c0382f9e53fc45ebace"
+ sha256: b0a8a7b8a76c493e85f1b84bffa0588859a06197863dba8c9036b15581fd9727
url: "https://pub.dev"
source: hosted
- version: "2.3.0"
+ version: "2.3.3"
build_runner_core:
dependency: transitive
description:
@@ -393,6 +393,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.0.2"
+ envied:
+ dependency: "direct main"
+ description:
+ name: envied
+ sha256: "60d3f5606c7b35bc6ef493e650d916b34351d8af2e58b7ac45881ba59dfcf039"
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.3.0+3"
+ envied_generator:
+ dependency: "direct dev"
+ description:
+ name: envied_generator
+ sha256: dfdbe5dc52863e54c036a4c4042afbdf1bd528cb4c1e638ecba26228ba72e9e5
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.3.0+3"
equatable:
dependency: "direct main"
description:
@@ -579,10 +595,10 @@ packages:
dependency: transitive
description:
name: frontend_server_client
- sha256: "4f4a162323c86ffc1245765cfe138872b8f069deb42f7dbb36115fa27f31469b"
+ sha256: "408e3ca148b31c20282ad6f37ebfa6f4bdc8fede5b74bc2f08d9d92b55db3612"
url: "https://pub.dev"
source: hosted
- version: "2.1.3"
+ version: "3.2.0"
fuchsia_remote_debug_protocol:
dependency: transitive
description: flutter
@@ -717,10 +733,10 @@ packages:
dependency: transitive
description:
name: intl_utils
- sha256: "856baa08d4735ee3476901827d52671c1a2b6f9ccb8b848d73bc5b8dd28b21e5"
+ sha256: db392393fbf891e3eb32f6beb1928b00cdb33e3c54597fd5f5dc5c43e5ba601c
url: "https://pub.dev"
source: hosted
- version: "2.7.0"
+ version: "2.8.2"
io:
dependency: transitive
description:
diff --git a/frontend/appflowy_flutter/pubspec.yaml b/frontend/appflowy_flutter/pubspec.yaml
index b0320c7953..0f2afa8481 100644
--- a/frontend/appflowy_flutter/pubspec.yaml
+++ b/frontend/appflowy_flutter/pubspec.yaml
@@ -108,6 +108,7 @@ dependencies:
flutter_svg: ^2.0.5
nanoid: ^1.0.0
supabase_flutter: ^1.9.1
+ envied: ^0.3.0+3
dev_dependencies:
flutter_lints: ^2.0.1
@@ -116,10 +117,11 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
- build_runner: ^2.2.0
+ build_runner: ^2.3.3
freezed: ^2.1.0+1
bloc_test: ^9.0.2
json_serializable: ^6.5.4
+ envied_generator: ^0.3.0+3
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
@@ -174,6 +176,7 @@ flutter:
- assets/images/emoji/
- assets/images/grid/field/
- assets/images/common/
+ - assets/images/login/
- assets/images/grid/setting/
- assets/translations/
diff --git a/frontend/appflowy_flutter/test/util.dart b/frontend/appflowy_flutter/test/util.dart
index 72606d5796..4278ea2a1b 100644
--- a/frontend/appflowy_flutter/test/util.dart
+++ b/frontend/appflowy_flutter/test/util.dart
@@ -1,6 +1,6 @@
import 'package:appflowy/startup/launch_configuration.dart';
import 'package:appflowy/startup/startup.dart';
-import 'package:appflowy/user/application/auth_service.dart';
+import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:appflowy/user/application/user_service.dart';
import 'package:appflowy/workspace/application/workspace/workspace_service.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
@@ -55,11 +55,11 @@ class AppFlowyUnitTest {
email: userEmail,
);
return result.fold(
+ (error) {},
(user) {
userProfile = user;
userService = UserBackendService(userId: userProfile.id);
},
- (error) {},
);
}
diff --git a/frontend/appflowy_tauri/src-tauri/Cargo.lock b/frontend/appflowy_tauri/src-tauri/Cargo.lock
index ed921f33f2..d0055f78ff 100644
--- a/frontend/appflowy_tauri/src-tauri/Cargo.lock
+++ b/frontend/appflowy_tauri/src-tauri/Cargo.lock
@@ -423,10 +423,10 @@ dependencies = [
"http",
"http-body",
"hyper",
- "hyper-rustls",
+ "hyper-rustls 0.23.2",
"lazy_static",
"pin-project-lite",
- "rustls",
+ "rustls 0.20.8",
"tokio",
"tower",
"tracing",
@@ -1791,6 +1791,7 @@ dependencies = [
"flowy-error",
"flowy-folder2",
"flowy-net",
+ "flowy-server",
"flowy-sqlite",
"flowy-task",
"flowy-user",
@@ -1932,6 +1933,7 @@ dependencies = [
"tokio",
"tracing",
"unicode-segmentation",
+ "uuid",
]
[[package]]
@@ -1939,33 +1941,14 @@ name = "flowy-net"
version = "0.1.0"
dependencies = [
"anyhow",
- "async-stream",
"bytes",
- "config",
- "dashmap",
"flowy-codegen",
"flowy-derive",
- "flowy-document2",
"flowy-error",
- "flowy-folder2",
- "flowy-user",
- "futures-util",
- "hyper",
- "lazy_static",
"lib-dispatch",
- "lib-infra",
- "lib-ws",
- "nanoid",
- "parking_lot 0.12.1",
"protobuf",
- "reqwest",
- "serde",
- "serde-aux",
- "serde_json",
- "strum",
"strum_macros",
"thiserror",
- "tokio",
"tracing",
]
@@ -1983,6 +1966,34 @@ dependencies = [
"tracing",
]
+[[package]]
+name = "flowy-server"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "bytes",
+ "config",
+ "flowy-config",
+ "flowy-error",
+ "flowy-user",
+ "futures-util",
+ "hyper",
+ "lazy_static",
+ "lib-infra",
+ "nanoid",
+ "parking_lot 0.12.1",
+ "postgrest",
+ "reqwest",
+ "serde",
+ "serde-aux",
+ "serde_json",
+ "thiserror",
+ "tokio",
+ "tokio-retry",
+ "tracing",
+ "uuid",
+]
+
[[package]]
name = "flowy-sqlite"
version = "0.1.0"
@@ -2036,6 +2047,7 @@ dependencies = [
"protobuf",
"serde",
"serde_json",
+ "serde_repr",
"strum",
"strum_macros",
"tokio",
@@ -2706,10 +2718,23 @@ dependencies = [
"http",
"hyper",
"log",
- "rustls",
+ "rustls 0.20.8",
"rustls-native-certs",
"tokio",
- "tokio-rustls",
+ "tokio-rustls 0.23.4",
+]
+
+[[package]]
+name = "hyper-rustls"
+version = "0.24.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0646026eb1b3eea4cd9ba47912ea5ce9cc07713d105b1a14698f4e6433d348b7"
+dependencies = [
+ "http",
+ "hyper",
+ "rustls 0.21.1",
+ "tokio",
+ "tokio-rustls 0.24.0",
]
[[package]]
@@ -3021,6 +3046,7 @@ dependencies = [
name = "lib-infra"
version = "0.1.0"
dependencies = [
+ "anyhow",
"async-trait",
"bytes",
"chrono",
@@ -3994,6 +4020,15 @@ dependencies = [
"miniz_oxide 0.7.1",
]
+[[package]]
+name = "postgrest"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e66400cb23a379592bc8c8bdc9adda652eef4a969b74ab78454a8e8c11330c2b"
+dependencies = [
+ "reqwest",
+]
+
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@@ -4419,6 +4454,7 @@ dependencies = [
"http",
"http-body",
"hyper",
+ "hyper-rustls 0.24.0",
"hyper-tls",
"ipnet",
"js-sys",
@@ -4428,16 +4464,20 @@ dependencies = [
"once_cell",
"percent-encoding",
"pin-project-lite",
+ "rustls 0.21.1",
+ "rustls-pemfile",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
+ "tokio-rustls 0.24.0",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
+ "webpki-roots",
"winreg 0.10.1",
]
@@ -4587,6 +4627,18 @@ dependencies = [
"webpki",
]
+[[package]]
+name = "rustls"
+version = "0.21.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c911ba11bc8433e811ce56fde130ccf32f5127cab0e0194e9c68c5a5b671791e"
+dependencies = [
+ "log",
+ "ring",
+ "rustls-webpki",
+ "sct",
+]
+
[[package]]
name = "rustls-native-certs"
version = "0.6.2"
@@ -4608,6 +4660,16 @@ dependencies = [
"base64 0.21.0",
]
+[[package]]
+name = "rustls-webpki"
+version = "0.100.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d6207cd5ed3d8dca7816f8f3725513a34609c0c765bf652b8c3cb4cfd87db46b"
+dependencies = [
+ "ring",
+ "untrusted",
+]
+
[[package]]
name = "rustversion"
version = "1.0.12"
@@ -4754,9 +4816,9 @@ dependencies = [
[[package]]
name = "serde-aux"
-version = "1.1.0"
+version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "905f2fc9f3d1574e8b5923a58118240021f01d4e239673937ffb9f42707a4f22"
+checksum = "c3dfe1b7eb6f9dcf011bd6fad169cdeaae75eda0d61b1a99a3f015b41b0cae39"
dependencies = [
"chrono",
"serde",
@@ -5641,11 +5703,21 @@ version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59"
dependencies = [
- "rustls",
+ "rustls 0.20.8",
"tokio",
"webpki",
]
+[[package]]
+name = "tokio-rustls"
+version = "0.24.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e0d409377ff5b1e3ca6437aa86c1eb7d40c134bfec254e44c830defa92669db5"
+dependencies = [
+ "rustls 0.21.1",
+ "tokio",
+]
+
[[package]]
name = "tokio-stream"
version = "0.1.14"
@@ -6329,6 +6401,15 @@ dependencies = [
"untrusted",
]
+[[package]]
+name = "webpki-roots"
+version = "0.22.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87"
+dependencies = [
+ "webpki",
+]
+
[[package]]
name = "webview2-com"
version = "0.19.1"
diff --git a/frontend/appflowy_tauri/src-tauri/src/init.rs b/frontend/appflowy_tauri/src-tauri/src/init.rs
index fbffd072c5..3043d0c785 100644
--- a/frontend/appflowy_tauri/src-tauri/src/init.rs
+++ b/frontend/appflowy_tauri/src-tauri/src/init.rs
@@ -1,5 +1,4 @@
-use flowy_core::{ AppFlowyCore, AppFlowyCoreConfig, DEFAULT_NAME};
-use flowy_net::http_server::self_host::configuration::get_client_server_configuration;
+use flowy_core::{AppFlowyCore, AppFlowyCoreConfig, DEFAULT_NAME};
pub fn init_flowy_core() -> AppFlowyCore {
let config_json = include_str!("../tauri.conf.json");
@@ -12,12 +11,7 @@ pub fn init_flowy_core() -> AppFlowyCore {
data_path.push("data");
std::env::set_var("RUST_LOG", "trace");
- let server_config = get_client_server_configuration().unwrap();
- let config = AppFlowyCoreConfig::new(
- data_path.to_str().unwrap(),
- DEFAULT_NAME.to_string(),
- server_config,
- )
- .log_filter("trace", vec!["appflowy_tauri".to_string()]);
+ let config = AppFlowyCoreConfig::new(data_path.to_str().unwrap(), DEFAULT_NAME.to_string())
+ .log_filter("trace", vec!["appflowy_tauri".to_string()]);
AppFlowyCore::new(config)
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/stores/effects/user/user_bd_svc.ts b/frontend/appflowy_tauri/src/appflowy_app/stores/effects/user/user_bd_svc.ts
index 7c9e50d3fc..2159dc761c 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/stores/effects/user/user_bd_svc.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/stores/effects/user/user_bd_svc.ts
@@ -1,5 +1,7 @@
import { nanoid } from '@reduxjs/toolkit';
import {
+ AuthTypePB,
+ SignOutPB,
UserEventCheckUser,
UserEventGetUserProfile,
UserEventSignIn,
@@ -8,13 +10,13 @@ import {
UserEventUpdateUserProfile,
} from '@/services/backend/events/flowy-user';
import {
+ CreateWorkspacePayloadPB,
SignInPayloadPB,
SignUpPayloadPB,
UpdateUserProfilePayloadPB,
WorkspaceIdPB,
- CreateWorkspacePayloadPB,
- WorkspaceSettingPB,
WorkspacePB,
+ WorkspaceSettingPB,
} from '@/services/backend';
import {
FolderEventCreateWorkspace,
@@ -81,7 +83,8 @@ export class UserBackendService {
};
signOut = () => {
- return UserEventSignOut();
+ const payload = SignOutPB.fromObject({ auth_type: AuthTypePB.Local });
+ return UserEventSignOut(payload);
};
}
@@ -97,7 +100,8 @@ export class AuthBackendService {
};
signOut = () => {
- return UserEventSignOut();
+ const payload = SignOutPB.fromObject({ auth_type: AuthTypePB.Local });
+ return UserEventSignOut(payload);
};
autoSignUp = () => {
diff --git a/frontend/rust-lib/.gitignore b/frontend/rust-lib/.gitignore
index 740f8d77a7..5e19a3e66b 100644
--- a/frontend/rust-lib/.gitignore
+++ b/frontend/rust-lib/.gitignore
@@ -13,4 +13,5 @@ bin/
**/src/protobuf
**/resources/proto
.idea/
-AppFlowy-Collab/
\ No newline at end of file
+AppFlowy-Collab/
+.env
\ No newline at end of file
diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lock
index bf8f64793c..591497edb9 100644
--- a/frontend/rust-lib/Cargo.lock
+++ b/frontend/rust-lib/Cargo.lock
@@ -1262,6 +1262,7 @@ dependencies = [
"flowy-derive",
"flowy-net",
"flowy-notification",
+ "flowy-server",
"lazy_static",
"lib-dispatch",
"log",
@@ -1376,6 +1377,12 @@ dependencies = [
"winapi",
]
+[[package]]
+name = "dotenv"
+version = "0.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
+
[[package]]
name = "dyn-clone"
version = "1.0.11"
@@ -1563,6 +1570,7 @@ dependencies = [
"flowy-error",
"flowy-folder2",
"flowy-net",
+ "flowy-server",
"flowy-sqlite",
"flowy-task",
"flowy-user",
@@ -1709,6 +1717,7 @@ dependencies = [
"tokio",
"tracing",
"unicode-segmentation",
+ "uuid",
]
[[package]]
@@ -1716,33 +1725,14 @@ name = "flowy-net"
version = "0.1.0"
dependencies = [
"anyhow",
- "async-stream",
"bytes",
- "config",
- "dashmap",
"flowy-codegen",
"flowy-derive",
- "flowy-document2",
"flowy-error",
- "flowy-folder2",
- "flowy-user",
- "futures-util",
- "hyper",
- "lazy_static",
"lib-dispatch",
- "lib-infra",
- "lib-ws",
- "nanoid",
- "parking_lot 0.12.1",
"protobuf",
- "reqwest",
- "serde",
- "serde-aux",
- "serde_json",
- "strum",
"strum_macros",
"thiserror",
- "tokio",
"tracing",
]
@@ -1760,6 +1750,35 @@ dependencies = [
"tracing",
]
+[[package]]
+name = "flowy-server"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "bytes",
+ "config",
+ "dotenv",
+ "flowy-config",
+ "flowy-error",
+ "flowy-user",
+ "futures-util",
+ "hyper",
+ "lazy_static",
+ "lib-infra",
+ "nanoid",
+ "parking_lot 0.12.1",
+ "postgrest",
+ "reqwest",
+ "serde",
+ "serde-aux",
+ "serde_json",
+ "thiserror",
+ "tokio",
+ "tokio-retry",
+ "tracing",
+ "uuid",
+]
+
[[package]]
name = "flowy-sqlite"
version = "0.1.0"
@@ -1804,6 +1823,7 @@ dependencies = [
"flowy-core",
"flowy-folder2",
"flowy-net",
+ "flowy-server",
"flowy-user",
"futures",
"futures-util",
@@ -1853,6 +1873,7 @@ dependencies = [
"rand_core 0.6.4",
"serde",
"serde_json",
+ "serde_repr",
"strum",
"strum_macros",
"tokio",
@@ -2488,6 +2509,7 @@ dependencies = [
name = "lib-infra"
version = "0.1.0"
dependencies = [
+ "anyhow",
"async-trait",
"bytes",
"chrono",
@@ -3237,6 +3259,15 @@ version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
+[[package]]
+name = "postgrest"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e66400cb23a379592bc8c8bdc9adda652eef4a969b74ab78454a8e8c11330c2b"
+dependencies = [
+ "reqwest",
+]
+
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@@ -3727,6 +3758,7 @@ dependencies = [
"http",
"http-body",
"hyper",
+ "hyper-rustls",
"hyper-tls",
"ipnet",
"js-sys",
@@ -3736,16 +3768,20 @@ dependencies = [
"once_cell",
"percent-encoding",
"pin-project-lite",
+ "rustls",
+ "rustls-pemfile",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
+ "tokio-rustls",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
+ "webpki-roots",
"winreg",
]
@@ -4030,9 +4066,9 @@ dependencies = [
[[package]]
name = "serde-aux"
-version = "1.1.0"
+version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "905f2fc9f3d1574e8b5923a58118240021f01d4e239673937ffb9f42707a4f22"
+checksum = "c3dfe1b7eb6f9dcf011bd6fad169cdeaae75eda0d61b1a99a3f015b41b0cae39"
dependencies = [
"chrono",
"serde",
@@ -4970,6 +5006,15 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
+[[package]]
+name = "uuid"
+version = "1.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "345444e32442451b267fc254ae85a209c64be56d2890e601a0c37ff0c3c5ecd2"
+dependencies = [
+ "getrandom 0.2.9",
+]
+
[[package]]
name = "validator"
version = "0.16.0"
@@ -5133,6 +5178,15 @@ dependencies = [
"untrusted",
]
+[[package]]
+name = "webpki-roots"
+version = "0.22.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87"
+dependencies = [
+ "webpki",
+]
+
[[package]]
name = "which"
version = "4.4.0"
diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.toml
index f9232200d6..d613ae3bea 100644
--- a/frontend/rust-lib/Cargo.toml
+++ b/frontend/rust-lib/Cargo.toml
@@ -14,6 +14,7 @@ members = [
"flowy-error",
"flowy-database2",
"flowy-task",
+ "flowy-server",
"flowy-config",
]
diff --git a/frontend/rust-lib/dart-ffi/Cargo.toml b/frontend/rust-lib/dart-ffi/Cargo.toml
index 5c2d0b71e9..0b0380d9e6 100644
--- a/frontend/rust-lib/dart-ffi/Cargo.toml
+++ b/frontend/rust-lib/dart-ffi/Cargo.toml
@@ -31,6 +31,7 @@ flowy-core = { path = "../flowy-core" }
flowy-notification = { path = "../flowy-notification" }
flowy-net = { path = "../flowy-net" }
flowy-derive = { path = "../../../shared-lib/flowy-derive" }
+flowy-server = { path = "../flowy-server" }
[features]
default = ["dart", "rev-sqlite"]
diff --git a/frontend/rust-lib/dart-ffi/src/lib.rs b/frontend/rust-lib/dart-ffi/src/lib.rs
index e86227aa1e..4b18368f90 100644
--- a/frontend/rust-lib/dart-ffi/src/lib.rs
+++ b/frontend/rust-lib/dart-ffi/src/lib.rs
@@ -6,7 +6,6 @@ use lazy_static::lazy_static;
use parking_lot::RwLock;
use flowy_core::*;
-use flowy_net::http_server::self_host::configuration::get_client_server_configuration;
use flowy_notification::register_notification_sender;
use lib_dispatch::prelude::ToBytes;
use lib_dispatch::prelude::*;
@@ -32,10 +31,9 @@ pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
let path: &str = c_str.to_str().unwrap();
- let server_config = get_client_server_configuration().unwrap();
let log_crates = vec!["flowy-ffi".to_string()];
- let config = AppFlowyCoreConfig::new(path, DEFAULT_NAME.to_string(), server_config)
- .log_filter("info", log_crates);
+ let config =
+ AppFlowyCoreConfig::new(path, DEFAULT_NAME.to_string()).log_filter("info", log_crates);
*APPFLOWY_CORE.write() = Some(AppFlowyCore::new(config));
0
diff --git a/frontend/rust-lib/flowy-config/src/entities.rs b/frontend/rust-lib/flowy-config/src/entities.rs
index d3b1bad2e1..625e45f7d0 100644
--- a/frontend/rust-lib/flowy-config/src/entities.rs
+++ b/frontend/rust-lib/flowy-config/src/entities.rs
@@ -15,18 +15,35 @@ pub struct KeyPB {
pub key: String,
}
+pub const SUPABASE_URL: &str = "SUPABASE_URL";
+pub const SUPABASE_ANON_KEY: &str = "SUPABASE_ANON_KEY";
+pub const SUPABASE_KEY: &str = "SUPABASE_KEY";
+pub const SUPABASE_JWT_SECRET: &str = "SUPABASE_JWT_SECRET";
+
#[derive(Default, ProtoBuf)]
pub struct SupabaseConfigPB {
#[pb(index = 1)]
supabase_url: String,
#[pb(index = 2)]
- supabase_key: String,
+ anon_key: String,
#[pb(index = 3)]
+ key: String,
+
+ #[pb(index = 4)]
jwt_secret: String,
}
+impl SupabaseConfigPB {
+ pub(crate) fn write_to_env(self) {
+ std::env::set_var(SUPABASE_URL, self.supabase_url);
+ std::env::set_var(SUPABASE_ANON_KEY, self.anon_key);
+ std::env::set_var(SUPABASE_KEY, self.key);
+ std::env::set_var(SUPABASE_JWT_SECRET, self.jwt_secret);
+ }
+}
+
#[derive(Default, ProtoBuf)]
pub struct AppFlowyCollabConfigPB {
#[pb(index = 1, one_of)]
diff --git a/frontend/rust-lib/flowy-config/src/event_handler.rs b/frontend/rust-lib/flowy-config/src/event_handler.rs
index 9bd3b1eda4..9915e83526 100644
--- a/frontend/rust-lib/flowy-config/src/event_handler.rs
+++ b/frontend/rust-lib/flowy-config/src/event_handler.rs
@@ -35,6 +35,7 @@ pub(crate) async fn remove_key_value_handler(data: AFPluginData<KeyPB>) -> Flowy
pub(crate) async fn set_supabase_config_handler(
data: AFPluginData<SupabaseConfigPB>,
) -> FlowyResult<()> {
- let _config = data.into_inner();
+ let config = data.into_inner();
+ config.write_to_env();
Ok(())
}
diff --git a/frontend/rust-lib/flowy-config/src/event_map.rs b/frontend/rust-lib/flowy-config/src/event_map.rs
index 1a4068acdb..e9c5392547 100644
--- a/frontend/rust-lib/flowy-config/src/event_map.rs
+++ b/frontend/rust-lib/flowy-config/src/event_map.rs
@@ -26,6 +26,8 @@ pub enum ConfigEvent {
#[event(input = "KeyPB")]
RemoveKeyValue = 2,
+ /// Set the supabase config. It will be written to the environment variables.
+ /// Check out the `write_to_env` of [SupabaseConfigPB].
#[event(input = "SupabaseConfigPB")]
SetSupabaseConfig = 3,
}
diff --git a/frontend/rust-lib/flowy-config/src/lib.rs b/frontend/rust-lib/flowy-config/src/lib.rs
index 0c11fb9c5a..e08a6c9ce6 100644
--- a/frontend/rust-lib/flowy-config/src/lib.rs
+++ b/frontend/rust-lib/flowy-config/src/lib.rs
@@ -1,4 +1,4 @@
-mod entities;
+pub mod entities;
mod event_handler;
pub mod event_map;
mod protobuf;
diff --git a/frontend/rust-lib/flowy-core/Cargo.toml b/frontend/rust-lib/flowy-core/Cargo.toml
index bee70ed1a5..05597d4c2a 100644
--- a/frontend/rust-lib/flowy-core/Cargo.toml
+++ b/frontend/rust-lib/flowy-core/Cargo.toml
@@ -19,6 +19,7 @@ flowy-document2 = { path = "../flowy-document2" }
#flowy-revision = { path = "../flowy-revision" }
flowy-error = { path = "../flowy-error" }
flowy-task = { path = "../flowy-task" }
+flowy-server = { path = "../flowy-server" }
flowy-config = { path = "../flowy-config" }
appflowy-integrate = { version = "0.1.0" }
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs
index db8bc84e45..d388be3b1e 100644
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs
+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs
@@ -31,7 +31,7 @@ impl DatabaseUser2 for DatabaseUserImpl {
.map_err(|e| FlowyError::internal().context(e))
}
- fn token(&self) -> Result<String, FlowyError> {
+ fn token(&self) -> Result<Option<String>, FlowyError> {
self
.0
.token()
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/document2_deps.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/document2_deps.rs
index 3bbc269443..5ad0a81dae 100644
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/document2_deps.rs
+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/document2_deps.rs
@@ -29,7 +29,7 @@ impl DocumentUser for DocumentUserImpl {
.map_err(|e| FlowyError::internal().context(e))
}
- fn token(&self) -> Result<String, FlowyError> {
+ fn token(&self) -> Result<Option<String>, FlowyError> {
self
.0
.token()
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/folder2_deps.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/folder2_deps.rs
index 4f74aed4c8..07cd1d0fc0 100644
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/folder2_deps.rs
+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/folder2_deps.rs
@@ -63,7 +63,7 @@ impl FolderUser for FolderUserImpl {
.map_err(|e| FlowyError::internal().context(e))
}
- fn token(&self) -> Result<String, FlowyError> {
+ fn token(&self) -> Result<Option<String>, FlowyError> {
self
.0
.token()
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/folder_deps.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/folder_deps.rs
index 772b015ca9..cf2a7381d1 100644
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/folder_deps.rs
+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/folder_deps.rs
@@ -117,7 +117,7 @@ impl WorkspaceUser for WorkspaceUserImpl {
.map_err(|e| FlowyError::internal().context(e))
}
- fn token(&self) -> Result<String, FlowyError> {
+ fn token(&self) -> Result<Option<String>, FlowyError> {
self
.0
.token()
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/mod.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/mod.rs
index 06fd4a9cef..7264d60864 100644
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/mod.rs
+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/mod.rs
@@ -1,11 +1,9 @@
pub use database_deps::*;
pub use document2_deps::*;
pub use folder2_deps::*;
-pub use user_deps::*;
mod document2_deps;
mod folder2_deps;
-mod user_deps;
mod util;
mod database_deps;
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/user_deps.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/user_deps.rs
deleted file mode 100644
index 66b3922070..0000000000
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/user_deps.rs
+++ /dev/null
@@ -1,19 +0,0 @@
-use std::sync::Arc;
-
-use flowy_net::http_server::self_host::configuration::ClientServerConfiguration;
-use flowy_net::http_server::self_host::user::UserHttpCloudService;
-use flowy_net::local_server::LocalServer;
-use flowy_user::event_map::UserCloudService;
-
-pub struct UserDepsResolver();
-impl UserDepsResolver {
- pub fn resolve(
- local_server: &Option<Arc<LocalServer>>,
- server_config: &ClientServerConfiguration,
- ) -> Arc<dyn UserCloudService> {
- match local_server.clone() {
- None => Arc::new(UserHttpCloudService::new(server_config)),
- Some(local_server) => local_server,
- }
- }
-}
diff --git a/frontend/rust-lib/flowy-core/src/integrate/mod.rs b/frontend/rust-lib/flowy-core/src/integrate/mod.rs
new file mode 100644
index 0000000000..da8d32a924
--- /dev/null
+++ b/frontend/rust-lib/flowy-core/src/integrate/mod.rs
@@ -0,0 +1 @@
+pub(crate) mod server;
diff --git a/frontend/rust-lib/flowy-core/src/integrate/server.rs b/frontend/rust-lib/flowy-core/src/integrate/server.rs
new file mode 100644
index 0000000000..eb4a5bb0f9
--- /dev/null
+++ b/frontend/rust-lib/flowy-core/src/integrate/server.rs
@@ -0,0 +1,68 @@
+use std::collections::HashMap;
+use std::sync::Arc;
+
+use parking_lot::RwLock;
+
+use flowy_error::{ErrorCode, FlowyError};
+use flowy_server::local_server::LocalServer;
+use flowy_server::self_host::configuration::self_host_server_configuration;
+use flowy_server::self_host::SelfHostServer;
+use flowy_server::supabase::{SupabaseConfiguration, SupabaseServer};
+use flowy_server::AppFlowyServer;
+use flowy_user::event_map::{UserAuthService, UserCloudServiceProvider};
+use flowy_user::services::AuthType;
+
+/// The [AppFlowyServerProvider] provides list of [AppFlowyServer] base on the [AuthType]. Using
+/// the auth type, the [AppFlowyServerProvider] will create a new [AppFlowyServer] if it doesn't
+/// exist.
+/// Each server implements the [AppFlowyServer] trait, which provides the [UserAuthService], etc.
+#[derive(Default)]
+pub struct AppFlowyServerProvider {
+ providers: RwLock<HashMap<AuthType, Arc<dyn AppFlowyServer>>>,
+}
+
+impl AppFlowyServerProvider {
+ pub fn new() -> Self {
+ Self::default()
+ }
+}
+
+impl UserCloudServiceProvider for AppFlowyServerProvider {
+ /// Returns the [UserAuthService] base on the current [AuthType].
+ /// Creates a new [AppFlowyServer] if it doesn't exist.
+ fn get_auth_service(&self, auth_type: &AuthType) -> Result<Arc<dyn UserAuthService>, FlowyError> {
+ if let Some(provider) = self.providers.read().get(auth_type) {
+ return Ok(provider.user_service());
+ }
+
+ let server = server_from_auth_type(auth_type)?;
+ let user_service = server.user_service();
+ self.providers.write().insert(auth_type.clone(), server);
+ Ok(user_service)
+ }
+}
+
+fn server_from_auth_type(auth_type: &AuthType) -> Result<Arc<dyn AppFlowyServer>, FlowyError> {
+ match auth_type {
+ AuthType::Local => {
+ let server = Arc::new(LocalServer::new());
+ Ok(server)
+ },
+ AuthType::SelfHosted => {
+ let config = self_host_server_configuration().map_err(|e| {
+ FlowyError::new(
+ ErrorCode::InvalidAuthConfig,
+ format!("Missing self host config: {:?}. Error: {:?}", auth_type, e),
+ )
+ })?;
+ let server = Arc::new(SelfHostServer::new(config));
+ Ok(server)
+ },
+ AuthType::Supabase => {
+ // init the SupabaseServerConfiguration from the environment variables.
+ let config = SupabaseConfiguration::from_env()?;
+ let server = Arc::new(SupabaseServer::new(config));
+ Ok(server)
+ },
+ }
+}
diff --git a/frontend/rust-lib/flowy-core/src/lib.rs b/frontend/rust-lib/flowy-core/src/lib.rs
index 0b865c82b2..c5b5b81a64 100644
--- a/frontend/rust-lib/flowy-core/src/lib.rs
+++ b/frontend/rust-lib/flowy-core/src/lib.rs
@@ -1,3 +1,5 @@
+#![allow(unused_doc_comments)]
+
use std::str::FromStr;
use std::time::Duration;
use std::{
@@ -16,12 +18,10 @@ use flowy_database2::DatabaseManager2;
use flowy_document2::manager::DocumentManager as DocumentManager2;
use flowy_error::FlowyResult;
use flowy_folder2::manager::Folder2Manager;
-use flowy_net::http_server::self_host::configuration::ClientServerConfiguration;
-use flowy_net::local_server::LocalServer;
use flowy_sqlite::kv::KV;
use flowy_task::{TaskDispatcher, TaskRunner};
use flowy_user::entities::UserProfile;
-use flowy_user::event_map::UserStatusCallback;
+use flowy_user::event_map::{UserCloudServiceProvider, UserStatusCallback};
use flowy_user::services::{UserSession, UserSessionConfig};
use lib_dispatch::prelude::*;
use lib_dispatch::runtime::tokio_default_runtime;
@@ -30,8 +30,10 @@ use module::make_plugins;
pub use module::*;
use crate::deps_resolve::*;
+use crate::integrate::server::AppFlowyServerProvider;
mod deps_resolve;
+mod integrate;
pub mod module;
static INIT_LOG: AtomicBool = AtomicBool::new(false);
@@ -47,25 +49,22 @@ pub struct AppFlowyCoreConfig {
/// Panics if the `root` path is not existing
storage_path: String,
log_filter: String,
- server_config: ClientServerConfiguration,
}
impl fmt::Debug for AppFlowyCoreConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AppFlowyCoreConfig")
.field("storage_path", &self.storage_path)
- .field("server-config", &self.server_config)
.finish()
}
}
impl AppFlowyCoreConfig {
- pub fn new(root: &str, name: String, server_config: ClientServerConfiguration) -> Self {
+ pub fn new(root: &str, name: String) -> Self {
AppFlowyCoreConfig {
name,
storage_path: root.to_owned(),
log_filter: create_log_filter("info".to_owned(), vec![]),
- server_config,
}
}
@@ -117,22 +116,33 @@ pub struct AppFlowyCore {
pub user_session: Arc<UserSession>,
pub document_manager2: Arc<DocumentManager2>,
pub folder_manager: Arc<Folder2Manager>,
- // pub database_manager: Arc<DatabaseManager>,
pub database_manager: Arc<DatabaseManager2>,
pub event_dispatcher: Arc<AFPluginDispatcher>,
- pub local_server: Option<Arc<LocalServer>>,
+ pub server_provider: Arc<AppFlowyServerProvider>,
pub task_dispatcher: Arc<RwLock<TaskDispatcher>>,
}
impl AppFlowyCore {
pub fn new(config: AppFlowyCoreConfig) -> Self {
+ /// The profiling can be used to tracing the performance of the application.
+ /// Check out the [Link](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/profiling)
+ /// for more information.
#[cfg(feature = "profiling")]
console_subscriber::init();
+ // Init the logger before anything else
init_log(&config);
+
+ // Init the key value database
init_kv(&config.storage_path);
+
+ // The collab config is used to build the [Collab] instance that used in document,
+ // database, folder, etc.
let collab_config = get_collab_config();
inject_aws_env(collab_config.aws_config());
+
+ /// The shared collab builder is used to build the [Collab] instance. The plugins will be loaded
+ /// on demand based on the [AppFlowyCollabConfig].
let collab_builder = Arc::new(AppFlowyCollabBuilder::new(collab_config));
tracing::debug!("🔥 {:?}", config);
@@ -141,11 +151,10 @@ impl AppFlowyCore {
let task_dispatcher = Arc::new(RwLock::new(task_scheduler));
runtime.spawn(TaskRunner::run(task_dispatcher.clone()));
- let local_server = mk_local_server(&config.server_config);
- let (user_session, folder_manager, local_server, database_manager, document_manager2) = runtime
- .block_on(async {
- let user_session = mk_user_session(&config, &local_server, &config.server_config);
-
+ let server_provider = Arc::new(AppFlowyServerProvider::new());
+ let (user_session, folder_manager, server_provider, database_manager, document_manager2) =
+ runtime.block_on(async {
+ let user_session = mk_user_session(&config, server_provider.clone());
let database_manager2 = Database2DepsResolver::resolve(
user_session.clone(),
task_dispatcher.clone(),
@@ -170,7 +179,7 @@ impl AppFlowyCore {
(
user_session,
folder_manager,
- local_server,
+ server_provider,
database_manager2,
document_manager2,
)
@@ -181,9 +190,11 @@ impl AppFlowyCore {
database_manager: database_manager.clone(),
config: config.clone(),
};
+
let user_status_callback = UserStatusCallbackImpl {
listener: Arc::new(user_status_listener),
};
+
let cloned_user_session = user_session.clone();
runtime.block_on(async move {
cloned_user_session.clone().init(user_status_callback).await;
@@ -205,7 +216,7 @@ impl AppFlowyCore {
folder_manager,
database_manager,
event_dispatcher,
- local_server,
+ server_provider,
task_dispatcher,
}
}
@@ -215,18 +226,6 @@ impl AppFlowyCore {
}
}
-fn mk_local_server(server_config: &ClientServerConfiguration) -> Option<Arc<LocalServer>> {
- // let ws_addr = server_config.ws_addr();
- if cfg!(feature = "http_sync") {
- // let ws_conn = Arc::new(FlowyWebSocketConnect::new(ws_addr));
- None
- } else {
- let context = flowy_net::local_server::build_server(server_config);
- // let ws_conn = Arc::new(FlowyWebSocketConnect::from_local(ws_addr, local_ws));
- Some(Arc::new(context.local_server))
- }
-}
-
fn init_kv(root: &str) {
match KV::init(root) {
Ok(_) => {},
@@ -263,12 +262,10 @@ fn init_log(config: &AppFlowyCoreConfig) {
fn mk_user_session(
config: &AppFlowyCoreConfig,
- local_server: &Option<Arc<LocalServer>>,
- server_config: &ClientServerConfiguration,
+ user_cloud_service_provider: Arc<dyn UserCloudServiceProvider>,
) -> Arc<UserSession> {
let user_config = UserSessionConfig::new(&config.name, &config.storage_path);
- let cloud_service = UserDepsResolver::resolve(local_server, server_config);
- Arc::new(UserSession::new(user_config, cloud_service))
+ Arc::new(UserSession::new(user_config, user_cloud_service_provider))
}
struct UserStatusListener {
@@ -279,20 +276,23 @@ struct UserStatusListener {
}
impl UserStatusListener {
- async fn did_sign_in(&self, token: &str, user_id: i64) -> FlowyResult<()> {
- self.folder_manager.initialize(user_id).await?;
- self.database_manager.initialize(user_id, token).await?;
- // self
- // .ws_conn
- // .start(token.to_owned(), user_id.to_owned())
- // .await?;
+ async fn did_sign_in(&self, user_id: i64, workspace_id: &str) -> FlowyResult<()> {
+ self
+ .folder_manager
+ .initialize(user_id, workspace_id)
+ .await?;
+ self.database_manager.initialize(user_id).await?;
Ok(())
}
async fn did_sign_up(&self, user_profile: &UserProfile) -> FlowyResult<()> {
self
.folder_manager
- .initialize_with_new_user(user_profile.id, &user_profile.token)
+ .initialize_with_new_user(
+ user_profile.id,
+ &user_profile.token,
+ &user_profile.workspace_id,
+ )
.await?;
self
@@ -314,11 +314,11 @@ struct UserStatusCallbackImpl {
}
impl UserStatusCallback for UserStatusCallbackImpl {
- fn did_sign_in(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>> {
+ fn did_sign_in(&self, user_id: i64, workspace_id: &str) -> Fut<FlowyResult<()>> {
let listener = self.listener.clone();
- let token = token.to_owned();
let user_id = user_id.to_owned();
- to_fut(async move { listener.did_sign_in(&token, user_id).await })
+ let workspace_id = workspace_id.to_owned();
+ to_fut(async move { listener.did_sign_in(user_id, &workspace_id).await })
}
fn did_sign_up(&self, user_profile: &UserProfile) -> Fut<FlowyResult<()>> {
@@ -333,9 +333,4 @@ impl UserStatusCallback for UserStatusCallbackImpl {
let user_id = user_id.to_owned();
to_fut(async move { listener.did_expired(&token, user_id).await })
}
-
- fn will_migrated(&self, _token: &str, _old_user_id: &str, _user_id: i64) -> Fut<FlowyResult<()>> {
- // Read the folder data
- todo!()
- }
}
diff --git a/frontend/rust-lib/flowy-database2/src/entities/filter_entities/util.rs b/frontend/rust-lib/flowy-database2/src/entities/filter_entities/util.rs
index 989736867c..ab9ca7bd9b 100644
--- a/frontend/rust-lib/flowy-database2/src/entities/filter_entities/util.rs
+++ b/frontend/rust-lib/flowy-database2/src/entities/filter_entities/util.rs
@@ -1,3 +1,12 @@
+use std::convert::TryInto;
+use std::sync::Arc;
+
+use bytes::Bytes;
+use collab_database::fields::Field;
+
+use flowy_derive::ProtoBuf;
+use flowy_error::ErrorCode;
+
use crate::entities::parser::NotEmptyStr;
use crate::entities::{
CheckboxFilterPB, ChecklistFilterPB, DateFilterContentPB, DateFilterPB, FieldType,
@@ -5,12 +14,6 @@ use crate::entities::{
};
use crate::services::field::SelectOptionIds;
use crate::services::filter::{Filter, FilterType};
-use bytes::Bytes;
-use collab_database::fields::Field;
-use flowy_derive::ProtoBuf;
-use flowy_error::ErrorCode;
-use std::convert::TryInto;
-use std::sync::Arc;
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct FilterPB {
@@ -95,7 +98,7 @@ impl TryInto<DeleteFilterParams> for DeleteFilterPayloadPB {
.0;
let filter_id = NotEmptyStr::parse(self.filter_id)
- .map_err(|_| ErrorCode::UnexpectedEmptyString)?
+ .map_err(|_| ErrorCode::UnexpectedEmpty)?
.0;
let filter_type = FilterType {
diff --git a/frontend/rust-lib/flowy-database2/src/entities/sort_entities.rs b/frontend/rust-lib/flowy-database2/src/entities/sort_entities.rs
index e4479a6874..ceea1fa61b 100644
--- a/frontend/rust-lib/flowy-database2/src/entities/sort_entities.rs
+++ b/frontend/rust-lib/flowy-database2/src/entities/sort_entities.rs
@@ -178,7 +178,7 @@ impl TryInto<DeleteSortParams> for DeleteSortPayloadPB {
.0;
let sort_id = NotEmptyStr::parse(self.sort_id)
- .map_err(|_| ErrorCode::UnexpectedEmptyString)?
+ .map_err(|_| ErrorCode::UnexpectedEmpty)?
.0;
let sort_type = SortType {
diff --git a/frontend/rust-lib/flowy-database2/src/manager.rs b/frontend/rust-lib/flowy-database2/src/manager.rs
index 3b59434c50..97ad7c132c 100644
--- a/frontend/rust-lib/flowy-database2/src/manager.rs
+++ b/frontend/rust-lib/flowy-database2/src/manager.rs
@@ -19,7 +19,7 @@ use crate::services::database::{DatabaseEditor, MutexDatabase};
pub trait DatabaseUser2: Send + Sync {
fn user_id(&self) -> Result<i64, FlowyError>;
- fn token(&self) -> Result<String, FlowyError>;
+ fn token(&self) -> Result<Option<String>, FlowyError>;
fn collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
}
@@ -46,7 +46,7 @@ impl DatabaseManager2 {
}
}
- pub async fn initialize(&self, user_id: i64, _token: &str) -> FlowyResult<()> {
+ pub async fn initialize(&self, user_id: i64) -> FlowyResult<()> {
let db = self.user.collab_db()?;
*self.user_database.lock() = Some(InnerUserDatabase::new(
user_id,
@@ -58,8 +58,8 @@ impl DatabaseManager2 {
Ok(())
}
- pub async fn initialize_with_new_user(&self, user_id: i64, token: &str) -> FlowyResult<()> {
- self.initialize(user_id, token).await?;
+ pub async fn initialize_with_new_user(&self, user_id: i64, _token: &str) -> FlowyResult<()> {
+ self.initialize(user_id).await?;
Ok(())
}
diff --git a/frontend/rust-lib/flowy-database2/src/services/field/type_options/type_option_cell.rs b/frontend/rust-lib/flowy-database2/src/services/field/type_options/type_option_cell.rs
index a1d86891d8..bdc7974b5b 100644
--- a/frontend/rust-lib/flowy-database2/src/services/field/type_options/type_option_cell.rs
+++ b/frontend/rust-lib/flowy-database2/src/services/field/type_options/type_option_cell.rs
@@ -1,4 +1,3 @@
-use std::any::Any;
use std::cmp::Ordering;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
@@ -7,6 +6,7 @@ use collab_database::fields::{Field, TypeOptionData};
use collab_database::rows::{Cell, RowId};
use flowy_error::FlowyResult;
+use lib_infra::box_any::BoxAny;
use crate::entities::FieldType;
use crate::services::cell::{
@@ -486,41 +486,7 @@ fn get_type_option_transform_handler(
}
}
-pub struct BoxCellData(Box<dyn Any + Send + Sync + 'static>);
-
-impl BoxCellData {
- fn new<T>(value: T) -> Self
- where
- T: Send + Sync + 'static,
- {
- Self(Box::new(value))
- }
-
- fn unbox_or_default<T>(self) -> T
- where
- T: Default + 'static,
- {
- match self.0.downcast::<T>() {
- Ok(value) => *value,
- Err(_) => T::default(),
- }
- }
-
- pub(crate) fn unbox_or_none<T>(self) -> Option<T>
- where
- T: Default + 'static,
- {
- match self.0.downcast::<T>() {
- Ok(value) => Some(*value),
- Err(_) => None,
- }
- }
-
- #[allow(dead_code)]
- fn downcast_ref<T: 'static>(&self) -> Option<&T> {
- self.0.downcast_ref()
- }
-}
+pub type BoxCellData = BoxAny;
pub struct RowSingleCellData {
pub row_id: RowId,
diff --git a/frontend/rust-lib/flowy-document2/src/manager.rs b/frontend/rust-lib/flowy-document2/src/manager.rs
index 0ca89467d3..1024c7610d 100644
--- a/frontend/rust-lib/flowy-document2/src/manager.rs
+++ b/frontend/rust-lib/flowy-document2/src/manager.rs
@@ -15,7 +15,7 @@ use crate::{
pub trait DocumentUser: Send + Sync {
fn user_id(&self) -> Result<i64, FlowyError>;
- fn token(&self) -> Result<String, FlowyError>; // unused now.
+ fn token(&self) -> Result<Option<String>, FlowyError>; // unused now.
fn collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
}
diff --git a/frontend/rust-lib/flowy-document2/tests/document/util.rs b/frontend/rust-lib/flowy-document2/tests/document/util.rs
index f42277c0f3..3d59b50af5 100644
--- a/frontend/rust-lib/flowy-document2/tests/document/util.rs
+++ b/frontend/rust-lib/flowy-document2/tests/document/util.rs
@@ -24,8 +24,8 @@ impl DocumentUser for FakeUser {
Ok(1)
}
- fn token(&self) -> Result<String, flowy_error::FlowyError> {
- Ok("1".to_string())
+ fn token(&self) -> Result<Option<String>, flowy_error::FlowyError> {
+ Ok(None)
}
fn collab_db(&self) -> Result<std::sync::Arc<RocksCollabDB>, flowy_error::FlowyError> {
diff --git a/frontend/rust-lib/flowy-error/src/code.rs b/frontend/rust-lib/flowy-error/src/code.rs
index 4b7abde17a..d238b0d810 100644
--- a/frontend/rust-lib/flowy-error/src/code.rs
+++ b/frontend/rust-lib/flowy-error/src/code.rs
@@ -149,9 +149,6 @@ pub enum ErrorCode {
#[error("Invalid date time format")]
InvalidDateTimeFormat = 47,
- #[error("The input string is empty or contains invalid characters")]
- UnexpectedEmptyString = 48,
-
#[error("Invalid data")]
InvalidData = 49,
@@ -185,14 +182,23 @@ pub enum ErrorCode {
#[error("Http request error")]
HttpError = 59,
- #[error("Payload should not be empty")]
- UnexpectedEmptyPayload = 60,
+ #[error("The content should not be empty")]
+ UnexpectedEmpty = 60,
#[error("Only the date type can be used in calendar")]
UnexpectedCalendarFieldType = 61,
#[error("Document Data Invalid")]
DocumentDataInvalid = 62,
+
+ #[error("Unsupported auth type")]
+ UnsupportedAuthType = 63,
+
+ #[error("Invalid auth configuration")]
+ InvalidAuthConfig = 64,
+
+ #[error("Missing auth field")]
+ MissingAuthField = 65,
}
impl ErrorCode {
diff --git a/frontend/rust-lib/flowy-error/src/errors.rs b/frontend/rust-lib/flowy-error/src/errors.rs
index 46f078e2a8..de8ac4e36a 100644
--- a/frontend/rust-lib/flowy-error/src/errors.rs
+++ b/frontend/rust-lib/flowy-error/src/errors.rs
@@ -1,9 +1,12 @@
-use crate::code::ErrorCode;
-use anyhow::Result;
-use flowy_derive::ProtoBuf;
use std::fmt::Debug;
+
+use anyhow::Result;
use thiserror::Error;
+use flowy_derive::ProtoBuf;
+
+use crate::code::ErrorCode;
+
pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
#[derive(Debug, Default, Clone, ProtoBuf, Error)]
@@ -26,10 +29,10 @@ macro_rules! static_flowy_error {
}
impl FlowyError {
- pub fn new(code: ErrorCode, msg: &str) -> Self {
+ pub fn new<T: ToString>(code: ErrorCode, msg: T) -> Self {
Self {
code: code.value() as i32,
- msg: msg.to_owned(),
+ msg: msg.to_string(),
}
}
pub fn context<T: Debug>(mut self, error: T) -> Self {
@@ -80,7 +83,7 @@ impl FlowyError {
static_flowy_error!(out_of_bounds, ErrorCode::OutOfBounds);
static_flowy_error!(serde, ErrorCode::Serde);
static_flowy_error!(field_record_not_found, ErrorCode::FieldRecordNotFound);
- static_flowy_error!(payload_none, ErrorCode::UnexpectedEmptyPayload);
+ static_flowy_error!(payload_none, ErrorCode::UnexpectedEmpty);
static_flowy_error!(http, ErrorCode::HttpError);
static_flowy_error!(
unexpect_calendar_field_type,
@@ -115,3 +118,9 @@ impl std::convert::From<protobuf::ProtobufError> for FlowyError {
FlowyError::internal().context(e)
}
}
+
+impl From<anyhow::Error> for FlowyError {
+ fn from(e: anyhow::Error) -> Self {
+ FlowyError::internal().context(e)
+ }
+}
diff --git a/frontend/rust-lib/flowy-folder2/Cargo.toml b/frontend/rust-lib/flowy-folder2/Cargo.toml
index 4ec7c71791..5c010052a0 100644
--- a/frontend/rust-lib/flowy-folder2/Cargo.toml
+++ b/frontend/rust-lib/flowy-folder2/Cargo.toml
@@ -26,6 +26,7 @@ chrono = { version = "0.4.24"}
strum = "0.21"
strum_macros = "0.21"
protobuf = {version = "2.28.0"}
+uuid = { version = "1.3.3", features = ["v4"] }
#flowy-document = { path = "../flowy-document" }
[dev-dependencies]
diff --git a/frontend/rust-lib/flowy-folder2/src/manager.rs b/frontend/rust-lib/flowy-folder2/src/manager.rs
index c3a4a01e64..253168d2af 100644
--- a/frontend/rust-lib/flowy-folder2/src/manager.rs
+++ b/frontend/rust-lib/flowy-folder2/src/manager.rs
@@ -4,7 +4,6 @@ use std::sync::Arc;
use appflowy_integrate::collab_builder::AppFlowyCollabBuilder;
use appflowy_integrate::RocksCollabDB;
-
use collab_folder::core::{
Folder as InnerFolder, FolderContext, TrashChange, TrashChangeReceiver, TrashInfo, TrashRecord,
View, ViewChange, ViewChangeReceiver, ViewLayout, Workspace,
@@ -30,7 +29,7 @@ use crate::view_ext::{
pub trait FolderUser: Send + Sync {
fn user_id(&self) -> Result<i64, FlowyError>;
- fn token(&self) -> Result<String, FlowyError>;
+ fn token(&self) -> Result<Option<String>, FlowyError>;
fn collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
}
@@ -92,32 +91,37 @@ impl Folder2Manager {
/// Called immediately after the application launched fi the user already sign in/sign up.
#[tracing::instrument(level = "trace", skip(self), err)]
- pub async fn initialize(&self, user_id: i64) -> FlowyResult<()> {
- if let Ok(uid) = self.user.user_id() {
- let folder_id = FolderId::new(uid);
-
- if let Ok(kv_db) = self.user.collab_db() {
- let collab = self.collab_builder.build(uid, folder_id.as_ref(), kv_db);
- let (view_tx, view_rx) = tokio::sync::broadcast::channel(100);
- let (trash_tx, trash_rx) = tokio::sync::broadcast::channel(100);
- let folder_context = FolderContext {
- view_change_tx: Some(view_tx),
- trash_change_tx: Some(trash_tx),
- };
- *self.folder.lock() = Some(InnerFolder::get_or_create(collab, folder_context));
- listen_on_trash_change(trash_rx, self.folder.clone());
- listen_on_view_change(view_rx, self.folder.clone());
- }
+ pub async fn initialize(&self, uid: i64, workspace_id: &str) -> FlowyResult<()> {
+ if let Ok(collab_db) = self.user.collab_db() {
+ let collab = self.collab_builder.build(uid, workspace_id, collab_db);
+ let (view_tx, view_rx) = tokio::sync::broadcast::channel(100);
+ let (trash_tx, trash_rx) = tokio::sync::broadcast::channel(100);
+ let folder_context = FolderContext {
+ view_change_tx: Some(view_tx),
+ trash_change_tx: Some(trash_tx),
+ };
+ *self.folder.lock() = Some(InnerFolder::get_or_create(collab, folder_context));
+ listen_on_trash_change(trash_rx, self.folder.clone());
+ listen_on_view_change(view_rx, self.folder.clone());
}
Ok(())
}
/// Called after the user sign up / sign in
- pub async fn initialize_with_new_user(&self, user_id: i64, token: &str) -> FlowyResult<()> {
- self.initialize(user_id).await?;
- let (folder_data, workspace_pb) =
- DefaultFolderBuilder::build(self.user.user_id()?, &self.view_processors).await;
+ pub async fn initialize_with_new_user(
+ &self,
+ user_id: i64,
+ token: &str,
+ workspace_id: &str,
+ ) -> FlowyResult<()> {
+ self.initialize(user_id, workspace_id).await?;
+ let (folder_data, workspace_pb) = DefaultFolderBuilder::build(
+ self.user.user_id()?,
+ workspace_id.to_string(),
+ &self.view_processors,
+ )
+ .await;
self.with_folder((), |folder| {
folder.create_with_data(folder_data);
});
@@ -555,7 +559,7 @@ fn notify_parent_view_did_change<T: AsRef<str>>(
for parent_view_id in parent_view_ids {
let parent_view_id = parent_view_id.as_ref();
- // if the view's bid is equal to workspace id. Then it will fetch the current
+ // if the view's parent id equal to workspace id. Then it will fetch the current
// workspace views. Because the the workspace is not a view stored in the views map.
if parent_view_id == workspace_id {
let repeated_view: RepeatedViewPB = get_workspace_view_pbs(&workspace_id, folder).into();
@@ -585,19 +589,6 @@ fn folder_not_init_error() -> FlowyError {
FlowyError::internal().context("Folder not initialized")
}
-#[derive(Clone)]
-pub struct FolderId(String);
-impl FolderId {
- pub fn new(uid: i64) -> Self {
- Self(format!("{}:folder", uid))
- }
-}
-
-impl AsRef<str> for FolderId {
- fn as_ref(&self) -> &str {
- &self.0
- }
-}
#[derive(Clone, Default)]
pub struct Folder(Arc<Mutex<Option<InnerFolder>>>);
diff --git a/frontend/rust-lib/flowy-folder2/src/user_default.rs b/frontend/rust-lib/flowy-folder2/src/user_default.rs
index 9e1d014907..8681d68297 100644
--- a/frontend/rust-lib/flowy-folder2/src/user_default.rs
+++ b/frontend/rust-lib/flowy-folder2/src/user_default.rs
@@ -11,10 +11,10 @@ pub struct DefaultFolderBuilder();
impl DefaultFolderBuilder {
pub async fn build(
uid: i64,
+ workspace_id: String,
view_processors: &ViewDataProcessorMap,
) -> (FolderData, WorkspacePB) {
let time = Utc::now().timestamp();
- let workspace_id = gen_workspace_id();
let view_id = gen_view_id();
let child_view_id = gen_view_id();
diff --git a/frontend/rust-lib/flowy-folder2/src/view_ext.rs b/frontend/rust-lib/flowy-folder2/src/view_ext.rs
index c8de262cf3..337f4211aa 100644
--- a/frontend/rust-lib/flowy-folder2/src/view_ext.rs
+++ b/frontend/rust-lib/flowy-folder2/src/view_ext.rs
@@ -4,7 +4,7 @@ use collab_folder::core::{View, ViewLayout};
use flowy_error::FlowyError;
use lib_infra::future::FutureResult;
use lib_infra::util::timestamp;
-use nanoid::nanoid;
+
use std::collections::HashMap;
use std::sync::Arc;
@@ -68,5 +68,5 @@ pub fn view_from_create_view_params(params: CreateViewParams, layout: ViewLayout
}
}
pub fn gen_view_id() -> String {
- format!("v:{}", nanoid!(10))
+ uuid::Uuid::new_v4().to_string()
}
diff --git a/frontend/rust-lib/flowy-net/Cargo.toml b/frontend/rust-lib/flowy-net/Cargo.toml
index 59aad0e8ea..23e52ec7bb 100644
--- a/frontend/rust-lib/flowy-net/Cargo.toml
+++ b/frontend/rust-lib/flowy-net/Cargo.toml
@@ -9,44 +9,22 @@ edition = "2018"
lib-dispatch = { path = "../lib-dispatch" }
flowy-error = { path = "../flowy-error", features = ["adaptor_reqwest", "adaptor_server_error"] }
flowy-derive = { path = "../../../shared-lib/flowy-derive" }
-flowy-folder2 = { path = "../flowy-folder2" }
-flowy-document2 = { path = "../flowy-document2" }
-flowy-user = { path = "../flowy-user" }
-#flowy-document = { path = "../flowy-document" }
-lazy_static = "1.4.0"
-lib-infra = { path = "../../../shared-lib/lib-infra" }
protobuf = {version = "2.28.0"}
-lib-ws = { path = "../../../shared-lib/lib-ws" }
-bytes = { version = "1.4" }
anyhow = "1.0"
-tokio = { version = "1.26", features = ["sync"]}
-parking_lot = "0.12.1"
-strum = "0.21"
-strum_macros = "0.21"
-tracing = { version = "0.1", features = ["log"] }
-dashmap = "5"
-async-stream = "0.3.4"
-futures-util = "0.3.26"
-reqwest = "0.11.14"
-hyper = "0.14"
-config = { version = "0.10.1", default-features = false, features = ["yaml"] }
-serde = { version = "1.0", features = ["derive"] }
-serde_json = "1.0"
-serde-aux = "1.1.0"
-nanoid = "0.4.0"
thiserror = "1.0"
+bytes = { version = "1.4" }
+strum_macros = "0.21"
+tracing = { version = "0.1"}
[features]
http_server = []
dart = [
"flowy-codegen/dart",
- "flowy-user/dart",
"flowy-error/dart",
]
ts = [
"flowy-codegen/ts",
- "flowy-user/ts",
"flowy-error/ts",
]
diff --git a/frontend/rust-lib/flowy-net/src/http_server/mod.rs b/frontend/rust-lib/flowy-net/src/http_server/mod.rs
deleted file mode 100644
index d5e9de07ec..0000000000
--- a/frontend/rust-lib/flowy-net/src/http_server/mod.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub mod self_host;
diff --git a/frontend/rust-lib/flowy-net/src/http_server/self_host/mod.rs b/frontend/rust-lib/flowy-net/src/http_server/self_host/mod.rs
deleted file mode 100644
index 2cd9f5266c..0000000000
--- a/frontend/rust-lib/flowy-net/src/http_server/self_host/mod.rs
+++ /dev/null
@@ -1,2 +0,0 @@
-pub mod configuration;
-pub mod user;
diff --git a/frontend/rust-lib/flowy-net/src/http_server/self_host/user.rs b/frontend/rust-lib/flowy-net/src/http_server/self_host/user.rs
deleted file mode 100644
index a8151d1871..0000000000
--- a/frontend/rust-lib/flowy-net/src/http_server/self_host/user.rs
+++ /dev/null
@@ -1,134 +0,0 @@
-use flowy_error::FlowyError;
-use flowy_user::entities::{
- SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams,
- UserProfilePB,
-};
-use flowy_user::event_map::UserCloudService;
-use lib_infra::future::FutureResult;
-
-use crate::http_server::self_host::configuration::{ClientServerConfiguration, HEADER_TOKEN};
-use crate::request::HttpRequestBuilder;
-
-pub struct UserHttpCloudService {
- config: ClientServerConfiguration,
-}
-impl UserHttpCloudService {
- pub fn new(config: &ClientServerConfiguration) -> Self {
- Self {
- config: config.clone(),
- }
- }
-}
-
-impl UserCloudService for UserHttpCloudService {
- fn sign_up(&self, params: SignUpParams) -> FutureResult<SignUpResponse, FlowyError> {
- let url = self.config.sign_up_url();
- FutureResult::new(async move {
- let resp = user_sign_up_request(params, &url).await?;
- Ok(resp)
- })
- }
-
- fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError> {
- let url = self.config.sign_in_url();
- FutureResult::new(async move {
- let resp = user_sign_in_request(params, &url).await?;
- Ok(resp)
- })
- }
-
- fn sign_out(&self, token: &str) -> FutureResult<(), FlowyError> {
- let token = token.to_owned();
- let url = self.config.sign_out_url();
- FutureResult::new(async move {
- let _ = user_sign_out_request(&token, &url).await;
- Ok(())
- })
- }
-
- fn update_user(
- &self,
- token: &str,
- params: UpdateUserProfileParams,
- ) -> FutureResult<(), FlowyError> {
- let token = token.to_owned();
- let url = self.config.user_profile_url();
- FutureResult::new(async move {
- update_user_profile_request(&token, params, &url).await?;
- Ok(())
- })
- }
-
- fn get_user(&self, token: &str) -> FutureResult<UserProfilePB, FlowyError> {
- let token = token.to_owned();
- let url = self.config.user_profile_url();
- FutureResult::new(async move {
- let profile = get_user_profile_request(&token, &url).await?;
- Ok(profile)
- })
- }
-
- fn ws_addr(&self) -> String {
- self.config.ws_addr()
- }
-}
-
-pub async fn user_sign_up_request(
- params: SignUpParams,
- url: &str,
-) -> Result<SignUpResponse, FlowyError> {
- let response = request_builder()
- .post(url)
- .json(params)?
- .json_response()
- .await?;
- Ok(response)
-}
-
-pub async fn user_sign_in_request(
- params: SignInParams,
- url: &str,
-) -> Result<SignInResponse, FlowyError> {
- let response = request_builder()
- .post(url)
- .json(params)?
- .json_response()
- .await?;
- Ok(response)
-}
-
-pub async fn user_sign_out_request(token: &str, url: &str) -> Result<(), FlowyError> {
- request_builder()
- .delete(url)
- .header(HEADER_TOKEN, token)
- .send()
- .await?;
- Ok(())
-}
-
-pub async fn get_user_profile_request(token: &str, url: &str) -> Result<UserProfilePB, FlowyError> {
- let user_profile = request_builder()
- .get(url)
- .header(HEADER_TOKEN, token)
- .response()
- .await?;
- Ok(user_profile)
-}
-
-pub async fn update_user_profile_request(
- token: &str,
- params: UpdateUserProfileParams,
- url: &str,
-) -> Result<(), FlowyError> {
- request_builder()
- .patch(url)
- .header(HEADER_TOKEN, token)
- .json(params)?
- .send()
- .await?;
- Ok(())
-}
-
-fn request_builder() -> HttpRequestBuilder {
- HttpRequestBuilder::new()
-}
diff --git a/frontend/rust-lib/flowy-net/src/lib.rs b/frontend/rust-lib/flowy-net/src/lib.rs
index d0f148701f..9e3de4ba43 100644
--- a/frontend/rust-lib/flowy-net/src/lib.rs
+++ b/frontend/rust-lib/flowy-net/src/lib.rs
@@ -1,8 +1,4 @@
pub mod entities;
pub mod event_map;
mod handlers;
-pub mod http_server;
-pub mod local_server;
pub mod protobuf;
-mod request;
-mod response;
diff --git a/frontend/rust-lib/flowy-net/src/local_server/context.rs b/frontend/rust-lib/flowy-net/src/local_server/context.rs
deleted file mode 100644
index e564f37561..0000000000
--- a/frontend/rust-lib/flowy-net/src/local_server/context.rs
+++ /dev/null
@@ -1,11 +0,0 @@
-use crate::http_server::self_host::configuration::ClientServerConfiguration;
-use crate::local_server::LocalServer;
-
-pub struct LocalServerContext {
- pub local_server: LocalServer,
-}
-
-pub fn build_server(_config: &ClientServerConfiguration) -> LocalServerContext {
- let local_server = LocalServer::new();
- LocalServerContext { local_server }
-}
diff --git a/frontend/rust-lib/flowy-net/src/local_server/mod.rs b/frontend/rust-lib/flowy-net/src/local_server/mod.rs
deleted file mode 100644
index ef14ac204a..0000000000
--- a/frontend/rust-lib/flowy-net/src/local_server/mod.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-pub use context::*;
-pub use server::*;
-
-mod context;
-mod server;
diff --git a/frontend/rust-lib/flowy-net/src/local_server/server.rs b/frontend/rust-lib/flowy-net/src/local_server/server.rs
deleted file mode 100644
index 528155a5cf..0000000000
--- a/frontend/rust-lib/flowy-net/src/local_server/server.rs
+++ /dev/null
@@ -1,80 +0,0 @@
-use lazy_static::lazy_static;
-use parking_lot::{Mutex, RwLock};
-use tokio::sync::mpsc;
-
-use flowy_error::FlowyError;
-use flowy_user::entities::{
- SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams,
- UserProfilePB,
-};
-use flowy_user::event_map::UserCloudService;
-use flowy_user::uid::UserIDGenerator;
-use lib_infra::future::FutureResult;
-
-lazy_static! {
- static ref ID_GEN: Mutex<UserIDGenerator> = Mutex::new(UserIDGenerator::new(1));
-}
-
-#[derive(Default)]
-pub struct LocalServer {
- stop_tx: RwLock<Option<mpsc::Sender<()>>>,
-}
-
-impl LocalServer {
- pub fn new() -> Self {
- Self::default()
- }
-
- pub async fn stop(&self) {
- let sender = self.stop_tx.read().clone();
- if let Some(stop_tx) = sender {
- let _ = stop_tx.send(()).await;
- }
- }
-}
-
-impl UserCloudService for LocalServer {
- fn sign_up(&self, params: SignUpParams) -> FutureResult<SignUpResponse, FlowyError> {
- let uid = ID_GEN.lock().next_id();
- FutureResult::new(async move {
- Ok(SignUpResponse {
- user_id: uid,
- name: params.name,
- email: params.email,
- token: "".to_string(),
- })
- })
- }
-
- fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError> {
- let uid = ID_GEN.lock().next_id();
- FutureResult::new(async move {
- Ok(SignInResponse {
- user_id: uid,
- name: params.name,
- email: params.email,
- token: "".to_string(),
- })
- })
- }
-
- fn sign_out(&self, _token: &str) -> FutureResult<(), FlowyError> {
- FutureResult::new(async { Ok(()) })
- }
-
- fn update_user(
- &self,
- _token: &str,
- _params: UpdateUserProfileParams,
- ) -> FutureResult<(), FlowyError> {
- FutureResult::new(async { Ok(()) })
- }
-
- fn get_user(&self, _token: &str) -> FutureResult<UserProfilePB, FlowyError> {
- FutureResult::new(async { Ok(UserProfilePB::default()) })
- }
-
- fn ws_addr(&self) -> String {
- "ws://localhost:8000/ws/".to_owned()
- }
-}
diff --git a/frontend/rust-lib/flowy-server/Cargo.toml b/frontend/rust-lib/flowy-server/Cargo.toml
new file mode 100644
index 0000000000..86de966116
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/Cargo.toml
@@ -0,0 +1,35 @@
+[package]
+name = "flowy-server"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+tracing = { version = "0.1" }
+futures-util = "0.3.26"
+reqwest = "0.11.14"
+hyper = "0.14"
+config = { version = "0.10.1", default-features = false, features = ["yaml"] }
+serde = { version = "1.0", features = ["derive"] }
+serde_json = "1.0"
+serde-aux = "4.2.0"
+nanoid = "0.4.0"
+thiserror = "1.0"
+tokio = { version = "1.26", features = ["sync"]}
+parking_lot = "0.12"
+lazy_static = "1.4.0"
+bytes = "1.0.1"
+postgrest = "1.0"
+tokio-retry = "0.3"
+anyhow = "1.0"
+uuid = { version = "1.3.3", features = ["v4"] }
+
+lib-infra = { path = "../../../shared-lib/lib-infra" }
+flowy-user = { path = "../flowy-user" }
+flowy-error = { path = "../flowy-error" }
+flowy-config = { path = "../flowy-config" }
+
+[dev-dependencies]
+uuid = { version = "1.3.3", features = ["v4"] }
+dotenv = "0.15.0"
diff --git a/frontend/rust-lib/flowy-server/src/lib.rs b/frontend/rust-lib/flowy-server/src/lib.rs
new file mode 100644
index 0000000000..a6df90122c
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/lib.rs
@@ -0,0 +1,13 @@
+use std::sync::Arc;
+
+use flowy_user::event_map::UserAuthService;
+
+pub mod local_server;
+mod request;
+mod response;
+pub mod self_host;
+pub mod supabase;
+
+pub trait AppFlowyServer: Send + Sync + 'static {
+ fn user_service(&self) -> Arc<dyn UserAuthService>;
+}
diff --git a/frontend/rust-lib/flowy-server/src/local_server/mod.rs b/frontend/rust-lib/flowy-server/src/local_server/mod.rs
new file mode 100644
index 0000000000..27e033264b
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/local_server/mod.rs
@@ -0,0 +1,5 @@
+pub use server::*;
+
+mod server;
+pub(crate) mod uid;
+mod user;
diff --git a/frontend/rust-lib/flowy-server/src/local_server/server.rs b/frontend/rust-lib/flowy-server/src/local_server/server.rs
new file mode 100644
index 0000000000..1074dae092
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/local_server/server.rs
@@ -0,0 +1,34 @@
+use std::sync::Arc;
+
+use parking_lot::RwLock;
+use tokio::sync::mpsc;
+
+use flowy_user::event_map::UserAuthService;
+
+use crate::local_server::user::LocalServerUserAuthServiceImpl;
+use crate::AppFlowyServer;
+
+#[derive(Default)]
+pub struct LocalServer {
+ stop_tx: RwLock<Option<mpsc::Sender<()>>>,
+}
+
+impl LocalServer {
+ pub fn new() -> Self {
+ // let _config = self_host_server_configuration().unwrap();
+ Self::default()
+ }
+
+ pub async fn stop(&self) {
+ let sender = self.stop_tx.read().clone();
+ if let Some(stop_tx) = sender {
+ let _ = stop_tx.send(()).await;
+ }
+ }
+}
+
+impl AppFlowyServer for LocalServer {
+ fn user_service(&self) -> Arc<dyn UserAuthService> {
+ Arc::new(LocalServerUserAuthServiceImpl())
+ }
+}
diff --git a/frontend/rust-lib/flowy-user/src/uid.rs b/frontend/rust-lib/flowy-server/src/local_server/uid.rs
similarity index 100%
rename from frontend/rust-lib/flowy-user/src/uid.rs
rename to frontend/rust-lib/flowy-server/src/local_server/uid.rs
diff --git a/frontend/rust-lib/flowy-server/src/local_server/user.rs b/frontend/rust-lib/flowy-server/src/local_server/user.rs
new file mode 100644
index 0000000000..95077bbcc5
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/local_server/user.rs
@@ -0,0 +1,71 @@
+use lazy_static::lazy_static;
+use parking_lot::Mutex;
+
+use flowy_error::FlowyError;
+use flowy_user::entities::{
+ SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile,
+};
+use flowy_user::event_map::UserAuthService;
+use lib_infra::box_any::BoxAny;
+use lib_infra::future::FutureResult;
+
+use crate::local_server::uid::UserIDGenerator;
+
+lazy_static! {
+ static ref ID_GEN: Mutex<UserIDGenerator> = Mutex::new(UserIDGenerator::new(1));
+}
+
+pub(crate) struct LocalServerUserAuthServiceImpl();
+
+impl UserAuthService for LocalServerUserAuthServiceImpl {
+ fn sign_up(&self, params: BoxAny) -> FutureResult<SignUpResponse, FlowyError> {
+ FutureResult::new(async move {
+ let params = params.unbox_or_error::<SignUpParams>()?;
+ let uid = ID_GEN.lock().next_id();
+ let workspace_id = uuid::Uuid::new_v4().to_string();
+ Ok(SignUpResponse {
+ user_id: uid,
+ name: params.name,
+ workspace_id,
+ email: Some(params.email),
+ token: None,
+ })
+ })
+ }
+
+ fn sign_in(&self, params: BoxAny) -> FutureResult<SignInResponse, FlowyError> {
+ FutureResult::new(async move {
+ let uid = ID_GEN.lock().next_id();
+ let params = params.unbox_or_error::<SignInParams>()?;
+ let workspace_id = uuid::Uuid::new_v4().to_string();
+ Ok(SignInResponse {
+ user_id: uid,
+ name: params.name,
+ workspace_id,
+ email: Some(params.email),
+ token: None,
+ })
+ })
+ }
+
+ fn sign_out(&self, _token: Option<String>) -> FutureResult<(), FlowyError> {
+ FutureResult::new(async { Ok(()) })
+ }
+
+ fn update_user(
+ &self,
+ _uid: i64,
+ _token: &Option<String>,
+ _params: UpdateUserProfileParams,
+ ) -> FutureResult<(), FlowyError> {
+ FutureResult::new(async { Ok(()) })
+ }
+
+ fn get_user_profile(
+ &self,
+ _token: Option<String>,
+ _uid: i64,
+ ) -> FutureResult<Option<UserProfile>, FlowyError> {
+ FutureResult::new(async { Ok(None) })
+ }
+}
diff --git a/frontend/rust-lib/flowy-net/src/request.rs b/frontend/rust-lib/flowy-server/src/request.rs
similarity index 67%
rename from frontend/rust-lib/flowy-net/src/request.rs
rename to frontend/rust-lib/flowy-server/src/request.rs
index 8facdf9d65..30a05d187f 100644
--- a/frontend/rust-lib/flowy-net/src/request.rs
+++ b/frontend/rust-lib/flowy-server/src/request.rs
@@ -1,19 +1,14 @@
-use std::{
- convert::{TryFrom, TryInto},
- sync::Arc,
- time::Duration,
-};
+use std::{sync::Arc, time::Duration};
use bytes::Bytes;
use hyper::http;
-use protobuf::ProtobufError;
use reqwest::{header::HeaderMap, Client, Method, Response};
use tokio::sync::oneshot;
-use flowy_error::FlowyError;
+use flowy_error::{internal_error, FlowyError};
-use crate::http_server::self_host::configuration::HEADER_TOKEN;
use crate::response::HttpResponse;
+use crate::self_host::configuration::HEADER_TOKEN;
pub trait ResponseMiddleware {
fn receive_response(&self, token: &Option<String>, response: &HttpResponse);
@@ -84,20 +79,11 @@ impl HttpRequestBuilder {
self
}
- #[allow(dead_code)]
- pub fn protobuf<T>(self, body: T) -> Result<Self, FlowyError>
- where
- T: TryInto<Bytes, Error = ProtobufError>,
- {
- let body: Bytes = body.try_into()?;
- self.bytes(body)
- }
-
pub fn json<T>(self, body: T) -> Result<Self, FlowyError>
where
T: serde::Serialize,
{
- let bytes = Bytes::from(serde_json::to_vec(&body)?);
+ let bytes = Bytes::from(serde_json::to_vec(&body).map_err(internal_error)?);
self.bytes(bytes)
}
@@ -112,60 +98,13 @@ impl HttpRequestBuilder {
}
pub async fn response<T>(self) -> Result<T, FlowyError>
- where
- T: TryFrom<Bytes, Error = ProtobufError>,
- {
- let builder = self.inner_send().await?;
- match builder.response {
- None => Err(unexpected_empty_payload(&builder.url)),
- Some(data) => Ok(T::try_from(data)?),
- }
- }
-
- pub async fn json_response<T>(self) -> Result<T, FlowyError>
where
T: serde::de::DeserializeOwned,
{
let builder = self.inner_send().await?;
match builder.response {
None => Err(unexpected_empty_payload(&builder.url)),
- Some(data) => Ok(serde_json::from_slice(&data)?),
- }
- }
-
- #[allow(dead_code)]
- pub async fn option_protobuf_response<T>(self) -> Result<Option<T>, FlowyError>
- where
- T: TryFrom<Bytes, Error = ProtobufError>,
- {
- let result = self.inner_send().await;
- match result {
- Ok(builder) => match builder.response {
- None => Err(unexpected_empty_payload(&builder.url)),
- Some(data) => Ok(Some(T::try_from(data)?)),
- },
- Err(error) => match error.is_record_not_found() {
- true => Ok(None),
- false => Err(error),
- },
- }
- }
-
- #[allow(dead_code)]
- pub async fn option_json_response<T>(self) -> Result<Option<T>, FlowyError>
- where
- T: serde::de::DeserializeOwned + 'static,
- {
- let result = self.inner_send().await;
- match result {
- Ok(builder) => match builder.response {
- None => Err(unexpected_empty_payload(&builder.url)),
- Some(data) => Ok(Some(serde_json::from_slice(&data)?)),
- },
- Err(error) => match error.is_record_not_found() {
- true => Ok(None),
- false => Err(error),
- },
+ Some(data) => serde_json::from_slice(&data).map_err(internal_error),
}
}
@@ -198,10 +137,7 @@ impl HttpRequestBuilder {
let _ = tx.send(response);
});
- let response = rx.await.map_err(|e| {
- let mag = format!("Receive http response channel error: {}", e);
- FlowyError::internal().context(mag)
- })??;
+ let response = rx.await.map_err(internal_error)?.map_err(internal_error)?;
tracing::trace!("Http Response: {:?}", response);
let flowy_response = flowy_response_from(response).await?;
let token = self.token();
@@ -224,16 +160,16 @@ fn unexpected_empty_payload(url: &str) -> FlowyError {
}
async fn flowy_response_from(original: Response) -> Result<HttpResponse, FlowyError> {
- let bytes = original.bytes().await?;
- let response: HttpResponse = serde_json::from_slice(&bytes)?;
+ let bytes = original.bytes().await.map_err(internal_error)?;
+ let response: HttpResponse = serde_json::from_slice(&bytes).map_err(internal_error)?;
Ok(response)
}
#[allow(dead_code)]
async fn get_response_data(original: Response) -> Result<Bytes, FlowyError> {
if original.status() == http::StatusCode::OK {
- let bytes = original.bytes().await?;
- let response: HttpResponse = serde_json::from_slice(&bytes)?;
+ let bytes = original.bytes().await.map_err(internal_error)?;
+ let response: HttpResponse = serde_json::from_slice(&bytes).map_err(internal_error)?;
match response.error {
None => Ok(response.data),
Some(error) => Err(FlowyError::new(error.code, &error.msg)),
diff --git a/frontend/rust-lib/flowy-net/src/response.rs b/frontend/rust-lib/flowy-server/src/response.rs
similarity index 100%
rename from frontend/rust-lib/flowy-net/src/response.rs
rename to frontend/rust-lib/flowy-server/src/response.rs
diff --git a/frontend/rust-lib/flowy-net/src/http_server/self_host/configuration.rs b/frontend/rust-lib/flowy-server/src/self_host/configuration.rs
similarity index 94%
rename from frontend/rust-lib/flowy-net/src/http_server/self_host/configuration.rs
rename to frontend/rust-lib/flowy-server/src/self_host/configuration.rs
index 54909825fd..5d2b89a8ab 100644
--- a/frontend/rust-lib/flowy-net/src/http_server/self_host/configuration.rs
+++ b/frontend/rust-lib/flowy-server/src/self_host/configuration.rs
@@ -6,7 +6,7 @@ use serde_aux::field_attributes::deserialize_number_from_string;
pub const HEADER_TOKEN: &str = "token";
#[derive(serde::Deserialize, Clone, Debug)]
-pub struct ClientServerConfiguration {
+pub struct SelfHostedConfiguration {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub host: String,
@@ -14,7 +14,7 @@ pub struct ClientServerConfiguration {
pub ws_scheme: String,
}
-pub fn get_client_server_configuration() -> Result<ClientServerConfiguration, config::ConfigError> {
+pub fn self_host_server_configuration() -> Result<SelfHostedConfiguration, config::ConfigError> {
let mut settings = config::Config::default();
let base = include_str!("./configuration/base.yaml");
settings.merge(config::File::from_str(base, FileFormat::Yaml).required(true))?;
@@ -33,7 +33,7 @@ pub fn get_client_server_configuration() -> Result<ClientServerConfiguration, co
settings.try_into()
}
-impl ClientServerConfiguration {
+impl SelfHostedConfiguration {
pub fn reset_host_with_port(&mut self, host: &str, port: u16) {
self.host = host.to_owned();
self.port = port;
diff --git a/frontend/rust-lib/flowy-net/src/http_server/self_host/configuration/base.yaml b/frontend/rust-lib/flowy-server/src/self_host/configuration/base.yaml
similarity index 100%
rename from frontend/rust-lib/flowy-net/src/http_server/self_host/configuration/base.yaml
rename to frontend/rust-lib/flowy-server/src/self_host/configuration/base.yaml
diff --git a/frontend/rust-lib/flowy-net/src/http_server/self_host/configuration/local.yaml b/frontend/rust-lib/flowy-server/src/self_host/configuration/local.yaml
similarity index 100%
rename from frontend/rust-lib/flowy-net/src/http_server/self_host/configuration/local.yaml
rename to frontend/rust-lib/flowy-server/src/self_host/configuration/local.yaml
diff --git a/frontend/rust-lib/flowy-net/src/http_server/self_host/configuration/production.yaml b/frontend/rust-lib/flowy-server/src/self_host/configuration/production.yaml
similarity index 100%
rename from frontend/rust-lib/flowy-net/src/http_server/self_host/configuration/production.yaml
rename to frontend/rust-lib/flowy-server/src/self_host/configuration/production.yaml
diff --git a/frontend/rust-lib/flowy-server/src/self_host/mod.rs b/frontend/rust-lib/flowy-server/src/self_host/mod.rs
new file mode 100644
index 0000000000..4247b51760
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/self_host/mod.rs
@@ -0,0 +1,6 @@
+pub use server::*;
+pub use user::*;
+
+pub mod configuration;
+mod server;
+mod user;
diff --git a/frontend/rust-lib/flowy-server/src/self_host/server.rs b/frontend/rust-lib/flowy-server/src/self_host/server.rs
new file mode 100644
index 0000000000..d10abbe600
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/self_host/server.rs
@@ -0,0 +1,23 @@
+use std::sync::Arc;
+
+use flowy_user::event_map::UserAuthService;
+
+use crate::self_host::configuration::SelfHostedConfiguration;
+use crate::self_host::SelfHostedUserAuthServiceImpl;
+use crate::AppFlowyServer;
+
+pub struct SelfHostServer {
+ pub(crate) config: SelfHostedConfiguration,
+}
+
+impl SelfHostServer {
+ pub fn new(config: SelfHostedConfiguration) -> Self {
+ Self { config }
+ }
+}
+
+impl AppFlowyServer for SelfHostServer {
+ fn user_service(&self) -> Arc<dyn UserAuthService> {
+ Arc::new(SelfHostedUserAuthServiceImpl::new(self.config.clone()))
+ }
+}
diff --git a/frontend/rust-lib/flowy-server/src/self_host/user.rs b/frontend/rust-lib/flowy-server/src/self_host/user.rs
new file mode 100644
index 0000000000..74148403b4
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/self_host/user.rs
@@ -0,0 +1,156 @@
+use flowy_error::{ErrorCode, FlowyError};
+use flowy_user::entities::{
+ SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile,
+};
+use flowy_user::event_map::UserAuthService;
+use lib_infra::box_any::BoxAny;
+use lib_infra::future::FutureResult;
+
+use crate::request::HttpRequestBuilder;
+use crate::self_host::configuration::{SelfHostedConfiguration, HEADER_TOKEN};
+
+pub(crate) struct SelfHostedUserAuthServiceImpl {
+ config: SelfHostedConfiguration,
+}
+
+impl SelfHostedUserAuthServiceImpl {
+ pub(crate) fn new(config: SelfHostedConfiguration) -> Self {
+ Self { config }
+ }
+}
+
+impl UserAuthService for SelfHostedUserAuthServiceImpl {
+ fn sign_up(&self, params: BoxAny) -> FutureResult<SignUpResponse, FlowyError> {
+ let url = self.config.sign_up_url();
+ FutureResult::new(async move {
+ let params = params.unbox_or_error::<SignUpParams>()?;
+ let resp = user_sign_up_request(params, &url).await?;
+ Ok(resp)
+ })
+ }
+
+ fn sign_in(&self, params: BoxAny) -> FutureResult<SignInResponse, FlowyError> {
+ let url = self.config.sign_in_url();
+ FutureResult::new(async move {
+ let params = params.unbox_or_error::<SignInParams>()?;
+ let resp = user_sign_in_request(params, &url).await?;
+ Ok(resp)
+ })
+ }
+
+ fn sign_out(&self, token: Option<String>) -> FutureResult<(), FlowyError> {
+ match token {
+ None => FutureResult::new(async {
+ Err(FlowyError::new(
+ ErrorCode::InvalidData,
+ "Token should not be empty",
+ ))
+ }),
+ Some(token) => {
+ let token = token;
+ let url = self.config.sign_out_url();
+ FutureResult::new(async move {
+ let _ = user_sign_out_request(&token, &url).await;
+ Ok(())
+ })
+ },
+ }
+ }
+
+ fn update_user(
+ &self,
+ _uid: i64,
+ token: &Option<String>,
+ params: UpdateUserProfileParams,
+ ) -> FutureResult<(), FlowyError> {
+ match token {
+ None => FutureResult::new(async {
+ Err(FlowyError::new(
+ ErrorCode::InvalidData,
+ "Token should not be empty",
+ ))
+ }),
+ Some(token) => {
+ let token = token.to_owned();
+ let url = self.config.user_profile_url();
+ FutureResult::new(async move {
+ update_user_profile_request(&token, params, &url).await?;
+ Ok(())
+ })
+ },
+ }
+ }
+
+ fn get_user_profile(
+ &self,
+ token: Option<String>,
+ _uid: i64,
+ ) -> FutureResult<Option<UserProfile>, FlowyError> {
+ let token = token;
+ let url = self.config.user_profile_url();
+ FutureResult::new(async move {
+ match token {
+ None => Err(FlowyError::new(
+ ErrorCode::UnexpectedEmpty,
+ "Token should not be empty",
+ )),
+ Some(token) => {
+ let profile = get_user_profile_request(&token, &url).await?;
+ Ok(Some(profile))
+ },
+ }
+ })
+ }
+}
+
+pub async fn user_sign_up_request(
+ params: SignUpParams,
+ url: &str,
+) -> Result<SignUpResponse, FlowyError> {
+ let response = request_builder().post(url).json(params)?.response().await?;
+ Ok(response)
+}
+
+pub async fn user_sign_in_request(
+ params: SignInParams,
+ url: &str,
+) -> Result<SignInResponse, FlowyError> {
+ let response = request_builder().post(url).json(params)?.response().await?;
+ Ok(response)
+}
+
+pub async fn user_sign_out_request(token: &str, url: &str) -> Result<(), FlowyError> {
+ request_builder()
+ .delete(url)
+ .header(HEADER_TOKEN, token)
+ .send()
+ .await?;
+ Ok(())
+}
+
+pub async fn get_user_profile_request(token: &str, url: &str) -> Result<UserProfile, FlowyError> {
+ let user_profile = request_builder()
+ .get(url)
+ .header(HEADER_TOKEN, token)
+ .response()
+ .await?;
+ Ok(user_profile)
+}
+
+pub async fn update_user_profile_request(
+ token: &str,
+ params: UpdateUserProfileParams,
+ url: &str,
+) -> Result<(), FlowyError> {
+ request_builder()
+ .patch(url)
+ .header(HEADER_TOKEN, token)
+ .json(params)?
+ .send()
+ .await?;
+ Ok(())
+}
+
+fn request_builder() -> HttpRequestBuilder {
+ HttpRequestBuilder::new()
+}
diff --git a/frontend/rust-lib/flowy-server/src/supabase/mod.rs b/frontend/rust-lib/flowy-server/src/supabase/mod.rs
new file mode 100644
index 0000000000..6870d3f721
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/supabase/mod.rs
@@ -0,0 +1,7 @@
+pub use server::*;
+
+mod request;
+mod response;
+mod retry;
+mod server;
+pub mod user;
diff --git a/frontend/rust-lib/flowy-server/src/supabase/request.rs b/frontend/rust-lib/flowy-server/src/supabase/request.rs
new file mode 100644
index 0000000000..5ff3a1747c
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/supabase/request.rs
@@ -0,0 +1,212 @@
+use std::collections::HashMap;
+use std::sync::Arc;
+
+use postgrest::Postgrest;
+use serde_json::json;
+
+use flowy_error::{ErrorCode, FlowyError};
+use flowy_user::entities::UpdateUserProfileParams;
+use lib_infra::box_any::BoxAny;
+
+use crate::supabase::response::{
+ InsertResponse, PostgrestError, UserProfile, UserProfileList, UserWorkspace, UserWorkspaceList,
+};
+use crate::supabase::user::{USER_PROFILE_TABLE, USER_TABLE, USER_WORKSPACE_TABLE};
+
+const USER_ID: &str = "uid";
+const USER_UUID: &str = "uuid";
+
+pub(crate) async fn create_user_with_uuid(
+ postgrest: Arc<Postgrest>,
+ uuid: String,
+) -> Result<UserWorkspace, FlowyError> {
+ let insert = format!("{{\"{}\": \"{}\"}}", USER_UUID, &uuid);
+
+ // Create a new user with uuid.
+ let resp = postgrest
+ .from(USER_TABLE)
+ .insert(insert)
+ .execute()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::HttpError, e))?;
+
+ // Check if the request is successful.
+ // If the request is successful, get the user id from the response. Otherwise, try to get the
+ // user id with uuid if the error is unique violation,
+ let is_success = resp.status().is_success();
+ let content = resp
+ .text()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::UnexpectedEmpty, e))?;
+
+ if is_success {
+ let record = serde_json::from_str::<InsertResponse>(&content)
+ .map_err(|e| FlowyError::serde().context(e))?
+ .first_or_error()?;
+
+ match get_user_workspace_with_uid(postgrest, record.uid).await {
+ Ok(Some(user)) => Ok(user),
+ _ => Err(FlowyError::new(
+ ErrorCode::Internal,
+ "Failed to get user workspace",
+ )),
+ }
+ } else {
+ let err = serde_json::from_str::<PostgrestError>(&content)
+ .map_err(|e| FlowyError::serde().context(e))?;
+
+ // If there is a unique violation, try to get the user id with uuid. At this point, the user
+ // should exist.
+ if err.is_unique_violation() {
+ match get_user_workspace_with_uuid(postgrest, uuid).await {
+ Ok(Some(user)) => Ok(user),
+ _ => Err(FlowyError::new(
+ ErrorCode::Internal,
+ "Failed to get user workspace",
+ )),
+ }
+ } else {
+ Err(FlowyError::new(ErrorCode::Internal, err))
+ }
+ }
+}
+
+#[allow(dead_code)]
+pub(crate) async fn get_user_id_with_uuid(
+ postgrest: Arc<Postgrest>,
+ uuid: String,
+) -> Result<Option<i64>, FlowyError> {
+ let resp = postgrest
+ .from(USER_TABLE)
+ .eq(USER_UUID, uuid)
+ .select("*")
+ .execute()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::HttpError, e))?;
+
+ let is_success = resp.status().is_success();
+ if !is_success {
+ return Err(FlowyError::new(
+ ErrorCode::Internal,
+ "Failed to get user id with uuid",
+ ));
+ }
+
+ let content = resp
+ .text()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::UnexpectedEmpty, e))?;
+ let resp = serde_json::from_str::<InsertResponse>(&content).unwrap();
+ if resp.0.is_empty() {
+ Ok(None)
+ } else {
+ Ok(Some(resp.0[0].uid))
+ }
+}
+
+pub(crate) fn uuid_from_box_any(any: BoxAny) -> Result<String, FlowyError> {
+ let map: HashMap<String, String> = any.unbox_or_error()?;
+ let uuid = map
+ .get(USER_UUID)
+ .ok_or_else(|| FlowyError::new(ErrorCode::MissingAuthField, "Missing uuid field"))?;
+ Ok(uuid.to_string())
+}
+
+#[allow(dead_code)]
+pub(crate) async fn get_user_profile(
+ postgrest: Arc<Postgrest>,
+ uid: i64,
+) -> Result<Option<UserProfile>, FlowyError> {
+ let resp = postgrest
+ .from(USER_PROFILE_TABLE)
+ .eq(USER_ID, uid.to_string())
+ .select("*")
+ .execute()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::HttpError, e))?;
+
+ let content = resp
+ .text()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::UnexpectedEmpty, e))?;
+ let resp = serde_json::from_str::<UserProfileList>(&content)
+ .map_err(|_e| FlowyError::new(ErrorCode::Serde, "Deserialize UserProfileList failed"))?;
+ Ok(resp.0.first().cloned())
+}
+
+pub(crate) async fn get_user_workspace_with_uuid(
+ postgrest: Arc<Postgrest>,
+ uuid: String,
+) -> Result<Option<UserWorkspace>, FlowyError> {
+ let resp = postgrest
+ .from(USER_WORKSPACE_TABLE)
+ .eq(USER_UUID, uuid)
+ .select("*")
+ .execute()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::HttpError, e))?;
+
+ let content = resp
+ .text()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::UnexpectedEmpty, e))?;
+ let resp = serde_json::from_str::<UserWorkspaceList>(&content)
+ .map_err(|_e| FlowyError::new(ErrorCode::Serde, "Deserialize UserWorkspaceList failed"))?;
+ Ok(resp.0.first().cloned())
+}
+
+pub(crate) async fn get_user_workspace_with_uid(
+ postgrest: Arc<Postgrest>,
+ uid: i64,
+) -> Result<Option<UserWorkspace>, FlowyError> {
+ let resp = postgrest
+ .from(USER_WORKSPACE_TABLE)
+ .eq(USER_ID, uid.to_string())
+ .select("*")
+ .execute()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::HttpError, e))?;
+
+ let content = resp
+ .text()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::UnexpectedEmpty, e))?;
+ let resp = serde_json::from_str::<UserWorkspaceList>(&content)
+ .map_err(|_e| FlowyError::new(ErrorCode::Serde, "Deserialize UserWorkspaceList failed"))?;
+ Ok(resp.0.first().cloned())
+}
+
+#[allow(dead_code)]
+pub(crate) async fn update_user_profile(
+ postgrest: Arc<Postgrest>,
+ params: UpdateUserProfileParams,
+) -> Result<Option<UserProfile>, FlowyError> {
+ if params.is_empty() {
+ return Err(FlowyError::new(
+ ErrorCode::UnexpectedEmpty,
+ "Empty update params",
+ ));
+ }
+
+ let mut update = serde_json::Map::new();
+ if let Some(name) = params.name {
+ update.insert("name".to_string(), json!(name));
+ }
+ let update_str = serde_json::to_string(&update).unwrap();
+ let resp = postgrest
+ .from(USER_PROFILE_TABLE)
+ .eq(USER_ID, params.id.to_string())
+ .update(update_str)
+ .execute()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::HttpError, e))?;
+
+ let content = resp
+ .text()
+ .await
+ .map_err(|e| FlowyError::new(ErrorCode::UnexpectedEmpty, e))?;
+
+ let resp = serde_json::from_str::<UserProfileList>(&content)
+ .map_err(|_e| FlowyError::new(ErrorCode::Serde, "Deserialize UserProfileList failed"))?;
+ Ok(resp.0.first().cloned())
+}
diff --git a/frontend/rust-lib/flowy-server/src/supabase/response.rs b/frontend/rust-lib/flowy-server/src/supabase/response.rs
new file mode 100644
index 0000000000..15cb821345
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/supabase/response.rs
@@ -0,0 +1,90 @@
+use serde::{Deserialize, Deserializer, Serialize};
+use serde_json::Value;
+use thiserror::Error;
+
+use flowy_error::{ErrorCode, FlowyError};
+
+#[derive(Debug, Error, Serialize, Deserialize)]
+#[error(
+ "PostgrestException(message: {message}, code: {code:?}, details: {details:?}, hint: {hint:?})"
+)]
+pub struct PostgrestError {
+ message: String,
+ code: String,
+ details: Value,
+ hint: Option<String>,
+}
+
+impl PostgrestError {
+ /// Error code 23505 is a PostgreSQL error code. It signifies a "unique_violation", which occurs
+ /// when a certain unique constraint has been violated.
+ pub fn is_unique_violation(&self) -> bool {
+ self.code == "23505"
+ }
+}
+
+#[allow(dead_code)]
+#[derive(Debug, Deserialize)]
+pub struct PostgrestResponse {
+ data: Option<String>,
+ status: i32,
+ count: Option<i32>,
+}
+
+#[derive(Debug, Deserialize)]
+pub(crate) struct InsertResponse(pub Vec<InsertRecord>);
+
+impl InsertResponse {
+ pub(crate) fn first_or_error(&self) -> Result<InsertRecord, FlowyError> {
+ if self.0.is_empty() {
+ Err(FlowyError::new(
+ ErrorCode::UnexpectedEmpty,
+ "Insert response contains no records",
+ ))
+ } else {
+ Ok(self.0[0].clone())
+ }
+ }
+}
+
+#[derive(Debug, Deserialize, Clone)]
+pub(crate) struct InsertRecord {
+ pub(crate) uid: i64,
+ #[allow(dead_code)]
+ pub(crate) uuid: String,
+}
+
+#[allow(dead_code)]
+#[derive(Debug, Deserialize, Clone)]
+pub(crate) struct UserProfile {
+ pub uid: i64,
+ #[serde(deserialize_with = "deserialize_null_or_default")]
+ pub name: String,
+ #[serde(deserialize_with = "deserialize_null_or_default")]
+ pub email: String,
+}
+
+#[derive(Debug, Deserialize)]
+pub(crate) struct UserProfileList(pub Vec<UserProfile>);
+
+#[derive(Debug, Deserialize, Clone)]
+pub(crate) struct UserWorkspace {
+ pub uid: i64,
+ #[serde(deserialize_with = "deserialize_null_or_default")]
+ pub name: String,
+ pub workspace_id: String,
+}
+
+#[derive(Debug, Deserialize)]
+pub(crate) struct UserWorkspaceList(pub Vec<UserWorkspace>);
+
+/// Handles the case where the value is null. If the value is null, return the default value of the
+/// type. Otherwise, deserialize the value.
+fn deserialize_null_or_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
+where
+ T: Default + Deserialize<'de>,
+ D: Deserializer<'de>,
+{
+ let opt = Option::deserialize(deserializer)?;
+ Ok(opt.unwrap_or_default())
+}
diff --git a/frontend/rust-lib/flowy-server/src/supabase/retry.rs b/frontend/rust-lib/flowy-server/src/supabase/retry.rs
new file mode 100644
index 0000000000..25a0598862
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/supabase/retry.rs
@@ -0,0 +1,11 @@
+// pub(crate) struct SupabaseRetryAction {}
+//
+// impl Action for SupabaseRetryAction {
+// type Future = ();
+// type Item = ();
+// type Error = ();
+//
+// fn run(&mut self) -> Self::Future {
+// todo!()
+// }
+// }
diff --git a/frontend/rust-lib/flowy-server/src/supabase/server.rs b/frontend/rust-lib/flowy-server/src/supabase/server.rs
new file mode 100644
index 0000000000..e4238ddd9a
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/supabase/server.rs
@@ -0,0 +1,56 @@
+use std::sync::Arc;
+
+use postgrest::Postgrest;
+
+use flowy_config::entities::{SUPABASE_JWT_SECRET, SUPABASE_KEY, SUPABASE_URL};
+use flowy_error::{ErrorCode, FlowyError};
+use flowy_user::event_map::UserAuthService;
+
+use crate::supabase::user::PostgrestUserAuthServiceImpl;
+use crate::AppFlowyServer;
+
+#[derive(Debug)]
+pub struct SupabaseConfiguration {
+ /// The url of the supabase server.
+ pub url: String,
+ /// The key of the supabase server.
+ pub key: String,
+ /// The secret used to sign the JWT tokens.
+ pub jwt_secret: String,
+}
+
+impl SupabaseConfiguration {
+ pub fn from_env() -> Result<Self, FlowyError> {
+ Ok(Self {
+ url: std::env::var(SUPABASE_URL)
+ .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_URL"))?,
+ key: std::env::var(SUPABASE_KEY)
+ .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_KEY"))?,
+ jwt_secret: std::env::var(SUPABASE_JWT_SECRET).map_err(|_| {
+ FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_JWT_SECRET")
+ })?,
+ })
+ }
+}
+
+pub struct SupabaseServer {
+ pub postgres: Arc<Postgrest>,
+}
+
+impl SupabaseServer {
+ pub fn new(config: SupabaseConfiguration) -> Self {
+ let url = format!("{}/rest/v1/", config.url);
+ let auth = format!("Bearer {}", config.key);
+ let postgrest = Postgrest::new(url)
+ .insert_header("apikey", config.key)
+ .insert_header("Authorization", auth);
+ let postgres = Arc::new(postgrest);
+ Self { postgres }
+ }
+}
+
+impl AppFlowyServer for SupabaseServer {
+ fn user_service(&self) -> Arc<dyn UserAuthService> {
+ Arc::new(PostgrestUserAuthServiceImpl::new(self.postgres.clone()))
+ }
+}
diff --git a/frontend/rust-lib/flowy-server/src/supabase/user.rs b/frontend/rust-lib/flowy-server/src/supabase/user.rs
new file mode 100644
index 0000000000..23c1b07018
--- /dev/null
+++ b/frontend/rust-lib/flowy-server/src/supabase/user.rs
@@ -0,0 +1,167 @@
+use std::sync::Arc;
+
+use postgrest::Postgrest;
+
+use flowy_error::FlowyError;
+use flowy_user::entities::{SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile};
+use flowy_user::event_map::UserAuthService;
+use lib_infra::box_any::BoxAny;
+use lib_infra::future::FutureResult;
+
+use crate::supabase::request::*;
+
+pub(crate) const USER_TABLE: &str = "af_user";
+pub(crate) const USER_PROFILE_TABLE: &str = "af_user_profile";
+pub(crate) const USER_WORKSPACE_TABLE: &str = "af_user_workspace_view";
+pub(crate) struct PostgrestUserAuthServiceImpl {
+ postgrest: Arc<Postgrest>,
+}
+
+impl PostgrestUserAuthServiceImpl {
+ pub(crate) fn new(postgrest: Arc<Postgrest>) -> Self {
+ Self { postgrest }
+ }
+}
+
+impl UserAuthService for PostgrestUserAuthServiceImpl {
+ fn sign_up(&self, params: BoxAny) -> FutureResult<SignUpResponse, FlowyError> {
+ let postgrest = self.postgrest.clone();
+ FutureResult::new(async move {
+ let uuid = uuid_from_box_any(params)?;
+ let user = create_user_with_uuid(postgrest, uuid).await?;
+ Ok(SignUpResponse {
+ user_id: user.uid,
+ workspace_id: user.workspace_id,
+ ..Default::default()
+ })
+ })
+ }
+
+ fn sign_in(&self, params: BoxAny) -> FutureResult<SignInResponse, FlowyError> {
+ let postgrest = self.postgrest.clone();
+ FutureResult::new(async move {
+ let uuid = uuid_from_box_any(params)?;
+ match get_user_workspace_with_uuid(postgrest, uuid).await? {
+ None => Err(FlowyError::user_not_exist()),
+ Some(user) => Ok(SignInResponse {
+ user_id: user.uid,
+ workspace_id: user.workspace_id,
+ ..Default::default()
+ }),
+ }
+ })
+ }
+
+ fn sign_out(&self, _token: Option<String>) -> FutureResult<(), FlowyError> {
+ FutureResult::new(async { Ok(()) })
+ }
+
+ fn update_user(
+ &self,
+ _uid: i64,
+ _token: &Option<String>,
+ params: UpdateUserProfileParams,
+ ) -> FutureResult<(), FlowyError> {
+ let postgrest = self.postgrest.clone();
+ FutureResult::new(async move {
+ let _ = update_user_profile(postgrest, params).await?;
+ Ok(())
+ })
+ }
+
+ fn get_user_profile(
+ &self,
+ _token: Option<String>,
+ uid: i64,
+ ) -> FutureResult<Option<UserProfile>, FlowyError> {
+ let postgrest = self.postgrest.clone();
+ FutureResult::new(async move {
+ let profile = get_user_workspace_with_uid(postgrest, uid)
+ .await?
+ .map(|user_workspace| UserProfile {
+ id: user_workspace.uid,
+ email: "".to_string(),
+ name: user_workspace.name,
+ token: "".to_string(),
+ icon_url: "".to_string(),
+ openai_key: "".to_string(),
+ workspace_id: user_workspace.workspace_id,
+ });
+ Ok(profile)
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::sync::Arc;
+
+ use dotenv::dotenv;
+
+ use flowy_user::entities::UpdateUserProfileParams;
+
+ use crate::supabase::request::{get_user_profile, get_user_workspace_with_uid};
+ use crate::supabase::user::{create_user_with_uuid, get_user_id_with_uuid, update_user_profile};
+ use crate::supabase::{SupabaseConfiguration, SupabaseServer};
+
+ #[tokio::test]
+ async fn read_user_table_test() {
+ dotenv().ok();
+ if let Ok(config) = SupabaseConfiguration::from_env() {
+ let server = Arc::new(SupabaseServer::new(config));
+ let uid = get_user_id_with_uuid(
+ server.postgres.clone(),
+ "c8c674fc-506f-403c-b052-209e09817f6e".to_string(),
+ )
+ .await
+ .unwrap();
+ println!("uid: {:?}", uid);
+ }
+ }
+
+ #[tokio::test]
+ async fn insert_user_table_test() {
+ dotenv().ok();
+ if let Ok(config) = SupabaseConfiguration::from_env() {
+ let server = Arc::new(SupabaseServer::new(config));
+ let uuid = uuid::Uuid::new_v4();
+ // let uuid = "c8c674fc-506f-403c-b052-209e09817f6e";
+ let uid = create_user_with_uuid(server.postgres.clone(), uuid.to_string()).await;
+ println!("uid: {:?}", uid);
+ }
+ }
+
+ #[tokio::test]
+ async fn create_and_then_update_user_profile_test() {
+ dotenv().ok();
+ if let Ok(config) = SupabaseConfiguration::from_env() {
+ let server = Arc::new(SupabaseServer::new(config));
+ let uuid = uuid::Uuid::new_v4();
+ let uid = create_user_with_uuid(server.postgres.clone(), uuid.to_string())
+ .await
+ .unwrap()
+ .uid;
+ let params = UpdateUserProfileParams {
+ id: uid,
+ name: Some("nathan".to_string()),
+ ..Default::default()
+ };
+ let result = update_user_profile(server.postgres.clone(), params)
+ .await
+ .unwrap();
+ println!("result: {:?}", result);
+
+ let result = get_user_profile(server.postgres.clone(), uid)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(result.name, "nathan".to_string());
+
+ let result = get_user_workspace_with_uid(server.postgres.clone(), uid)
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(!result.workspace_id.is_empty());
+ }
+ }
+}
diff --git a/frontend/rust-lib/flowy-test/Cargo.toml b/frontend/rust-lib/flowy-test/Cargo.toml
index 00a3e4a146..f9407dcb3a 100644
--- a/frontend/rust-lib/flowy-test/Cargo.toml
+++ b/frontend/rust-lib/flowy-test/Cargo.toml
@@ -14,6 +14,7 @@ flowy-folder2 = { path = "../flowy-folder2", features = ["test_helper"] }
lib-dispatch = { path = "../lib-dispatch" }
lib-ot = { path = "../../../shared-lib/lib-ot" }
lib-infra = { path = "../../../shared-lib/lib-infra" }
+flowy-server = { path = "../flowy-server" }
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0"}
diff --git a/frontend/rust-lib/flowy-test/src/helper.rs b/frontend/rust-lib/flowy-test/src/helper.rs
index 3024af529b..4a4566738b 100644
--- a/frontend/rust-lib/flowy-test/src/helper.rs
+++ b/frontend/rust-lib/flowy-test/src/helper.rs
@@ -1,8 +1,10 @@
-use crate::prelude::*;
+use std::sync::Arc;
+
use flowy_folder2::entities::{
CreateViewPayloadPB, CreateWorkspacePayloadPB, ViewLayoutPB, ViewPB, WorkspaceIdPB, WorkspacePB,
};
use flowy_folder2::event_map::FolderEvent::{CreateView, CreateWorkspace, OpenWorkspace};
+use flowy_user::entities::AuthTypePB;
use flowy_user::{
entities::{SignInPayloadPB, SignUpPayloadPB, UserProfilePB},
errors::FlowyError,
@@ -10,7 +12,7 @@ use flowy_user::{
};
use lib_dispatch::prelude::{AFPluginDispatcher, AFPluginRequest, ToBytes};
-use std::sync::Arc;
+use crate::prelude::*;
pub struct ViewTest {
pub sdk: FlowySDKTest,
@@ -142,6 +144,7 @@ pub fn sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
+ auth_type: AuthTypePB::Local,
}
.into_bytes()
.unwrap();
@@ -165,6 +168,7 @@ pub async fn async_sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
email,
name: "app flowy".to_string(),
password: password.clone(),
+ auth_type: AuthTypePB::Local,
}
.into_bytes()
.unwrap();
@@ -194,6 +198,7 @@ fn sign_in(dispatch: Arc<AFPluginDispatcher>) -> UserProfilePB {
email: login_email(),
password: login_password(),
name: "rust".to_owned(),
+ auth_type: AuthTypePB::Local,
}
.into_bytes()
.unwrap();
diff --git a/frontend/rust-lib/flowy-test/src/lib.rs b/frontend/rust-lib/flowy-test/src/lib.rs
index 28c1a4a16e..b023aa434e 100644
--- a/frontend/rust-lib/flowy-test/src/lib.rs
+++ b/frontend/rust-lib/flowy-test/src/lib.rs
@@ -2,7 +2,6 @@ use nanoid::nanoid;
use std::env::temp_dir;
use flowy_core::{AppFlowyCore, AppFlowyCoreConfig};
-use flowy_net::http_server::self_host::configuration::get_client_server_configuration;
use flowy_user::entities::UserProfilePB;
use crate::helper::*;
@@ -37,9 +36,8 @@ impl std::default::Default for FlowySDKTest {
impl FlowySDKTest {
pub fn new() -> Self {
- let server_config = get_client_server_configuration().unwrap();
- let config = AppFlowyCoreConfig::new(temp_dir().to_str().unwrap(), nanoid!(6), server_config)
- .log_filter("info", vec![]);
+ let config =
+ AppFlowyCoreConfig::new(temp_dir().to_str().unwrap(), nanoid!(6)).log_filter("info", vec![]);
let sdk = std::thread::spawn(|| AppFlowyCore::new(config))
.join()
.unwrap();
diff --git a/frontend/rust-lib/flowy-user/Cargo.toml b/frontend/rust-lib/flowy-user/Cargo.toml
index cbeef480cf..1a2dee82e8 100644
--- a/frontend/rust-lib/flowy-user/Cargo.toml
+++ b/frontend/rust-lib/flowy-user/Cargo.toml
@@ -18,6 +18,7 @@ tracing = { version = "0.1", features = ["log"] }
bytes = "1.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0"}
+serde_repr = "0.1"
log = "0.4.17"
protobuf = {version = "2.28.0"}
lazy_static = "1.4.0"
diff --git a/frontend/rust-lib/flowy-user/src/entities/auth.rs b/frontend/rust-lib/flowy-user/src/entities/auth.rs
index fd1f3b7dcb..d663d10dcc 100644
--- a/frontend/rust-lib/flowy-user/src/entities/auth.rs
+++ b/frontend/rust-lib/flowy-user/src/entities/auth.rs
@@ -1,11 +1,13 @@
+use std::collections::HashMap;
use std::convert::TryInto;
use serde::{Deserialize, Serialize};
-use flowy_derive::ProtoBuf;
+use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use crate::entities::parser::*;
use crate::errors::ErrorCode;
+use crate::services::AuthType;
#[derive(ProtoBuf, Default)]
pub struct SignInPayloadPB {
@@ -17,6 +19,9 @@ pub struct SignInPayloadPB {
#[pb(index = 3)]
pub name: String,
+
+ #[pb(index = 4)]
+ pub auth_type: AuthTypePB,
}
impl TryInto<SignInParams> for SignInPayloadPB {
@@ -30,6 +35,7 @@ impl TryInto<SignInParams> for SignInPayloadPB {
email: email.0,
password: password.0,
name: self.name,
+ auth_type: self.auth_type.into(),
})
}
}
@@ -44,6 +50,9 @@ pub struct SignUpPayloadPB {
#[pb(index = 3)]
pub password: String,
+
+ #[pb(index = 4)]
+ pub auth_type: AuthTypePB,
}
impl TryInto<SignUpParams> for SignUpPayloadPB {
type Error = ErrorCode;
@@ -57,6 +66,7 @@ impl TryInto<SignUpParams> for SignUpPayloadPB {
email: email.0,
name: name.0,
password: password.0,
+ auth_type: self.auth_type.into(),
})
}
}
@@ -66,14 +76,16 @@ pub struct SignInParams {
pub email: String,
pub password: String,
pub name: String,
+ pub auth_type: AuthType,
}
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct SignInResponse {
pub user_id: i64,
pub name: String,
- pub email: String,
- pub token: String,
+ pub workspace_id: String,
+ pub email: Option<String>,
+ pub token: Option<String>,
}
#[derive(Serialize, Deserialize, Default, Debug)]
@@ -81,14 +93,43 @@ pub struct SignUpParams {
pub email: String,
pub name: String,
pub password: String,
+ pub auth_type: AuthType,
}
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SignUpResponse {
pub user_id: i64,
pub name: String,
- pub email: String,
- pub token: String,
+ pub workspace_id: String,
+ pub email: Option<String>,
+ pub token: Option<String>,
+}
+
+#[derive(ProtoBuf, Default)]
+pub struct ThirdPartyAuthPB {
+ /// Use this field to store the third party auth information.
+ /// Different auth type has different fields.
+ /// Supabase:
+ /// - map: { "uuid": "xxx" }
+ ///
+ #[pb(index = 1)]
+ pub map: HashMap<String, String>,
+
+ #[pb(index = 2)]
+ pub auth_type: AuthTypePB,
+}
+
+#[derive(ProtoBuf_Enum, Debug, Clone)]
+pub enum AuthTypePB {
+ Local = 0,
+ SelfHosted = 1,
+ Supabase = 2,
+}
+
+impl Default for AuthTypePB {
+ fn default() -> Self {
+ Self::Local
+ }
}
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
@@ -99,11 +140,13 @@ pub struct UserProfile {
pub token: String,
pub icon_url: String,
pub openai_key: String,
+ pub workspace_id: String,
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct UpdateUserProfileParams {
pub id: i64,
+ pub auth_type: AuthType,
pub name: Option<String>,
pub email: Option<String>,
pub password: Option<String>,
@@ -112,17 +155,6 @@ pub struct UpdateUserProfileParams {
}
impl UpdateUserProfileParams {
- pub fn new(id: i64) -> Self {
- Self {
- id,
- name: None,
- email: None,
- password: None,
- icon_url: None,
- openai_key: None,
- }
- }
-
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
@@ -147,4 +179,18 @@ impl UpdateUserProfileParams {
self.openai_key = Some(openai_key.to_owned());
self
}
+
+ pub fn is_empty(&self) -> bool {
+ self.name.is_none()
+ && self.email.is_none()
+ && self.password.is_none()
+ && self.icon_url.is_none()
+ && self.openai_key.is_none()
+ }
+}
+
+#[derive(ProtoBuf, Default)]
+pub struct SignOutPB {
+ #[pb(index = 1)]
+ pub auth_type: AuthTypePB,
}
diff --git a/frontend/rust-lib/flowy-user/src/entities/user_profile.rs b/frontend/rust-lib/flowy-user/src/entities/user_profile.rs
index a535d2b026..acec2f0d8d 100644
--- a/frontend/rust-lib/flowy-user/src/entities/user_profile.rs
+++ b/frontend/rust-lib/flowy-user/src/entities/user_profile.rs
@@ -3,7 +3,7 @@ use std::convert::TryInto;
use flowy_derive::ProtoBuf;
use crate::entities::parser::{UserEmail, UserIcon, UserName, UserOpenaiKey, UserPassword};
-use crate::entities::{UpdateUserProfileParams, UserProfile};
+use crate::entities::{AuthTypePB, UpdateUserProfileParams, UserProfile};
use crate::errors::ErrorCode;
#[derive(Default, ProtoBuf)]
@@ -71,6 +71,9 @@ pub struct UpdateUserProfilePayloadPB {
#[pb(index = 6, one_of)]
pub openai_key: Option<String>,
+
+ #[pb(index = 7)]
+ pub auth_type: AuthTypePB,
}
impl UpdateUserProfilePayloadPB {
@@ -138,6 +141,7 @@ impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
Ok(UpdateUserProfileParams {
id: self.id,
+ auth_type: self.auth_type.into(),
name,
email,
password,
diff --git a/frontend/rust-lib/flowy-user/src/event_handler.rs b/frontend/rust-lib/flowy-user/src/event_handler.rs
index 6f1eb6ade2..02cb5f407b 100644
--- a/frontend/rust-lib/flowy-user/src/event_handler.rs
+++ b/frontend/rust-lib/flowy-user/src/event_handler.rs
@@ -3,19 +3,23 @@ use std::{convert::TryInto, sync::Arc};
use flowy_error::FlowyError;
use flowy_sqlite::kv::KV;
use lib_dispatch::prelude::*;
+use lib_infra::box_any::BoxAny;
use crate::entities::*;
use crate::entities::{SignInParams, SignUpParams, UpdateUserProfileParams};
-use crate::services::UserSession;
+use crate::services::{AuthType, UserSession};
-// tracing instrument 👉🏻 https://docs.rs/tracing/0.1.26/tracing/attr.instrument.html
#[tracing::instrument(level = "debug", name = "sign_in", skip(data, session), fields(email = %data.email), err)]
pub async fn sign_in(
data: AFPluginData<SignInPayloadPB>,
session: AFPluginState<Arc<UserSession>>,
) -> DataResult<UserProfilePB, FlowyError> {
let params: SignInParams = data.into_inner().try_into()?;
- let user_profile: UserProfilePB = session.sign_in(params).await?.into();
+ let auth_type = params.auth_type.clone();
+ let user_profile: UserProfilePB = session
+ .sign_in(&auth_type, BoxAny::new(params))
+ .await?
+ .into();
data_result_ok(user_profile)
}
@@ -34,8 +38,11 @@ pub async fn sign_up(
session: AFPluginState<Arc<UserSession>>,
) -> DataResult<UserProfilePB, FlowyError> {
let params: SignUpParams = data.into_inner().try_into()?;
- let user_profile: UserProfilePB = session.sign_up(params).await?.into();
-
+ let auth_type = params.auth_type.clone();
+ let user_profile: UserProfilePB = session
+ .sign_up(&auth_type, BoxAny::new(params))
+ .await?
+ .into();
data_result_ok(user_profile)
}
@@ -61,9 +68,13 @@ pub async fn get_user_profile_handler(
data_result_ok(user_profile)
}
-#[tracing::instrument(level = "debug", name = "sign_out", skip(session))]
-pub async fn sign_out(session: AFPluginState<Arc<UserSession>>) -> Result<(), FlowyError> {
- session.sign_out().await?;
+#[tracing::instrument(level = "debug", skip(data, session))]
+pub async fn sign_out(
+ data: AFPluginData<SignOutPB>,
+ session: AFPluginState<Arc<UserSession>>,
+) -> Result<(), FlowyError> {
+ let auth_type: AuthType = data.into_inner().auth_type.into();
+ session.sign_out(&auth_type).await?;
Ok(())
}
@@ -88,8 +99,7 @@ pub async fn set_appearance_setting(
setting.theme = APPEARANCE_DEFAULT_THEME.to_string();
}
- let s = serde_json::to_string(&setting)?;
- KV::set_str(APPEARANCE_SETTING_CACHE_KEY, s);
+ KV::set_object(APPEARANCE_SETTING_CACHE_KEY, setting)?;
Ok(())
}
@@ -120,3 +130,19 @@ pub async fn get_user_setting(
let user_setting = session.user_setting()?;
data_result_ok(user_setting)
}
+
+/// Only used for third party auth.
+/// Use [UserEvent::SignIn] or [UserEvent::SignUp] If the [AuthType] is Local or SelfHosted
+#[tracing::instrument(level = "debug", skip(data, session), err)]
+pub async fn third_party_auth_handler(
+ data: AFPluginData<ThirdPartyAuthPB>,
+ session: AFPluginState<Arc<UserSession>>,
+) -> DataResult<UserProfilePB, FlowyError> {
+ let params = data.into_inner();
+ let auth_type: AuthType = params.auth_type.into();
+ let user_profile: UserProfilePB = session
+ .sign_up(&auth_type, BoxAny::new(params.map))
+ .await?
+ .into();
+ data_result_ok(user_profile)
+}
diff --git a/frontend/rust-lib/flowy-user/src/event_map.rs b/frontend/rust-lib/flowy-user/src/event_map.rs
index 7862fc015e..b0342c9285 100644
--- a/frontend/rust-lib/flowy-user/src/event_map.rs
+++ b/frontend/rust-lib/flowy-user/src/event_map.rs
@@ -4,14 +4,14 @@ use strum_macros::Display;
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
use flowy_error::FlowyResult;
+
use lib_dispatch::prelude::*;
+use lib_infra::box_any::BoxAny;
use lib_infra::future::{Fut, FutureResult};
-use crate::entities::UserProfilePB;
-use crate::entities::{
- SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile,
-};
+use crate::entities::{SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile};
use crate::event_handler::*;
+use crate::services::AuthType;
use crate::{errors::FlowyError, services::UserSession};
pub fn init(user_session: Arc<UserSession>) -> AFPlugin {
@@ -28,41 +28,76 @@ pub fn init(user_session: Arc<UserSession>) -> AFPlugin {
.event(UserEvent::SetAppearanceSetting, set_appearance_setting)
.event(UserEvent::GetAppearanceSetting, get_appearance_setting)
.event(UserEvent::GetUserSetting, get_user_setting)
+ .event(UserEvent::ThirdPartyAuth, third_party_auth_handler)
}
pub trait UserStatusCallback: Send + Sync + 'static {
- fn did_sign_in(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
+ fn did_sign_in(&self, user_id: i64, workspace_id: &str) -> Fut<FlowyResult<()>>;
fn did_sign_up(&self, user_profile: &UserProfile) -> Fut<FlowyResult<()>>;
fn did_expired(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
- fn will_migrated(&self, token: &str, old_user_id: &str, user_id: i64) -> Fut<FlowyResult<()>>;
}
-pub trait UserCloudService: Send + Sync {
- fn sign_up(&self, params: SignUpParams) -> FutureResult<SignUpResponse, FlowyError>;
- fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError>;
- fn sign_out(&self, token: &str) -> FutureResult<(), FlowyError>;
+/// The user cloud service provider.
+/// The provider can be supabase, firebase, aws, or any other cloud service.
+pub trait UserCloudServiceProvider: Send + Sync + 'static {
+ fn get_auth_service(&self, auth_type: &AuthType) -> Result<Arc<dyn UserAuthService>, FlowyError>;
+}
+
+impl<T> UserCloudServiceProvider for Arc<T>
+where
+ T: UserCloudServiceProvider,
+{
+ fn get_auth_service(&self, auth_type: &AuthType) -> Result<Arc<dyn UserAuthService>, FlowyError> {
+ (**self).get_auth_service(auth_type)
+ }
+}
+
+/// Provide the generic interface for the user cloud service
+/// The user cloud service is responsible for the user authentication and user profile management
+pub trait UserAuthService: Send + Sync {
+ /// Sign up a new account.
+ /// The type of the params is defined the this trait's implementation.
+ /// Use the `unbox_or_error` of the [BoxAny] to get the params.
+ fn sign_up(&self, params: BoxAny) -> FutureResult<SignUpResponse, FlowyError>;
+
+ /// Sign in an account
+ /// The type of the params is defined the this trait's implementation.
+ fn sign_in(&self, params: BoxAny) -> FutureResult<SignInResponse, FlowyError>;
+
+ /// Sign out an account
+ fn sign_out(&self, token: Option<String>) -> FutureResult<(), FlowyError>;
+
+ /// Using the user's token to update the user information
fn update_user(
&self,
- token: &str,
+ uid: i64,
+ token: &Option<String>,
params: UpdateUserProfileParams,
) -> FutureResult<(), FlowyError>;
- fn get_user(&self, token: &str) -> FutureResult<UserProfilePB, FlowyError>;
- fn ws_addr(&self) -> String;
+
+ /// Get the user information using the user's token
+ fn get_user_profile(
+ &self,
+ token: Option<String>,
+ uid: i64,
+ ) -> FutureResult<Option<UserProfile>, FlowyError>;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
#[event_err = "FlowyError"]
pub enum UserEvent {
+ /// Only use when the [AuthType] is Local or SelfHosted
/// Logging into an account using a register email and password
#[event(input = "SignInPayloadPB", output = "UserProfilePB")]
SignIn = 0,
+ /// Only use when the [AuthType] is Local or SelfHosted
/// Creating a new account
#[event(input = "SignUpPayloadPB", output = "UserProfilePB")]
SignUp = 1,
/// Logging out fo an account
- #[event(passthrough)]
+ #[event(input = "SignOutPB")]
SignOut = 2,
/// Update the user information
@@ -92,4 +127,7 @@ pub enum UserEvent {
/// Get the settings of the user, such as the user storage folder
#[event(output = "UserSettingPB")]
GetUserSetting = 9,
+
+ #[event(input = "ThirdPartyAuthPB", output = "UserProfilePB")]
+ ThirdPartyAuth = 10,
}
diff --git a/frontend/rust-lib/flowy-user/src/lib.rs b/frontend/rust-lib/flowy-user/src/lib.rs
index e53ce14814..48aae0f063 100644
--- a/frontend/rust-lib/flowy-user/src/lib.rs
+++ b/frontend/rust-lib/flowy-user/src/lib.rs
@@ -1,14 +1,12 @@
+#[macro_use]
+extern crate flowy_sqlite;
+
pub mod entities;
mod event_handler;
pub mod event_map;
mod notification;
pub mod protobuf;
pub mod services;
-pub mod uid;
-// mod sql_tables;
-
-#[macro_use]
-extern crate flowy_sqlite;
pub mod errors {
pub use flowy_error::*;
diff --git a/frontend/rust-lib/flowy-user/src/services/database.rs b/frontend/rust-lib/flowy-user/src/services/database.rs
index af0569e5ac..98bc6fde47 100644
--- a/frontend/rust-lib/flowy-user/src/services/database.rs
+++ b/frontend/rust-lib/flowy-user/src/services/database.rs
@@ -112,20 +112,20 @@ pub struct UserTable {
pub(crate) name: String,
pub(crate) token: String,
pub(crate) email: String,
- pub(crate) workspace: String, // deprecated
+ pub(crate) workspace: String,
pub(crate) icon_url: String,
pub(crate) openai_key: String,
}
impl UserTable {
- pub fn new(id: String, name: String, email: String, token: String) -> Self {
+ pub fn new(id: String, name: String, email: String, token: String, workspace_id: String) -> Self {
Self {
id,
name,
email,
token,
icon_url: "".to_owned(),
- workspace: "".to_owned(),
+ workspace: workspace_id,
openai_key: "".to_owned(),
}
}
@@ -138,13 +138,29 @@ impl UserTable {
impl From<SignUpResponse> for UserTable {
fn from(resp: SignUpResponse) -> Self {
- UserTable::new(resp.user_id.to_string(), resp.name, resp.email, resp.token)
+ UserTable {
+ id: resp.user_id.to_string(),
+ name: resp.name,
+ token: resp.token.unwrap_or_default(),
+ email: resp.email.unwrap_or_default(),
+ workspace: resp.workspace_id,
+ icon_url: "".to_string(),
+ openai_key: "".to_string(),
+ }
}
}
impl From<SignInResponse> for UserTable {
fn from(resp: SignInResponse) -> Self {
- UserTable::new(resp.user_id.to_string(), resp.name, resp.email, resp.token)
+ UserTable {
+ id: resp.user_id.to_string(),
+ name: resp.name,
+ token: resp.token.unwrap_or_default(),
+ email: resp.email.unwrap_or_default(),
+ workspace: resp.workspace_id,
+ icon_url: "".to_string(),
+ openai_key: "".to_string(),
+ }
}
}
@@ -157,6 +173,7 @@ impl From<UserTable> for UserProfile {
token: table.token,
icon_url: table.icon_url,
openai_key: table.openai_key,
+ workspace_id: table.workspace,
}
}
}
diff --git a/frontend/rust-lib/flowy-user/src/services/user_session.rs b/frontend/rust-lib/flowy-user/src/services/user_session.rs
index 007ba2aa49..fdd2f8c23a 100644
--- a/frontend/rust-lib/flowy-user/src/services/user_session.rs
+++ b/frontend/rust-lib/flowy-user/src/services/user_session.rs
@@ -2,6 +2,7 @@ use std::sync::Arc;
use appflowy_integrate::RocksCollabDB;
use serde::{Deserialize, Serialize};
+use serde_repr::*;
use tokio::sync::RwLock;
use flowy_error::internal_error;
@@ -12,15 +13,16 @@ use flowy_sqlite::{
schema::{user_table, user_table::dsl},
DBConnection, ExpressionMethods, UserDatabaseConnection,
};
+use lib_infra::box_any::BoxAny;
use crate::entities::{
- SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile,
+ AuthTypePB, SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfile,
};
use crate::entities::{UserProfilePB, UserSettingPB};
-use crate::event_map::UserStatusCallback;
+use crate::event_map::{UserCloudServiceProvider, UserStatusCallback};
use crate::{
errors::FlowyError,
- event_map::UserCloudService,
+ event_map::UserAuthService,
notification::*,
services::database::{UserDB, UserTable, UserTableChangeset},
};
@@ -47,18 +49,21 @@ impl UserSessionConfig {
pub struct UserSession {
database: UserDB,
session_config: UserSessionConfig,
- cloud_service: Arc<dyn UserCloudService>,
+ cloud_services: Arc<dyn UserCloudServiceProvider>,
user_status_callback: RwLock<Option<Arc<dyn UserStatusCallback>>>,
}
impl UserSession {
- pub fn new(session_config: UserSessionConfig, cloud_service: Arc<dyn UserCloudService>) -> Self {
+ pub fn new(
+ session_config: UserSessionConfig,
+ cloud_services: Arc<dyn UserCloudServiceProvider>,
+ ) -> Self {
let db = UserDB::new(&session_config.root_dir);
let user_status_callback = RwLock::new(None);
Self {
database: db,
session_config,
- cloud_service,
+ cloud_services,
user_status_callback,
}
}
@@ -66,7 +71,7 @@ impl UserSession {
pub async fn init<C: UserStatusCallback + 'static>(&self, user_status_callback: C) {
if let Ok(session) = self.get_session() {
let _ = user_status_callback
- .did_sign_in(&session.token, session.user_id)
+ .did_sign_in(session.user_id, &session.workspace_id)
.await;
}
*self.user_status_callback.write().await = Some(Arc::new(user_status_callback));
@@ -93,77 +98,79 @@ impl UserSession {
self.database.get_kv_db(user_id)
}
- #[tracing::instrument(level = "debug", skip(self))]
- pub async fn sign_in(&self, params: SignInParams) -> Result<UserProfile, FlowyError> {
- if self.is_user_login(&params.email) {
- match self.get_user_profile().await {
- Ok(profile) => {
- send_sign_in_notification()
- .payload::<UserProfilePB>(profile.clone().into())
- .send();
- Ok(profile)
- },
- Err(err) => Err(err),
- }
- } else {
- let resp = self.cloud_service.sign_in(params).await?;
- let session: Session = resp.clone().into();
- self.set_session(Some(session))?;
- let user_profile: UserProfile = self.save_user(resp.into()).await?.into();
- let _ = self
- .user_status_callback
- .read()
- .await
- .as_ref()
- .unwrap()
- .did_sign_in(&user_profile.token, user_profile.id)
- .await;
- send_sign_in_notification()
- .payload::<UserProfilePB>(user_profile.clone().into())
- .send();
- Ok(user_profile)
- }
+ #[tracing::instrument(level = "debug", skip(self, params))]
+ pub async fn sign_in(
+ &self,
+ auth_type: &AuthType,
+ params: BoxAny,
+ ) -> Result<UserProfile, FlowyError> {
+ let resp = self
+ .cloud_services
+ .get_auth_service(auth_type)?
+ .sign_in(params)
+ .await?;
+
+ let session: Session = resp.clone().into();
+ self.set_session(Some(session))?;
+ let user_profile: UserProfile = self.save_user(resp.into()).await?.into();
+ let _ = self
+ .user_status_callback
+ .read()
+ .await
+ .as_ref()
+ .unwrap()
+ .did_sign_in(user_profile.id, &user_profile.workspace_id)
+ .await;
+ send_sign_in_notification()
+ .payload::<UserProfilePB>(user_profile.clone().into())
+ .send();
+
+ Ok(user_profile)
}
- #[tracing::instrument(level = "debug", skip(self))]
- pub async fn sign_up(&self, params: SignUpParams) -> Result<UserProfile, FlowyError> {
- if self.is_user_login(&params.email) {
- self.get_user_profile().await
- } else {
- let resp = self.cloud_service.sign_up(params).await?;
- let session: Session = resp.clone().into();
- self.set_session(Some(session))?;
- let user_table = self.save_user(resp.into()).await?;
- let user_profile: UserProfile = user_table.into();
- let _ = self
- .user_status_callback
- .read()
- .await
- .as_ref()
- .unwrap()
- .did_sign_up(&user_profile)
- .await;
- Ok(user_profile)
- }
+ #[tracing::instrument(level = "debug", skip(self, params))]
+ pub async fn sign_up(
+ &self,
+ auth_type: &AuthType,
+ params: BoxAny,
+ ) -> Result<UserProfile, FlowyError> {
+ let resp = self
+ .cloud_services
+ .get_auth_service(auth_type)?
+ .sign_up(params)
+ .await?;
+
+ let session: Session = resp.clone().into();
+ self.set_session(Some(session))?;
+ let user_table = self.save_user(resp.into()).await?;
+ let user_profile: UserProfile = user_table.into();
+ let _ = self
+ .user_status_callback
+ .read()
+ .await
+ .as_ref()
+ .unwrap()
+ .did_sign_up(&user_profile)
+ .await;
+ Ok(user_profile)
}
#[tracing::instrument(level = "debug", skip(self))]
- pub async fn sign_out(&self) -> Result<(), FlowyError> {
+ pub async fn sign_out(&self, auth_type: &AuthType) -> Result<(), FlowyError> {
let session = self.get_session()?;
let uid = session.user_id.to_string();
let _ = diesel::delete(dsl::user_table.filter(dsl::id.eq(&uid)))
.execute(&*(self.db_connection()?))?;
self.database.close_user_db(session.user_id)?;
self.set_session(None)?;
- let _ = self
- .user_status_callback
- .read()
- .await
- .as_ref()
- .unwrap()
- .did_expired(&session.token, session.user_id)
- .await;
- self.sign_out_on_server(&session.token).await?;
+ let server = self.cloud_services.get_auth_service(auth_type)?;
+ let token = session.token;
+ let _ = tokio::spawn(async move {
+ match server.sign_out(token).await {
+ Ok(_) => {},
+ Err(e) => tracing::error!("Sign out failed: {:?}", e),
+ }
+ });
Ok(())
}
@@ -173,16 +180,22 @@ impl UserSession {
&self,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
+ let auth_type = params.auth_type.clone();
let session = self.get_session()?;
let changeset = UserTableChangeset::new(params.clone());
diesel_update_table!(user_table, changeset, &*self.db_connection()?);
let user_profile = self.get_user_profile().await?;
let profile_pb: UserProfilePB = user_profile.into();
- send_notification(&session.token, UserNotification::DidUpdateUserProfile)
- .payload(profile_pb)
- .send();
- self.update_user_on_server(&session.token, params).await?;
+ send_notification(
+ &session.user_id.to_string(),
+ UserNotification::DidUpdateUserProfile,
+ )
+ .payload(profile_pb)
+ .send();
+ self
+ .update_user(&auth_type, session.user_id, &session.token, params)
+ .await?;
Ok(())
}
@@ -191,24 +204,21 @@ impl UserSession {
}
pub async fn check_user(&self) -> Result<UserProfile, FlowyError> {
- let (user_id, token) = self.get_session()?.into_part();
+ let (user_id, _token) = self.get_session()?.into_part();
let user_id = user_id.to_string();
let user = dsl::user_table
.filter(user_table::id.eq(&user_id))
.first::<UserTable>(&*(self.db_connection()?))?;
-
- self.read_user_profile_on_server(&token)?;
Ok(user.into())
}
pub async fn get_user_profile(&self) -> Result<UserProfile, FlowyError> {
- let (user_id, token) = self.get_session()?.into_part();
+ let (user_id, _) = self.get_session()?.into_part();
let user_id = user_id.to_string();
let user = dsl::user_table
.filter(user_table::id.eq(&user_id))
.first::<UserTable>(&*(self.db_connection()?))?;
- self.read_user_profile_on_server(&token)?;
Ok(user.into())
}
@@ -235,25 +245,23 @@ impl UserSession {
Ok(self.get_session()?.name)
}
- pub fn token(&self) -> Result<String, FlowyError> {
+ pub fn token(&self) -> Result<Option<String>, FlowyError> {
Ok(self.get_session()?.token)
}
}
impl UserSession {
- fn read_user_profile_on_server(&self, _token: &str) -> Result<(), FlowyError> {
- Ok(())
- }
-
- async fn update_user_on_server(
+ async fn update_user(
&self,
- token: &str,
+ auth_type: &AuthType,
+ uid: i64,
+ token: &Option<String>,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
- let server = self.cloud_service.clone();
+ let server = self.cloud_services.get_auth_service(auth_type)?;
let token = token.to_owned();
let _ = tokio::spawn(async move {
- match server.update_user(&token, params).await {
+ match server.update_user(uid, &token, params).await {
Ok(_) => {},
Err(e) => {
// TODO: retry?
@@ -265,19 +273,6 @@ impl UserSession {
Ok(())
}
- async fn sign_out_on_server(&self, token: &str) -> Result<(), FlowyError> {
- let server = self.cloud_service.clone();
- let token = token.to_owned();
- let _ = tokio::spawn(async move {
- match server.sign_out(&token).await {
- Ok(_) => {},
- Err(e) => tracing::error!("Sign out failed: {:?}", e),
- }
- })
- .await;
- Ok(())
- }
-
async fn save_user(&self, user: UserTable) -> Result<UserTable, FlowyError> {
let conn = self.db_connection()?;
let _ = diesel::insert_into(user_table::table)
@@ -304,17 +299,10 @@ impl UserSession {
Some(session) => Ok(session),
}
}
-
- fn is_user_login(&self, email: &str) -> bool {
- match self.get_session() {
- Ok(session) => session.email == email,
- Err(_) => false,
- }
- }
}
pub async fn update_user(
- _cloud_service: Arc<dyn UserCloudService>,
+ _cloud_service: Arc<dyn UserAuthService>,
pool: Arc<ConnectionPool>,
params: UpdateUserProfileParams,
) -> Result<(), FlowyError> {
@@ -333,10 +321,17 @@ impl UserDatabaseConnection for UserSession {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct Session {
user_id: i64,
- token: String,
- email: String,
+
+ workspace_id: String,
+
#[serde(default)]
name: String,
+
+ #[serde(default)]
+ token: Option<String>,
+
+ #[serde(default)]
+ email: Option<String>,
}
impl std::convert::From<SignInResponse> for Session {
@@ -346,6 +341,7 @@ impl std::convert::From<SignInResponse> for Session {
token: resp.token,
email: resp.email,
name: resp.name,
+ workspace_id: resp.workspace_id,
}
}
}
@@ -357,12 +353,13 @@ impl std::convert::From<SignUpResponse> for Session {
token: resp.token,
email: resp.email,
name: resp.name,
+ workspace_id: resp.workspace_id,
}
}
}
impl Session {
- pub fn into_part(self) -> (i64, String) {
+ pub fn into_part(self) -> (i64, Option<String>) {
(self.user_id, self.token)
}
}
@@ -390,11 +387,30 @@ impl std::convert::From<Session> for String {
}
}
-#[derive(Debug, Clone, Default, Serialize, Deserialize)]
-struct OldSession {
- user_id: String,
- token: String,
- email: String,
- #[serde(default)]
- name: String,
+#[derive(Debug, Clone, Hash, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
+#[repr(u8)]
+pub enum AuthType {
+ /// It's a local server, we do fake sign in default.
+ Local = 0,
+ /// Currently not supported. It will be supported in the future when the
+ /// [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Server) ready.
+ SelfHosted = 1,
+ /// It uses Supabase as the backend.
+ Supabase = 2,
+}
+
+impl Default for AuthType {
+ fn default() -> Self {
+ Self::Local
+ }
+}
+
+impl From<AuthTypePB> for AuthType {
+ fn from(pb: AuthTypePB) -> Self {
+ match pb {
+ AuthTypePB::Supabase => AuthType::Supabase,
+ AuthTypePB::Local => AuthType::Local,
+ AuthTypePB::SelfHosted => AuthType::SelfHosted,
+ }
+ }
}
diff --git a/frontend/rust-lib/flowy-user/tests/event/auth_test.rs b/frontend/rust-lib/flowy-user/tests/event/auth_test.rs
index 7fd5e0bfa8..408baa064a 100644
--- a/frontend/rust-lib/flowy-user/tests/event/auth_test.rs
+++ b/frontend/rust-lib/flowy-user/tests/event/auth_test.rs
@@ -1,8 +1,9 @@
-use crate::helper::*;
use flowy_test::{event_builder::UserModuleEventBuilder, FlowySDKTest};
-use flowy_user::entities::{SignInPayloadPB, SignUpPayloadPB, UserProfilePB};
+use flowy_user::entities::{AuthTypePB, SignInPayloadPB, SignUpPayloadPB, UserProfilePB};
use flowy_user::{errors::ErrorCode, event_map::UserEvent::*};
+use crate::helper::*;
+
#[tokio::test]
async fn sign_up_with_invalid_email() {
for email in invalid_email_test_case() {
@@ -11,6 +12,7 @@ async fn sign_up_with_invalid_email() {
email: email.to_string(),
name: valid_name(),
password: login_password(),
+ auth_type: AuthTypePB::Local,
};
assert_eq!(
@@ -33,6 +35,7 @@ async fn sign_up_with_invalid_password() {
email: random_email(),
name: valid_name(),
password,
+ auth_type: AuthTypePB::Local,
};
UserModuleEventBuilder::new(sdk)
@@ -56,6 +59,7 @@ async fn sign_in_success() {
email: sign_up_context.user_profile.email.clone(),
password: sign_up_context.password.clone(),
name: "".to_string(),
+ auth_type: AuthTypePB::Local,
};
let response = UserModuleEventBuilder::new(test.clone())
@@ -75,6 +79,7 @@ async fn sign_in_with_invalid_email() {
email: email.to_string(),
password: login_password(),
name: "".to_string(),
+ auth_type: AuthTypePB::Local,
};
assert_eq!(
@@ -99,6 +104,7 @@ async fn sign_in_with_invalid_password() {
email: random_email(),
password,
name: "".to_string(),
+ auth_type: AuthTypePB::Local,
};
UserModuleEventBuilder::new(sdk)
diff --git a/shared-lib/Cargo.lock b/shared-lib/Cargo.lock
index e743c3327f..7f0316748a 100644
--- a/shared-lib/Cargo.lock
+++ b/shared-lib/Cargo.lock
@@ -29,6 +29,12 @@ dependencies = [
"libc",
]
+[[package]]
+name = "anyhow"
+version = "1.0.71"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
+
[[package]]
name = "async-trait"
version = "0.1.64"
@@ -820,6 +826,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
name = "lib-infra"
version = "0.1.0"
dependencies = [
+ "anyhow",
"async-trait",
"bytes",
"chrono",
diff --git a/shared-lib/lib-infra/Cargo.toml b/shared-lib/lib-infra/Cargo.toml
index d4d3196064..7ee1c8559f 100644
--- a/shared-lib/lib-infra/Cargo.toml
+++ b/shared-lib/lib-infra/Cargo.toml
@@ -14,3 +14,4 @@ tokio = { version = "1.26", features = ["time", "rt"] }
rand = "0.8.5"
async-trait = "0.1.64"
md5 = "0.7.0"
+anyhow = "1.0"
diff --git a/shared-lib/lib-infra/src/box_any.rs b/shared-lib/lib-infra/src/box_any.rs
new file mode 100644
index 0000000000..25562fda43
--- /dev/null
+++ b/shared-lib/lib-infra/src/box_any.rs
@@ -0,0 +1,53 @@
+use std::any::Any;
+
+use anyhow::Result;
+
+pub struct BoxAny(Box<dyn Any + Send + Sync + 'static>);
+
+impl BoxAny {
+ pub fn new<T>(value: T) -> Self
+ where
+ T: Send + Sync + 'static,
+ {
+ Self(Box::new(value))
+ }
+
+ pub fn unbox_or_default<T>(self) -> T
+ where
+ T: Default + 'static,
+ {
+ match self.0.downcast::<T>() {
+ Ok(value) => *value,
+ Err(_) => T::default(),
+ }
+ }
+
+ pub fn unbox_or_error<T>(self) -> Result<T>
+ where
+ T: Default + 'static,
+ {
+ match self.0.downcast::<T>() {
+ Ok(value) => Ok(*value),
+ Err(e) => Err(anyhow::anyhow!(
+ "downcast error to {} failed: {:?}",
+ std::any::type_name::<T>(),
+ e
+ )),
+ }
+ }
+
+ pub fn unbox_or_none<T>(self) -> Option<T>
+ where
+ T: Default + 'static,
+ {
+ match self.0.downcast::<T>() {
+ Ok(value) => Some(*value),
+ Err(_) => None,
+ }
+ }
+
+ #[allow(dead_code)]
+ pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
+ self.0.downcast_ref()
+ }
+}
diff --git a/shared-lib/lib-infra/src/lib.rs b/shared-lib/lib-infra/src/lib.rs
index 5c0f9819cf..f2cf594209 100644
--- a/shared-lib/lib-infra/src/lib.rs
+++ b/shared-lib/lib-infra/src/lib.rs
@@ -1,6 +1,7 @@
+pub use async_trait;
+
+pub mod box_any;
pub mod future;
pub mod ref_map;
pub mod retry;
pub mod util;
-
-pub use async_trait;