Don't crash on activity handling problems.
[akkoma] / lib / pleroma / web / ostatus / ostatus.ex
index 7fd3a5eddd127bdcc82f09ce30828499518df3c0..d9a5924dc26237ef5874df518a049c985d67b141 100644 (file)
@@ -30,26 +30,36 @@ defmodule Pleroma.Web.OStatus do
     activities = Enum.map(entries, fn (entry) ->
       {:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
       {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
+      Logger.debug("Handling #{verb}")
 
-      case verb do
-        'http://activitystrea.ms/schema/1.0/follow' ->
-          with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
-        'http://activitystrea.ms/schema/1.0/share' ->
-          with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
-        'http://activitystrea.ms/schema/1.0/favorite' ->
-          with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc), do: [activity, favorited_activity]
-        _ ->
-          case object_type do
-            'http://activitystrea.ms/schema/1.0/note' ->
-              with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
-            'http://activitystrea.ms/schema/1.0/comment' ->
-              with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
-            _ ->
-              Logger.error("Couldn't parse incoming document")
-              nil
-          end
+      try do
+        case verb do
+          'http://activitystrea.ms/schema/1.0/follow' ->
+            with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
+          'http://activitystrea.ms/schema/1.0/share' ->
+            with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
+          'http://activitystrea.ms/schema/1.0/favorite' ->
+            with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc), do: [activity, favorited_activity]
+          _ ->
+            case object_type do
+              'http://activitystrea.ms/schema/1.0/note' ->
+                with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
+              'http://activitystrea.ms/schema/1.0/comment' ->
+                with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
+              _ ->
+                Logger.error("Couldn't parse incoming document")
+                nil
+            end
+        end
+      rescue
+        e ->
+          Logger.error("Error occured while handling activity")
+          Logger.error(inspect(e))
+          nil
       end
     end)
+    |> Enum.filter(&(&1))
+
     {:ok, activities}
   end
 
@@ -161,7 +171,7 @@ defmodule Pleroma.Web.OStatus do
 
     with false <- user.local,
          avatar <- make_avatar_object(doc),
-         bio when not is_nil(bio) <- string_from_xpath("//author[1]/summary", doc),
+         bio <- string_from_xpath("//author[1]/summary", doc),
          name when not is_nil(name) <- string_from_xpath("//author[1]/poco:displayName", doc),
          new_data <- %{avatar: avatar, name: name, bio: bio},
          false <- new_data == old_data do
@@ -192,6 +202,11 @@ defmodule Pleroma.Web.OStatus do
     end
   end
 
+  def insert_or_update_user(data) do
+    cs = User.remote_user_creation(data)
+    Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
+  end
+
   def make_user(uri) do
     with {:ok, info} <- gather_user_info(uri) do
       data = %{
@@ -204,9 +219,7 @@ defmodule Pleroma.Web.OStatus do
       }
       with %User{} = user <- User.get_by_ap_id(data.ap_id) do
         {:ok, user}
-      else _e ->
-        cs = User.remote_user_creation(data)
-        Repo.insert(cs)
+      else _e -> insert_or_update_user(data)
       end
     end
   end