add backlinks
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
2025-02-15 06:00:37 +00:00
parent 6c2aba84ef
commit 0c5442f0cd
25 changed files with 502 additions and 147 deletions

View File

@ -88,6 +88,42 @@ defmodule Memex.Contexts do
)
end
@doc """
Returns the list of contexts that link to a particular slug.
## Examples
iex> backlink(%User{id: 123})
[%Context{}, ...]
iex> backlink("[other-context]", %User{id: 123})
[%Context{content: "[other-context]"}, ...]
"""
@spec backlink(String.t(), User.t()) :: [Context.t()]
def backlink(link, %{id: user_id}) when user_id |> is_binary() do
link = link |> String.replace("[", "\\[") |> String.replace("]", "\\]")
link_regex = "(^|[^\[])#{link}($|[^\]])"
Repo.all(
from c in Context,
where: fragment("? ~ ?", c.content, ^link_regex),
order_by: c.slug
)
end
def backlink(link, _invalid_user) do
link = link |> String.replace("[", "\\[") |> String.replace("]", "\\]")
link_regex = "(^|[^\[])#{link}($|[^\]])"
Repo.all(
from c in Context,
where: fragment("? ~ ?", c.content, ^link_regex),
where: c.visibility == :public,
order_by: c.slug
)
end
@doc """
Gets a single context.

View File

@ -86,6 +86,42 @@ defmodule Memex.Notes do
)
end
@doc """
Returns the list of notes that link to a particular slug.
## Examples
iex> backlink(%User{id: 123})
[%Note{}, ...]
iex> backlink("[other-note]", %User{id: 123})
[%Note{content: "[other-note]"}, ...]
"""
@spec backlink(String.t(), User.t()) :: [Note.t()]
def backlink(link, %{id: user_id}) when user_id |> is_binary() do
link = link |> String.replace("[", "\\[") |> String.replace("]", "\\]")
link_regex = "(^|[^\[])#{link}($|[^\]])"
Repo.all(
from n in Note,
where: fragment("? ~ ?", n.content, ^link_regex),
order_by: n.slug
)
end
def backlink(link, _invalid_user) do
link = link |> String.replace("[", "\\[") |> String.replace("]", "\\]")
link_regex = "(^|[^\[])#{link}($|[^\]])"
Repo.all(
from n in Note,
where: fragment("? ~ ?", n.content, ^link_regex),
where: n.visibility == :public,
order_by: n.slug
)
end
@doc """
Gets a single note.

View File

@ -142,6 +142,50 @@ defmodule Memex.Pipelines do
)
end
@doc """
Returns the list of pipelines that link to a particular slug.
## Examples
iex> backlink(%User{id: 123})
[%Pipeline{}, ...]
iex> backlink("[other-pipeline]", %User{id: 123})
[%Pipeline{description: "[other-pipeline]"}, ...]
"""
@spec backlink(String.t(), User.t()) :: [Pipeline.t()]
def backlink(link, %{id: user_id}) when user_id |> is_binary() do
link = link |> String.replace("[", "\\[") |> String.replace("]", "\\]")
link_regex = "(^|[^\[])#{link}($|[^\]])"
Repo.all(
from p in Pipeline,
left_join: s in assoc(p, :steps),
where:
fragment("? ~ ?", p.description, ^link_regex) or
fragment("? ~ ?", s.content, ^link_regex),
distinct: true,
order_by: p.slug
)
end
def backlink(link, _invalid_user) do
link = link |> String.replace("[", "\\[") |> String.replace("]", "\\]")
link_regex = "(^|[^\[])#{link}($|[^\]])"
Repo.all(
from p in Pipeline,
left_join: s in assoc(p, :steps),
where:
fragment("? ~ ?", p.description, ^link_regex) or
fragment("? ~ ?", s.content, ^link_regex),
where: p.visibility == :public,
distinct: true,
order_by: p.slug
)
end
@doc """
Creates a pipeline.