Add relations
This commit is contained in:
parent
f43b4bd038
commit
3a57d74357
12 changed files with 81 additions and 85 deletions
|
@ -1,7 +1,8 @@
|
||||||
defmodule Nulla.Models.Activity do
|
defmodule Nulla.Models.Activity do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
alias Nulla.SnowFlake
|
alias Nulla.Repo
|
||||||
|
alias Nulla.Snowflake
|
||||||
|
|
||||||
@primary_key {:id, :integer, autogenerate: false}
|
@primary_key {:id, :integer, autogenerate: false}
|
||||||
schema "activities" do
|
schema "activities" do
|
||||||
|
@ -27,7 +28,7 @@ defmodule Nulla.Models.Activity do
|
||||||
|
|
||||||
%__MODULE__{}
|
%__MODULE__{}
|
||||||
|> __MODULE__.changeset(attrs)
|
|> __MODULE__.changeset(attrs)
|
||||||
|> Ecto.Changeset.put_change(:id, id)
|
|> Changeset.put_change(:id, id)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -106,7 +106,7 @@ defmodule Nulla.Models.Actor do
|
||||||
|
|
||||||
%__MODULE__{}
|
%__MODULE__{}
|
||||||
|> changeset(attrs)
|
|> changeset(attrs)
|
||||||
|> Ecto.Changeset.put_change(:id, id)
|
|> Changeset.put_change(:id, id)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
defmodule Nulla.Models.Follow do
|
|
||||||
use Ecto.Schema
|
|
||||||
import Ecto.Changeset
|
|
||||||
alias Nulla.Repo
|
|
||||||
alias Nulla.Snowflake
|
|
||||||
alias Nulla.Models.Actor
|
|
||||||
|
|
||||||
@primary_key {:id, :integer, autogenerate: false}
|
|
||||||
schema "follows" do
|
|
||||||
belongs_to :follower, Actor
|
|
||||||
belongs_to :followed, Actor
|
|
||||||
|
|
||||||
timestamps()
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc false
|
|
||||||
def changeset(follow, attrs) do
|
|
||||||
follow
|
|
||||||
|> cast(attrs, [:user_id, :target_id])
|
|
||||||
|> validate_required([:user_id, :target_id])
|
|
||||||
|> unique_constraint([:user_id, :target_id])
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_follow(attrs) do
|
|
||||||
id = Snowflake.next_id()
|
|
||||||
|
|
||||||
%__MODULE__{}
|
|
||||||
|> __MODULE__.changeset(attrs)
|
|
||||||
|> Ecto.Changeset.put_change(:id, id)
|
|
||||||
|> Repo.insert()
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Nulla.Models.MediaAttachment do
|
defmodule Nulla.Models.MediaAttachment do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
alias Nulla.Models.Note
|
||||||
|
|
||||||
@primary_key {:id, :integer, autogenerate: false}
|
@primary_key {:id, :integer, autogenerate: false}
|
||||||
schema "media_attachments" do
|
schema "media_attachments" do
|
||||||
|
@ -8,7 +9,7 @@ defmodule Nulla.Models.MediaAttachment do
|
||||||
field :mime_type, :string
|
field :mime_type, :string
|
||||||
field :description, :string
|
field :description, :string
|
||||||
|
|
||||||
belongs_to :note, Nulla.Models.Note
|
belongs_to :note, Note
|
||||||
|
|
||||||
timestamps(type: :utc_datetime)
|
timestamps(type: :utc_datetime)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Nulla.Models.ModerationLog do
|
defmodule Nulla.Models.ModerationLog do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
alias Nulla.Models.User
|
||||||
|
|
||||||
@primary_key {:id, :integer, autogenerate: false}
|
@primary_key {:id, :integer, autogenerate: false}
|
||||||
schema "moderation_logs" do
|
schema "moderation_logs" do
|
||||||
|
@ -10,7 +11,7 @@ defmodule Nulla.Models.ModerationLog do
|
||||||
field :reason, :string
|
field :reason, :string
|
||||||
field :metadata, :map
|
field :metadata, :map
|
||||||
|
|
||||||
belongs_to :moderator, Nulla.Models.User
|
belongs_to :moderator, User
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,30 +1,62 @@
|
||||||
defmodule Nulla.Models.Relation do
|
defmodule Nulla.Models.Relation do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
alias Nulla.Repo
|
||||||
|
alias Nulla.Snowflake
|
||||||
alias Nulla.Models.Actor
|
alias Nulla.Models.Actor
|
||||||
alias Nulla.Models.Activity
|
|
||||||
|
|
||||||
@primary_key {:id, :integer, autogenerate: false}
|
@primary_key {:id, :integer, autogenerate: false}
|
||||||
|
@foreign_key_type :integer
|
||||||
schema "relations" do
|
schema "relations" do
|
||||||
field :type, :string
|
field :following, :boolean, default: false
|
||||||
field :status, :string
|
field :followed_by, :boolean, default: false
|
||||||
|
field :showing_replies, :boolean, default: true
|
||||||
|
field :showing_reblogs, :boolean, default: true
|
||||||
|
field :notifying, :boolean, default: false
|
||||||
|
field :muting, :boolean, default: false
|
||||||
|
field :muting_notifications, :boolean, default: false
|
||||||
|
field :blocking, :boolean, default: false
|
||||||
|
field :blocked_by, :boolean, default: false
|
||||||
|
field :domain_blocking, :boolean, default: false
|
||||||
|
field :requested, :boolean, default: false
|
||||||
|
field :note, :string
|
||||||
|
|
||||||
belongs_to :source, Actor, foreign_key: :source_id, type: :integer
|
belongs_to :local_actor_id, Actor
|
||||||
belongs_to :target, Actor, foreign_key: :target_id, type: :integer
|
belongs_to :remote_actor_id, Actor
|
||||||
belongs_to :activity, Activity, foreign_key: :activity_id, type: :integer
|
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
def changeset(relation, attrs) do
|
def changeset(relation, attrs) do
|
||||||
relation
|
relation
|
||||||
|> cast(attrs, [:id, :source_id, :target_id, :type, :status, :activity_id])
|
|> cast(attrs, [
|
||||||
|> validate_required([:id, :source_id, :target_id, :type])
|
:id,
|
||||||
|> validate_inclusion(:type, ~w(follow block mute friend_request))
|
:following,
|
||||||
|> validate_inclusion(:status, ~w(pending accepted rejected active))
|
:followed_by,
|
||||||
|> foreign_key_constraint(:source_id)
|
:showing_replies,
|
||||||
|> foreign_key_constraint(:target_id)
|
:showing_reblogs,
|
||||||
|> foreign_key_constraint(:activity_id)
|
:notifying,
|
||||||
|> unique_constraint([:source_id, :target_id, :type])
|
:muting,
|
||||||
|
:muting_notifications,
|
||||||
|
:blocking,
|
||||||
|
:blocked_by,
|
||||||
|
:domain_blocking,
|
||||||
|
:requested,
|
||||||
|
:note,
|
||||||
|
:source_id,
|
||||||
|
:target_id
|
||||||
|
])
|
||||||
|
|> validate_required([:id, :local_actor_id, :remote_actor_id])
|
||||||
|
|> unique_constraint([:local_actor_id, :remote_actor_id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_relation(attrs) do
|
||||||
|
id = Snowflake.next_id()
|
||||||
|
|
||||||
|
%__MODULE__{}
|
||||||
|
|> __MODULE__.changeset(attrs)
|
||||||
|
|> Changeset.put_change(:id, id)
|
||||||
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Nulla.Models.Session do
|
defmodule Nulla.Models.Session do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
alias Nulla.Models.User
|
||||||
|
|
||||||
@primary_key {:id, :integer, autogenerate: false}
|
@primary_key {:id, :integer, autogenerate: false}
|
||||||
schema "sessions" do
|
schema "sessions" do
|
||||||
|
@ -8,7 +9,7 @@ defmodule Nulla.Models.Session do
|
||||||
field :user_agent, :string
|
field :user_agent, :string
|
||||||
field :ip, :string
|
field :ip, :string
|
||||||
|
|
||||||
belongs_to :user, Nulla.Models.User
|
belongs_to :user, User
|
||||||
|
|
||||||
timestamps(type: :utc_datetime)
|
timestamps(type: :utc_datetime)
|
||||||
end
|
end
|
||||||
|
|
|
@ -69,7 +69,7 @@ defmodule Nulla.Models.User do
|
||||||
|
|
||||||
def update_last_active(user) do
|
def update_last_active(user) do
|
||||||
user
|
user
|
||||||
|> Ecto.Changeset.change(last_active_at: DateTime.utc_now())
|
|> Changeset.change(last_active_at: DateTime.utc_now())
|
||||||
|> Repo.update()
|
|> Repo.update()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
defmodule NullaWeb.ActorController do
|
defmodule NullaWeb.ActorController do
|
||||||
use NullaWeb, :controller
|
use NullaWeb, :controller
|
||||||
alias Nulla.ActivityPub
|
alias Nulla.ActivityPub
|
||||||
alias Nulla.Utils
|
|
||||||
alias Nulla.Models.Actor
|
alias Nulla.Models.Actor
|
||||||
alias Nulla.Models.Note
|
alias Nulla.Models.Note
|
||||||
alias Nulla.Models.InstanceSettings
|
alias Nulla.Models.InstanceSettings
|
||||||
|
|
|
@ -5,6 +5,7 @@ defmodule NullaWeb.InboxController do
|
||||||
alias Nulla.Utils
|
alias Nulla.Utils
|
||||||
alias Nulla.Models.Actor
|
alias Nulla.Models.Actor
|
||||||
alias Nulla.Models.Relation
|
alias Nulla.Models.Relation
|
||||||
|
alias Nulla.Models.Activity
|
||||||
|
|
||||||
def inbox(
|
def inbox(
|
||||||
conn,
|
conn,
|
||||||
|
@ -24,19 +25,19 @@ defmodule NullaWeb.InboxController do
|
||||||
Activity.create_activity(%{
|
Activity.create_activity(%{
|
||||||
ap_id: follow_id,
|
ap_id: follow_id,
|
||||||
type: "Follow",
|
type: "Follow",
|
||||||
actor: actor_uri,
|
actor: remote_actor.id,
|
||||||
object: target_uri
|
object: target_uri
|
||||||
}),
|
}),
|
||||||
accept_activity <-
|
accept_activity <-
|
||||||
Activity.create_activity(%{
|
Activity.create_activity(%{
|
||||||
type: "Accept",
|
type: "Accept",
|
||||||
actor: target_uri,
|
actor: target_actor.id,
|
||||||
object: follow_activity
|
object: follow_activity
|
||||||
}),
|
}),
|
||||||
relation <- Relation.create_relation(%{
|
relation <- Relation.create_relation(%{
|
||||||
id: 1,
|
followed_by: true,
|
||||||
follower: remote_actor.id,
|
local_actor_id: target_actor.id,
|
||||||
followed: target_actor.id
|
remote_actor_id: remote_actor.id
|
||||||
}) do
|
}) do
|
||||||
conn
|
conn
|
||||||
|> put_resp_content_type("application/activity+json")
|
|> put_resp_content_type("application/activity+json")
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
defmodule Nulla.Repo.Migrations.CreateFollows do
|
|
||||||
use Ecto.Migration
|
|
||||||
|
|
||||||
def change do
|
|
||||||
create table(:follows, primary_key: false) do
|
|
||||||
add :id, :bigint, primary_key: true
|
|
||||||
add :follower_id, references(:actors, on_delete: :delete_all), null: false
|
|
||||||
add :following_id, references(:actors, on_delete: :delete_all), null: false
|
|
||||||
|
|
||||||
timestamps()
|
|
||||||
end
|
|
||||||
|
|
||||||
create unique_index(:follows, [:follower_id, :following_id])
|
|
||||||
create index(:follows, [:following_id])
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -4,20 +4,28 @@ defmodule Nulla.Repo.Migrations.CreateActorRelations do
|
||||||
def change do
|
def change do
|
||||||
create table(:relations, primary_key: false) do
|
create table(:relations, primary_key: false) do
|
||||||
add :id, :bigint, primary_key: true
|
add :id, :bigint, primary_key: true
|
||||||
add :source_id, :bigint, null: false
|
add :following, :boolean, null: false, default: false
|
||||||
add :target_id, :bigint, null: false
|
add :followed_by, :boolean, null: false, default: false
|
||||||
add :type, :string, null: false
|
add :showing_replies, :boolean, null: false, default: true
|
||||||
add :status, :string, null: false
|
add :showing_reblogs, :boolean, null: false, default: true
|
||||||
add :activity_id, :bigint
|
add :notifying, :boolean, null: false, default: false
|
||||||
|
add :muting, :boolean, null: false, default: false
|
||||||
|
add :muting_notifications, :boolean, null: false, default: false
|
||||||
|
add :blocking, :boolean, null: false, default: false
|
||||||
|
add :blocked_by, :boolean, null: false, default: false
|
||||||
|
add :domain_blocking, :boolean, null: false, default: false
|
||||||
|
add :requested, :boolean, null: false, default: false
|
||||||
|
add :note, :string
|
||||||
|
|
||||||
|
add :local_actor_id, references(:actors, type: :bigint), null: false
|
||||||
|
add :remote_actor_id, references(:actors, type: :bigint), null: false
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index(:relations, [:source_id])
|
create index(:relations, [:local_actor_id])
|
||||||
create index(:relations, [:target_id])
|
create index(:relations, [:remote_actor_id])
|
||||||
create index(:relations, [:type])
|
|
||||||
create index(:relations, [:activity_id])
|
|
||||||
|
|
||||||
create unique_index(:relations, [:source_id, :target_id, :type])
|
create unique_index(:relations, [:local_actor_id, :remote_actor_id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue