zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit 5872b4893a2fa69c1b94364f4cebc49d960fd337
parent e1d95889bba6399d79acdd5ca69ce2d4abc8e5f1
Author: srfsh <dev@srf.sh>
Date:   Wed,  3 Aug 2022 10:32:15 +0300

Zenflows.VF: support image fields

Diffstat:
Msrc/zenflows/vf/agent.ex | 2+-
Msrc/zenflows/vf/agent/resolv.ex | 11++---------
Msrc/zenflows/vf/agent/type.ex | 6+++---
Msrc/zenflows/vf/economic_resource.ex | 4++--
Msrc/zenflows/vf/economic_resource/type.ex | 8++++----
Msrc/zenflows/vf/intent.ex | 4++--
Msrc/zenflows/vf/organization.ex | 4++--
Msrc/zenflows/vf/organization/type.ex | 10+++++-----
Msrc/zenflows/vf/person.ex | 4++--
Msrc/zenflows/vf/person/domain.ex | 1-
Msrc/zenflows/vf/person/type.ex | 10+++++-----
Msrc/zenflows/vf/recipe_resource.ex | 4++--
Msrc/zenflows/vf/recipe_resource/type.ex | 8++++----
Msrc/zenflows/vf/resource_specification.ex | 4++--
Msrc/zenflows/vf/resource_specification/type.ex | 9+++++----
Msrc/zenflows/vf/validate.ex | 23+++++++++++++++++++++++
16 files changed, 64 insertions(+), 48 deletions(-)

diff --git a/src/zenflows/vf/agent.ex b/src/zenflows/vf/agent.ex @@ -50,7 +50,7 @@ schema "vf_agent" do field :type, Ecto.Enum, values: [:per, :org] field :name, :string field :note, :string - field :image, :string, virtual: true + field :image, :string belongs_to :primary_location, SpatialThing # person diff --git a/src/zenflows/vf/agent/resolv.ex b/src/zenflows/vf/agent/resolv.ex @@ -20,15 +20,8 @@ defmodule Zenflows.VF.Agent.Resolv do alias Zenflows.VF.{Agent, Agent.Domain} -def my_agent(_args, _info) do - agent = %Agent{ - type: :per, - id: Zenflows.DB.ID.gen(), - name: "hello", - image: "https://example.test/img.jpg", - note: "world", - } - {:ok, agent} +def my_agent(_args, %{context: %{req_user: user}}) do + {:ok, user} end def agent(%{id: id}, _info) do diff --git a/src/zenflows/vf/agent/type.ex b/src/zenflows/vf/agent/type.ex @@ -26,8 +26,8 @@ An informal or formal textual identifier for an agent. Does not imply uniqueness. """ @image """ -The URI to an image relevant to the agent, such as a logo, avatar, -photo, etc. +The base64-encoded image binary relevant to the agent, such as a logo, +avatar, photo, etc. """ @note "A textual description or comment." @primary_location """ @@ -45,7 +45,7 @@ interface :agent do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string diff --git a/src/zenflows/vf/economic_resource.ex b/src/zenflows/vf/economic_resource.ex @@ -60,7 +60,7 @@ alias Zenflows.VF.{ schema "vf_economic_resource" do field :name, :string field :note, :string - field :image, :string, virtual: true + field :image, :string field :tracking_identifier, :string field :classified_as, {:array, :string} belongs_to :conforms_to, ResourceSpecification @@ -103,7 +103,7 @@ def chgset(schema \\ %__MODULE__{}, params) do |> Changeset.validate_required(@reqr) |> Validate.name(:name) |> Validate.note(:note) - |> Validate.uri(:image) + |> Validate.img(:image) |> Validate.class(:classified_as) |> require_quantity_units_same() |> Changeset.assoc_constraint(:conforms_to) diff --git a/src/zenflows/vf/economic_resource/type.ex b/src/zenflows/vf/economic_resource/type.ex @@ -28,7 +28,7 @@ uniqueness. """ @note "A textual description or comment." @image """ -The URI to an image relevant to the entity, such as a photo, diagram, etc. +The base64-encoded image binary relevant to the entity, such as a photo, diagram, etc. """ @tracking_identifier """ Sometimes called serial number, used when each item must have a traceable @@ -106,7 +106,7 @@ object :economic_resource do field :note, :string @desc @image - field :image, :uri + field :image, :base64 @desc @tracking_identifier field :tracking_identifier, :string @@ -167,7 +167,7 @@ input_object :economic_resource_create_params do field :note, :string @desc @image - field :image, :uri + field :image, :base64 @desc @tracking_identifier field :tracking_identifier, :string @@ -183,7 +183,7 @@ input_object :economic_resource_update_params do field :note, :string @desc @image - field :image, :uri + field :image, :base64 end object :query_economic_resource do diff --git a/src/zenflows/vf/intent.ex b/src/zenflows/vf/intent.ex @@ -85,7 +85,7 @@ schema "vf_intent" do field :has_point_in_time, :utc_datetime_usec field :due, :utc_datetime_usec field :finished, :boolean, default: false - field :image, :string, virtual: true + field :image, :string field :note, :string # field :in_scope_of field :agreed_in, :string @@ -109,7 +109,7 @@ def chgset(schema \\ %__MODULE__{}, params) do |> mutex_check() |> Validate.name(:name) |> Validate.note(:note) - |> Validate.uri(:image) + |> Validate.img(:image) |> Validate.class(:resource_classified_as) |> Measure.cast(:resource_quantity) |> Measure.cast(:effort_quantity) diff --git a/src/zenflows/vf/organization.ex b/src/zenflows/vf/organization.ex @@ -34,7 +34,7 @@ alias Zenflows.VF.{SpatialThing, Validate} schema "vf_agent" do field :type, Ecto.Enum, values: [:org], default: :org field :name, :string - field :image, :string, virtual: true + field :image, :string field :note, :string belongs_to :primary_location, SpatialThing field :classified_as, {:array, :string} @@ -51,7 +51,7 @@ def chgset(schema \\ %__MODULE__{}, params) do |> Changeset.validate_required(@reqr) |> Validate.name(:name) |> Validate.note(:note) - |> Validate.uri(:image) + |> Validate.img(:image) |> Validate.class(:classified_as) |> Changeset.assoc_constraint(:primary_location) end diff --git a/src/zenflows/vf/organization/type.ex b/src/zenflows/vf/organization/type.ex @@ -24,8 +24,8 @@ alias Zenflows.VF.Organization.Resolv @name "The name that this agent will be referred to by." @image """ -The URI to an image relevant to the agent, such as a logo, avatar, -photo, etc. +The base64-encoded image binary relevant to the agent, such as a logo, +avatar, photo, etc. """ @primary_location """ The main place an agent is located, often an address where activities @@ -49,7 +49,7 @@ object :organization do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @primary_location field :primary_location, :spatial_thing, @@ -71,7 +71,7 @@ input_object :organization_create_params do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string @@ -93,7 +93,7 @@ input_object :organization_update_params do field :name, :string @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string diff --git a/src/zenflows/vf/person.ex b/src/zenflows/vf/person.ex @@ -40,7 +40,7 @@ alias Zenflows.VF.{SpatialThing, Validate} schema "vf_agent" do field :type, Ecto.Enum, values: [:per], default: :per field :name, :string - field :image, :string, virtual: true + field :image, :string field :note, :string belongs_to :primary_location, SpatialThing field :user, :string @@ -97,7 +97,7 @@ def chgset(schema, params) do |> Changeset.cast(params, @update_cast) |> Validate.name(:name) |> Validate.name(:user) - |> Validate.uri(:image) + |> Validate.img(:image) |> Validate.note(:note) |> check_email() |> Changeset.unique_constraint(:user) diff --git a/src/zenflows/vf/person/domain.ex b/src/zenflows/vf/person/domain.ex @@ -30,7 +30,6 @@ alias Zenflows.VF.Person @typep id() :: Zenflows.DB.Schema.id() @typep params() :: Zenflows.DB.Schema.params() - @spec by(repo(), Keyword.t() | map()) :: Person.t() | nil def by(repo \\ Repo, clauses) def by(repo, clauses) when is_map(clauses) do diff --git a/src/zenflows/vf/person/type.ex b/src/zenflows/vf/person/type.ex @@ -24,8 +24,8 @@ alias Zenflows.VF.Person.Resolv @name "The name that this agent will be referred to by." @image """ -The URI to an image relevant to the agent, such as a logo, avatar, -photo, etc. +The base64-encoded image binary relevant to the agent, such as a logo, +avatar, photo, etc. """ @note "A textual description or comment." @primary_location """ @@ -52,7 +52,7 @@ object :person do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string @@ -92,7 +92,7 @@ input_object :person_create_params do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string @@ -132,7 +132,7 @@ input_object :person_update_params do field :name, :string @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string diff --git a/src/zenflows/vf/recipe_resource.ex b/src/zenflows/vf/recipe_resource.ex @@ -46,7 +46,7 @@ schema "vf_recipe_resource" do field :resource_classified_as, {:array, :string} belongs_to :resource_conforms_to, ResourceSpecification field :substitutable, :boolean, default: false - field :image, :string, virtual: true + field :image, :string field :note, :string end @@ -65,7 +65,7 @@ def chgset(schema \\ %__MODULE__{}, params) do |> Changeset.validate_required(@reqr) |> Validate.name(:name) |> Validate.note(:note) - |> Validate.uri(:image) + |> Validate.img(:image) |> Validate.class(:resource_conforms_to) |> Changeset.assoc_constraint(:unit_of_resource) |> Changeset.assoc_constraint(:unit_of_effort) diff --git a/src/zenflows/vf/recipe_resource/type.ex b/src/zenflows/vf/recipe_resource/type.ex @@ -27,7 +27,7 @@ An informal or formal textual identifier for a recipe resource. Does not imply uniqueness. """ @image """ -The URI to an image relevant to the entity, such as a photo, diagram, etc. +The base64-encoded image binary relevant to the entity, such as a photo, diagram, etc. """ @unit_of_resource """ The unit of inventory used for this resource in the recipe. @@ -70,7 +70,7 @@ object :recipe_resource do resolve: &Resolv.unit_of_effort/3 @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string @@ -104,7 +104,7 @@ input_object :recipe_resource_create_params do field :unit_of_effort_id, :id, name: "unit_of_effort" @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string @@ -132,7 +132,7 @@ input_object :recipe_resource_update_params do field :unit_of_effort_id, :id, name: "unit_of_effort" @desc @image - field :image, :uri + field :image, :base64 @desc @note field :note, :string diff --git a/src/zenflows/vf/resource_specification.ex b/src/zenflows/vf/resource_specification.ex @@ -37,7 +37,7 @@ alias Zenflows.VF.{Unit, Validate} schema "vf_resource_specification" do field :name, :string - field :image, :string, virtual: true + field :image, :string field :resource_classified_as, {:array, :string} field :note, :string belongs_to :default_unit_of_resource, Unit @@ -59,7 +59,7 @@ def chgset(schema \\ %__MODULE__{}, params) do |> Changeset.validate_required(@reqr) |> Validate.name(:name) |> Validate.note(:note) - |> Validate.uri(:image) + |> Validate.img(:image) |> Validate.class(:resource_classified_as) |> Changeset.assoc_constraint(:default_unit_of_resource) |> Changeset.assoc_constraint(:default_unit_of_effort) diff --git a/src/zenflows/vf/resource_specification/type.ex b/src/zenflows/vf/resource_specification/type.ex @@ -27,7 +27,8 @@ An informal or formal textual identifier for a type of resource. Does not imply uniqueness. """ @image """ -The URI to an image relevant to the entity, such as a photo, diagram, etc. +The base64-encoded image binary relevant to the entity, such as a photo, +diagram, etc. """ @resource_classified_as """ References a concept in a common taxonomy or other classification scheme @@ -49,7 +50,7 @@ object :resource_specification do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @resource_classified_as field :resource_classified_as, list_of(non_null(:uri)) @@ -75,7 +76,7 @@ input_object :resource_specification_create_params do field :name, non_null(:string) @desc @image - field :image, :uri + field :image, :base64 @desc @resource_classified_as field :resource_classified_as, list_of(non_null(:uri)) @@ -100,7 +101,7 @@ input_object :resource_specification_update_params do field :name, :string @desc @image - field :image, :uri + field :image, :base64 @desc @resource_classified_as field :resource_classified_as, list_of(non_null(:uri)) diff --git a/src/zenflows/vf/validate.ex b/src/zenflows/vf/validate.ex @@ -23,6 +23,8 @@ here are rough and can be changed in the future. alias Ecto.Changeset, as: Chset +require Logger + @doc "Checks if the given string field is [16, 2048] bytes long." @spec key(Chset.t(), atom()) :: Chset.t() def key(cset, field) do @@ -75,6 +77,27 @@ def uri(cset, field) do end) end +@mebibyte 1024**2 + +@doc """ +Check if the given base64-encoded binary data is at least 1B, at most +25MiB in size. And, display a warning if it is longer than 4MiB. +""" +@spec img(Chset.t(), atom()) :: Chset.t() +def img(cset, field) do + Chset.validate_change(cset, field, :valflow, fn + _, str when byte_size(str) < 1 -> + [{field, "should be at least 1B long"}] + _, str when byte_size(str) > 25 * @mebibyte -> + [{field, "should be at most 25MiB long"}] + _, str when byte_size(str) > 4 * @mebibyte -> + Logger.warning("file exceeds 4MiB") + [] + _, _ -> + [] + end) +end + @doc """ Checks if the given classifications (list of strings) for: