Compare commits
No commits in common. "0afe2d3b30baab8daf6f5d8f08c19844c35a3939" and "5d45f170f1c878e6dff48da932895f5ee5d38ee2" have entirely different histories.
0afe2d3b30
...
5d45f170f1
|
@ -89,7 +89,6 @@ defmodule Clacks.ActivityPub do
|
||||||
|
|
||||||
@spec synthesized_create(object :: map()) :: map()
|
@spec synthesized_create(object :: map()) :: map()
|
||||||
def synthesized_create(object) do
|
def synthesized_create(object) do
|
||||||
# todo: does this need to have an id field?
|
|
||||||
%{
|
%{
|
||||||
"@context" => @context,
|
"@context" => @context,
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
defmodule Clacks.UserActionsHelper do
|
|
||||||
alias Clacks.{User, Repo, Activity, Object, ActivityPub}
|
|
||||||
|
|
||||||
@public "https://www.w3.org/ns/activitystreams#Public"
|
|
||||||
|
|
||||||
@spec post_status(
|
|
||||||
author :: User.t(),
|
|
||||||
content :: String.t(),
|
|
||||||
in_reply_to :: String.t() | Activity.t() | nil
|
|
||||||
) :: {:ok, Activity.t()} | {:error, any()}
|
|
||||||
|
|
||||||
def post_status(author, content, in_reply_to_ap_id) when is_binary(in_reply_to_ap_id) do
|
|
||||||
case Activity.get_by_ap_id(in_reply_to_ap_id) do
|
|
||||||
nil ->
|
|
||||||
{:error, "Could find post to reply to with AP ID '#{in_reply_to_ap_id}'"}
|
|
||||||
|
|
||||||
in_reply_to ->
|
|
||||||
post_status(author, content, in_reply_to)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def post_status(author, content, in_reply_to) do
|
|
||||||
note = note_for_posting(author, content, in_reply_to)
|
|
||||||
note_changeset = Object.changeset_for_creating(note)
|
|
||||||
{:ok, _object} = Repo.insert(note_changeset)
|
|
||||||
|
|
||||||
%{"id" => ap_id} = create = ActivityPub.create(note)
|
|
||||||
|
|
||||||
case ActivityPub.Helper.save_and_federate(create, author.actor) do
|
|
||||||
{:ok, activity} ->
|
|
||||||
{:ok, activity}
|
|
||||||
|
|
||||||
:error ->
|
|
||||||
{:error, "Unable to save and federate activity with ID '#{ap_id}'"}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp note_for_posting(author, content, %Activity{
|
|
||||||
data: %{"id" => in_reply_to_ap_id, "context" => context, "actor" => in_reply_to_actor}
|
|
||||||
}) do
|
|
||||||
to = [in_reply_to_actor, @public]
|
|
||||||
# todo: followers
|
|
||||||
cc = []
|
|
||||||
|
|
||||||
ActivityPub.note(
|
|
||||||
author.actor.ap_id,
|
|
||||||
content,
|
|
||||||
context,
|
|
||||||
in_reply_to_ap_id,
|
|
||||||
nil,
|
|
||||||
DateTime.utc_now(),
|
|
||||||
to,
|
|
||||||
cc
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp note_for_posting(author, content, _in_reply_to) do
|
|
||||||
ActivityPub.note(author.actor.ap_id, content)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,6 +1,6 @@
|
||||||
defmodule ClacksWeb.FrontendController do
|
defmodule ClacksWeb.FrontendController do
|
||||||
use ClacksWeb, :controller
|
use ClacksWeb, :controller
|
||||||
alias Clacks.{Actor, User, Timeline, Repo, ActivityPub, Activity, Object, UserActionsHelper}
|
alias Clacks.{Actor, User, Timeline, Repo, ActivityPub, Activity, Object}
|
||||||
alias ClacksWeb.Router.Helpers, as: Routes
|
alias ClacksWeb.Router.Helpers, as: Routes
|
||||||
alias ClacksWeb.Endpoint
|
alias ClacksWeb.Endpoint
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
@ -251,20 +251,45 @@ defmodule ClacksWeb.FrontendController do
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_status(conn, %{"content" => content} = params) do
|
def post_status(conn, %{"content" => _content} = params) do
|
||||||
current_user = conn.assigns[:user] |> Repo.preload(:actor)
|
current_user = conn.assigns[:user] |> Repo.preload(:actor)
|
||||||
|
|
||||||
UserActionsHelper.post_status(current_user, content, Map.get(params, "in_reply_to"))
|
note = note_for_posting(current_user, params)
|
||||||
|> case do
|
note_changeset = Object.changeset_for_creating(note)
|
||||||
{:ok, activity} ->
|
{:ok, _object} = Repo.insert(note_changeset)
|
||||||
|
|
||||||
|
create = ActivityPub.create(note)
|
||||||
|
{:ok, activity} = ActivityPub.Helper.save_and_federate(create, current_user.actor)
|
||||||
|
|
||||||
path = Map.get(params, "continue", Routes.frontend_path(Endpoint, :status, activity.id))
|
path = Map.get(params, "continue", Routes.frontend_path(Endpoint, :status, activity.id))
|
||||||
redirect(conn, to: path)
|
redirect(conn, to: path)
|
||||||
|
|
||||||
{:error, reason} ->
|
|
||||||
conn
|
|
||||||
|> put_flash(:error, "Unable to post status: #{inspect(reason)}")
|
|
||||||
|> redirect(to: Map.get(params, "continue", Routes.frontend_path(Endpoint, :index)))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp note_for_posting(current_user, %{"content" => content, "in_reply_to" => in_reply_to_ap_id}) do
|
||||||
|
with %Activity{data: %{"context" => context, "actor" => in_reply_to_actor}} <-
|
||||||
|
Activity.get_by_ap_id(in_reply_to_ap_id) do
|
||||||
|
to = [in_reply_to_actor, @public]
|
||||||
|
# todo: followers
|
||||||
|
cc = []
|
||||||
|
|
||||||
|
ActivityPub.note(
|
||||||
|
current_user.actor.ap_id,
|
||||||
|
content,
|
||||||
|
context,
|
||||||
|
in_reply_to_ap_id,
|
||||||
|
nil,
|
||||||
|
DateTime.utc_now(),
|
||||||
|
to,
|
||||||
|
cc
|
||||||
|
)
|
||||||
|
else
|
||||||
|
_ ->
|
||||||
|
ActivityPub.note(current_user.actor.ap_id, content)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp note_for_posting(current_user, %{"content" => content}) do
|
||||||
|
ActivityPub.note(current_user.actor.ap_id, content)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec follow_activity(follower :: Actor.t(), followee :: Actor.t()) :: map()
|
@spec follow_activity(follower :: Actor.t(), followee :: Actor.t()) :: map()
|
||||||
|
|
|
@ -46,17 +46,8 @@ defmodule ClacksWeb.FrontendView do
|
||||||
# less than a week, days
|
# less than a week, days
|
||||||
"#{Integer.floor_div(diff, 60 * 60 * 24)}d"
|
"#{Integer.floor_div(diff, 60 * 60 * 24)}d"
|
||||||
|
|
||||||
diff < 60 * 60 * 24 * 30 ->
|
|
||||||
# less than a month(ish), weeks
|
|
||||||
"#{Integer.floor_div(diff, 60 * 60 * 24 * 7)}wk"
|
|
||||||
|
|
||||||
diff < 60 * 60 * 24 * 365 ->
|
|
||||||
# less than a year, months(ish)
|
|
||||||
# todo: figure out actually how many months
|
|
||||||
"#{Integer.floor_div(diff, 60 * 60 * 24 * 30)}mo"
|
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
Timex.format!(datetime, "%F", :strftime)
|
Timex.format!(datetime, "%FT%T%:z", :strftime)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue