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
def get_tags(entry) do
:xmerl_xpath.string('//category', entry)
- |> Enum.map(fn (category) -> string_from_xpath("/category/@term", category) end)
+ |> Enum.map(fn (category) -> string_from_xpath("/category/@term", category) |> String.downcase end)
+ end
+
+ def maybe_update(doc, user) do
+ old_data = %{
+ avatar: user.avatar,
+ bio: user.bio,
+ name: user.name
+ }
+
+ with false <- user.local,
+ avatar <- make_avatar_object(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
+ change = Ecto.Changeset.change(user, new_data)
+ Repo.update(change)
+ else e ->
+ {:ok, user}
+ end
end
def find_make_or_update_user(doc) do
uri = string_from_xpath("//author/uri[1]", doc)
with {:ok, user} <- find_or_make_user(uri) do
- avatar = make_avatar_object(doc)
- if !user.local && user.avatar != avatar do
- change = Ecto.Changeset.change(user, %{avatar: avatar})
- Repo.update(change)
- else
- {:ok, user}
- end
+ maybe_update(doc, user)
end
end
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 = %{
nickname: info["nickname"] <> "@" <> info["host"],
ap_id: info["uri"],
info: info,
- avatar: info["avatar"]
+ avatar: info["avatar"],
+ bio: info["bio"]
}
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
def fetch_activity_from_html_url(url) do
Logger.debug("Trying to fetch #{url}")
- with {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true),
+ with {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
{:ok, atom_url} <- get_atom_url(body),
- {:ok, %{status_code: code, body: body}} when code in 200..299 <- @httpoison.get(atom_url, [], follow_redirect: true) do
+ {:ok, %{status_code: code, body: body}} when code in 200..299 <- @httpoison.get(atom_url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do
Logger.debug("Got document from #{url}, handling...")
handle_incoming(body)
else e -> Logger.debug("Couldn't get #{url}: #{inspect(e)}")