alias Ecto.{Changeset, UUID}
import Ecto.Query
- def insert(map) when is_map(map) do
+ def insert(map, local \\ true) when is_map(map) do
map = map
|> Map.put_new_lazy("id", &generate_activity_id/0)
|> Map.put_new_lazy("published", &make_date/0)
map
end
- Repo.insert(%Activity{data: map})
+ Repo.insert(%Activity{data: map, local: local})
end
- def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
+ def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
+ published = published || make_date()
+
+ activity = %{
+ "type" => "Create",
+ "to" => to |> Enum.uniq,
+ "actor" => actor.ap_id,
+ "object" => object,
+ "published" => published,
+ "context" => context
+ }
+ |> Map.merge(additional)
+
+ with {:ok, activity} <- insert(activity, local) do
+ if actor.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity}
+ end
+ end
+
+ def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
cond do
# There's already a like here, so return the original activity.
ap_id in (object.data["likes"] || []) ->
"type" => "Like",
"actor" => ap_id,
"object" => id,
- "to" => [User.ap_followers(user), object.data["actor"]]
+ "to" => [User.ap_followers(user), object.data["actor"]],
+ "context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ {:ok, activity} = insert(data, local)
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
update_object_in_activities(object)
+ if user.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
{:ok, activity, object}
end
end
end
def generate_object_id do
- generate_id("objects")
+ Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :object, Ecto.UUID.generate)
end
def generate_id(type) do
query = from activity in query,
where: activity.id > ^since_id
+ query = if opts["local_only"] do
+ from activity in query, where: activity.local == true
+ else
+ query
+ end
+
query = if opts["max_id"] do
from activity in query, where: activity.id < ^opts["max_id"]
else
Enum.reverse(Repo.all(query))
end
- def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
+ def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
data = %{
"type" => "Announce",
"actor" => ap_id,
"object" => id,
- "to" => [User.ap_followers(user), object.data["actor"]]
+ "to" => [User.ap_followers(user), object.data["actor"]],
+ "context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ {:ok, activity} = insert(data, local)
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
update_object_in_activities(object)
+ if user.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
{:ok, activity, object}
end
+ def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, local \\ true) do
+ data = %{
+ "type" => "Follow",
+ "actor" => follower_id,
+ "to" => [followed_id],
+ "object" => followed_id,
+ "published" => make_date()
+ }
+
+ with {:ok, activity} <- insert(data, local) do
+ if actor_local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity}
+ end
+ end
+
+ def unfollow(follower, followed, local \\ true) do
+ with follow_activity when not is_nil(follow_activity) <- fetch_latest_follow(follower, followed) do
+ data = %{
+ "type" => "Undo",
+ "actor" => follower.ap_id,
+ "to" => [followed.ap_id],
+ "object" => follow_activity.data["id"],
+ "published" => make_date()
+ }
+
+ with {:ok, activity} <- insert(data, local) do
+ if follower.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
+ {:ok, activity}
+ end
+ end
+ end
+
def fetch_activities_for_context(context) do
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ context: context })
Repo.all(query)
end
+ def fetch_latest_follow(%User{ap_id: follower_id},
+ %User{ap_id: followed_id}) do
+ query = from activity in Activity,
+ where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
+ object: followed_id}),
+ order_by: [desc: :inserted_at],
+ limit: 1
+ Repo.one(query)
+ end
+
def upload(file) do
data = Upload.store(file)
Repo.insert(%Object{data: data})