Compare commits

..
Author SHA1 Message Date
Abdullah Atta 7ce7f02646 api: disable password changing due to a critical issue 2026-07-16 11:32:39 +05:00
Abdullah Atta a1dbd3f8b8 common: simplify email sender api 2026-07-08 09:05:58 +05:00
Abdullah Atta 99489b9b4c common: remove internal paddle related code 2026-07-07 09:20:24 +05:00
Abdullah Atta 1b953f756e identity: force change email casing to lower case on password reset 2026-06-29 10:01:15 +05:00
01zulfiandGitHub d27ab68735 inbox: trigger inboxUpdated SSE whenever pgp keys change (#104) 2026-06-10 08:17:06 +05:00
Abdullah Atta 294d885dbf common: expose x-object-size & content-length header via cors 2026-06-06 10:18:30 +05:00
Abdullah Atta bdd5017394 s3: add x-object-size header alongwith content-length header 2026-06-06 08:57:53 +05:00
01zulfiandGitHub 7ad70c63ee api: move inboxitemhistory collection sync in RequestFetchV4 (#101) 2026-05-19 13:39:52 +05:00
Abdullah Atta 0367ab6f80 sync: get rid of ._id fallback for inbox items 2026-05-15 08:58:21 +05:00
01zulfiandGitHub f3bfe0957b inbox: create InboxItemsHistory synced collection (#96) 2026-05-14 11:20:17 +05:00
01zulfiandGitHub 7f614f6954 inbox: remove 'Default' api key creation (#100) 2026-05-14 11:09:28 +05:00
Abdullah Atta 6663778e3e identity: fix email templates 2026-05-14 10:45:10 +05:00
14f0a3b37e inbox: improve http response status codes (#99)
* inbox: improve http response status codes
* inbox api: respond with 401 unauthorized for invalid inbox api key
* notesnook api (get public encryption key): respond with 404 if not found

* Update Notesnook.Inbox.API/src/index.ts

---------

Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2026-05-14 09:37:28 +05:00
01zulfiandGitHub 82a1152f9f inbox: fix expiry date validation check (#97) 2026-05-13 09:51:24 +05:00
01zulfiandGitHub 580524b855 inbox: require non-empty source in inbox item (#98) 2026-05-13 09:50:18 +05:00
Abdullah Atta 159fe0e376 identity: handle errors when sending/verifying sms otp codes 2026-05-08 11:08:55 +05:00
01zulfiandGitHub 30fdaae36c identity: return ok if user not found in recover endpoint (#95) 2026-05-02 22:19:26 +05:00
Abdullah Atta 815c8fb84c sync: log on disconnect 2026-05-02 22:18:39 +05:00
Abdullah Atta 04b0c305ed api: add redis to healthchecks 2026-05-02 22:18:34 +05:00
Abdullah Atta 31c57f95b2 sse: improve reliability 2026-04-23 09:54:59 +05:00
Abdullah Atta 33413b0a5c inbox: use separate model for CreateInboxApiKeyRequest 2026-04-15 09:05:41 +05:00
Abdullah Atta 4278b0624e sync: set ws keepalive interval to 15s 2026-04-11 10:55:05 +05:00
Abdullah Atta a56ef1fe11 sync: reduce chunk size to 3MB
this will increase the number of round trips but will make the sync
more reliable on slow devices that take longer to process 7MB
chunks.
2026-04-11 10:55:05 +05:00
01zulfiandGitHub d9c282fcf8 identity: replace account already exists errors on signup with one message (#90) 2026-04-05 11:36:15 +05:00
Abdullah Atta 73750613c4 monograph: move slug handling behind v2 endpoint 2026-04-02 15:01:14 +05:00
01zulfiandGitHub bb008c032d identity: render HTML pages for email confirmation outcomes (#89) 2026-04-01 19:27:42 +05:00
Abdullah Atta 5715f4c9ca monograph: fix 404 errors due to slug restriction on new monographs 2026-03-31 22:36:48 +05:00
Abdullah Atta 8116ce70e4 ci: publish inbox api docker image 2026-03-31 13:58:58 +05:00
01zulfiandGitHub 2f8b0ad607 identity: validate disposable email before sending 'email change' mail (#87) 2026-03-30 14:06:11 +05:00
1c5bcd6eff inbox: add health endpoint & docs (#64)
* inbox: add health endpoint && docs

* inbox: update post endpoint uri

* inbox: update docs & dockerfile

---------

Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2026-03-27 14:20:42 +05:00
Abdullah Atta 3a2a04317f inbox: add scripts for testing inbox api locally 2026-03-27 14:11:15 +05:00
8d92aff8cd inbox: use pgp encryption (#70)
* inbox: use pgp encryption && other fixes
* fix inbox key last used at time
* remove inbox items if keys change or same item id syncs

* inbox:update inbox sync item
* rename item field to sync
* add alg field

* sync: delete inbox items after commit succeeds

* user: merge if conditions

---------

Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2026-03-27 14:06:29 +05:00
4bc1469dfe monographs: add slug field which regenerates on republish (#72)
* monographs: add slug field which regenerates on update

* monographs: don't regenerate slug on update

* common: fix monograph public url constant

* monographs: improve APIs && use .Project when fetching monographs
* create separate endpoint for fetching monographs by slug
* combine analytics and publish-url endpoint into a publish-info endpoint

* monographs: reinstate analytics endpoint

* common: add missing monograph constant

* monograph: refactoring

---------

Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2026-03-26 23:14:20 +05:00
Abdullah Atta da58262afb cors: remove XFO header 2026-03-25 20:46:54 +05:00
Abdullah Atta 864baa702b common: add package name to clients 2026-03-25 11:11:07 +05:00
Abdullah Atta 55c5cd0a7c cors: fix youtube embeds not working on mobile 2026-03-20 12:35:36 +05:00
Abdullah Atta 077d411fc7 sync: use w1 write concern for device_ids_chunks collection 2026-03-15 22:09:42 +05:00
Abdullah Atta 1cecfe4b3c s3: disable bulk deletion (temporarily) 2026-03-12 10:24:24 +05:00
Abdullah Atta b8a7bd16a6 sync: add redis backplane for signalr 2026-03-09 11:49:48 +05:00
Abdullah AttaandAbdullah Atta fe7c546d9b monograph: fix typo 2026-02-25 15:43:35 +05:00
Abdullah AttaandAbdullah Atta 8d4336d1bc monograph: fix monograph content sanitization 2026-02-25 15:43:35 +05:00
Abdullah AttaandAbdullah Atta 9ae5db378d identity: simplify user sign up 2026-02-16 13:43:04 +05:00
Abdullah AttaandAbdullah Atta d5790d8785 api: minor refactor 2026-02-16 13:43:04 +05:00
Abdullah AttaandAbdullah Atta 9424afed68 api: move to atomic password reset 2026-02-16 13:43:04 +05:00
01zulfiandGitHub b9385ae112 s3: add bulk delete api (#82) 2026-02-13 11:29:15 +05:00
Abdullah Atta 014c4e3b32 data: configure mongodb using connection string 2026-02-02 22:33:32 +05:00
Abdullah Atta bf70a32b95 identity: temporarily disable password recovery & changing 2026-01-19 09:12:39 +05:00
Abdullah Atta d047bd052e cors: add headers to allow the YouTube embed wrapper to be displayed in an iframe 2026-01-19 08:57:50 +05:00
Abdullah Atta 03f230dbca cors: add special handling for youtube embeds to bypass referer policy restrictions 2026-01-15 15:07:34 +05:00
Abdullah Atta 0a8c707387 docker: do not publish trimmed images 2026-01-01 10:36:02 +05:00
Abdullah Atta 8908b8c6ed sync: minor refactors 2026-01-01 10:35:35 +05:00
Abdullah Atta da8df8973c sync: reduce batch size for fetching chunks 2025-12-30 12:52:56 +05:00
Abdullah Atta 7e50311c92 sync: reduce device ids chunk size 2025-12-30 12:52:36 +05:00
Abdullah Atta 02ccd6cd06 cors: make hostname configurable via env variable 2025-12-29 14:02:48 +05:00
Abdullah Atta 75369a5988 cors: add self hostable cors proxy 2025-12-29 12:33:11 +05:00
Abdullah Atta a3235ca381 common: add support for reading secret from file
this is required to support `docker secrets`
2025-12-26 14:57:38 +05:00
Abdullah Atta 119d5b0e7a docker: update to net9.0 2025-12-23 16:30:19 +05:00
Abdullah Atta b98612be7a sync: migrate sync devices from fs to mongodb 2025-12-22 20:11:43 +05:00
Abdullah Atta c7bb053cea common: simplify wamp logic
previously we were opening a new channel for each topic which was unnecessary
2025-12-22 13:38:58 +05:00
Abdullah Atta 265b456c46 s3: add support for failover 2025-12-17 09:06:26 +05:00
Abdullah Atta 347507f00a global: switch to dotnet9 2025-12-15 22:58:25 +05:00
Abdullah Atta 8dd9d0dc62 api: add support for {{Email}} placeholder in announcements 2025-11-28 13:01:28 +05:00
Abdullah Atta e489ce7376 common: add missing enum conversion for legacy_pro 2025-11-24 11:58:23 +05:00
Abdullah Atta 5ca9c142e3 monograph: {id}/stats -> {id}/analytics 2025-11-08 12:42:10 +05:00
01zulfiandGitHub 23b0b2ddfc monographs: add stats endpoint (#69)
* also don't mark monograph for sync when tracking view count
2025-11-07 10:28:03 +05:00
01zulfiandGitHub 7a8873db32 monographs: fix view count setting to 0 when updating (#68) 2025-11-07 09:59:52 +05:00
01zulfiandGitHub 294769fd71 monographs: track view count (#67) 2025-11-06 13:15:23 +05:00
01zulfiandGitHub 55136597aa fix: fix Streetwriters.Common build error (#65) 2025-11-05 22:52:49 +05:00
Abdullah Atta 9d3ef51c0c common: throw error if s3 env variables not defined 2025-11-05 22:43:38 +05:00
Abdullah Atta de23ea3e66 s3: add s3 health check 2025-11-05 22:42:58 +05:00
Abdullah Atta 54d0fdcf4f s3: fix storage limit rolloever 2025-11-05 22:42:45 +05:00
Abdullah Atta 1e8a205719 s3: improve error reporting 2025-11-05 22:41:24 +05:00
Abdullah Atta 70da841531 inbox: minor fixes and improvements 2025-10-30 13:12:05 +05:00
Abdullah Atta 5445c51d8d identity: fix error on sessions clear 2025-10-30 13:12:05 +05:00
dd05c55875 global: add support for specifying known proxies (#63)
* Add KNOWN_PROXIES

* Add known proxy setup in Startup.cs

Refactor forwarded headers configuration to use a variable for options.

* Document KNOWN_PROXIES in .env file

Added documentation for KNOWN_PROXIES environment variable

* Clean up

Restored license comments and formatting in Constants.cs.

* Apply suggestion from @thecodrr

* Added KnownProxies functionality at Streetwriters.Common level

---------

Co-authored-by: Abdullah Atta <thecodrr@protonmail.com>
2025-10-29 10:14:49 +05:00
Abdullah Atta ab9efaea7f identity: fix sign ups 2025-10-22 10:13:58 +05:00
Abdullah Atta 8bc1a52a60 s3: fix uploads for legacy users 2025-10-22 10:13:51 +05:00
Abdullah Atta 75a4462fd1 identity: add client id checks in grant validators 2025-10-14 21:50:57 +05:00
Abdullah Atta 8db33889b6 api: remove legacy sync hub 2025-10-14 21:16:59 +05:00
Abdullah Atta 50f159a37b s3: improve upload limit checks 2025-10-14 21:16:22 +05:00
Abdullah Atta 6e35edb715 global: add null safety checks 2025-10-14 21:15:51 +05:00
Abdullah Atta be432dfd24 global: migrate to using ILogger 2025-10-14 09:29:07 +05:00
01zulfiandGitHub 0cc3365e44 inbox: add rate limiting (#59) 2025-10-14 09:16:35 +05:00
01zulfiandGitHub a8cc02ef1a inbox: send salt in encrypted inbox item (#57) 2025-10-13 11:31:12 +05:00
Abdullah Atta d1421d640f identity: fix user subscription claim value incorrect for legacy pro users 2025-10-13 11:27:07 +05:00
Abdullah Atta 131df3df04 s3: use internal upload object url for uploading files 2025-10-09 14:16:59 +05:00
Abdullah Atta 1ecd8adee1 s3: fix attachments not uploading on self hosted servers
this was due to s3 endpoints requesting user's subscription
which didn't exist in case of
self hosted setups
2025-10-09 14:13:11 +05:00
01zulfiandGitHub 32b24dead2 inbox: sync inbox items 2025-10-08 12:49:41 +05:00
Abdullah Atta 4779a21134 ci: fix workflow 2025-10-07 17:05:10 +05:00
Abdullah Atta 6215c3c916 ci: run workflow on tag creation 2025-10-07 17:01:52 +05:00
Abdullah AttaandAbdullah Atta 16ce7942be sync: fix error on monographs sync 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 3a2e5f884b monograph: refactor to fix type errors 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 2b6a58ff04 api: handle subscription fetch errors 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 72e825a12c common: update subscription models 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 8aeeba0eeb s3: fix ContentLength header is required error 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 2952dd2c63 sync: fix monograph sync 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 908d64bd4f api: fix minor issues 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 41a185bd9f s3: disable download limits based on file size 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 61dd2e1f74 s3: add limit on download file size 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 3bb140aeb3 common: handle paddle billing errors 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 7172510c9e api: fix sync not working on android 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta cfe2875a67 api: refactor user subscription check for monograph embed & links 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 9860df2379 identity: refactor sms mfa authorization check 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta cc459f9fea common: add paddle v1 api client 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 9e6a25ec1d common: fix plan change if subscription is trialing 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 6304d8178f identity: fix delete subscription handling 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 500a64de18 identity: use subscription v2 types & api 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 55a2223198 common: add subscribe with semaphore to wamp to allow serial messaging 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 3beb716b83 common: add paddle billing api client 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta ed6e3c56f2 api: minor refactors aimed at improving memory consumption 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 579e65b0be api: add support for storage limits 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta b3dcdda697 api: improve sync hub auth 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 44a9ff57e7 common: add new subscription types 2025-10-07 16:44:15 +05:00
Abdullah AttaandAbdullah Atta 4361b90425 api: minor refactors 2025-10-07 16:44:15 +05:00
01zulfiandGitHub 3471ecb21a feat: set up public inbox api server (#52)
* feat: set up public inbox api server

* feat: add zod validation for raw inbox item

* chore: update encrypted item type && raw inbox item schema

* feat: use symmetric & asymmetric combination for inbox encryption

* chore: improve error handling

* chore: update encrypted item type

* feat: add Dockerfile for Notesnook.Inbox.Api
2025-10-06 12:22:25 +05:00
01zulfiandGitHub 5a9b98fd06 inbox: add GET public inbox key & POST inbox items endpoint (#51)
* inbox: add GET public inbox key && POST inbox items endpoint

* inbox: update SyncItem to support inbox items

* inbox: update post inbox items request payload

* inbox: update post inbox item endpoint
2025-10-06 12:21:31 +05:00
Abdullah Atta 34e5dc6a20 identity: fix super_strict rate limiting policy 2025-09-25 09:07:56 +05:00
Abdullah Atta 8f8c60e0b3 api: fix error on user patch 2025-09-17 11:38:33 +05:00
01zulfiandGitHub 9b774d640c feat: get, add, & delete user's inbox api tokens (#50)
* feat: get, add, & delete user's inbox api tokens

* inbox: generate inbox api key on the server

* inbox: use nanoid to generate api key && set created date on server

* inbox: set api key in constructor && increase default expiry date to 1 year
2025-09-16 08:40:52 +05:00
Abdullah Atta 4a0aee1c44 monograph: fix json content serialized as html 2025-09-15 12:30:28 +05:00
Abdullah Atta 97fbd3226d monograph: add support for webrisk api for analyzing urls for pro users 2025-09-15 11:22:37 +05:00
Abdullah Atta 0f43b3ee66 monograph: remove links, embeds & images for non-pro users 2025-09-15 09:19:16 +05:00
Abdullah Atta b469da70e8 api: only expose /metrics endpoint internally 2025-09-10 09:33:19 +05:00
01zulfiandGitHub 10e33de897 api: include inbox keys in get user endpoint (#48) 2025-09-09 19:44:08 +05:00
01zulfiandGitHub 6e8fb81ade sync: ensure monographs sync for first time ever on an existing device (#44) 2025-09-06 09:33:18 +05:00
01zulfiandGitHub 34a09ad15d inbox: store user's inbox keys (#47) 2025-09-06 09:32:44 +05:00
Abdullah Atta 201a235357 identity: make otp rate limiting more strict 2025-08-25 10:46:31 +05:00
01zulfiandGitHub e68b8f7e7c sync: add RequestFetchV2 hub method (#43) 2025-08-20 11:20:50 +05:00
Abdullah Atta a5b3a12914 monograph: fix some monographs can't be unpublished 2025-08-19 14:23:56 +05:00
Abdullah Atta 3ed30b206c api: run device cleanup job 1st of every month 2025-08-19 14:00:55 +05:00
Abdullah Atta 1344199807 api: add job to cleanup stale devices every 30 days 2025-08-19 12:24:43 +05:00
Abdullah Atta 33a189fe91 api: minor refactor 2025-08-18 23:44:22 +05:00
Abdullah Atta 2f361db9df monographs: fix monographs not appearing on clients 2025-08-18 23:44:22 +05:00
Minecon724andGitHub bf6cd6cd46 docker: fix the mongo healthcheck (#42) 2025-08-18 23:43:01 +05:00
Abdullah Atta 54266d1ba3 sync: disable SendMonographs on sync since its not supported by clients yet 2025-08-18 18:23:42 +05:00
Abdullah Atta d3894d2a9f sync: fix StorageLimit does not match any field or property 2025-08-18 17:54:14 +05:00
Abdullah Atta 00c089e677 docker: get rid of initiate-rs0 2025-08-18 14:22:31 +05:00
dyw770andGitHub fa8f69157a docker: Fix MongoDB container volume error (#12) 2025-08-18 13:34:31 +05:00
30d5394425 api: fix s3 multipart upload (#22)
* api: fix s3 multipart upload use external s3 url

* api: fix s3 multipart upload use external s3 url

* api: fix CompleteMultipartUploadRequest can not deserialize

* api: start multipart upload use s3 internal url

* Update Notesnook.API/Models/PartETagWrapper.cs

remove default constructor

Co-authored-by: Abdullah Atta <thecodrr@protonmail.com>

* api: remove default constructor

Co-authored-by: Abdullah Atta <thecodrr@protonmail.com>

* api: merge method call

Co-authored-by: Abdullah Atta <thecodrr@protonmail.com>

* api: revocation due to conflict

* api: revocation due to conflict

---------

Co-authored-by: Abdullah Atta <thecodrr@protonmail.com>
2025-08-18 13:33:49 +05:00
Vsevolod VolkovandGitHub 037bf4c3ea api: update S3 client mode for presigned URL generation to EXTERNAL (#33)
Signed-off-by: Vsevolod Volkov <73965070+StLyn4@users.noreply.github.com>
2025-08-18 11:48:48 +05:00
01zulfiandGitHub 6d6342dbff api: add user's monograph passwords key (#41) 2025-08-18 11:45:42 +05:00
01zulfiandGitHub 8df70c81fc monograph: fix existing item condition in update endpoint (#40) 2025-08-04 13:00:55 +05:00
bf2e6efeff monograph: add sync support (#39)
* monograph: add sync support

* monograph: fix password field && improve syncing logic && fix delete endpoint

* sync: get rid of unnecessary .ToList & ToListAsync

* sync: AddIdsToAllDevices is no longer asynchronous

* monograph: simplify and fix several bugs

- we were sending the triggerSync event to all users instead of all devices
- asynchronous methods did not have the `Async` suffix
- we weren't properly replacing the deleted monograph

* monograph: fix minor issues
* fix publishing
* don't return deleted monograph in monographs/:id endpoint
* persist UserId when soft deleting monograph

* monograph: check soft delete status in several endpoints

---------

Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2025-08-04 11:51:15 +05:00
Abdullah Atta 1e2ef0685d sync: don't break the loop if device id is same 2025-08-02 09:17:36 +05:00
Abdullah Atta c1f0e24d21 sync: update LastAccessTime on push, pull & register 2025-08-01 11:39:18 +05:00
Abdullah Atta a96b0e1e42 sync: use synchronous locks instead of semaphores 2025-08-01 10:52:48 +05:00
Abdullah Atta 76af2cbfc8 identity: fix 2fa recovery codes not working 2025-07-15 13:35:07 +05:00
Abdullah Atta 34fa43f302 global: add some basic rate limiting 2025-07-15 13:34:31 +05:00
Abdullah Atta 8c267e51f4 common: add gift card to SubscriptionProvider 2025-07-15 13:33:54 +05:00
Abdullah Atta 182558136a identify: validate mfa methods against user's enabled methods 2025-04-30 11:48:50 +05:00
Abdullah Atta 8d3b0d6dbf identity: validate mfa method against user's mfa methods 2025-04-30 11:45:18 +05:00
Abdullah Atta 0841ca1aa8 common: add gift card model 2025-04-10 12:20:03 +05:00
Abdullah Atta 11dff4f0cc identity: move email sender to common 2025-04-10 12:19:49 +05:00
Abdullah Atta bbabf51073 global: newline changes 2025-04-10 11:09:28 +05:00
Abdullah Atta a135bd50d7 db: increase heartbeat interval to 60 seconds 2024-12-02 09:38:21 +05:00
Abdullah Atta e5bf3367cc global: add support for -DSTAGING 2024-11-28 14:38:28 +05:00
Abdullah Atta c6bcd4a84d monograph: add new ItemId property that is a simple string
This fixes the issue where an invalid objectid causes error when publishing a monograph
2024-11-28 14:21:57 +05:00
Abdullah Atta 07675632e0 identity: enable user's 2fa method after verifying 2fa code
This must be done after the MFA code is verified otherwise the security stamp will be updated making the 2FA code invalid
2024-11-28 14:20:35 +05:00
Abdullah Atta 8d15e176ff docker: DISABLE_ACCOUNT_CREATION -> DISABLE_SIGNUPS 2024-10-25 12:40:53 +05:00
Abdullah Atta e3f97bc47e docker: use named volumes instead of creating folders in $HOME 2024-10-12 13:39:57 +05:00
Abdullah Atta 9482e1ddc1 docker: add health checks for mongodb and minio 2024-10-12 13:18:12 +05:00
Abdullah Atta 0447ab6e55 docker: fix variable descriptions 2024-10-12 12:07:31 +05:00
Abdullah Atta 682d904dc9 docker: make monograph self hostable 2024-10-12 12:00:40 +05:00
Abdullah Atta d8ee28389a docker: make .env more consistent 2024-10-12 12:00:29 +05:00
Abdullah Atta 309dcafa02 monograph: add new id/view endpoint for self destruction 2024-10-12 11:59:10 +05:00
Abdullah Atta 1b97ba77da identity: use new server PublicURL 2024-10-12 11:56:57 +05:00
Abdullah Atta 6d19112fb6 common: replace server Domain with PublicURL 2024-10-12 11:56:34 +05:00
Abdullah Atta 1c68942a6d identity: replace Sodium.Core with Geralt 2024-10-12 11:55:52 +05:00
ManiuesandGitHub 3cc84d7603 Update self-hosting TO-DO (#10)
* Update self-hosting TO-DO

* Add starting version and update note
2024-09-26 18:53:17 +05:00
Abdullah AttaandGitHub 7f94a647c7 Merge pull request #8 from dyw770/master
docker: fix db persistent configuration
2024-09-06 21:00:28 +05:00
dyw770andGitHub ba006974a0 docker: fix db persistent configuration 2024-09-05 23:18:12 +08:00
Abdullah AttaandGitHub c3772c86ee Merge pull request #7 from dyw770/master
docker: add db persistence configuration and modify the value of the S3_INTERNAL_SERVICE_URL environment variable
2024-09-05 20:09:21 +05:00
dyw770 53695174b5 docker: Add db persistence configuration and modify the value of the S3_INTERNAL_SERVICE_URL environment variable 2024-09-05 12:06:04 +08:00
225 changed files with 15695 additions and 12224 deletions
+4 -4
View File
@@ -1,5 +1,5 @@
**/Dockerfile **/Dockerfile
**/bin **/bin
**/obj **/obj
**/.env **/.env
**/.env.local **/.env.local
+21 -21
View File
@@ -9,8 +9,8 @@ NOTESNOOK_API_SECRET=
# Description: Use this flag to disable creation of new accounts on your instance (i.e. in case it is exposed to the Internet). # Description: Use this flag to disable creation of new accounts on your instance (i.e. in case it is exposed to the Internet).
# Required: yes # Required: yes
# Possible values: 0 for false; 1 for true # Possible values: true/false
DISABLE_ACCOUNT_CREATION=0 DISABLE_SIGNUPS=false
### SMTP Configuration ### ### SMTP Configuration ###
# SMTP Configuration is required for sending emails for password reset, 2FA emails etc. You can get SMTP settings from your email provider. # SMTP Configuration is required for sending emails for password reset, 2FA emails etc. You can get SMTP settings from your email provider.
@@ -29,14 +29,6 @@ SMTP_HOST=
# Required: yes # Required: yes
# Example: 465 # Example: 465
SMTP_PORT= SMTP_PORT=
# Description: The FROM email address when sending out emails. Must be an email address under your control otherwise sending will fail. Most times it is the same email address as the SMTP_USERNAME.
# Required: no
# Example: support@notesnook.com
NOTESNOOK_SENDER_EMAIL=
# Description: The reply-to email is used whenever a user is replying to the email you sent. You can use this to set a different reply-to email address than the one you used to send the email.
# Required: no
# Example: support@notesnook.com
SMTP_REPLYTO_EMAIL=
# Description: Twilio account SID is required for sending SMS with 2FA codes. Learn more here: https://help.twilio.com/articles/14726256820123-What-is-a-Twilio-Account-SID-and-where-can-I-find-it- # Description: Twilio account SID is required for sending SMS with 2FA codes. Learn more here: https://help.twilio.com/articles/14726256820123-What-is-a-Twilio-Account-SID-and-where-can-I-find-it-
# Required: no # Required: no
@@ -49,21 +41,33 @@ TWILIO_AUTH_TOKEN=
# Example: VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # Example: VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
TWILIO_SERVICE_SID= TWILIO_SERVICE_SID=
# Description: This is the public domain for the Authentication server. It can also be the IP address if you don't own a domain name. The domain/IP must be accessible from wherever you are running the Notesnook clients. Used for generating email confirmation & password reset URLs.
# Required: yes
# Example: auth.streetwriters.co
IDENTITY_SERVER_DOMAIN=localhost:8264
# Description: Add the origins for which you want to allow CORS. Leave it empty to allow all origins to access your server. If you want to allow multiple origins, seperate each origin with a comma. # Description: Add the origins for which you want to allow CORS. Leave it empty to allow all origins to access your server. If you want to allow multiple origins, seperate each origin with a comma.
# Required: no # Required: no
# Example: https://app.notesnook.com,http://localhost:3000 # Example: https://app.notesnook.com,http://localhost:3000
NOTESNOOK_CORS_ORIGINS= NOTESNOOK_CORS_ORIGINS=
# Description: This is the URL for the web app, and is used by the backend for creating redirect URLs (e.g. after email confirmation etc). # Description: Add known proxies for incoming HTTP requests
# Required: no
# Example: 192.168.1.2,192.168.1.3
KNOWN_PROXIES=
# Description: This is the public URL for the web app, and is used by the backend for creating redirect URLs (e.g. after email confirmation etc).
# Note: the URL has no slashes at the end # Note: the URL has no slashes at the end
# Required: yes # Required: yes
# Example: https://app.notesnook.com # Example: https://app.notesnook.com
NOTESNOOK_APP_HOST=https://app.notesnook.com NOTESNOOK_APP_PUBLIC_URL=https://app.notesnook.com
# Description: This is the public URL for the monograph frontend.
# Required: yes
# Example: https://monogr.ph
MONOGRAPH_PUBLIC_URL=http://localhost:6264
# Description: This is the public URL for the Authentication server. Used for generating email confirmation & password reset URLs.
# Required: yes
# Example: https://auth.streetwriters.co
AUTH_SERVER_PUBLIC_URL=http://localhost:8264
# Description: This is the public URL for the S3 attachments server (minio). It'll be used by the Notesnook clients for uploading/downloading attachments.
# Required: yes
# Example: https://attachments.notesnook.com
ATTACHMENTS_SERVER_PUBLIC_URL=http://localhost:9000
# Description: Custom username for the root Minio account. Minio is used for storing your attachments. This must be greater than 3 characters in length. # Description: Custom username for the root Minio account. Minio is used for storing your attachments. This must be greater than 3 characters in length.
# Required: no # Required: no
@@ -71,7 +75,3 @@ MINIO_ROOT_USER=
# Description: Custom password for the root Minio account. Minio is used for storing your attachments. This must be greater than 8 characters in length. # Description: Custom password for the root Minio account. Minio is used for storing your attachments. This must be greater than 8 characters in length.
# Required: no # Required: no
MINIO_ROOT_PASSWORD= MINIO_ROOT_PASSWORD=
# Description: The URL must be accessible from wherever you are running the Notesnook clients. It'll be used by the Notesnook clients for uploading/downloading attachments.
# Required: no
# Example: https://attachments.notesnook.com
S3_SERVICE_URL=
+17 -5
View File
@@ -10,8 +10,9 @@
name: Publish Docker images name: Publish Docker images
on: on:
release: push:
types: [published] tags:
- "v*"
jobs: jobs:
push_to_registry: push_to_registry:
@@ -22,12 +23,23 @@ jobs:
repos: repos:
- image: streetwriters/notesnook-sync - image: streetwriters/notesnook-sync
file: ./Notesnook.API/Dockerfile file: ./Notesnook.API/Dockerfile
context: .
- image: streetwriters/cors-proxy
file: ./cors-proxy/Dockerfile
context: ./cors-proxy/
- image: streetwriters/identity - image: streetwriters/identity
file: ./Streetwriters.Identity/Dockerfile file: ./Streetwriters.Identity/Dockerfile
context: .
- image: streetwriters/sse - image: streetwriters/sse
file: ./Streetwriters.Messenger/Dockerfile file: ./Streetwriters.Messenger/Dockerfile
context: .
- image: streetwriters/notesnook-inbox
file: ./Notesnook.Inbox.API/Dockerfile
context: ./Notesnook.Inbox.API/
permissions: permissions:
packages: write packages: write
contents: read contents: read
@@ -41,7 +53,7 @@ jobs:
- name: Docker Setup Buildx - name: Docker Setup Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
with: with:
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v8 platforms: linux/amd64,linux/arm64
- name: Log in to Docker Hub - name: Log in to Docker Hub
uses: docker/login-action@v3 uses: docker/login-action@v3
@@ -70,10 +82,10 @@ jobs:
id: push id: push
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
with: with:
context: . context: ${{ matrix.repos.context }}
file: ${{ matrix.repos.file }} file: ${{ matrix.repos.file }}
push: true push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v8 platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }} tags: ${{ steps.meta.outputs.tags }}
cache-from: ${{ matrix.repos.image }}:latest cache-from: ${{ matrix.repos.image }}:latest
+266 -266
View File
@@ -1,267 +1,267 @@
## Ignore Visual Studio temporary files, build results, and ## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons. ## files generated by popular Visual Studio add-ons.
# User-specific files # User-specific files
*.suo *.suo
*.user *.user
*.userosscache *.userosscache
*.sln.docstates *.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio) # User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs *.userprefs
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
[Dd]ebugPublic/ [Dd]ebugPublic/
[Rr]elease/ [Rr]elease/
[Rr]eleases/ [Rr]eleases/
x64/ x64/
x86/ x86/
bld/ bld/
[Bb]in/ [Bb]in/
[Oo]bj/ [Oo]bj/
[Ll]og/ [Ll]og/
# Visual Studio 2015 cache/options directory # Visual Studio 2015 cache/options directory
.vs/ .vs/
# Uncomment if you have tasks that create the project's static files in wwwroot # Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/ #wwwroot/
# MSTest test Results # MSTest test Results
[Tt]est[Rr]esult*/ [Tt]est[Rr]esult*/
[Bb]uild[Ll]og.* [Bb]uild[Ll]og.*
# NUNIT # NUNIT
*.VisualState.xml *.VisualState.xml
TestResult.xml TestResult.xml
# Build Results of an ATL Project # Build Results of an ATL Project
[Dd]ebugPS/ [Dd]ebugPS/
[Rr]eleasePS/ [Rr]eleasePS/
dlldata.c dlldata.c
# DNX # DNX
project.lock.json project.lock.json
project.fragment.lock.json project.fragment.lock.json
artifacts/ artifacts/
*_i.c *_i.c
*_p.c *_p.c
*_i.h *_i.h
*.ilk *.ilk
*.meta *.meta
*.obj *.obj
*.pch *.pch
*.pdb *.pdb
*.pgc *.pgc
*.pgd *.pgd
*.rsp *.rsp
*.sbr *.sbr
*.tlb *.tlb
*.tli *.tli
*.tlh *.tlh
*.tmp *.tmp
*.tmp_proj *.tmp_proj
*.log *.log
*.vspscc *.vspscc
*.vssscc *.vssscc
.builds .builds
*.pidb *.pidb
*.svclog *.svclog
*.scc *.scc
# Chutzpah Test files # Chutzpah Test files
_Chutzpah* _Chutzpah*
# Visual C++ cache files # Visual C++ cache files
ipch/ ipch/
*.aps *.aps
*.ncb *.ncb
*.opendb *.opendb
*.opensdf *.opensdf
*.sdf *.sdf
*.cachefile *.cachefile
*.VC.db *.VC.db
*.VC.VC.opendb *.VC.VC.opendb
# Visual Studio profiler # Visual Studio profiler
*.psess *.psess
*.vsp *.vsp
*.vspx *.vspx
*.sap *.sap
# TFS 2012 Local Workspace # TFS 2012 Local Workspace
$tf/ $tf/
# Guidance Automation Toolkit # Guidance Automation Toolkit
*.gpState *.gpState
# ReSharper is a .NET coding add-in # ReSharper is a .NET coding add-in
_ReSharper*/ _ReSharper*/
*.[Rr]e[Ss]harper *.[Rr]e[Ss]harper
*.DotSettings.user *.DotSettings.user
# JustCode is a .NET coding add-in # JustCode is a .NET coding add-in
.JustCode .JustCode
# TeamCity is a build add-in # TeamCity is a build add-in
_TeamCity* _TeamCity*
# DotCover is a Code Coverage Tool # DotCover is a Code Coverage Tool
*.dotCover *.dotCover
# NCrunch # NCrunch
_NCrunch_* _NCrunch_*
.*crunch*.local.xml .*crunch*.local.xml
nCrunchTemp_* nCrunchTemp_*
# MightyMoose # MightyMoose
*.mm.* *.mm.*
AutoTest.Net/ AutoTest.Net/
# Web workbench (sass) # Web workbench (sass)
.sass-cache/ .sass-cache/
# Installshield output folder # Installshield output folder
[Ee]xpress/ [Ee]xpress/
# DocProject is a documentation generator add-in # DocProject is a documentation generator add-in
DocProject/buildhelp/ DocProject/buildhelp/
DocProject/Help/*.HxT DocProject/Help/*.HxT
DocProject/Help/*.HxC DocProject/Help/*.HxC
DocProject/Help/*.hhc DocProject/Help/*.hhc
DocProject/Help/*.hhk DocProject/Help/*.hhk
DocProject/Help/*.hhp DocProject/Help/*.hhp
DocProject/Help/Html2 DocProject/Help/Html2
DocProject/Help/html DocProject/Help/html
# Click-Once directory # Click-Once directory
publish/ publish/
# Publish Web Output # Publish Web Output
*.[Pp]ublish.xml *.[Pp]ublish.xml
*.azurePubxml *.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings # TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted # but database connection strings (with potential passwords) will be unencrypted
#*.pubxml #*.pubxml
*.publishproj *.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to # Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained # checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted # in these scripts will be unencrypted
PublishScripts/ PublishScripts/
# NuGet Packages # NuGet Packages
*.nupkg *.nupkg
# The packages folder can be ignored because of Package Restore # The packages folder can be ignored because of Package Restore
**/packages/* **/packages/*
# except build/, which is used as an MSBuild target. # except build/, which is used as an MSBuild target.
!**/packages/build/ !**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed # Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config #!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files # NuGet v3's project.json files produces more ignoreable files
*.nuget.props *.nuget.props
*.nuget.targets *.nuget.targets
# Microsoft Azure Build Output # Microsoft Azure Build Output
csx/ csx/
*.build.csdef *.build.csdef
# Microsoft Azure Emulator # Microsoft Azure Emulator
ecf/ ecf/
rcf/ rcf/
# Windows Store app package directories and files # Windows Store app package directories and files
AppPackages/ AppPackages/
BundleArtifacts/ BundleArtifacts/
Package.StoreAssociation.xml Package.StoreAssociation.xml
_pkginfo.txt _pkginfo.txt
# Visual Studio cache files # Visual Studio cache files
# files ending in .cache can be ignored # files ending in .cache can be ignored
*.[Cc]ache *.[Cc]ache
# but keep track of directories ending in .cache # but keep track of directories ending in .cache
!*.[Cc]ache/ !*.[Cc]ache/
# Others # Others
ClientBin/ ClientBin/
~$* ~$*
*~ *~
*.dbmdl *.dbmdl
*.dbproj.schemaview *.dbproj.schemaview
*.jfm *.jfm
*.pfx *.pfx
*.publishsettings *.publishsettings
node_modules/ node_modules/
orleans.codegen.cs orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components # Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/ #bower_components/
# RIA/Silverlight projects # RIA/Silverlight projects
Generated_Code/ Generated_Code/
# Backup & report files from converting an old project file # Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed, # to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-) # because we have git ;-)
_UpgradeReport_Files/ _UpgradeReport_Files/
Backup*/ Backup*/
UpgradeLog*.XML UpgradeLog*.XML
UpgradeLog*.htm UpgradeLog*.htm
# SQL Server files # SQL Server files
*.mdf *.mdf
*.ldf *.ldf
# Business Intelligence projects # Business Intelligence projects
*.rdl.data *.rdl.data
*.bim.layout *.bim.layout
*.bim_*.settings *.bim_*.settings
# Microsoft Fakes # Microsoft Fakes
FakesAssemblies/ FakesAssemblies/
# GhostDoc plugin setting file # GhostDoc plugin setting file
*.GhostDoc.xml *.GhostDoc.xml
# Node.js Tools for Visual Studio # Node.js Tools for Visual Studio
.ntvs_analysis.dat .ntvs_analysis.dat
# Visual Studio 6 build log # Visual Studio 6 build log
*.plg *.plg
# Visual Studio 6 workspace options file # Visual Studio 6 workspace options file
*.opt *.opt
# Visual Studio LightSwitch build output # Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts **/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts **/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml **/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts **/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml **/*.Server/ModelManifest.xml
_Pvt_Extensions _Pvt_Extensions
# Paket dependency manager # Paket dependency manager
.paket/paket.exe .paket/paket.exe
paket-files/ paket-files/
# FAKE - F# Make # FAKE - F# Make
.fake/ .fake/
# JetBrains Rider # JetBrains Rider
.idea/ .idea/
*.sln.iml *.sln.iml
# CodeRush # CodeRush
.cr/ .cr/
# Python Tools for Visual Studio (PTVS) # Python Tools for Visual Studio (PTVS)
__pycache__/ __pycache__/
*.pyc *.pyc
keys/ keys/
dist/ dist/
keystore/ keystore/
.env.local .env.local
Notesnook.API/sync/ Notesnook.API/sync/
+3 -3
View File
@@ -9,7 +9,7 @@
"type": "coreclr", "type": "coreclr",
"request": "launch", "request": "launch",
"preLaunchTask": "build-notesnook", "preLaunchTask": "build-notesnook",
"program": "bin/Debug/net8.0/Notesnook.API.dll", "program": "bin/Debug/net9.0/Notesnook.API.dll",
"args": [], "args": [],
"cwd": "${workspaceFolder}/Notesnook.API", "cwd": "${workspaceFolder}/Notesnook.API",
"stopAtEntry": false, "stopAtEntry": false,
@@ -24,7 +24,7 @@
"type": "coreclr", "type": "coreclr",
"request": "launch", "request": "launch",
"preLaunchTask": "build-identity", "preLaunchTask": "build-identity",
"program": "bin/Debug/net8.0/Streetwriters.Identity.dll", "program": "bin/Debug/net9.0/Streetwriters.Identity.dll",
"args": [], "args": [],
"cwd": "${workspaceFolder}/Streetwriters.Identity", "cwd": "${workspaceFolder}/Streetwriters.Identity",
"stopAtEntry": false, "stopAtEntry": false,
@@ -39,7 +39,7 @@
"type": "coreclr", "type": "coreclr",
"request": "launch", "request": "launch",
"preLaunchTask": "build-messenger", "preLaunchTask": "build-messenger",
"program": "bin/Debug/net8.0/Streetwriters.Messenger.dll", "program": "bin/Debug/net9.0/Streetwriters.Messenger.dll",
"args": [], "args": [],
"cwd": "${workspaceFolder}/Streetwriters.Messenger", "cwd": "${workspaceFolder}/Streetwriters.Messenger",
"stopAtEntry": false, "stopAtEntry": false,
+41 -41
View File
@@ -1,41 +1,41 @@
{ {
"version": "2.0.0", "version": "2.0.0",
"tasks": [ "tasks": [
{ {
"label": "build-notesnook", "label": "build-notesnook",
"command": "dotnet", "command": "dotnet",
"type": "process", "type": "process",
"args": [ "args": [
"build", "build",
"${workspaceFolder}/Notesnook.API/Notesnook.API.csproj", "${workspaceFolder}/Notesnook.API/Notesnook.API.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],
"problemMatcher": "$msCompile" "problemMatcher": "$msCompile"
}, },
{ {
"label": "build-identity", "label": "build-identity",
"command": "dotnet", "command": "dotnet",
"type": "process", "type": "process",
"args": [ "args": [
"build", "build",
"${workspaceFolder}/Streetwriters.Identity/Streetwriters.Identity.csproj", "${workspaceFolder}/Streetwriters.Identity/Streetwriters.Identity.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],
"problemMatcher": "$msCompile" "problemMatcher": "$msCompile"
}, },
{ {
"label": "build-messenger", "label": "build-messenger",
"command": "dotnet", "command": "dotnet",
"type": "process", "type": "process",
"args": [ "args": [
"build", "build",
"${workspaceFolder}/Streetwriters.Messenger/Streetwriters.Messenger.csproj", "${workspaceFolder}/Streetwriters.Messenger/Streetwriters.Messenger.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],
"problemMatcher": "$msCompile" "problemMatcher": "$msCompile"
}, },
] ]
} }
+3 -3
View File
@@ -1,3 +1,3 @@
Notesnook Sync Server is written & maintained by: Notesnook Sync Server is written & maintained by:
- Abdullah Atta <abdullahatta@streetwriters.co> - Abdullah Atta <abdullahatta@streetwriters.co>
- Ammar Ahmed <ammarahmed6506@gmail.com> - Ammar Ahmed <ammarahmed6506@gmail.com>
+128 -128
View File
@@ -1,128 +1,128 @@
# Contributor Covenant Code of Conduct # Contributor Covenant Code of Conduct
## Our Pledge ## Our Pledge
We as members, contributors, and leaders pledge to make participation in our We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status, identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity nationality, personal appearance, race, religion, or sexual identity
and orientation. and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming, We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community. diverse, inclusive, and healthy community.
## Our Standards ## Our Standards
Examples of behavior that contributes to a positive environment for our Examples of behavior that contributes to a positive environment for our
community include: community include:
- Demonstrating empathy and kindness toward other people - Demonstrating empathy and kindness toward other people
- Being respectful of differing opinions, viewpoints, and experiences - Being respectful of differing opinions, viewpoints, and experiences
- Giving and gracefully accepting constructive feedback - Giving and gracefully accepting constructive feedback
- Accepting responsibility and apologizing to those affected by our mistakes, - Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience and learning from the experience
- Focusing on what is best not just for us as individuals, but for the - Focusing on what is best not just for us as individuals, but for the
overall community overall community
Examples of unacceptable behavior include: Examples of unacceptable behavior include:
- The use of sexualized language or imagery, and sexual attention or - The use of sexualized language or imagery, and sexual attention or
advances of any kind advances of any kind
- Trolling, insulting or derogatory comments, and personal or political attacks - Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment - Public or private harassment
- Publishing others' private information, such as a physical or email - Publishing others' private information, such as a physical or email
address, without their explicit permission address, without their explicit permission
- Other conduct which could reasonably be considered inappropriate in a - Other conduct which could reasonably be considered inappropriate in a
professional setting professional setting
## Enforcement Responsibilities ## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive, response to any behavior that they deem inappropriate, threatening, offensive,
or harmful. or harmful.
Community leaders have the right and responsibility to remove, edit, or reject Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate. decisions when appropriate.
## Scope ## Scope
This Code of Conduct applies within all community spaces, and also applies when This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces. an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address, Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed posting via an official social media account, or acting as an appointed
representative at an online or offline event. representative at an online or offline event.
## Enforcement ## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at reported to the community leaders responsible for enforcement at
support@streetwriters.co. support@streetwriters.co.
All complaints will be reviewed and investigated promptly and fairly. All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the All community leaders are obligated to respect the privacy and security of the
reporter of any incident. reporter of any incident.
## Enforcement Guidelines ## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct: the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction ### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed **Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community. unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing **Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested. behavior was inappropriate. A public apology may be requested.
### 2. Warning ### 2. Warning
**Community Impact**: A violation through a single incident or series **Community Impact**: A violation through a single incident or series
of actions. of actions.
**Consequence**: A warning with consequences for continued behavior. No **Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or like social media. Violating these terms may lead to a temporary or
permanent ban. permanent ban.
### 3. Temporary Ban ### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including **Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior. sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public **Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period. with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban. Violating these terms may lead to a permanent ban.
### 4. Permanent Ban ### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community **Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals. individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within **Consequence**: A permanent ban from any sort of public interaction within
the community. the community.
## Attribution ## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
Community Impact Guidelines were inspired by [Mozilla's code of conduct Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity). enforcement ladder](https://github.com/mozilla/diversity).
[homepage]: https://www.contributor-covenant.org [homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see the FAQ at For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations. https://www.contributor-covenant.org/translations.
+661 -661
View File
File diff suppressed because it is too large Load Diff
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MongoDB.Driver; using MongoDB.Driver;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Models; using Notesnook.API.Models;
@@ -41,8 +42,13 @@ namespace Notesnook.API.Accessors
public SyncItemsRepository Colors { get; } public SyncItemsRepository Colors { get; }
public SyncItemsRepository Vaults { get; } public SyncItemsRepository Vaults { get; }
public SyncItemsRepository Tags { get; } public SyncItemsRepository Tags { get; }
public SyncItemsRepository InboxItemsHistory { get; }
public Repository<UserSettings> UsersSettings { get; } public Repository<UserSettings> UsersSettings { get; }
public Repository<Monograph> Monographs { get; } public Repository<Monograph> Monographs { get; }
public Repository<InboxApiKey> InboxApiKey { get; }
public Repository<InboxSyncItem> InboxItems { get; }
public Repository<SyncDevice> SyncDevices { get; }
public Repository<DeviceIdsChunk> DeviceIdsChunks { get; }
public SyncItemsRepositoryAccessor(IDbContext dbContext, public SyncItemsRepositoryAccessor(IDbContext dbContext,
@@ -70,23 +76,36 @@ namespace Notesnook.API.Accessors
IMongoCollection<SyncItem> vaults, IMongoCollection<SyncItem> vaults,
[FromKeyedServices(Collections.TagsKey)] [FromKeyedServices(Collections.TagsKey)]
IMongoCollection<SyncItem> tags, IMongoCollection<SyncItem> tags,
[FromKeyedServices(Collections.InboxItemsHistoryKey)]
IMongoCollection<SyncItem> inboxItemsHistory,
Repository<UserSettings> usersSettings, Repository<Monograph> monographs) Repository<UserSettings> usersSettings,
Repository<Monograph> monographs,
Repository<InboxApiKey> inboxApiKey,
Repository<InboxSyncItem> inboxItems,
Repository<SyncDevice> syncDevices,
Repository<DeviceIdsChunk> deviceIdsChunks,
ILogger<SyncItemsRepository> logger)
{ {
UsersSettings = usersSettings; UsersSettings = usersSettings;
Monographs = monographs; Monographs = monographs;
Notebooks = new SyncItemsRepository(dbContext, notebooks); InboxApiKey = inboxApiKey;
Notes = new SyncItemsRepository(dbContext, notes); InboxItems = inboxItems;
Contents = new SyncItemsRepository(dbContext, content); SyncDevices = syncDevices;
Settings = new SyncItemsRepository(dbContext, settings); DeviceIdsChunks = deviceIdsChunks;
LegacySettings = new SyncItemsRepository(dbContext, legacySettings); Notebooks = new SyncItemsRepository(dbContext, notebooks, logger);
Attachments = new SyncItemsRepository(dbContext, attachments); Notes = new SyncItemsRepository(dbContext, notes, logger);
Shortcuts = new SyncItemsRepository(dbContext, shortcuts); Contents = new SyncItemsRepository(dbContext, content, logger);
Reminders = new SyncItemsRepository(dbContext, reminders); Settings = new SyncItemsRepository(dbContext, settings, logger);
Relations = new SyncItemsRepository(dbContext, relations); LegacySettings = new SyncItemsRepository(dbContext, legacySettings, logger);
Colors = new SyncItemsRepository(dbContext, colors); Attachments = new SyncItemsRepository(dbContext, attachments, logger);
Vaults = new SyncItemsRepository(dbContext, vaults); Shortcuts = new SyncItemsRepository(dbContext, shortcuts, logger);
Tags = new SyncItemsRepository(dbContext, tags); Reminders = new SyncItemsRepository(dbContext, reminders, logger);
Relations = new SyncItemsRepository(dbContext, relations, logger);
Colors = new SyncItemsRepository(dbContext, colors, logger);
Vaults = new SyncItemsRepository(dbContext, vaults, logger);
Tags = new SyncItemsRepository(dbContext, tags, logger);
InboxItemsHistory = new SyncItemsRepository(dbContext, inboxItemsHistory, logger);
} }
} }
} }
@@ -0,0 +1,101 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Notesnook.API.Models;
using Streetwriters.Data.Repositories;
namespace Notesnook.API.Authorization
{
public static class InboxApiKeyAuthenticationDefaults
{
public const string AuthenticationScheme = "InboxApiKey";
}
public class InboxApiKeyAuthenticationSchemeOptions : AuthenticationSchemeOptions
{
}
public class InboxApiKeyAuthenticationHandler : AuthenticationHandler<InboxApiKeyAuthenticationSchemeOptions>
{
private readonly Repository<InboxApiKey> _inboxApiKeyRepository;
public InboxApiKeyAuthenticationHandler(
IOptionsMonitor<InboxApiKeyAuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
Repository<InboxApiKey> inboxApiKeyRepository)
: base(options, logger, encoder)
{
_inboxApiKeyRepository = inboxApiKeyRepository;
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.ContainsKey("Authorization"))
{
return AuthenticateResult.Fail("Missing Authorization header");
}
var apiKey = Request.Headers["Authorization"].ToString().Trim();
if (string.IsNullOrEmpty(apiKey))
{
return AuthenticateResult.Fail("Missing API key");
}
try
{
var inboxApiKey = await _inboxApiKeyRepository.FindOneAsync(k => k.Key == apiKey);
if (inboxApiKey == null)
{
return AuthenticateResult.Fail("Invalid API key");
}
if (inboxApiKey.ExpiryDate > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() > inboxApiKey.ExpiryDate)
{
return AuthenticateResult.Fail("API key has expired");
}
inboxApiKey.LastUsedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
await _inboxApiKeyRepository.UpsertAsync(inboxApiKey, k => k.Key == apiKey);
var claims = new[]
{
new Claim("sub", inboxApiKey.UserId),
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return AuthenticateResult.Success(ticket);
}
catch (Exception ex)
{
Logger.LogError(ex, "Error validating inbox API key");
return AuthenticateResult.Fail("Error validating API key");
}
}
}
}
@@ -1,36 +1,36 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
namespace Notesnook.API.Authorization namespace Notesnook.API.Authorization
{ {
public class NotesnookUserRequirement : AuthorizationHandler<NotesnookUserRequirement>, IAuthorizationRequirement public class NotesnookUserRequirement : AuthorizationHandler<NotesnookUserRequirement>, IAuthorizationRequirement
{ {
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NotesnookUserRequirement requirement) protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NotesnookUserRequirement requirement)
{ {
var isInAudience = context.User.HasClaim("aud", "notesnook"); var isInAudience = context.User.HasClaim("aud", "notesnook");
var hasRole = context.User.HasClaim("role", "notesnook"); var hasRole = context.User.HasClaim("role", "notesnook");
if (isInAudience && hasRole) if (isInAudience && hasRole)
context.Succeed(requirement); context.Succeed(requirement);
return Task.CompletedTask; return Task.CompletedTask;
} }
} }
} }
@@ -1,63 +0,0 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
namespace Notesnook.API.Authorization
{
public class ProUserRequirement : AuthorizationHandler<ProUserRequirement>, IAuthorizationRequirement
{
private readonly Dictionary<string, string> pathErrorPhraseMap = new()
{
["/s3"] = "upload attachments",
["/s3/multipart"] = "upload attachments",
};
private readonly string[] allowedClaims = ["trial", "premium", "premium_canceled"];
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ProUserRequirement requirement)
{
PathString path = context.Resource is DefaultHttpContext httpContext ? httpContext.Request.Path : null;
var isProOrTrial = context.User.Claims.Any((c) => c.Type == "notesnook:status" && allowedClaims.Contains(c.Value));
if (isProOrTrial) context.Succeed(requirement);
else
{
var phrase = "continue";
foreach (var item in pathErrorPhraseMap)
{
if (path != null && path.StartsWithSegments(item.Key))
phrase = item.Value;
}
var error = $"Please upgrade to Pro to {phrase}.";
context.Fail(new AuthorizationFailureReason(this, error));
}
return Task.CompletedTask;
}
public override Task HandleAsync(AuthorizationHandlerContext context)
{
return this.HandleRequirementAsync(context, this);
}
}
}
@@ -43,16 +43,16 @@ namespace Notesnook.API.Authorization
PathString path = context.Resource is DefaultHttpContext httpContext ? httpContext.Request.Path : null; PathString path = context.Resource is DefaultHttpContext httpContext ? httpContext.Request.Path : null;
var result = this.IsAuthorized(context.User, path); var result = this.IsAuthorized(context.User, path);
if (result.Succeeded) context.Succeed(requirement); if (result.Succeeded) context.Succeed(requirement);
else if (result.AuthorizationFailure.FailureReasons.Any()) else if (result.AuthorizationFailure?.FailureReasons.Any() == true)
context.Fail(result.AuthorizationFailure.FailureReasons.First()); context.Fail(result.AuthorizationFailure.FailureReasons.First());
else context.Fail(); else context.Fail();
return Task.CompletedTask; return Task.CompletedTask;
} }
public PolicyAuthorizationResult IsAuthorized(ClaimsPrincipal User, PathString requestPath) public PolicyAuthorizationResult IsAuthorized(ClaimsPrincipal? User, PathString requestPath)
{ {
var id = User.FindFirstValue("sub"); var id = User?.FindFirstValue("sub");
if (string.IsNullOrEmpty(id)) if (string.IsNullOrEmpty(id))
{ {
@@ -63,11 +63,11 @@ namespace Notesnook.API.Authorization
return PolicyAuthorizationResult.Forbid(AuthorizationFailure.Failed(reason)); return PolicyAuthorizationResult.Forbid(AuthorizationFailure.Failed(reason));
} }
var hasSyncScope = User.HasClaim("scope", "notesnook.sync"); var hasSyncScope = User?.HasClaim("scope", "notesnook.sync") ?? false;
var isInAudience = User.HasClaim("aud", "notesnook"); var isInAudience = User?.HasClaim("aud", "notesnook") ?? false;
var hasRole = User.HasClaim("role", "notesnook"); var hasRole = User?.HasClaim("role", "notesnook") ?? false;
var isEmailVerified = User.HasClaim("verified", "true"); var isEmailVerified = User?.HasClaim("verified", "true") ?? false;
if (!isEmailVerified) if (!isEmailVerified)
{ {
+5
View File
@@ -14,5 +14,10 @@ namespace Notesnook.API
public const string TagsKey = "tags"; public const string TagsKey = "tags";
public const string ColorsKey = "colors"; public const string ColorsKey = "colors";
public const string VaultsKey = "vaults"; public const string VaultsKey = "vaults";
public const string InboxItemsKey = "inbox_items";
public const string InboxApiKeysKey = "inbox_api_keys";
public const string SyncDevicesKey = "sync_devices";
public const string DeviceIdsChunksKey = "device_ids_chunks";
public const string InboxItemsHistoryKey = "inbox_items_history";
} }
} }
@@ -1,69 +1,82 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using AngleSharp.Text;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization;
using MongoDB.Driver; using Microsoft.AspNetCore.Mvc;
using Notesnook.API.Models; using MongoDB.Driver;
using Streetwriters.Data.Repositories; using Notesnook.API.Accessors;
using Notesnook.API.Models;
namespace Notesnook.API.Controllers using Streetwriters.Common;
{ using Streetwriters.Common.Accessors;
// TODO: this should be moved out into its own microservice using Streetwriters.Common.Interfaces;
[ApiController] using Streetwriters.Common.Models;
[Route("announcements")] using Streetwriters.Data.Repositories;
public class AnnouncementController : ControllerBase
{ namespace Notesnook.API.Controllers
private Repository<Announcement> Announcements { get; set; } {
public AnnouncementController(Repository<Announcement> announcements) // TODO: this should be moved out into its own microservice
{ [ApiController]
Announcements = announcements; [Route("announcements")]
} public class AnnouncementController(Repository<Announcement> announcements, WampServiceAccessor serviceAccessor) : ControllerBase
{
[HttpGet("active")] [HttpGet("active")]
[AllowAnonymous] [AllowAnonymous]
public async Task<IActionResult> GetActiveAnnouncements([FromQuery] string userId) public async Task<IActionResult> GetActiveAnnouncements([FromQuery] string? userId)
{ {
var totalActive = await Announcements.Collection.CountDocumentsAsync(Builders<Announcement>.Filter.Eq("IsActive", true)); var filter = Builders<Announcement>.Filter.Eq(x => x.IsActive, true);
if (totalActive <= 0) return Ok(new Announcement[] { }); if (!string.IsNullOrEmpty(userId))
{
var announcements = (await Announcements.FindAsync((a) => a.IsActive)).Where((a) => a.UserIds == null || a.UserIds.Length == 0 || a.UserIds.Contains(userId)); var userFilter = Builders<Announcement>.Filter.Or(
foreach (var announcement in announcements) Builders<Announcement>.Filter.Eq(x => x.UserIds, null),
{ Builders<Announcement>.Filter.Size(x => x.UserIds, 0),
if (announcement.UserIds != null && !announcement.UserIds.Contains(userId)) continue; Builders<Announcement>.Filter.AnyEq(x => x.UserIds, userId)
);
foreach (var item in announcement.Body) filter = Builders<Announcement>.Filter.And(filter, userFilter);
{ }
if (item.Type != "callToActions") continue; var userAnnouncements = await announcements.Collection.Find(filter).ToListAsync();
foreach (var action in item.Actions) foreach (var announcement in userAnnouncements)
{ {
if (action.Type != "link") continue; if (userId != null && announcement.UserIds != null && !announcement.UserIds.Contains(userId)) continue;
action.Data = action.Data.Replace("{{UserId}}", userId ?? "0"); foreach (var item in announcement.Body)
} {
} if (item.Type != "callToActions") continue;
} foreach (var action in item.Actions)
return Ok(announcements); {
} if (action.Type != "link" || action.Data == null) continue;
}
} action.Data = action.Data.Replace("{{UserId}}", userId ?? "");
if (action.Data.Contains("{{Email}}"))
{
var user = string.IsNullOrEmpty(userId) ? null : await serviceAccessor.UserAccountService.GetUserAsync(Clients.Notesnook.Id, userId);
action.Data = action.Data.Replace("{{Email}}", user?.Email ?? "");
}
}
}
}
return Ok(userAnnouncements);
}
}
}
@@ -0,0 +1,191 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using Notesnook.API.Authorization;
using Notesnook.API.Models;
using Notesnook.API.Services;
using Streetwriters.Common;
using Streetwriters.Common.Messages;
using Streetwriters.Data.Repositories;
namespace Notesnook.API.Controllers
{
[ApiController]
[Route("inbox")]
public class InboxController(
Repository<InboxApiKey> inboxApiKeysRepository,
Repository<UserSettings> userSettingsRepository,
Repository<InboxSyncItem> inboxItemsRepository,
SyncDeviceService syncDeviceService,
ILogger<InboxController> logger) : ControllerBase
{
[HttpGet("api-keys")]
[Authorize(Policy = "Notesnook")]
public async Task<IActionResult> GetApiKeysAsync()
{
var userId = User.GetUserId();
try
{
var apiKeys = await inboxApiKeysRepository.FindAsync(t => t.UserId == userId);
return Ok(apiKeys);
}
catch (Exception ex)
{
logger.LogError(ex, "Couldn't get inbox api keys for user {UserId}", userId);
return BadRequest(new { error = ex.Message });
}
}
[HttpPost("api-keys")]
[Authorize(Policy = "Notesnook")]
public async Task<IActionResult> CreateApiKeyAsync([FromBody] CreateInboxApiKeyRequest request)
{
var userId = User.GetUserId();
try
{
if (string.IsNullOrWhiteSpace(request.Name))
{
return BadRequest(new { error = "Api key name is required." });
}
if (request.ExpiryDate == null)
{
return BadRequest(new { error = "Expiry date is required." });
}
var count = await inboxApiKeysRepository.CountAsync(t => t.UserId == userId);
if (count >= 10)
{
return BadRequest(new { error = "Maximum of 10 inbox api keys allowed." });
}
var inboxApiKey = new InboxApiKey
{
UserId = userId,
Name = request.Name,
DateCreated = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
ExpiryDate = request.ExpiryDate,
LastUsedAt = 0
};
await inboxApiKeysRepository.InsertAsync(inboxApiKey);
return Ok(inboxApiKey);
}
catch (Exception ex)
{
logger.LogError(ex, "Couldn't create inbox api key for {UserId}.", userId);
return BadRequest(new { error = ex.Message });
}
}
[HttpDelete("api-keys/{apiKey}")]
[Authorize(Policy = "Notesnook")]
public async Task<IActionResult> DeleteApiKeyAsync(string apiKey)
{
var userId = User.GetUserId();
try
{
if (string.IsNullOrWhiteSpace(apiKey))
{
return BadRequest(new { error = "Api key is required." });
}
await inboxApiKeysRepository.DeleteAsync(t => t.UserId == userId && t.Key == apiKey);
return Ok(new { message = "Api key deleted successfully." });
}
catch (Exception ex)
{
logger.LogError(ex, "Couldn't delete inbox api key for user {UserId}", userId);
return BadRequest(new { error = ex.Message });
}
}
[HttpGet("public-encryption-key")]
[Authorize(Policy = InboxApiKeyAuthenticationDefaults.AuthenticationScheme)]
public async Task<IActionResult> GetPublicKeyAsync()
{
var userId = User.GetUserId();
try
{
var userSetting = await userSettingsRepository.FindOneAsync(u => u.UserId == userId);
if (string.IsNullOrWhiteSpace(userSetting?.InboxKeys?.Public))
{
return NotFound(new { error = "Inbox public key is not configured." });
}
return Ok(new { key = userSetting.InboxKeys.Public });
}
catch (Exception ex)
{
logger.LogError(ex, "Couldn't get user's inbox's public key for user {UserId}", userId);
return BadRequest(new { error = ex.Message });
}
}
[HttpPost("items")]
[Authorize(Policy = InboxApiKeyAuthenticationDefaults.AuthenticationScheme)]
public async Task<IActionResult> CreateInboxItemAsync([FromBody] InboxSyncItem request)
{
var userId = User.GetUserId();
try
{
if (string.IsNullOrWhiteSpace(request.Cipher))
{
return BadRequest(new { error = "Inbox item is required." });
}
if (string.IsNullOrWhiteSpace(request.Algorithm))
{
return BadRequest(new { error = "Inbox item algorithm is required." });
}
if (request.Version <= 0)
{
return BadRequest(new { error = "Valid inbox item version is required." });
}
request.UserId = userId;
request.ItemId = ObjectId.GenerateNewId().ToString();
await inboxItemsRepository.InsertAsync(request);
await syncDeviceService.AddIdsToAllDevicesAsync(userId, [new(request.ItemId, "inbox_item")]);
await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
{
OriginTokenId = null,
UserId = userId,
Message = new Message
{
Type = "triggerSync",
Data = JsonSerializer.Serialize(new { reason = "Inbox items updated." })
}
});
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Couldn't create inbox item for user {UserId}", userId);
return BadRequest(new { error = ex.Message });
}
}
}
}
+478 -70
View File
@@ -20,13 +20,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Text.RegularExpressions; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using AngleSharp;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using NanoidDotNet;
using Notesnook.API.Extensions;
using Notesnook.API.Models; using Notesnook.API.Models;
using Streetwriters.Data.Interfaces; using Notesnook.API.Services;
using Streetwriters.Common;
using Streetwriters.Common.Accessors;
using Streetwriters.Common.Enums;
using Streetwriters.Common.Helpers;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages;
using Streetwriters.Data.Repositories; using Streetwriters.Data.Repositories;
namespace Notesnook.API.Controllers namespace Notesnook.API.Controllers
@@ -34,86 +46,236 @@ namespace Notesnook.API.Controllers
[ApiController] [ApiController]
[Route("monographs")] [Route("monographs")]
[Authorize("Sync")] [Authorize("Sync")]
public class MonographsController : ControllerBase public class MonographsController(Repository<Monograph> monographs, IURLAnalyzer analyzer, SyncDeviceService syncDeviceService, WampServiceAccessor serviceAccessor, ILogger<MonographsController> logger) : ControllerBase
{ {
private Repository<Monograph> Monographs { get; set; } const string SVG_PIXEL = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><circle r='9'/></svg>";
private readonly IUnitOfWork unit;
private const int MAX_DOC_SIZE = 15 * 1024 * 1024; private const int MAX_DOC_SIZE = 15 * 1024 * 1024;
public MonographsController(Repository<Monograph> monographs, IUnitOfWork unitOfWork)
private static FilterDefinition<Monograph> CreateMonographFilter(string userId, Monograph monograph)
{ {
Monographs = monographs; var userIdFilter = Builders<Monograph>.Filter.Eq("UserId", userId);
unit = unitOfWork; monograph.ItemId ??= monograph.Id;
return ObjectId.TryParse(monograph.ItemId, out ObjectId id)
? Builders<Monograph>.Filter
.And(userIdFilter,
Builders<Monograph>.Filter.Or(
Builders<Monograph>.Filter.Eq("_id", id), Builders<Monograph>.Filter.Eq("ItemId", monograph.ItemId)
)
)
: Builders<Monograph>.Filter
.And(userIdFilter,
Builders<Monograph>.Filter.Eq("ItemId", monograph.ItemId)
);
}
private static FilterDefinition<Monograph> CreateMonographFilter(string itemId)
{
return ObjectId.TryParse(itemId, out ObjectId id)
? Builders<Monograph>.Filter.Or(
Builders<Monograph>.Filter.Eq("_id", id),
Builders<Monograph>.Filter.Eq("ItemId", itemId))
: Builders<Monograph>.Filter.Eq("ItemId", itemId);
}
private async Task<Monograph> FindMonographAsync(string userId, Monograph monograph)
{
var result = await monographs.Collection.FindAsync(CreateMonographFilter(userId, monograph), new FindOptions<Monograph>
{
Limit = 1
});
return await result.FirstOrDefaultAsync();
}
private async Task<Monograph> FindMonographAsync(string itemId)
{
var result = await monographs.Collection.FindAsync(CreateMonographFilter(itemId), new FindOptions<Monograph>
{
Limit = 1
});
return await result.FirstOrDefaultAsync();
}
private async Task<Monograph> FindMonographBySlugAsync(string slug)
{
var result = await monographs.Collection.FindAsync(
Builders<Monograph>.Filter.Eq("Slug", slug), new FindOptions<Monograph>
{
Limit = 1
});
return await result.FirstOrDefaultAsync();
}
private async Task<string> GenerateUniqueSlugAsync(int length = 10, int maxAttempts = 5)
{
for (var i = 0; i < maxAttempts; i++)
{
var slug = Nanoid.Generate(size: length);
var exists = await monographs.Collection.Find(Builders<Monograph>.Filter.Eq("Slug", slug))
.Limit(1)
.AnyAsync();
if (!exists) return slug;
}
throw new Exception("Failed to generate unique slug");
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> PublishAsync([FromBody] Monograph monograph) public async Task<IActionResult> PublishAsync([FromQuery] string? deviceId, [FromBody] Monograph monograph)
{ {
var userId = this.User.FindFirstValue("sub"); try
if (userId == null) return Unauthorized();
if (await Monographs.GetAsync(monograph.Id) != null) return base.Conflict("This monograph is already published.");
if (monograph.EncryptedContent == null)
monograph.CompressedContent = monograph.Content.CompressBrotli();
monograph.UserId = userId;
monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (monograph.EncryptedContent?.Cipher.Length > MAX_DOC_SIZE || monograph.CompressedContent?.Length > MAX_DOC_SIZE)
return base.BadRequest("Monograph is too big. Max allowed size is 15mb.");
Monographs.Insert(monograph);
if (!await unit.Commit()) return BadRequest();
return Ok(new
{ {
id = monograph.Id var userId = this.User.GetUserId();
}); var jti = this.User.FindFirstValue("jti");
var existingMonograph = await FindMonographAsync(userId, monograph);
if (existingMonograph != null && !existingMonograph.Deleted) return await UpdateAsync(deviceId, monograph);
monograph = await CreateMonographAsync(monograph, userId);
if (existingMonograph != null)
{
monograph.Id = existingMonograph.Id;
}
await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(userId, monograph),
monograph,
new ReplaceOptions { IsUpsert = true }
);
await MarkMonographForSyncAsync(userId, monograph.ItemId ?? monograph.Id, deviceId, jti);
return Ok(new
{
id = monograph.ItemId,
datePublished = monograph.DatePublished,
});
}
catch (Exception e)
{
logger.LogError(e, "Failed to publish monograph");
return BadRequest(new { error = e.Message });
}
}
[HttpPost("v2")]
public async Task<IActionResult> PublishV2Async([FromQuery] string? deviceId, [FromBody] Monograph monograph)
{
try
{
var userId = this.User.GetUserId();
var jti = this.User.FindFirstValue("jti");
var existingMonograph = await FindMonographAsync(userId, monograph);
if (existingMonograph != null && !existingMonograph.Deleted) return await UpdateAsync(deviceId, monograph);
monograph = await CreateMonographAsync(monograph, userId);
monograph.Slug = await GenerateUniqueSlugAsync();
if (existingMonograph != null)
{
monograph.Id = existingMonograph.Id;
}
await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(userId, monograph),
monograph,
new ReplaceOptions { IsUpsert = true }
);
await MarkMonographForSyncAsync(userId, monograph.ItemId ?? monograph.Id, deviceId, jti);
return Ok(new
{
id = monograph.ItemId,
datePublished = monograph.DatePublished,
publishUrl = Helpers.UrlHelper.ConstructPublishUrl(monograph)
});
}
catch (Exception e)
{
logger.LogError(e, "Failed to publish monograph");
return BadRequest(new { error = e.Message });
}
} }
[HttpPatch] [HttpPatch]
public async Task<IActionResult> UpdateAsync([FromBody] Monograph monograph) public async Task<IActionResult> UpdateAsync([FromQuery] string? deviceId, [FromBody] Monograph monograph)
{ {
if (await Monographs.GetAsync(monograph.Id) == null) return NotFound(); try
if (monograph.EncryptedContent?.Cipher.Length > MAX_DOC_SIZE || monograph.CompressedContent?.Length > MAX_DOC_SIZE)
return base.BadRequest("Monograph is too big. Max allowed size is 15mb.");
if (monograph.EncryptedContent == null)
monograph.CompressedContent = monograph.Content.CompressBrotli();
else
monograph.Content = null;
monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Monographs.Update(monograph.Id, monograph);
if (!await unit.Commit()) return BadRequest();
return Ok(new
{ {
id = monograph.Id var userId = this.User.GetUserId();
}); var jti = this.User.FindFirstValue("jti");
var existingMonograph = await FindMonographAsync(userId, monograph);
if (existingMonograph == null || existingMonograph.Deleted)
{
return NotFound();
}
if (monograph.EncryptedContent?.Cipher.Length > MAX_DOC_SIZE || monograph.CompressedContent?.Length > MAX_DOC_SIZE)
return base.BadRequest("Monograph is too big. Max allowed size is 15mb.");
var sanitizationLevel = ContentSanitizationLevel.Unknown;
if (monograph.EncryptedContent == null)
{
sanitizationLevel = User.IsUserSubscribed() ? ContentSanitizationLevel.Partial : ContentSanitizationLevel.Full;
monograph.CompressedContent = (await SanitizeContentAsync(monograph.Content, sanitizationLevel)).CompressBrotli();
}
else
monograph.Content = null;
monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var result = await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(userId, monograph),
Builders<Monograph>.Update
.Set(m => m.DatePublished, monograph.DatePublished)
.Set(m => m.CompressedContent, monograph.CompressedContent)
.Set(m => m.EncryptedContent, monograph.EncryptedContent)
.Set(m => m.SelfDestruct, monograph.SelfDestruct)
.Set(m => m.Title, monograph.Title)
.Set(m => m.Password, monograph.Password)
.Set(m => m.ContentSanitizationLevel, sanitizationLevel)
);
if (!result.IsAcknowledged) return BadRequest();
await MarkMonographForSyncAsync(userId, monograph.ItemId ?? monograph.Id, deviceId, jti);
return Ok(new
{
id = monograph.ItemId,
datePublished = monograph.DatePublished,
publishUrl = Helpers.UrlHelper.ConstructPublishUrl(existingMonograph)
});
}
catch (Exception e)
{
logger.LogError(e, "Failed to update monograph");
return BadRequest(new { error = e.Message });
}
} }
[HttpGet] [HttpGet]
public async Task<IActionResult> GetUserMonographsAsync() public async Task<IActionResult> GetUserMonographsAsync()
{ {
var userId = this.User.FindFirstValue("sub"); var userId = this.User.GetUserId();
if (userId == null) return Unauthorized();
var monographs = (await Monographs.Collection.FindAsync(Builders<Monograph>.Filter.Eq("UserId", userId), new FindOptions<Monograph, ObjectWithId> var userMonographs = (await monographs.Collection.FindAsync(
{ Builders<Monograph>.Filter.And(
Projection = Builders<Monograph>.Projection.Include("_id"), Builders<Monograph>.Filter.Eq("UserId", userId),
})).ToEnumerable(); Builders<Monograph>.Filter.Ne("Deleted", true)
return Ok(monographs.Select((m) => m.Id)); )
, new FindOptions<Monograph, ObjectWithId>
{
Projection = Builders<Monograph>.Projection.Include("_id").Include("ItemId"),
})).ToEnumerable();
return Ok(userMonographs.Select((m) => m.ItemId ?? m.Id));
} }
[HttpGet("{id}")] [HttpGet("{id}")]
[AllowAnonymous] [AllowAnonymous]
public async Task<IActionResult> GetMonographAsync([FromRoute] string id) public async Task<IActionResult> GetMonographAsync([FromRoute] string id)
{ {
var monograph = await Monographs.GetAsync(id); var monograph = await FindMonographAsync(id);
if (monograph == null) if (monograph == null || monograph.Deleted)
{ {
return NotFound(new return NotFound(new
{ {
@@ -122,17 +284,42 @@ namespace Notesnook.API.Controllers
}); });
} }
if (monograph.EncryptedContent == null) return Ok(await ProcessMonographAsync(monograph));
monograph.Content = monograph.CompressedContent.DecompressBrotli();
return Ok(monograph);
} }
[HttpGet("{id}/destruct")] [HttpGet("{id}/view")]
[AllowAnonymous] [AllowAnonymous]
public async Task<IActionResult> DestructMonographAsync([FromRoute] string id) public async Task<IActionResult> TrackView([FromRoute] string id)
{ {
var monograph = await Monographs.GetAsync(id); var monograph = await FindMonographAsync(id);
if (monograph == null) if (monograph == null || monograph.Deleted)
return Content(SVG_PIXEL, "image/svg+xml");
var cookieName = $"viewed_{id}";
await TrackViewAsync(monograph, cookieName, $"/monographs/{id}");
return Content(SVG_PIXEL, "image/svg+xml");
}
[HttpGet("v2/{slug}/view")]
[AllowAnonymous]
public async Task<IActionResult> TrackViewV2([FromRoute] string slug)
{
var monograph = await FindMonographBySlugAsync(slug);
if (monograph == null || monograph.Deleted)
return Content(SVG_PIXEL, "image/svg+xml");
var cookieName = $"viewed_{slug}";
await TrackViewAsync(monograph, cookieName, $"/monographs/v2/{slug}");
return Content(SVG_PIXEL, "image/svg+xml");
}
[HttpGet("v2/{slug}")]
[AllowAnonymous]
public async Task<IActionResult> GetMonographBySlugAsync([FromRoute] string slug)
{
var monograph = await FindMonographBySlugAsync(slug);
if (monograph == null || monograph.Deleted)
{ {
return NotFound(new return NotFound(new
{ {
@@ -141,18 +328,239 @@ namespace Notesnook.API.Controllers
}); });
} }
if (monograph.SelfDestruct) return Ok(await ProcessMonographAsync(monograph));
await Monographs.DeleteByIdAsync(monograph.Id); }
return Ok(); [HttpGet("{id}/analytics")]
[Obsolete("This endpoint is deprecated and will be removed in future versions. Use GET /monographs/{id}/metadata instead.")]
public async Task<IActionResult> GetMonographAnalyticsAsync([FromRoute] string id)
{
if (!FeatureAuthorizationHelper.IsFeatureAllowed(Features.MONOGRAPH_ANALYTICS, Clients.Notesnook.Id, User))
return BadRequest(new { error = "Monograph analytics are only available on the Pro & Believer plans." });
var userId = this.User.GetUserId();
var monograph = await FindMonographAsync(id);
if (monograph == null || monograph.Deleted || monograph.UserId != userId)
{
return NotFound();
}
return Ok(new { totalViews = monograph.ViewCount });
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync([FromRoute] string id) public async Task<IActionResult> DeleteAsync([FromQuery] string? deviceId, [FromRoute] string id)
{ {
Monographs.DeleteById(id); var userId = this.User.GetUserId();
if (!await unit.Commit()) return BadRequest();
var monograph = await FindMonographAsync(id);
if (monograph == null || monograph.Deleted)
return Ok();
var jti = this.User.FindFirstValue("jti");
await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(userId, monograph),
new Monograph
{
ItemId = id,
Id = monograph.Id,
Deleted = true,
UserId = monograph.UserId,
ViewCount = 0
}
);
await MarkMonographForSyncAsync(userId, id, deviceId, jti);
return Ok(); return Ok();
} }
[HttpGet("{id}/metadata")]
public async Task<IActionResult> GetMetadataAsync([FromRoute] string id)
{
var userId = this.User.GetUserId();
var monograph = await FindMonographAsync(id);
if (monograph == null || monograph.Deleted || monograph.UserId != userId)
{
return NotFound();
}
var isPro = FeatureAuthorizationHelper.IsFeatureAllowed(Features.MONOGRAPH_ANALYTICS, Clients.Notesnook.Id, User);
var totalViews = isPro ? monograph.ViewCount : 0;
return Ok(new
{
publishUrl = Helpers.UrlHelper.ConstructPublishUrl(monograph),
analytics = new
{
totalViews
}
});
}
private async Task MarkMonographForSyncAsync(string userId, string monographId, string? deviceId, string? jti)
{
if (deviceId == null) return;
await syncDeviceService.AddIdsToOtherDevicesAsync(userId, deviceId, [new(monographId, "monograph")]);
}
private async Task MarkMonographForSyncAsync(string userId, string monographId)
{
await syncDeviceService.AddIdsToAllDevicesAsync(userId, [new(monographId, "monograph")]);
}
// (selector, url-bearing attribute) pairs to inspect
private static readonly (string Selector, string Attribute)[] urlElements =
[
("a", "href"),
("img", "src"),
("iframe", "src"),
("embed", "src"),
("object", "data"),
("source", "src"),
("video", "src"),
("audio", "src"),
];
private async Task<Monograph> CreateMonographAsync(Monograph monograph, string userId)
{
if (monograph.EncryptedContent == null)
{
var sanitizationLevel = User.IsUserSubscribed() ? ContentSanitizationLevel.Partial : ContentSanitizationLevel.Full;
monograph.CompressedContent = (await SanitizeContentAsync(monograph.Content, sanitizationLevel)).CompressBrotli();
monograph.ContentSanitizationLevel = sanitizationLevel;
}
monograph.UserId = userId;
monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (monograph.EncryptedContent?.Cipher.Length > MAX_DOC_SIZE || monograph.CompressedContent?.Length > MAX_DOC_SIZE)
throw new Exception("Monograph is too big. Max allowed size is 15mb.");
monograph.Deleted = false;
monograph.ViewCount = 0;
return monograph;
}
private async Task TrackViewAsync(Monograph monograph, string cookieName, string cookiePath)
{
var hasVisitedBefore = Request.Cookies.ContainsKey(cookieName);
if (monograph.SelfDestruct)
{
await monographs.Collection.ReplaceOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
new Monograph
{
ItemId = monograph.ItemId,
Id = monograph.Id,
Deleted = true,
UserId = monograph.UserId,
ViewCount = 0
}
);
await MarkMonographForSyncAsync(monograph.UserId!, monograph.ItemId ?? monograph.Id);
}
else if (!hasVisitedBefore)
{
await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
Builders<Monograph>.Update.Inc(m => m.ViewCount, 1)
);
var cookieOptions = new CookieOptions
{
Path = cookiePath,
HttpOnly = true,
Secure = Request.IsHttps,
Expires = DateTimeOffset.UtcNow.AddMonths(1)
};
Response.Cookies.Append(cookieName, "1", cookieOptions);
}
}
private async Task<Monograph> ProcessMonographAsync(Monograph monograph)
{
if (monograph.EncryptedContent == null)
{
var isContentUnsanitized = monograph.ContentSanitizationLevel == ContentSanitizationLevel.Partial || monograph.ContentSanitizationLevel == ContentSanitizationLevel.Unknown;
if (!Constants.IS_SELF_HOSTED && isContentUnsanitized && serviceAccessor.UserSubscriptionService != null && !await serviceAccessor.UserSubscriptionService.IsUserSubscribedAsync(Clients.Notesnook.Id, monograph.UserId!))
{
var cleaned = await SanitizeContentAsync(monograph.CompressedContent?.DecompressBrotli(), ContentSanitizationLevel.Full);
monograph.CompressedContent = cleaned.CompressBrotli();
await monographs.Collection.UpdateOneAsync(
CreateMonographFilter(monograph.UserId!, monograph),
Builders<Monograph>.Update
.Set(m => m.CompressedContent, monograph.CompressedContent)
.Set(m => m.ContentSanitizationLevel, ContentSanitizationLevel.Full)
);
}
monograph.Content = monograph.CompressedContent?.DecompressBrotli();
}
monograph.ItemId ??= monograph.Id;
return monograph;
}
private async Task<string> SanitizeContentAsync(string? content, ContentSanitizationLevel level)
{
if (string.IsNullOrEmpty(content)) return string.Empty;
if (Constants.IS_SELF_HOSTED) return content;
try
{
var json = JsonSerializer.Deserialize<MonographContent>(content) ?? throw new Exception("Invalid monograph content.");
var html = json.Data;
if (level == ContentSanitizationLevel.Partial)
{
var config = Configuration.Default.WithDefaultLoader();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync(r => r.Content(html));
foreach (var (selector, attribute) in urlElements)
{
foreach (var element in document.QuerySelectorAll(selector))
{
var url = element.GetAttribute(attribute);
if (string.IsNullOrEmpty(url)) continue;
if (!await analyzer.IsURLSafeAsync(url))
{
logger.LogInformation("Malicious URL detected in <{Selector} {Attribute}>: {Url}", selector, attribute, url);
element.RemoveAttribute(attribute);
}
}
}
html = document.ToHtml();
}
else if (level == ContentSanitizationLevel.Full)
{
var config = Configuration.Default.WithDefaultLoader();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync(r => r.Content(html));
foreach (var element in document.QuerySelectorAll("a,iframe,img,object,svg,button,link"))
{
foreach (var attr in element.Attributes.ToList())
element.RemoveAttribute(attr.Name);
}
html = document.ToHtml();
}
return JsonSerializer.Serialize<MonographContent>(new MonographContent
{
Type = json.Type,
Data = html
});
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to cleanup monograph content");
return content;
}
}
} }
} }
+195 -88
View File
@@ -17,111 +17,218 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Amazon.S3.Model;
using System.Threading.Tasks;
using System.Security.Claims;
using Notesnook.API.Interfaces;
using System; using System;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Notesnook.API.Helpers;
using Notesnook.API.Interfaces;
using Notesnook.API.Models;
using Streetwriters.Common;
using Streetwriters.Common.Accessors;
using Streetwriters.Common.Extensions;
using Streetwriters.Common.Models;
namespace Notesnook.API.Controllers namespace Notesnook.API.Controllers
{ {
[ApiController] [ApiController]
[Route("s3")] [Route("s3")]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] [ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public class S3Controller : ControllerBase [Authorize("Sync")]
public class S3Controller(IS3Service s3Service, ISyncItemsRepositoryAccessor repositories, WampServiceAccessor serviceAccessor, ILogger<S3Controller> logger) : ControllerBase
{ {
private IS3Service S3Service { get; set; }
public S3Controller(IS3Service s3Service)
{
S3Service = s3Service;
}
[HttpPut] [HttpPut]
[Authorize("Pro")] public async Task<IActionResult> Upload([FromQuery] string name)
public IActionResult Upload([FromQuery] string name)
{
var userId = this.User.FindFirstValue("sub");
var url = S3Service.GetUploadObjectUrl(userId, name);
if (url == null) return BadRequest("Could not create signed url.");
return Ok(url);
}
[HttpGet("multipart")]
[Authorize("Pro")]
public async Task<IActionResult> MultipartUpload([FromQuery] string name, [FromQuery] int parts, [FromQuery] string uploadId)
{
var userId = this.User.FindFirstValue("sub");
try
{
var meta = await S3Service.StartMultipartUploadAsync(userId, name, parts, uploadId);
return Ok(meta);
}
catch (Exception ex) { return BadRequest(ex.Message); }
}
[HttpDelete("multipart")]
[Authorize("Pro")]
public async Task<IActionResult> AbortMultipartUpload([FromQuery] string name, [FromQuery] string uploadId)
{
var userId = this.User.FindFirstValue("sub");
try
{
await S3Service.AbortMultipartUploadAsync(userId, name, uploadId);
return Ok();
}
catch (Exception ex) { return BadRequest(ex.Message); }
}
[HttpPost("multipart")]
[Authorize("Pro")]
public async Task<IActionResult> CompleteMultipartUpload([FromBody] CompleteMultipartUploadRequest uploadRequest)
{
var userId = this.User.FindFirstValue("sub");
try
{
await S3Service.CompleteMultipartUploadAsync(userId, uploadRequest);
return Ok();
}
catch (Exception ex) { return BadRequest(ex.Message); }
}
[HttpGet]
[Authorize("Sync")]
public IActionResult Download([FromQuery] string name)
{
var userId = this.User.FindFirstValue("sub");
var url = S3Service.GetDownloadObjectUrl(userId, name);
if (url == null) return BadRequest("Could not create signed url.");
return Ok(url);
}
[HttpHead]
[Authorize("Sync")]
public async Task<IActionResult> Info([FromQuery] string name)
{
var userId = this.User.FindFirstValue("sub");
var size = await S3Service.GetObjectSizeAsync(userId, name);
HttpContext.Response.Headers.ContentLength = size;
return Ok();
}
[HttpDelete]
[Authorize("Sync")]
public async Task<IActionResult> DeleteAsync([FromQuery] string name)
{ {
try try
{ {
var userId = this.User.FindFirstValue("sub"); var userId = this.User.GetUserId();
await S3Service.DeleteObjectAsync(userId, name);
var fileSize = HttpContext.Request.ContentLength ?? 0;
bool hasBody = fileSize > 0;
if (!hasBody)
{
return Ok(Request.GetEncodedUrl() + "&access_token=" + Request.Headers.Authorization.ToString().Replace("Bearer ", ""));
}
if (Constants.IS_SELF_HOSTED) await UploadFileAsync(userId, name, fileSize);
else await UploadFileWithChecksAsync(userId, name, fileSize);
return Ok(); return Ok();
} }
catch (Exception ex) catch (Exception ex)
{ {
return BadRequest(ex.Message); logger.LogError(ex, "Error uploading attachment for user.");
return BadRequest(new { error = "Failed to upload attachment." });
} }
} }
private async Task UploadFileWithChecksAsync(string userId, string name, long fileSize)
{
var userSettings = await repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId);
var subscription = await serviceAccessor.UserSubscriptionService.GetUserSubscriptionAsync(Clients.Notesnook.Id, userId) ?? throw new Exception("User subscription not found.");
if (StorageHelper.IsFileSizeExceeded(subscription, fileSize))
throw new Exception("Max file size exceeded.");
userSettings.StorageLimit = StorageHelper.RolloverStorageLimit(userSettings.StorageLimit);
if (StorageHelper.IsStorageLimitReached(subscription, userSettings.StorageLimit.Value + fileSize))
throw new Exception("Storage limit exceeded.");
var uploadedFileSize = await UploadFileAsync(userId, name, fileSize);
userSettings.StorageLimit.Value += uploadedFileSize;
await repositories.UsersSettings.Collection.UpdateOneAsync(
Builders<UserSettings>.Filter.Eq(u => u.UserId, userId),
Builders<UserSettings>.Update.Set(u => u.StorageLimit, userSettings.StorageLimit)
);
// extra check in case user sets wrong ContentLength in the HTTP header
if (uploadedFileSize != fileSize && StorageHelper.IsStorageLimitReached(subscription, userSettings.StorageLimit.Value))
{
await s3Service.DeleteObjectAsync(userId, name);
throw new Exception("Storage limit exceeded.");
}
}
private async Task<long> UploadFileAsync(string userId, string name, long fileSize)
{
var url = await s3Service.GetInternalUploadObjectUrlAsync(userId, name) ?? throw new Exception("Could not create signed url.");
var httpClient = new HttpClient();
var content = new StreamContent(HttpContext.Request.BodyReader.AsStream());
content.Headers.ContentLength = fileSize;
var response = await httpClient.SendRequestAsync<Response>(url, null, HttpMethod.Put, content);
if (!response.Success) throw new Exception(response.Content != null ? await response.Content.ReadAsStringAsync() : "Could not upload file.");
return await s3Service.GetObjectSizeAsync(userId, name);
}
[HttpGet("multipart")]
public async Task<IActionResult> MultipartUpload([FromQuery] string name, [FromQuery] int parts, [FromQuery] string? uploadId)
{
var userId = this.User.GetUserId();
try
{
var meta = await s3Service.StartMultipartUploadAsync(userId, name, parts, uploadId);
return Ok(meta);
}
catch (Exception ex)
{
logger.LogError(ex, "Error starting multipart upload for user.");
return BadRequest(new { error = "Failed to start multipart upload." });
}
}
[HttpDelete("multipart")]
public async Task<IActionResult> AbortMultipartUpload([FromQuery] string name, [FromQuery] string uploadId)
{
var userId = this.User.GetUserId();
try
{
await s3Service.AbortMultipartUploadAsync(userId, name, uploadId);
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Error aborting multipart upload for user.");
return BadRequest(new { error = "Failed to abort multipart upload." });
}
}
[HttpPost("multipart")]
public async Task<IActionResult> CompleteMultipartUpload([FromBody] CompleteMultipartUploadRequestWrapper uploadRequestWrapper)
{
var userId = this.User.GetUserId();
try
{
await s3Service.CompleteMultipartUploadAsync(userId, uploadRequestWrapper.ToRequest());
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Error completing multipart upload for user.");
return BadRequest(new { error = "Failed to complete multipart upload." });
}
}
[HttpGet]
public async Task<IActionResult> Download([FromQuery] string name)
{
try
{
var userId = this.User.GetUserId();
var url = await s3Service.GetDownloadObjectUrlAsync(userId, name);
if (url == null) return BadRequest("Could not create signed url.");
return Ok(url);
}
catch (Exception ex)
{
logger.LogError(ex, "Error generating download url for user.");
return BadRequest(new { error = "Failed to get attachment url." });
}
}
[HttpHead]
public async Task<IActionResult> Info([FromQuery] string name)
{
try
{
var userId = this.User.GetUserId();
var size = await s3Service.GetObjectSizeAsync(userId, name); Response.Headers.ContentLength = size;
Response.Headers["X-Object-Size"] = size.ToString();
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Error getting object info for user.");
return BadRequest(new { error = "Failed to get attachment info." });
}
}
[HttpDelete]
public async Task<IActionResult> DeleteAsync([FromQuery] string name)
{
try
{
var userId = this.User.GetUserId();
await s3Service.DeleteObjectAsync(userId, name);
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Error deleting object for user.");
return BadRequest(new { error = "Failed to delete attachment." });
}
}
// [HttpPost("bulk-delete")]
// public async Task<IActionResult> DeleteBulkAsync([FromBody] DeleteBulkObjectsRequest request)
// {
// try
// {
// if (request.Names == null || request.Names.Length == 0)
// {
// return BadRequest(new { error = "No files specified for deletion." });
// }
// var userId = this.User.GetUserId();
// await s3Service.DeleteObjectsAsync(userId, request.Names);
// return Ok();
// }
// catch (Exception ex)
// {
// logger.LogError(ex, "Error deleting objects for user.");
// return BadRequest(new { error = "Failed to delete attachments." });
// }
// }
} }
} }
@@ -24,6 +24,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Models.Responses; using Notesnook.API.Models.Responses;
using Notesnook.API.Services; using Notesnook.API.Services;
@@ -36,20 +37,20 @@ namespace Notesnook.API.Controllers
[ApiController] [ApiController]
[Authorize] [Authorize]
[Route("devices")] [Route("devices")]
public class SyncDeviceController : ControllerBase public class SyncDeviceController(SyncDeviceService syncDeviceService, ILogger<SyncDeviceController> logger) : ControllerBase
{ {
[HttpPost] [HttpPost]
public async Task<IActionResult> RegisterDevice([FromQuery] string deviceId) public async Task<IActionResult> RegisterDevice([FromQuery] string deviceId)
{ {
try try
{ {
var userId = this.User.FindFirstValue("sub"); var userId = this.User.GetUserId();
new SyncDeviceService(new SyncDevice(ref userId, ref deviceId)).RegisterDevice(); await syncDeviceService.RegisterDeviceAsync(userId, deviceId);
return Ok(); return Ok();
} }
catch (Exception ex) catch (Exception ex)
{ {
await Slogger<UsersController>.Error(nameof(UnregisterDevice), "Couldn't register device.", ex.ToString()); logger.LogError(ex, "Failed to register device: {DeviceId}", deviceId);
return BadRequest(new { error = ex.Message }); return BadRequest(new { error = ex.Message });
} }
} }
@@ -60,13 +61,13 @@ namespace Notesnook.API.Controllers
{ {
try try
{ {
var userId = this.User.FindFirstValue("sub"); var userId = this.User.GetUserId();
new SyncDeviceService(new SyncDevice(ref userId, ref deviceId)).UnregisterDevice(); await syncDeviceService.UnregisterDeviceAsync(userId, deviceId);
return Ok(); return Ok();
} }
catch (Exception ex) catch (Exception ex)
{ {
await Slogger<UsersController>.Error(nameof(UnregisterDevice), "Couldn't unregister device.", ex.ToString()); logger.LogError(ex, "Failed to unregister device: {DeviceId}", deviceId);
return BadRequest(new { error = ex.Message }); return BadRequest(new { error = ex.Message });
} }
} }
+159 -116
View File
@@ -1,116 +1,159 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Security.Claims; using System.Net.Http;
using System.Threading.Tasks; using System.Security.Claims;
using Microsoft.AspNetCore.Authorization; using System.Text.Json;
using Microsoft.AspNetCore.Http.Timeouts; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization;
using Notesnook.API.Interfaces; using Microsoft.AspNetCore.Http.Timeouts;
using Notesnook.API.Models; using Microsoft.AspNetCore.Mvc;
using Notesnook.API.Models.Responses; using Microsoft.Extensions.Logging;
using Streetwriters.Common; using Notesnook.API.Interfaces;
using Notesnook.API.Models;
namespace Notesnook.API.Controllers using Notesnook.API.Models.Responses;
{ using Streetwriters.Common;
[ApiController] using Streetwriters.Common.Accessors;
[Authorize] using Streetwriters.Common.Extensions;
[Route("users")] using Streetwriters.Common.Messages;
public class UsersController(IUserService UserService) : ControllerBase using Streetwriters.Common.Models;
{
[HttpPost] namespace Notesnook.API.Controllers
[AllowAnonymous] {
public async Task<IActionResult> Signup() [ApiController]
{ [Authorize]
try [Route("users")]
{ public class UsersController(IUserService UserService, WampServiceAccessor serviceAccessor, ILogger<UsersController> logger) : ControllerBase
await UserService.CreateUserAsync(); {
return Ok(); [HttpPost]
} [AllowAnonymous]
catch (Exception ex) public async Task<IActionResult> Signup([FromForm] SignupForm form)
{ {
await Slogger<UsersController>.Error(nameof(Signup), "Couldn't sign up.", ex.ToString()); try
return BadRequest(new { error = ex.Message }); {
} return Ok(await UserService.CreateUserAsync(form));
} }
catch (Exception ex)
[HttpGet] {
public async Task<IActionResult> GetUser() logger.LogError(ex, "Failed to sign up user");
{ return BadRequest(new { error = ex.Message });
var userId = User.FindFirstValue("sub"); }
try }
{
UserResponse response = await UserService.GetUserAsync(userId); [HttpGet]
if (!response.Success) return BadRequest(response); public async Task<IActionResult> GetUser()
return Ok(response); {
} var userId = User.GetUserId();
catch (Exception ex) try
{ {
await Slogger<UsersController>.Error(nameof(GetUser), "Couldn't get user for id.", userId, ex.ToString()); UserResponse response = await UserService.GetUserAsync(userId);
return BadRequest(new { error = ex.Message }); if (!response.Success) return BadRequest();
} return Ok(response);
} }
catch (Exception ex)
[HttpPatch] {
public async Task<IActionResult> UpdateUser([FromBody] UserResponse user) logger.LogError(ex, "Failed to get user with id: {UserId}", userId);
{ return BadRequest(new { error = ex.Message });
var userId = User.FindFirstValue("sub"); }
try }
{
if (user.AttachmentsKey != null) [HttpPatch]
await UserService.SetUserAttachmentsKeyAsync(userId, user.AttachmentsKey); public async Task<IActionResult> UpdateUser([FromBody] UserKeys keys)
return Ok(); {
} var userId = User.GetUserId();
catch (Exception ex) try
{ {
await Slogger<UsersController>.Error(nameof(GetUser), "Couldn't update user with id.", userId, ex.ToString()); await UserService.SetUserKeysAsync(userId, keys);
return BadRequest(new { error = ex.Message }); return Ok();
} }
} catch (Exception ex)
{
[HttpPost("reset")] logger.LogError(ex, "Failed to update user with id: {UserId}", userId);
public async Task<IActionResult> Reset([FromForm] bool removeAttachments) return BadRequest(new { error = ex.Message });
{ }
var userId = this.User.FindFirstValue("sub"); }
if (await UserService.ResetUserAsync(userId, removeAttachments)) [HttpPatch("password/{type}")]
return Ok(); public async Task<IActionResult> ChangePassword([FromRoute] string type, [FromBody] ChangePasswordForm form)
return BadRequest(); {
} return BadRequest(new { error = "Password change is currently disabled." });
// var userId = User.GetUserId();
[HttpPost("delete")] // var clientId = User.FindFirstValue("client_id");
[RequestTimeout(5 * 60 * 1000)] // var jti = User.FindFirstValue("jti");
public async Task<IActionResult> Delete([FromForm] DeleteAccountForm form) // var isPasswordReset = type == "reset";
{ // try
var userId = this.User.FindFirstValue("sub"); // {
var jti = User.FindFirstValue("jti"); // var result = isPasswordReset ? await serviceAccessor.UserAccountService.ResetPasswordAsync(userId, form.NewPassword) : await serviceAccessor.UserAccountService.ChangePasswordAsync(userId, form.OldPassword, form.NewPassword);
try // if (!result)
{ // return BadRequest("Failed to change password.");
await UserService.DeleteUserAsync(userId, jti, form.Password);
return Ok(); // await UserService.SetUserKeysAsync(userId, form.UserKeys);
}
catch (Exception ex) // await serviceAccessor.UserAccountService.ClearSessionsAsync(userId, clientId, all: false, jti, null);
{
await Slogger<UsersController>.Error(nameof(GetUser), "Couldn't delete user with id.", userId, ex.ToString()); // await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
return BadRequest(new { error = ex.Message }); // {
} // UserId = userId,
} // OriginTokenId = jti,
} // Message = new Message
} // {
// Type = "logout",
// Data = JsonSerializer.Serialize(new { reason = "Password changed." })
// }
// });
// return Ok();
// }
// catch (Exception ex)
// {
// logger.LogError(ex, "Failed to change password");
// return BadRequest(new { error = ex.Message });
// }
}
[HttpPost("reset")]
public async Task<IActionResult> Reset([FromForm] bool removeAttachments)
{
var userId = this.User.GetUserId();
if (await UserService.ResetUserAsync(userId, removeAttachments))
return Ok();
return BadRequest();
}
[HttpPost("delete")]
[RequestTimeout(5 * 60 * 1000)]
public async Task<IActionResult> Delete([FromForm] DeleteAccountForm form)
{
var userId = this.User.GetUserId();
var jti = User.FindFirstValue("jti");
try
{
await UserService.DeleteUserAsync(userId, jti, form.Password);
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to delete user with id: {UserId}", userId);
return BadRequest(new { error = ex.Message });
}
}
}
}
+47 -49
View File
@@ -1,50 +1,48 @@
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine AS base FROM mcr.microsoft.com/dotnet/runtime-deps:9.0-alpine AS base
WORKDIR /app WORKDIR /app
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:9.0-alpine AS build
ARG TARGETARCH ARG TARGETARCH
ARG BUILDPLATFORM ARG BUILDPLATFORM
ENV DOTNET_TC_QuickJitForLoops="1" DOTNET_ReadyToRun="0" DOTNET_TieredPGO="1" DOTNET_SYSTEM_GLOBALIZATION_INVARIANT="true" ENV DOTNET_TC_QuickJitForLoops="1" DOTNET_ReadyToRun="0" DOTNET_TieredPGO="1" DOTNET_SYSTEM_GLOBALIZATION_INVARIANT="true"
WORKDIR /src WORKDIR /src
COPY Streetwriters.Data/*.csproj ./Streetwriters.Data/ COPY Streetwriters.Data/*.csproj ./Streetwriters.Data/
COPY Streetwriters.Common/*.csproj ./Streetwriters.Common/ COPY Streetwriters.Common/*.csproj ./Streetwriters.Common/
COPY Notesnook.API/*.csproj ./Notesnook.API/ COPY Notesnook.API/*.csproj ./Notesnook.API/
# restore dependencies # restore dependencies
RUN dotnet restore -v d /src/Notesnook.API/Notesnook.API.csproj --use-current-runtime RUN dotnet restore -v d /src/Notesnook.API/Notesnook.API.csproj --use-current-runtime
COPY Streetwriters.Data/ ./Streetwriters.Data/ COPY Streetwriters.Data/ ./Streetwriters.Data/
COPY Streetwriters.Common/ ./Streetwriters.Common/ COPY Streetwriters.Common/ ./Streetwriters.Common/
COPY Notesnook.API/ ./Notesnook.API/ COPY Notesnook.API/ ./Notesnook.API/
WORKDIR /src/Notesnook.API/ WORKDIR /src/Notesnook.API/
RUN dotnet build -c Release -o /app/build -a $TARGETARCH RUN dotnet build -c Release -o /app/build -a $TARGETARCH
FROM build AS publish FROM build AS publish
RUN dotnet publish -c Release -o /app/publish \ RUN dotnet publish -c Release -o /app/publish \
#--runtime alpine-x64 \ #--runtime alpine-x64 \
--self-contained true \ --self-contained true \
/p:TrimMode=partial \ /p:PublishSingleFile=true \
/p:PublishTrimmed=true \ /p:JsonSerializerIsReflectionEnabledByDefault=true \
/p:PublishSingleFile=true \ -a $TARGETARCH
/p:JsonSerializerIsReflectionEnabledByDefault=true \
-a $TARGETARCH FROM --platform=$BUILDPLATFORM base AS final
ARG TARGETARCH
FROM --platform=$BUILDPLATFORM base AS final ARG BUILDPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM # create a new user and change directory ownership
RUN adduser --disabled-password \
# create a new user and change directory ownership --home /app \
RUN adduser --disabled-password \ --gecos '' dotnetuser && chown -R dotnetuser /app
--home /app \
--gecos '' dotnetuser && chown -R dotnetuser /app # impersonate into the new user
USER dotnetuser
# impersonate into the new user WORKDIR /app
USER dotnetuser
WORKDIR /app COPY --from=publish /app/publish .
COPY --from=publish /app/publish .
ENTRYPOINT ["./Notesnook.API"] ENTRYPOINT ["./Notesnook.API"]
@@ -7,19 +7,17 @@ public sealed class SyncEventCounterSource : EventSource
{ {
public static readonly SyncEventCounterSource Log = new(); public static readonly SyncEventCounterSource Log = new();
private Meter meter = new("Notesnook.API.Metrics.Sync", "1.0.0"); private readonly Meter meter = new("Notesnook.API.Metrics.Sync", "1.0.0");
private Counter<int> fetchCounter; private readonly Counter<int> fetchCounter;
private Counter<int> pushCounter; private readonly Counter<int> pushCounter;
private Counter<int> legacyFetchCounter; private readonly Counter<int> pushV2Counter;
private Counter<int> pushV2Counter; private readonly Counter<int> fetchV2Counter;
private Counter<int> fetchV2Counter; private readonly Histogram<long> fetchV2Duration;
private Histogram<long> fetchV2Duration; private readonly Histogram<long> pushV2Duration;
private Histogram<long> pushV2Duration;
private SyncEventCounterSource() private SyncEventCounterSource()
{ {
fetchCounter = meter.CreateCounter<int>("sync.fetches", "fetches", "Total fetches"); fetchCounter = meter.CreateCounter<int>("sync.fetches", "fetches", "Total fetches");
pushCounter = meter.CreateCounter<int>("sync.pushes", "pushes", "Total pushes"); pushCounter = meter.CreateCounter<int>("sync.pushes", "pushes", "Total pushes");
legacyFetchCounter = meter.CreateCounter<int>("sync.legacy-fetches", "fetches", "Total legacy fetches");
fetchV2Counter = meter.CreateCounter<int>("sync.v2.fetches", "fetches", "Total v2 fetches"); fetchV2Counter = meter.CreateCounter<int>("sync.v2.fetches", "fetches", "Total v2 fetches");
pushV2Counter = meter.CreateCounter<int>("sync.v2.pushes", "pushes", "Total v2 pushes"); pushV2Counter = meter.CreateCounter<int>("sync.v2.pushes", "pushes", "Total v2 pushes");
fetchV2Duration = meter.CreateHistogram<long>("sync.v2.fetch_duration"); fetchV2Duration = meter.CreateHistogram<long>("sync.v2.fetch_duration");
@@ -27,7 +25,6 @@ public sealed class SyncEventCounterSource : EventSource
} }
public void Fetch() => fetchCounter.Add(1); public void Fetch() => fetchCounter.Add(1);
public void LegacyFetch() => legacyFetchCounter.Add(1);
public void FetchV2() => fetchV2Counter.Add(1); public void FetchV2() => fetchV2Counter.Add(1);
public void PushV2() => pushV2Counter.Add(1); public void PushV2() => pushV2Counter.Add(1);
public void Push() => pushCounter.Add(1); public void Push() => pushCounter.Add(1);
@@ -36,14 +33,7 @@ public sealed class SyncEventCounterSource : EventSource
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
legacyFetchCounter = null;
fetchV2Counter = null;
pushV2Counter = null;
pushCounter = null;
fetchCounter = null;
meter.Dispose(); meter.Dispose();
meter = null;
base.Dispose(disposing); base.Dispose(disposing);
} }
} }
@@ -1,71 +1,62 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Linq; using System;
using System.Net; using System.Linq;
using System.Text.Json; using System.Net;
using System.Threading.Tasks; using System.Net.WebSockets;
using Microsoft.AspNetCore.Authorization; using System.Text;
using Microsoft.AspNetCore.Authorization.Policy; using System.Text.Json;
using Microsoft.AspNetCore.Http; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace Notesnook.API.Extensions using Microsoft.AspNetCore.Authorization.Policy;
{ using Microsoft.AspNetCore.Http;
public class AuthorizationResultTransformer : IAuthorizationMiddlewareResultHandler
{ namespace Notesnook.API.Extensions
private readonly IAuthorizationMiddlewareResultHandler _handler; {
public class AuthorizationResultTransformer : IAuthorizationMiddlewareResultHandler
public AuthorizationResultTransformer() {
{ private readonly IAuthorizationMiddlewareResultHandler _handler;
_handler = new AuthorizationMiddlewareResultHandler();
} public AuthorizationResultTransformer()
{
public async Task HandleAsync( _handler = new AuthorizationMiddlewareResultHandler();
RequestDelegate requestDelegate, }
HttpContext httpContext,
AuthorizationPolicy authorizationPolicy, public async Task HandleAsync(
PolicyAuthorizationResult policyAuthorizationResult) RequestDelegate requestDelegate,
{ HttpContext httpContext,
var isWebsocket = httpContext.Request.Headers.Upgrade == "websocket"; AuthorizationPolicy authorizationPolicy,
PolicyAuthorizationResult policyAuthorizationResult)
if (!isWebsocket && policyAuthorizationResult.Forbidden && policyAuthorizationResult.AuthorizationFailure != null) {
{ if (policyAuthorizationResult.Forbidden && policyAuthorizationResult.AuthorizationFailure != null)
var error = string.Join("\n", policyAuthorizationResult.AuthorizationFailure.FailureReasons.Select((r) => r.Message)); {
var error = string.Join("\n", policyAuthorizationResult.AuthorizationFailure.FailureReasons.Select((r) => r.Message));
if (!string.IsNullOrEmpty(error)) if (!string.IsNullOrEmpty(error))
{ {
httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
httpContext.Response.ContentType = "application/json"; httpContext.Response.ContentType = "application/json";
await httpContext.Response.WriteAsync(JsonSerializer.Serialize(new { error })); await httpContext.Response.WriteAsync(JsonSerializer.Serialize(new { error }));
return; return;
} }
}
await _handler.HandleAsync(requestDelegate, httpContext, authorizationPolicy, policyAuthorizationResult); await _handler.HandleAsync(requestDelegate, httpContext, authorizationPolicy, policyAuthorizationResult);
} }
else if (isWebsocket) }
{
await _handler.HandleAsync(requestDelegate, httpContext, authorizationPolicy, PolicyAuthorizationResult.Success());
}
else
{
await _handler.HandleAsync(requestDelegate, httpContext, authorizationPolicy, policyAuthorizationResult);
}
}
}
} }
@@ -0,0 +1,17 @@
using System.Threading;
using System;
using System.Threading.Tasks;
using System.Linq;
namespace System.Security.Claims
{
public static class ClaimsPrincipalExtensions
{
private readonly static string[] SUBSCRIBED_CLAIMS = ["believer", "education", "essential", "pro", "legacy_pro"];
public static bool IsUserSubscribed(this ClaimsPrincipal user)
=> user.Claims.Any((c) => c.Type == "notesnook:status" && SUBSCRIBED_CLAIMS.Contains(c.Value));
public static string GetUserId(this ClaimsPrincipal user)
=> user.FindFirstValue("sub") ?? throw new Exception("User ID not found in claims.");
}
}
+43 -43
View File
@@ -1,44 +1,44 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Threading; using System.Threading;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDB.Driver namespace MongoDB.Driver
{ {
public static class TransactionHelper public static class TransactionHelper
{ {
public static async Task StartTransaction(this IMongoClient client, Action<CancellationToken> operate, CancellationToken ct) public static async Task StartTransaction(this IMongoClient client, Action<CancellationToken> operate, CancellationToken ct)
{ {
using (var session = await client.StartSessionAsync()) using (var session = await client.StartSessionAsync())
{ {
var transactionOptions = new TransactionOptions(readPreference: ReadPreference.Nearest, readConcern: ReadConcern.Local, writeConcern: WriteConcern.WMajority); var transactionOptions = new TransactionOptions(readPreference: ReadPreference.Nearest, readConcern: ReadConcern.Local, writeConcern: WriteConcern.WMajority);
await session.WithTransactionAsync((handle, token) => await session.WithTransactionAsync((handle, token) =>
{ {
return Task.Run(() => return Task.Run(() =>
{ {
operate(token); operate(token);
return true; return true;
}); });
}, transactionOptions, ct); }, transactionOptions, ct);
} }
} }
} }
} }
+414
View File
@@ -0,0 +1,414 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
using Microsoft.Extensions.Logging;
namespace Notesnook.API.Helpers
{
/// <summary>
/// Configuration for S3 failover behavior
/// </summary>
public class S3FailoverConfig
{
/// <summary>
/// Maximum number of retry attempts per endpoint
/// </summary>
public int MaxRetries { get; set; } = 3;
/// <summary>
/// Delay between retries in milliseconds
/// </summary>
public int RetryDelayMs { get; set; } = 1000;
/// <summary>
/// Whether to use exponential backoff for retries
/// </summary>
public bool UseExponentialBackoff { get; set; } = true;
/// <summary>
/// Whether to allow failover for write operations (PUT, POST, DELETE).
/// Default is false to prevent data consistency issues.
/// </summary>
public bool AllowWriteFailover { get; set; } = false;
/// <summary>
/// List of exception types that should trigger failover
/// </summary>
public HashSet<Type> FailoverExceptions { get; set; } = new()
{
typeof(AmazonS3Exception),
typeof(System.Net.Http.HttpRequestException),
typeof(System.Net.Sockets.SocketException),
typeof(System.Threading.Tasks.TaskCanceledException),
typeof(TimeoutException)
};
/// <summary>
/// List of S3 error codes that should trigger failover
/// </summary>
public HashSet<string> FailoverErrorCodes { get; set; } = new()
{
"ServiceUnavailable",
"SlowDown",
"InternalError",
"RequestTimeout"
};
}
/// <summary>
/// Result of a failover operation
/// </summary>
public class S3FailoverResult<T>
{
public T? Result { get; set; }
public bool UsedFailover { get; set; }
public int ClientIndex { get; set; } = 0;
public int AttemptsUsed { get; set; }
public Exception? LastException { get; set; }
}
/// <summary>
/// Helper class for S3 operations with automatic failover to multiple endpoints
/// </summary>
public class S3FailoverHelper
{
private readonly List<AmazonS3Client> clients;
private readonly S3FailoverConfig config;
private readonly ILogger? logger;
/// <summary>
/// Initialize with a list of S3 clients (first is primary, rest are failover endpoints)
/// </summary>
public S3FailoverHelper(
IEnumerable<AmazonS3Client> clients,
S3FailoverConfig? config = null,
ILogger? logger = null)
{
if (clients == null) throw new ArgumentNullException(nameof(clients));
this.clients = new List<AmazonS3Client>(clients);
if (this.clients.Count == 0) throw new ArgumentException("At least one S3 client is required", nameof(clients));
this.config = config ?? new S3FailoverConfig();
this.logger = logger;
}
/// <summary>
/// Initialize with params array of S3 clients
/// </summary>
public S3FailoverHelper(
S3FailoverConfig? config = null,
ILogger? logger = null,
params AmazonS3Client[] clients)
{
if (clients == null || clients.Length == 0)
throw new ArgumentException("At least one S3 client is required", nameof(clients));
this.clients = new List<AmazonS3Client>(clients);
this.config = config ?? new S3FailoverConfig();
this.logger = logger;
}
/// <summary>
/// Execute an S3 operation with automatic failover
/// </summary>
/// <param name="operation">The S3 operation to execute</param>
/// <param name="operationName">Name of the operation for logging</param>
/// <param name="isWriteOperation">Whether this is a write operation (PUT/POST/DELETE). Write operations only use primary endpoint by default.</param>
public async Task<T> ExecuteWithFailoverAsync<T>(
Func<AmazonS3Client, Task<T>> operation,
string operationName = "S3Operation",
bool isWriteOperation = false)
{
var result = await ExecuteWithFailoverInternalAsync(operation, operationName, isWriteOperation);
if (result.Result == null)
{
throw result.LastException ?? new Exception($"Failed to execute {operationName} on all endpoints");
}
return result.Result;
}
/// <summary>
/// Execute an S3 operation with automatic failover and return detailed result
/// </summary>
/// <param name="operation">The S3 operation to execute</param>
/// <param name="operationName">Name of the operation for logging</param>
/// <param name="isWriteOperation">Whether this is a write operation (PUT/POST/DELETE). Write operations only use primary endpoint by default.</param>
private async Task<S3FailoverResult<T>> ExecuteWithFailoverInternalAsync<T>(
Func<AmazonS3Client, Task<T>> operation,
string operationName = "S3Operation",
bool isWriteOperation = false)
{
var result = new S3FailoverResult<T>();
Exception? lastException = null;
// Determine max clients to try based on write operation flag
var maxClientsToTry = (isWriteOperation && !config.AllowWriteFailover) ? 1 : clients.Count;
if (isWriteOperation && !config.AllowWriteFailover && clients.Count > 1)
{
logger?.LogDebug(
"Write operation {Operation} will only use primary endpoint. Failover is disabled for write operations.",
operationName);
}
// Try each client in sequence (first is primary, rest are failovers)
for (int i = 0; i < maxClientsToTry; i++)
{
var client = clients[i];
var clientName = i == 0 ? "primary" : $"failover-{i}";
var isPrimary = i == 0;
if (!isPrimary && lastException != null)
{
logger?.LogWarning(lastException,
"Previous S3 endpoint failed for {Operation}. Attempting {ClientName} (endpoint {Index}/{Total}).",
operationName, clientName, i + 1, maxClientsToTry);
}
var (success, value, exception, attempts) = await TryExecuteAsync(client, operation, operationName, clientName);
result.AttemptsUsed += attempts;
if (success && value != null)
{
result.Result = value;
result.UsedFailover = !isPrimary;
result.ClientIndex = i;
if (!isPrimary)
{
logger?.LogInformation(
"Successfully failed over to {ClientName} S3 endpoint for {Operation}",
clientName, operationName);
}
return result;
}
lastException = exception;
// If this is not the last client and should retry, log and continue
if (i < maxClientsToTry - 1 && ShouldFailover(exception))
{
logger?.LogWarning(exception,
"Endpoint {ClientName} failed for {Operation}. {Remaining} endpoint(s) remaining.",
clientName, operationName, maxClientsToTry - i - 1);
}
}
// All clients failed
result.LastException = lastException;
logger?.LogError(lastException,
"All S3 endpoints failed for {Operation}. Total endpoints tried: {EndpointCount}, Total attempts: {Attempts}",
operationName, maxClientsToTry, result.AttemptsUsed);
return result;
} /// <summary>
/// Try to execute an operation with retries
/// </summary>
private async Task<(bool success, T? value, Exception? exception, int attempts)> TryExecuteAsync<T>(
AmazonS3Client client,
Func<AmazonS3Client, Task<T>> operation,
string operationName,
string endpointName)
{
Exception? lastException = null;
int attempts = 0;
for (int retry = 0; retry <= config.MaxRetries; retry++)
{
attempts++;
try
{
var result = await operation(client);
return (true, result, null, attempts);
}
catch (Exception ex)
{
lastException = ex;
if (retry < config.MaxRetries && ShouldRetry(ex))
{
var delay = CalculateRetryDelay(retry);
logger?.LogWarning(ex,
"Attempt {Attempt}/{MaxAttempts} failed for {Operation} on {Endpoint}. Retrying in {Delay}ms",
retry + 1, config.MaxRetries + 1, operationName, endpointName, delay);
await Task.Delay(delay);
}
else
{
logger?.LogError(ex,
"Operation {Operation} failed on {Endpoint} after {Attempts} attempts",
operationName, endpointName, attempts);
break;
}
}
}
return (false, default, lastException, attempts);
}
/// <summary>
/// Determine if an exception should trigger a retry
/// </summary>
private bool ShouldRetry(Exception exception)
{
// Check if exception type is in the retry list
var exceptionType = exception.GetType();
if (config.FailoverExceptions.Contains(exceptionType))
{
// For S3 exceptions, check error codes
if (exception is AmazonS3Exception s3Exception)
{
return config.FailoverErrorCodes.Contains(s3Exception.ErrorCode);
}
return true;
}
return false;
}
/// <summary>
/// Determine if an exception should trigger failover to secondary endpoint
/// </summary>
private bool ShouldFailover(Exception? exception)
{
if (exception == null) return false;
return ShouldRetry(exception);
}
/// <summary>
/// Calculate delay for retry based on retry attempt number
/// </summary>
private int CalculateRetryDelay(int retryAttempt)
{
if (!config.UseExponentialBackoff)
{
return config.RetryDelayMs;
}
// Exponential backoff: delay * 2^retryAttempt
return config.RetryDelayMs * (int)Math.Pow(2, retryAttempt);
}
/// <summary>
/// Execute a void operation with automatic failover
/// </summary>
/// <param name="operation">The S3 operation to execute</param>
/// <param name="operationName">Name of the operation for logging</param>
/// <param name="isWriteOperation">Whether this is a write operation (PUT/POST/DELETE). Write operations only use primary endpoint by default.</param>
public async Task ExecuteWithFailoverAsync(
Func<AmazonS3Client, Task> operation,
string operationName = "S3Operation",
bool isWriteOperation = false)
{
await ExecuteWithFailoverAsync<object?>(async (client) =>
{
await operation(client);
return null;
}, operationName, isWriteOperation);
}
}
public static class S3ClientFactory
{
public static List<AmazonS3Client> CreateS3Clients(
string serviceUrls,
string regions,
string accessKeyIds,
string secretKeys,
bool forcePathStyle = true)
{
if (string.IsNullOrWhiteSpace(serviceUrls))
return new List<AmazonS3Client>();
var urls = SplitAndTrim(serviceUrls);
var regionList = SplitAndTrim(regions);
var keyIds = SplitAndTrim(accessKeyIds);
var secrets = SplitAndTrim(secretKeys);
if (urls.Length != regionList.Length ||
urls.Length != keyIds.Length ||
urls.Length != secrets.Length)
{
throw new ArgumentException("All S3 configuration parameters must have the same number of values");
}
var clients = new List<AmazonS3Client>();
for (int i = 0; i < urls.Length; i++)
{
var url = urls[i];
if (string.IsNullOrWhiteSpace(url))
continue;
// Get corresponding values from other arrays
var region = regionList[i];
var keyId = keyIds[i];
var secret = secrets[i];
// Validate that all required values are present
if (string.IsNullOrWhiteSpace(region) ||
string.IsNullOrWhiteSpace(keyId) ||
string.IsNullOrWhiteSpace(secret))
{
System.Diagnostics.Debug.WriteLine(
$"Skipping S3 client at index {i}: Missing required values (URL={url}, Region={region}, KeyId={keyId?.Length > 0}, Secret={secret?.Length > 0})");
continue;
}
try
{
var config = new AmazonS3Config
{
ServiceURL = url,
AuthenticationRegion = region,
ForcePathStyle = forcePathStyle,
SignatureMethod = Amazon.Runtime.SigningAlgorithm.HmacSHA256,
SignatureVersion = "4"
};
var client = new AmazonS3Client(keyId, secret, config);
clients.Add(client);
}
catch (Exception ex)
{
// Log configuration error but continue with other clients
System.Diagnostics.Debug.WriteLine($"Failed to create S3 client for URL {url}: {ex.Message}");
}
}
return clients;
}
private static string[] SplitAndTrim(string? input)
{
if (string.IsNullOrWhiteSpace(input))
return Array.Empty<string>();
return input.Split(';', StringSplitOptions.None)
.Select(s => s.Trim())
.ToArray();
}
}
}
+79
View File
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using Notesnook.API.Models;
using Streetwriters.Common.Enums;
using Streetwriters.Common.Models;
namespace Notesnook.API.Helpers
{
class StorageHelper
{
const long MB = 1024 * 1024;
const long GB = 1024 * MB;
public readonly static Dictionary<SubscriptionPlan, long> MAX_STORAGE_PER_MONTH = new()
{
{ SubscriptionPlan.FREE, 50L * MB },
{ SubscriptionPlan.ESSENTIAL, GB },
{ SubscriptionPlan.PRO, 10L * GB },
{ SubscriptionPlan.EDUCATION, 10L * GB },
{ SubscriptionPlan.BELIEVER, 25L * GB },
{ SubscriptionPlan.LEGACY_PRO, -1 }
};
public readonly static Dictionary<SubscriptionPlan, long> MAX_FILE_SIZE = new()
{
{ SubscriptionPlan.FREE, 10 * MB },
{ SubscriptionPlan.ESSENTIAL, 100 * MB },
{ SubscriptionPlan.PRO, 1L * GB },
{ SubscriptionPlan.EDUCATION, 1L * GB },
{ SubscriptionPlan.BELIEVER, 5L * GB },
{ SubscriptionPlan.LEGACY_PRO, 512 * MB }
};
public static long GetStorageLimitForPlan(Subscription subscription)
{
return MAX_STORAGE_PER_MONTH[subscription.Plan];
}
public static long GetFileSizeLimitForPlan(Subscription subscription)
{
return MAX_FILE_SIZE[subscription.Plan];
}
public static bool IsStorageLimitReached(Subscription subscription, long limit)
{
var storageLimit = GetStorageLimitForPlan(subscription);
if (storageLimit == -1) return false;
return limit > storageLimit;
}
public static bool IsFileSizeExceeded(Subscription subscription, long fileSize)
{
var maxFileSize = MAX_FILE_SIZE[subscription.Plan];
return fileSize > maxFileSize;
}
public static Limit RolloverStorageLimit(Limit? limit)
{
var updatedAt = DateTimeOffset.FromUnixTimeMilliseconds(limit?.UpdatedAt ?? 0);
if (limit == null || DateTimeOffset.UtcNow.Year > updatedAt.Year || DateTimeOffset.UtcNow.Month > updatedAt.Month)
{
limit = new Limit { UpdatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), Value = 0 };
return limit;
}
return limit;
}
private static readonly string[] sizes = ["B", "KB", "MB", "GB", "TB"];
public static string FormatBytes(long size)
{
int order = 0;
while (size >= 1024 && order < sizes.Length - 1)
{
order++;
size = size / 1024;
}
return String.Format("{0:0.##} {1}", size, sizes[order]);
}
}
}
+50
View File
@@ -0,0 +1,50 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Notesnook.API.Models;
using Streetwriters.Common;
namespace Notesnook.API.Helpers
{
public class UrlHelper
{
public static string ConstructPublishUrl(string slug)
{
var baseUrl = Constants.MONOGRAPH_PUBLIC_URL;
return $"{baseUrl}/{slug}";
}
public static string ConstructPublishUrl(Monograph monograph)
{
if (!string.IsNullOrEmpty(monograph.Slug))
{
return ConstructPublishUrl("s/" + monograph.Slug);
}
return ConstructPublishUrl(monograph.ItemId ?? monograph.Id);
}
public static string ConstructPublishUrl(MonographMetadata metadata)
{
if (!string.IsNullOrEmpty(metadata.PublishUrl))
{
return ConstructPublishUrl("s/" + metadata.PublishUrl);
}
return ConstructPublishUrl(metadata.PublishUrl ?? metadata.ItemId);
}
}
}
-473
View File
@@ -1,473 +0,0 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using MongoDB.Driver;
using Notesnook.API.Authorization;
using Notesnook.API.Interfaces;
using Notesnook.API.Models;
using Notesnook.API.Repositories;
using Streetwriters.Common.Models;
using Streetwriters.Data.Interfaces;
namespace Notesnook.API.Hubs
{
public struct RunningPush
{
public long Timestamp { get; set; }
public long Validity { get; set; }
public string ConnectionId { get; set; }
}
public interface ISyncHubClient
{
Task PushItems(SyncTransferItemV2 transferItem);
Task<bool> SendItems(SyncTransferItemV2 transferItem);
Task PushCompleted(long lastSynced);
}
public class GlobalSync
{
private const long PUSH_VALIDITY_EXTENSION_PERIOD = 16 * 1000; // 16 second
private const int PUSH_VALIDITY_PERIOD_PER_ITEM = 5 * 100; // 0.5 second
private const long BASE_PUSH_VALIDITY_PERIOD = 5 * 1000; // 5 seconds
private const long BASE_PUSH_VALIDITY_PERIOD_NEW = 16 * 1000; // 16 seconds
private readonly static Dictionary<string, List<RunningPush>> PushOperations = new();
public static void ClearPushOperations(string userId, string connectionId)
{
if (PushOperations.TryGetValue(userId, out List<RunningPush> operations))
{
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
foreach (var push in operations.ToArray())
if (push.ConnectionId == connectionId || !IsPushValid(push, now))
operations.Remove(push);
}
}
public static bool IsPushing(string userId, string connectionId)
{
if (PushOperations.TryGetValue(userId, out List<RunningPush> operations))
{
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
foreach (var push in operations)
if (push.ConnectionId == connectionId && IsPushValid(push, now)) return true;
}
return false;
}
public static bool IsUserPushing(string userId)
{
var count = 0;
if (PushOperations.TryGetValue(userId, out List<RunningPush> operations))
{
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
foreach (var push in operations)
if (IsPushValid(push, now)) ++count;
}
return count > 0;
}
public static void StartPush(string userId, string connectionId, long? totalItems = null)
{
if (IsPushing(userId, connectionId)) return;
if (!PushOperations.ContainsKey(userId))
PushOperations[userId] = new List<RunningPush>();
PushOperations[userId].Add(new RunningPush
{
ConnectionId = connectionId,
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
Validity = totalItems.HasValue ? BASE_PUSH_VALIDITY_PERIOD + (totalItems.Value * PUSH_VALIDITY_PERIOD_PER_ITEM) : BASE_PUSH_VALIDITY_PERIOD_NEW
});
}
public static void ExtendPush(string userId, string connectionId)
{
if (!IsPushing(userId, connectionId) || !PushOperations.ContainsKey(userId))
{
StartPush(userId, connectionId);
return;
}
var index = PushOperations[userId].FindIndex((push) => push.ConnectionId == connectionId);
if (index < 0)
{
StartPush(userId, connectionId);
return;
}
var pushOperation = PushOperations[userId][index];
pushOperation.Validity += PUSH_VALIDITY_EXTENSION_PERIOD;
}
private static bool IsPushValid(RunningPush push, long now)
{
return now < push.Timestamp + push.Validity;
}
}
[Authorize("Sync")]
public class SyncHub : Hub<ISyncHubClient>
{
private ISyncItemsRepositoryAccessor Repositories { get; }
private readonly IUnitOfWork unit;
private readonly string[] CollectionKeys = new[] {
"settings",
"attachment",
"note",
"notebook",
"content",
"shortcut",
"reminder",
"relation", // relations must sync at the end to prevent invalid state
};
public SyncHub(ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor, IUnitOfWork unitOfWork)
{
Repositories = syncItemsRepositoryAccessor;
unit = unitOfWork;
}
public override async Task OnConnectedAsync()
{
var result = new SyncRequirement().IsAuthorized(Context.User, new PathString("/hubs/sync"));
if (!result.Succeeded)
{
var reason = result.AuthorizationFailure.FailureReasons.FirstOrDefault();
throw new HubException(reason?.Message ?? "Unauthorized");
}
var id = Context.User.FindFirstValue("sub");
await Groups.AddToGroupAsync(Context.ConnectionId, id);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
try
{
await base.OnDisconnectedAsync(exception);
}
finally
{
var id = Context.User.FindFirstValue("sub");
GlobalSync.ClearPushOperations(id, Context.ConnectionId);
}
}
private Action<SyncItem, string, long> MapTypeToUpsertAction(string type)
{
return type switch
{
"attachment" => Repositories.Attachments.Upsert,
"note" => Repositories.Notes.Upsert,
"notebook" => Repositories.Notebooks.Upsert,
"content" => Repositories.Contents.Upsert,
"shortcut" => Repositories.Shortcuts.Upsert,
"reminder" => Repositories.Reminders.Upsert,
"relation" => Repositories.Relations.Upsert,
_ => null,
};
}
public async Task<long> InitializePush(SyncMetadata syncMetadata)
{
if (syncMetadata.LastSynced <= 0) throw new HubException("Last synced time cannot be zero or less than zero.");
var userId = Context.User.FindFirstValue("sub");
if (string.IsNullOrEmpty(userId)) return 0;
UserSettings userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId);
long dateSynced = Math.Max(syncMetadata.LastSynced, userSettings.LastSynced);
GlobalSync.StartPush(userId, Context.ConnectionId);
if (
(userSettings.VaultKey != null &&
syncMetadata.VaultKey != null &&
!userSettings.VaultKey.Equals(syncMetadata.VaultKey) &&
!syncMetadata.VaultKey.IsEmpty()) ||
(userSettings.VaultKey == null &&
syncMetadata.VaultKey != null &&
!syncMetadata.VaultKey.IsEmpty()))
{
userSettings.VaultKey = syncMetadata.VaultKey;
await Repositories.UsersSettings.UpsertAsync(userSettings, (u) => u.UserId == userId);
}
return dateSynced;
}
public async Task<int> PushItems(SyncTransferItemV2 pushItem, long dateSynced)
{
var userId = Context.User.FindFirstValue("sub");
if (string.IsNullOrEmpty(userId)) return 0;
SyncEventCounterSource.Log.Push();
try
{
var others = Clients.OthersInGroup(userId);
others.PushItems(pushItem);
GlobalSync.ExtendPush(userId, Context.ConnectionId);
if (pushItem.Type == "settings")
{
var settings = pushItem.Items.First();
if (settings == null) return 0;
settings.Id = MongoDB.Bson.ObjectId.Parse(userId);
settings.ItemId = userId;
Repositories.LegacySettings.Upsert(settings, userId, dateSynced);
}
else
{
var UpsertItem = MapTypeToUpsertAction(pushItem.Type) ?? throw new Exception("Invalid item type.");
foreach (var item in pushItem.Items)
{
UpsertItem(item, userId, dateSynced);
}
}
return await unit.Commit() ? 1 : 0;
}
catch (Exception ex)
{
GlobalSync.ClearPushOperations(userId, Context.ConnectionId);
throw ex;
}
}
public async Task<bool> SyncCompleted(long dateSynced)
{
var userId = Context.User.FindFirstValue("sub");
try
{
UserSettings userSettings = await this.Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId);
long lastSynced = Math.Max(dateSynced, userSettings.LastSynced);
userSettings.LastSynced = lastSynced;
await this.Repositories.UsersSettings.UpsertAsync(userSettings, (u) => u.UserId == userId);
await Clients.OthersInGroup(userId).PushCompleted(lastSynced);
return true;
}
finally
{
GlobalSync.ClearPushOperations(userId, Context.ConnectionId);
}
}
private static async IAsyncEnumerable<SyncTransferItemV2> PrepareChunks(Func<string, long, int, Task<IAsyncCursor<SyncItem>>>[] collections, string[] types, string userId, long lastSyncedTimestamp, int size, long maxBytes, int skipChunks)
{
var chunksProcessed = 0;
for (int i = 0; i < collections.Length; i++)
{
var type = types[i];
using var cursor = await collections[i](userId, lastSyncedTimestamp, size);
var chunk = new List<SyncItem>();
long totalBytes = 0;
long METADATA_BYTES = 5 * 1024;
while (await cursor.MoveNextAsync())
{
if (chunksProcessed++ < skipChunks) continue;
foreach (var item in cursor.Current)
{
chunk.Add(item);
totalBytes += item.Length + METADATA_BYTES;
if (totalBytes >= maxBytes)
{
yield return new SyncTransferItemV2
{
Items = chunk,
Type = type,
Count = chunksProcessed
};
totalBytes = 0;
chunk.Clear();
}
}
}
if (chunk.Count > 0)
{
if (chunksProcessed++ < skipChunks) continue;
yield return new SyncTransferItemV2
{
Items = chunk,
Type = type,
Count = chunksProcessed
};
}
}
}
public Task<SyncMetadata> RequestFetch(long lastSyncedTimestamp)
{
return RequestResumableFetch(lastSyncedTimestamp);
}
public async Task<SyncMetadata> RequestResumableFetch(long lastSyncedTimestamp, int cursor = 0)
{
var userId = Context.User.FindFirstValue("sub");
if (GlobalSync.IsUserPushing(userId))
{
throw new HubException("Cannot fetch data while another sync is in progress. Please try again later.");
}
SyncEventCounterSource.Log.Fetch();
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId);
if (userSettings.LastSynced > 0 && lastSyncedTimestamp > userSettings.LastSynced)
{
throw new HubException($"Provided timestamp value is too large. Server timestamp: {userSettings.LastSynced} Sent timestamp: {lastSyncedTimestamp}. Please run a Force Sync to fix this issue.");
}
// var client = Clients.Caller;
if (lastSyncedTimestamp > 0 && userSettings.LastSynced == lastSyncedTimestamp)
{
return new SyncMetadata
{
LastSynced = userSettings.LastSynced,
};
}
var isResumable = lastSyncedTimestamp == 0;
if (!isResumable) cursor = 0;
var chunks = PrepareChunks(
collections: new[] {
Repositories.LegacySettings.FindItemsSyncedAfter,
Repositories.Attachments.FindItemsSyncedAfter,
Repositories.Notes.FindItemsSyncedAfter,
Repositories.Notebooks.FindItemsSyncedAfter,
Repositories.Contents.FindItemsSyncedAfter,
Repositories.Shortcuts.FindItemsSyncedAfter,
Repositories.Reminders.FindItemsSyncedAfter,
Repositories.Relations.FindItemsSyncedAfter,
},
types: CollectionKeys,
userId,
lastSyncedTimestamp,
size: 1000,
maxBytes: 7 * 1024 * 1024,
skipChunks: cursor
);
await foreach (var chunk in chunks)
{
_ = await Clients.Caller.SendItems(chunk).WaitAsync(TimeSpan.FromMinutes(10));
}
return new SyncMetadata
{
VaultKey = userSettings.VaultKey,
LastSynced = userSettings.LastSynced,
};
}
}
[MessagePack.MessagePackObject]
public struct BatchedSyncTransferItem
{
[MessagePack.Key("lastSynced")]
public long LastSynced { get; set; }
[MessagePack.Key("items")]
public string[] Items { get; set; }
[MessagePack.Key("types")]
public string[] Types { get; set; }
[MessagePack.Key("total")]
public int Total { get; set; }
[MessagePack.Key("current")]
public int Current { get; set; }
}
[MessagePack.MessagePackObject]
public struct SyncTransferItem
{
[MessagePack.Key("synced")]
public bool Synced { get; set; }
[MessagePack.Key("lastSynced")]
public long LastSynced { get; set; }
[MessagePack.Key("item")]
public string Item { get; set; }
[MessagePack.Key("itemType")]
public string ItemType { get; set; }
[MessagePack.Key("total")]
public int Total { get; set; }
[MessagePack.Key("current")]
public int Current { get; set; }
}
[MessagePack.MessagePackObject]
public struct SyncTransferItemV2
{
[MessagePack.Key("items")]
[JsonPropertyName("items")]
public IEnumerable<SyncItem> Items { get; set; }
[MessagePack.Key("type")]
[JsonPropertyName("type")]
public string Type { get; set; }
[MessagePack.Key("count")]
[JsonPropertyName("count")]
public int Count { get; set; }
}
[MessagePack.MessagePackObject]
public struct SyncMetadata
{
[MessagePack.Key("vaultKey")]
[JsonPropertyName("vaultKey")]
public EncryptedData VaultKey { get; set; }
[MessagePack.Key("lastSynced")]
[JsonPropertyName("lastSynced")]
public long LastSynced { get; set; }
// [MessagePack.Key("total")]
// public long TotalItems { get; set; }
}
}
+196 -122
View File
@@ -18,20 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Frozen;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims; using System.Security.Claims;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using MongoDB.Bson; using Microsoft.Extensions.Logging;
using MongoDB.Bson.Serialization;
using MongoDB.Driver; using MongoDB.Driver;
using Notesnook.API.Authorization; using Notesnook.API.Authorization;
using Notesnook.API.Extensions;
using Notesnook.API.Helpers;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Models; using Notesnook.API.Models;
using Notesnook.API.Services; using Notesnook.API.Services;
@@ -43,32 +45,71 @@ namespace Notesnook.API.Hubs
{ {
Task<bool> SendItems(SyncTransferItemV2 transferItem); Task<bool> SendItems(SyncTransferItemV2 transferItem);
Task<bool> SendVaultKey(EncryptedData vaultKey); Task<bool> SendVaultKey(EncryptedData vaultKey);
Task<bool> SendMonographs(IEnumerable<MonographMetadata> monographs);
Task<bool> SendInboxItems(IEnumerable<InboxSyncItem> inboxItems);
Task PushCompleted(); Task PushCompleted();
Task PushCompletedV2(string deviceId);
} }
[Authorize("Sync")] [Authorize]
public class SyncV2Hub : Hub<ISyncV2HubClient> public class SyncV2Hub : Hub<ISyncV2HubClient>
{ {
private ISyncItemsRepositoryAccessor Repositories { get; } private ISyncItemsRepositoryAccessor Repositories { get; }
private SyncDeviceService SyncDeviceService { get; }
private readonly IUnitOfWork unit; private readonly IUnitOfWork unit;
private readonly string[] CollectionKeys = [ private readonly FrozenDictionary<string, Action<IEnumerable<SyncItem>, string, long>> UpsertActionsMap;
"settingitem", private readonly CollectionDef[] BaseCollectionDefs;
"attachment", private readonly CollectionDef[] V4CollectionDefs;
"note", ILogger<SyncV2Hub> Logger { get; }
"notebook",
"content",
"shortcut",
"reminder",
"color",
"tag",
"vault",
"relation", // relations must sync at the end to prevent invalid state
];
public SyncV2Hub(ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor, IUnitOfWork unitOfWork) public SyncV2Hub(ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor, IUnitOfWork unitOfWork, SyncDeviceService syncDeviceService, ILogger<SyncV2Hub> logger)
{ {
Logger = logger;
Repositories = syncItemsRepositoryAccessor; Repositories = syncItemsRepositoryAccessor;
unit = unitOfWork; unit = unitOfWork;
SyncDeviceService = syncDeviceService;
BaseCollectionDefs = [
new("settingitem", Repositories.Settings.FindItemsById),
new("attachment", Repositories.Attachments.FindItemsById),
new("note", Repositories.Notes.FindItemsById),
new("notebook", Repositories.Notebooks.FindItemsById),
new("content", Repositories.Contents.FindItemsById),
new("shortcut", Repositories.Shortcuts.FindItemsById),
new("reminder", Repositories.Reminders.FindItemsById),
new("color", Repositories.Colors.FindItemsById),
new("tag", Repositories.Tags.FindItemsById),
new("vault", Repositories.Vaults.FindItemsById),
new("relation", Repositories.Relations.FindItemsById), // relations must sync at the end to prevent invalid state
];
V4CollectionDefs = [
new("settingitem", Repositories.Settings.FindItemsById),
new("attachment", Repositories.Attachments.FindItemsById),
new("note", Repositories.Notes.FindItemsById),
new("notebook", Repositories.Notebooks.FindItemsById),
new("content", Repositories.Contents.FindItemsById),
new("shortcut", Repositories.Shortcuts.FindItemsById),
new("reminder", Repositories.Reminders.FindItemsById),
new("color", Repositories.Colors.FindItemsById),
new("tag", Repositories.Tags.FindItemsById),
new("vault", Repositories.Vaults.FindItemsById),
new("inboxitemhistory", Repositories.InboxItemsHistory.FindItemsById),
new("relation", Repositories.Relations.FindItemsById), // relations must sync at the end to prevent invalid state
];
UpsertActionsMap = new Dictionary<string, Action<IEnumerable<SyncItem>, string, long>> {
{ "settingitem", Repositories.Settings.UpsertMany },
{ "attachment", Repositories.Attachments.UpsertMany },
{ "note", Repositories.Notes.UpsertMany },
{ "notebook", Repositories.Notebooks.UpsertMany },
{ "content", Repositories.Contents.UpsertMany },
{ "shortcut", Repositories.Shortcuts.UpsertMany },
{ "reminder", Repositories.Reminders.UpsertMany },
{ "relation", Repositories.Relations.UpsertMany },
{ "color", Repositories.Colors.UpsertMany },
{ "vault", Repositories.Vaults.UpsertMany },
{ "tag", Repositories.Tags.UpsertMany },
{ "inboxitemhistory", Repositories.InboxItemsHistory.UpsertMany },
}.ToFrozenDictionary();
} }
public override async Task OnConnectedAsync() public override async Task OnConnectedAsync()
@@ -76,97 +117,84 @@ namespace Notesnook.API.Hubs
var result = new SyncRequirement().IsAuthorized(Context.User, new PathString("/hubs/sync/v2")); var result = new SyncRequirement().IsAuthorized(Context.User, new PathString("/hubs/sync/v2"));
if (!result.Succeeded) if (!result.Succeeded)
{ {
var reason = result.AuthorizationFailure.FailureReasons.FirstOrDefault(); var reason = result.AuthorizationFailure?.FailureReasons.FirstOrDefault();
throw new HubException(reason?.Message ?? "Unauthorized"); throw new HubException(reason?.Message ?? "Unauthorized");
} }
var id = Context.User.FindFirstValue("sub"); var id = Context.User?.FindFirstValue("sub") ?? throw new HubException("User not found.");
await Groups.AddToGroupAsync(Context.ConnectionId, id); await Groups.AddToGroupAsync(Context.ConnectionId, id);
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }
private Action<IEnumerable<SyncItem>, string, long> MapTypeToUpsertAction(string type) public override async Task OnDisconnectedAsync(Exception? exception)
{ {
return type switch if (exception != null)
{ {
"settingitem" => Repositories.Settings.UpsertMany, Logger.LogWarning(exception, "Connection {ConnectionId} disconnected with error (server-side drop)", Context.ConnectionId);
"attachment" => Repositories.Attachments.UpsertMany, }
"note" => Repositories.Notes.UpsertMany, else
"notebook" => Repositories.Notebooks.UpsertMany, {
"content" => Repositories.Contents.UpsertMany, Logger.LogInformation("Connection {ConnectionId} disconnected cleanly (client-initiated)", Context.ConnectionId);
"shortcut" => Repositories.Shortcuts.UpsertMany, }
"reminder" => Repositories.Reminders.UpsertMany, await base.OnDisconnectedAsync(exception);
"relation" => Repositories.Relations.UpsertMany,
"color" => Repositories.Colors.UpsertMany,
"vault" => Repositories.Vaults.UpsertMany,
"tag" => Repositories.Tags.UpsertMany,
_ => null,
};
} }
private Func<string, IEnumerable<string>, bool, int, Task<IAsyncCursor<SyncItem>>> MapTypeToFindItemsAction(string type)
{
return type switch
{
"settingitem" => Repositories.Settings.FindItemsById,
"attachment" => Repositories.Attachments.FindItemsById,
"note" => Repositories.Notes.FindItemsById,
"notebook" => Repositories.Notebooks.FindItemsById,
"content" => Repositories.Contents.FindItemsById,
"shortcut" => Repositories.Shortcuts.FindItemsById,
"reminder" => Repositories.Reminders.FindItemsById,
"relation" => Repositories.Relations.FindItemsById,
"color" => Repositories.Colors.FindItemsById,
"vault" => Repositories.Vaults.FindItemsById,
"tag" => Repositories.Tags.FindItemsById,
_ => null,
};
}
public async Task<int> PushItems(string deviceId, SyncTransferItemV2 pushItem) public async Task<int> PushItems(string deviceId, SyncTransferItemV2 pushItem)
{ {
var userId = Context.User.FindFirstValue("sub"); var userId = Context.User?.FindFirstValue("sub") ?? throw new HubException("Please login to sync.");
if (string.IsNullOrEmpty(userId)) throw new HubException("Please login to sync.");
SyncEventCounterSource.Log.PushV2(); SyncEventCounterSource.Log.PushV2();
var stopwatch = new Stopwatch(); var stopwatch = Stopwatch.StartNew();
stopwatch.Start();
try try
{ {
var UpsertItems = UpsertActionsMap[pushItem.Type] ?? throw new Exception($"Invalid item type: {pushItem.Type}.");
var UpsertItems = MapTypeToUpsertAction(pushItem.Type) ?? throw new Exception($"Invalid item type: {pushItem.Type}.");
UpsertItems(pushItem.Items, userId, 1); UpsertItems(pushItem.Items, userId, 1);
if (!await unit.Commit()) return 0; if (!await unit.Commit()) return 0;
await new SyncDeviceService(new SyncDevice(ref userId, ref deviceId)).AddIdsToOtherDevicesAsync(pushItem.Items.Select((i) => $"{i.ItemId}:{pushItem.Type}").ToList()); await SyncDeviceService.AddIdsToOtherDevicesAsync(userId, deviceId, pushItem.Items.Select((i) => new ItemKey(i.ItemId, pushItem.Type)));
// we need to delete the inbox items from the inbox collection
// after syncing to prevent them from being sent again in the
// next fetch.
var itemIds = pushItem.Items.Select(i => i.ItemId).ToList();
await Repositories.InboxItems.DeleteManyAsync(i => i.UserId == userId && itemIds.Contains(i.ItemId));
return 1; return 1;
} }
finally finally
{ {
stopwatch.Stop();
SyncEventCounterSource.Log.RecordPushDuration(stopwatch.ElapsedMilliseconds); SyncEventCounterSource.Log.RecordPushDuration(stopwatch.ElapsedMilliseconds);
} }
} }
public async Task<bool> PushCompleted() public async Task<bool> PushCompleted()
{ {
var userId = Context.User.FindFirstValue("sub"); var userId = Context.User?.FindFirstValue("sub") ?? throw new HubException("User not found.");
await Clients.OthersInGroup(userId).PushCompleted(); await Clients.OthersInGroup(userId).PushCompleted();
return true; return true;
} }
private static async IAsyncEnumerable<SyncTransferItemV2> PrepareChunks(Func<string, string[], bool, int, Task<IAsyncCursor<SyncItem>>>[] collections, string[] types, string userId, string[] ids, int size, bool resetSync, long maxBytes) public async Task<bool> PushCompletedV2(string deviceId)
{ {
var chunksProcessed = 0; var userId = Context.User?.FindFirstValue("sub") ?? throw new HubException("User not found.");
for (int i = 0; i < collections.Length; i++) await Clients.OthersInGroup(userId).PushCompleted();
{ await Clients.OthersInGroup(userId).PushCompletedV2(deviceId);
var type = types[i]; return true;
}
var filteredIds = ids.Where((id) => id.EndsWith($":{type}")).Select((id) => id.Split(":")[0]).ToArray(); private async IAsyncEnumerable<SyncTransferItemV2> PrepareChunks(string userId, HashSet<ItemKey> ids, int size, bool resetSync, long maxBytes, CollectionDef[] collectionDefs)
{
var itemsProcessed = 0;
foreach (var def in collectionDefs)
{
var type = def.Key;
var filteredIds = ids.Where((id) => id.Type == type).Select((id) => id.ItemId).ToArray();
if (!resetSync && filteredIds.Length == 0) continue; if (!resetSync && filteredIds.Length == 0) continue;
using var cursor = await collections[i](userId, filteredIds, resetSync, size); using var cursor = await def.FindItems(userId, filteredIds, resetSync, size);
var chunk = new List<SyncItem>(); var chunk = new List<SyncItem>();
long totalBytes = 0; long totalBytes = 0;
@@ -180,11 +208,12 @@ namespace Notesnook.API.Hubs
totalBytes += item.Length + METADATA_BYTES; totalBytes += item.Length + METADATA_BYTES;
if (totalBytes >= maxBytes) if (totalBytes >= maxBytes)
{ {
itemsProcessed += chunk.Count;
yield return new SyncTransferItemV2 yield return new SyncTransferItemV2
{ {
Items = chunk, Items = chunk,
Type = type, Type = type,
Count = chunksProcessed Count = itemsProcessed
}; };
totalBytes = 0; totalBytes = 0;
@@ -194,11 +223,12 @@ namespace Notesnook.API.Hubs
} }
if (chunk.Count > 0) if (chunk.Count > 0)
{ {
itemsProcessed += chunk.Count;
yield return new SyncTransferItemV2 yield return new SyncTransferItemV2
{ {
Items = chunk, Items = chunk,
Type = type, Type = type,
Count = chunksProcessed Count = itemsProcessed
}; };
} }
} }
@@ -206,67 +236,112 @@ namespace Notesnook.API.Hubs
public async Task<SyncV2Metadata> RequestFetch(string deviceId) public async Task<SyncV2Metadata> RequestFetch(string deviceId)
{ {
var userId = Context.User.FindFirstValue("sub"); return await HandleRequestFetch(deviceId, false, false, BaseCollectionDefs);
if (string.IsNullOrEmpty(userId)) throw new HubException("Please login to sync."); }
public async Task<SyncV2Metadata> RequestFetchV2(string deviceId)
{
return await HandleRequestFetch(deviceId, true, false, BaseCollectionDefs);
}
public async Task<SyncV2Metadata> RequestFetchV3(string deviceId)
{
return await HandleRequestFetch(deviceId, true, true, BaseCollectionDefs);
}
public async Task<SyncV2Metadata> RequestFetchV4(string deviceId)
{
return await HandleRequestFetch(deviceId, true, true, V4CollectionDefs);
}
private async Task<SyncV2Metadata> HandleRequestFetch(string deviceId, bool includeMonographs, bool includeInboxItems, CollectionDef[] collectionDefs)
{
var userId = Context.User?.FindFirstValue("sub") ?? throw new HubException("Please login to sync.");
SyncEventCounterSource.Log.FetchV2(); SyncEventCounterSource.Log.FetchV2();
var deviceService = new SyncDeviceService(new SyncDevice(ref userId, ref deviceId)); var device = await SyncDeviceService.GetDeviceAsync(userId, deviceId);
if (!deviceService.IsDeviceRegistered()) deviceService.RegisterDevice(); if (device == null)
device = await SyncDeviceService.RegisterDeviceAsync(userId, deviceId);
else
await SyncDeviceService.UpdateLastAccessTimeAsync(userId, deviceId);
var isResetSync = deviceService.IsSyncReset(); var stopwatch = Stopwatch.StartNew();
if (!deviceService.IsUnsynced() &&
!deviceService.IsSyncPending() &&
!isResetSync)
return new SyncV2Metadata { Synced = true };
var stopwatch = new Stopwatch();
stopwatch.Start();
try try
{ {
string[] ids = await deviceService.FetchUnsyncedIdsAsync(); var ids = await SyncDeviceService.FetchUnsyncedIdsAsync(userId, deviceId);
if (!device.IsSyncReset && ids.Count == 0)
return new SyncV2Metadata { Synced = true };
var chunks = PrepareChunks( var chunks = PrepareChunks(
collections: [
Repositories.Settings.FindItemsById,
Repositories.Attachments.FindItemsById,
Repositories.Notes.FindItemsById,
Repositories.Notebooks.FindItemsById,
Repositories.Contents.FindItemsById,
Repositories.Shortcuts.FindItemsById,
Repositories.Reminders.FindItemsById,
Repositories.Colors.FindItemsById,
Repositories.Tags.FindItemsById,
Repositories.Vaults.FindItemsById,
Repositories.Relations.FindItemsById,
],
types: CollectionKeys,
userId, userId,
ids, ids,
size: 1000, size: 100,
resetSync: isResetSync, resetSync: device.IsSyncReset,
maxBytes: 7 * 1024 * 1024 maxBytes: 3 * 1024 * 1024,
collectionDefs
); );
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId.Equals(userId));
if (userSettings.VaultKey != null)
{
if (!await Clients.Caller.SendVaultKey(userSettings.VaultKey).WaitAsync(TimeSpan.FromMinutes(10))) throw new HubException("Client rejected vault key.");
}
await foreach (var chunk in chunks) await foreach (var chunk in chunks)
{ {
if (!await Clients.Caller.SendItems(chunk).WaitAsync(TimeSpan.FromMinutes(10))) throw new HubException("Client rejected sent items."); if (!await Clients.Caller.SendItems(chunk).WaitAsync(TimeSpan.FromMinutes(10))) throw new HubException("Client rejected sent items.");
if (!isResetSync) if (!device.IsSyncReset)
{ {
var syncedIds = chunk.Items.Select((i) => $"{i.ItemId}:{chunk.Type}").ToHashSet(); ids.ExceptWith(chunk.Items.Select(i => new ItemKey(i.ItemId, chunk.Type)));
ids = ids.Where((id) => !syncedIds.Contains(id)).ToArray(); await SyncDeviceService.WritePendingIdsAsync(userId, deviceId, ids);
await deviceService.WritePendingIdsAsync(ids);
} }
} }
deviceService.Reset(); if (includeMonographs)
{
var unsyncedMonographIds = ids.Where(k => k.Type == "monograph").Select(k => k.ItemId);
FilterDefinition<Monograph> filter = device.IsSyncReset
? Builders<Monograph>.Filter.Eq(m => m.UserId, userId)
: Builders<Monograph>.Filter.And(
Builders<Monograph>.Filter.Eq(m => m.UserId, userId),
Builders<Monograph>.Filter.Or(
Builders<Monograph>.Filter.In(m => m.ItemId, unsyncedMonographIds),
Builders<Monograph>.Filter.In("_id", unsyncedMonographIds)
)
);
var userMonographs = await Repositories.Monographs.Collection
.Find(filter)
.Project((m) => new MonographMetadata
{
DatePublished = m.DatePublished,
Deleted = m.Deleted,
Password = m.Password,
SelfDestruct = m.SelfDestruct,
Title = m.Title,
ItemId = m.ItemId ?? m.Id.ToString(),
PublishUrl = m.Slug // this will be converted to full url in the end, but we only need slug for now
})
.ToListAsync();
userMonographs = userMonographs.Select((p) =>
{
p.PublishUrl = UrlHelper.ConstructPublishUrl(p);
return p;
}).ToList();
if (userMonographs.Count > 0 && !await Clients.Caller.SendMonographs(userMonographs).WaitAsync(TimeSpan.FromMinutes(10)))
throw new HubException("Client rejected monographs.");
}
if (includeInboxItems)
{
var unsyncedInboxItemIds = ids.Where(k => k.Type == "inbox_item").Select(k => k.ItemId);
var userInboxItems = device.IsSyncReset
? await Repositories.InboxItems.FindAsync(m => m.UserId == userId)
: await Repositories.InboxItems.FindAsync(m => m.UserId == userId && unsyncedInboxItemIds.Contains(m.ItemId));
if (userInboxItems.Any() && !await Clients.Caller.SendInboxItems(userInboxItems).WaitAsync(TimeSpan.FromMinutes(10)))
{
throw new HubException("Client rejected inbox items.");
}
}
await SyncDeviceService.ResetAsync(userId, deviceId);
return new SyncV2Metadata return new SyncV2Metadata
{ {
@@ -275,10 +350,14 @@ namespace Notesnook.API.Hubs
} }
finally finally
{ {
stopwatch.Stop();
SyncEventCounterSource.Log.RecordFetchDuration(stopwatch.ElapsedMilliseconds); SyncEventCounterSource.Log.RecordFetchDuration(stopwatch.ElapsedMilliseconds);
} }
} }
private record CollectionDef(
string Key,
Func<string, IEnumerable<string>, bool, int, Task<IAsyncCursor<SyncItem>>> FindItems
);
} }
[MessagePack.MessagePackObject] [MessagePack.MessagePackObject]
@@ -290,7 +369,7 @@ namespace Notesnook.API.Hubs
} }
[MessagePack.MessagePackObject] [MessagePack.MessagePackObject]
public struct SyncV2TransferItem public struct SyncTransferItemV2
{ {
[MessagePack.Key("items")] [MessagePack.Key("items")]
[JsonPropertyName("items")] [JsonPropertyName("items")]
@@ -299,13 +378,8 @@ namespace Notesnook.API.Hubs
[MessagePack.Key("type")] [MessagePack.Key("type")]
[JsonPropertyName("type")] [JsonPropertyName("type")]
public string Type { get; set; } public string Type { get; set; }
[MessagePack.Key("count")]
[MessagePack.Key("final")] [JsonPropertyName("count")]
[JsonPropertyName("final")] public int Count { get; set; }
public bool Final { get; set; }
[MessagePack.Key("vaultKey")]
[JsonPropertyName("vaultKey")]
public EncryptedData VaultKey { get; set; }
} }
} }
+5 -6
View File
@@ -17,23 +17,22 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Amazon.S3.Model; using Amazon.S3.Model;
using Notesnook.API.Models; using Notesnook.API.Models;
using Notesnook.API.Models.Responses;
using Streetwriters.Common.Interfaces;
namespace Notesnook.API.Interfaces namespace Notesnook.API.Interfaces
{ {
public interface IS3Service public interface IS3Service
{ {
Task DeleteObjectAsync(string userId, string name); Task DeleteObjectAsync(string userId, string name);
Task DeleteObjectsAsync(string userId, string[] names);
Task DeleteDirectoryAsync(string userId); Task DeleteDirectoryAsync(string userId);
Task<long> GetObjectSizeAsync(string userId, string name); Task<long> GetObjectSizeAsync(string userId, string name);
string GetUploadObjectUrl(string userId, string name); Task<string?> GetUploadObjectUrlAsync(string userId, string name);
string GetDownloadObjectUrl(string userId, string name); Task<string?> GetInternalUploadObjectUrlAsync(string userId, string name);
Task<MultipartUploadMeta> StartMultipartUploadAsync(string userId, string name, int parts, string uploadId = null); Task<string?> GetDownloadObjectUrlAsync(string userId, string name);
Task<MultipartUploadMeta> StartMultipartUploadAsync(string userId, string name, int parts, string? uploadId = null);
Task AbortMultipartUploadAsync(string userId, string name, string uploadId); Task AbortMultipartUploadAsync(string userId, string name, string uploadId);
Task CompleteMultipartUploadAsync(string userId, CompleteMultipartUploadRequest uploadRequest); Task CompleteMultipartUploadAsync(string userId, CompleteMultipartUploadRequest uploadRequest);
} }
@@ -38,7 +38,12 @@ namespace Notesnook.API.Interfaces
SyncItemsRepository Colors { get; } SyncItemsRepository Colors { get; }
SyncItemsRepository Vaults { get; } SyncItemsRepository Vaults { get; }
SyncItemsRepository Tags { get; } SyncItemsRepository Tags { get; }
SyncItemsRepository InboxItemsHistory { get; }
Repository<UserSettings> UsersSettings { get; } Repository<UserSettings> UsersSettings { get; }
Repository<Monograph> Monographs { get; } Repository<Monograph> Monographs { get; }
Repository<InboxApiKey> InboxApiKey { get; }
Repository<InboxSyncItem> InboxItems { get; }
Repository<SyncDevice> SyncDevices { get; }
Repository<DeviceIdsChunk> DeviceIdsChunks { get; }
} }
} }
+35 -35
View File
@@ -1,36 +1,36 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Threading; using System.Threading.Tasks;
using System.Threading.Tasks; using Notesnook.API.Models;
using Notesnook.API.Models.Responses; using Notesnook.API.Models.Responses;
using Streetwriters.Common.Interfaces; using Streetwriters.Common.Models;
namespace Notesnook.API.Interfaces namespace Notesnook.API.Interfaces
{ {
public interface IUserService public interface IUserService
{ {
Task CreateUserAsync(); Task<SignupResponse> CreateUserAsync(SignupForm form);
Task DeleteUserAsync(string userId); Task DeleteUserAsync(string userId);
Task DeleteUserAsync(string userId, string jti, string password); Task DeleteUserAsync(string userId, string? jti, string password);
Task<bool> ResetUserAsync(string userId, bool removeAttachments); Task<bool> ResetUserAsync(string userId, bool removeAttachments);
Task<UserResponse> GetUserAsync(string userId); Task<UserResponse> GetUserAsync(string userId);
Task SetUserAttachmentsKeyAsync(string userId, IEncrypted key); Task SetUserKeysAsync(string userId, UserKeys keys);
} }
} }
+41
View File
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;
using Notesnook.API.Interfaces;
using Notesnook.API.Models;
using Notesnook.API.Services;
using Quartz;
namespace Notesnook.API.Jobs
{
public class DeviceCleanupJob(ISyncItemsRepositoryAccessor repositories) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var cutoffDate = DateTimeOffset.UtcNow.AddMonths(-1).ToUnixTimeMilliseconds();
var deviceFilter = Builders<SyncDevice>.Filter.Lt(x => x.LastAccessTime, cutoffDate);
using var cursor = await repositories.SyncDevices.Collection.Find(deviceFilter, new FindOptions { BatchSize = 1000 })
.Project(x => x.DeviceId)
.ToCursorAsync();
var deleteModels = new List<WriteModel<DeviceIdsChunk>>();
while (await cursor.MoveNextAsync())
{
if (!cursor.Current.Any()) continue;
deleteModels.Add(new DeleteManyModel<DeviceIdsChunk>(Builders<DeviceIdsChunk>.Filter.In(x => x.DeviceId, cursor.Current)));
}
if (deleteModels.Count > 0)
{
var bulkOptions = new BulkWriteOptions { IsOrdered = false };
await repositories.DeviceIdsChunks.Collection.BulkWriteAsync(deleteModels, bulkOptions);
}
await repositories.SyncDevices.Collection.DeleteManyAsync(deviceFilter);
}
}
}
+25 -25
View File
@@ -1,26 +1,26 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
public class Algorithms public class Algorithms
{ {
public static string Default => "xcha-argon2i13-7"; public static string Default => "xcha-argon2i13-7";
} }
} }
+158 -158
View File
@@ -1,159 +1,159 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
public class Announcement public class Announcement
{ {
public Announcement() public Announcement()
{ {
this.Id = ObjectId.GenerateNewId().ToString(); this.Id = ObjectId.GenerateNewId().ToString();
} }
[BsonId] [BsonId]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
[BsonElement("id")] [BsonElement("id")]
[JsonPropertyName("id")] [JsonPropertyName("id")]
public string Id { get; set; } public string Id { get; set; }
[JsonPropertyName("type")] [JsonPropertyName("type")]
[BsonElement("type")] [BsonElement("type")]
public string Type { get; set; } public required string Type { get; set; }
[JsonPropertyName("timestamp")] [JsonPropertyName("timestamp")]
[BsonElement("timestamp")] [BsonElement("timestamp")]
public long Timestamp { get; set; } public long Timestamp { get; set; }
[JsonPropertyName("platforms")] [JsonPropertyName("platforms")]
[BsonElement("platforms")] [BsonElement("platforms")]
public string[] Platforms { get; set; } public required string[] Platforms { get; set; }
[JsonPropertyName("isActive")] [JsonPropertyName("isActive")]
[BsonElement("isActive")] [BsonElement("isActive")]
public bool IsActive { get; set; } public bool IsActive { get; set; }
[JsonPropertyName("userTypes")] [JsonPropertyName("userTypes")]
[BsonElement("userTypes")] [BsonElement("userTypes")]
public string[] UserTypes { get; set; } public required string[] UserTypes { get; set; }
[JsonPropertyName("appVersion")] [JsonPropertyName("appVersion")]
[BsonElement("appVersion")] [BsonElement("appVersion")]
public int AppVersion { get; set; } public int AppVersion { get; set; }
[JsonPropertyName("body")] [JsonPropertyName("body")]
[BsonElement("body")] [BsonElement("body")]
public BodyComponent[] Body { get; set; } public required BodyComponent[] Body { get; set; }
[JsonIgnore] [JsonIgnore]
[BsonElement("userIds")] [BsonElement("userIds")]
public string[] UserIds { get; set; } public string[]? UserIds { get; set; }
[Obsolete] [Obsolete]
[JsonPropertyName("title")] [JsonPropertyName("title")]
[DataMember(Name = "title")] [DataMember(Name = "title")]
[BsonElement("title")] [BsonElement("title")]
public string Title { get; set; } public string? Title { get; set; }
[Obsolete] [Obsolete]
[JsonPropertyName("description")] [JsonPropertyName("description")]
[BsonElement("description")] [BsonElement("description")]
public string Description { get; set; } public string? Description { get; set; }
[Obsolete] [Obsolete]
[JsonPropertyName("callToActions")] [JsonPropertyName("callToActions")]
[BsonElement("callToActions")] [BsonElement("callToActions")]
public CallToAction[] CallToActions { get; set; } public CallToAction[]? CallToActions { get; set; }
} }
public class BodyComponent public class BodyComponent
{ {
[JsonPropertyName("type")] [JsonPropertyName("type")]
[BsonElement("type")] [BsonElement("type")]
public string Type { get; set; } public required string Type { get; set; }
[JsonPropertyName("platforms")] [JsonPropertyName("platforms")]
[BsonElement("platforms")] [BsonElement("platforms")]
public string[] Platforms { get; set; } public string[]? Platforms { get; set; }
[JsonPropertyName("style")] [JsonPropertyName("style")]
[BsonElement("style")] [BsonElement("style")]
public Style Style { get; set; } public Style? Style { get; set; }
[JsonPropertyName("src")] [JsonPropertyName("src")]
[BsonElement("src")] [BsonElement("src")]
public string Src { get; set; } public string? Src { get; set; }
[JsonPropertyName("text")] [JsonPropertyName("text")]
[BsonElement("text")] [BsonElement("text")]
public string Text { get; set; } public string? Text { get; set; }
[JsonPropertyName("value")] [JsonPropertyName("value")]
[BsonElement("value")] [BsonElement("value")]
public string Value { get; set; } public string? Value { get; set; }
[JsonPropertyName("items")] [JsonPropertyName("items")]
[BsonElement("items")] [BsonElement("items")]
public BodyComponent[] Items { get; set; } public BodyComponent[]? Items { get; set; }
[JsonPropertyName("actions")] [JsonPropertyName("actions")]
[BsonElement("actions")] [BsonElement("actions")]
public CallToAction[] Actions { get; set; } public required CallToAction[] Actions { get; set; }
} }
public class Style public class Style
{ {
[JsonPropertyName("marginTop")] [JsonPropertyName("marginTop")]
[BsonElement("marginTop")] [BsonElement("marginTop")]
public int MarginTop { get; set; } public int MarginTop { get; set; }
[JsonPropertyName("marginBottom")] [JsonPropertyName("marginBottom")]
[BsonElement("marginBottom")] [BsonElement("marginBottom")]
public int MarginBottom { get; set; } public int MarginBottom { get; set; }
[JsonPropertyName("textAlign")] [JsonPropertyName("textAlign")]
[BsonElement("textAlign")] [BsonElement("textAlign")]
public string TextAlign { get; set; } public string? TextAlign { get; set; }
} }
public class CallToAction public class CallToAction
{ {
[JsonPropertyName("type")] [JsonPropertyName("type")]
[BsonElement("type")] [BsonElement("type")]
public string Type { get; set; } public required string Type { get; set; }
[JsonPropertyName("platforms")] [JsonPropertyName("platforms")]
[BsonElement("platforms")] [BsonElement("platforms")]
public string[] Platforms { get; set; } public string[]? Platforms { get; set; }
[JsonPropertyName("data")] [JsonPropertyName("data")]
[BsonElement("data")] [BsonElement("data")]
public string Data { get; set; } public string? Data { get; set; }
[JsonPropertyName("title")] [JsonPropertyName("title")]
[BsonElement("title")] [BsonElement("title")]
public string Title { get; set; } public string? Title { get; set; }
} }
} }
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
namespace Notesnook.API.Models
{
public class ChangePasswordForm
{
public string? OldPassword
{
get; set;
}
[Required]
public required string NewPassword
{
get; set;
}
[Required]
public required UserKeys UserKeys
{
get; set;
}
}
}
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using Amazon.S3.Model;
namespace Notesnook.API.Models;
public class CompleteMultipartUploadRequestWrapper
{
public required string Key { get; set; }
public required List<PartETagWrapper> PartETags { get; set; }
public required string UploadId { get; set; }
public CompleteMultipartUploadRequest ToRequest()
{
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest();
completeMultipartUploadRequest.Key = Key;
completeMultipartUploadRequest.UploadId = UploadId;
completeMultipartUploadRequest.PartETags = [];
foreach (var partETagWrapper in PartETags)
{
var partETag = new PartETag
{
PartNumber = partETagWrapper.PartNumber,
ETag = partETagWrapper.ETag
};
completeMultipartUploadRequest.PartETags.Add(partETag);
}
return completeMultipartUploadRequest;
}
}
@@ -17,17 +17,22 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using Notesnook.API.Models; namespace Notesnook.API.Models
using Streetwriters.Common.Interfaces;
namespace Notesnook.API.Interfaces
{ {
public interface IMonograph : IDocument public enum ContentSanitizationLevel
{ {
string Title { get; set; } Unknown = 0,
string UserId { get; set; } /// <summary>
byte[] CompressedContent { get; set; } /// Full sanitization applied: links, iframes, images, and other embeds are stripped.
EncryptedData EncryptedContent { get; set; } /// Applied to monographs published by free-tier users.
long DatePublished { get; set; } /// </summary>
Full = 1,
/// <summary>
/// Partial sanitization: only unsafe/malicious URLs are removed; rich content is preserved.
/// Applied to monographs published by subscribed users. Requires re-sanitization if the
/// publisher's subscription lapses.
/// </summary>
Partial = 2
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Notesnook.API.Models
public class DeleteAccountForm public class DeleteAccountForm
{ {
[Required] [Required]
public string Password public required string Password
{ {
get; set; get; set;
} }
@@ -17,16 +17,9 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Collections.Generic; namespace Notesnook.API.Models;
using Streetwriters.Common.Enums;
using Streetwriters.Common.Models;
namespace Streetwriters.Common.Interfaces public class DeleteBulkObjectsRequest
{ {
public interface IOffer : IDocument public required string[] Names { get; set; }
{
ApplicationType AppId { get; set; }
string PromoCode { get; set; }
PromoCode[] Codes { get; set; }
}
} }
+16
View File
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Notesnook.API.Models
{
public class DeviceIdsChunk
{
[BsonId]
public ObjectId Id { get; set; }
public required string UserId { get; set; }
public required string DeviceId { get; set; }
public required string Key { get; set; }
public required string[] Ids { get; set; } = [];
}
}
+69 -75
View File
@@ -1,75 +1,69 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
[MessagePack.MessagePackObject] [MessagePack.MessagePackObject]
public class EncryptedData : IEncrypted public class EncryptedData
{ {
[MessagePack.Key("iv")] [MessagePack.Key("iv")]
[JsonPropertyName("iv")] [JsonPropertyName("iv")]
[BsonElement("iv")] [BsonElement("iv")]
[DataMember(Name = "iv")] [DataMember(Name = "iv")]
public string IV public required string IV { get; set; }
{
get; set; [MessagePack.Key("cipher")]
} [JsonPropertyName("cipher")]
[BsonElement("cipher")]
[MessagePack.Key("cipher")] [DataMember(Name = "cipher")]
[JsonPropertyName("cipher")] public required string Cipher { get; set; }
[BsonElement("cipher")]
[DataMember(Name = "cipher")] [MessagePack.Key("length")]
public string Cipher [JsonPropertyName("length")]
{ [BsonElement("length")]
get; set; [DataMember(Name = "length")]
} public long Length { get; set; }
[MessagePack.Key("length")] [MessagePack.Key("salt")]
[JsonPropertyName("length")] [JsonPropertyName("salt")]
[BsonElement("length")] [BsonElement("salt")]
[DataMember(Name = "length")] [DataMember(Name = "salt")]
public long Length { get; set; } public required string Salt { get; set; }
[MessagePack.Key("salt")] public override bool Equals(object? obj)
[JsonPropertyName("salt")] {
[BsonElement("salt")] if (obj is EncryptedData encryptedData)
[DataMember(Name = "salt")] {
public string Salt { get; set; } return IV == encryptedData.IV && Salt == encryptedData.Salt && Cipher == encryptedData.Cipher && Length == encryptedData.Length;
}
public override bool Equals(object obj) return base.Equals(obj);
{ }
if (obj is EncryptedData encryptedData)
{ public bool IsEmpty()
return IV == encryptedData.IV && Salt == encryptedData.Salt && Cipher == encryptedData.Cipher && Length == encryptedData.Length; {
} return this.Cipher == null && this.IV == null && this.Length == 0 && this.Salt == null;
return base.Equals(obj); }
} }
}
public bool IsEmpty()
{
return this.Cipher == null && this.IV == null && this.Length == 0 && this.Salt == null;
}
}
}
+69
View File
@@ -0,0 +1,69 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using NanoidDotNet;
namespace Notesnook.API.Models
{
public class CreateInboxApiKeyRequest
{
[JsonPropertyName("name")]
public required string Name { get; set; }
[JsonPropertyName("expiryDate")]
public long ExpiryDate { get; set; }
}
public class InboxApiKey
{
public InboxApiKey()
{
var random = Nanoid.Generate(size: 64);
Key = "nn__" + random;
}
[BsonId]
[BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)]
[JsonIgnore]
[MessagePack.IgnoreMember]
public string Id { get; set; } = string.Empty;
[JsonPropertyName("userId")]
public required string UserId { get; set; }
[JsonPropertyName("name")]
public required string Name { get; set; }
[JsonPropertyName("key")]
public string Key { get; set; } = string.Empty;
[JsonPropertyName("dateCreated")]
public long DateCreated { get; set; }
[JsonPropertyName("expiryDate")]
public long ExpiryDate { get; set; }
[JsonPropertyName("lastUsedAt")]
public long LastUsedAt { get; set; }
}
}
+85
View File
@@ -0,0 +1,85 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Notesnook.API.Models
{
[MessagePack.MessagePackObject]
public class InboxSyncItem
{
[DataMember(Name = "cipher")]
[JsonPropertyName("cipher")]
[MessagePack.Key("cipher")]
[Required]
public string Cipher
{
get; set;
}
[DataMember(Name = "userId")]
[JsonPropertyName("userId")]
[MessagePack.Key("userId")]
public string? UserId
{
get; set;
}
[DataMember(Name = "id")]
[JsonPropertyName("id")]
[MessagePack.Key("id")]
public string? ItemId
{
get; set;
}
[BsonId]
[BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)]
[JsonIgnore]
[MessagePack.IgnoreMember]
public ObjectId Id
{
get; set;
}
[JsonPropertyName("v")]
[DataMember(Name = "v")]
[MessagePack.Key("v")]
[Required]
public double Version
{
get; set;
}
[JsonPropertyName("alg")]
[DataMember(Name = "alg")]
[MessagePack.Key("alg")]
[Required]
public string Algorithm
{
get; set;
}
}
}
+92 -66
View File
@@ -1,67 +1,93 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Notesnook.API.Interfaces; using System.Runtime.Serialization;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
public class ObjectWithId public class ObjectWithId
{ {
[BsonId] [BsonId]
[BsonRepresentation(BsonType.ObjectId)] [BsonIgnoreIfDefault]
public string Id { get; set; } [BsonRepresentation(BsonType.ObjectId)]
} public required string Id { get; set; }
public class Monograph : IMonograph public required string ItemId { get; set; }
{ }
public Monograph()
{ public class Monograph
Id = ObjectId.GenerateNewId().ToString(); {
} public Monograph()
{
[BsonId] Id = ObjectId.GenerateNewId().ToString();
[BsonRepresentation(BsonType.ObjectId)] }
public string Id { get; set; }
[DataMember(Name = "id")]
[JsonPropertyName("title")] [JsonPropertyName("id")]
public string Title { get; set; } [MessagePack.Key("id")]
public string? ItemId { get; set; }
[JsonPropertyName("userId")]
public string UserId { get; set; } [BsonId]
[BsonIgnoreIfDefault]
[JsonPropertyName("selfDestruct")] [BsonRepresentation(BsonType.ObjectId)]
public bool SelfDestruct { get; set; } [JsonIgnore]
[MessagePack.IgnoreMember]
[JsonPropertyName("encryptedContent")] public string Id { get; set; } = string.Empty;
public EncryptedData EncryptedContent { get; set; }
[JsonPropertyName("title")]
[JsonPropertyName("datePublished")] public string? Title { get; set; }
public long DatePublished { get; set; }
[JsonPropertyName("slug")]
[JsonPropertyName("content")] public string? Slug { get; set; }
[BsonIgnore]
public string Content { get; set; } [JsonPropertyName("userId")]
public string? UserId { get; set; }
[JsonIgnore]
public byte[] CompressedContent { get; set; } [JsonPropertyName("selfDestruct")]
} public bool SelfDestruct { get; set; }
[JsonPropertyName("encryptedContent")]
public EncryptedData? EncryptedContent { get; set; }
[JsonPropertyName("datePublished")]
public long DatePublished { get; set; }
[JsonPropertyName("content")]
[BsonIgnore]
public string? Content { get; set; }
[JsonIgnore]
public byte[]? CompressedContent { get; set; }
[JsonPropertyName("password")]
public EncryptedData? Password { get; set; }
[JsonPropertyName("deleted")]
public bool Deleted { get; set; }
[JsonPropertyName("viewCount")]
public int ViewCount { get; set; }
[JsonIgnore]
public ContentSanitizationLevel ContentSanitizationLevel { get; set; }
}
} }
@@ -1,33 +1,35 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System.Text.Json.Serialization;
using System.Text.Json.Serialization; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Streetwriters.Common.Attributes using System.Runtime.Serialization;
{
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)] namespace Notesnook.API.Models
public class JsonInterfaceConverterAttribute : JsonConverterAttribute {
{
public JsonInterfaceConverterAttribute(Type converterType) public class MonographContent
: base(converterType) {
{ [JsonPropertyName("data")]
} public required string Data { get; set; }
} [JsonPropertyName("type")]
public required string Type { get; set; }
}
} }
+53
View File
@@ -0,0 +1,53 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
namespace Notesnook.API.Models
{
public class MonographMetadata
{
[DataMember(Name = "id")]
[JsonPropertyName("id")]
[MessagePack.Key("id")]
public required string ItemId
{
get; set;
}
[JsonPropertyName("title")]
public string? Title { get; set; }
[JsonPropertyName("publishUrl")]
public string? PublishUrl { get; set; }
[JsonPropertyName("selfDestruct")]
public bool SelfDestruct { get; set; }
[JsonPropertyName("datePublished")]
public long DatePublished { get; set; }
[JsonPropertyName("password")]
public EncryptedData? Password { get; set; }
[JsonPropertyName("deleted")]
public bool Deleted { get; set; }
}
}
+28 -26
View File
@@ -1,27 +1,29 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Notesnook.API.Models using System;
{
public class MultipartUploadMeta namespace Notesnook.API.Models
{ {
public string UploadId { get; set; } public class MultipartUploadMeta
public string[] Parts { get; set; } {
} public string UploadId { get; set; } = string.Empty;
public string[] Parts { get; set; } = Array.Empty<string>();
}
} }
+7
View File
@@ -0,0 +1,7 @@
namespace Notesnook.API.Models;
public class PartETagWrapper
{
public int PartNumber { get; set; }
public string ETag { get; set; } = string.Empty;
}
+19
View File
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
namespace Notesnook.API.Models
{
public class ResetPasswordForm
{
[Required]
public required string NewPassword
{
get; set;
}
[Required]
public required UserKeys UserKeys
{
get; set;
}
}
}
@@ -1,14 +0,0 @@
using System.Text.Json.Serialization;
using Streetwriters.Common.Models;
namespace Notesnook.API.Models.Responses
{
public class SignupResponse : Response
{
[JsonPropertyName("userId")]
public string UserId { get; set; }
[JsonPropertyName("errors")]
public string[] Errors { get; set; }
}
}
+42 -25
View File
@@ -1,25 +1,42 @@
using System.Text.Json.Serialization; using System.Net.Http;
using Streetwriters.Common.Interfaces; using System.Text.Json.Serialization;
using Streetwriters.Common.Models; using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Models;
namespace Notesnook.API.Models.Responses
{ namespace Notesnook.API.Models.Responses
public class UserResponse : UserModel, IResponse {
{ public class UserResponse : UserModel, IResponse
[JsonPropertyName("salt")] {
public string Salt { get; set; } [JsonPropertyName("salt")]
public string? Salt { get; set; }
[JsonPropertyName("attachmentsKey")]
public EncryptedData AttachmentsKey { get; set; } [JsonPropertyName("attachmentsKey")]
public EncryptedData? AttachmentsKey { get; set; }
[JsonPropertyName("subscription")]
public ISubscription Subscription { get; set; } [JsonPropertyName("monographPasswordsKey")]
public EncryptedData? MonographPasswordsKey { get; set; }
[JsonPropertyName("profile")] [JsonPropertyName("dataEncryptionKey")]
public EncryptedData Profile { get; set; } public EncryptedData? DataEncryptionKey { get; set; }
[JsonIgnore] [JsonPropertyName("legacyDataEncryptionKey")]
public bool Success { get; set; } public EncryptedData? LegacyDataEncryptionKey { get; set; }
public int StatusCode { get; set; }
} [JsonPropertyName("inboxKeys")]
} public InboxKeys? InboxKeys { get; set; }
[JsonPropertyName("subscription")]
public Subscription? Subscription { get; set; }
[JsonPropertyName("storageUsed")]
public long StorageUsed { get; set; }
[JsonPropertyName("totalStorage")]
public long TotalStorage { get; set; }
[JsonIgnore]
public bool Success { get; set; }
public int StatusCode { get; set; }
[JsonIgnore]
public HttpContent? Content { get; set; }
}
}
+28 -28
View File
@@ -1,29 +1,29 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
public class S3Options public class S3Options
{ {
public string ServiceUrl { get; set; } public string ServiceUrl { get; set; } = string.Empty;
public string Region { get; set; } public string Region { get; set; } = string.Empty;
public string AccessKeyId { get; set; } public string AccessKeyId { get; set; } = string.Empty;
public string SecretAccessKey { get; set; } public string SecretAccessKey { get; set; } = string.Empty;
} }
} }
+19
View File
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Notesnook.API.Models
{
public class SyncDevice
{
[BsonId]
public ObjectId Id { get; set; }
public required string UserId { get; set; }
public required string DeviceId { get; set; }
public required long LastAccessTime { get; set; }
public required bool IsSyncReset { get; set; }
public string? AppVersion { get; set; }
public string? DatabaseVersion { get; set; }
}
}
+13 -14
View File
@@ -44,7 +44,7 @@ namespace Notesnook.API.Models
[DataMember(Name = "userId")] [DataMember(Name = "userId")]
[JsonPropertyName("userId")] [JsonPropertyName("userId")]
[MessagePack.Key("userId")] [MessagePack.Key("userId")]
public string UserId public string? UserId
{ {
get; set; get; set;
} }
@@ -53,25 +53,19 @@ namespace Notesnook.API.Models
[DataMember(Name = "iv")] [DataMember(Name = "iv")]
[MessagePack.Key("iv")] [MessagePack.Key("iv")]
[Required] [Required]
public string IV public string IV { get; set; } = string.Empty;
{
get; set;
}
[JsonPropertyName("cipher")] [JsonPropertyName("cipher")]
[DataMember(Name = "cipher")] [DataMember(Name = "cipher")]
[MessagePack.Key("cipher")] [MessagePack.Key("cipher")]
[Required] [Required]
public string Cipher public string Cipher { get; set; } = string.Empty;
{
get; set;
}
[DataMember(Name = "id")] [DataMember(Name = "id")]
[JsonPropertyName("id")] [JsonPropertyName("id")]
[MessagePack.Key("id")] [MessagePack.Key("id")]
public string ItemId public string? ItemId
{ {
get; set; get; set;
} }
@@ -104,14 +98,19 @@ namespace Notesnook.API.Models
get; set; get; set;
} }
[JsonPropertyName("keyVersion")]
[DataMember(Name = "keyVersion")]
[MessagePack.Key("keyVersion")]
public int? KeyVersion
{
get; set;
}
[JsonPropertyName("alg")] [JsonPropertyName("alg")]
[DataMember(Name = "alg")] [DataMember(Name = "alg")]
[MessagePack.Key("alg")] [MessagePack.Key("alg")]
[Required] [Required]
public string Algorithm public string Algorithm { get; set; } = string.Empty;
{
get; set;
} = Algorithms.Default;
} }
public class SyncItemBsonSerializer : SerializerBase<SyncItem> public class SyncItemBsonSerializer : SerializerBase<SyncItem>
@@ -17,26 +17,20 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using Notesnook.API.Models; namespace Notesnook.API.Models
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Models;
namespace Notesnook.API.Interfaces
{ {
public interface IUserSettings : IDocument public class UserKeys
{ {
string UserId { get; set; } public EncryptedData? AttachmentsKey { get; set; }
public EncryptedData? MonographPasswordsKey { get; set; }
public InboxKeys? InboxKeys { get; set; }
public EncryptedData? DataEncryptionKey { get; set; }
public EncryptedData? LegacyDataEncryptionKey { get; set; }
}
long LastSynced public class InboxKeys
{ {
get; set; public string? Public { get; set; }
} public EncryptedData? Private { get; set; }
EncryptedData VaultKey
{
get; set;
}
string Salt { get; set; }
} }
} }
+67 -42
View File
@@ -1,42 +1,67 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using MongoDB.Bson; using System;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson;
using Notesnook.API.Interfaces; using MongoDB.Bson.Serialization.Attributes;
using Notesnook.API.Interfaces;
namespace Notesnook.API.Models
{ namespace Notesnook.API.Models
public class UserSettings : IUserSettings {
{ public class Limit
public UserSettings() {
{ private long _value = 0;
this.Id = ObjectId.GenerateNewId().ToString(); public long Value
} {
public string UserId { get; set; } get => _value;
public long LastSynced { get; set; } set
public string Salt { get; set; } {
public EncryptedData VaultKey { get; set; } _value = value;
public EncryptedData AttachmentsKey { get; set; } UpdatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
[BsonId] }
[BsonRepresentation(BsonType.ObjectId)] public long UpdatedAt
public string Id { get; set; } {
} get;
} set;
}
}
public class UserSettings
{
public UserSettings()
{
this.Id = ObjectId.GenerateNewId();
}
public required string UserId { get; set; }
public long LastSynced { get; set; }
public required string Salt { get; set; }
public EncryptedData? VaultKey { get; set; }
public EncryptedData? AttachmentsKey { get; set; }
public EncryptedData? MonographPasswordsKey { get; set; }
public EncryptedData? DataEncryptionKey { get; set; }
public EncryptedData? LegacyDataEncryptionKey { get; set; }
public InboxKeys? InboxKeys { get; set; }
public Limit? StorageLimit { get; set; }
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }
}
}
+9 -1
View File
@@ -1,11 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<StartupObject>Notesnook.API.Program</StartupObject> <StartupObject>Notesnook.API.Program</StartupObject>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AngleSharp" Version="1.3.0" />
<PackageReference Include="AspNetCore.HealthChecks.Aws.S3" Version="9.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="9.0.0" />
<PackageReference Include="AWSSDK.Core" Version="3.7.304.31" /> <PackageReference Include="AWSSDK.Core" Version="3.7.304.31" />
<PackageReference Include="DotNetEnv" Version="2.3.0" /> <PackageReference Include="DotNetEnv" Version="2.3.0" />
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.2.0" /> <PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.2.0" />
@@ -14,9 +18,13 @@
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1-rc2.2" /> <PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1-rc2.2" />
<PackageReference Include="AWSSDK.S3" Version="3.7.310.8" /> <PackageReference Include="AWSSDK.S3" Version="3.7.310.8" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="6.0.3" /> <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="9.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" />
<PackageReference Include="Nanoid" Version="3.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-alpha.2" /> <PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-alpha.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" /> <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="Quartz" Version="3.5.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+69 -68
View File
@@ -1,68 +1,69 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#if !DEBUG using System.Net;
using System.Net; using System.Threading.Tasks;
#endif using Microsoft.AspNetCore.Hosting;
using System; using Microsoft.Extensions.Hosting;
using System.Threading.Tasks; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Hosting; using Streetwriters.Common;
using Microsoft.Extensions.Hosting;
using Streetwriters.Common; namespace Notesnook.API
using System.Linq; {
using Microsoft.Extensions.Logging; public class Program
using System.Net; {
public static async Task Main(string[] args)
namespace Notesnook.API {
{ #if (DEBUG || STAGING)
public class Program DotNetEnv.Env.TraversePath().Load(".env.local");
{ #else
public static async Task Main(string[] args) DotNetEnv.Env.TraversePath().Load(".env");
{ #endif
#if DEBUG IHost host = CreateHostBuilder(args).Build();
DotNetEnv.Env.TraversePath().Load(".env.local"); await host.RunAsync();
#else }
DotNetEnv.Env.TraversePath().Load(".env");
#endif public static IHostBuilder CreateHostBuilder(string[] args) =>
IHost host = CreateHostBuilder(args).Build(); Host.CreateDefaultBuilder(args)
await host.RunAsync(); .ConfigureLogging(logging =>
} {
logging.ClearProviders();
public static IHostBuilder CreateHostBuilder(string[] args) => logging.AddConsole();
Host.CreateDefaultBuilder(args) logging.AddSystemdConsole();
.ConfigureWebHostDefaults(webBuilder => })
{ .ConfigureWebHostDefaults(webBuilder =>
webBuilder {
.UseStartup<Startup>() webBuilder
.UseKestrel((options) => .UseStartup<Startup>()
{ .UseKestrel((options) =>
options.Limits.MaxRequestBodySize = long.MaxValue; {
options.ListenAnyIP(Servers.NotesnookAPI.Port); options.Limits.MaxRequestBodySize = long.MaxValue;
if (Servers.NotesnookAPI.IsSecure) options.ListenAnyIP(Servers.NotesnookAPI.Port);
{ if (Servers.NotesnookAPI.IsSecure && Servers.NotesnookAPI.SSLCertificate != null)
options.ListenAnyIP(443, listenerOptions => {
{ options.ListenAnyIP(443, listenerOptions =>
listenerOptions.UseHttps(Servers.NotesnookAPI.SSLCertificate); {
}); listenerOptions.UseHttps(Servers.NotesnookAPI.SSLCertificate);
} });
options.Listen(IPAddress.Parse("127.0.0.1"), 5067); }
}); options.Listen(IPAddress.Parse("127.0.0.1"), 5067);
}); });
} });
} }
}
@@ -26,6 +26,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using IdentityModel; using IdentityModel;
using Microsoft.VisualBasic; using Microsoft.VisualBasic;
using Microsoft.Extensions.Logging;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using Notesnook.API.Hubs; using Notesnook.API.Hubs;
@@ -41,16 +42,11 @@ namespace Notesnook.API.Repositories
public class SyncItemsRepository : Repository<SyncItem> public class SyncItemsRepository : Repository<SyncItem>
{ {
private readonly string collectionName; private readonly string collectionName;
public SyncItemsRepository(IDbContext dbContext, IMongoCollection<SyncItem> collection) : base(dbContext, collection) private readonly ILogger<SyncItemsRepository> logger;
public SyncItemsRepository(IDbContext dbContext, IMongoCollection<SyncItem> collection, ILogger<SyncItemsRepository> logger) : base(dbContext, collection)
{ {
this.collectionName = collection.CollectionNamespace.CollectionName; this.collectionName = collection.CollectionNamespace.CollectionName;
#if DEBUG this.logger = logger;
Collection.Indexes.CreateMany([
new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending("UserId").Descending("DateSynced")),
new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending("UserId").Ascending("ItemId")),
new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending("UserId"))
]);
#endif
} }
private readonly List<string> ALGORITHMS = [Algorithms.Default]; private readonly List<string> ALGORITHMS = [Algorithms.Default];
@@ -95,11 +91,7 @@ namespace Notesnook.API.Repositories
public void DeleteByUserId(string userId) public void DeleteByUserId(string userId)
{ {
var filter = Builders<SyncItem>.Filter.Eq("UserId", userId); var filter = Builders<SyncItem>.Filter.Eq("UserId", userId);
var writes = new List<WriteModel<SyncItem>> dbContext.AddCommand((handle, ct) => Collection.DeleteManyAsync(handle, filter, null, ct));
{
new DeleteManyModel<SyncItem>(filter)
};
dbContext.AddCommand((handle, ct) => Collection.BulkWriteAsync(handle, writes, options: null, ct));
} }
public void Upsert(SyncItem item, string userId, long dateSynced) public void Upsert(SyncItem item, string userId, long dateSynced)
@@ -117,10 +109,14 @@ namespace Notesnook.API.Repositories
// Handle case where the cipher is corrupted. // Handle case where the cipher is corrupted.
if (!IsBase64String(item.Cipher)) if (!IsBase64String(item.Cipher))
{ {
Slogger<SyncHub>.Error("Upsert", "Corrupted", item.ItemId, item.Length.ToString(), item.Cipher); logger.LogError("Corrupted item {ItemId} in collection {CollectionName}. Length: {Length}, Cipher: {Cipher}",
item.ItemId, this.collectionName, item.Length, item.Cipher);
throw new Exception($"Corrupted item \"{item.ItemId}\" in collection \"{this.collectionName}\". Please report this error to support@streetwriters.co."); throw new Exception($"Corrupted item \"{item.ItemId}\" in collection \"{this.collectionName}\". Please report this error to support@streetwriters.co.");
} }
if (item.ItemId == null)
throw new Exception($"Item does not have an ItemId.");
item.DateSynced = dateSynced; item.DateSynced = dateSynced;
item.UserId = userId; item.UserId = userId;
@@ -151,10 +147,14 @@ namespace Notesnook.API.Repositories
// Handle case where the cipher is corrupted. // Handle case where the cipher is corrupted.
if (!IsBase64String(item.Cipher)) if (!IsBase64String(item.Cipher))
{ {
Slogger<SyncHub>.Error("Upsert", "Corrupted", item.ItemId, item.Length.ToString(), item.Cipher); logger.LogError("Corrupted item {ItemId} in collection {CollectionName}. Length: {Length}, Cipher: {Cipher}",
item.ItemId, this.collectionName, item.Length, item.Cipher);
throw new Exception($"Corrupted item \"{item.ItemId}\" in collection \"{this.collectionName}\". Please report this error to support@streetwriters.co."); throw new Exception($"Corrupted item \"{item.ItemId}\" in collection \"{this.collectionName}\". Please report this error to support@streetwriters.co.");
} }
if (item.ItemId == null)
throw new Exception($"Item does not have an ItemId.");
var filter = Builders<SyncItem>.Filter.And( var filter = Builders<SyncItem>.Filter.And(
userIdFilter, userIdFilter,
Builders<SyncItem>.Filter.Eq("ItemId", item.ItemId) Builders<SyncItem>.Filter.Eq("ItemId", item.ItemId)
+215 -101
View File
@@ -24,13 +24,15 @@ using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Amazon; using Amazon;
using Amazon.Runtime;
using Amazon.S3; using Amazon.S3;
using Amazon.S3.Model; using Amazon.S3.Model;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Notesnook.API.Helpers;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Models; using Notesnook.API.Models;
using Streetwriters.Common; using Streetwriters.Common;
using Streetwriters.Common.Accessors;
namespace Notesnook.API.Services namespace Notesnook.API.Services
{ {
@@ -42,9 +44,11 @@ namespace Notesnook.API.Services
public class S3Service : IS3Service public class S3Service : IS3Service
{ {
private readonly string BUCKET_NAME = Constants.S3_BUCKET_NAME ?? ""; private readonly string BUCKET_NAME = Constants.S3_BUCKET_NAME;
private readonly string INTERNAL_BUCKET_NAME = Constants.S3_INTERNAL_BUCKET_NAME ?? ""; private readonly string INTERNAL_BUCKET_NAME = Constants.S3_INTERNAL_BUCKET_NAME ?? Constants.S3_BUCKET_NAME;
private AmazonS3Client S3Client { get; } private readonly S3FailoverHelper S3Client;
private ISyncItemsRepositoryAccessor Repositories { get; }
private WampServiceAccessor ServiceAccessor { get; }
// When running in a dockerized environment the sync server doesn't have access // When running in a dockerized environment the sync server doesn't have access
// to the host's S3 Service URL. It can only talk to S3 server via its own internal // to the host's S3 Service URL. It can only talk to S3 server via its own internal
@@ -54,60 +58,121 @@ namespace Notesnook.API.Services
// URLs generated by S3 are host specific. Changing their hostname on the fly causes // URLs generated by S3 are host specific. Changing their hostname on the fly causes
// SignatureDoesNotMatch error. // SignatureDoesNotMatch error.
// That is why we create 2 separate S3 clients. One for internal traffic and one for external. // That is why we create 2 separate S3 clients. One for internal traffic and one for external.
private AmazonS3Client S3InternalClient { get; } private readonly S3FailoverHelper S3InternalClient;
private HttpClient httpClient = new HttpClient(); private readonly HttpClient httpClient = new();
public S3Service() public S3Service(ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor, WampServiceAccessor wampServiceAccessor, ILogger<S3Service> logger)
{ {
var config = new AmazonS3Config Repositories = syncItemsRepositoryAccessor;
{ ServiceAccessor = wampServiceAccessor;
#if DEBUG S3Client = new S3FailoverHelper(
ServiceURL = Servers.S3Server.ToString(), S3ClientFactory.CreateS3Clients(
#else Constants.S3_SERVICE_URL,
ServiceURL = Constants.S3_SERVICE_URL, Constants.S3_REGION,
AuthenticationRegion = Constants.S3_REGION, Constants.S3_ACCESS_KEY_ID,
#endif Constants.S3_ACCESS_KEY,
ForcePathStyle = true, forcePathStyle: true
SignatureMethod = SigningAlgorithm.HmacSHA256, ),
SignatureVersion = "4" logger: logger
}; );
#if DEBUG
S3Client = new AmazonS3Client("S3RVER", "S3RVER", config);
#else
S3Client = new AmazonS3Client(Constants.S3_ACCESS_KEY_ID, Constants.S3_ACCESS_KEY, config);
#endif
if (!string.IsNullOrEmpty(Constants.S3_INTERNAL_SERVICE_URL)) if (!string.IsNullOrEmpty(Constants.S3_INTERNAL_SERVICE_URL) && !string.IsNullOrEmpty(Constants.S3_INTERNAL_BUCKET_NAME))
{ {
S3InternalClient = new AmazonS3Client(Constants.S3_ACCESS_KEY_ID, Constants.S3_ACCESS_KEY, new AmazonS3Config S3InternalClient = new S3FailoverHelper(
{ S3ClientFactory.CreateS3Clients(
ServiceURL = Constants.S3_INTERNAL_SERVICE_URL, Constants.S3_INTERNAL_SERVICE_URL,
AuthenticationRegion = Constants.S3_REGION, Constants.S3_REGION,
ForcePathStyle = true, Constants.S3_ACCESS_KEY_ID,
SignatureMethod = SigningAlgorithm.HmacSHA256, Constants.S3_ACCESS_KEY,
SignatureVersion = "4" forcePathStyle: true
}); ),
logger: logger
);
} }
else S3InternalClient = S3Client;
AWSConfigsS3.UseSignatureVersion4 = true; AWSConfigsS3.UseSignatureVersion4 = true;
} }
public async Task DeleteObjectAsync(string userId, string name) public async Task DeleteObjectAsync(string userId, string name)
{ {
var objectName = GetFullObjectName(userId, name); var objectName = GetFullObjectName(userId, name) ?? throw new Exception("Invalid object name.");
if (objectName == null) throw new Exception("Invalid object name."); ;
var response = await GetS3Client(S3ClientMode.INTERNAL).DeleteObjectAsync(GetBucketName(S3ClientMode.INTERNAL), objectName); var response = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.DeleteObjectAsync(INTERNAL_BUCKET_NAME, objectName), operationName: "DeleteObject", isWriteOperation: true);
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) if (!IsSuccessStatusCode((int)response.HttpStatusCode))
throw new Exception("Could not delete object."); throw new Exception("Could not delete object.");
} }
public async Task DeleteObjectsAsync(string userId, string[] names)
{
var objectsToDelete = new List<KeyVersion>();
foreach (var name in names)
{
var objectName = GetFullObjectName(userId, name);
if (objectName == null) continue;
objectsToDelete.Add(new KeyVersion { Key = objectName });
}
if (objectsToDelete.Count == 0)
{
return;
}
// S3 DeleteObjectsRequest supports max 1000 keys per request
var batchSize = 1000;
var deleteErrors = new List<DeleteError>();
var failedBatches = 0;
for (int i = 0; i < objectsToDelete.Count; i += batchSize)
{
var batch = objectsToDelete.Skip(i).Take(batchSize).ToList();
var deleteObjectsResponse = await S3InternalClient.ExecuteWithFailoverAsync(
(client) => client.DeleteObjectsAsync(new DeleteObjectsRequest
{
BucketName = INTERNAL_BUCKET_NAME,
Objects = batch,
}),
operationName: "DeleteObjects",
isWriteOperation: true
);
if (!IsSuccessStatusCode((int)deleteObjectsResponse.HttpStatusCode))
{
failedBatches++;
}
if (deleteObjectsResponse.DeleteErrors.Count > 0)
{
deleteErrors.AddRange(deleteObjectsResponse.DeleteErrors);
}
}
if (failedBatches > 0 || deleteErrors.Count > 0)
{
var errorParts = new List<string>();
if (failedBatches > 0)
{
errorParts.Add($"{failedBatches} batch(es) failed with unsuccessful status code");
}
if (deleteErrors.Count > 0)
{
errorParts.Add(string.Join(", ", deleteErrors.Select(e => $"{e.Key}: {e.Message}")));
}
throw new Exception(string.Join("; ", errorParts));
}
}
public async Task DeleteDirectoryAsync(string userId) public async Task DeleteDirectoryAsync(string userId)
{ {
var request = new ListObjectsV2Request var request = new ListObjectsV2Request
{ {
BucketName = GetBucketName(S3ClientMode.INTERNAL), BucketName = INTERNAL_BUCKET_NAME,
Prefix = userId, Prefix = userId,
}; };
@@ -115,7 +180,7 @@ namespace Notesnook.API.Services
var keys = new List<KeyVersion>(); var keys = new List<KeyVersion>();
do do
{ {
response = await GetS3Client(S3ClientMode.INTERNAL).ListObjectsV2Async(request); response = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.ListObjectsV2Async(request), operationName: "ListObjectsV2");
response.S3Objects.ForEach(obj => keys.Add(new KeyVersion response.S3Objects.ForEach(obj => keys.Add(new KeyVersion
{ {
Key = obj.Key, Key = obj.Key,
@@ -127,12 +192,11 @@ namespace Notesnook.API.Services
if (keys.Count <= 0) return; if (keys.Count <= 0) return;
var deleteObjectsResponse = await GetS3Client(S3ClientMode.INTERNAL) var deleteObjectsResponse = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.DeleteObjectsAsync(new DeleteObjectsRequest
.DeleteObjectsAsync(new DeleteObjectsRequest
{ {
BucketName = GetBucketName(S3ClientMode.INTERNAL), BucketName = INTERNAL_BUCKET_NAME,
Objects = keys, Objects = keys,
}); }), operationName: "DeleteObjects", isWriteOperation: true);
if (!IsSuccessStatusCode((int)deleteObjectsResponse.HttpStatusCode)) if (!IsSuccessStatusCode((int)deleteObjectsResponse.HttpStatusCode))
throw new Exception("Could not delete directory."); throw new Exception("Could not delete directory.");
@@ -140,44 +204,51 @@ namespace Notesnook.API.Services
public async Task<long> GetObjectSizeAsync(string userId, string name) public async Task<long> GetObjectSizeAsync(string userId, string name)
{ {
var url = this.GetPresignedURL(userId, name, HttpVerb.HEAD, S3ClientMode.INTERNAL); var url = await this.GetPresignedURLAsync(userId, name, HttpVerb.HEAD, S3ClientMode.INTERNAL);
if (url == null) return 0; if (url == null) return 0;
var request = new HttpRequestMessage(HttpMethod.Head, url); var request = new HttpRequestMessage(HttpMethod.Head, url);
var response = await httpClient.SendAsync(request); var response = await httpClient.SendAsync(request);
const long MAX_SIZE = 513 * 1024 * 1024; // 512 MB
if (!Constants.IS_SELF_HOSTED && response.Content.Headers.ContentLength >= MAX_SIZE)
{
await this.DeleteObjectAsync(userId, name);
throw new Exception("File size exceeds the maximum allowed size.");
}
return response.Content.Headers.ContentLength ?? 0; return response.Content.Headers.ContentLength ?? 0;
} }
public string GetUploadObjectUrl(string userId, string name) public async Task<string?> GetUploadObjectUrlAsync(string userId, string name)
{ {
var url = this.GetPresignedURL(userId, name, HttpVerb.PUT); return await this.GetPresignedURLAsync(userId, name, HttpVerb.PUT);
}
public async Task<string?> GetInternalUploadObjectUrlAsync(string userId, string name)
{
return await this.GetPresignedURLAsync(userId, name, HttpVerb.PUT, S3ClientMode.INTERNAL);
}
public async Task<string?> GetDownloadObjectUrlAsync(string userId, string name)
{
// var subscriptionService = await WampServers.SubscriptionServer.GetServiceAsync<IUserSubscriptionService>(SubscriptionServerTopics.UserSubscriptionServiceTopic);
// var subscription = await subscriptionService.GetUserSubscriptionAsync(Clients.Notesnook.Id, userId);
// var size = await GetObjectSizeAsync(userId, name);
// if (StorageHelper.IsFileSizeExceeded(subscription, size))
// {
// var fileSizeLimit = StorageHelper.GetFileSizeLimitForPlan(subscription);
// throw new Exception($"You cannot download files larger than {StorageHelper.FormatBytes(fileSizeLimit)} on this plan.");
// }
var url = await this.GetPresignedURLAsync(userId, name, HttpVerb.GET);
if (url == null) return null; if (url == null) return null;
return url; return url;
} }
public string GetDownloadObjectUrl(string userId, string name) public async Task<MultipartUploadMeta> StartMultipartUploadAsync(string userId, string name, int parts, string? uploadId = null)
{
var url = this.GetPresignedURL(userId, name, HttpVerb.GET);
if (url == null) return null;
return url;
}
public async Task<MultipartUploadMeta> StartMultipartUploadAsync(string userId, string name, int parts, string uploadId = null)
{ {
var objectName = GetFullObjectName(userId, name); var objectName = GetFullObjectName(userId, name);
if (userId == null || objectName == null) throw new Exception("Could not initiate multipart upload."); if (userId == null || objectName == null) throw new Exception("Could not initiate multipart upload.");
if (string.IsNullOrEmpty(uploadId)) if (string.IsNullOrEmpty(uploadId))
{ {
var response = await GetS3Client(S3ClientMode.INTERNAL).InitiateMultipartUploadAsync(GetBucketName(S3ClientMode.INTERNAL), objectName); var response = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.InitiateMultipartUploadAsync(INTERNAL_BUCKET_NAME, objectName), operationName: "InitiateMultipartUpload", isWriteOperation: true);
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to initiate multipart upload."); if (!IsSuccessStatusCode((int)response.HttpStatusCode)) throw new Exception("Failed to initiate multipart upload.");
uploadId = response.UploadId; uploadId = response.UploadId;
} }
@@ -185,7 +256,7 @@ namespace Notesnook.API.Services
var signedUrls = new string[parts]; var signedUrls = new string[parts];
for (var i = 0; i < parts; ++i) for (var i = 0; i < parts; ++i)
{ {
signedUrls[i] = GetPresignedURLForUploadPart(objectName, uploadId, i + 1); signedUrls[i] = await GetPresignedURLForUploadPartAsync(objectName, uploadId, i + 1);
} }
return new MultipartUploadMeta return new MultipartUploadMeta
@@ -200,83 +271,126 @@ namespace Notesnook.API.Services
var objectName = GetFullObjectName(userId, name); var objectName = GetFullObjectName(userId, name);
if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload."); if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload.");
var response = await GetS3Client(S3ClientMode.INTERNAL).AbortMultipartUploadAsync(GetBucketName(S3ClientMode.INTERNAL), objectName, uploadId); var response = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.AbortMultipartUploadAsync(INTERNAL_BUCKET_NAME, objectName, uploadId), operationName: "AbortMultipartUpload", isWriteOperation: true);
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to abort multipart upload."); if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to abort multipart upload.");
} }
private async Task<long> GetMultipartUploadSizeAsync(string userId, string key, string uploadId)
{
var objectName = GetFullObjectName(userId, key);
var parts = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.ListPartsAsync(INTERNAL_BUCKET_NAME, objectName, uploadId), operationName: "ListParts");
long totalSize = 0;
foreach (var part in parts.Parts)
{
totalSize += part.Size;
}
return totalSize;
}
public async Task CompleteMultipartUploadAsync(string userId, CompleteMultipartUploadRequest uploadRequest) public async Task CompleteMultipartUploadAsync(string userId, CompleteMultipartUploadRequest uploadRequest)
{ {
var objectName = GetFullObjectName(userId, uploadRequest.Key); var objectName = GetFullObjectName(userId, uploadRequest.Key);
if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload."); if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload.");
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId);
if (userSettings == null)
{
await this.AbortMultipartUploadAsync(userId, uploadRequest.Key, uploadRequest.UploadId);
throw new Exception("User settings not found.");
}
userSettings.StorageLimit ??= StorageHelper.RolloverStorageLimit(userSettings.StorageLimit);
if (!Constants.IS_SELF_HOSTED)
{
var subscription = await ServiceAccessor.UserSubscriptionService.GetUserSubscriptionAsync(Clients.Notesnook.Id, userId) ?? throw new Exception("User subscription not found.");
long fileSize = await GetMultipartUploadSizeAsync(userId, uploadRequest.Key, uploadRequest.UploadId);
if (StorageHelper.IsFileSizeExceeded(subscription, fileSize))
{
await this.AbortMultipartUploadAsync(userId, uploadRequest.Key, uploadRequest.UploadId);
throw new Exception("Max file size exceeded.");
}
userSettings.StorageLimit.Value += fileSize;
if (StorageHelper.IsStorageLimitReached(subscription, userSettings.StorageLimit.Value))
{
await this.AbortMultipartUploadAsync(userId, uploadRequest.Key, uploadRequest.UploadId);
throw new Exception("Storage limit reached.");
}
}
uploadRequest.Key = objectName; uploadRequest.Key = objectName;
uploadRequest.BucketName = GetBucketName(S3ClientMode.INTERNAL); uploadRequest.BucketName = INTERNAL_BUCKET_NAME;
var response = await GetS3Client(S3ClientMode.INTERNAL).CompleteMultipartUploadAsync(uploadRequest); var response = await S3InternalClient.ExecuteWithFailoverAsync((client) => client.CompleteMultipartUploadAsync(uploadRequest), operationName: "CompleteMultipartUpload", isWriteOperation: true);
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to complete multipart upload."); if (!IsSuccessStatusCode((int)response.HttpStatusCode)) throw new Exception("Failed to complete multipart upload.");
if (!Constants.IS_SELF_HOSTED)
{
await Repositories.UsersSettings.UpsertAsync(userSettings, (u) => u.UserId == userId);
await Repositories.UsersSettings.Collection.UpdateOneAsync(
Builders<UserSettings>.Filter.Eq(u => u.UserId, userId),
Builders<UserSettings>.Update.Set(u => u.StorageLimit, userSettings.StorageLimit)
);
}
} }
private string GetPresignedURL(string userId, string name, HttpVerb httpVerb, S3ClientMode mode = S3ClientMode.EXTERNAL) private async Task<string?> GetPresignedURLAsync(string userId, string name, HttpVerb httpVerb, S3ClientMode mode = S3ClientMode.EXTERNAL)
{ {
var objectName = GetFullObjectName(userId, name); var objectName = GetFullObjectName(userId, name);
if (userId == null || objectName == null) return null; if (userId == null || objectName == null) return null;
var client = GetS3Client(mode); var client = mode == S3ClientMode.INTERNAL ? S3InternalClient : S3Client;
var request = new GetPreSignedUrlRequest var bucketName = mode == S3ClientMode.INTERNAL ? INTERNAL_BUCKET_NAME : BUCKET_NAME;
return await client.ExecuteWithFailoverAsync(client =>
{ {
BucketName = GetBucketName(mode), var request = new GetPreSignedUrlRequest
Expires = System.DateTime.Now.AddHours(1), {
Verb = httpVerb, BucketName = bucketName,
Key = objectName, Expires = System.DateTime.Now.AddHours(1),
#if DEBUG Verb = httpVerb,
Protocol = Protocol.HTTP, Key = objectName,
#if (DEBUG || STAGING)
Protocol = Protocol.HTTP,
#else #else
Protocol = client.Config.ServiceURL.StartsWith("http://") ? Protocol.HTTP : Protocol.HTTPS, Protocol = client.Config.ServiceURL.StartsWith("http://") ? Protocol.HTTP : Protocol.HTTPS,
#endif #endif
}; };
return client.GetPreSignedURL(request); return client.GetPreSignedURLAsync(request);
}, operationName: "GetPreSignedURL");
} }
private string GetPresignedURLForUploadPart(string objectName, string uploadId, int partNumber) private Task<string> GetPresignedURLForUploadPartAsync(string objectName, string uploadId, int partNumber, S3ClientMode mode = S3ClientMode.EXTERNAL)
{ {
var client = GetS3Client(S3ClientMode.INTERNAL); var client = mode == S3ClientMode.INTERNAL ? S3InternalClient : S3Client;
return client.GetPreSignedURL(new GetPreSignedUrlRequest var bucketName = mode == S3ClientMode.INTERNAL ? INTERNAL_BUCKET_NAME : BUCKET_NAME;
return client.ExecuteWithFailoverAsync(c => c.GetPreSignedURLAsync(new GetPreSignedUrlRequest
{ {
BucketName = GetBucketName(S3ClientMode.INTERNAL), BucketName = bucketName,
Expires = System.DateTime.Now.AddHours(1), Expires = System.DateTime.Now.AddHours(1),
Verb = HttpVerb.PUT, Verb = HttpVerb.PUT,
Key = objectName, Key = objectName,
PartNumber = partNumber, PartNumber = partNumber,
UploadId = uploadId, UploadId = uploadId,
#if DEBUG #if (DEBUG || STAGING)
Protocol = Protocol.HTTP, Protocol = Protocol.HTTP,
#else #else
Protocol = client.Config.ServiceURL.StartsWith("http://") ? Protocol.HTTP : Protocol.HTTPS, Protocol = c.Config.ServiceURL.StartsWith("http://") ? Protocol.HTTP : Protocol.HTTPS,
#endif #endif
}); }), operationName: "GetPreSignedURL");
} }
private string GetFullObjectName(string userId, string name) private static string? GetFullObjectName(string userId, string name)
{ {
if (userId == null || !Regex.IsMatch(name, "[0-9a-zA-Z!" + Regex.Escape("-") + "_.*'()]")) return null; if (userId == null || !Regex.IsMatch(name, "[0-9a-zA-Z!" + Regex.Escape("-") + "_.*'()]")) return null;
return $"{userId}/{name}"; return $"{userId}/{name}";
} }
bool IsSuccessStatusCode(int statusCode) static bool IsSuccessStatusCode(int statusCode)
{ {
return ((int)statusCode >= 200) && ((int)statusCode <= 299); return ((int)statusCode >= 200) && ((int)statusCode <= 299);
} }
AmazonS3Client GetS3Client(S3ClientMode mode = S3ClientMode.EXTERNAL)
{
if (mode == S3ClientMode.INTERNAL && S3InternalClient != null) return S3InternalClient;
return S3Client;
}
string GetBucketName(S3ClientMode mode = S3ClientMode.EXTERNAL)
{
if (mode == S3ClientMode.INTERNAL && S3InternalClient != null) return INTERNAL_BUCKET_NAME;
return BUCKET_NAME;
}
} }
} }
+177 -176
View File
@@ -24,200 +24,201 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Notesnook.API.Interfaces;
using Notesnook.API.Models;
namespace Notesnook.API.Services namespace Notesnook.API.Services
{ {
public struct SyncDevice(ref string userId, ref string deviceId) public readonly record struct ItemKey(string ItemId, string Type)
{ {
public readonly string DeviceId = deviceId; public override string ToString() => $"{ItemId}:{Type}";
public readonly string UserId = userId;
private string userSyncDirectoryPath = null;
public string UserSyncDirectoryPath
{
get
{
userSyncDirectoryPath ??= Path.Join("sync", UserId);
return userSyncDirectoryPath;
}
}
private string userDeviceDirectoryPath = null;
public string UserDeviceDirectoryPath
{
get
{
userDeviceDirectoryPath ??= Path.Join(UserSyncDirectoryPath, DeviceId);
return userDeviceDirectoryPath;
}
}
private string pendingIdsFilePath = null;
public string PendingIdsFilePath
{
get
{
pendingIdsFilePath ??= Path.Join(UserDeviceDirectoryPath, "pending");
return pendingIdsFilePath;
}
}
private string unsyncedIdsFilePath = null;
public string UnsyncedIdsFilePath
{
get
{
unsyncedIdsFilePath ??= Path.Join(UserDeviceDirectoryPath, "unsynced");
return unsyncedIdsFilePath;
}
}
private string resetSyncFilePath = null;
public string ResetSyncFilePath
{
get
{
resetSyncFilePath ??= Path.Join(UserDeviceDirectoryPath, "reset-sync");
return resetSyncFilePath;
}
}
} }
public class SyncDeviceService(SyncDevice device) public class SyncDeviceService(ISyncItemsRepositoryAccessor repositories, ILogger<SyncDeviceService> logger)
{ {
public async Task<string[]> GetUnsyncedIdsAsync() private static FilterDefinition<SyncDevice> DeviceFilter(string userId, string deviceId) =>
{ Builders<SyncDevice>.Filter.Eq(x => x.UserId, userId) &
try Builders<SyncDevice>.Filter.Eq(x => x.DeviceId, deviceId);
{ private static FilterDefinition<DeviceIdsChunk> DeviceIdsChunkFilter(string userId, string deviceId, string key) =>
return await File.ReadAllLinesAsync(device.UnsyncedIdsFilePath); Builders<DeviceIdsChunk>.Filter.Eq(x => x.UserId, userId) &
} Builders<DeviceIdsChunk>.Filter.Eq(x => x.DeviceId, deviceId) &
catch { return []; } Builders<DeviceIdsChunk>.Filter.Eq(x => x.Key, key);
}
public async Task<string[]> GetUnsyncedIdsAsync(string deviceId) private static FilterDefinition<DeviceIdsChunk> DeviceIdsChunkFilter(string userId, string deviceId) =>
{ Builders<DeviceIdsChunk>.Filter.Eq(x => x.UserId, userId) &
try Builders<DeviceIdsChunk>.Filter.Eq(x => x.DeviceId, deviceId);
{
return await File.ReadAllLinesAsync(Path.Join(device.UserSyncDirectoryPath, deviceId, "unsynced"));
}
catch { return []; }
}
public async Task<string[]> FetchUnsyncedIdsAsync() private static FilterDefinition<DeviceIdsChunk> DeviceIdsChunkFilter(string userId) =>
Builders<DeviceIdsChunk>.Filter.Eq(x => x.UserId, userId);
private static FilterDefinition<SyncDevice> UserFilter(string userId) => Builders<SyncDevice>.Filter.Eq(x => x.UserId, userId);
public async Task<HashSet<ItemKey>> GetIdsAsync(string userId, string deviceId, string key)
{ {
if (IsSyncReset()) return Array.Empty<string>(); var cursor = await repositories.DeviceIdsChunks.Collection.FindAsync(DeviceIdsChunkFilter(userId, deviceId, key));
if (UnsyncedIdsFileLocks.TryGetValue(device.DeviceId, out SemaphoreSlim fileLock) && fileLock.CurrentCount == 0) var result = new HashSet<ItemKey>();
await fileLock.WaitAsync(); while (await cursor.MoveNextAsync())
try
{ {
var unsyncedIds = await GetUnsyncedIdsAsync(); foreach (var chunk in cursor.Current)
if (IsSyncPending())
{ {
unsyncedIds = unsyncedIds.Union(await File.ReadAllLinesAsync(device.PendingIdsFilePath)).ToArray(); foreach (var id in chunk.Ids)
{
var parts = id.Split(':', 2);
result.Add(new ItemKey(parts[0], parts[1]));
}
} }
if (unsyncedIds.Length == 0) return [];
File.Delete(device.UnsyncedIdsFilePath);
await File.WriteAllLinesAsync(device.PendingIdsFilePath, unsyncedIds);
return unsyncedIds;
} }
catch return result;
}
const int MaxIdsPerChunk = 25_000;
public async Task AppendIdsAsync(string userId, string deviceId, string key, IEnumerable<ItemKey> ids)
{
var filter = DeviceIdsChunkFilter(userId, deviceId, key) & Builders<DeviceIdsChunk>.Filter.Where(x => x.Ids.Length < MaxIdsPerChunk);
var chunk = await repositories.DeviceIdsChunks.Collection.Find(filter).FirstOrDefaultAsync();
if (chunk != null)
{ {
return Array.Empty<string>(); var update = Builders<DeviceIdsChunk>.Update.AddToSetEach(x => x.Ids, ids.Select(i => i.ToString()));
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).UpdateOneAsync(
Builders<DeviceIdsChunk>.Filter.Eq(x => x.Id, chunk.Id),
update
);
} }
finally else
{ {
if (fileLock != null && fileLock.CurrentCount == 0) fileLock.Release(); var newChunk = new DeviceIdsChunk
}
}
public async Task WritePendingIdsAsync(IEnumerable<string> ids)
{
await File.WriteAllLinesAsync(device.PendingIdsFilePath, ids);
}
public bool IsSyncReset()
{
return File.Exists(device.ResetSyncFilePath);
}
public bool IsSyncReset(string deviceId)
{
return File.Exists(Path.Join(device.UserSyncDirectoryPath, deviceId, "reset-sync"));
}
public bool IsSyncPending()
{
return File.Exists(device.PendingIdsFilePath);
}
public bool IsUnsynced()
{
return File.Exists(device.UnsyncedIdsFilePath);
}
public void Reset()
{
File.Delete(device.ResetSyncFilePath);
File.Delete(device.PendingIdsFilePath);
}
public bool IsDeviceRegistered()
{
return Directory.Exists(device.UserDeviceDirectoryPath);
}
public bool IsDeviceRegistered(string deviceId)
{
return Directory.Exists(Path.Join(device.UserSyncDirectoryPath, deviceId));
}
public string[] ListDevices()
{
return Directory.GetDirectories(device.UserSyncDirectoryPath).Select((path) => path[(path.LastIndexOf(Path.DirectorySeparatorChar) + 1)..]).ToArray();
}
public void ResetDevices()
{
if (File.Exists(device.UserSyncDirectoryPath)) File.Delete(device.UserSyncDirectoryPath);
Directory.CreateDirectory(device.UserSyncDirectoryPath);
}
private readonly ConcurrentDictionary<string, SemaphoreSlim> UnsyncedIdsFileLocks = [];
public async Task AddIdsToOtherDevicesAsync(List<string> ids)
{
await Parallel.ForEachAsync(ListDevices(), async (id, ct) =>
{
if (id == device.DeviceId || IsSyncReset(id)) return;
if (!UnsyncedIdsFileLocks.TryGetValue(id, out SemaphoreSlim fileLock))
{ {
fileLock = UnsyncedIdsFileLocks.AddOrUpdate(id, (id) => new SemaphoreSlim(1, 1), (id, old) => new SemaphoreSlim(1, 1)); UserId = userId,
} DeviceId = deviceId,
Key = key,
await fileLock.WaitAsync(ct); Ids = [.. ids.Select(i => i.ToString())]
try };
{ await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).InsertOneAsync(newChunk);
if (!IsDeviceRegistered(id)) Directory.CreateDirectory(Path.Join(device.UserSyncDirectoryPath, id));
var oldIds = await GetUnsyncedIdsAsync(id);
await File.WriteAllLinesAsync(Path.Join(device.UserSyncDirectoryPath, id, "unsynced"), ids.Union(oldIds), ct);
}
finally
{
fileLock.Release();
}
});
}
public void RegisterDevice()
{
Directory.CreateDirectory(device.UserDeviceDirectoryPath);
File.Create(device.ResetSyncFilePath).Close();
}
public void UnregisterDevice()
{
try
{
Directory.Delete(device.UserDeviceDirectoryPath, true);
} }
catch { }
var emptyChunksFilter = DeviceIdsChunkFilter(userId, deviceId, key) & Builders<DeviceIdsChunk>.Filter.Size(x => x.Ids, 0);
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).DeleteManyAsync(emptyChunksFilter);
}
public async Task WriteIdsAsync(string userId, string deviceId, string key, IEnumerable<ItemKey> ids)
{
var writes = new List<WriteModel<DeviceIdsChunk>>
{
new DeleteManyModel<DeviceIdsChunk>(DeviceIdsChunkFilter(userId, deviceId, key))
};
var chunks = ids.Chunk(MaxIdsPerChunk);
foreach (var chunk in chunks)
{
var newChunk = new DeviceIdsChunk
{
UserId = userId,
DeviceId = deviceId,
Key = key,
Ids = [.. chunk.Select(i => i.ToString())]
};
writes.Add(new InsertOneModel<DeviceIdsChunk>(newChunk));
}
await repositories.DeviceIdsChunks.Collection.WithWriteConcern(WriteConcern.W1).BulkWriteAsync(writes);
}
public async Task<HashSet<ItemKey>> FetchUnsyncedIdsAsync(string userId, string deviceId)
{
var device = await GetDeviceAsync(userId, deviceId);
if (device == null || device.IsSyncReset) return [];
var unsyncedIds = await GetIdsAsync(userId, deviceId, "unsynced");
var pendingIds = await GetIdsAsync(userId, deviceId, "pending");
unsyncedIds = [.. unsyncedIds, .. pendingIds];
if (unsyncedIds.Count == 0) return [];
await repositories.DeviceIdsChunks.Collection.DeleteManyAsync(DeviceIdsChunkFilter(userId, deviceId, "unsynced"));
await WriteIdsAsync(userId, deviceId, "pending", unsyncedIds);
return unsyncedIds;
}
public async Task WritePendingIdsAsync(string userId, string deviceId, HashSet<ItemKey> ids)
{
await WriteIdsAsync(userId, deviceId, "pending", ids);
}
public async Task ResetAsync(string userId, string deviceId)
{
await repositories.SyncDevices.Collection.UpdateOneAsync(DeviceFilter(userId, deviceId), Builders<SyncDevice>.Update
.Set(x => x.IsSyncReset, false));
await repositories.DeviceIdsChunks.Collection.DeleteManyAsync(DeviceIdsChunkFilter(userId, deviceId, "pending"));
}
public async Task<SyncDevice?> GetDeviceAsync(string userId, string deviceId)
{
return await repositories.SyncDevices.Collection.Find(DeviceFilter(userId, deviceId)).FirstOrDefaultAsync();
}
public async IAsyncEnumerable<SyncDevice> ListDevicesAsync(string userId)
{
using var cursor = await repositories.SyncDevices.Collection.FindAsync(UserFilter(userId));
while (await cursor.MoveNextAsync())
{
foreach (var device in cursor.Current)
{
yield return device;
}
}
}
public async Task ResetDevicesAsync(string userId)
{
await repositories.SyncDevices.Collection.DeleteManyAsync(UserFilter(userId));
await repositories.DeviceIdsChunks.Collection.DeleteManyAsync(DeviceIdsChunkFilter(userId));
}
public async Task UpdateLastAccessTimeAsync(string userId, string deviceId)
{
await repositories.SyncDevices.Collection.UpdateOneAsync(DeviceFilter(userId, deviceId), Builders<SyncDevice>.Update
.Set(x => x.LastAccessTime, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()));
}
public async Task AddIdsToOtherDevicesAsync(string userId, string deviceId, IEnumerable<ItemKey> ids)
{
await UpdateLastAccessTimeAsync(userId, deviceId);
await foreach (var device in ListDevicesAsync(userId))
{
if (device.DeviceId == deviceId || device.IsSyncReset) continue;
await AppendIdsAsync(userId, device.DeviceId, "unsynced", ids);
}
}
public async Task AddIdsToAllDevicesAsync(string userId, IEnumerable<ItemKey> ids)
{
await foreach (var device in ListDevicesAsync(userId))
{
if (device.IsSyncReset) continue;
await AppendIdsAsync(userId, device.DeviceId, "unsynced", ids);
}
}
public async Task<SyncDevice> RegisterDeviceAsync(string userId, string deviceId)
{
var newDevice = new SyncDevice
{
UserId = userId,
DeviceId = deviceId,
LastAccessTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
IsSyncReset = true
};
await repositories.SyncDevices.Collection.InsertOneAsync(newDevice);
return newDevice;
}
public async Task UnregisterDeviceAsync(string userId, string deviceId)
{
await repositories.SyncDevices.Collection.DeleteOneAsync(DeviceFilter(userId, deviceId));
await repositories.DeviceIdsChunks.Collection.DeleteManyAsync(DeviceIdsChunkFilter(userId, deviceId));
} }
} }
} }
+105 -46
View File
@@ -18,94 +18,89 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.IO; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Notesnook.API.Helpers;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Models; using Notesnook.API.Models;
using Notesnook.API.Models.Responses; using Notesnook.API.Models.Responses;
using Streetwriters.Common; using Streetwriters.Common;
using Streetwriters.Common.Accessors;
using Streetwriters.Common.Enums; using Streetwriters.Common.Enums;
using Streetwriters.Common.Extensions; using Streetwriters.Common.Extensions;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages; using Streetwriters.Common.Messages;
using Streetwriters.Common.Models; using Streetwriters.Common.Models;
using Streetwriters.Data.Interfaces; using Streetwriters.Data.Interfaces;
namespace Notesnook.API.Services namespace Notesnook.API.Services
{ {
public class UserService : IUserService public class UserService(IHttpContextAccessor accessor,
ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor,
IUnitOfWork unitOfWork, IS3Service s3Service, SyncDeviceService syncDeviceService, WampServiceAccessor serviceAccessor, ILogger<UserService> logger) : IUserService
{ {
private static readonly System.Security.Cryptography.RandomNumberGenerator Rng = System.Security.Cryptography.RandomNumberGenerator.Create(); private static readonly System.Security.Cryptography.RandomNumberGenerator Rng = System.Security.Cryptography.RandomNumberGenerator.Create();
private readonly HttpClient httpClient; private readonly HttpClient httpClient = new();
private IHttpContextAccessor HttpContextAccessor { get; } private IHttpContextAccessor HttpContextAccessor { get; } = accessor;
private ISyncItemsRepositoryAccessor Repositories { get; } private ISyncItemsRepositoryAccessor Repositories { get; } = syncItemsRepositoryAccessor;
private IS3Service S3Service { get; set; } private IS3Service S3Service { get; set; } = s3Service;
private readonly IUnitOfWork unit; private readonly IUnitOfWork unit = unitOfWork;
public UserService(IHttpContextAccessor accessor, public async Task<SignupResponse> CreateUserAsync(SignupForm form)
ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor,
IUnitOfWork unitOfWork, IS3Service s3Service)
{ {
httpClient = new HttpClient(); SignupResponse response = await serviceAccessor.UserAccountService.CreateUserAsync(form.ClientId, form.Email, form.Password, HttpContextAccessor.HttpContext?.Request.Headers["User-Agent"].ToString());
Repositories = syncItemsRepositoryAccessor; if ((response.Errors != null && response.Errors.Length > 0) || response.UserId == null)
HttpContextAccessor = accessor;
unit = unitOfWork;
S3Service = s3Service;
}
public async Task CreateUserAsync()
{
SignupResponse response = await httpClient.ForwardAsync<SignupResponse>(this.HttpContextAccessor, $"{Servers.IdentityServer}/signup", HttpMethod.Post);
if (!response.Success || (response.Errors != null && response.Errors.Length > 0))
{ {
await Slogger<UserService>.Error(nameof(CreateUserAsync), "Couldn't sign up.", JsonSerializer.Serialize(response)); logger.LogError("Failed to sign up user: {Response}", JsonSerializer.Serialize(response));
if (response.Errors != null && response.Errors.Length > 0) if (response.Errors != null && response.Errors.Length > 0)
throw new Exception(string.Join(" ", response.Errors)); throw new Exception(string.Join(" ", response.Errors));
else throw new Exception("Could not create a new account. Error code: " + response.StatusCode); else throw new Exception("Could not create a new account.");
} }
await Repositories.UsersSettings.InsertAsync(new UserSettings await Repositories.UsersSettings.InsertAsync(new UserSettings
{ {
UserId = response.UserId, UserId = response.UserId,
StorageLimit = new Limit { UpdatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), Value = 0 },
LastSynced = 0, LastSynced = 0,
Salt = GetSalt() Salt = GetSalt()
}); });
if (!Constants.IS_SELF_HOSTED) if (!Constants.IS_SELF_HOSTED)
{ {
await WampServers.SubscriptionServer.PublishMessageAsync(SubscriptionServerTopics.CreateSubscriptionTopic, new CreateSubscriptionMessage await WampServers.SubscriptionServer.PublishMessageAsync(SubscriptionServerTopics.CreateSubscriptionV2Topic, new CreateSubscriptionMessageV2
{ {
AppId = ApplicationType.NOTESNOOK, AppId = ApplicationType.NOTESNOOK,
Provider = SubscriptionProvider.STREETWRITERS, Provider = SubscriptionProvider.STREETWRITERS,
Type = SubscriptionType.BASIC, Status = SubscriptionStatus.ACTIVE,
Plan = SubscriptionPlan.FREE,
UserId = response.UserId, UserId = response.UserId,
StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
}); });
} }
await Slogger<UserService>.Info(nameof(CreateUserAsync), "New user created.", JsonSerializer.Serialize(response)); return response;
} }
public async Task<UserResponse> GetUserAsync(string userId) public async Task<UserResponse> GetUserAsync(string userId)
{ {
var userService = await WampServers.IdentityServer.GetServiceAsync<IUserAccountService>(IdentityServerTopics.UserAccountServiceTopic); var user = await serviceAccessor.UserAccountService.GetUserAsync(Clients.Notesnook.Id, userId) ?? throw new Exception("User not found.");
var user = await userService.GetUserAsync(Clients.Notesnook.Id, userId) ?? throw new Exception("User not found."); Subscription? subscription = null;
ISubscription subscription = null;
if (Constants.IS_SELF_HOSTED) if (Constants.IS_SELF_HOSTED)
{ {
subscription = new Subscription subscription = new Subscription
{ {
AppId = ApplicationType.NOTESNOOK, AppId = ApplicationType.NOTESNOOK,
Provider = SubscriptionProvider.STREETWRITERS, Provider = SubscriptionProvider.STREETWRITERS,
Type = SubscriptionType.PREMIUM, Plan = SubscriptionPlan.BELIEVER,
Status = SubscriptionStatus.ACTIVE,
UserId = user.UserId, UserId = user.UserId,
StartDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), StartDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
// this date doesn't matter as the subscription is static. // this date doesn't matter as the subscription is static.
@@ -114,11 +109,22 @@ namespace Notesnook.API.Services
} }
else else
{ {
var subscriptionService = await WampServers.SubscriptionServer.GetServiceAsync<IUserSubscriptionService>(SubscriptionServerTopics.UserSubscriptionServiceTopic); subscription = await serviceAccessor.UserSubscriptionService.GetUserSubscriptionAsync(Clients.Notesnook.Id, userId) ?? throw new Exception("User subscription not found.");
subscription = await subscriptionService.GetUserSubscriptionAsync(Clients.Notesnook.Id, userId);
} }
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == user.UserId) ?? throw new Exception("User settings not found."); var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == user.UserId) ?? throw new Exception("User settings not found.");
// reset user's attachment limit every month
var limit = StorageHelper.RolloverStorageLimit(userSettings.StorageLimit);
if (userSettings.StorageLimit == null || limit.UpdatedAt != userSettings.StorageLimit?.UpdatedAt)
{
userSettings.StorageLimit = limit;
await Repositories.UsersSettings.Collection.UpdateOneAsync(
Builders<UserSettings>.Filter.Eq(u => u.UserId, user.UserId),
Builders<UserSettings>.Update.Set(u => u.StorageLimit, userSettings.StorageLimit)
);
}
return new UserResponse return new UserResponse
{ {
UserId = user.UserId, UserId = user.UserId,
@@ -128,24 +134,67 @@ namespace Notesnook.API.Services
MFA = user.MFA, MFA = user.MFA,
PhoneNumber = user.PhoneNumber, PhoneNumber = user.PhoneNumber,
AttachmentsKey = userSettings.AttachmentsKey, AttachmentsKey = userSettings.AttachmentsKey,
MonographPasswordsKey = userSettings.MonographPasswordsKey,
DataEncryptionKey = userSettings.DataEncryptionKey,
LegacyDataEncryptionKey = userSettings.LegacyDataEncryptionKey,
InboxKeys = userSettings.InboxKeys,
Salt = userSettings.Salt, Salt = userSettings.Salt,
Subscription = subscription, Subscription = subscription,
StorageUsed = userSettings.StorageLimit.Value,
TotalStorage = StorageHelper.GetStorageLimitForPlan(subscription),
Success = true, Success = true,
StatusCode = 200 StatusCode = 200
}; };
} }
public async Task SetUserAttachmentsKeyAsync(string userId, IEncrypted key) public async Task SetUserKeysAsync(string userId, UserKeys keys)
{ {
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId) ?? throw new Exception("User not found."); var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId) ?? throw new Exception("User not found.");
userSettings.AttachmentsKey = (EncryptedData)key;
if (keys.AttachmentsKey != null)
{
userSettings.AttachmentsKey = keys.AttachmentsKey;
}
if (keys.MonographPasswordsKey != null)
{
userSettings.MonographPasswordsKey = keys.MonographPasswordsKey;
}
if (keys.DataEncryptionKey != null)
userSettings.DataEncryptionKey = keys.DataEncryptionKey;
if (keys.LegacyDataEncryptionKey != null)
userSettings.LegacyDataEncryptionKey = keys.LegacyDataEncryptionKey;
if (keys.InboxKeys != null)
{
if (keys.InboxKeys.Public == null || keys.InboxKeys.Private == null)
{
userSettings.InboxKeys = null;
await Repositories.InboxApiKey.DeleteManyAsync(t => t.UserId == userId);
}
else
{
userSettings.InboxKeys = keys.InboxKeys;
}
await Repositories.InboxItems.DeleteManyAsync(t => t.UserId == userId);
await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
{
OriginTokenId = null,
UserId = userId,
Message = new Message
{
Type = "inboxUpdated",
Data = JsonSerializer.Serialize(new { reason = "Inbox PGP keys added, updated, or removed." })
}
});
}
await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings); await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings);
} }
public async Task DeleteUserAsync(string userId) public async Task DeleteUserAsync(string userId)
{ {
new SyncDeviceService(new SyncDevice(ref userId, ref userId)).ResetDevices(); logger.LogInformation("Deleting user {UserId}", userId);
var cc = new CancellationTokenSource(); var cc = new CancellationTokenSource();
Repositories.Notes.DeleteByUserId(userId); Repositories.Notes.DeleteByUserId(userId);
@@ -160,13 +209,17 @@ namespace Notesnook.API.Services
Repositories.Colors.DeleteByUserId(userId); Repositories.Colors.DeleteByUserId(userId);
Repositories.Tags.DeleteByUserId(userId); Repositories.Tags.DeleteByUserId(userId);
Repositories.Vaults.DeleteByUserId(userId); Repositories.Vaults.DeleteByUserId(userId);
Repositories.InboxItemsHistory.DeleteByUserId(userId);
Repositories.UsersSettings.Delete((u) => u.UserId == userId); Repositories.UsersSettings.Delete((u) => u.UserId == userId);
Repositories.Monographs.DeleteMany((m) => m.UserId == userId); Repositories.Monographs.DeleteMany((m) => m.UserId == userId);
Repositories.InboxApiKey.DeleteMany((t) => t.UserId == userId);
var result = await unit.Commit(); var result = await unit.Commit();
await Slogger<UserService>.Info(nameof(DeleteUserAsync), "User data deleted", userId, result.ToString()); logger.LogInformation("User data deleted for user {UserId}: {Result}", userId, result);
if (!result) throw new Exception("Could not delete user data."); if (!result) throw new Exception("Could not delete user data.");
await syncDeviceService.ResetDevicesAsync(userId);
if (!Constants.IS_SELF_HOSTED) if (!Constants.IS_SELF_HOSTED)
{ {
await WampServers.SubscriptionServer.PublishMessageAsync(SubscriptionServerTopics.DeleteSubscriptionTopic, new DeleteSubscriptionMessage await WampServers.SubscriptionServer.PublishMessageAsync(SubscriptionServerTopics.DeleteSubscriptionTopic, new DeleteSubscriptionMessage
@@ -179,18 +232,17 @@ namespace Notesnook.API.Services
await S3Service.DeleteDirectoryAsync(userId); await S3Service.DeleteDirectoryAsync(userId);
} }
public async Task DeleteUserAsync(string userId, string jti, string password) public async Task DeleteUserAsync(string userId, string? jti, string password)
{ {
await Slogger<UserService>.Info(nameof(DeleteUserAsync), "Deleting user account", userId); logger.LogInformation("Deleting user account: {UserId}", userId);
var userService = await WampServers.IdentityServer.GetServiceAsync<IUserAccountService>(IdentityServerTopics.UserAccountServiceTopic); await serviceAccessor.UserAccountService.DeleteUserAsync(Clients.Notesnook.Id, userId, password);
await userService.DeleteUserAsync(Clients.Notesnook.Id, userId, password);
await DeleteUserAsync(userId); await DeleteUserAsync(userId);
await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
{ {
SendToAll = false, SendToAll = jti == null,
OriginTokenId = jti, OriginTokenId = jti,
UserId = userId, UserId = userId,
Message = new Message Message = new Message
@@ -204,7 +256,6 @@ namespace Notesnook.API.Services
public async Task<bool> ResetUserAsync(string userId, bool removeAttachments) public async Task<bool> ResetUserAsync(string userId, bool removeAttachments)
{ {
new SyncDeviceService(new SyncDevice(ref userId, ref userId)).ResetDevices();
var cc = new CancellationTokenSource(); var cc = new CancellationTokenSource();
@@ -220,13 +271,21 @@ namespace Notesnook.API.Services
Repositories.Colors.DeleteByUserId(userId); Repositories.Colors.DeleteByUserId(userId);
Repositories.Tags.DeleteByUserId(userId); Repositories.Tags.DeleteByUserId(userId);
Repositories.Vaults.DeleteByUserId(userId); Repositories.Vaults.DeleteByUserId(userId);
Repositories.InboxItemsHistory.DeleteByUserId(userId);
Repositories.Monographs.DeleteMany((m) => m.UserId == userId); Repositories.Monographs.DeleteMany((m) => m.UserId == userId);
Repositories.InboxApiKey.DeleteMany((t) => t.UserId == userId);
if (!await unit.Commit()) return false; if (!await unit.Commit()) return false;
await syncDeviceService.ResetDevicesAsync(userId);
var userSettings = await Repositories.UsersSettings.FindOneAsync((s) => s.UserId == userId); var userSettings = await Repositories.UsersSettings.FindOneAsync((s) => s.UserId == userId);
userSettings.AttachmentsKey = null; userSettings.AttachmentsKey = null;
userSettings.MonographPasswordsKey = null;
userSettings.DataEncryptionKey = null;
userSettings.LegacyDataEncryptionKey = null;
userSettings.VaultKey = null; userSettings.VaultKey = null;
userSettings.InboxKeys = null;
userSettings.LastSynced = 0; userSettings.LastSynced = 0;
await Repositories.UsersSettings.UpsertAsync(userSettings, (s) => s.UserId == userId); await Repositories.UsersSettings.UpsertAsync(userSettings, (s) => s.UserId == userId);
+82 -28
View File
@@ -24,6 +24,8 @@ using System.Security.Claims;
using System.Text; using System.Text;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; using System.Threading.Tasks;
using Amazon.Runtime;
using StackExchange.Redis;
using IdentityModel.AspNetCore.OAuth2Introspection; using IdentityModel.AspNetCore.OAuth2Introspection;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -39,6 +41,7 @@ using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@@ -48,15 +51,19 @@ using Notesnook.API.Authorization;
using Notesnook.API.Extensions; using Notesnook.API.Extensions;
using Notesnook.API.Hubs; using Notesnook.API.Hubs;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Notesnook.API.Jobs;
using Notesnook.API.Models; using Notesnook.API.Models;
using Notesnook.API.Repositories; using Notesnook.API.Repositories;
using Notesnook.API.Services; using Notesnook.API.Services;
using OpenTelemetry.Metrics; using OpenTelemetry.Metrics;
using OpenTelemetry.Resources; using OpenTelemetry.Resources;
using Quartz;
using Streetwriters.Common; using Streetwriters.Common;
using Streetwriters.Common.Extensions; using Streetwriters.Common.Extensions;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages; using Streetwriters.Common.Messages;
using Streetwriters.Common.Models; using Streetwriters.Common.Models;
using Streetwriters.Common.Services;
using Streetwriters.Data; using Streetwriters.Data;
using Streetwriters.Data.DbContexts; using Streetwriters.Data.DbContexts;
using Streetwriters.Data.Interfaces; using Streetwriters.Data.Interfaces;
@@ -108,16 +115,15 @@ namespace Notesnook.API
policy.RequireAuthenticatedUser(); policy.RequireAuthenticatedUser();
policy.Requirements.Add(new SyncRequirement()); policy.Requirements.Add(new SyncRequirement());
}); });
options.AddPolicy("Pro", policy =>
options.AddPolicy(InboxApiKeyAuthenticationDefaults.AuthenticationScheme, policy =>
{ {
policy.AuthenticationSchemes.Add("introspection"); policy.AuthenticationSchemes.Add(InboxApiKeyAuthenticationDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser(); policy.RequireAuthenticatedUser();
policy.Requirements.Add(new SyncRequirement());
policy.Requirements.Add(new ProUserRequirement());
}); });
options.DefaultPolicy = options.GetPolicy("Notesnook"); options.DefaultPolicy = options.GetPolicy("Notesnook") ?? throw new Exception("Notesnook policy not found");
}).AddSingleton<IAuthorizationMiddlewareResultHandler, AuthorizationResultTransformer>(); ; }).AddSingleton<IAuthorizationMiddlewareResultHandler, AuthorizationResultTransformer>();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddOAuth2Introspection("introspection", options => .AddOAuth2Introspection("introspection", options =>
@@ -135,22 +141,26 @@ namespace Notesnook.API
options.Events.OnTokenValidated = (context) => options.Events.OnTokenValidated = (context) =>
{ {
if (long.TryParse(context.Principal.FindFirst("exp")?.Value, out long expiryTime)) if (long.TryParse(context.Principal?.FindFirst("exp")?.Value, out long expiryTime))
{ {
context.Properties.ExpiresUtc = DateTimeOffset.FromUnixTimeSeconds(expiryTime); context.Properties.ExpiresUtc = DateTimeOffset.FromUnixTimeSeconds(expiryTime);
} }
context.Properties.AllowRefresh = true; context.Properties.AllowRefresh = true;
context.Properties.IsPersistent = true; context.Properties.IsPersistent = true;
context.HttpContext.User = context.Principal; context.HttpContext.User = context.Principal ?? throw new Exception("No principal found in token.");
return Task.CompletedTask; return Task.CompletedTask;
}; };
options.CacheKeyGenerator = (options, token) => (token + ":" + "reference_token").Sha256(); options.CacheKeyGenerator = (options, token) => (token + ":" + "reference_token").Sha256();
options.SaveToken = true; options.SaveToken = true;
options.EnableCaching = true; options.EnableCaching = true;
options.CacheDuration = TimeSpan.FromMinutes(30); options.CacheDuration = TimeSpan.FromMinutes(30);
}); })
.AddScheme<InboxApiKeyAuthenticationSchemeOptions, InboxApiKeyAuthenticationHandler>(
InboxApiKeyAuthenticationDefaults.AuthenticationScheme,
options => { }
);
BsonSerializer.RegisterSerializer(new SyncItemBsonSerializer()); // Serializer.RegisterSerializer(new SyncItemBsonSerializer());
if (!BsonClassMap.IsClassMapRegistered(typeof(UserSettings))) if (!BsonClassMap.IsClassMapRegistered(typeof(UserSettings)))
BsonClassMap.RegisterClassMap<UserSettings>(); BsonClassMap.RegisterClassMap<UserSettings>();
@@ -160,12 +170,19 @@ namespace Notesnook.API
if (!BsonClassMap.IsClassMapRegistered(typeof(CallToAction))) if (!BsonClassMap.IsClassMapRegistered(typeof(CallToAction)))
BsonClassMap.RegisterClassMap<CallToAction>(); BsonClassMap.RegisterClassMap<CallToAction>();
if (!BsonClassMap.IsClassMapRegistered(typeof(SyncDevice)))
BsonClassMap.RegisterClassMap<SyncDevice>();
services.AddScoped<IDbContext, MongoDbContext>(); services.AddScoped<IDbContext, MongoDbContext>();
services.AddScoped<IUnitOfWork, UnitOfWork>(); services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddRepository<UserSettings>("user_settings", "notesnook") services.AddRepository<UserSettings>("user_settings", "notesnook")
.AddRepository<Monograph>("monographs", "notesnook") .AddRepository<Monograph>("monographs", "notesnook")
.AddRepository<Announcement>("announcements", "notesnook"); .AddRepository<Announcement>("announcements", "notesnook")
.AddRepository<DeviceIdsChunk>(Collections.DeviceIdsChunksKey, "notesnook")
.AddRepository<SyncDevice>(Collections.SyncDevicesKey, "notesnook")
.AddRepository<InboxApiKey>(Collections.InboxApiKeysKey, "notesnook")
.AddRepository<InboxSyncItem>(Collections.InboxItemsKey, "notesnook");
services.AddMongoCollection(Collections.SettingsKey) services.AddMongoCollection(Collections.SettingsKey)
.AddMongoCollection(Collections.AttachmentsKey) .AddMongoCollection(Collections.AttachmentsKey)
@@ -178,22 +195,47 @@ namespace Notesnook.API
.AddMongoCollection(Collections.ShortcutsKey) .AddMongoCollection(Collections.ShortcutsKey)
.AddMongoCollection(Collections.TagsKey) .AddMongoCollection(Collections.TagsKey)
.AddMongoCollection(Collections.ColorsKey) .AddMongoCollection(Collections.ColorsKey)
.AddMongoCollection(Collections.VaultsKey); .AddMongoCollection(Collections.VaultsKey)
.AddMongoCollection(Collections.InboxItemsKey)
.AddMongoCollection(Collections.InboxApiKeysKey)
.AddMongoCollection(Collections.InboxItemsHistoryKey);
services.AddScoped<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>(); services.AddScoped<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>();
services.AddScoped<SyncDeviceService>();
services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>();
services.AddScoped<IS3Service, S3Service>(); services.AddScoped<IS3Service, S3Service>();
services.AddScoped<IURLAnalyzer, URLAnalyzer>();
services.AddWampServiceAccessor(Servers.NotesnookAPI);
services.AddControllers(); services.AddControllers();
services.AddHealthChecks(); // .AddMongoDb(dbSettings.ConnectionString, dbSettings.DatabaseName, "database-check"); services.AddHealthChecks();
services.AddSignalR((hub) =>
var signalR = services.AddSignalR((hub) =>
{ {
hub.MaximumReceiveMessageSize = 100 * 1024 * 1024; hub.MaximumReceiveMessageSize = 100 * 1024 * 1024;
hub.KeepAliveInterval = TimeSpan.FromSeconds(15);
hub.ClientTimeoutInterval = TimeSpan.FromMinutes(10); hub.ClientTimeoutInterval = TimeSpan.FromMinutes(10);
hub.EnableDetailedErrors = true; hub.EnableDetailedErrors = true;
}).AddMessagePackProtocol().AddJsonProtocol(); }).AddMessagePackProtocol().AddJsonProtocol();
if (!string.IsNullOrEmpty(Constants.SIGNALR_REDIS_CONNECTION_STRING))
{
services.AddHealthChecks()
.AddRedis(Constants.SIGNALR_REDIS_CONNECTION_STRING, tags: ["ready"]);
signalR.AddStackExchangeRedis(options =>
{
options.Configuration = ConfigurationOptions.Parse(Constants.SIGNALR_REDIS_CONNECTION_STRING);
options.Configuration.AbortOnConnectFail = false;
options.Configuration.ConnectRetry = 5;
options.Configuration.ReconnectRetryPolicy = new ExponentialRetry(5000, 30000);
options.Configuration.KeepAlive = 60;
options.Configuration.ConnectTimeout = 5000;
options.Configuration.SyncTimeout = 5000;
});
}
services.AddResponseCompression(options => services.AddResponseCompression(options =>
{ {
options.EnableForHttps = true; options.EnableForHttps = true;
@@ -216,22 +258,40 @@ namespace Notesnook.API
.WithMetrics((builder) => builder .WithMetrics((builder) => builder
.AddMeter("Notesnook.API.Metrics.Sync") .AddMeter("Notesnook.API.Metrics.Sync")
.AddPrometheusExporter()); .AddPrometheusExporter());
services.AddQuartzHostedService(q =>
{
q.WaitForJobsToComplete = false;
q.AwaitApplicationStarted = true;
q.StartDelay = TimeSpan.FromMinutes(1);
}).AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionJobFactory();
var jobKey = new JobKey("DeviceCleanupJob");
q.AddJob<DeviceCleanupJob>(opts => opts.WithIdentity(jobKey));
q.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity("DeviceCleanup-trigger")
// first of every month
.WithCronSchedule("0 0 0 1 * ? *"));
});
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
if (!env.IsDevelopment()) app.UseForwardedHeadersWithKnownProxies(env);
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
}
app.UseOpenTelemetryPrometheusScrapingEndpoint((context) => context.Request.Path == "/metrics" && context.Connection.LocalPort == 5067); app.UseOpenTelemetryPrometheusScrapingEndpoint((context) => context.Request.Path == "/metrics" && context.Connection.LocalPort == 5067);
app.UseResponseCompression(); app.UseResponseCompression();
app.UseWebSockets(new Microsoft.AspNetCore.Builder.WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(30),
KeepAliveTimeout = TimeSpan.FromSeconds(60),
});
app.UseCors("notesnook"); app.UseCors("notesnook");
app.UseVersion(Servers.NotesnookAPI); app.UseVersion(Servers.NotesnookAPI);
@@ -257,14 +317,8 @@ namespace Notesnook.API
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapPrometheusScrapingEndpoint();
endpoints.MapControllers(); endpoints.MapControllers();
endpoints.MapHealthChecks("/health"); endpoints.MapHealthChecks("/health");
endpoints.MapHub<SyncHub>("/hubs/sync", options =>
{
options.CloseOnAuthenticationExpiration = false;
options.Transports = HttpTransportType.WebSockets;
});
endpoints.MapHub<SyncV2Hub>("/hubs/sync/v2", options => endpoints.MapHub<SyncV2Hub>("/hubs/sync/v2", options =>
{ {
options.CloseOnAuthenticationExpiration = false; options.CloseOnAuthenticationExpiration = false;
@@ -278,7 +332,7 @@ namespace Notesnook.API
{ {
public static IServiceCollection AddMongoCollection(this IServiceCollection services, string collectionName, string database = "notesnook") public static IServiceCollection AddMongoCollection(this IServiceCollection services, string collectionName, string database = "notesnook")
{ {
services.AddKeyedSingleton(collectionName, (provider, key) => MongoDbContext.GetMongoCollection<SyncItem>(provider.GetService<MongoDB.Driver.IMongoClient>(), database, collectionName)); services.AddKeyedSingleton(collectionName, (provider, key) => MongoDbContext.GetMongoCollection<SyncItem>(provider.GetRequiredService<MongoDB.Driver.IMongoClient>(), database, collectionName));
return services; return services;
} }
} }
+15 -15
View File
@@ -1,15 +1,15 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Debug",
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information", "Microsoft.Hosting.Lifetime": "Information",
"Microsoft.AspNetCore.SignalR": "Trace", "Microsoft.AspNetCore.SignalR": "Trace",
"Microsoft.AspNetCore.Http.Connections": "Trace" "Microsoft.AspNetCore.Http.Connections": "Trace"
} }
}, },
"MongoDbSettings": { "MongoDbSettings": {
"ConnectionString": "mongodb://localhost:27017/notesnook", "ConnectionString": "mongodb://localhost:27017/notesnook",
"DatabaseName": "notesnook" "DatabaseName": "notesnook"
} }
} }
+7 -7
View File
@@ -1,7 +1,7 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Warning" "Default": "Warning"
} }
} }
} }
+2
View File
@@ -0,0 +1,2 @@
PORT=5181
NOTESNOOK_API_SERVER_URL=http://localhost:5264/
+3
View File
@@ -0,0 +1,3 @@
node_modules/
dist/
.env
+19
View File
@@ -0,0 +1,19 @@
FROM oven/bun:1.3.5-slim
RUN mkdir -p /home/bun/app && chown -R bun:bun /home/bun/app
WORKDIR /home/bun/app
USER bun
COPY --chown=bun:bun package.json bun.lock .
RUN bun install --frozen-lockfile
COPY --chown=bun:bun . .
RUN bun run build
EXPOSE 5181
CMD ["bun", "run", "start"]
+68
View File
@@ -0,0 +1,68 @@
# Notesnook Inbox API
## Running locally
### Requirements
- Bun (v1.3.0 or higher)
### Commands
- `bun install` - Install dependencies
- `bun run dev` - Start the development server
- `bun run build` - Build the project for production
- `bun run start` - Start the production server
## Self-hosting
The easiest way to self-host is with Docker or Docker Compose.
Prerequisites:
- `docker` (Engine) installed
- `docker-compose` (optional, for multi-service setups)
Build and run with Docker:
```bash
# build the image from the current folder
docker build -t notesnook-inbox-api .
# run the container (example)
docker run --rm -p 3000:3000 \
-e PORT=3000 \
-e NOTESNOOK_API_SERVER_URL="https://api.notesnook.com" \
notesnook-inbox-api
```
Docker Compose (example):
```yaml
services:
inbox-api:
image: notesnook-inbox-api
build: .
ports:
- "3000:3000"
environment:
PORT: 3000
NOTESNOOK_API_SERVER_URL: "https://api.notesnook.com"
restart: unless-stopped
```
Environment variables:
- `PORT` — port the service listens on (default: `5181`)
- `NOTESNOOK_API_SERVER_URL` — base URL of the Notesnook API used to fetch public inbox keys
_If you prefer running without Docker, use `bun install` and `bun run start` with the environment variables set._
## Writing from scratch
The inbox API server is pretty simple to write from scratch in any programming language and/or framework. There's only one endpoint that needs to be implemented, which does these three steps:
1. Fetch the user's public inbox API key from the Notesnook API.
2. Encrypt the payload using `openpgp` or any other `openpgp` compatible library.
3. Post the encrypted payload to the Notesnook API.
You can refer to the [source code](./src/index.ts) for implementation details.
+195
View File
@@ -0,0 +1,195 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "notesnook-inbox-api",
"dependencies": {
"express": "^5.1.0",
"express-rate-limit": "^8.1.0",
"openpgp": "^6.2.2",
"zod": "^4.1.9",
},
"devDependencies": {
"@types/express": "^5.0.3",
"@types/libsodium-wrappers-sumo": "^0.7.8",
"@types/node": "^24.5.2",
"typescript": "^5.9.2",
},
},
},
"packages": {
"@types/body-parser": ["@types/body-parser@1.19.6", "", { "dependencies": { "@types/connect": "*", "@types/node": "*" } }, "sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g=="],
"@types/connect": ["@types/connect@3.4.38", "", { "dependencies": { "@types/node": "*" } }, "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug=="],
"@types/express": ["@types/express@5.0.3", "", { "dependencies": { "@types/body-parser": "*", "@types/express-serve-static-core": "^5.0.0", "@types/serve-static": "*" } }, "sha512-wGA0NX93b19/dZC1J18tKWVIYWyyF2ZjT9vin/NRu0qzzvfVzWjs04iq2rQ3H65vCTQYlRqs3YHfY7zjdV+9Kw=="],
"@types/express-serve-static-core": ["@types/express-serve-static-core@5.0.7", "", { "dependencies": { "@types/node": "*", "@types/qs": "*", "@types/range-parser": "*", "@types/send": "*" } }, "sha512-R+33OsgWw7rOhD1emjU7dzCDHucJrgJXMA5PYCzJxVil0dsyx5iBEPHqpPfiKNJQb7lZ1vxwoLR4Z87bBUpeGQ=="],
"@types/http-errors": ["@types/http-errors@2.0.5", "", {}, "sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg=="],
"@types/libsodium-wrappers": ["@types/libsodium-wrappers@0.7.14", "", {}, "sha512-5Kv68fXuXK0iDuUir1WPGw2R9fOZUlYlSAa0ztMcL0s0BfIDTqg9GXz8K30VJpPP3sxWhbolnQma2x+/TfkzDQ=="],
"@types/libsodium-wrappers-sumo": ["@types/libsodium-wrappers-sumo@0.7.8", "", { "dependencies": { "@types/libsodium-wrappers": "*" } }, "sha512-N2+df4MB/A+W0RAcTw7A5oxKgzD+Vh6Ye7lfjWIi5SdTzVLfHPzxUjhwPqHLO5Ev9fv/+VHl+sUaUuTg4fUPqw=="],
"@types/mime": ["@types/mime@1.3.5", "", {}, "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w=="],
"@types/node": ["@types/node@24.5.2", "", { "dependencies": { "undici-types": "~7.12.0" } }, "sha512-FYxk1I7wPv3K2XBaoyH2cTnocQEu8AOZ60hPbsyukMPLv5/5qr7V1i8PLHdl6Zf87I+xZXFvPCXYjiTFq+YSDQ=="],
"@types/qs": ["@types/qs@6.14.0", "", {}, "sha512-eOunJqu0K1923aExK6y8p6fsihYEn/BYuQ4g0CxAAgFc4b/ZLN4CrsRZ55srTdqoiLzU2B2evC+apEIxprEzkQ=="],
"@types/range-parser": ["@types/range-parser@1.2.7", "", {}, "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ=="],
"@types/send": ["@types/send@0.17.5", "", { "dependencies": { "@types/mime": "^1", "@types/node": "*" } }, "sha512-z6F2D3cOStZvuk2SaP6YrwkNO65iTZcwA2ZkSABegdkAh/lf+Aa/YQndZVfmEXT5vgAp6zv06VQ3ejSVjAny4w=="],
"@types/serve-static": ["@types/serve-static@1.15.8", "", { "dependencies": { "@types/http-errors": "*", "@types/node": "*", "@types/send": "*" } }, "sha512-roei0UY3LhpOJvjbIP6ZZFngyLKl5dskOtDhxY5THRSpO+ZI+nzJ+m5yUMzGrp89YRa7lvknKkMYjqQFGwA7Sg=="],
"accepts": ["accepts@2.0.0", "", { "dependencies": { "mime-types": "^3.0.0", "negotiator": "^1.0.0" } }, "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng=="],
"body-parser": ["body-parser@2.2.0", "", { "dependencies": { "bytes": "^3.1.2", "content-type": "^1.0.5", "debug": "^4.4.0", "http-errors": "^2.0.0", "iconv-lite": "^0.6.3", "on-finished": "^2.4.1", "qs": "^6.14.0", "raw-body": "^3.0.0", "type-is": "^2.0.0" } }, "sha512-02qvAaxv8tp7fBa/mw1ga98OGm+eCbqzJOKoRt70sLmfEEi+jyBYVTDGfCL/k06/4EMk/z01gCe7HoCH/f2LTg=="],
"bytes": ["bytes@3.1.2", "", {}, "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg=="],
"call-bind-apply-helpers": ["call-bind-apply-helpers@1.0.2", "", { "dependencies": { "es-errors": "^1.3.0", "function-bind": "^1.1.2" } }, "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ=="],
"call-bound": ["call-bound@1.0.4", "", { "dependencies": { "call-bind-apply-helpers": "^1.0.2", "get-intrinsic": "^1.3.0" } }, "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg=="],
"content-disposition": ["content-disposition@1.0.0", "", { "dependencies": { "safe-buffer": "5.2.1" } }, "sha512-Au9nRL8VNUut/XSzbQA38+M78dzP4D+eqg3gfJHMIHHYa3bg067xj1KxMUWj+VULbiZMowKngFFbKczUrNJ1mg=="],
"content-type": ["content-type@1.0.5", "", {}, "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA=="],
"cookie": ["cookie@0.7.2", "", {}, "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w=="],
"cookie-signature": ["cookie-signature@1.2.2", "", {}, "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg=="],
"debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="],
"depd": ["depd@2.0.0", "", {}, "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="],
"dunder-proto": ["dunder-proto@1.0.1", "", { "dependencies": { "call-bind-apply-helpers": "^1.0.1", "es-errors": "^1.3.0", "gopd": "^1.2.0" } }, "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A=="],
"ee-first": ["ee-first@1.1.1", "", {}, "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="],
"encodeurl": ["encodeurl@2.0.0", "", {}, "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg=="],
"es-define-property": ["es-define-property@1.0.1", "", {}, "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g=="],
"es-errors": ["es-errors@1.3.0", "", {}, "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw=="],
"es-object-atoms": ["es-object-atoms@1.1.1", "", { "dependencies": { "es-errors": "^1.3.0" } }, "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA=="],
"escape-html": ["escape-html@1.0.3", "", {}, "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow=="],
"etag": ["etag@1.8.1", "", {}, "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg=="],
"express": ["express@5.1.0", "", { "dependencies": { "accepts": "^2.0.0", "body-parser": "^2.2.0", "content-disposition": "^1.0.0", "content-type": "^1.0.5", "cookie": "^0.7.1", "cookie-signature": "^1.2.1", "debug": "^4.4.0", "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "etag": "^1.8.1", "finalhandler": "^2.1.0", "fresh": "^2.0.0", "http-errors": "^2.0.0", "merge-descriptors": "^2.0.0", "mime-types": "^3.0.0", "on-finished": "^2.4.1", "once": "^1.4.0", "parseurl": "^1.3.3", "proxy-addr": "^2.0.7", "qs": "^6.14.0", "range-parser": "^1.2.1", "router": "^2.2.0", "send": "^1.1.0", "serve-static": "^2.2.0", "statuses": "^2.0.1", "type-is": "^2.0.1", "vary": "^1.1.2" } }, "sha512-DT9ck5YIRU+8GYzzU5kT3eHGA5iL+1Zd0EutOmTE9Dtk+Tvuzd23VBU+ec7HPNSTxXYO55gPV/hq4pSBJDjFpA=="],
"express-rate-limit": ["express-rate-limit@8.1.0", "", { "dependencies": { "ip-address": "10.0.1" }, "peerDependencies": { "express": ">= 4.11" } }, "sha512-4nLnATuKupnmwqiJc27b4dCFmB/T60ExgmtDD7waf4LdrbJ8CPZzZRHYErDYNhoz+ql8fUdYwM/opf90PoPAQA=="],
"finalhandler": ["finalhandler@2.1.0", "", { "dependencies": { "debug": "^4.4.0", "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "on-finished": "^2.4.1", "parseurl": "^1.3.3", "statuses": "^2.0.1" } }, "sha512-/t88Ty3d5JWQbWYgaOGCCYfXRwV1+be02WqYYlL6h0lEiUAMPM8o8qKGO01YIkOHzka2up08wvgYD0mDiI+q3Q=="],
"forwarded": ["forwarded@0.2.0", "", {}, "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow=="],
"fresh": ["fresh@2.0.0", "", {}, "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A=="],
"function-bind": ["function-bind@1.1.2", "", {}, "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA=="],
"get-intrinsic": ["get-intrinsic@1.3.0", "", { "dependencies": { "call-bind-apply-helpers": "^1.0.2", "es-define-property": "^1.0.1", "es-errors": "^1.3.0", "es-object-atoms": "^1.1.1", "function-bind": "^1.1.2", "get-proto": "^1.0.1", "gopd": "^1.2.0", "has-symbols": "^1.1.0", "hasown": "^2.0.2", "math-intrinsics": "^1.1.0" } }, "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ=="],
"get-proto": ["get-proto@1.0.1", "", { "dependencies": { "dunder-proto": "^1.0.1", "es-object-atoms": "^1.0.0" } }, "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g=="],
"gopd": ["gopd@1.2.0", "", {}, "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg=="],
"has-symbols": ["has-symbols@1.1.0", "", {}, "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ=="],
"hasown": ["hasown@2.0.2", "", { "dependencies": { "function-bind": "^1.1.2" } }, "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ=="],
"http-errors": ["http-errors@2.0.0", "", { "dependencies": { "depd": "2.0.0", "inherits": "2.0.4", "setprototypeof": "1.2.0", "statuses": "2.0.1", "toidentifier": "1.0.1" } }, "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ=="],
"iconv-lite": ["iconv-lite@0.6.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw=="],
"inherits": ["inherits@2.0.4", "", {}, "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="],
"ip-address": ["ip-address@10.0.1", "", {}, "sha512-NWv9YLW4PoW2B7xtzaS3NCot75m6nK7Icdv0o3lfMceJVRfSoQwqD4wEH5rLwoKJwUiZ/rfpiVBhnaF0FK4HoA=="],
"ipaddr.js": ["ipaddr.js@1.9.1", "", {}, "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="],
"is-promise": ["is-promise@4.0.0", "", {}, "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ=="],
"math-intrinsics": ["math-intrinsics@1.1.0", "", {}, "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="],
"media-typer": ["media-typer@1.1.0", "", {}, "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw=="],
"merge-descriptors": ["merge-descriptors@2.0.0", "", {}, "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g=="],
"mime-db": ["mime-db@1.54.0", "", {}, "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ=="],
"mime-types": ["mime-types@3.0.1", "", { "dependencies": { "mime-db": "^1.54.0" } }, "sha512-xRc4oEhT6eaBpU1XF7AjpOFD+xQmXNB5OVKwp4tqCuBpHLS/ZbBDrc07mYTDqVMg6PfxUjjNp85O6Cd2Z/5HWA=="],
"ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],
"negotiator": ["negotiator@1.0.0", "", {}, "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg=="],
"object-inspect": ["object-inspect@1.13.4", "", {}, "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew=="],
"on-finished": ["on-finished@2.4.1", "", { "dependencies": { "ee-first": "1.1.1" } }, "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg=="],
"once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="],
"openpgp": ["openpgp@6.2.2", "", {}, "sha512-P/dyEqQ3gfwOCo+xsqffzXjmUhGn4AZTOJ1LCcN21S23vAk+EAvMJOQTsb/C8krL6GjOSBxqGYckhik7+hneNw=="],
"parseurl": ["parseurl@1.3.3", "", {}, "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="],
"path-to-regexp": ["path-to-regexp@8.3.0", "", {}, "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA=="],
"proxy-addr": ["proxy-addr@2.0.7", "", { "dependencies": { "forwarded": "0.2.0", "ipaddr.js": "1.9.1" } }, "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg=="],
"qs": ["qs@6.14.0", "", { "dependencies": { "side-channel": "^1.1.0" } }, "sha512-YWWTjgABSKcvs/nWBi9PycY/JiPJqOD4JA6o9Sej2AtvSGarXxKC3OQSk4pAarbdQlKAh5D4FCQkJNkW+GAn3w=="],
"range-parser": ["range-parser@1.2.1", "", {}, "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg=="],
"raw-body": ["raw-body@3.0.1", "", { "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", "iconv-lite": "0.7.0", "unpipe": "1.0.0" } }, "sha512-9G8cA+tuMS75+6G/TzW8OtLzmBDMo8p1JRxN5AZ+LAp8uxGA8V8GZm4GQ4/N5QNQEnLmg6SS7wyuSmbKepiKqA=="],
"router": ["router@2.2.0", "", { "dependencies": { "debug": "^4.4.0", "depd": "^2.0.0", "is-promise": "^4.0.0", "parseurl": "^1.3.3", "path-to-regexp": "^8.0.0" } }, "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ=="],
"safe-buffer": ["safe-buffer@5.2.1", "", {}, "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="],
"safer-buffer": ["safer-buffer@2.1.2", "", {}, "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="],
"send": ["send@1.2.0", "", { "dependencies": { "debug": "^4.3.5", "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "etag": "^1.8.1", "fresh": "^2.0.0", "http-errors": "^2.0.0", "mime-types": "^3.0.1", "ms": "^2.1.3", "on-finished": "^2.4.1", "range-parser": "^1.2.1", "statuses": "^2.0.1" } }, "sha512-uaW0WwXKpL9blXE2o0bRhoL2EGXIrZxQ2ZQ4mgcfoBxdFmQold+qWsD2jLrfZ0trjKL6vOw0j//eAwcALFjKSw=="],
"serve-static": ["serve-static@2.2.0", "", { "dependencies": { "encodeurl": "^2.0.0", "escape-html": "^1.0.3", "parseurl": "^1.3.3", "send": "^1.2.0" } }, "sha512-61g9pCh0Vnh7IutZjtLGGpTA355+OPn2TyDv/6ivP2h/AdAVX9azsoxmg2/M6nZeQZNYBEwIcsne1mJd9oQItQ=="],
"setprototypeof": ["setprototypeof@1.2.0", "", {}, "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="],
"side-channel": ["side-channel@1.1.0", "", { "dependencies": { "es-errors": "^1.3.0", "object-inspect": "^1.13.3", "side-channel-list": "^1.0.0", "side-channel-map": "^1.0.1", "side-channel-weakmap": "^1.0.2" } }, "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw=="],
"side-channel-list": ["side-channel-list@1.0.0", "", { "dependencies": { "es-errors": "^1.3.0", "object-inspect": "^1.13.3" } }, "sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA=="],
"side-channel-map": ["side-channel-map@1.0.1", "", { "dependencies": { "call-bound": "^1.0.2", "es-errors": "^1.3.0", "get-intrinsic": "^1.2.5", "object-inspect": "^1.13.3" } }, "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA=="],
"side-channel-weakmap": ["side-channel-weakmap@1.0.2", "", { "dependencies": { "call-bound": "^1.0.2", "es-errors": "^1.3.0", "get-intrinsic": "^1.2.5", "object-inspect": "^1.13.3", "side-channel-map": "^1.0.1" } }, "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A=="],
"statuses": ["statuses@2.0.2", "", {}, "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw=="],
"toidentifier": ["toidentifier@1.0.1", "", {}, "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA=="],
"type-is": ["type-is@2.0.1", "", { "dependencies": { "content-type": "^1.0.5", "media-typer": "^1.1.0", "mime-types": "^3.0.0" } }, "sha512-OZs6gsjF4vMp32qrCbiVSkrFmXtG/AZhY3t0iAMrMBiAZyV9oALtXO8hsrHbMXF9x6L3grlFuwW2oAz7cav+Gw=="],
"typescript": ["typescript@5.9.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A=="],
"undici-types": ["undici-types@7.12.0", "", {}, "sha512-goOacqME2GYyOZZfb5Lgtu+1IDmAlAEu5xnD3+xTzS10hT0vzpf0SPjkXwAw9Jm+4n/mQGDP3LO8CPbYROeBfQ=="],
"unpipe": ["unpipe@1.0.0", "", {}, "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ=="],
"vary": ["vary@1.1.2", "", {}, "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg=="],
"wrappy": ["wrappy@1.0.2", "", {}, "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="],
"zod": ["zod@4.1.9", "", {}, "sha512-HI32jTq0AUAC125z30E8bQNz0RQ+9Uc+4J7V97gLYjZVKRjeydPgGt6dvQzFrav7MYOUGFqqOGiHpA/fdbd0cQ=="],
"http-errors/statuses": ["statuses@2.0.1", "", {}, "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ=="],
"raw-body/iconv-lite": ["iconv-lite@0.7.0", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-cf6L2Ds3h57VVmkZe+Pn+5APsT7FpqJtEhhieDCvrE2MK5Qk9MyffgQyuxQTm6BChfeZNtcOLHp9IcWRVcIcBQ=="],
}
}
+34
View File
@@ -0,0 +1,34 @@
{
"name": "notesnook-inbox-api",
"version": "1.0.0",
"description": "Notesnook Inbox API server",
"main": "dist/index.js",
"scripts": {
"build": "bun build src/index.ts --outdir dist --target bun",
"start": "bun run dist/index.js",
"dev": "bun --watch src/index.ts"
},
"keywords": [
"notesnook",
"inbox",
"api"
],
"license": "GPL-3.0-or-later",
"author": {
"name": "Streetwriters (Private) Limited",
"email": "support@streetwriters.co",
"url": "https://streetwriters.co"
},
"dependencies": {
"express": "^5.1.0",
"express-rate-limit": "^8.1.0",
"openpgp": "^6.2.2",
"zod": "^4.1.9"
},
"devDependencies": {
"@types/libsodium-wrappers-sumo": "^0.7.8",
"@types/express": "^5.0.3",
"@types/node": "^24.5.2",
"typescript": "^5.9.2"
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/bin/bash
GNUPGHOME=$(mktemp -d)
curl -s http://localhost:5264/inbox/public-encryption-key -H "Authorization: $API_KEY" | jq -r .key > "$GNUPGHOME"/pubkey.asc && gpg --batch --homedir "$GNUPGHOME" --import "$GNUPGHOME"/pubkey.asc >/dev/null 2>&1 && KEYID=$(gpg --homedir "$GNUPGHOME" --list-keys --with-colons | awk -F: '/^pub:/ {print $5; exit}') && printf '%s' '{"title":"Test title CLIE S","type":"note","source":"cli","version":1}' | gpg --batch --homedir "$GNUPGHOME" --trust-model always --armor --encrypt -r "$KEYID" | jq -Rs --arg alg "pgp-aes256" '{v:1, cipher:., alg:$alg}' | curl -s -X POST http://localhost:5264/inbox/items -H "Content-Type: application/json" -H "Authorization: $API_KEY" -d @- && rm -rf "$GNUPGHOME"
+18
View File
@@ -0,0 +1,18 @@
const response = await fetch("http://localhost:5181/inbox", {
method: "POST",
headers: {
Authorization: process.env.API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
title: "This is test note 4",
type: "note",
source: "script",
version: 1,
content: {
type: "html",
data: "<p>This is test note content 3</p>",
},
}),
});
console.log(await response.text());
+167
View File
@@ -0,0 +1,167 @@
import express from "express";
import { z } from "zod";
import { rateLimit } from "express-rate-limit";
import * as openpgp from "openpgp";
const NOTESNOOK_API_SERVER_URL = process.env.NOTESNOOK_API_SERVER_URL;
if (!NOTESNOOK_API_SERVER_URL) {
throw new Error("NOTESNOOK_API_SERVER_URL is not defined");
}
const RawInboxItemSchema = z.object({
title: z.string().min(1, "Title is required"),
pinned: z.boolean().optional(),
favorite: z.boolean().optional(),
readonly: z.boolean().optional(),
archived: z.boolean().optional(),
notebookIds: z.array(z.string()).optional(),
tagIds: z.array(z.string()).optional(),
type: z.enum(["note"]),
source: z.string().min(1, "Source is required"),
version: z.literal(1),
content: z
.object({
type: z.enum(["html"]),
data: z.string(),
})
.optional(),
});
interface EncryptedInboxItem {
v: 1;
cipher: string;
alg: string;
}
/**
* Encrypts raw data using OpenPGP with the recipient's public key
*
* @param {string} rawData - The plaintext data to encrypt
* @param {string} rawPublicKey - The recipient's OpenPGP public key
*/
async function encrypt(
rawData: string,
rawPublicKey: string,
): Promise<EncryptedInboxItem> {
const publicKey = await openpgp.readKey({ armoredKey: rawPublicKey });
const message = await openpgp.createMessage({ text: rawData });
const encrypted = await openpgp.encrypt({
message,
encryptionKeys: publicKey,
});
return {
v: 1,
cipher: encrypted,
alg: "pgp-aes256",
};
}
async function getInboxPublicEncryptionKey(
apiKey: string,
): Promise<{ status: "unauthorized" } | { status: "ok"; key: string | null }> {
const response = await fetch(
`${NOTESNOOK_API_SERVER_URL}/inbox/public-encryption-key`,
{
headers: {
Authorization: apiKey,
},
},
);
if (response.status === 401) {
return { status: "unauthorized" };
}
if (!response.ok) {
throw new Error(
`failed to fetch inbox public encryption key: ${await response.text()}`,
);
}
const data = (await response.json()) as unknown as any;
return { status: "ok", key: (data?.key as string) || null };
}
async function postEncryptedInboxItem(
apiKey: string,
item: EncryptedInboxItem,
) {
const response = await fetch(`${NOTESNOOK_API_SERVER_URL}/inbox/items`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: apiKey,
},
body: JSON.stringify({ ...item }),
});
if (!response.ok) {
throw new Error(`failed to post inbox item: ${await response.text()}`);
}
}
const app = express();
app.use(express.json({ limit: "10mb" }));
app.use(
rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
limit: 60,
}),
);
app.get("/health", (_, res) => {
return res.status(200).json({ status: "ok" });
});
app.post("/", async (req, res) => {
try {
const apiKey = req.headers["authorization"];
if (!apiKey) {
return res.status(401).json({ error: "unauthorized" });
}
const encryptionKeyResult = await getInboxPublicEncryptionKey(apiKey);
if (encryptionKeyResult.status === "unauthorized") {
return res.status(401).json({ error: "unauthorized" });
}
if (!encryptionKeyResult.key) {
return res.status(404).json({ error: "inbox public key not found" });
}
const inboxPublicKey = encryptionKeyResult.key;
console.log("[info] fetched inbox public key");
const validationResult = RawInboxItemSchema.safeParse(req.body);
if (!validationResult.success) {
return res.status(400).json({
error: "invalid item",
details: validationResult.error.issues,
});
}
const encryptedItem = await encrypt(
JSON.stringify(validationResult.data),
inboxPublicKey,
);
console.log("[info] encrypted item");
await postEncryptedInboxItem(apiKey, encryptedItem);
console.log("[info] posted encrypted inbox item successfully");
return res.status(200).json({ success: true });
} catch (error) {
if (error instanceof Error) {
console.log("[error]", error.message);
return res
.status(500)
.json({ error: "internal server error", description: error.message });
} else {
console.log("[error] unknown error occured:", error);
return res.status(500).json({
error: "internal server error",
description: `unknown error occured: ${error}`,
});
}
}
});
const PORT = Number(process.env.PORT || "5181");
app.listen(PORT, () => {
console.log(`📫 notesnook inbox api server running on port ${PORT}`);
});
export default app;
+13
View File
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"lib": ["ES2020"],
"outDir": "./dist",
"strict": true,
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
+2 -2
View File
@@ -69,7 +69,7 @@ This takes care of setting up everything including MongoDB, Minio etc.
## TODO Self-hosting ## TODO Self-hosting
**Note: Self-hosting the Notesnook Sync Server is not yet possible. We are working to enable full on-premise self hosting so stay tuned!** **Note: Self-hosting the Notesnook Sync Server is now possible, but without support. Documentation will be provided at a later date. We are working to enable full on-premise self-hosting, so stay tuned!**
- [x] Open source the Sync server - [x] Open source the Sync server
- [x] Open source the Identity server - [x] Open source the Identity server
@@ -77,8 +77,8 @@ This takes care of setting up everything including MongoDB, Minio etc.
- [x] Fully Dockerize all services - [x] Fully Dockerize all services
- [x] Use self-hosted Minio for S3 storage - [x] Use self-hosted Minio for S3 storage
- [x] Publish on DockerHub - [x] Publish on DockerHub
- [x] Add settings to change server URLs in Notesnook client apps (starting from v3.0.18)
- [ ] Write self hosting docs - [ ] Write self hosting docs
- [ ] Add settings to change server URLs in Notesnook client apps
## License ## License
@@ -0,0 +1,48 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Streetwriters.Common.Interfaces;
namespace Streetwriters.Common.Accessors
{
public class WampServiceAccessor(Server server) : IHostedService
{
public IUserAccountService UserAccountService { get; set; }
public IUserSubscriptionService? UserSubscriptionService { get; set; }
public async Task StartAsync(CancellationToken cancellationToken)
{
await InitAsync();
}
private async Task InitAsync()
{
this.UserAccountService = await WampServers.IdentityServer.GetServiceAsync<IUserAccountService>(InitAsync);
if (!Constants.IS_SELF_HOSTED && server != Servers.SubscriptionServer)
{
this.UserSubscriptionService = await WampServers.SubscriptionServer.GetServiceAsync<IUserSubscriptionService>(InitAsync);
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
+83 -81
View File
@@ -1,82 +1,84 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Streetwriters.Common.Enums; using Streetwriters.Common.Enums;
using Streetwriters.Common.Interfaces; using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages; using Streetwriters.Common.Messages;
using Streetwriters.Common.Models; using Streetwriters.Common.Models;
namespace Streetwriters.Common namespace Streetwriters.Common
{ {
public class Clients public class Clients
{ {
public static readonly Client Notesnook = new() public static readonly Client Notesnook = new()
{ {
Id = "notesnook", Id = "notesnook",
Name = "Notesnook", Name = "Notesnook",
SenderEmail = Constants.NOTESNOOK_SENDER_EMAIL, SenderEmail = Constants.NOTESNOOK_SENDER_EMAIL ?? "noreply@notesnook.com",
SenderName = "Notesnook", SenderName = "Notesnook",
Type = ApplicationType.NOTESNOOK, Type = ApplicationType.NOTESNOOK,
AppId = ApplicationType.NOTESNOOK, AppId = ApplicationType.NOTESNOOK,
AccountRecoveryRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/recovery", AccountRecoveryRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/recovery",
EmailConfirmedRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/verified", EmailConfirmedRedirectURL = $"{Constants.NOTESNOOK_APP_HOST}/account/verified",
OnEmailConfirmed = async (userId) => PackageName = "com.streetwriters.notesnook",
{ OnEmailConfirmed = async (userId) =>
await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage {
{ await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
UserId = userId, {
Message = new Message UserId = userId,
{ Message = new Message
Type = "emailConfirmed", {
Data = null Type = "emailConfirmed",
} Data = null
}); }
} });
}; }
};
public static Dictionary<string, Client> ClientsMap = new()
{ public static Dictionary<string, Client> ClientsMap = new()
{ "notesnook", Notesnook } {
}; { "notesnook", Notesnook }
};
public static Client FindClientById(string id)
{ public static Client? FindClientById(string? id)
if (!IsValidClient(id)) return null; {
return ClientsMap[id]; if (string.IsNullOrEmpty(id) || !IsValidClient(id)) return null;
} return ClientsMap[id];
}
public static Client FindClientByAppId(ApplicationType appId)
{ public static Client? FindClientByAppId(ApplicationType? appId)
switch (appId) {
{ if (appId is null) return null;
case ApplicationType.NOTESNOOK: switch (appId)
return ClientsMap["notesnook"]; {
} case ApplicationType.NOTESNOOK:
return null; return ClientsMap["notesnook"];
} }
return null;
public static bool IsValidClient(string id) }
{
return ClientsMap.ContainsKey(id); public static bool IsValidClient(string id)
} {
} return ClientsMap.ContainsKey(id);
}
}
} }
+98 -81
View File
@@ -1,81 +1,98 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
namespace Streetwriters.Common namespace Streetwriters.Common
{ {
public class Constants public class Constants
{ {
public static int COMPATIBILITY_VERSION = 1; public static int COMPATIBILITY_VERSION = 1;
public static bool IS_SELF_HOSTED => Environment.GetEnvironmentVariable("SELF_HOSTED") == "1"; public static bool IS_SELF_HOSTED => ReadSecret("SELF_HOSTED") == "1";
public static bool DISABLE_ACCOUNT_CREATION => Environment.GetEnvironmentVariable("DISABLE_ACCOUNT_CREATION") == "1"; public static bool DISABLE_SIGNUPS => ReadSecret("DISABLE_SIGNUPS") == "true";
public static string INSTANCE_NAME => Environment.GetEnvironmentVariable("INSTANCE_NAME") ?? "default"; public static string INSTANCE_NAME => ReadSecret("INSTANCE_NAME") ?? "default";
// S3 related // S3 related
public static string S3_ACCESS_KEY => Environment.GetEnvironmentVariable("S3_ACCESS_KEY"); public static string S3_ACCESS_KEY => ReadSecret("S3_ACCESS_KEY") ?? throw new InvalidOperationException("S3_ACCESS_KEY is required");
public static string S3_ACCESS_KEY_ID => Environment.GetEnvironmentVariable("S3_ACCESS_KEY_ID"); public static string S3_ACCESS_KEY_ID => ReadSecret("S3_ACCESS_KEY_ID") ?? throw new InvalidOperationException("S3_ACCESS_KEY_ID is required");
public static string S3_SERVICE_URL => Environment.GetEnvironmentVariable("S3_SERVICE_URL"); public static string S3_SERVICE_URL => ReadSecret("S3_SERVICE_URL") ?? throw new InvalidOperationException("S3_SERVICE_URL is required");
public static string S3_REGION => Environment.GetEnvironmentVariable("S3_REGION"); public static string S3_REGION => ReadSecret("S3_REGION") ?? throw new InvalidOperationException("S3_REGION is required");
public static string S3_BUCKET_NAME => Environment.GetEnvironmentVariable("S3_BUCKET_NAME"); public static string S3_BUCKET_NAME => ReadSecret("S3_BUCKET_NAME") ?? throw new InvalidOperationException("S3_BUCKET_NAME is required");
public static string S3_INTERNAL_BUCKET_NAME => Environment.GetEnvironmentVariable("S3_INTERNAL_BUCKET_NAME"); public static string? S3_INTERNAL_BUCKET_NAME => ReadSecret("S3_INTERNAL_BUCKET_NAME");
public static string S3_INTERNAL_SERVICE_URL => Environment.GetEnvironmentVariable("S3_INTERNAL_SERVICE_URL"); public static string? S3_INTERNAL_SERVICE_URL => ReadSecret("S3_INTERNAL_SERVICE_URL");
// SMTP settings // SMTP settings
public static string SMTP_USERNAME => Environment.GetEnvironmentVariable("SMTP_USERNAME"); public static string? SMTP_USERNAME => ReadSecret("SMTP_USERNAME");
public static string SMTP_PASSWORD => Environment.GetEnvironmentVariable("SMTP_PASSWORD"); public static string? SMTP_PASSWORD => ReadSecret("SMTP_PASSWORD");
public static string SMTP_HOST => Environment.GetEnvironmentVariable("SMTP_HOST"); public static string? SMTP_HOST => ReadSecret("SMTP_HOST");
public static string SMTP_PORT => Environment.GetEnvironmentVariable("SMTP_PORT"); public static string? SMTP_PORT => ReadSecret("SMTP_PORT");
public static string SMTP_REPLYTO_EMAIL => Environment.GetEnvironmentVariable("SMTP_REPLYTO_EMAIL"); public static string? SMTP_REPLYTO_EMAIL => ReadSecret("SMTP_REPLYTO_EMAIL");
public static string NOTESNOOK_SENDER_EMAIL => Environment.GetEnvironmentVariable("NOTESNOOK_SENDER_EMAIL") ?? Environment.GetEnvironmentVariable("SMTP_USERNAME"); public static string? NOTESNOOK_SENDER_EMAIL => ReadSecret("NOTESNOOK_SENDER_EMAIL") ?? ReadSecret("SMTP_USERNAME");
public static string NOTESNOOK_APP_HOST => Environment.GetEnvironmentVariable("NOTESNOOK_APP_HOST"); public static string? NOTESNOOK_APP_HOST => ReadSecret("NOTESNOOK_APP_HOST");
public static string NOTESNOOK_API_SECRET => Environment.GetEnvironmentVariable("NOTESNOOK_API_SECRET"); public static string NOTESNOOK_API_SECRET => ReadSecret("NOTESNOOK_API_SECRET") ?? throw new InvalidOperationException("NOTESNOOK_API_SECRET is required");
// MessageBird is used for SMS sending // MessageBird is used for SMS sending
public static string TWILIO_ACCOUNT_SID => Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"); public static string? TWILIO_ACCOUNT_SID => ReadSecret("TWILIO_ACCOUNT_SID");
public static string TWILIO_AUTH_TOKEN => Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN"); public static string? TWILIO_AUTH_TOKEN => ReadSecret("TWILIO_AUTH_TOKEN");
public static string TWILIO_SERVICE_SID => Environment.GetEnvironmentVariable("TWILIO_SERVICE_SID"); public static string? TWILIO_SERVICE_SID => ReadSecret("TWILIO_SERVICE_SID");
// Server discovery // Server discovery
public static int NOTESNOOK_SERVER_PORT => int.Parse(Environment.GetEnvironmentVariable("NOTESNOOK_SERVER_PORT") ?? "80"); public static int NOTESNOOK_SERVER_PORT => int.Parse(ReadSecret("NOTESNOOK_SERVER_PORT") ?? "80");
public static string NOTESNOOK_SERVER_HOST => Environment.GetEnvironmentVariable("NOTESNOOK_SERVER_HOST"); public static string? NOTESNOOK_SERVER_HOST => ReadSecret("NOTESNOOK_SERVER_HOST");
public static string NOTESNOOK_CERT_PATH => Environment.GetEnvironmentVariable("NOTESNOOK_CERT_PATH"); public static string? NOTESNOOK_CERT_PATH => ReadSecret("NOTESNOOK_CERT_PATH");
public static string NOTESNOOK_CERT_KEY_PATH => Environment.GetEnvironmentVariable("NOTESNOOK_CERT_KEY_PATH"); public static string? NOTESNOOK_CERT_KEY_PATH => ReadSecret("NOTESNOOK_CERT_KEY_PATH");
public static string[] KNOWN_PROXIES => (ReadSecret("KNOWN_PROXIES") ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
public static int IDENTITY_SERVER_PORT => int.Parse(Environment.GetEnvironmentVariable("IDENTITY_SERVER_PORT") ?? "80");
public static string IDENTITY_SERVER_HOST => Environment.GetEnvironmentVariable("IDENTITY_SERVER_HOST"); public static int IDENTITY_SERVER_PORT => int.Parse(ReadSecret("IDENTITY_SERVER_PORT") ?? "80");
public static string IDENTITY_SERVER_DOMAIN => Environment.GetEnvironmentVariable("IDENTITY_SERVER_DOMAIN"); public static string? IDENTITY_SERVER_HOST => ReadSecret("IDENTITY_SERVER_HOST");
public static string IDENTITY_CERT_PATH => Environment.GetEnvironmentVariable("IDENTITY_CERT_PATH"); public static Uri? IDENTITY_SERVER_URL => ReadSecret("IDENTITY_SERVER_URL") is string url ? new Uri(url) : null;
public static string IDENTITY_CERT_KEY_PATH => Environment.GetEnvironmentVariable("IDENTITY_CERT_KEY_PATH"); public static string? IDENTITY_CERT_PATH => ReadSecret("IDENTITY_CERT_PATH");
public static string? IDENTITY_CERT_KEY_PATH => ReadSecret("IDENTITY_CERT_KEY_PATH");
public static int SSE_SERVER_PORT => int.Parse(Environment.GetEnvironmentVariable("SSE_SERVER_PORT") ?? "80");
public static string SSE_SERVER_HOST => Environment.GetEnvironmentVariable("SSE_SERVER_HOST"); public static int SSE_SERVER_PORT => int.Parse(ReadSecret("SSE_SERVER_PORT") ?? "80");
public static string SSE_CERT_PATH => Environment.GetEnvironmentVariable("SSE_CERT_PATH"); public static string? SSE_SERVER_HOST => ReadSecret("SSE_SERVER_HOST");
public static string SSE_CERT_KEY_PATH => Environment.GetEnvironmentVariable("SSE_CERT_KEY_PATH"); public static string? SSE_CERT_PATH => ReadSecret("SSE_CERT_PATH");
public static string? SSE_CERT_KEY_PATH => ReadSecret("SSE_CERT_KEY_PATH");
// internal
public static string MONGODB_CONNECTION_STRING => Environment.GetEnvironmentVariable("MONGODB_CONNECTION_STRING"); // internal
public static string MONGODB_DATABASE_NAME => Environment.GetEnvironmentVariable("MONGODB_DATABASE_NAME"); public static string? WEBRISK_API_URI => ReadSecret("WEBRISK_API_URI");
public static int SUBSCRIPTIONS_SERVER_PORT => int.Parse(Environment.GetEnvironmentVariable("SUBSCRIPTIONS_SERVER_PORT") ?? "80"); public static string MONGODB_CONNECTION_STRING => ReadSecret("MONGODB_CONNECTION_STRING") ?? throw new ArgumentNullException("MONGODB_CONNECTION_STRING environment variable is not set");
public static string SUBSCRIPTIONS_SERVER_HOST => Environment.GetEnvironmentVariable("SUBSCRIPTIONS_SERVER_HOST"); public static string MONGODB_DATABASE_NAME => ReadSecret("MONGODB_DATABASE_NAME") ?? throw new ArgumentNullException("MONGODB_DATABASE_NAME environment variable is not set");
public static string SUBSCRIPTIONS_CERT_PATH => Environment.GetEnvironmentVariable("SUBSCRIPTIONS_CERT_PATH"); public static int SUBSCRIPTIONS_SERVER_PORT => int.Parse(ReadSecret("SUBSCRIPTIONS_SERVER_PORT") ?? "80");
public static string SUBSCRIPTIONS_CERT_KEY_PATH => Environment.GetEnvironmentVariable("SUBSCRIPTIONS_CERT_KEY_PATH"); public static string? SUBSCRIPTIONS_SERVER_HOST => ReadSecret("SUBSCRIPTIONS_SERVER_HOST");
public static string[] NOTESNOOK_CORS_ORIGINS => Environment.GetEnvironmentVariable("NOTESNOOK_CORS")?.Split(",") ?? new string[] { }; public static string? SUBSCRIPTIONS_CERT_PATH => ReadSecret("SUBSCRIPTIONS_CERT_PATH");
} public static string? SUBSCRIPTIONS_CERT_KEY_PATH => ReadSecret("SUBSCRIPTIONS_CERT_KEY_PATH");
} public static string[] NOTESNOOK_CORS_ORIGINS => ReadSecret("NOTESNOOK_CORS")?.Split(",") ?? [];
public static string? SIGNALR_REDIS_CONNECTION_STRING => ReadSecret("SIGNALR_REDIS_CONNECTION_STRING");
public static string MONOGRAPH_PUBLIC_URL => ReadSecret("MONOGRAPH_PUBLIC_URL") ?? "https://monogr.ph";
public static string? ReadSecret(string name)
{
var value = Environment.GetEnvironmentVariable(name);
if (!string.IsNullOrEmpty(value)) return value;
var file = Environment.GetEnvironmentVariable(name + "_FILE");
if (!string.IsNullOrEmpty(file) && System.IO.File.Exists(file))
{
return System.IO.File.ReadAllText(file);
}
return null;
}
}
}
@@ -1,54 +0,0 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Streetwriters.Common.Converters
{
/// <summary>
/// Converts simple interface into an object (assumes that there is only one class of TInterface)
/// </summary>
/// <typeparam name="TInterface">Interface type</typeparam>
/// <typeparam name="TClass">Class type</typeparam>
public class InterfaceConverter<TInterface, TClass> : JsonConverter<TInterface> where TClass : TInterface
{
public override TInterface Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<TClass>(ref reader, options);
}
public override void Write(Utf8JsonWriter writer, TInterface value, JsonSerializerOptions options)
{
switch (value)
{
case null:
JsonSerializer.Serialize(writer, null, options);
break;
default:
{
var type = value.GetType();
JsonSerializer.Serialize(writer, value, type, options);
break;
}
}
}
}
}
+25 -25
View File
@@ -1,26 +1,26 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Streetwriters.Common.Enums namespace Streetwriters.Common.Enums
{ {
public enum ApplicationType public enum ApplicationType
{ {
NOTESNOOK = 0 NOTESNOOK = 0
} }
} }
+28 -28
View File
@@ -1,29 +1,29 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Streetwriters.Common.Enums namespace Streetwriters.Common.Enums
{ {
public class MFAMethods public class MFAMethods
{ {
public static string Email => "email"; public static string Email => "email";
public static string SMS => "sms"; public static string SMS => "sms";
public static string App => "app"; public static string App => "app";
public static string RecoveryCode => "recoveryCode"; public static string RecoveryCode => "recoveryCode";
} }
} }
@@ -17,15 +17,17 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Runtime.Serialization; using System;
using System.Text.Json.Serialization;
using Streetwriters.Common.Interfaces;
namespace Streetwriters.Common.Models namespace Streetwriters.Common.Enums
{ {
public class SubscriptionResponse : Response public enum SubscriptionPlan
{ {
[JsonPropertyName("subscription")] FREE = 0,
public ISubscription Subscription { get; set; } ESSENTIAL = 1,
PRO = 2,
BELIEVER = 3,
EDUCATION = 4,
LEGACY_PRO = 5
} }
} }
@@ -1,29 +1,30 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Streetwriters.Common.Enums namespace Streetwriters.Common.Enums
{ {
public enum SubscriptionProvider public enum SubscriptionProvider
{ {
STREETWRITERS = 0, STREETWRITERS = 0,
APPLE = 1, APPLE = 1,
GOOGLE = 2, GOOGLE = 2,
PADDLE = 3 PADDLE = 3,
} GIFT_CARD = 4,
} }
}
@@ -17,13 +17,14 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Notesnook.API.Interfaces namespace Streetwriters.Common.Enums
{ {
public interface IEncrypted public enum SubscriptionStatus
{ {
string Cipher { get; set; } ACTIVE,
string IV { get; set; } TRIAL,
long Length { get; set; } CANCELED,
string Salt { get; set; } PAUSED,
EXPIRED
} }
} }
@@ -27,6 +27,6 @@ namespace Streetwriters.Common.Enums
PREMIUM = 5, PREMIUM = 5,
PREMIUM_EXPIRED = 6, PREMIUM_EXPIRED = 6,
PREMIUM_CANCELED = 7, PREMIUM_CANCELED = 7,
PREMIUM_PAUSED = 8 PREMIUM_PAUSED = 8,
} }
} }
@@ -1,86 +1,113 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json; using System.Net;
using Microsoft.AspNetCore.Builder; using System.Text.Json;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Hosting;
using WampSharp.AspNetCore.WebSockets.Server; using Microsoft.AspNetCore.Http;
using WampSharp.Binding; using Microsoft.AspNetCore.HttpOverrides;
using WampSharp.V2; using Microsoft.Extensions.DependencyInjection;
using WampSharp.V2.Realm; using Microsoft.Extensions.Hosting;
using WampSharp.AspNetCore.WebSockets.Server;
namespace Streetwriters.Common.Extensions using WampSharp.Binding;
{ using WampSharp.V2;
public static class AppBuilderExtensions using WampSharp.V2.Realm;
{
public static IApplicationBuilder UseVersion(this IApplicationBuilder app, Server server) namespace Streetwriters.Common.Extensions
{ {
app.Map("/version", (app) => public static class AppBuilderExtensions
{ {
app.Run(async context => public static IApplicationBuilder UseVersion(this IApplicationBuilder app, Server server)
{ {
context.Response.ContentType = "application/json"; app.Map("/version", (app) =>
var data = new Dictionary<string, object> {
{ app.Run(async context =>
{ "version", Constants.COMPATIBILITY_VERSION }, {
{ "id", server.Id }, context.Response.ContentType = "application/json";
{ "instance", Constants.INSTANCE_NAME } var data = new Dictionary<string, object>
}; {
await context.Response.WriteAsync(JsonSerializer.Serialize(data)); { "version", Constants.COMPATIBILITY_VERSION },
}); { "id", server.Id ?? "unknown" },
}); { "instance", Constants.INSTANCE_NAME }
return app; };
} await context.Response.WriteAsync(JsonSerializer.Serialize(data));
});
public static IApplicationBuilder UseWamp<T>(this IApplicationBuilder app, WampServer<T> server, Action<IWampHostedRealm, WampServer<T>> action) where T : new() });
{ return app;
WampHost host = new WampHost(); }
app.Map(server.Endpoint, builder => public static IApplicationBuilder UseWamp(this IApplicationBuilder app, WampServer server, Action<IWampHostedRealm, WampServer> action)
{ {
builder.UseWebSockets(); WampHost host = new();
host.RegisterTransport(new AspNetCoreWebSocketTransport(builder),
new JTokenJsonBinding(), app.Map(server.Endpoint, builder =>
new JTokenMsgpackBinding()); {
}); builder.UseWebSockets();
host.RegisterTransport(new AspNetCoreWebSocketTransport(builder),
host.Open(); new JTokenJsonBinding(),
new JTokenMsgpackBinding());
action.Invoke(host.RealmContainer.GetRealmByName(server.Realm), server); });
return app; host.Open();
}
action.Invoke(host.RealmContainer.GetRealmByName(server.Realm), server);
public static T GetService<T>(this IApplicationBuilder app)
{ return app;
return app.ApplicationServices.GetRequiredService<T>(); }
}
public static T GetService<T>(this IApplicationBuilder app) where T : notnull
public static T GetScopedService<T>(this IApplicationBuilder app) {
{ return app.ApplicationServices.GetRequiredService<T>();
using (var scope = app.ApplicationServices.CreateScope()) }
{
return scope.ServiceProvider.GetRequiredService<T>(); public static T GetScopedService<T>(this IApplicationBuilder app) where T : notnull
} {
} using var scope = app.ApplicationServices.CreateScope();
} return scope.ServiceProvider.GetRequiredService<T>();
}
public static IApplicationBuilder UseForwardedHeadersWithKnownProxies(this IApplicationBuilder app, IWebHostEnvironment env, string forwardedForHeaderName = null)
{
if (!env.IsDevelopment())
{
var forwardedHeadersOptions = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
};
if (!string.IsNullOrEmpty(forwardedForHeaderName))
{
forwardedHeadersOptions.ForwardedForHeaderName = forwardedForHeaderName;
}
foreach (var proxy in Constants.KNOWN_PROXIES)
{
forwardedHeadersOptions.KnownProxies.Add(IPAddress.Parse(proxy));
}
app.UseForwardedHeaders(forwardedHeadersOptions);
}
return app;
}
}
} }
@@ -1,72 +1,76 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Linq; using System;
using System.Net.Http; using System.Linq;
using System.Net.Http.Json; using System.Net.Http;
using System.Threading.Tasks; using System.Net.Http.Json;
using Microsoft.AspNetCore.Http; using System.Threading.Tasks;
using Streetwriters.Common.Interfaces; using Microsoft.AspNetCore.Http;
using Streetwriters.Common.Interfaces;
namespace Streetwriters.Common.Extensions
{ namespace Streetwriters.Common.Extensions
public static class HttpClientExtensions {
{ public static class HttpClientExtensions
public static async Task<T> SendRequestAsync<T>(this HttpClient httpClient, string url, IHeaderDictionary headers, HttpMethod method, HttpContent content = null) where T : IResponse, new() {
{ public static async Task<T> SendRequestAsync<T>(this HttpClient httpClient, string url, IHeaderDictionary? headers, HttpMethod method, HttpContent? content = null) where T : IResponse, new()
var request = new HttpRequestMessage(method, url); {
var request = new HttpRequestMessage(method, url);
if (method != HttpMethod.Get && method != HttpMethod.Delete)
{ if (method != HttpMethod.Get && method != HttpMethod.Delete)
request.Content = content; {
} request.Content = content;
}
foreach (var header in headers)
{ if (headers != null)
if (header.Key == "Content-Type" || header.Key == "Content-Length") {
{ foreach (var header in headers)
if (request.Content != null) {
request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value.AsEnumerable()); if (header.Key == "Content-Type" || header.Key == "Content-Length")
continue; {
} request.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.AsEnumerable());
request.Headers.TryAddWithoutValidation(header.Key, header.Value.AsEnumerable()); continue;
} }
request.Headers.TryAddWithoutValidation(header.Key, header.Value.AsEnumerable());
var response = await httpClient.SendAsync(request); }
if (response.Content.Headers.ContentLength > 0) }
{
var res = await response.Content.ReadFromJsonAsync<T>(); var response = await httpClient.SendAsync(request);
res.Success = response.IsSuccessStatusCode; if (response.Content.Headers.ContentLength > 0 && response.Content.Headers.ContentType?.ToString()?.Contains("application/json") == true)
res.StatusCode = (int)response.StatusCode; {
return res; var res = await response.Content.ReadFromJsonAsync<T>();
} if (res != null)
else {
{ res.Success = response.IsSuccessStatusCode;
return new T { Success = response.IsSuccessStatusCode, StatusCode = (int)response.StatusCode }; res.StatusCode = (int)response.StatusCode;
} return res;
} }
}
public static Task<T> ForwardAsync<T>(this HttpClient httpClient, IHttpContextAccessor accessor, string url, HttpMethod method) where T : IResponse, new()
{ return new T { Success = response.IsSuccessStatusCode, StatusCode = (int)response.StatusCode, Content = response.Content };
var httpContext = accessor.HttpContext; }
var content = new StreamContent(httpContext.Request.BodyReader.AsStream());
return httpClient.SendRequestAsync<T>(url, httpContext.Request.Headers, method, content); public static Task<T> ForwardAsync<T>(this HttpClient httpClient, IHttpContextAccessor accessor, string url, HttpMethod method) where T : IResponse, new()
} {
} var httpContext = accessor.HttpContext ?? throw new InvalidOperationException("HttpContext is not available");
var content = new StreamContent(httpContext.Request.BodyReader.AsStream());
return httpClient.SendRequestAsync<T>(url, httpContext.Request.Headers, method, content);
}
}
} }
@@ -1,53 +1,62 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Streetwriters.Data.DbContexts; using Streetwriters.Common.Accessors;
using Streetwriters.Data.Repositories; using Streetwriters.Data.DbContexts;
using Streetwriters.Data.Repositories;
namespace Streetwriters.Common.Extensions
{ namespace Streetwriters.Common.Extensions
public static class ServiceCollectionServiceExtensions {
{ public static class ServiceCollectionServiceExtensions
public static IServiceCollection AddRepository<T>(this IServiceCollection services, string collectionName, string database) where T : class {
{ public static IServiceCollection AddWampServiceAccessor(this IServiceCollection services, Server server)
services.AddSingleton((provider) => MongoDbContext.GetMongoCollection<T>(provider.GetService<MongoDB.Driver.IMongoClient>(), database, collectionName)); {
services.AddScoped<Repository<T>>(); services.AddSingleton<WampServiceAccessor>((provider) => new WampServiceAccessor(server));
return services; services.AddHostedService(provider => provider.GetRequiredService<WampServiceAccessor>());
} return services;
}
public static IServiceCollection AddDefaultCors(this IServiceCollection services)
{ public static IServiceCollection AddRepository<T>(this IServiceCollection services, string collectionName, string database) where T : class
services.AddCors(options => {
{ services.AddSingleton((provider) => MongoDbContext.GetMongoCollection<T>(provider.GetRequiredService<MongoDB.Driver.IMongoClient>(), database, collectionName));
options.AddPolicy("notesnook", (b) => services.AddScoped<Repository<T>>();
{ return services;
if (Constants.NOTESNOOK_CORS_ORIGINS.Length <= 0) }
b.AllowAnyOrigin();
else public static IServiceCollection AddDefaultCors(this IServiceCollection services)
b.WithOrigins(Constants.NOTESNOOK_CORS_ORIGINS); {
services.AddCors(options =>
b.AllowAnyMethod() {
.AllowAnyHeader(); options.AddPolicy("notesnook", (b) =>
}); {
}); if (Constants.NOTESNOOK_CORS_ORIGINS.Length <= 0)
return services; b.AllowAnyOrigin();
} else
} b.WithOrigins(Constants.NOTESNOOK_CORS_ORIGINS);
}
b.AllowAnyMethod()
.AllowAnyHeader()
.WithExposedHeaders(["X-Object-Size", "Content-Length"]);
});
});
return services;
}
}
}
@@ -1,87 +1,87 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
namespace System namespace System
{ {
public static class StringExtensions public static class StringExtensions
{ {
public static string Sha256(this string input) public static string Sha256(this string input)
{ {
var bytes = Encoding.UTF8.GetBytes(input); var bytes = Encoding.UTF8.GetBytes(input);
var hash = SHA256.HashData(bytes); var hash = SHA256.HashData(bytes);
return Convert.ToBase64String(hash); return Convert.ToBase64String(hash);
} }
public static byte[] CompressBrotli(this string input) public static byte[] CompressBrotli(this string input)
{ {
var raw = Encoding.Default.GetBytes(input); var raw = Encoding.Default.GetBytes(input);
using (MemoryStream memory = new MemoryStream()) using (MemoryStream memory = new MemoryStream())
{ {
using (BrotliStream brotli = new BrotliStream(memory, CompressionLevel.Optimal)) using (BrotliStream brotli = new BrotliStream(memory, CompressionLevel.Optimal))
{ {
brotli.Write(raw, 0, raw.Length); brotli.Write(raw, 0, raw.Length);
} }
return memory.ToArray(); return memory.ToArray();
} }
} }
public static string DecompressBrotli(this byte[] compressed) public static string DecompressBrotli(this byte[] compressed)
{ {
using (BrotliStream stream = new BrotliStream(new MemoryStream(compressed), CompressionMode.Decompress)) using (BrotliStream stream = new BrotliStream(new MemoryStream(compressed), CompressionMode.Decompress))
{ {
const int size = 4096; const int size = 4096;
byte[] buffer = new byte[size]; byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream()) using (MemoryStream memory = new MemoryStream())
{ {
int count = 0; int count = 0;
do do
{ {
count = stream.Read(buffer, 0, size); count = stream.Read(buffer, 0, size);
if (count > 0) if (count > 0)
{ {
memory.Write(buffer, 0, count); memory.Write(buffer, 0, count);
} }
} }
while (count > 0); while (count > 0);
return Encoding.Default.GetString(memory.ToArray()); return Encoding.Default.GetString(memory.ToArray());
} }
} }
} }
private static string ToHex(byte[] bytes, int startIndex, int length) private static string ToHex(byte[] bytes, int startIndex, int length)
{ {
char[] c = new char[length * 2]; char[] c = new char[length * 2];
byte b; byte b;
for (int bx = startIndex, cx = startIndex; bx < length; ++bx, ++cx) for (int bx = startIndex, cx = startIndex; bx < length; ++bx, ++cx)
{ {
b = ((byte)(bytes[bx] >> 4)); b = ((byte)(bytes[bx] >> 4));
c[cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30); c[cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30);
b = ((byte)(bytes[bx] & 0x0F)); b = ((byte)(bytes[bx] & 0x0F));
c[++cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30); c[++cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30);
} }
return new string(c); return new string(c);
} }
} }
} }
@@ -1,42 +1,66 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using Microsoft.AspNetCore.Builder; using System.Reactive.Disposables;
using Streetwriters.Common.Interfaces; using System.Threading;
using WampSharp.AspNetCore.WebSockets.Server; using Microsoft.AspNetCore.Builder;
using WampSharp.Binding; using Streetwriters.Common.Interfaces;
using WampSharp.V2; using WampSharp.AspNetCore.WebSockets.Server;
using WampSharp.V2.Realm; using WampSharp.Binding;
using WampSharp.V2;
namespace Streetwriters.Common.Extensions using WampSharp.V2.Realm;
{
public static class WampRealmExtensions namespace Streetwriters.Common.Extensions
{ {
public static IDisposable Subscribe<T>(this IWampHostedRealm realm, string topicName, Action<T> onNext) public static class WampRealmExtensions
{ {
return realm.Services.GetSubject<T>(topicName).Subscribe<T>(onNext); public static IDisposable Subscribe<T>(this IWampHostedRealm realm, string topicName, Action<T> onNext)
} {
return realm.Services.GetSubject<T>(topicName).Subscribe<T>(onNext);
public static IDisposable Subscribe<T>(this IWampHostedRealm realm, string topicName, IMessageHandler<T> handler) }
{
return realm.Services.GetSubject<T>(topicName).Subscribe<T>(async (message) => await handler.Process(message)); public static IDisposable Subscribe<T>(this IWampHostedRealm realm, string topicName, IMessageHandler<T> handler)
} {
} return realm.Services.GetSubject<T>(topicName).Subscribe<T>(async (message) => await handler.Process(message));
}
public static IDisposable SubscribeWithSemaphore<T>(this IWampHostedRealm realm, string topicName, IMessageHandler<T> handler)
{
var semaphore = new SemaphoreSlim(1, 1);
var subscriber = realm.Services.GetSubject<T>(topicName).Subscribe<T>(async (message) =>
{
await semaphore.WaitAsync();
try
{
await handler.Process(message);
}
finally
{
semaphore.Release();
}
});
return Disposable.Create(() =>
{
subscriber.Dispose();
semaphore.Dispose();
});
}
}
} }
@@ -0,0 +1,62 @@
using System.IO;
using System.Security.Claims;
using System.Threading.Tasks;
using Streetwriters.Common.Enums;
using Streetwriters.Common.Interfaces;
using WebMarkupMin.Core;
using WebMarkupMin.Core.Loggers;
namespace Streetwriters.Common.Helpers
{
public enum Features
{
SMS_2FA,
MONOGRAPH_ANALYTICS
}
public static class FeatureAuthorizationHelper
{
private static SubscriptionPlan? GetUserSubscriptionPlan(string clientId, ClaimsPrincipal user)
{
var claimKey = $"{clientId}:status";
var status = user.FindFirstValue(claimKey);
switch (status)
{
case "free":
return SubscriptionPlan.FREE;
case "believer":
return SubscriptionPlan.BELIEVER;
case "education":
return SubscriptionPlan.EDUCATION;
case "essential":
return SubscriptionPlan.ESSENTIAL;
case "pro":
return SubscriptionPlan.PRO;
case "legacy_pro":
return SubscriptionPlan.LEGACY_PRO;
default:
return null;
}
}
public static bool IsFeatureAllowed(Features feature, string clientId, ClaimsPrincipal user)
{
if (Constants.IS_SELF_HOSTED)
return true;
var status = GetUserSubscriptionPlan(clientId, user);
switch (feature)
{
case Features.SMS_2FA:
case Features.MONOGRAPH_ANALYTICS:
return status == SubscriptionPlan.LEGACY_PRO ||
status == SubscriptionPlan.PRO ||
status == SubscriptionPlan.EDUCATION ||
status == SubscriptionPlan.BELIEVER;
default:
return false;
}
}
}
}
@@ -0,0 +1,23 @@
using System.IO;
using WebMarkupMin.Core;
using WebMarkupMin.Core.Loggers;
namespace Streetwriters.Common.Helpers
{
public static class HtmlHelper
{
public static string ReadMinifiedHtmlFile(string path)
{
var settings = new HtmlMinificationSettings()
{
WhitespaceMinificationMode = WhitespaceMinificationMode.Medium,
};
var cssMinifier = new KristensenCssMinifier();
var jsMinifier = new CrockfordJsMinifier();
var minifier = new HtmlMinifier(settings, cssMinifier, jsMinifier, new NullLogger());
return minifier.Minify(File.ReadAllText(path), false).MinifiedContent;
}
}
}
+24 -3
View File
@@ -17,6 +17,7 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Collections.Generic;
using System.Reactive.Subjects; using System.Reactive.Subjects;
using System.Threading.Tasks; using System.Threading.Tasks;
using Streetwriters.Common.Messages; using Streetwriters.Common.Messages;
@@ -27,15 +28,28 @@ namespace Streetwriters.Common.Helpers
{ {
public class WampHelper public class WampHelper
{ {
public static async Task<IWampRealmProxy> OpenWampChannelAsync(string server, string realmName) public static async Task<IWampChannel> OpenWampChannelAsync(string server, string realmName)
{ {
DefaultWampChannelFactory channelFactory = new(); DefaultWampChannelFactory channelFactory = new();
IWampChannel channel = channelFactory.CreateJsonChannel(server, realmName); IWampChannel channel = channelFactory.CreateJsonChannel(server, realmName);
await channel.Open(); var isConnected = false;
while (!isConnected)
{
try
{
await channel.Open();
isConnected = true;
}
catch
{
await Task.Delay(5000);
continue;
}
}
return channel.RealmProxy; return channel;
} }
public static void PublishMessage<T>(IWampRealmProxy realm, string topicName, T message) public static void PublishMessage<T>(IWampRealmProxy realm, string topicName, T message)
@@ -43,5 +57,12 @@ namespace Streetwriters.Common.Helpers
var subject = realm.Services.GetSubject<T>(topicName); var subject = realm.Services.GetSubject<T>(topicName);
subject.OnNext(message); subject.OnNext(message);
} }
public static void PublishMessages<T>(IWampRealmProxy realm, string topicName, IEnumerable<T> messages)
{
var subject = realm.Services.GetSubject<T>(topicName);
foreach (var message in messages)
subject.OnNext(message);
}
} }
} }
+38 -38
View File
@@ -1,38 +1,38 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Streetwriters.Common.Enums; using Streetwriters.Common.Enums;
namespace Streetwriters.Common.Interfaces namespace Streetwriters.Common.Interfaces
{ {
public interface IClient public interface IClient
{ {
string Id { get; set; } string Id { get; set; }
string Name { get; set; } string Name { get; set; }
ApplicationType Type { get; set; } ApplicationType Type { get; set; }
ApplicationType AppId { get; set; } ApplicationType AppId { get; set; }
string SenderEmail { get; set; } string SenderEmail { get; set; }
string SenderName { get; set; } string SenderName { get; set; }
string EmailConfirmedRedirectURL { get; } string EmailConfirmedRedirectURL { get; }
string AccountRecoveryRedirectURL { get; } string AccountRecoveryRedirectURL { get; }
Func<string, Task> OnEmailConfirmed { get; set; } Func<string, Task>? OnEmailConfirmed { get; set; }
} }
} }
+31 -29
View File
@@ -1,29 +1,31 @@
/* /*
This file is part of the Notesnook Sync Server project (https://notesnook.com/) This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details. Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace Streetwriters.Common.Interfaces using MongoDB.Bson;
{
public interface IDocument namespace Streetwriters.Common.Interfaces
{ {
string Id public interface IDocument
{ {
get; set; ObjectId Id
} {
} get; set;
} }
}
}
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Net.Mail;
using System.Threading.Tasks;
using MimeKit;
using MimeKit.Cryptography;
using Streetwriters.Common.Models;
namespace Streetwriters.Common.Interfaces
{
public interface IEmailSender
{
Task SendEmailAsync(
string email,
EmailTemplate template,
MailAddress from,
GnuPGContext? gpgContext = null,
Dictionary<string, byte[]>? attachments = null
);
}
}

Some files were not shown because too many files have changed in this diff Show More