activity.ex 1.11 KB
Newer Older
lain's avatar
lain committed
1 2
defmodule Pleroma.Activity do
  use Ecto.Schema
3
  alias Pleroma.{Repo, Activity, Notification}
4
  import Ecto.Query
lain's avatar
lain committed
5 6 7

  schema "activities" do
    field :data, :map
8
    field :local, :boolean, default: true
9
    has_many :notifications, Notification
lain's avatar
lain committed
10 11 12

    timestamps()
  end
13 14 15

  def get_by_ap_id(ap_id) do
    Repo.one(from activity in Activity,
lain's avatar
lain committed
16
      where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)))
17 18
  end

19
  # Wrong name, only returns create activities
lain's avatar
lain committed
20 21 22 23 24
  def all_by_object_ap_id_q(ap_id) do
    from activity in Activity,
      where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id))
  end

25 26 27 28 29
  def all_non_create_by_object_ap_id_q(ap_id) do
    from activity in Activity,
      where: fragment("(?)->>'object' = ?", activity.data, ^to_string(ap_id))
  end

30
  def all_by_object_ap_id(ap_id) do
lain's avatar
lain committed
31
    Repo.all(all_by_object_ap_id_q(ap_id))
32
  end
33 34 35

  def get_create_activity_by_object_ap_id(ap_id) do
    Repo.one(from activity in Activity,
lain's avatar
lain committed
36 37
      where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id))
             and fragment("(?)->>'type' = 'Create'", activity.data))
38
  end
lain's avatar
lain committed
39
end