- doc = parse_document(xml_string)
- entries = :xmerl_xpath.string('//entry', doc)
-
- activities = Enum.map(entries, fn (entry) ->
- {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
-
- case object_type do
- 'http://activitystrea.ms/schema/1.0/note' ->
- with {:ok, activity} <- handle_note(entry, doc), do: activity
- 'http://activitystrea.ms/schema/1.0/comment' ->
- with {:ok, activity} <- handle_note(entry, doc), do: activity
- _ ->
- Logger.error("Couldn't parse incoming document")
- nil
- end
- end)
- {:ok, activities}
+ with doc when doc != :error <- parse_document(xml_string) do
+ entries = :xmerl_xpath.string('//entry', doc)
+
+ 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}")
+
+ try do
+ case verb do
+ 'http://activitystrea.ms/schema/1.0/delete' ->
+ with {:ok, activity} <- DeleteHandler.handle_delete(entry, doc), do: activity
+
+ 'http://activitystrea.ms/schema/1.0/follow' ->
+ with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
+
+ 'http://activitystrea.ms/schema/1.0/unfollow' ->
+ with {:ok, activity} <- UnfollowHandler.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(xml_string)
+ Logger.error(inspect(e))
+ nil
+ end
+ end)
+ |> Enum.filter(& &1)
+
+ {:ok, activities}
+ else
+ _e -> {:error, []}
+ end
+ end
+
+ def make_share(entry, doc, retweeted_activity) do
+ with {:ok, actor} <- find_make_or_update_user(doc),
+ %Object{} = object <- Object.get_by_ap_id(retweeted_activity.data["object"]["id"]),
+ id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
+ {:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
+ {:ok, activity}
+ end