-defmodule Pleroma.Web.OStatus do
- @httpoison Application.get_env(:pleroma, :httpoison)
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+defmodule Pleroma.Web.OStatus do
import Ecto.Query
import Pleroma.Web.XML
require Logger
- alias Pleroma.{Repo, User, Web, Object, Activity, Formatter}
+ alias Pleroma.Activity
+ alias Pleroma.HTTP
+ alias Pleroma.Object
+ alias Pleroma.Repo
+ alias Pleroma.User
+ alias Pleroma.Web
alias Pleroma.Web.ActivityPub.ActivityPub
- alias Pleroma.Web.{WebFinger, Websub, MediaProxy}
- alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler}
alias Pleroma.Web.ActivityPub.Transmogrifier
- alias Phoenix.HTML
+ alias Pleroma.Web.ActivityPub.Visibility
+ alias Pleroma.Web.OStatus.DeleteHandler
+ alias Pleroma.Web.OStatus.FollowHandler
+ alias Pleroma.Web.OStatus.NoteHandler
+ alias Pleroma.Web.OStatus.UnfollowHandler
+ alias Pleroma.Web.WebFinger
+ alias Pleroma.Web.Websub
- def is_representable?(%Activity{data: data}) do
- object = Object.normalize(data["object"])
+ def is_representable?(%Activity{} = activity) do
+ object = Object.normalize(activity)
cond do
is_nil(object) ->
false
- object.data["type"] == "Note" ->
+ Visibility.is_public?(activity) && object.data["type"] == "Note" ->
true
true ->
end
end
- def metadata(activity, user, url) do
- Enum.concat([
- if(meta_enabled?(:opengraph), do: opengraph_tags(activity, user), else: []),
- if(meta_enabled?(:oembed), do: oembed_links(url), else: [])
- ])
- |> Enum.map(&to_tag/1)
- |> Enum.map(&HTML.safe_to_string/1)
- |> Enum.join("\n")
- end
-
- def meta_enabled?(type) do
- config = Pleroma.Config.get(:metadata, [])
- Keyword.get(config, type, false)
- end
-
- def to_tag(data) do
- with {name, attrs, _content = []} <- data do
- HTML.Tag.tag(name, attrs)
- else
- {name, attrs, content} ->
- HTML.Tag.content_tag(name, content, attrs)
-
- _ ->
- raise ArgumentError, message: "make_tag invalid args"
- end
- end
-
- def oembed_links(url) do
- Enum.map(["xml", "json"], fn format ->
- href = HTML.raw(oembed_path(url, format))
- { :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] }
- end)
- end
-
- def opengraph_tags(activity, user) do
- with image = User.avatar_url(user) |> MediaProxy.url(),
- truncated_content = Formatter.truncate(activity.data["object"]["content"]),
- domain = Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN") do
- [
- {:meta,
- [
- property: "og:title",
- content: "#{user.name} (@#{user.nickname}@#{domain}) post ##{activity.id}"
- ], []},
- {:meta, [property: "og:url", content: activity.data["id"]], []},
- {:meta, [property: "og:description", content: truncated_content],
- []},
- {:meta, [property: "og:image", content: image], []},
- {:meta, [property: "og:image:width", content: 120], []},
- {:meta, [property: "og:image:height", content: 120], []},
- {:meta, [property: "twitter:card", content: "summary"], []}
- ]
- end
- end
-
def feed_path(user) do
"#{user.ap_id}/feed.atom"
end
"#{Web.base_url()}/ostatus_subscribe?acct={uri}"
end
- def oembed_path(url, format) do
- query = URI.encode_query(%{url: url, format: format})
- "#{Web.base_url()}/oembed?#{query}"
- end
-
def handle_incoming(xml_string) do
with doc when doc != :error <- parse_document(xml_string) do
+ with {:ok, actor_user} <- find_make_or_update_user(doc),
+ do: Pleroma.Instances.set_reachable(actor_user.ap_id)
+
entries = :xmerl_xpath.string('//entry', doc)
activities =
def make_share(entry, doc, retweeted_activity) do
with {:ok, actor} <- find_make_or_update_user(doc),
- %Object{} = object <- Object.normalize(retweeted_activity.data["object"]),
+ %Object{} = object <- Object.normalize(retweeted_activity),
id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
{:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
{:ok, activity}
def make_favorite(entry, doc, favorited_activity) do
with {:ok, actor} <- find_make_or_update_user(doc),
- %Object{} = object <- Object.normalize(favorited_activity.data["object"]),
+ %Object{} = object <- Object.normalize(favorited_activity),
id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
{:ok, activity, _object} = ActivityPub.like(actor, object, id, false) do
{:ok, activity}
Logger.debug("Trying to get entry from db")
with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
- %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
+ %Activity{} = activity <- Activity.get_create_by_object_ap_id_with_object(id) do
{:ok, activity}
else
_ ->
}
with false <- update,
- %User{} = user <- User.get_by_ap_id(data.ap_id) do
+ %User{} = user <- User.get_cached_by_ap_id(data.ap_id) do
{:ok, user}
else
_e -> User.insert_or_update_user(data)
def fetch_activity_from_atom_url(url) do
with true <- String.starts_with?(url, "http"),
{:ok, %{body: body, status: code}} when code in 200..299 <-
- @httpoison.get(
+ HTTP.get(
url,
[{:Accept, "application/atom+xml"}]
) do
Logger.debug("Trying to fetch #{url}")
with true <- String.starts_with?(url, "http"),
- {:ok, %{body: body}} <- @httpoison.get(url, []),
+ {:ok, %{body: body}} <- HTTP.get(url, []),
{:ok, atom_url} <- get_atom_url(body) do
fetch_activity_from_atom_url(atom_url)
else