diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/stats.ex | 15 | ||||
| -rw-r--r-- | lib/pleroma/web/ostatus/ostatus.ex | 7 | ||||
| -rw-r--r-- | lib/pleroma/web/router.ex | 13 | ||||
| -rw-r--r-- | lib/pleroma/web/templates/twitter_api/util/follow.html.eex | 11 | ||||
| -rw-r--r-- | lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex | 14 | ||||
| -rw-r--r-- | lib/pleroma/web/templates/twitter_api/util/followed.html.eex | 6 | ||||
| -rw-r--r-- | lib/pleroma/web/twitter_api/controllers/util_controller.ex | 48 | ||||
| -rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 10 | ||||
| -rw-r--r-- | lib/pleroma/web/web_finger/web_finger.ex | 3 | 
9 files changed, 112 insertions, 15 deletions
| diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex index 938e5c343..0484f1a2d 100644 --- a/lib/pleroma/stats.ex +++ b/lib/pleroma/stats.ex @@ -5,7 +5,7 @@ defmodule Pleroma.Stats do    def start_link do      agent = Agent.start_link(fn -> {[], %{}} end, name: __MODULE__) -    schedule_update() +    spawn(fn -> schedule_update() end)      agent    end @@ -18,23 +18,22 @@ defmodule Pleroma.Stats do    end    def schedule_update do -    update_stats()      spawn(fn ->        Process.sleep(1000 * 60 * 60 * 1) # 1 hour        schedule_update()      end) +    update_stats()    end    def update_stats do      peers = from(u in Pleroma.User, -      select: fragment("?->'host'", u.info), +      select: fragment("distinct ?->'host'", u.info),        where: u.local != ^true) -    |> Repo.all() |> Enum.uniq() +    |> Repo.all()      domain_count = Enum.count(peers) -    status_query = from p in Activity, -      where: p.local == ^true, -      where: fragment("?->'object'->>'type' = ?", p.data, ^"Note") -    status_count = Repo.aggregate(status_query, :count, :id) +    status_query = from(u in User.local_user_query, +      select: fragment("sum((?->>'note_count')::int)", u.info)) +    status_count = Repo.one(status_query) |> IO.inspect      user_count = Repo.aggregate(User.local_user_query, :count, :id)      Agent.update(__MODULE__, fn _ ->        {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}} diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 745539b3e..c35ba42be 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -22,6 +22,10 @@ defmodule Pleroma.Web.OStatus do      "#{user.ap_id}/salmon"    end +  def remote_follow_path do +    "#{Web.base_url}/ostatus_subscribe?acct={uri}" +  end +    def handle_incoming(xml_string) do      with doc when doc != :error <- parse_document(xml_string) do        entries = :xmerl_xpath.string('//entry', doc) @@ -159,8 +163,7 @@ defmodule Pleroma.Web.OStatus do      Get the cw that mastodon uses.    """    def get_cw(entry) do -    with scope when not is_nil(scope) <- string_from_xpath("//mastodon:scope", entry), -         cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do +    with cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do        cw      else _e -> nil      end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 09104fc86..63dbd6245 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -28,6 +28,13 @@ defmodule Pleroma.Web.Router do      plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true}    end +  pipeline :pleroma_html do +    plug :accepts, ["html"] +    plug :fetch_session +    plug Pleroma.Plugs.OAuthPlug +    plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} +  end +    pipeline :well_known do      plug :accepts, ["xml", "xrd+xml"]    end @@ -51,6 +58,12 @@ defmodule Pleroma.Web.Router do      get "/emoji", UtilController, :emoji    end +  scope "/", Pleroma.Web.TwitterAPI do +    pipe_through :pleroma_html +    get "/ostatus_subscribe", UtilController, :remote_follow +    post "/ostatus_subscribe", UtilController, :do_remote_follow +  end +    scope "/api/pleroma", Pleroma.Web.TwitterAPI do      pipe_through :authenticated_api      post "/follow_import", UtilController, :follow_import diff --git a/lib/pleroma/web/templates/twitter_api/util/follow.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex new file mode 100644 index 000000000..06359fa6c --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex @@ -0,0 +1,11 @@ +<%= if @error == :error do %> +    <h2>Error fetching user</h2> +<% else %> +    <h2>Remote follow</h2> +    <img width="128" height="128" src="<%= @avatar %>"> +    <p><%= @name %></p> +    <%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "user"], fn f -> %> +    <%= hidden_input f, :id, value: @id %> +    <%= submit "Authorize" %> +    <% end %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex new file mode 100644 index 000000000..4e3a2be67 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex @@ -0,0 +1,14 @@ +<%= if @error do %> +    <h2><%= @error %></h2> +<% end %> +<h2>Log in to follow</h2> +<p><%= @name %></p> +<img height="128" width="128" src="<%= @avatar %>"> +<%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "authorization"], fn f -> %> +<%= text_input f, :name, placeholder: "Username" %> +<br> +<%= password_input f, :password, placeholder: "Password" %> +<br> +<%= hidden_input f, :id, value: @id %> +<%= submit "Authorize" %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/followed.html.eex b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex new file mode 100644 index 000000000..da473d502 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex @@ -0,0 +1,6 @@ +<%= if @error do %> +<p>Error following account</p> +<% else %> +<h2>Account followed!</h2> +<% end %> + diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 9d5654995..a1d56e3ab 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -2,6 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do    use Pleroma.Web, :controller    require Logger    alias Pleroma.Web +  alias Pleroma.Web.OStatus +  alias Comeonin.Pbkdf2    alias Pleroma.Formatter    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.{Repo, PasswordResetToken, User} @@ -30,6 +32,52 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do      json(conn, "ok")    end +  def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do +    {err, followee} = OStatus.find_or_make_user(acct) +    avatar = User.avatar_url(followee) +    name = followee.nickname +    id = followee.id + +    if !!user do +      conn +      |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id}) +    else +      conn +      |> render("follow_login.html", %{error: false, acct: acct, avatar: avatar, name: name, id: id}) +    end +  end + +  def do_remote_follow(conn, %{"authorization" => %{"name" => username, "password" => password, "id" => id}}) do +    followee = Repo.get(User, id) +    avatar = User.avatar_url(followee) +    name = followee.nickname +    with %User{} = user <- User.get_cached_by_nickname(username), +         true <- Pbkdf2.checkpw(password, user.password_hash), +           %User{} = followed <- Repo.get(User, id), +         {:ok, follower} <- User.follow(user, followee), +         {:ok, _activity} <- ActivityPub.follow(follower, followee) do +      conn +      |> render("followed.html", %{error: false}) +    else +      _e -> +        conn +        |> render("follow_login.html", %{error: "Wrong username or password", id: id, name: name, avatar: avatar}) +    end +  end +  def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do +    with %User{} = followee <- Repo.get(User, id), +         {:ok, follower} <- User.follow(user, followee), +         {:ok, _activity} <- ActivityPub.follow(follower, followee) do +      conn +      |> render("followed.html", %{error: false}) +    else +      e -> +        Logger.debug("Remote follow failed with error #{inspect e}") +      conn +      |> render("followed.html", %{error: inspect(e)}) +    end +  end +    @instance Application.get_env(:pleroma, :instance)    def config(conn, _params) do      case get_format(conn) do diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index d04a81cd4..faecebde0 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -316,10 +316,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do    def get_external_profile(for_user, uri) do      with {:ok, %User{} = user} <- OStatus.find_or_make_user(uri) do -      with url <- user.info["topic"], -           {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do -        OStatus.handle_incoming(body) -      end +      spawn(fn -> +        with url <- user.info["topic"], +             {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do +          OStatus.handle_incoming(body) +        end +      end)        {:ok, UserView.render("show.json", %{user: user, for: for_user})}      else _e ->          {:error, "Couldn't find user"} diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 026d2f98b..4ae3eab59 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -44,7 +44,8 @@ defmodule Pleroma.Web.WebFinger do          {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},          {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},          {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}}, -        {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}} +        {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}, +        {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}        ]      }      |> XmlBuilder.to_doc | 
