aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/web')
-rw-r--r--lib/pleroma/web/admin_api/admin_api_controller.ex13
-rw-r--r--lib/pleroma/web/common_api/common_api.ex7
-rw-r--r--lib/pleroma/web/common_api/utils.ex10
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex69
-rw-r--r--lib/pleroma/web/mastodon_api/views/push_subscription_view.ex7
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex23
-rw-r--r--lib/pleroma/web/nodeinfo/nodeinfo_controller.ex1
-rw-r--r--lib/pleroma/web/push/push.ex120
-rw-r--r--lib/pleroma/web/push/subscription.ex14
-rw-r--r--lib/pleroma/web/router.ex20
-rw-r--r--lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex1
-rw-r--r--lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex1
-rw-r--r--lib/pleroma/web/twitter_api/controllers/util_controller.ex14
-rw-r--r--lib/pleroma/web/twitter_api/twitter_api.ex19
-rw-r--r--lib/pleroma/web/twitter_api/twitter_api_controller.ex11
-rw-r--r--lib/pleroma/web/twitter_api/views/activity_view.ex15
16 files changed, 244 insertions, 101 deletions
diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex
index 06c3c7c81..4d73cf219 100644
--- a/lib/pleroma/web/admin_api/admin_api_controller.ex
+++ b/lib/pleroma/web/admin_api/admin_api_controller.ex
@@ -147,6 +147,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
end
+ @doc "Sends registration invite via email"
+ def email_invite(%{assigns: %{user: user}} = conn, %{"email" => email} = params) do
+ with true <-
+ Pleroma.Config.get([:instance, :invites_enabled]) &&
+ !Pleroma.Config.get([:instance, :registrations_open]),
+ {:ok, invite_token} <- Pleroma.UserInviteToken.create_token(),
+ email <-
+ Pleroma.UserEmail.user_invitation_email(user, invite_token, email, params["name"]),
+ {:ok, _} <- Pleroma.Mailer.deliver(email) do
+ json_response(conn, :no_content, "")
+ end
+ end
+
@doc "Get a account registeration invite token (base64 string)"
def get_invite_token(conn, _params) do
{:ok, token} = Pleroma.UserInviteToken.create_token()
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index e3385310f..f01d36370 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -1,6 +1,7 @@
defmodule Pleroma.Web.CommonAPI do
alias Pleroma.{User, Repo, Activity, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Formatter
import Pleroma.Web.CommonAPI.Utils
@@ -16,7 +17,8 @@ defmodule Pleroma.Web.CommonAPI do
def repeat(id_or_ap_id, user) do
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
- object <- Object.normalize(activity.data["object"]["id"]) do
+ object <- Object.normalize(activity.data["object"]["id"]),
+ nil <- Utils.get_existing_announce(user.ap_id, object) do
ActivityPub.announce(user, object)
else
_ ->
@@ -36,7 +38,8 @@ defmodule Pleroma.Web.CommonAPI do
def favorite(id_or_ap_id, user) do
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
- object <- Object.normalize(activity.data["object"]["id"]) do
+ object <- Object.normalize(activity.data["object"]["id"]),
+ nil <- Utils.get_existing_like(user.ap_id, object) do
ActivityPub.like(user, object)
else
_ ->
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index ce0926b99..142283684 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -112,6 +112,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do
Enum.join([text | attachment_text], "<br>")
end
+ @doc """
+ Formatting text to plain text.
+ """
def format_input(text, mentions, tags, "text/plain") do
text
|> Formatter.html_escape("text/plain")
@@ -123,6 +126,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> Formatter.finalize()
end
+ @doc """
+ Formatting text to html.
+ """
def format_input(text, mentions, _tags, "text/html") do
text
|> Formatter.html_escape("text/html")
@@ -132,8 +138,12 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> Formatter.finalize()
end
+ @doc """
+ Formatting text to markdown.
+ """
def format_input(text, mentions, tags, "text/markdown") do
text
+ |> Formatter.mentions_escape(mentions)
|> Earmark.as_html!()
|> Formatter.html_escape("text/html")
|> String.replace(~r/\r?\n/, "")
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index a46e1878f..726807f0a 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -1057,52 +1057,37 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do
actor = User.get_cached_by_ap_id(activity.data["actor"])
+ parent_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+ mastodon_type = Activity.mastodon_notification_type(activity)
- created_at =
- NaiveDateTime.to_iso8601(created_at)
- |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
-
- id = id |> to_string
+ response = %{
+ id: to_string(id),
+ type: mastodon_type,
+ created_at: CommonAPI.Utils.to_masto_date(created_at),
+ account: AccountView.render("account.json", %{user: actor, for: user})
+ }
- case activity.data["type"] do
- "Create" ->
- %{
- id: id,
- type: "mention",
- created_at: created_at,
- account: AccountView.render("account.json", %{user: actor, for: user}),
+ case mastodon_type do
+ "mention" ->
+ response
+ |> Map.merge(%{
status: StatusView.render("status.json", %{activity: activity, for: user})
- }
+ })
- "Like" ->
- liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+ "favourite" ->
+ response
+ |> Map.merge(%{
+ status: StatusView.render("status.json", %{activity: parent_activity, for: user})
+ })
- %{
- id: id,
- type: "favourite",
- created_at: created_at,
- account: AccountView.render("account.json", %{user: actor, for: user}),
- status: StatusView.render("status.json", %{activity: liked_activity, for: user})
- }
+ "reblog" ->
+ response
+ |> Map.merge(%{
+ status: StatusView.render("status.json", %{activity: parent_activity, for: user})
+ })
- "Announce" ->
- announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
-
- %{
- id: id,
- type: "reblog",
- created_at: created_at,
- account: AccountView.render("account.json", %{user: actor, for: user}),
- status: StatusView.render("status.json", %{activity: announced_activity, for: user})
- }
-
- "Follow" ->
- %{
- id: id,
- type: "follow",
- created_at: created_at,
- account: AccountView.render("account.json", %{user: actor, for: user})
- }
+ "follow" ->
+ response
_ ->
nil
@@ -1169,6 +1154,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do
+ true = Pleroma.Web.Push.enabled()
Pleroma.Web.Push.Subscription.delete_if_exists(user, token)
{:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params)
view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
@@ -1176,6 +1162,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
+ true = Pleroma.Web.Push.enabled()
subscription = Pleroma.Web.Push.Subscription.get(user, token)
view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
json(conn, view)
@@ -1185,12 +1172,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
%{assigns: %{user: user, token: token}} = conn,
params
) do
+ true = Pleroma.Web.Push.enabled()
{:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params)
view = PushSubscriptionView.render("push_subscription.json", subscription: subscription)
json(conn, view)
end
def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do
+ true = Pleroma.Web.Push.enabled()
{:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token)
json(conn, %{})
end
diff --git a/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex b/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex
index c8b95d14c..67e86294e 100644
--- a/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex
@@ -5,7 +5,12 @@ defmodule Pleroma.Web.MastodonAPI.PushSubscriptionView do
%{
id: to_string(subscription.id),
endpoint: subscription.endpoint,
- alerts: Map.get(subscription.data, "alerts")
+ alerts: Map.get(subscription.data, "alerts"),
+ server_key: server_key()
}
end
+
+ defp server_key do
+ Keyword.get(Application.get_env(:web_push_encryption, :vapid_details), :public_key)
+ end
end
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index c3c735d5d..46c559e3a 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -140,7 +140,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
visibility: get_visibility(object),
media_attachments: attachments |> Enum.take(4),
mentions: mentions,
- tags: tags,
+ tags: build_tags(tags),
application: %{
name: "Web",
website: nil
@@ -235,6 +235,27 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
def render_content(object), do: object["content"] || ""
@doc """
+ Builds a dictionary tags.
+
+ ## Examples
+
+ iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
+ [{"name": "fediverse", "url": "/tag/fediverse"},
+ {"name": "nextcloud", "url": "/tag/nextcloud"}]
+
+ """
+ @spec build_tags(list(any())) :: list(map())
+ def build_tags(object_tags) when is_list(object_tags) do
+ object_tags = for tag when is_binary(tag) <- object_tags, do: tag
+
+ Enum.reduce(object_tags, [], fn tag, tags ->
+ tags ++ [%{name: tag, url: "/tag/#{tag}"}]
+ end)
+ end
+
+ def build_tags(_), do: []
+
+ @doc """
Builds list emojis.
Arguments: `nil` or list tuple of name and url.
diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 277dc6ba1..44c11f40a 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -132,6 +132,7 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
banner: Keyword.get(instance, :banner_upload_limit),
background: Keyword.get(instance, :background_upload_limit)
},
+ invitesEnabled: Keyword.get(instance, :invites_enabled, false),
features: features
}
}
diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex
index 5a873ec19..477943450 100644
--- a/lib/pleroma/web/push/push.ex
+++ b/lib/pleroma/web/push/push.ex
@@ -9,67 +9,99 @@ defmodule Pleroma.Web.Push do
@types ["Create", "Follow", "Announce", "Like"]
- @gcm_api_key nil
-
def start_link() do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
- def init(:ok) do
- case Application.get_env(:web_push_encryption, :vapid_details) do
- nil ->
- Logger.warn(
- "VAPID key pair is not found. Please, add VAPID configuration to config. Run `mix web_push.gen.keypair` mix task to create a key pair"
- )
-
- :ignore
+ def vapid_config() do
+ Application.get_env(:web_push_encryption, :vapid_details, [])
+ end
- _ ->
- {:ok, %{}}
+ def enabled() do
+ case vapid_config() do
+ [] -> false
+ list when is_list(list) -> true
+ _ -> false
end
end
def send(notification) do
- if Application.get_env(:web_push_encryption, :vapid_details) do
+ if enabled() do
GenServer.cast(Pleroma.Web.Push, {:send, notification})
end
end
+ def init(:ok) do
+ if !enabled() do
+ Logger.warn("""
+ VAPID key pair is not found. If you wish to enabled web push, please run
+
+ mix web_push.gen.keypair
+
+ and add the resulting output to your configuration file.
+ """)
+
+ :ignore
+ else
+ {:ok, nil}
+ end
+ end
+
def handle_cast(
{:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification},
state
)
when type in @types do
actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
- body = notification |> format(actor) |> Jason.encode!()
+
+ type = Pleroma.Activity.mastodon_notification_type(notification.activity)
Subscription
|> where(user_id: ^user_id)
+ |> preload(:token)
|> Repo.all()
- |> Enum.each(fn record ->
- subscription = %{
+ |> Enum.filter(fn subscription ->
+ get_in(subscription.data, ["alerts", type]) || false
+ end)
+ |> Enum.each(fn subscription ->
+ sub = %{
keys: %{
- p256dh: record.key_p256dh,
- auth: record.key_auth
+ p256dh: subscription.key_p256dh,
+ auth: subscription.key_auth
},
- endpoint: record.endpoint
+ endpoint: subscription.endpoint
}
- case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do
+ body =
+ Jason.encode!(%{
+ title: format_title(notification),
+ access_token: subscription.token.token,
+ body: format_body(notification, actor),
+ notification_id: notification.id,
+ notification_type: type,
+ icon: User.avatar_url(actor),
+ preferred_locale: "en"
+ })
+
+ case WebPushEncryption.send_web_push(
+ body,
+ sub,
+ Application.get_env(:web_push_encryption, :gcm_api_key)
+ ) do
{:ok, %{status_code: code}} when 400 <= code and code < 500 ->
Logger.debug("Removing subscription record")
- Repo.delete!(record)
+ Repo.delete!(subscription)
:ok
{:ok, %{status_code: code}} when 200 <= code and code < 300 ->
:ok
{:ok, %{status_code: code}} ->
- Logger.error("Web Push Nonification failed with code: #{code}")
+ Logger.error("Web Push Notification failed with code: #{code}")
:error
_ ->
- Logger.error("Web Push Nonification failed with unknown error")
+ Logger.error("Web Push Notification failed with unknown error")
:error
end
end)
@@ -82,35 +114,21 @@ defmodule Pleroma.Web.Push do
{:noreply, state}
end
- def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do
- %{
- title: "New Mention",
- body: "@#{actor.nickname} has mentiond you",
- icon: User.avatar_url(actor)
- }
- end
-
- def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do
- %{
- title: "New Follower",
- body: "@#{actor.nickname} has followed you",
- icon: User.avatar_url(actor)
- }
- end
-
- def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
- %{
- title: "New Announce",
- body: "@#{actor.nickname} has announced your post",
- icon: User.avatar_url(actor)
- }
+ defp format_title(%{activity: %{data: %{"type" => type}}}) do
+ case type do
+ "Create" -> "New Mention"
+ "Follow" -> "New Follower"
+ "Announce" -> "New Repeat"
+ "Like" -> "New Favorite"
+ end
end
- def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
- %{
- title: "New Like",
- body: "@#{actor.nickname} has liked your post",
- icon: User.avatar_url(actor)
- }
+ defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do
+ case type do
+ "Create" -> "@#{actor.nickname} has mentioned you"
+ "Follow" -> "@#{actor.nickname} has followed you"
+ "Announce" -> "@#{actor.nickname} has repeated your post"
+ "Like" -> "@#{actor.nickname} has favorited your post"
+ end
end
end
diff --git a/lib/pleroma/web/push/subscription.ex b/lib/pleroma/web/push/subscription.ex
index cfab7a98e..1ad405daf 100644
--- a/lib/pleroma/web/push/subscription.ex
+++ b/lib/pleroma/web/push/subscription.ex
@@ -37,8 +37,8 @@ defmodule Pleroma.Web.Push.Subscription do
user_id: user.id,
token_id: token.id,
endpoint: endpoint,
- key_auth: key_auth,
- key_p256dh: key_p256dh,
+ key_auth: ensure_base64_urlsafe(key_auth),
+ key_p256dh: ensure_base64_urlsafe(key_p256dh),
data: alerts(params)
})
end
@@ -63,4 +63,14 @@ defmodule Pleroma.Web.Push.Subscription do
sub -> Repo.delete(sub)
end
end
+
+ # Some webpush clients (e.g. iOS Toot!) use an non urlsafe base64 as an encoding for the key.
+ # However, the web push rfs specify to use base64 urlsafe, and the `web_push_encryption` library we use
+ # requires the key to be properly encoded. So we just convert base64 to urlsafe base64.
+ defp ensure_base64_urlsafe(string) do
+ string
+ |> String.replace("+", "-")
+ |> String.replace("/", "_")
+ |> String.replace("=", "")
+ end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 9c06fac4f..daff3362c 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -85,6 +85,15 @@ defmodule Pleroma.Web.Router do
plug(:accepts, ["html", "json"])
end
+ pipeline :mailbox_preview do
+ plug(:accepts, ["html"])
+
+ plug(:put_secure_browser_headers, %{
+ "content-security-policy" =>
+ "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' 'unsafe-eval'"
+ })
+ end
+
scope "/api/pleroma", Pleroma.Web.TwitterAPI do
pipe_through(:pleroma_api)
get("/password_reset/:token", UtilController, :show_password_reset)
@@ -108,6 +117,8 @@ defmodule Pleroma.Web.Router do
delete("/relay", AdminAPIController, :relay_unfollow)
get("/invite_token", AdminAPIController, :get_invite_token)
+ post("/email_invite", AdminAPIController, :email_invite)
+
get("/password_reset", AdminAPIController, :get_password_reset)
end
@@ -268,6 +279,7 @@ defmodule Pleroma.Web.Router do
get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)
post("/account/register", TwitterAPI.Controller, :register)
+ post("/account/password_reset", TwitterAPI.Controller, :password_reset)
get("/search", TwitterAPI.Controller, :search)
get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
@@ -424,6 +436,14 @@ defmodule Pleroma.Web.Router do
get("/:sig/:url/:filename", MediaProxyController, :remote)
end
+ if Mix.env() == :dev do
+ scope "/dev" do
+ pipe_through([:mailbox_preview])
+
+ forward("/mailbox", Plug.Swoosh.MailboxPreview, base_path: "/dev/mailbox")
+ end
+ end
+
scope "/", Fallback do
get("/registration/:token", RedirectController, :registration_page)
get("/*path", RedirectController, :redirector)
diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex b/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex
index 58a3736fd..df037c01e 100644
--- a/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex
+++ b/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex
@@ -1 +1,2 @@
<h2>Password reset failed</h2>
+<h3><a href="<%= Pleroma.Web.base_url() %>">Homepage</a></h3>
diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex b/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex
index c7dfcb6dd..f30ba3274 100644
--- a/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex
+++ b/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex
@@ -1 +1,2 @@
<h2>Password changed!</h2>
+<h3><a href="<%= Pleroma.Web.base_url() %>">Homepage</a></h3>
diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
index b1e4c77e8..2f2b69623 100644
--- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex
+++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
@@ -156,17 +156,25 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
|> send_resp(200, response)
_ ->
- vapid_public_key =
- Keyword.get(Application.get_env(:web_push_encryption, :vapid_details), :public_key)
+ vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
+
+ uploadlimit = %{
+ uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
+ avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
+ backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
+ bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
+ }
data = %{
name: Keyword.get(instance, :name),
description: Keyword.get(instance, :description),
server: Web.base_url(),
textlimit: to_string(Keyword.get(instance, :limit)),
+ uploadlimit: uploadlimit,
closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"),
private: if(Keyword.get(instance, :public, true), do: "0", else: "1"),
- vapidPublicKey: vapid_public_key
+ vapidPublicKey: vapid_public_key,
+ invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0")
}
pleroma_fe = %{
diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex
index 79ea48d86..1e764f24a 100644
--- a/lib/pleroma/web/twitter_api/twitter_api.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api.ex
@@ -167,6 +167,25 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end
end
+ def password_reset(nickname_or_email) do
+ with true <- is_binary(nickname_or_email),
+ %User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email),
+ {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
+ user
+ |> Pleroma.UserEmail.password_reset_email(token_record.token)
+ |> Pleroma.Mailer.deliver()
+ else
+ false ->
+ {:error, "bad user identifier"}
+
+ %User{local: false} ->
+ {:error, "remote user"}
+
+ nil ->
+ {:error, "unknown user"}
+ end
+ end
+
def get_by_id_or_nickname(id_or_nickname) do
if !is_integer(id_or_nickname) && :error == Integer.parse(id_or_nickname) do
Repo.get_by(User, nickname: id_or_nickname)
diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
index 786849aa3..38eff8191 100644
--- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
@@ -1,5 +1,8 @@
defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller
+
+ import Pleroma.Web.ControllerHelper, only: [json_response: 3]
+
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView}
alias Pleroma.Web.CommonAPI
alias Pleroma.{Repo, Activity, Object, User, Notification}
@@ -322,6 +325,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end
end
+ def password_reset(conn, params) do
+ nickname_or_email = params["email"] || params["nickname"]
+
+ with {:ok, _} <- TwitterAPI.password_reset(nickname_or_email) do
+ json_response(conn, :no_content, "")
+ end
+ end
+
def update_avatar(%{assigns: %{user: user}} = conn, params) do
{:ok, object} = ActivityPub.upload(params, type: :avatar)
change = Changeset.change(user, %{avatar: object.data})
diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
index 83e8fb765..0699bf1da 100644
--- a/lib/pleroma/web/twitter_api/views/activity_view.ex
+++ b/lib/pleroma/web/twitter_api/views/activity_view.ex
@@ -14,6 +14,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
alias Pleroma.HTML
import Ecto.Query
+ require Logger
defp query_context_ids([]), do: []
@@ -190,6 +191,11 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
text = "#{user.nickname} favorited a status."
+ favorited_status =
+ if liked_activity,
+ do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
+ else: nil
+
%{
"id" => activity.id,
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
@@ -199,6 +205,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
"is_post_verb" => false,
"uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
"created_at" => created_at,
+ "favorited_status" => favorited_status,
"in_reply_to_status_id" => liked_activity_id,
"external_url" => activity.data["id"],
"activity_type" => "like"
@@ -233,7 +240,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
{summary, content} = render_content(object)
html =
- HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
+ content
+ |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
|> Formatter.emojify(object["emoji"])
reply_parent = Activity.get_in_reply_to_activity(activity)
@@ -270,6 +278,11 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
}
end
+ def render("activity.json", %{activity: unhandled_activity}) do
+ Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
+ nil
+ end
+
def render_content(%{"type" => "Note"} = object) do
summary = object["summary"]