Update note
This commit is contained in:
parent
02bdaef80d
commit
20a3ed9e71
1 changed files with 48 additions and 6 deletions
|
@ -27,28 +27,70 @@ defmodule Nulla.Models.Note do
|
|||
@doc false
|
||||
def changeset(note, attrs) do
|
||||
note
|
||||
|> cast(attrs, [:content, :visibility, :sensitive, :language, :inReplyTo, :actor_id])
|
||||
|> validate_required([:content, :visibility, :sensitive, :language, :actor_id])
|
||||
|> cast(attrs, [
|
||||
:inReplyTo,
|
||||
:published,
|
||||
:url,
|
||||
:to,
|
||||
:cc,
|
||||
:sensitive,
|
||||
:content,
|
||||
:language,
|
||||
:actor_id
|
||||
])
|
||||
|> validate_required([:published, :url, :to, :cc, :content, :language, :actor_id])
|
||||
end
|
||||
|
||||
def create_note(attrs) when is_map(attrs) do
|
||||
def create_note(attrs, visibility)
|
||||
when is_map(attrs) and visibility in ["public", "unlisted", "followers", "private"] do
|
||||
id = Map.get(attrs, :id, Snowflake.next_id())
|
||||
actor_id = Map.get(attrs, :actor_id)
|
||||
actor = Actor.get_actor(id: actor_id)
|
||||
published = Map.get(attrs, :published, DateTime.utc_now())
|
||||
|
||||
url =
|
||||
case Map.get(attrs, :url) do
|
||||
nil ->
|
||||
actor_url = Map.get(attrs, :actor_url)
|
||||
|
||||
"#{actor_url}/#{id}"
|
||||
"#{actor.url}/#{id}"
|
||||
|
||||
_ ->
|
||||
Map.get(attrs, :url)
|
||||
end
|
||||
|
||||
{to, cc} =
|
||||
case visibility do
|
||||
"public" ->
|
||||
to = ["https://www.w3.org/ns/activitystreams#Public"]
|
||||
cc = [actor.followers | Map.get(attrs, :cc, [])]
|
||||
|
||||
{to, cc}
|
||||
|
||||
"unlisted" ->
|
||||
to = [actor.followers]
|
||||
cc = ["https://www.w3.org/ns/activitystreams#Public" | Map.get(attrs, :cc, [])]
|
||||
|
||||
{to, cc}
|
||||
|
||||
"followers" ->
|
||||
to = [actor.followers]
|
||||
cc = Map.get(attrs, :cc, [])
|
||||
|
||||
{to, cc}
|
||||
|
||||
"private" ->
|
||||
to = Map.get(attrs, :to, [])
|
||||
cc = []
|
||||
|
||||
{to, cc}
|
||||
end
|
||||
|
||||
%__MODULE__{}
|
||||
|> changeset(attrs)
|
||||
|> put_change(:id, id)
|
||||
|> put_change(:published, published)
|
||||
|> put_change(:url, url)
|
||||
|> put_change(:to, to)
|
||||
|> put_change(:cc, cc)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue