-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin.rb
134 lines (100 loc) · 3.28 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# name: discourse-blog-post
# about: Style a Discourse post as a blog post
# version: 0.2.2
# authors: scossar
# url: https://github.com/scossar/discourse-blog-post
enabled_site_setting :blog_post_enabled
register_asset 'stylesheets/blog-post-styles.scss'
PLUGIN_NAME = 'discourse_blog_post'.freeze
after_initialize do
module ::DiscourseBlogPost
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseBlogPost
end
end
require_dependency 'application_controller'
class DiscourseBlogPost::BlogPostController < ::ApplicationController
def mark_as_blog_post
post = Post.find(params[:id].to_i)
post.custom_fields['is_blog_post'] = 'true'
post.topic.custom_fields['blog_post_id'] = post.id
post.save!
post.topic.save!
render json: success_json
end
def unmark_as_blog_post
post = Post.find(params[:id].to_i)
post.custom_fields['is_blog_post'] = nil
post.topic.custom_fields['blog_post_id'] = nil
post.save!
post.topic.save!
render json: success_json
end
end
DiscourseBlogPost::Engine.routes.draw do
post 'mark_as_blog_post' => 'blog_post#mark_as_blog_post'
post 'unmark_as_blog_post' => 'blog_post#unmark_as_blog_post'
end
Discourse::Application.routes.append do
mount ::DiscourseBlogPost::Engine, at: 'blog'
end
TopicView.add_post_custom_fields_whitelister do |user|
['is_blog_post']
end
require_dependency 'topic_view_serializer'
class ::TopicViewSerializer
attributes :has_blog_post, :image_url
def image_url
object.image_url
end
def has_blog_post
blog_post_id ? true : false
end
def blog_post_id
id = object.topic.custom_fields['blog_post_id']
id && id.to_i rescue nil
end
end
require_dependency 'post_serializer'
class ::PostSerializer
attributes :is_blog_post, :can_create_blog_post, :allow_blog_posts_in_category, :image_url
def image_url
topic = (topic_view && topic_view.topic) || object.topic
topic.image_url
end
def is_blog_post
post_custom_fields['is_blog_post'] == 'true'
end
def can_create_blog_post
allowed_groups = SiteSetting.blog_post_allowed_groups.split('|')
current_user = scope.current_user.present? ? scope.current_user : nil
topic = (topic_view && topic_view.topic) || object.topic
unless current_user && (current_user.id == topic.user_id || current_user.admin)
return false
end
current_user.groups.each do |group|
return true if allowed_groups.include?(group.name)
end
false
end
def allow_blog_posts_in_category
allowed_categories = SiteSetting.blog_post_allowed_categories.split('|')
topic = (topic_view && topic_view.topic) || object.topic
# Private messages and banners don't have a category.
if topic.category_id
allowed_categories.include? topic.category.name
else
false
end
end
end
require_dependency 'topic_list_item_serializer'
class ::TopicListItemSerializer
attributes :has_blog_post
def has_blog_post
object.custom_fields['blog_post_id'] ? true : false
end
end
TopicList.preloaded_custom_fields << 'blog_post_id' if TopicList.respond_to? :preloaded_custom_fields
end