diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/filter.ex | 62 | ||||
| -rw-r--r-- | lib/pleroma/formatter.ex | 4 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 5 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 65 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_socket.ex | 6 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/views/account_view.ex | 9 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/views/filter_view.ex | 27 | ||||
| -rw-r--r-- | lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 6 | ||||
| -rw-r--r-- | lib/pleroma/web/router.ex | 6 | ||||
| -rw-r--r-- | lib/pleroma/web/twitter_api/views/activity_view.ex | 3 | 
10 files changed, 185 insertions, 8 deletions
| diff --git a/lib/pleroma/filter.ex b/lib/pleroma/filter.ex new file mode 100644 index 000000000..fe904df3a --- /dev/null +++ b/lib/pleroma/filter.ex @@ -0,0 +1,62 @@ +defmodule Pleroma.Filter do +  use Ecto.Schema +  import Ecto.{Changeset, Query} +  alias Pleroma.{User, Repo, Activity} + +  schema "filters" do +    belongs_to(:user, Pleroma.User) +    field(:filter_id, :integer) +    field(:hide, :boolean, default: false) +    field(:whole_word, :boolean, default: true) +    field(:phrase, :string) +    field(:context, {:array, :string}) +    field(:expires_at, :utc_datetime) + +    timestamps() +  end + +  def get(id, %{id: user_id} = _user) do +    query = +      from( +        f in Pleroma.Filter, +        where: f.filter_id == ^id, +        where: f.user_id == ^user_id +      ) + +    Repo.one(query) +  end + +  def get_filters(%Pleroma.User{id: user_id} = user) do +    query = +      from( +        f in Pleroma.Filter, +        where: f.user_id == ^user_id +      ) + +    Repo.all(query) +  end + +  def create(%Pleroma.Filter{} = filter) do +    Repo.insert(filter) +  end + +  def delete(%Pleroma.Filter{id: filter_key} = filter) when is_number(filter_key) do +    Repo.delete(filter) +  end + +  def delete(%Pleroma.Filter{id: filter_key} = filter) when is_nil(filter_key) do +    %Pleroma.Filter{id: id} = get(filter.filter_id, %{id: filter.user_id}) + +    filter +    |> Map.put(:id, id) +    |> Repo.delete() +  end + +  def update(%Pleroma.Filter{} = filter) do +    destination = Map.from_struct(filter) + +    Pleroma.Filter.get(filter.filter_id, %{id: filter.user_id}) +    |> cast(destination, [:phrase, :context, :hide, :expires_at, :whole_word]) +    |> Repo.update() +  end +end diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 9be54e863..2b4c3c2aa 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -158,10 +158,12 @@ defmodule Pleroma.Formatter do      end)    end -  def get_emoji(text) do +  def get_emoji(text) when is_binary(text) do      Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)    end +  def get_emoji(_), do: [] +    def get_custom_emoji() do      @emoji    end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 81c11dd76..361e93e91 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -93,6 +93,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do            Pleroma.Web.Streamer.stream("public:local", activity)          end +        activity.data["object"] +        |> Map.get("tag", []) +        |> Enum.filter(fn tag -> is_bitstring(tag) end) +        |> Enum.map(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end) +          if activity.data["object"]["attachment"] != [] do            Pleroma.Web.Streamer.stream("public:media", activity) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index e0267f1dc..49a8655f0 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -2,7 +2,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do    use Pleroma.Web, :controller    alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}    alias Pleroma.Web -  alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView} +  alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView, FilterView}    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.Web.ActivityPub.Utils    alias Pleroma.Web.CommonAPI @@ -125,7 +125,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do    end    @instance Application.get_env(:pleroma, :instance) -  @mastodon_api_level "2.3.3" +  @mastodon_api_level "2.4.3"    def masto_instance(conn, _params) do      response = %{ @@ -1030,6 +1030,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do        NaiveDateTime.to_iso8601(created_at)        |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) +    id = id |> to_string +      case activity.data["type"] do        "Create" ->          %{ @@ -1075,6 +1077,65 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do      end    end +  def get_filters(%{assigns: %{user: user}} = conn, params) do +    filters = Pleroma.Filter.get_filters(user) +    res = FilterView.render("filters.json", filters: filters) +    json(conn, res) +  end + +  def create_filter( +        %{assigns: %{user: user}} = conn, +        %{"phrase" => phrase, "context" => context} = params +      ) do +    query = %Pleroma.Filter{ +      user_id: user.id, +      phrase: phrase, +      context: context, +      hide: Map.get(params, "irreversible", nil), +      whole_word: Map.get(params, "boolean", true) +      # expires_at +    } + +    {:ok, response} = Pleroma.Filter.create(query) +    res = FilterView.render("filter.json", filter: response) +    json(conn, res) +  end + +  def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id} = params) do +    filter = Pleroma.Filter.get(filter_id, user) +    res = FilterView.render("filter.json", filter: filter) +    json(conn, res) +  end + +  def update_filter( +        %{assigns: %{user: user}} = conn, +        %{"phrase" => phrase, "context" => context, "id" => filter_id} = params +      ) do +    query = %Pleroma.Filter{ +      user_id: user.id, +      filter_id: filter_id, +      phrase: phrase, +      context: context, +      hide: Map.get(params, "irreversible", nil), +      whole_word: Map.get(params, "boolean", true) +      # expires_at +    } + +    {:ok, response} = Pleroma.Filter.update(query) +    res = FilterView.render("filter.json", filter: response) +    json(conn, res) +  end + +  def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id} = params) do +    query = %Pleroma.Filter{ +      user_id: user.id, +      filter_id: filter_id +    } + +    {:ok, response} = Pleroma.Filter.delete(query) +    json(conn, %{}) +  end +    def errors(conn, _) do      conn      |> put_status(500) diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex index 174293906..bc628ba56 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex @@ -23,16 +23,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do                  "public:local:media",                  "user",                  "direct", -                "list" +                "list", +                "hashtag"                ] <- params["stream"] do        topic = if stream == "list", do: "list:#{params["list"]}", else: stream +      socket_stream = if stream == "hashtag", do: "hashtag:#{params["tag"]}", else: stream        socket =          socket          |> assign(:topic, topic)          |> assign(:user, user) -      Pleroma.Web.Streamer.add_socket(params["stream"], socket) +      Pleroma.Web.Streamer.add_socket(socket_stream, socket)        {:ok, socket}      else        _e -> :error diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 7bc32e688..85aac493f 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -13,6 +13,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do      image = User.avatar_url(user) |> MediaProxy.url()      header = User.banner_url(user) |> MediaProxy.url()      user_info = User.user_info(user) +    bot = (user.info["source_data"]["type"] || "Person") in ["Application", "Service"]      emojis =        (user.info["source_data"]["tag"] || []) @@ -26,6 +27,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do          }        end) +    fields = +      (user.info["source_data"]["attachment"] || []) +      |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end) +      |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end) +      %{        id: to_string(user.id),        username: username_from_nickname(user.nickname), @@ -43,7 +49,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do        header: header,        header_static: header,        emojis: emojis, -      fields: [], +      fields: fields, +      bot: bot,        source: %{          note: "",          privacy: user_info.default_scope, diff --git a/lib/pleroma/web/mastodon_api/views/filter_view.ex b/lib/pleroma/web/mastodon_api/views/filter_view.ex new file mode 100644 index 000000000..6bd687d46 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/views/filter_view.ex @@ -0,0 +1,27 @@ +defmodule Pleroma.Web.MastodonAPI.FilterView do +  use Pleroma.Web, :view +  alias Pleroma.Web.MastodonAPI.FilterView +  alias Pleroma.Web.CommonAPI.Utils + +  def render("filters.json", %{filters: filters} = opts) do +    render_many(filters, FilterView, "filter.json", opts) +  end + +  def render("filter.json", %{filter: filter}) do +    expires_at = +      if filter.expires_at do +        Utils.to_masto_date(filter.expires_at) +      else +        nil +      end + +    %{ +      id: to_string(filter.filter_id), +      phrase: filter.phrase, +      context: filter.context, +      expires_at: expires_at, +      irreversible: filter.hide, +      whole_word: false +    } +  end +end diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex index d95addb0b..67cef004a 100644 --- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex +++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex @@ -23,6 +23,8 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do      instance = Application.get_env(:pleroma, :instance)      media_proxy = Application.get_env(:pleroma, :media_proxy)      suggestions = Application.get_env(:pleroma, :suggestions) +    chat = Application.get_env(:pleroma, :chat) +    gopher = Application.get_env(:pleroma, :gopher)      stats = Stats.get_stats()      staff_accounts = @@ -59,7 +61,9 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do            timeout: Keyword.get(suggestions, :timeout, 5000),            web: Keyword.get(suggestions, :web, "")          }, -        staffAccounts: staff_accounts +        staffAccounts: staff_accounts, +        chat: Keyword.get(chat, :enabled), +        gopher: Keyword.get(gopher, :enabled)        }      } diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 927323794..b212a2909 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -154,6 +154,12 @@ defmodule Pleroma.Web.Router do      post("/domain_blocks", MastodonAPIController, :block_domain)      delete("/domain_blocks", MastodonAPIController, :unblock_domain) +    get("/filters", MastodonAPIController, :get_filters) +    post("/filters", MastodonAPIController, :create_filter) +    get("/filters/:id", MastodonAPIController, :get_filter) +    put("/filters/:id", MastodonAPIController, :update_filter) +    delete("/filters/:id", MastodonAPIController, :delete_filter) +      get("/suggestions", MastodonAPIController, :suggestions)    end diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 55b5287f5..909eefdd8 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -181,6 +181,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do    def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do      user = get_user(activity.data["actor"], opts)      liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) +    liked_activity_id = if liked_activity, do: liked_activity.id, else: nil      created_at =        activity.data["published"] @@ -197,7 +198,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do        "is_post_verb" => false,        "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",        "created_at" => created_at, -      "in_reply_to_status_id" => liked_activity.id, +      "in_reply_to_status_id" => liked_activity_id,        "external_url" => activity.data["id"],        "activity_type" => "like"      } | 
