2022-11-16 18:24:13 +01:00
|
|
|
import {
|
|
|
|
APP_DEV_PREFIX,
|
|
|
|
DocumentType,
|
|
|
|
getGlobalIDFromUserMetadataID,
|
|
|
|
} from "../db/utils"
|
|
|
|
import {
|
2021-05-21 14:07:10 +02:00
|
|
|
doesUserHaveLock,
|
|
|
|
updateLock,
|
|
|
|
checkDebounce,
|
|
|
|
setDebounce,
|
2022-11-16 18:24:13 +01:00
|
|
|
} from "../utilities/redis"
|
2023-05-30 18:41:20 +02:00
|
|
|
import { db as dbCore, cache } from "@budibase/backend-core"
|
|
|
|
import { UserCtx, Database } from "@budibase/types"
|
2021-05-21 14:07:10 +02:00
|
|
|
|
2021-05-21 16:14:35 +02:00
|
|
|
const DEBOUNCE_TIME_SEC = 30
|
2021-05-21 14:07:10 +02:00
|
|
|
|
|
|
|
/************************************************** *
|
|
|
|
* This middleware has been broken out of the *
|
|
|
|
* "authorized" middleware as it had nothing to do *
|
|
|
|
* with authorization, but requires the perms *
|
|
|
|
* imparted by it. This middleware shouldn't *
|
|
|
|
* be called directly, it should always be called *
|
|
|
|
* through the authorized middleware *
|
|
|
|
****************************************************/
|
|
|
|
|
2023-05-30 18:41:20 +02:00
|
|
|
async function checkDevAppLocks(ctx: UserCtx) {
|
2021-05-21 14:07:10 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
|
|
|
|
// if any public usage, don't proceed
|
2022-11-16 18:24:13 +01:00
|
|
|
if (!ctx.user?._id && !ctx.user?.userId) {
|
2021-05-21 14:07:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// not a development app, don't need to do anything
|
|
|
|
if (!appId || !appId.startsWith(APP_DEV_PREFIX)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:55:08 +02:00
|
|
|
// If this user already owns the lock, then update it
|
|
|
|
if (await doesUserHaveLock(appId, ctx.user)) {
|
|
|
|
await updateLock(appId, ctx.user)
|
|
|
|
}
|
2021-05-21 14:07:10 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:41:20 +02:00
|
|
|
async function updateAppUpdatedAt(ctx: UserCtx) {
|
2021-05-21 14:07:10 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
// if debouncing skip this update
|
|
|
|
// get methods also aren't updating
|
2021-05-21 16:03:28 +02:00
|
|
|
if (ctx.method === "GET" || (await checkDebounce(appId))) {
|
2021-05-21 14:07:10 +02:00
|
|
|
return
|
|
|
|
}
|
2022-11-17 15:35:03 +01:00
|
|
|
await dbCore.doWithDB(appId, async (db: Database) => {
|
2023-05-30 18:41:20 +02:00
|
|
|
try {
|
|
|
|
const metadata = await db.get(DocumentType.APP_METADATA)
|
|
|
|
metadata.updatedAt = new Date().toISOString()
|
2022-05-05 13:52:17 +02:00
|
|
|
|
2023-05-30 18:41:20 +02:00
|
|
|
metadata.updatedBy = getGlobalIDFromUserMetadataID(ctx.user?.userId!)
|
2022-05-05 13:52:17 +02:00
|
|
|
|
2023-05-30 18:41:20 +02:00
|
|
|
const response = await db.put(metadata)
|
|
|
|
metadata._rev = response.rev
|
|
|
|
await cache.app.invalidateAppMetadata(appId, metadata)
|
|
|
|
// set a new debounce record with a short TTL
|
|
|
|
await setDebounce(appId, DEBOUNCE_TIME_SEC)
|
|
|
|
} catch (err: any) {
|
|
|
|
// if a 409 occurs, then multiple clients connected at the same time - ignore
|
|
|
|
if (err?.status === 409) {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
})
|
2021-05-21 14:07:10 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:41:20 +02:00
|
|
|
export default async function builder(ctx: UserCtx) {
|
2021-05-21 14:07:10 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
// this only functions within an app context
|
|
|
|
if (!appId) {
|
|
|
|
return
|
|
|
|
}
|
2023-02-22 14:39:31 +01:00
|
|
|
|
|
|
|
// check authenticated
|
|
|
|
if (!ctx.isAuthenticated) {
|
|
|
|
return ctx.throw(403, "Session not authenticated")
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:07:10 +02:00
|
|
|
const referer = ctx.headers["referer"]
|
2022-05-05 13:52:17 +02:00
|
|
|
|
|
|
|
const hasAppId = !referer ? false : referer.includes(appId)
|
|
|
|
const editingApp = referer ? hasAppId : false
|
2021-05-21 14:07:10 +02:00
|
|
|
// check this is a builder call and editing
|
2023-02-22 14:39:31 +01:00
|
|
|
if (!editingApp) {
|
2021-05-21 14:07:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// check locks
|
|
|
|
await checkDevAppLocks(ctx)
|
|
|
|
// set updated at time on app
|
|
|
|
await updateAppUpdatedAt(ctx)
|
2021-05-21 15:49:59 +02:00
|
|
|
}
|