Add user timeline
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
index e1b6f248713c5fadbaeff35200d48d2f65dd4e6c..0641200312e86f02b31bf34919f47c4f1f68c79d 100644 (file)
@@ -6,8 +6,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   import Ecto.Query
 
   def create_status(user = %User{}, data = %{}) do
-    date = DateTime.utc_now() |> DateTime.to_iso8601
-
     attachments = Enum.map(data["media_ids"] || [], fn (media_id) ->
       Repo.get(Object, media_id).data
     end)
@@ -27,6 +25,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
 
     content_html = add_user_links(content, mentions)
 
+    date = make_date()
+
     activity = %{
       "type" => "Create",
       "to" => to,
@@ -69,7 +69,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   end
 
   def fetch_friend_statuses(user, opts \\ %{}) do
-    ActivityPub.fetch_activities(user.following, opts)
+    ActivityPub.fetch_activities([user.ap_id | user.following], opts)
     |> activities_to_statuses(%{for: user})
   end
 
@@ -78,6 +78,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     |> activities_to_statuses(%{for: user})
   end
 
+  def fetch_user_statuses(user, opts \\ %{}) do
+    target = get_user(user, opts)
+    ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id}))
+    |> activities_to_statuses(%{for: user})
+  end
+
   def fetch_conversation(user, id) do
     query = from activity in Activity,
       where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
@@ -107,7 +113,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
          { :ok, activity } <- ActivityPub.insert(%{
            "type" => "Follow",
            "actor" => follower.ap_id,
-           "object" => followed.ap_id
+           "object" => followed.ap_id,
+           "published" => make_date()
          })
     do
       { :ok, follower, followed, activity }
@@ -183,4 +190,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
     ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
   end
+
+  defp make_date do
+    DateTime.utc_now() |> DateTime.to_iso8601
+  end
+
+  defp get_user(user, params) do
+    case params do
+      %{ "user_id" => user_id } ->
+        Repo.get(User, user_id)
+      %{ "screen_name" => nickname } ->
+        Repo.get_by!(User, nickname: nickname)
+      _ ->
+        user
+    end
+  end
 end