Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contexts API #861

Merged
merged 15 commits into from
Mar 12, 2024
Merged

Contexts API #861

merged 15 commits into from
Mar 12, 2024

Conversation

blizzz
Copy link
Member

@blizzz blizzz commented Feb 21, 2024

contributes to #311

Implementation state

  • Contexts index
  • Context detail
  • Context create
  • Context update
  • Add a node to a Context
  • Remove a node from a Context
  • Update a node's order on a page
  • Transfer ownership
  • No endpoint for dedicated page logic so far, as in this iteration we only have one page ("startpage") and as long we manage it automatically with context / node updates.

Examples

Index

Get an overview of available contexts for the current user

Request

curl --silent \
    -H "OCS-APIRequest: true" \
    -u alice:password \
    'https://my.nxt.cld/ocs/v2.php/apps/tables/api/2/contexts?format=json' | jq

Result

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": [
      {
        "id": 0,
        "name": "Test One",
        "iconName": "account_circle",
        "description": "A good context",
        "owner": "foobar",
        "ownerType": 0
      },
      {
        "id": 3,
        "name": "All good things are three",
        "iconName": "local_shipping",
        "description": "A fascinating context",
        "owner": "alice",
        "ownerType": 0
      }
    ]
  }
}

The context with ID 0 stems for a share towards requesting user alice (either direct or group share), while the context with ID 3 is owned by them.

ownerType is always 0 (= user) in the first Contexts iteration.

Get full context information for a context receiver (by share)

Show Context

Request

curl --silent \
    -H "OCS-APIRequest: true" \
    -u jenna:jenna 
    'https://my.nxt.cld/ocs/v2.php/apps/tables/api/2/contexts/11?format=json' | jq

Result

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": {
      "id": 11,
      "name": "Vacation requests",
      "iconName": "holiday_village",
      "description": "Applications for vacations",
      "owner": "0",
      "ownerType": null,
      "sharing": {
        "5": {
          "share_id": 5,
          "receiver": "jenna",
          "receiver_type": "user",
          "display_mode_default": 0,
          "display_mode": 2
        }
      },
      "nodes": {
        "2": {
          "id": 2,
          "node_id": 9,
          "node_type": "table",
          "permissions": 660
        },
        "5": {
          "id": 5,
          "node_id": 19,
          "node_type": "view",
          "permissions": 660
        },
        "4": {
          "id": 4,
          "node_id": 17,
          "node_type": "view",
          "permissions": 660
        },
        "3": {
          "id": 3,
          "node_id": 13,
          "node_type": "view",
          "permissions": 660
        },
        "1": {
          "id": 1,
          "node_id": 7,
          "node_type": "table",
          "permissions": 660
        }
      },
      "pages": {
        "1": {
          "content": {
            "2": {
              "order": 20,
              "node_rel_id": 2
            },
            "5": {
              "order": 30,
              "node_rel_id": 5
            },
            "4": {
              "order": 50,
              "node_rel_id": 4
            },
            "3": {
              "order": 60,
              "node_rel_id": 3
            },
            "1": {
              "order": 90,
              "node_rel_id": 1
            }
          },
          "id": 1,
          "page_type": "startpage"
        }
      }
    }
  }
}

Create Context

Creating a context

The nodes are optional. The context is created empty without nodes, when those are not provided.

Request

curl --silent \
    -X POST \
    -u alice:password \
    -H "OCS-APIRequest: true" \
    -H "Content-Type: application/json" \
    -d '{ "name": "Enchanting Pancake", "iconName": "airline_seat_recline_extra", "description": "comparison of pancake receipes, ingredients, cafés and cooks.", "nodes": [{"id": 4, "type": "0", "permissions": 7}, {"id": 2, "type": "1", "permissions": 4}] }' \
    'https://my.nc.srv/ocs/v2.php/apps/tables/api/2/contexts?format=json' | jq

Return

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": {
      "id": 20,
      "name": "Enchanting Pancake",
      "iconName": "airline_seat_recline_extra",
      "description": "comparison of pancake receipes, ingredients, cafés and cooks.",
      "owner": "alice",
      "ownerType": 0,
      "sharing": null,
      "nodes": [
        {
          "id": 36,
          "contextId": 20,
          "nodeId": 4,
          "nodeType": "table",
          "permissions": 7
        },
        {
          "id": 37,
          "contextId": 20,
          "nodeId": 2,
          "nodeType": "views",
          "permissions": 4
        }
      ],
      "pages": {
        "id": 10,
        "contextId": 20,
        "pageType": "startpage",
        "content": {
          "10": {
            "id": 10,
            "nodeRelId": 36,
            "order": 10
          },
          "11": {
            "id": 11,
            "nodeRelId": 37,
            "order": 20
          }
        }
      }
    }
  }
}

Update Context

Updating a context

This does not include transfering a context. The basic information name iconName and description can be changed. When their values are not provided, they are being left unchanged.

Request

curl --silent \
    -X PUT \
    -u alice:password \
    -H "OCS-APIRequest: true" \
    -H "Content-Type: application/json" \
    -d '{ "name": "Enchanting Guitar", "iconName": "view_headline" "description": "A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.", "nodes": [{"id": 4, "type": "0", "permissions": 7, "order": 100}, {"id": 7, "type": "1", "permissions": 4, "order": 60}] }' \
    'https://my.nc.srv/master/ocs/v2.php/apps/tables/api/2/contexts/26?format=json' | jq

Return

The full Context object is being returned.

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": {
      "id": 26,
      "name": "Enchanting Guitar",
      "iconName": "view_headline",
      "description": "A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.",
      "owner": "alice",
      "ownerType": 0,
      "sharing": [],
      "nodes": {
        "64": {
          "id": 64,
          "node_id": 4,
          "node_type": "0",
          "permissions": 7
        },
        "65": {
          "id": 63,
          "node_id": 7,
          "node_type": "1",
          "permissions": 4
        },
      },
      "pages": {
        "13": {
          "content": {
            "19": {
              "order": 100,
              "node_rel_id": 64
            },
            "20": {
              "order": 60,
              "node_rel_id": 65
            },
          },
          "id": 13,
          "page_type": "startpage"
        }
      }
    }
  }
}

Delete a specific node from a Context

Deleting a node from a context

The node is being automatically removed from any page content. The whole Context object is returned upon success.

Request

curl --silent \
    -X POST \
    -u alice:password \
    -H "OCS-APIRequest: true" \
    -H "Content-Type: application/json" \
    -d '{ "nodeId": 4, "nodeType": 0, "permissions": 7 }' \
    'https://my.nc.srv/ocs/v2.php/apps/tables/api/2/contexts/26/nodes?format=json' | jq

Return

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": {
      "id": 26,
      "name": "Enchanting Pancake",
      "iconName": "airline_seat_recline_extra",
      "description": "comparison of pancake receipes, ingredients, cafés and cooks.",
      "owner": "alice",
      "ownerType": 0,
      "sharing": [],
      "nodes": {
        "63": {
          "id": 63,
          "node_id": 2,
          "node_type": "1",
          "permissions": 4
        }
      },
      "pages": {
        "13": {
          "content": {
            "18": {
              "order": 20,
              "node_rel_id": 63
            }
          },
          "id": 13,
          "page_type": "startpage"
        }
      }
    }
  }
}

Change order of contents in a page

Change the order of the page

In this example, the order of three content items is changed. One does not exists. Those are being ignored. However returned are only the content items that actually where updated.

Request

curl --silent \
    -X PUT \
    -u alice:password \
    -H "OCS-APIRequest: true" \
    -H "Content-Type: application/json" \
    -d '{ "content": [ {"id": 18, "order": 60}, {"id": 19, "order": 161}, { "id": 404, "order": 404} ]}' \
    'https://my.nxt.cld/ocs/v2.php/apps/tables/api/2/contexts/26/pages/13?format=json' | jq

Return

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": [
      {
        "id": 18,
        "pageId": 13,
        "nodeRelId": 63,
        "order": 60
      },
      {
        "id": 19,
        "pageId": 13,
        "nodeRelId": 64,
        "order": 161
      }
    ]
  }
}

Transfer ownership of a Context

Change the owner of the context

At the moment where we only support user owner contexts, the newOwnerType parameter is optional and defaults to 0(user), but might change in future.

The user the context is transferred to must exist.

Request

curl --silent \
    -X PUT \
    -u alice:password \
    -H "OCS-APIRequest: true" \
    -H "Content-Type: application/json" \
    -d '{"newOwnerId": "bob", "newOwnerType": 0}' \
    'https://example.com/ocs/v2.php/apps/tables/api/2/contexts/26/transfer?format=json' | jq

Return

{
  "ocs": {
    "meta": {
      "status": "ok",
      "statuscode": 200,
      "message": "OK"
    },
    "data": {
      "id": 26,
      "name": "Enchanting Guitar",
      "iconName": "view_headline",
      "description": "A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.",
      "owner": "bob",
      "ownerType": 0,
      "sharing": [],
      "nodes": {
        "63": {
          "id": 63,
          "node_id": 2,
          "node_type": "1",
          "permissions": 4
        },
        "64": {
          "id": 64,
          "node_id": 4,
          "node_type": "0",
          "permissions": 7
        }
      },
      "pages": {
        "13": {
          "content": {
            "18": {
              "order": 60,
              "node_rel_id": 63
            },
            "19": {
              "order": 161,
              "node_rel_id": 64
            }
          },
          "id": 13,
          "page_type": "startpage"
        }
      }
    }
  }
}

occ Commands

I also added them while doing it

occ tables:contexts:list [options] [--] [<user-id>]
Description:
  Get all contexts or contexts available to a specified user

Usage:
  tables:contexts:list [options] [--] [&lt;user-id&gt;]

Arguments:
  user-id                User ID of the user
occ tables:contexts:show [options] [--] <context-id> [<user-id>]
Description:
  Get all contexts or contexts available to a specified user

Usage:
  tables:contexts:show [options] [--] &lt;context-id&gt; [&lt;user-id&gt;]

Arguments:
  context-id             The ID of the context to show
  user-id                Optionally, showing the context from the perspective of the given user

@blizzz blizzz added enhancement New feature or request 2. developing Work in progress labels Feb 21, 2024
@blizzz blizzz added this to the v0.8.0 milestone Feb 21, 2024
@blizzz
Copy link
Member Author

blizzz commented Feb 21, 2024

I see some Controller names are prefixed with Api. I can adjust to that if this is preferred 🤷

<referencedClass name="Doctrine\DBAL\Types\Types" />
<referencedClass name="OC\Core\Command\Base" />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activity is doing the same 😇

@blizzz blizzz self-assigned this Feb 21, 2024
@blizzz
Copy link
Member Author

blizzz commented Feb 21, 2024

Added also a GET to fetch the information for a context. While in the overview there are only basic information from the context DB, the endpoint for a specific context is more detailed. It fetches data for sharing, the nodes, and the pages as well. Unfold Get full context information for a context receiver (by share) to get a response preview (ignore the permission value of 660, that's a placeholder for now).

The table/view data has to be fetched separately.

@enjeck What do you think? Did i oversee something, or do you have ideas for improvements?

The second commit is not super clean yet (therefore the temporary commit message), but generally should do what it does. I wonder whether i misuse the Mapper and Entity though, going across the tables. I could split of all the fetching of the sharing, node, and page data, but it would result in more DB queries. And in the end I'd have to compose them altogether in some type as well. Insofar I guess it is ok (unless some future need suggests otherwise - the modification parts are still ahead).

@blizzz blizzz changed the base branch from main to feat/contexts February 21, 2024 21:31
Copy link
Contributor

@enjeck enjeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enjeck What do you think? Did i oversee something, or do you have ideas for improvements?

I don't understand enough of the code to say much. But if there's a GET request for fetching contexts, I guess we'll have more for creating, editing and deleting?

appinfo/routes.php Outdated Show resolved Hide resolved
lib/Controller/ContextsController.php Outdated Show resolved Hide resolved
@blizzz
Copy link
Member Author

blizzz commented Feb 22, 2024

@enjeck What do you think? Did i oversee something, or do you have ideas for improvements?

I don't understand enough of the code to say much. But if there's a GET request for fetching contexts, I guess we'll have more for creating, editing and deleting?

Yes, unfold the examples in the PR description ^ There is a usage example with curl as well as result output.

Creating, Editing, Deleting will follow. So far i filled tables with dummy data for my testing.

@blizzz blizzz force-pushed the enh/noid/contexts-api branch 3 times, most recently from 6c4259d to 0b3bc71 Compare February 26, 2024 17:05
@blizzz
Copy link
Member Author

blizzz commented Feb 26, 2024

Just pushed a quick implementation of creating contexts (no occ yet, curl example added to description).

@blizzz blizzz force-pushed the enh/noid/contexts-api branch 3 times, most recently from 9567cb2 to 43208a1 Compare February 27, 2024 12:35
@blizzz blizzz force-pushed the enh/noid/contexts-api branch 2 times, most recently from bdc9e09 to f8a1591 Compare February 28, 2024 20:23
@blizzz
Copy link
Member Author

blizzz commented Feb 28, 2024

Added more endpoints and two examples ^

@blizzz
Copy link
Member Author

blizzz commented Feb 29, 2024

Almost complete, not very polished, but the endpoints should be there (just transfer upcoming) – I rushed it a bit now.. Needs better descriptions and I have to make it nice for openapi, but the examples in the description ^ should give a good idea already. Happy about feedback and of course open for improvements.

@juliusknorr juliusknorr mentioned this pull request Mar 3, 2024
13 tasks
@blizzz blizzz force-pushed the enh/noid/contexts-api branch 2 times, most recently from 3140478 to 618fa9a Compare March 4, 2024 11:16
@blizzz
Copy link
Member Author

blizzz commented Mar 11, 2024

Updated Controller information for OpenAPI, but did not regenerated openapi.json, because it needs #919

@blizzz blizzz added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Mar 12, 2024
@blizzz blizzz marked this pull request as ready for review March 12, 2024 09:45

$context = $this->contextMapper->update($context);

$auditEvent = new CriticalActionPerformedEvent(
Copy link
Member

@juliusknorr juliusknorr Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I wasn't aware we have this event

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
- also emits an Event for admin_audit

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
- includes a fix to also show pages that have no content

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
@blizzz
Copy link
Member Author

blizzz commented Mar 12, 2024

(rebased)

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
@blizzz blizzz merged commit b9e9e42 into feat/contexts Mar 12, 2024
46 checks passed
@blizzz blizzz deleted the enh/noid/contexts-api branch March 12, 2024 19:18
@juliusknorr juliusknorr modified the milestones: v0.8.0, v0.7.0 Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review Waiting for reviews enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants