Basic status creation and retrieval.
This commit is contained in:
parent
8de523c8ae
commit
9a8850eb9e
14 changed files with 272 additions and 9 deletions
|
|
@ -1,5 +1,6 @@
|
|||
defmodule Pleroma.User do
|
||||
use Ecto.Schema
|
||||
alias Pleroma.User
|
||||
|
||||
schema "users" do
|
||||
field :bio, :string
|
||||
|
|
@ -7,7 +8,22 @@ defmodule Pleroma.User do
|
|||
field :name, :string
|
||||
field :nickname, :string
|
||||
field :password_hash, :string
|
||||
field :following, { :array, :string }, default: []
|
||||
field :ap_id, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def ap_id(%User{nickname: nickname}) do
|
||||
host =
|
||||
Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|
||||
|> Keyword.fetch!(:url)
|
||||
|> Keyword.fetch!(:host)
|
||||
|
||||
"https://#{host}/users/#{nickname}"
|
||||
end
|
||||
|
||||
def ap_followers(%User{} = user) do
|
||||
"#{ap_id(user)}/followers"
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,16 @@
|
|||
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Activity
|
||||
import Ecto.Query
|
||||
|
||||
def insert(map) when is_map(map) do
|
||||
Repo.insert(%Activity{data: map})
|
||||
end
|
||||
|
||||
def fetch_public_activities do
|
||||
query = from activity in Activity,
|
||||
where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data)
|
||||
|
||||
Repo.all(query)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
||||
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
|
||||
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
|
||||
|
||||
def to_map(activity, %{user: user}) do
|
||||
content = get_in(activity.data, ["object", "content"])
|
||||
%{
|
||||
"id" => activity.id,
|
||||
"user" => UserRepresenter.to_map(user),
|
||||
"attentions" => [],
|
||||
"statusnet_html" => content,
|
||||
"text" => content,
|
||||
"is_local" => true,
|
||||
"is_post_verb" => true
|
||||
}
|
||||
end
|
||||
end
|
||||
33
lib/pleroma/web/twitter_api/twitter_api.ex
Normal file
33
lib/pleroma/web/twitter_api/twitter_api.ex
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||
|
||||
def create_status(user = %User{}, data = %{}) do
|
||||
activity = %{
|
||||
type: "Create",
|
||||
to: [
|
||||
User.ap_followers(user),
|
||||
"https://www.w3.org/ns/activitystreams#Public"
|
||||
],
|
||||
actor: User.ap_id(user),
|
||||
object: %{
|
||||
type: "Note",
|
||||
content: data.status
|
||||
}
|
||||
}
|
||||
|
||||
ActivityPub.insert(activity)
|
||||
end
|
||||
|
||||
def fetch_public_statuses do
|
||||
activities = ActivityPub.fetch_public_activities
|
||||
|
||||
Enum.map(activities, fn(activity) ->
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = Repo.get_by!(User, ap_id: actor)
|
||||
ActivityRepresenter.to_map(activity, %{user: user})
|
||||
end)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue