Merge branch 'develop' into feature/activitypub
[akkoma] / lib / pleroma / web / websub / websub.ex
index a5abc303c1feb1e2f533b13a863d3fa3664ad6f1..47a01849df74bc8b5bec01ff4b66954f9e8cb752 100644 (file)
@@ -31,14 +31,23 @@ 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
+  @supported_activities [
+    "Create",
+    "Follow",
+    "Like",
+    "Announce",
+    "Undo",
+    "Delete"
+  ]
+  def publish(topic, user, %{data: %{"type" => type}} = activity) when type in @supported_activities 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,17 +57,16 @@ 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)
-
-      Task.start(fn ->
-        @httpoison.post(sub.callback, response, [
-              {"Content-Type", "application/atom+xml"},
-              {"X-Hub-Signature", "sha1=#{signature}"}
-            ])
-      end)
+      data = %{
+        xml: response,
+        topic: topic,
+        callback: sub.callback,
+        secret: sub.secret
+      }
+      Pleroma.Web.Federator.enqueue(:publish_single_websub, data)
     end)
   end
+  def publish(_,_,_), do: ""
 
   def sign(secret, doc) do
     :crypto.hmac(:sha, secret, to_string(doc)) |> Base.encode16 |> String.downcase
@@ -126,7 +134,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 ->
@@ -155,6 +163,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,
@@ -162,7 +171,8 @@ 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}
@@ -204,4 +214,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