glib-web 8.1.0 → 8.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/glib/json/libs.rb +16 -0
  3. data/app/controllers/glib/glib_direct_uploads_controller.rb +2 -1
  4. data/app/helpers/glib/app_feature_support_helper.rb +33 -8
  5. data/app/helpers/glib/json_ui/view_builder/fields.rb +110 -9
  6. data/app/helpers/glib/json_ui/view_builder.rb +29 -0
  7. data/app/jobs/glib/scheduled_job.rb +581 -0
  8. data/app/jobs/glib/scheduled_job_tick.rb +21 -0
  9. data/app/models/glib/scheduled_job_run.rb +16 -0
  10. data/app/views/json_ui/garage/actions/sheet_content.json.jbuilder +1 -1
  11. data/app/views/json_ui/garage/forms/_action_text_conversion.json.jbuilder +12 -0
  12. data/app/views/json_ui/garage/forms/action_text.json.jbuilder +57 -0
  13. data/app/views/json_ui/garage/forms/action_text_attachment.json.jbuilder +7 -0
  14. data/app/views/json_ui/garage/forms/action_text_conversion.json.jbuilder +5 -0
  15. data/app/views/json_ui/garage/forms/action_text_library.json.jbuilder +26 -0
  16. data/app/views/json_ui/garage/forms/action_text_mentions.json.jbuilder +4 -0
  17. data/app/views/json_ui/garage/forms/action_text_preview.json.jbuilder +9 -0
  18. data/app/views/json_ui/garage/forms/dialogs_form.json.jbuilder +10 -10
  19. data/app/views/json_ui/garage/forms/dirty_prompt.json.jbuilder +1 -1
  20. data/app/views/json_ui/garage/forms/dynamic_group.json.jbuilder +1 -1
  21. data/app/views/json_ui/garage/forms/index.json.jbuilder +6 -0
  22. data/app/views/json_ui/garage/forms/pickers.json.jbuilder +7 -0
  23. data/app/views/json_ui/garage/forms/show_hide.json.jbuilder +1 -1
  24. data/app/views/json_ui/garage/test_page/_header.json.jbuilder +6 -0
  25. data/app/views/json_ui/garage/test_page/charts.json.jbuilder +4 -2
  26. data/app/views/json_ui/garage/test_page/fields_actionText.json.jbuilder +189 -0
  27. data/app/views/json_ui/garage/test_page/fields_richText.json.jbuilder +45 -1
  28. data/app/views/json_ui/garage/test_page/fields_sign.json.jbuilder +34 -3
  29. data/app/views/json_ui/garage/test_page/form_dirty_fields.json.jbuilder +1 -1
  30. data/app/views/json_ui/garage/test_page/form_dirty_hooks.json.jbuilder +22 -0
  31. data/app/views/json_ui/garage/test_page/form_hidden_panels.json.jbuilder +105 -0
  32. data/app/views/json_ui/garage/views/charts.json.jbuilder +4 -2
  33. data/app/views/layouts/json_ui/renderer.html.erb +11 -1
  34. data/engines/blog/README.md +172 -0
  35. data/engines/blog/app/assets/stylesheets/glib/blog/admin.css +855 -0
  36. data/engines/blog/app/assets/stylesheets/glib/blog/themes/clean.css +816 -0
  37. data/engines/blog/app/assets/stylesheets/glib/blog/themes/editorial.css +631 -0
  38. data/engines/blog/app/controllers/concerns/glib/blog/admin/media_listing.rb +27 -0
  39. data/engines/blog/app/controllers/concerns/glib/blog/pagination.rb +14 -0
  40. data/engines/blog/app/controllers/glib/blog/admin/application_controller.rb +28 -0
  41. data/engines/blog/app/controllers/glib/blog/admin/authors_controller.rb +55 -0
  42. data/engines/blog/app/controllers/glib/blog/admin/categories_controller.rb +53 -0
  43. data/engines/blog/app/controllers/glib/blog/admin/comment_replies_controller.rb +34 -0
  44. data/engines/blog/app/controllers/glib/blog/admin/comments_controller.rb +50 -0
  45. data/engines/blog/app/controllers/glib/blog/admin/dashboards_controller.rb +34 -0
  46. data/engines/blog/app/controllers/glib/blog/admin/direct_uploads_controller.rb +30 -0
  47. data/engines/blog/app/controllers/glib/blog/admin/editor_attachments_controller.rb +16 -0
  48. data/engines/blog/app/controllers/glib/blog/admin/media_item_trashes_controller.rb +43 -0
  49. data/engines/blog/app/controllers/glib/blog/admin/media_items_controller.rb +61 -0
  50. data/engines/blog/app/controllers/glib/blog/admin/media_picks_controller.rb +19 -0
  51. data/engines/blog/app/controllers/glib/blog/admin/post_bulk_actions_controller.rb +33 -0
  52. data/engines/blog/app/controllers/glib/blog/admin/posts_controller.rb +79 -0
  53. data/engines/blog/app/controllers/glib/blog/admin/settings_controller.rb +27 -0
  54. data/engines/blog/app/controllers/glib/blog/admin/tags_controller.rb +46 -0
  55. data/engines/blog/app/controllers/glib/blog/application_controller.rb +90 -0
  56. data/engines/blog/app/controllers/glib/blog/authors_controller.rb +15 -0
  57. data/engines/blog/app/controllers/glib/blog/categories_controller.rb +15 -0
  58. data/engines/blog/app/controllers/glib/blog/comments_controller.rb +41 -0
  59. data/engines/blog/app/controllers/glib/blog/posts_controller.rb +47 -0
  60. data/engines/blog/app/controllers/glib/blog/tags_controller.rb +15 -0
  61. data/engines/blog/app/helpers/glib/blog/admin_helper.rb +57 -0
  62. data/engines/blog/app/helpers/glib/blog/comments_helper.rb +16 -0
  63. data/engines/blog/app/helpers/glib/blog/post_list_helper.rb +26 -0
  64. data/engines/blog/app/helpers/glib/blog/theme_helper.rb +85 -0
  65. data/engines/blog/app/jobs/glib/blog/purge_trash_job.rb +13 -0
  66. data/engines/blog/app/models/concerns/glib/blog/sluggable.rb +72 -0
  67. data/engines/blog/app/models/concerns/glib/blog/uses_media.rb +35 -0
  68. data/engines/blog/app/models/glib/blog/application_record.rb +7 -0
  69. data/engines/blog/app/models/glib/blog/author.rb +47 -0
  70. data/engines/blog/app/models/glib/blog/category.rb +53 -0
  71. data/engines/blog/app/models/glib/blog/comment.rb +66 -0
  72. data/engines/blog/app/models/glib/blog/comment_token.rb +30 -0
  73. data/engines/blog/app/models/glib/blog/media_item.rb +121 -0
  74. data/engines/blog/app/models/glib/blog/media_upload.rb +97 -0
  75. data/engines/blog/app/models/glib/blog/media_usage.rb +10 -0
  76. data/engines/blog/app/models/glib/blog/post.rb +110 -0
  77. data/engines/blog/app/models/glib/blog/post_view.rb +24 -0
  78. data/engines/blog/app/models/glib/blog/setting.rb +55 -0
  79. data/engines/blog/app/models/glib/blog/slug_redirect.rb +7 -0
  80. data/engines/blog/app/models/glib/blog/tag.rb +15 -0
  81. data/engines/blog/app/models/glib/blog/tagging.rb +10 -0
  82. data/engines/blog/app/policies/glib/blog/admin/author_policy.rb +11 -0
  83. data/engines/blog/app/policies/glib/blog/admin/category_policy.rb +11 -0
  84. data/engines/blog/app/policies/glib/blog/admin/comment_policy.rb +16 -0
  85. data/engines/blog/app/policies/glib/blog/admin/comment_reply_policy.rb +12 -0
  86. data/engines/blog/app/policies/glib/blog/admin/dashboard_policy.rb +11 -0
  87. data/engines/blog/app/policies/glib/blog/admin/direct_upload_policy.rb +11 -0
  88. data/engines/blog/app/policies/glib/blog/admin/editor_attachment_policy.rb +11 -0
  89. data/engines/blog/app/policies/glib/blog/admin/media_item_policy.rb +20 -0
  90. data/engines/blog/app/policies/glib/blog/admin/media_item_trash_policy.rb +11 -0
  91. data/engines/blog/app/policies/glib/blog/admin/media_pick_policy.rb +11 -0
  92. data/engines/blog/app/policies/glib/blog/admin/post_bulk_action_policy.rb +12 -0
  93. data/engines/blog/app/policies/glib/blog/admin/post_policy.rb +16 -0
  94. data/engines/blog/app/policies/glib/blog/admin/setting_policy.rb +11 -0
  95. data/engines/blog/app/policies/glib/blog/admin/tag_policy.rb +11 -0
  96. data/engines/blog/app/policies/glib/blog/application_policy.rb +16 -0
  97. data/engines/blog/app/policies/glib/blog/author_policy.rb +9 -0
  98. data/engines/blog/app/policies/glib/blog/category_policy.rb +9 -0
  99. data/engines/blog/app/policies/glib/blog/comment_policy.rb +11 -0
  100. data/engines/blog/app/policies/glib/blog/post_policy.rb +9 -0
  101. data/engines/blog/app/policies/glib/blog/tag_policy.rb +9 -0
  102. data/engines/blog/app/views/glib/blog/admin/_delete_button.json.jbuilder +24 -0
  103. data/engines/blog/app/views/glib/blog/admin/_nav.json.jbuilder +45 -0
  104. data/engines/blog/app/views/glib/blog/admin/_status_chip.json.jbuilder +2 -0
  105. data/engines/blog/app/views/glib/blog/admin/authors/_form.json.jbuilder +17 -0
  106. data/engines/blog/app/views/glib/blog/admin/authors/edit.json.jbuilder +7 -0
  107. data/engines/blog/app/views/glib/blog/admin/authors/index.json.jbuilder +50 -0
  108. data/engines/blog/app/views/glib/blog/admin/authors/new.json.jbuilder +11 -0
  109. data/engines/blog/app/views/glib/blog/admin/categories/_form.json.jbuilder +13 -0
  110. data/engines/blog/app/views/glib/blog/admin/categories/edit.json.jbuilder +7 -0
  111. data/engines/blog/app/views/glib/blog/admin/categories/index.json.jbuilder +68 -0
  112. data/engines/blog/app/views/glib/blog/admin/categories/new.json.jbuilder +7 -0
  113. data/engines/blog/app/views/glib/blog/admin/comments/_moderation_buttons.json.jbuilder +35 -0
  114. data/engines/blog/app/views/glib/blog/admin/comments/index.json.jbuilder +59 -0
  115. data/engines/blog/app/views/glib/blog/admin/dashboards/show.json.jbuilder +109 -0
  116. data/engines/blog/app/views/glib/blog/admin/media_item_trashes/index.json.jbuilder +32 -0
  117. data/engines/blog/app/views/glib/blog/admin/media_items/_rows.json.jbuilder +32 -0
  118. data/engines/blog/app/views/glib/blog/admin/media_items/edit.json.jbuilder +68 -0
  119. data/engines/blog/app/views/glib/blog/admin/media_items/index.json.jbuilder +68 -0
  120. data/engines/blog/app/views/glib/blog/admin/media_items/new.json.jbuilder +18 -0
  121. data/engines/blog/app/views/glib/blog/admin/media_picks/index.json.jbuilder +29 -0
  122. data/engines/blog/app/views/glib/blog/admin/posts/_form.json.jbuilder +104 -0
  123. data/engines/blog/app/views/glib/blog/admin/posts/edit.json.jbuilder +1 -0
  124. data/engines/blog/app/views/glib/blog/admin/posts/index.json.jbuilder +109 -0
  125. data/engines/blog/app/views/glib/blog/admin/posts/new.json.jbuilder +1 -0
  126. data/engines/blog/app/views/glib/blog/admin/settings/edit.json.jbuilder +55 -0
  127. data/engines/blog/app/views/glib/blog/admin/tags/_form.json.jbuilder +6 -0
  128. data/engines/blog/app/views/glib/blog/admin/tags/edit.json.jbuilder +7 -0
  129. data/engines/blog/app/views/glib/blog/admin/tags/index.json.jbuilder +30 -0
  130. data/engines/blog/app/views/glib/blog/admin/tags/new.json.jbuilder +7 -0
  131. data/engines/blog/app/views/glib/blog/authors/show.json.jbuilder +4 -0
  132. data/engines/blog/app/views/glib/blog/categories/show.json.jbuilder +4 -0
  133. data/engines/blog/app/views/glib/blog/comments/pending.json.jbuilder +7 -0
  134. data/engines/blog/app/views/glib/blog/media_items/_media_item.html.erb +14 -0
  135. data/engines/blog/app/views/glib/blog/posts/_comment.json.jbuilder +23 -0
  136. data/engines/blog/app/views/glib/blog/posts/_comment_form.json.jbuilder +14 -0
  137. data/engines/blog/app/views/glib/blog/posts/_comments.json.jbuilder +21 -0
  138. data/engines/blog/app/views/glib/blog/posts/index.json.jbuilder +4 -0
  139. data/engines/blog/app/views/glib/blog/posts/show.json.jbuilder +5 -0
  140. data/engines/blog/app/views/glib/blog/shared/errors.json.jbuilder +5 -0
  141. data/engines/blog/app/views/glib/blog/tags/show.json.jbuilder +3 -0
  142. data/engines/blog/app/views/glib/blog/themes/_nav_links.json.jbuilder +12 -0
  143. data/engines/blog/app/views/glib/blog/themes/clean/_brand.json.jbuilder +11 -0
  144. data/engines/blog/app/views/glib/blog/themes/clean/_card.json.jbuilder +15 -0
  145. data/engines/blog/app/views/glib/blog/themes/clean/_featured.json.jbuilder +23 -0
  146. data/engines/blog/app/views/glib/blog/themes/clean/_footer.json.jbuilder +15 -0
  147. data/engines/blog/app/views/glib/blog/themes/clean/_header.json.jbuilder +9 -0
  148. data/engines/blog/app/views/glib/blog/themes/clean/_post.json.jbuilder +98 -0
  149. data/engines/blog/app/views/glib/blog/themes/clean/_post_list.json.jbuilder +40 -0
  150. data/engines/blog/app/views/glib/blog/themes/clean/_post_rows.json.jbuilder +10 -0
  151. data/engines/blog/app/views/glib/blog/themes/editorial/_footer.json.jbuilder +15 -0
  152. data/engines/blog/app/views/glib/blog/themes/editorial/_header.json.jbuilder +11 -0
  153. data/engines/blog/app/views/glib/blog/themes/editorial/_post.json.jbuilder +62 -0
  154. data/engines/blog/app/views/glib/blog/themes/editorial/_post_list.json.jbuilder +25 -0
  155. data/engines/blog/app/views/glib/blog/themes/editorial/_post_rows.json.jbuilder +18 -0
  156. data/engines/blog/config/locales/en.yml +500 -0
  157. data/engines/blog/config/routes.rb +33 -0
  158. data/engines/blog/db/migrate/20261103090000_create_glib_blog_tables.rb +70 -0
  159. data/engines/blog/db/migrate/20261104090000_create_glib_blog_media_tables.rb +44 -0
  160. data/engines/blog/db/migrate/20261104120000_create_glib_blog_comments.rb +23 -0
  161. data/engines/blog/db/migrate/20261105090000_add_glib_blog_comment_moderation.rb +16 -0
  162. data/engines/blog/db/migrate/20261105100000_add_deleted_at_to_glib_blog_posts.rb +6 -0
  163. data/engines/blog/db/migrate/20261105110000_add_parent_and_default_to_glib_blog_categories.rb +10 -0
  164. data/engines/blog/db/migrate/20261105120000_create_glib_blog_post_views.rb +13 -0
  165. data/engines/blog/db/migrate/20261105130000_create_glib_blog_settings.rb +26 -0
  166. data/engines/blog/db/migrate/20261106090000_add_theme_to_glib_blog_settings.rb +7 -0
  167. data/engines/blog/lib/tasks/glib_blog_tasks.rake +32 -0
  168. data/lib/generators/glib/blog/install_generator.rb +44 -0
  169. data/lib/generators/glib/blog/templates/initializer.rb +22 -0
  170. data/lib/generators/glib/scheduled_jobs_generator.rb +30 -0
  171. data/lib/generators/templates/scheduled_jobs/create_glib_scheduled_job_runs.rb +22 -0
  172. data/lib/glib/blog/engine.rb +20 -0
  173. data/lib/glib/blog/theme.rb +35 -0
  174. data/lib/glib/blog.rb +50 -0
  175. data/lib/glib/engine.rb +17 -0
  176. data/lib/glib/scheduled_jobs/config.rb +36 -0
  177. data/lib/glib/scheduled_jobs/loader.rb +98 -0
  178. data/lib/glib-web.rb +2 -0
  179. metadata +177 -3
@@ -0,0 +1,15 @@
1
+ module Glib
2
+ module Blog
3
+ class AuthorsController < ApplicationController
4
+ def show
5
+ paginate_posts(@author.posts.live.newest_first.includes(:author, :category, cover_media_item: { file_attachment: :blob }))
6
+ end
7
+
8
+ private
9
+ def glib_load_resource
10
+ @author = Author.find_by_any_slug!(params[:id])
11
+ redirect_to_current_slug(@author)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Glib
2
+ module Blog
3
+ class CategoriesController < ApplicationController
4
+ def show
5
+ paginate_posts(@category.posts.live.newest_first.includes(:author, cover_media_item: { file_attachment: :blob }))
6
+ end
7
+
8
+ private
9
+ def glib_load_resource
10
+ @category = Category.find_by_any_slug!(params[:id])
11
+ redirect_to_current_slug(@category)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ module Glib
2
+ module Blog
3
+ class CommentsController < ApplicationController
4
+ # Lives only in the cache store; no visitor data reaches the database.
5
+ rate_limit to: 5, within: 10.minutes, only: :create, name: 'glib_blog_comments',
6
+ with: -> { render_errors(t('glib.blog.comments.create.too_many')) }
7
+
8
+ def create
9
+ CommentToken.verify!(params.dig(:comment, :token), @post)
10
+
11
+ @comment.assign_attributes(comment_params)
12
+ if current_blog_author
13
+ @comment.author = current_blog_author
14
+ @comment.author_name = current_blog_author.display_name
15
+ @comment.author_email = nil
16
+ end
17
+ @comment.status = Comment.initial_status_for(current_blog_author)
18
+
19
+ if @comment.save
20
+ @comment.pending? ? render(:pending) : glib_redirect_to(post_url(@post.slug))
21
+ else
22
+ render_errors(@comment.errors.full_messages)
23
+ end
24
+ rescue CommentToken::TooSoon
25
+ render_errors(t('.too_soon'))
26
+ rescue CommentToken::Invalid
27
+ render_errors(t('.expired'))
28
+ end
29
+
30
+ private
31
+ def glib_load_resource
32
+ @post = Post.live.find_by_any_slug!(params[:post_id])
33
+ @comment = @post.comments.new
34
+ end
35
+
36
+ def comment_params
37
+ params.require(:comment).permit(:author_name, :author_email, :body, :parent_id)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,47 @@
1
+ module Glib
2
+ module Blog
3
+ class PostsController < ApplicationController
4
+ RELATED_POSTS = 3
5
+
6
+ def index
7
+ posts = Post.live.newest_first.includes(:author, :category, cover_media_item: { file_attachment: :blob })
8
+ # The newest featured post leads the first page, so every page leaves it out of the list.
9
+ @featured_post = posts.find_by(featured: true)
10
+ paginate_posts(@featured_post ? posts.where.not(id: @featured_post.id) : posts)
11
+ end
12
+
13
+ def show
14
+ # Staff previewing their own blog and crawlers would swamp a small blog's numbers.
15
+ PostView.record(@post) unless current_blog_author || PostView.bot?(request.user_agent)
16
+
17
+ @comments = @post.comments.approved.top_level.oldest_first.includes(:approved_replies)
18
+ load_more_posts
19
+ # The page readers share, not the JSON the client fetched it as.
20
+ @post_page_url = glib_html_url(post_url(@post.slug))
21
+ seo_head_tags(
22
+ description: @post.seo_description,
23
+ canonical_url: @post.canonical_url.presence || @post_page_url,
24
+ noindex: @post.noindex
25
+ )
26
+ end
27
+
28
+ private
29
+ # What a theme offers after the post: others in its category, and the post before it.
30
+ # Both are lazy, so a theme that shows neither runs no query.
31
+ def load_more_posts
32
+ live = Post.live.where.not(id: @post.id).newest_first
33
+ related = live.includes(:author, :category, cover_media_item: { file_attachment: :blob }).limit(RELATED_POSTS)
34
+ @related_posts = @post.category ? related.where(category: @post.category) : related
35
+ @previous_posts = live.where(published_at: ..@post.published_at).limit(1)
36
+ end
37
+
38
+ def glib_load_resource
39
+ return unless action_name == 'show'
40
+
41
+ # Drafts, archived and scheduled posts are not live, so they 404.
42
+ @post = Post.live.with_rich_text_body.includes(:author, :category, :tags, cover_media_item: { file_attachment: :blob }).find_by_any_slug!(params[:id])
43
+ redirect_to_current_slug(@post)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ module Glib
2
+ module Blog
3
+ class TagsController < ApplicationController
4
+ def show
5
+ paginate_posts(@tag.posts.live.newest_first.includes(:author, :category, cover_media_item: { file_attachment: :blob }))
6
+ end
7
+
8
+ private
9
+ def glib_load_resource
10
+ @tag = Tag.find_by_any_slug!(params[:id])
11
+ redirect_to_current_slug(@tag)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,57 @@
1
+ module Glib
2
+ module Blog
3
+ module AdminHelper
4
+ # The admin page shell: the sidebar, the styles and the page header. The block receives the
5
+ # page panel; `actions` receives a flow for the header's buttons. A helper rather than a
6
+ # partial, so that `t('.key')` in the caller's blocks still resolves against the caller.
7
+ def glib_blog_admin_page(json, page, active:, title:, subtitle: nil, actions: nil, &content)
8
+ json.title title
9
+ json.styleClasses ['blog-admin']
10
+ page.template 'fullWidth'
11
+
12
+ render 'glib/blog/admin/nav', json: json, page: page, active: active
13
+
14
+ page.body childViews: ->(body) do
15
+ body.html text: admin_stylesheet_html
16
+ body.panels_responsive styleClasses: ['admin-main'], childViews: ->(res) do
17
+ glib_blog_admin_header(res, title: title, subtitle: subtitle, actions: actions)
18
+ content.call(res)
19
+ end
20
+ end
21
+ end
22
+
23
+ # The status shown on a post's pill; scheduled is a published post whose time hasn't come.
24
+ def glib_blog_post_status(post)
25
+ return :trash if post.trashed?
26
+
27
+ post.scheduled? ? :scheduled : post.status
28
+ end
29
+
30
+ # Drafts show when they were last edited, trashed posts when they were trashed, everything
31
+ # else its publish date.
32
+ def glib_blog_post_date(post)
33
+ if post.trashed?
34
+ t('glib.blog.admin.posts.trashed', time: time_ago_in_words(post.deleted_at))
35
+ elsif post.draft?
36
+ t('glib.blog.admin.posts.edited', time: time_ago_in_words(post.updated_at))
37
+ else
38
+ l(post.published_at.to_date, format: :long)
39
+ end
40
+ end
41
+
42
+ def glib_blog_admin_header(view, title:, subtitle: nil, actions: nil)
43
+ view.panels_split width: 'matchParent', align: 'middle', styleClasses: ['admin-header'], content: ->(split) do
44
+ split.left childViews: ->(left) do
45
+ left.h1 text: title
46
+ left.p text: subtitle, styleClasses: ['admin-subtitle'] if subtitle
47
+ end
48
+ if actions
49
+ split.right childViews: ->(right) do
50
+ right.panels_flow innerPadding: { bottom: 0, right: 8 }, childViews: actions
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,16 @@
1
+ module Glib
2
+ module Blog
3
+ module CommentsHelper
4
+ URL_PATTERN = %r{https?://[^\s<]+}
5
+
6
+ # Comments are plain text: escaped, with links made clickable (and marked as user content
7
+ # for search engines) and line breaks kept.
8
+ def glib_blog_comment_html(body)
9
+ linked = ERB::Util.html_escape(body).to_str.gsub(URL_PATTERN) do |url|
10
+ tag.a(url, href: CGI.unescapeHTML(url), rel: 'nofollow ugc noopener', target: '_blank')
11
+ end
12
+ simple_format(linked, {}, sanitize: false)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module Glib
2
+ module Blog
3
+ module PostListHelper
4
+ # A public page listing posts, a page at a time, laid out by the theme. When the reader
5
+ # scrolls to the end, the same URL is asked for just the next section. `kicker` is a short
6
+ # label above the heading, naming what kind of list this is.
7
+ def glib_blog_post_list_page(json, page, title:, heading: title, kicker: nil, description: nil)
8
+ if @next_page
9
+ next_page = { url: url_for(request.query_parameters.merge(page: @next_page, section_only: 'v1')), autoload: 'asNeeded' }
10
+ end
11
+
12
+ if params[:section_only].present?
13
+ json.nextPage next_page if next_page
14
+ json.sections do
15
+ json.child! { render glib_blog_theme_partial('post_rows'), json: json, section: page.list_section_builder, posts: @posts }
16
+ end
17
+ else
18
+ glib_blog_public_page(json, page, title: title) do |body|
19
+ render glib_blog_theme_partial('post_list'), json: json, view: body, posts: @posts, next_page: next_page,
20
+ heading: heading, kicker: kicker, description: description
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,85 @@
1
+ module Glib
2
+ module Blog
3
+ # The public blog's pages, drawn by the theme chosen in the settings (Glib::Blog::Theme). The
4
+ # action views only say what the page holds; the theme's partials draw it.
5
+ module ThemeHelper
6
+ NAV_CATEGORIES = 4
7
+
8
+ # The page shell: the theme's styles, header and footer. The block receives the page body.
9
+ # A helper rather than a partial, so that `t('.key')` in the caller's blocks still resolves
10
+ # against the caller.
11
+ #
12
+ # The page carries the class of every theme in the lineage, so a theme based on another
13
+ # gets that theme's rules too, and can override them.
14
+ def glib_blog_public_page(json, page, title:, &content)
15
+ json.title title
16
+ json.styleClasses ['blog-public', *blog_theme.lineage.map { |theme| "blog-theme-#{theme.name}" }]
17
+ page.template 'fullWidth'
18
+
19
+ page.body childViews: ->(body) do
20
+ body.html text: glib_blog_theme_stylesheet_html
21
+ render glib_blog_theme_partial('header'), json: json, view: body
22
+ content.call(body)
23
+ render glib_blog_theme_partial('footer'), json: json, view: body
24
+ end
25
+ end
26
+
27
+ # The partial from the nearest theme in the lineage that has it. Partials render their
28
+ # siblings through this too, so a theme based on another can replace any one of them.
29
+ def glib_blog_theme_partial(name)
30
+ @glib_blog_theme_partials ||= {}
31
+ @glib_blog_theme_partials[name] ||= begin
32
+ lineage = blog_theme.lineage
33
+ theme = lineage.find { |candidate| lookup_context.exists?(name, ["glib/blog/themes/#{candidate.name}"], true) }
34
+ # A partial no theme has falls through to the chosen one, so Rails names it as missing.
35
+ "glib/blog/themes/#{(theme || lineage.first).name}/#{name}"
36
+ end
37
+ end
38
+
39
+ # A theme-wide string (`glib.blog.themes.<theme>.<key>`) from the nearest theme in the
40
+ # lineage that has it.
41
+ def glib_blog_theme_t(key, **options)
42
+ keys = blog_theme.lineage.map { |theme| :"glib.blog.themes.#{theme.name}.#{key}" }
43
+ t(keys.first, **options, default: keys.drop(1))
44
+ end
45
+
46
+ # Sent in each page's body for the same reason as the admin's styles: the HTML head is
47
+ # missing after in-app navigation. One `<style>` per stylesheet, base themes first, so each
48
+ # keeps its own `@import` of fonts at its top.
49
+ def glib_blog_theme_stylesheet_html
50
+ blog_theme.lineage.reverse.map { |theme| "<style>#{theme.css}</style>" }.join
51
+ end
52
+
53
+ # Top-level categories, for the themes' navigation and topic filters.
54
+ def glib_blog_top_categories
55
+ @glib_blog_top_categories ||= Category.top_level.ordered.to_a
56
+ end
57
+
58
+ def glib_blog_nav_categories
59
+ glib_blog_top_categories.first(NAV_CATEGORIES)
60
+ end
61
+
62
+ # The category a list page belongs to, counting a sub-category as its parent's.
63
+ def glib_blog_active_category
64
+ @category && (@category.parent || @category)
65
+ end
66
+
67
+ # The post's body with an id on each heading, and those headings, so a theme can link to
68
+ # each section. Trix writes every heading as an `h1`. The ids go in after Action Text has
69
+ # rendered and sanitized the body, since its sanitizer drops them.
70
+ def glib_blog_post_sections(post)
71
+ fragment = Nokogiri::HTML5.fragment(post.body.to_s)
72
+ headings = fragment.css('h1').each_with_index.map do |heading, index|
73
+ heading['id'] = "section-#{index + 1}"
74
+ { id: heading['id'], text: heading.text.squish }
75
+ end
76
+ [fragment.to_html, headings]
77
+ end
78
+
79
+ # The caption shown under a cover image.
80
+ def glib_blog_media_caption(media_item)
81
+ [media_item.caption.presence, media_item.credit.presence].compact.join(' · ').presence
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,13 @@
1
+ module Glib
2
+ module Blog
3
+ # Permanently deletes posts and media trashed more than 30 days ago, including the stored
4
+ # files. Host apps schedule it daily (Solid Queue recurring task, cron, ...).
5
+ class PurgeTrashJob < ActiveJob::Base
6
+ # Posts first: deleting a post releases the files it used, so they can go in the same run.
7
+ def perform
8
+ Post.purgeable.find_each(&:destroy!)
9
+ MediaItem.purgeable.find_each(&:destroy!)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,72 @@
1
+ module Glib
2
+ module Blog
3
+ # URL slugs that stay reachable after they change: the previous slug is kept in
4
+ # `slug_redirects` so old links can be answered with a 301.
5
+ module Sluggable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ has_many :slug_redirects, as: :sluggable, class_name: 'Glib::Blog::SlugRedirect', dependent: :delete_all
10
+
11
+ before_validation :assign_slug
12
+ after_save :record_slug_change, if: :saved_change_to_slug?
13
+
14
+ validates :slug, presence: true, uniqueness: true
15
+ end
16
+
17
+ class_methods do
18
+ def sluggable_from(attribute)
19
+ @slug_source = attribute
20
+ end
21
+
22
+ def slug_source
23
+ @slug_source
24
+ end
25
+
26
+ # Keeps letters and digits from every script, so non-Latin titles (e.g. Chinese)
27
+ # still produce readable slugs instead of an empty string.
28
+ def slugify(text)
29
+ text.to_s.unicode_normalize(:nfkc).downcase.gsub(/[^\p{L}\p{N}]+/, '-').gsub(/\A-+|-+\z/, '')
30
+ end
31
+
32
+ # Finds a record by its current slug, or by a slug it used to have.
33
+ def find_by_any_slug!(slug)
34
+ find_by(slug: slug) ||
35
+ find_by!(id: SlugRedirect.where(sluggable_type: polymorphic_name, slug: slug).select(:sluggable_id))
36
+ end
37
+ end
38
+
39
+ private
40
+ def assign_slug
41
+ base = self.class.slugify(slug.presence || public_send(self.class.slug_source))
42
+ return if base.blank?
43
+
44
+ self.slug = unique_slug(base)
45
+ end
46
+
47
+ def unique_slug(base)
48
+ others = self.class.where.not(id: id)
49
+ candidate = base
50
+ suffix = 1
51
+ while others.exists?(slug: candidate)
52
+ suffix += 1
53
+ candidate = "#{base}-#{suffix}"
54
+ end
55
+ candidate
56
+ end
57
+
58
+ # The newest owner of a slug wins: a slug now in use no longer redirects, and an
59
+ # old slug points at whichever record gave it up last.
60
+ def record_slug_change
61
+ redirects = SlugRedirect.where(sluggable_type: self.class.polymorphic_name)
62
+ redirects.where(slug: slug).delete_all
63
+
64
+ previous_slug = slug_before_last_save
65
+ return if previous_slug.blank?
66
+
67
+ redirects.where(slug: previous_slug).delete_all
68
+ slug_redirects.create!(slug: previous_slug)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,35 @@
1
+ module Glib
2
+ module Blog
3
+ # Keeps `media_usages` in step with the media items a record shows (its rich text body and
4
+ # its cover), so the library knows which files are safe to trash.
5
+ module UsesMedia
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ has_many :media_usages, as: :record, class_name: 'Glib::Blog::MediaUsage', dependent: :destroy
10
+
11
+ after_save :sync_media_usages
12
+ end
13
+
14
+ private
15
+ def sync_media_usages
16
+ wanted = media_usage_keys
17
+ existing = media_usages.to_a
18
+
19
+ existing.reject { |usage| wanted.include?([usage.media_item_id, usage.role.to_s]) }.each(&:destroy!)
20
+
21
+ existing_keys = existing.map { |usage| [usage.media_item_id, usage.role.to_s] }
22
+ (wanted - existing_keys).each do |media_item_id, role|
23
+ media_usages.create!(media_item_id: media_item_id, role: role)
24
+ end
25
+ end
26
+
27
+ def media_usage_keys
28
+ body_items = body.body ? body.body.attachables.grep(MediaItem) : []
29
+ keys = body_items.map(&:id).uniq.map { |id| [id, 'body'] }
30
+ keys << [cover_media_item_id, 'cover'] if cover_media_item_id
31
+ keys
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module Glib
2
+ module Blog
3
+ class ApplicationRecord < ::Glib::ApplicationRecord
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ module Glib
2
+ module Blog
3
+ # A host-app user's blog profile and role. Without one, a user has no admin access.
4
+ class Author < ApplicationRecord
5
+ include Sluggable
6
+ sluggable_from :display_name
7
+
8
+ enum_symbolize :role, { author: 'author', editor: 'editor', admin: 'admin' }
9
+
10
+ belongs_to :user, class_name: Glib::Blog.user_class
11
+ has_many :posts, dependent: :restrict_with_error
12
+ has_many :media_items, foreign_key: :uploaded_by_id, inverse_of: :uploaded_by, dependent: :restrict_with_error
13
+
14
+ validates :display_name, presence: true
15
+ validates :user_id, uniqueness: true
16
+
17
+ scope :ordered, -> { order(:display_name, :id) }
18
+
19
+ def self.for(user)
20
+ user && find_by(user_id: user.id)
21
+ end
22
+
23
+ def initials
24
+ display_name.split.first(2).map { |word| word[0] }.join.upcase
25
+ end
26
+
27
+ def manages_all_content?
28
+ editor? || admin?
29
+ end
30
+
31
+ # Editors and admins manage every post; authors their own.
32
+ def manageable_posts
33
+ manages_all_content? ? Post.all : posts
34
+ end
35
+
36
+ # Editors and admins moderate every comment; authors the comments on their own posts.
37
+ def manageable_comments
38
+ manages_all_content? ? Comment.all : Comment.joins(:post).where(glib_blog_posts: { author_id: id })
39
+ end
40
+
41
+ # Every author sees and inserts any file, but edits and trashes only their own uploads.
42
+ def manageable_media_items
43
+ manages_all_content? ? MediaItem.all : media_items
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,53 @@
1
+ module Glib
2
+ module Blog
3
+ # Categories nest one level deep. One category can be the default, which new posts start in.
4
+ class Category < ApplicationRecord
5
+ include Sluggable
6
+ sluggable_from :name
7
+
8
+ belongs_to :parent, class_name: 'Glib::Blog::Category', optional: true
9
+ has_many :children, class_name: 'Glib::Blog::Category', foreign_key: :parent_id, inverse_of: :parent, dependent: :nullify
10
+ has_many :posts, dependent: :nullify
11
+
12
+ validates :name, presence: true
13
+ validate :parent_is_another_top_level_category, if: :parent
14
+
15
+ before_save :take_over_default, if: -> { default? && will_save_change_to_default? }
16
+ before_destroy :keep_default
17
+
18
+ scope :ordered, -> { order(:position, :name, :id) }
19
+ scope :top_level, -> { where(parent_id: nil) }
20
+
21
+ def self.default_category
22
+ find_by(default: true)
23
+ end
24
+
25
+ # Every category, each top-level one followed by its children.
26
+ def self.in_tree_order
27
+ by_parent = ordered.to_a.group_by(&:parent_id)
28
+ by_parent.fetch(nil, []).flat_map { |category| [category, *by_parent[category.id]] }
29
+ end
30
+
31
+ private
32
+ def parent_is_another_top_level_category
33
+ if parent == self || parent.parent_id.present?
34
+ errors.add(:parent, :invalid)
35
+ elsif children.exists?
36
+ errors.add(:parent, :has_children)
37
+ end
38
+ end
39
+
40
+ # The unique index allows one default, so the previous one gives it up first.
41
+ def take_over_default
42
+ self.class.where(default: true).where.not(id: id).update_all(default: false)
43
+ end
44
+
45
+ def keep_default
46
+ return unless default?
47
+
48
+ errors.add(:base, :default_in_use)
49
+ throw :abort
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,66 @@
1
+ module Glib
2
+ module Blog
3
+ # A reader's comment on a post. It waits for approval unless staff wrote it or the blog's
4
+ # settings turn moderation off. Replies go one level deep.
5
+ class Comment < ApplicationRecord
6
+ MAX_BODY_LENGTH = 5000
7
+ AVATAR_COLORS = %w[#0E7C7B #C2185B #6A1B9A #1565C0 #2E7D32 #EF6C00 #5D4037 #455A64].freeze
8
+
9
+ enum_symbolize :status, { pending: 'pending', approved: 'approved', spam: 'spam', trash: 'trash' }
10
+
11
+ belongs_to :post
12
+ belongs_to :parent, class_name: 'Glib::Blog::Comment', optional: true
13
+ belongs_to :author, optional: true
14
+ has_many :replies, class_name: 'Glib::Blog::Comment', foreign_key: :parent_id, inverse_of: :parent,
15
+ dependent: :destroy
16
+ has_many :approved_replies, -> { approved.oldest_first }, class_name: 'Glib::Blog::Comment', foreign_key: :parent_id
17
+
18
+ validates :author_name, presence: true, length: { maximum: 100 }
19
+ validates :author_email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, unless: :author
20
+ validates :body, presence: true, length: { maximum: MAX_BODY_LENGTH }
21
+ validate :parent_is_top_level_on_same_post, if: :parent
22
+ # Staff may still answer an existing thread after the post stops taking comments.
23
+ validate :post_accepts_comments, on: :create, unless: :staff?
24
+
25
+ after_commit :refresh_post_comments_count
26
+
27
+ scope :top_level, -> { where(parent_id: nil) }
28
+ scope :oldest_first, -> { order(created_at: :asc, id: :asc) }
29
+ scope :newest_first, -> { order(created_at: :desc, id: :desc) }
30
+
31
+ # The status a new comment starts with.
32
+ def self.initial_status_for(author)
33
+ author || !Setting.current.comments_require_approval? ? :approved : :pending
34
+ end
35
+
36
+ def staff?
37
+ author_id.present?
38
+ end
39
+
40
+ def initials
41
+ author_name.split.first(2).map { |word| word[0] }.join.upcase
42
+ end
43
+
44
+ # Stable per name, so a commenter keeps the same color across comments.
45
+ def avatar_color
46
+ AVATAR_COLORS[Zlib.crc32(author_name.to_s.downcase) % AVATAR_COLORS.size]
47
+ end
48
+
49
+ private
50
+ def parent_is_top_level_on_same_post
51
+ errors.add(:parent, :invalid) if parent.parent_id.present? || parent.post_id != post_id
52
+ end
53
+
54
+ def post_accepts_comments
55
+ errors.add(:base, :comments_closed) unless post&.comments_open?
56
+ end
57
+
58
+ # Readers only ever see approved comments, so only those are counted.
59
+ def refresh_post_comments_count
60
+ return if post.destroyed?
61
+
62
+ post.update_column(:comments_count, post.comments.approved.count)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,30 @@
1
+ module Glib
2
+ module Blog
3
+ # Signed token in the comment form, recording which post it was rendered for and when. A bot
4
+ # posting straight to the endpoint has no valid token, and one replaying a fresh page has to
5
+ # wait between posts. Nothing about the visitor is stored.
6
+ class CommentToken
7
+ class Invalid < StandardError; end
8
+ class TooSoon < Invalid; end
9
+
10
+ MIN_AGE = 3.seconds
11
+ MAX_AGE = 1.day
12
+
13
+ def self.generate(post)
14
+ verifier.generate({ 'post_id' => post.id, 'rendered_at' => Time.current.to_i }, expires_in: MAX_AGE)
15
+ end
16
+
17
+ def self.verify!(token, post)
18
+ payload = verifier.verified(token.to_s)
19
+ raise Invalid unless payload.is_a?(Hash) && payload['post_id'] == post.id
20
+ raise TooSoon if Time.current.to_i - payload['rendered_at'].to_i < MIN_AGE
21
+
22
+ true
23
+ end
24
+
25
+ def self.verifier
26
+ Rails.application.message_verifier('glib_blog_comment')
27
+ end
28
+ end
29
+ end
30
+ end