Explorer

definy.accept-upload-content

description
(en) UploadContent of definy.handle-event-service: stores the items with definy.upload-content only when every content hash is allowed by an accepted SubmitEvent (definy.accept-submit-event), so that others cannot fill the store with contents no event uses. The allowances of the items are removed
type
Fn(request: @definy.json) -> @definy.service-io
expression
fn(request$0) =>
  let hashes$1 = #text<@definy.sql-value>(@definy.json-to-string(
      value: #array<@definy.json>(@definy.json-string-array(
        items: @core.list-map(
          T: @definy.json,
          U: String,
          f: fn(item$2) =>
            match @definy.json-get-string(value: $2, key: "contentHash") {
              #some(hash$3) =>
                $3,
              #none(_) =>
                ""
            },
          xs: @definy.json-get-array(value: $0, key: "items")
        ),
        index: 0
      ))
    ));
  #sql<@definy.service-io>({
    statements: [
      {sql: "CREATE TABLE IF NOT EXISTS upload_allowances (content_hash TEXT PRIMARY KEY)", params: []},
      {
        sql: "SELECT value AS content_hash FROM json_each(?) WHERE value NOT IN (SELECT content_hash FROM upload_allowances)",
        params: [$1]
      }
    ],
    then: fn(results$4) =>
      match @core.list-get(list: @definy.sql-rows-concat(results: $4, index: 0), index: 0) {
        #some(_) =>
          @definy.rpc-error(
            code: "permission_denied",
            message: "This server stores only the contents that a SubmitEvent of an accepted account is missing (send SubmitEvent first)"
          ),
        #none(_) =>
          #sql<@definy.service-io>({
            statements: [
              {
                sql: "DELETE FROM upload_allowances WHERE content_hash IN (SELECT value FROM json_each(?))",
                params: [$1]
              }
            ],
            then: fn(results$5) =>
              @definy.upload-content(request: $0)
          })
      }
  })
visibility
public
definy.accept-upload-content (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: eVQ_S5pOtv_XaJbZur4bffiukuZFse_PuF04SBGUKR8
content hash: lmCNbas5ZTI5I_J4fJ1whUl8J5sz2p6IkY1i_U7pzgw