Introduce group basic federation, event new page and notifications
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
47
lib/mobilizon/todos/todo.ex
Normal file
47
lib/mobilizon/todos/todo.ex
Normal file
@@ -0,0 +1,47 @@
|
||||
defmodule Mobilizon.Todos.Todo do
|
||||
@moduledoc """
|
||||
Represents a todo, or task
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Mobilizon.Storage.Ecto, only: [ensure_url: 2]
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Todos.TodoList
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
status: boolean(),
|
||||
title: String.t(),
|
||||
due_date: DateTime.t(),
|
||||
todo_list: TodoList.t(),
|
||||
creator: Actor.t(),
|
||||
assigned_to: Actor.t(),
|
||||
local: boolean
|
||||
}
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
schema "todos" do
|
||||
field(:status, :boolean, default: false)
|
||||
field(:title, :string)
|
||||
field(:url, :string)
|
||||
field(:due_date, :utc_datetime)
|
||||
field(:local, :boolean, default: true)
|
||||
belongs_to(:todo_list, TodoList, type: :binary_id)
|
||||
belongs_to(:creator, Actor)
|
||||
belongs_to(:assigned_to, Actor)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@required_attrs [:title, :creator_id, :url, :todo_list_id]
|
||||
@optional_attrs [:status, :due_date, :assigned_to_id, :local]
|
||||
@attrs @required_attrs ++ @optional_attrs
|
||||
|
||||
@doc false
|
||||
def changeset(todo, attrs) do
|
||||
todo
|
||||
|> cast(attrs, @attrs)
|
||||
|> ensure_url(:todo)
|
||||
|> validate_required(@required_attrs)
|
||||
end
|
||||
end
|
||||
42
lib/mobilizon/todos/todo_list.ex
Normal file
42
lib/mobilizon/todos/todo_list.ex
Normal file
@@ -0,0 +1,42 @@
|
||||
defmodule Mobilizon.Todos.TodoList do
|
||||
@moduledoc """
|
||||
Represents a todo list, or task list
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Mobilizon.Storage.Ecto, only: [ensure_url: 2]
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Todos.Todo
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
title: String.t(),
|
||||
todos: [Todo.t()],
|
||||
actor: Actor.t(),
|
||||
local: boolean
|
||||
}
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
schema "todo_lists" do
|
||||
field(:title, :string)
|
||||
field(:url, :string)
|
||||
field(:local, :boolean, default: true)
|
||||
|
||||
belongs_to(:actor, Actor)
|
||||
has_many(:todos, Todo)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@required_attrs [:title, :url, :actor_id]
|
||||
@optional_attrs [:local]
|
||||
@attrs @required_attrs ++ @optional_attrs
|
||||
|
||||
@doc false
|
||||
def changeset(todo_list, attrs) do
|
||||
todo_list
|
||||
|> cast(attrs, @attrs)
|
||||
|> ensure_url(:todo_list)
|
||||
|> validate_required(@required_attrs)
|
||||
end
|
||||
end
|
||||
109
lib/mobilizon/todos/todos.ex
Normal file
109
lib/mobilizon/todos/todos.ex
Normal file
@@ -0,0 +1,109 @@
|
||||
defmodule Mobilizon.Todos do
|
||||
@moduledoc """
|
||||
The Todos context.
|
||||
"""
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Storage.{Page, Repo}
|
||||
alias Mobilizon.Todos.{Todo, TodoList}
|
||||
import Ecto.Query
|
||||
|
||||
@doc """
|
||||
Get a todo list by it's ID
|
||||
"""
|
||||
@spec get_todo_list(integer | String.t()) :: TodoList.t() | nil
|
||||
def get_todo_list(id), do: Repo.get(TodoList, id)
|
||||
|
||||
@doc """
|
||||
Get a todo list by it's URL
|
||||
"""
|
||||
@spec get_todo_list_by_url(String.t()) :: TodoList.t() | nil
|
||||
def get_todo_list_by_url(url), do: Repo.get_by(TodoList, url: url)
|
||||
|
||||
@doc """
|
||||
Returns the list of todo lists for a group.
|
||||
"""
|
||||
@spec get_todo_lists_for_group(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
||||
def get_todo_lists_for_group(%Actor{id: group_id, type: :Group}, page \\ nil, limit \\ nil) do
|
||||
TodoList
|
||||
|> where(actor_id: ^group_id)
|
||||
|> order_by(desc: :updated_at)
|
||||
|> Page.build_page(page, limit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the list of todos for a group.
|
||||
"""
|
||||
@spec get_todos_for_todo_list(TodoList.t(), integer | nil, integer | nil) :: Page.t()
|
||||
def get_todos_for_todo_list(%TodoList{id: todo_list_id}, page \\ nil, limit \\ nil) do
|
||||
Todo
|
||||
|> where(todo_list_id: ^todo_list_id)
|
||||
|> order_by(asc: :status)
|
||||
# |> order_by(desc: :updated_at)
|
||||
|> Page.build_page(page, limit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a todo list.
|
||||
"""
|
||||
@spec create_todo_list(map) :: {:ok, TodoList.t()} | {:error, Ecto.Changeset.t()}
|
||||
def create_todo_list(attrs \\ %{}) do
|
||||
%TodoList{}
|
||||
|> TodoList.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a todo list.
|
||||
"""
|
||||
@spec update_todo_list(TodoList.t(), map) ::
|
||||
{:ok, TodoList.t()} | {:error, Ecto.Changeset.t()}
|
||||
def update_todo_list(%TodoList{} = todo_list, attrs) do
|
||||
todo_list
|
||||
|> TodoList.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a todo list
|
||||
"""
|
||||
@spec delete_todo_list(TodoList.t()) :: {:ok, TodoList.t()} | {:error, Ecto.Changeset.t()}
|
||||
def delete_todo_list(%TodoList{} = todo_list), do: Repo.delete(todo_list)
|
||||
|
||||
@doc """
|
||||
Get a todo by it's ID
|
||||
"""
|
||||
@spec get_todo(integer | String.t()) :: Todo.t() | nil
|
||||
def get_todo(id), do: Repo.get(Todo, id)
|
||||
|
||||
@doc """
|
||||
Get a todo by it's URL
|
||||
"""
|
||||
@spec get_todo_by_url(String.t()) :: Todo.t() | nil
|
||||
def get_todo_by_url(url), do: Repo.get_by(Todo, url: url)
|
||||
|
||||
@doc """
|
||||
Creates a todo.
|
||||
"""
|
||||
@spec create_todo(map) :: {:ok, Todo.t()} | {:error, Ecto.Changeset.t()}
|
||||
def create_todo(attrs \\ %{}) do
|
||||
%Todo{}
|
||||
|> Todo.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a todo.
|
||||
"""
|
||||
@spec update_todo(Todo.t(), map) :: {:ok, Todo.t()} | {:error, Ecto.Changeset.t()}
|
||||
def update_todo(%Todo{} = todo, attrs) do
|
||||
todo
|
||||
|> Todo.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a todo
|
||||
"""
|
||||
@spec delete_todo(Todo.t()) :: {:ok, Todo.t()} | {:error, Ecto.Changeset.t()}
|
||||
def delete_todo(%Todo{} = todo), do: Repo.delete(todo)
|
||||
end
|
||||
Reference in New Issue
Block a user