X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fweb%2Fwebsub%2Fwebsub.ex;h=db1577a9373a71044263266a0b9443254dad2d94;hb=6dfa62800ad6cdcef9e73ecdabe45363c574a528;hp=848aac6156926b71e89c650f72df765c39d9e0e8;hpb=7269c51f3a4409110bc61faada08e35ce1b4d030;p=akkoma diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index 848aac615..db1577a93 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -9,9 +9,9 @@ defmodule Pleroma.Web.Websub do import Ecto.Query - @websub_verifier Application.get_env(:pleroma, :websub_verifier) + @httpoison Application.get_env(:pleroma, :httpoison) - def verify(subscription, getter \\ &HTTPoison.get/3) do + def verify(subscription, getter \\ &@httpoison.get/3) do challenge = Base.encode16(:crypto.strong_rand_bytes(8)) lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.updated_at) lease_seconds = lease_seconds |> to_string @@ -31,14 +31,15 @@ defmodule Pleroma.Web.Websub do do changeset = Changeset.change(subscription, %{state: "active"}) Repo.update(changeset) - else _e -> - changeset = Changeset.change(subscription, %{state: "rejected"}) - {:ok, subscription} = Repo.update(changeset) + else e -> + Logger.debug("Couldn't verify subscription") + Logger.debug(inspect(e)) {:error, subscription} end end def publish(topic, user, activity) do + # TODO: Only send to still valid subscriptions. query = from sub in WebsubServerSubscription, where: sub.topic == ^topic and sub.state == "active" subscriptions = Repo.all(query) @@ -48,13 +49,13 @@ defmodule Pleroma.Web.Websub do |> :xmerl.export_simple(:xmerl_xml) |> to_string - signature = sign(sub.secret || "", response) - Logger.debug(fn -> "Pushing to #{sub.callback}" end) - - HTTPoison.post(sub.callback, response, [ - {"Content-Type", "application/atom+xml"}, - {"X-Hub-Signature", "sha1=#{signature}"} - ]) + data = %{ + xml: response, + topic: topic, + callback: sub.callback, + secret: sub.secret + } + Pleroma.Web.Federator.enqueue(:publish_single_websub, data) end) end @@ -124,7 +125,7 @@ defmodule Pleroma.Web.Websub do topic = subscribed.info["topic"] # FIXME: Race condition, use transactions {:ok, subscription} = with subscription when not is_nil(subscription) <- Repo.get_by(WebsubClientSubscription, topic: topic) do - subscribers = [subscriber.ap_id, subscription.subscribers] |> Enum.uniq + subscribers = [subscriber.ap_id | subscription.subscribers] |> Enum.uniq change = Ecto.Changeset.change(subscription, %{subscribers: subscribers}) Repo.update(change) else _e -> @@ -141,7 +142,7 @@ defmodule Pleroma.Web.Websub do requester.(subscription) end - def gather_feed_data(topic, getter \\ &HTTPoison.get/1) do + def gather_feed_data(topic, getter \\ &@httpoison.get/1) do with {:ok, response} <- getter.(topic), status_code when status_code in 200..299 <- response.status_code, body <- response.body, @@ -153,6 +154,7 @@ defmodule Pleroma.Web.Websub do preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc) displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc) avatar = OStatus.make_avatar_object(doc) + bio = XML.string_from_xpath("/feed/author[1]/summary", doc) {:ok, %{ "uri" => uri, @@ -160,14 +162,15 @@ defmodule Pleroma.Web.Websub do "nickname" => preferredUsername || name, "name" => displayName || name, "host" => URI.parse(uri).host, - "avatar" => avatar + "avatar" => avatar, + "bio" => bio }} else e -> {:error, e} end end - def request_subscription(websub, poster \\ &HTTPoison.post/3, timeout \\ 10_000) do + def request_subscription(websub, poster \\ &@httpoison.post/3, timeout \\ 10_000) do data = [ "hub.mode": "subscribe", "hub.topic": websub.topic, @@ -202,4 +205,19 @@ defmodule Pleroma.Web.Websub do {:error, websub} end end + + def refresh_subscriptions(delta \\ 60 * 60 * 24) do + Logger.debug("Refreshing subscriptions") + + cut_off = NaiveDateTime.add(NaiveDateTime.utc_now, delta) + + query = from sub in WebsubClientSubscription, + where: sub.valid_until < ^cut_off + + subs = Repo.all(query) + + Enum.each(subs, fn (sub) -> + Pleroma.Web.Federator.enqueue(:request_subscription, sub) + end) + end end