Skip to content

Commit

Permalink
Implement new user endpoint on client and companion
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Jun 22, 2023
1 parent 3199237 commit bc5f55f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/@uppy/companion-client/src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export default class Provider extends RequestClient {
return this.get(`${this.id}/list/${directory || ''}`, options)
}

user (options) {
return this.get(`${this.id}/user`, options)
}

logout () {
return this.get(`${this.id}/logout`)
.then((response) => Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/companion/src/companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ module.exports.app = (optionsArg = {}) => {
app.get('/:providerName/logout', middlewares.hasSessionAndProvider, middlewares.hasOAuthProvider, middlewares.gentleVerifyToken, controllers.logout)
app.get('/:providerName/send-token', middlewares.hasSessionAndProvider, middlewares.hasOAuthProvider, middlewares.verifyToken, controllers.sendToken)

app.get('/:providerName/user/:id?', middlewares.hasSessionAndProvider, middlewares.verifyToken, controllers.user)

app.get('/:providerName/list/:id?', middlewares.hasSessionAndProvider, middlewares.verifyToken, controllers.list)
// backwards compat:
app.get('/search/:providerName/list', middlewares.hasSessionAndProvider, middlewares.verifyToken, controllers.list)
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/companion/src/server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
get: require('./get'),
thumbnail: require('./thumbnail'),
list: require('./list'),
user: require('./user'),
logout: require('./logout'),
connect: require('./connect'),
preauth: require('./preauth'),
Expand Down
19 changes: 19 additions & 0 deletions packages/@uppy/companion/src/server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { errorToResponse } = require('../provider/error')

async function user ({ query, params, companion }, res, next) {
const token = companion.providerToken

try {
const data = await companion.provider.user({ companion, token, directory: params.id, query })
res.json(data)
} catch (err) {
const errResp = errorToResponse(err)
if (errResp) {
res.status(errResp.code).json({ message: errResp.message })
return
}
next(err)
}
}

module.exports = user
5 changes: 2 additions & 3 deletions packages/@uppy/companion/src/server/provider/drive/adapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const querystring = require('node:querystring')

const getUsername = (data) => {
exports.getUsername = (data) => {
return data.user.emailAddress
}

Expand Down Expand Up @@ -140,7 +140,7 @@ const getVideoDurationMillis = (item) => item.videoMediaMetadata && item.videoMe
// Hopefully this name will not be used by Google
exports.VIRTUAL_SHARED_DIR = 'shared-with-me'

exports.adaptData = (listFilesResp, sharedDrivesResp, directory, query, showSharedWithMe, about) => {
exports.adaptData = (listFilesResp, sharedDrivesResp, directory, query, showSharedWithMe) => {
const adaptItem = (item) => ({
isFolder: isFolder(item),
icon: getItemIcon(item),
Expand Down Expand Up @@ -183,7 +183,6 @@ exports.adaptData = (listFilesResp, sharedDrivesResp, directory, query, showShar
]

return {
username: getUsername(about),
items: adaptedItems,
nextPagePath: getNextPagePath(listFilesResp, query, directory),
}
Expand Down
30 changes: 21 additions & 9 deletions packages/@uppy/companion/src/server/provider/drive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ const got = require('got').default

const Provider = require('../Provider')
const logger = require('../../logger')
const { VIRTUAL_SHARED_DIR, adaptData, isShortcut, isGsuiteFile, getGsuiteExportType } = require('./adapter')
const {
VIRTUAL_SHARED_DIR,
adaptData,
getUsername,
isShortcut,
isGsuiteFile,
getGsuiteExportType,
} = require('./adapter')
const { withProviderErrorHandling } = require('../providerErrors')
const { prepareStream } = require('../../helpers/utils')

Expand Down Expand Up @@ -45,6 +52,18 @@ class Drive extends Provider {
return 'google'
}

async user (options) {
return this.#withErrorHandling('provider.drive.user.error', async () => {
const { token } = options
const searchParams = { fields: 'user' }
const client = getClient({ token })

const res = await client.get('about', { searchParams, responseType: 'json' }).json()

return getUsername(res)
})
}

async list (options) {
return this.#withErrorHandling('provider.drive.list.error', async () => {
const directory = options.directory || 'root'
Expand Down Expand Up @@ -91,21 +110,14 @@ class Drive extends Provider {
return client.get('files', { searchParams, responseType: 'json' }).json()
}

async function fetchAbout () {
const searchParams = { fields: 'user' }

return client.get('about', { searchParams, responseType: 'json' }).json()
}

const [sharedDrives, filesResponse, about] = await Promise.all([fetchSharedDrives(), fetchFiles(), fetchAbout()])
const [sharedDrives, filesResponse] = await Promise.all([fetchSharedDrives(), fetchFiles()])

return adaptData(
filesResponse,
sharedDrives,
directory,
query,
isRoot && !query.cursor, // we can only show it on the first page request, or else we will have duplicates of it
about,
)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export default class ProviderView extends View {
const files = []
let path = id

if (!this.username) {
this.username = await this.provider.user()
}

while (path) {
const res = await this.provider.list(path, { signal: controller.signal })

Expand All @@ -135,7 +139,6 @@ export default class ProviderView extends View {
}

path = res.nextPagePath
this.username ??= res.username
this.setLoading(this.plugin.uppy.i18n('addedNumFiles', { numFiles: files.length + folders.length }))
}

Expand Down Expand Up @@ -179,6 +182,7 @@ export default class ProviderView extends View {

const newState = {
authenticated: false,
username: null,
files: [],
folders: [],
directories: [],
Expand Down

0 comments on commit bc5f55f

Please sign in to comment.