Browse Source

Add API endpoints for a custom user mascot

tags/v1.1.4
Sadposter 5 years ago
parent
commit
54e9cb5c2d
No known key found for this signature in database GPG Key ID: 6F3BAD60DE190290
4 changed files with 98 additions and 1 deletions
  1. +39
    -0
      docs/api/pleroma_api.md
  2. +21
    -0
      lib/pleroma/user/info.ex
  3. +35
    -1
      lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
  4. +3
    -0
      lib/pleroma/web/router.ex

+ 39
- 0
docs/api/pleroma_api.md View File

@@ -252,6 +252,45 @@ See [Admin-API](Admin-API.md)
]
```

## `/api/v1/pleroma/mascot`
### Gets user mascot image
* Method `GET`
* Authentication: required

* Response: JSON. Returns a mastodon media attachment entity.
* Example response:
```json
{
"id": "abcdefg",
"url": "https://pleroma.example.org/media/abcdefg.png",
"type": "image",
"pleroma": {
"mime_type": "image/png"
}
}
```

### Updates user mascot image
* Method `PUT`
* Authentication: required
* Params:
* `image`: Multipart image
* Response: JSON. Returns a mastodon media attachment entity
when successful, otherwise returns HTTP 415 `{"error": "error_msg"}`
* Example response:
```json
{
"id": "abcdefg",
"url": "https://pleroma.example.org/media/abcdefg.png",
"type": "image",
"pleroma": {
"mime_type": "image/png"
}
}
```
* Note: Behaves exactly the same as `POST /api/v1/upload`.
Can only accept images - any attempt to upload non-image files will be met with `HTTP 415 Unsupported Media Type`.

## `/api/pleroma/notification_settings`
### Updates user notification settings
* Method `PUT`


+ 21
- 0
lib/pleroma/user/info.ex View File

@@ -43,6 +43,19 @@ defmodule Pleroma.User.Info do
field(:hide_favorites, :boolean, default: true)
field(:pinned_activities, {:array, :string}, default: [])
field(:flavour, :string, default: nil)

field(:mascot, :map,
default: %{
id: "pleromatan",
url: "/images/pleroma-fox-tan-smol.png",
type: "image",
preview_url: "/images/pleroma-fox-tan-smol.png",
pleroma: %{
mime_type: "image/png"
}
}
)

field(:emoji, {:array, :map}, default: [])

field(:notification_settings, :map,
@@ -248,6 +261,14 @@ defmodule Pleroma.User.Info do
|> validate_required([:flavour])
end

def mascot_update(info, url) do
params = %{mascot: url}

info
|> cast(params, [:mascot])
|> validate_required([:mascot])
end

def set_source_data(info, source_data) do
params = %{source_data: source_data}



+ 35
- 1
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex View File

@@ -707,6 +707,40 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
end

def set_mascot(%{assigns: %{user: user}} = conn, %{"file" => file}) do
with {:ok, object} <- ActivityPub.upload(file, actor: User.ap_id(user)),
%{} = attachment_data <- Map.put(object.data, "id", object.id),
%{type: type} = rendered <-
StatusView.render("attachment.json", %{attachment: attachment_data}) do
# Reject if not an image
if type == "image" do
# Sure!
# Save to the user's info
info_changeset = User.Info.mascot_update(user.info, rendered)

user_changeset =
user
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:info, info_changeset)

{:ok, _user} = User.update_and_set_cache(user_changeset)

conn
|> json(rendered)
else
conn
|> send_resp(415, Jason.encode!(%{"error" => "mascots can only be images"}))
end
end
end

def get_mascot(%{assigns: %{user: user}} = conn, _params) do
%{info: %{mascot: mascot}} = user

conn
|> json(mascot)
end

def favourited_by(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with %Activity{data: %{"object" => object}} <- Repo.get(Activity, id),
%Object{data: %{"likes" => likes}} <- Object.normalize(object) do
@@ -1329,7 +1363,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
display_sensitive_media: false,
reduce_motion: false,
max_toot_chars: limit,
mascot: "/images/pleroma-fox-tan-smol.png"
mascot: Map.get(user.info.mascot, "url", "/images/pleroma-fox-tan-smol.png")
},
rights: %{
delete_others_notice: present?(user.info.is_moderator),


+ 3
- 0
lib/pleroma/web/router.ex View File

@@ -352,6 +352,9 @@ defmodule Pleroma.Web.Router do

post("/pleroma/flavour/:flavour", MastodonAPIController, :set_flavour)

get("/pleroma/mascot", MastodonAPIController, :get_mascot)
put("/pleroma/mascot", MastodonAPIController, :set_mascot)

post("/reports", MastodonAPIController, :reports)
end



Loading…
Cancel
Save