alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
alias Comeonin.Pbkdf2
alias Pleroma.Web.{OStatus, Websub}
- alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils
schema "users" do
end
end
+ def banner_url(user) do
+ case user.info["banner"] do
+ %{"url" => [%{"href" => href} | _]} -> href
+ _ -> nil
+ end
+ end
+
def ap_id(%User{nickname: nickname}) do
"#{Web.base_url}/users/#{nickname}"
end
end
def user_info(%User{} = user) do
+ oneself = if user.local, do: 1, else: 0
%{
- following_count: length(user.following),
+ following_count: length(user.following) - oneself,
note_count: user.info["note_count"] || 0,
follower_count: user.info["follower_count"] || 0
}
end
def update_changeset(struct, params \\ %{}) do
- changeset = struct
+ struct
|> cast(params, [:bio, :name])
|> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_length(:name, min: 1, max: 100)
end
+ def password_update_changeset(struct, params) do
+ changeset = struct
+ |> cast(params, [:password, :password_confirmation])
+ |> validate_required([:password, :password_confirmation])
+ |> validate_confirmation(:password)
+
+ if changeset.valid? do
+ hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
+ changeset
+ |> put_change(:password_hash, hashed)
+ else
+ changeset
+ end
+ end
+
+ def reset_password(user, data) do
+ Repo.update(password_update_changeset(user, data))
+ end
+
def register_changeset(struct, params \\ %{}) do
changeset = struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
|> follow_changeset(%{following: following})
|> Repo.update
- {:ok, followed} = update_follower_count(followed)
+ {:ok, _} = update_follower_count(followed)
follower
end
def unfollow(%User{} = follower, %User{} = followed) do
ap_followers = followed.follower_address
- if following?(follower, followed) do
+ if following?(follower, followed) and follower.ap_id != followed.ap_id do
following = follower.following
|> List.delete(ap_followers)
with %User{} = user <- get_by_nickname(nickname) do
user
else _e ->
- with [nick, domain] <- String.split(nickname, "@"),
+ with [_nick, _domain] <- String.split(nickname, "@"),
{:ok, user} <- OStatus.make_user(nickname) do
user
else _e -> nil
{:ok, Repo.all(q)}
end
+ def increase_note_count(%User{} = user) do
+ note_count = (user.info["note_count"] || 0) + 1
+ new_info = Map.put(user.info, "note_count", note_count)
+
+ cs = info_changeset(user, %{info: new_info})
+
+ Repo.update(cs)
+ end
+
def update_note_count(%User{} = user) do
note_count_query = from a in Object,
- where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
+ where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
select: count(a.id)
note_count = Repo.one(note_count_query)
Repo.update(cs)
end
- def get_notified_from_activity(%Activity{data: %{"to" => to}} = activity) do
+ def get_notified_from_activity(%Activity{data: %{"to" => to}}) do
query = from u in User,
where: u.ap_id in ^to,
where: u.local == true
Repo.all(query)
end
+
+ def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do
+ query = from u in User,
+ where: u.ap_id in ^to,
+ or_where: fragment("? \\\?| ?", u.following, ^to)
+
+ query = from u in query,
+ where: u.local == true
+
+ Repo.all(query)
+ end
+
+ def search(query, resolve) do
+ if resolve do
+ User.get_or_fetch_by_nickname(query)
+ end
+ q = from u in User,
+ where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
+ limit: 20
+ Repo.all(q)
+ end
+
+ def block(user, %{ap_id: ap_id}) do
+ blocks = user.info["blocks"] || []
+ new_blocks = Enum.uniq([ap_id | blocks])
+ new_info = Map.put(user.info, "blocks", new_blocks)
+
+ cs = User.info_changeset(user, %{info: new_info})
+ Repo.update(cs)
+ end
+
+ def unblock(user, %{ap_id: ap_id}) do
+ blocks = user.info["blocks"] || []
+ new_blocks = List.delete(blocks, ap_id)
+ new_info = Map.put(user.info, "blocks", new_blocks)
+
+ cs = User.info_changeset(user, %{info: new_info})
+ Repo.update(cs)
+ end
+
+ def blocks?(user, %{ap_id: ap_id}) do
+ blocks = user.info["blocks"] || []
+ Enum.member?(blocks, ap_id)
+ end
+
end