Skip to content

Commit

Permalink
feat: Use local search only for persistent sessions
Browse files Browse the repository at this point in the history
We now rely on the checkbox on login to determine whether or not the
local search should be used. If the user enable this checkbox, it means
he trusts this device and accept to store local data on it.
Otherwise, we do not store any private data.

See cozy/cozy-libs#2611 for the search part.
  • Loading branch information
paultranvan committed Nov 7, 2024
1 parent 8dd23b3 commit 8ff0946
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"comlink": "^4.4.1",
"cozy-app-publish": "^0.34.0",
"cozy-client": "^50.3.1",
"cozy-client": "^51.0.0",
"cozy-dataproxy-lib": "^1.1.1",
"cozy-device-helper": "^3.1.0",
"cozy-flags": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/@types/cozy-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ declare module 'cozy-client' {
capabilities: ClientCapabilities
registerPlugin: (Plugin: Function, options: unknown) => void
getCollectionFromState: (doctype: string) => unknown
setLinks: (links: unknown[]) => void
}

export const createMockClient = (options?: ClientOptions): CozyClient =>
Expand Down
15 changes: 12 additions & 3 deletions src/dataproxy/worker/shared-worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Comlink from 'comlink'

import CozyClient from 'cozy-client'
import CozyClient, { StackLink } from 'cozy-client'
import { SearchEngine } from 'cozy-dataproxy-lib'
import Minilog from 'cozy-minilog'
import PouchLink from 'cozy-pouch-link'
Expand All @@ -18,6 +18,7 @@ import {
DataProxyWorkerPartialState
} from '@/dataproxy/common/DataProxyInterface'
import { platformWorker } from '@/dataproxy/worker/platformWorker'
import { queryIsTrustedDevice } from '@/dataproxy/worker/utils'
import schema from '@/doctypes'
import { getPouchLink } from '@/helpers/client'

Expand Down Expand Up @@ -63,9 +64,17 @@ const dataProxy: DataProxyWorker = {
version: '1'
},
schema,
store: true,
links: [new PouchLink(pouchLinkOptions)]
store: true
})

// If the device is not trusted, we do not want to store any private data in Pouch
// So use the PouchLink only if the user declared a trustful device for the given session
const isTrustedDevice = await queryIsTrustedDevice(client)
const link = isTrustedDevice
? new PouchLink(pouchLinkOptions)
: new StackLink()

client.setLinks([link])
client.instanceOptions = clientData.instanceOptions
client.capabilities = clientData.capabilities

Expand Down
23 changes: 23 additions & 0 deletions src/dataproxy/worker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import CozyClient from 'cozy-client'
import Minilog from 'cozy-minilog'

import { LOCALSTORAGE_KEY_DELETING_DATA } from '@/consts'
const log = Minilog('👷‍♂️ [Worker utils]')

interface SessionInfo {
last_seen: string
long_run: boolean
}

interface SessionResponse {
data: {
attributes: SessionInfo
}
}

export const queryIsTrustedDevice = async (
client: CozyClient
): Promise<boolean> => {
const resp: SessionResponse = await client
.getStackClient()
.fetchJSON('GET', '/settings/sessions/current')

const isLongRun = resp?.data?.attributes?.long_run
return !!isLongRun
}

const deleteDatabases = async (): Promise<void> => {
const databases = await window.indexedDB.databases()
// Remove all indexedDB databases
Expand Down
36 changes: 35 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2248,7 +2248,7 @@ cozy-app-publish@^0.34.0:
tar "^6.1.11"
verror "^1.10.1"

cozy-client@^50.3.1, cozy-client@^50.4.0:
cozy-client@^50.4.0:
version "50.4.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-50.4.0.tgz#dc341e71b2acee90342cf86e17392e890e7e7093"
integrity sha512-DINi/YwDrzBkXzcm63hFj8Y09yzzLaRWhJqyCu1JT6bdIpV2jc9lJMJe1pcud8xfg29OzSQ5/8gDf5MaQLkzsQ==
Expand All @@ -2273,6 +2273,31 @@ cozy-client@^50.3.1, cozy-client@^50.4.0:
sift "^6.0.0"
url-search-params-polyfill "^8.0.0"

cozy-client@^51.0.0:
version "51.0.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-51.0.0.tgz#84b7f9c6a3c678791de7b06539af6595e0bb51c2"
integrity sha512-txbEkQFuZX1ZXi1/2bXLJK9SQR+sbZjjztIv3pt+g73veuVVz5wyDoJkMbLLf5dSiHARpHIWgIdBfX4prZwpWQ==
dependencies:
"@cozy/minilog" "1.0.0"
"@types/jest" "^26.0.20"
"@types/lodash" "^4.14.170"
btoa "^1.2.1"
cozy-stack-client "^51.0.0"
date-fns "2.29.3"
json-stable-stringify "^1.0.1"
lodash "^4.17.13"
microee "^0.0.6"
node-fetch "^2.6.1"
node-polyglot "2.4.2"
open "7.4.2"
prop-types "^15.6.2"
react-redux "^7.2.0"
redux "3 || 4"
redux-thunk "^2.3.0"
server-destroy "^1.0.1"
sift "^6.0.0"
url-search-params-polyfill "^8.0.0"

cozy-dataproxy-lib@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/cozy-dataproxy-lib/-/cozy-dataproxy-lib-1.1.1.tgz#2a70f62ef92cff6fea156408db3e2ed48ada2918"
Expand Down Expand Up @@ -2336,6 +2361,15 @@ cozy-stack-client@^50.4.0:
mime "^2.4.0"
qs "^6.7.0"

cozy-stack-client@^51.0.0:
version "51.0.0"
resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-51.0.0.tgz#73fbdb1cf8efc46cb89ad2266d04e1289a9ae355"
integrity sha512-ToaheKT0cziulvAxUl+H8mqmSXQmblCp6a5TKNnrEHOS3ExTmzOHmIgNrRDgDKi4G8hK93CZTi8gj49ffk0HYw==
dependencies:
detect-node "^2.0.4"
mime "^2.4.0"
qs "^6.7.0"

cozy-tsconfig@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/cozy-tsconfig/-/cozy-tsconfig-1.2.0.tgz#17e61f960f139fae4d26cbac2254b9ab632b269e"
Expand Down

0 comments on commit 8ff0946

Please sign in to comment.