An IndieWeb engine for a self-hostable website. https://koype.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timeline.ex 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Koype: a IndieWeb-focused, single-tenant website engine for people.
  2. #
  3. # Copyright © 2019 Jacky Alciné <jacky.is@black.af>
  4. #
  5. # This file belongs to the Koype project.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. defmodule Koype.Timeline do
  20. @moduledoc """
  21. Implements a common interface for working with the timeline of Koype.
  22. This module provides logic to handle pagination, discovery and looking
  23. up of entries of the user's information in a "stream" like format. This
  24. aims to provide helper methods for listing the content in the following
  25. forms:
  26. * Sequential: Starting from the most recent post to the first post in a
  27. paged apporach. The size of the page is arbitrarily defined.
  28. * Chronological: starting from today to another time in the past. The unit
  29. of measure of the number of posts in a page is flexible. The current page
  30. is defined as a timestamp with another flag determining a unit of time
  31. (hour, minute, day, week, month, year). An additional parameter can be
  32. a multipler to determine how many units to show.
  33. """
  34. import Ecto.Query, only: [where: 3, where: 2]
  35. require Ecto.Query
  36. @doc "Provides a paginator for sequential post listing."
  37. @spec sequential(map(), Ecto.Query.t()) :: Scrivener.Page.t()
  38. def sequential(
  39. filter \\ [],
  40. models \\ Koype.Repo.Entry.published()
  41. |> Koype.Repo.descending(:published_at)
  42. ) do
  43. config = [page: Keyword.get(filter, :page, 1), page_size: Keyword.get(filter, :size, 10)]
  44. models
  45. |> do_filter_for_types(Keyword.get(filter, :type, :all), filter)
  46. |> Koype.Repo.paginate(config)
  47. end
  48. defp do_filter_for_types(query, type, filter \\ [])
  49. defp do_filter_for_types(query, :responses, _) do
  50. response_types = IndieWeb.Post.response_types() |> Enum.map(&Atom.to_string/1)
  51. where(query, [e], e.type in ^response_types)
  52. end
  53. defp do_filter_for_types(query, :content, _) do
  54. response_types = IndieWeb.Post.response_types() |> Enum.map(&Atom.to_string/1)
  55. where(query, [e], e.type not in ^response_types)
  56. end
  57. defp do_filter_for_types(_, :notifications, _) do
  58. Koype.Repo.Webmention
  59. |> Koype.Repo.undeleted()
  60. |> Koype.Repo.descending(:inserted_at)
  61. |> Koype.Repo.descending(:updated_at)
  62. end
  63. defp do_filter_for_types(query, :category, filter) do
  64. category_query = filter[:params]["category"]
  65. query
  66. |> Ecto.Query.join(:left, [q], c in assoc(q, :categories))
  67. |> where(
  68. [q, c],
  69. c.name == ^category_query or c.slug == ^category_query
  70. )
  71. end
  72. defp do_filter_for_types(query, type, _) when is_binary(type) do
  73. if type in Enum.map(Koype.Post.known_types(), fn typeinfo -> typeinfo[:type] end) do
  74. where(query, type: ^type)
  75. else
  76. do_filter_for_types(query, String.to_existing_atom(type))
  77. end
  78. end
  79. defp do_filter_for_types(query, :all, _), do: query
  80. end