Class: TagsController
Constant Summary
collapse
- LIST_LIMIT =
51
ApplicationController::CHALLENGE_KEY, ApplicationController::HONEYPOT_KEY, ApplicationController::LEGACY_NO_THEMES, ApplicationController::LEGACY_NO_UNOFFICIAL_PLUGINS, ApplicationController::NO_PLUGINS, ApplicationController::NO_THEMES, ApplicationController::NO_UNOFFICIAL_PLUGINS, ApplicationController::SAFE_MODE
CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS
Instance Attribute Summary
#theme_id
Class Method Summary
collapse
Instance Method Summary
collapse
#respond_with_list
#application_layout, #can_cache_content?, #clear_notifications, #conditionally_allow_site_embedding, #current_homepage, #discourse_expires_in, #dont_cache_page, #ember_cli_required?, #fetch_user_from_params, #guardian, #handle_permalink, #handle_theme, #handle_unverified_request, #has_escaped_fragment?, #immutable_for, #login_method, #no_cookies, #perform_refresh_session, #post_ids_including_replies, #preload_json, #rate_limit_second_factor!, #redirect_with_client_support, #render_json_dump, #render_serialized, requires_plugin, #rescue_discourse_actions, #resolve_safe_mode, #secure_session, #serialize_data, #set_current_user_for_logs, #set_layout, #set_mobile_view, #set_mp_snapshot_fields, #show_browser_update?, #store_preloaded, #use_crawler_layout?, #with_resolved_locale
Methods included from VaryHeader
#ensure_vary_header
resolve_theme_id
#add_readonly_header, #allowed_in_staff_writes_only_mode?, #block_if_readonly_mode, #check_readonly_mode, #get_or_check_readonly_mode, #get_or_check_staff_writes_only_mode, included, #staff_writes_only_mode?
Methods included from Hijack
#hijack
Methods included from GlobalPath
#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path
Methods included from JsonError
#create_errors_json
#canonical_url, #default_canonical, included
#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session
Class Method Details
.show_methods ⇒ Object
9
10
11
|
# File 'app/controllers/tags_controller.rb', line 9
def self.show_methods
Discourse.anonymous_filters.map { |f| :"show_#{f}" }
end
|
Instance Method Details
#create_synonyms ⇒ Object
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
# File 'app/controllers/tags_controller.rb', line 390
def create_synonyms
guardian.ensure_can_admin_tags!
value = DiscourseTagging.add_or_create_synonyms_by_name(@tag, params[:synonyms])
if value.is_a?(Array)
render json:
failed_json.merge(
failed_tags:
value.inject({}) do |h, t|
h[t.name] = t.errors.full_messages.first
h
end,
)
else
render json: success_json
end
end
|
#destroy ⇒ Object
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'app/controllers/tags_controller.rb', line 258
def destroy
guardian.ensure_can_admin_tags!
tag_name = params[:tag_id]
tag = Tag.find_by_name(tag_name)
raise Discourse::NotFound if tag.nil?
TopicCustomField.transaction do
tag.destroy
StaffActionLogger.new(current_user).log_custom("deleted_tag", subject: tag_name)
end
render json: success_json
end
|
#destroy_synonym ⇒ Object
407
408
409
410
411
412
413
414
415
416
417
|
# File 'app/controllers/tags_controller.rb', line 407
def destroy_synonym
guardian.ensure_can_admin_tags!
synonym = Tag.where_name(params[:synonym_id]).first
raise Discourse::NotFound unless synonym
if synonym.target_tag == @tag
synonym.update!(target_tag: nil)
render json: success_json
else
render json: failed_json, status: 400
end
end
|
#destroy_unused ⇒ Object
250
251
252
253
254
255
256
|
# File 'app/controllers/tags_controller.rb', line 250
def destroy_unused
guardian.ensure_can_admin_tags!
tags = Tag.unused
StaffActionLogger.new(current_user).log_custom("deleted_unused_tags", tags: tags.pluck(:name))
tags.destroy_all
render json: success_json
end
|
#index ⇒ Object
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
|
# File 'app/controllers/tags_controller.rb', line 37
def index
@description_meta = I18n.t("tags.title")
@title = @description_meta
show_all_tags = guardian.can_admin_tags? && guardian.is_admin?
if SiteSetting.tags_listed_by_group
ungrouped_tags = Tag.where("tags.id NOT IN (SELECT tag_id FROM tag_group_memberships)")
ungrouped_tags = ungrouped_tags.used_tags_in_regular_topics(guardian) unless show_all_tags
ungrouped_tags = ungrouped_tags.order(:id)
grouped_tag_counts =
TagGroup
.visible(guardian)
.order("name ASC")
.includes(:none_synonym_tags)
.map do |tag_group|
{
id: tag_group.id,
name: tag_group.name,
tags: self.class.tag_counts_json(tag_group.none_synonym_tags, guardian),
}
end
@tags = self.class.tag_counts_json(ungrouped_tags, guardian)
@extras = { tag_groups: grouped_tag_counts }
else
tags = show_all_tags ? Tag.all : Tag.used_tags_in_regular_topics(guardian)
tags = tags.order(:id)
unrestricted_tags = DiscourseTagging.filter_visible(tags.where(target_tag_id: nil), guardian)
categories =
Category
.where(
"id IN (SELECT category_id FROM category_tags WHERE category_id IN (?))",
guardian.allowed_category_ids,
)
.includes(:none_synonym_tags)
.order(:id)
category_tag_counts =
categories
.map do |c|
category_tags =
self.class.tag_counts_json(
DiscourseTagging.filter_visible(c.none_synonym_tags, guardian),
guardian,
)
next if category_tags.empty?
{ id: c.id, tags: category_tags }
end
.compact
@tags = self.class.tag_counts_json(unrestricted_tags, guardian)
@extras = { categories: category_tag_counts }
end
respond_to do |format|
format.html { render :index }
format.json { render json: { tags: @tags, extras: @extras } }
end
end
|
#info ⇒ Object
184
185
186
|
# File 'app/controllers/tags_controller.rb', line 184
def info
render_serialized(@tag, DetailedTagSerializer, rest_serializer: true, root: :tag_info)
end
|
#list ⇒ Object
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
135
136
137
138
139
140
141
|
# File 'app/controllers/tags_controller.rb', line 105
def list
offset = params[:offset].to_i || 0
tags = guardian.can_admin_tags? ? Tag.all : Tag.visible(guardian)
load_more_query_params = { offset: offset + 1 }
if filter = params[:filter]
tags = tags.where("LOWER(tags.name) ILIKE ?", "%#{filter.downcase}%")
load_more_query_params[:filter] = filter
end
if only_tags = params[:only_tags]
tags = tags.where("LOWER(tags.name) IN (?)", only_tags.split(",").map(&:downcase))
load_more_query_params[:only_tags] = only_tags
end
if exclude_tags = params[:exclude_tags]
tags = tags.where("LOWER(tags.name) NOT IN (?)", exclude_tags.split(",").map(&:downcase))
load_more_query_params[:exclude_tags] = exclude_tags
end
tags_count = tags.count
tags = tags.order("LOWER(tags.name) ASC").limit(LIST_LIMIT).offset(offset * LIST_LIMIT)
load_more_url = URI("/tags/list.json")
load_more_url.query = URI.encode_www_form(load_more_query_params)
render_serialized(
tags,
TagSerializer,
root: "list_tags",
meta: {
total_rows_list_tags: tags_count,
load_more_list_tags: load_more_url.to_s,
},
)
end
|
#list_unused ⇒ Object
245
246
247
248
|
# File 'app/controllers/tags_controller.rb', line 245
def list_unused
guardian.ensure_can_admin_tags!
render json: { tags: Tag.unused.pluck(:name) }
end
|
#notifications ⇒ Object
363
364
365
366
367
368
369
370
|
# File 'app/controllers/tags_controller.rb', line 363
def notifications
tag = Tag.where_name(params[:tag_id]).first
raise Discourse::NotFound unless tag
level =
tag.tag_users.where(user: current_user).first.try(:notification_level) ||
TagUser.notification_levels[:regular]
render json: { tag_notification: { id: tag.name, notification_level: level.to_i } }
end
|
#personal_messages ⇒ Object
380
381
382
383
384
385
386
387
388
|
# File 'app/controllers/tags_controller.rb', line 380
def personal_messages
guardian.ensure_can_tag_pms!
allowed_user = fetch_user_from_params
raise Discourse::NotFound if allowed_user.blank?
raise Discourse::NotFound if current_user.id != allowed_user.id && !@guardian.is_admin?
pm_tags = Tag.pm_tags(guardian: guardian, allowed_user: allowed_user)
render json: { tags: pm_tags }
end
|
#search ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# File 'app/controllers/tags_controller.rb', line 287
def search
filter_params = {
for_input: params[:filterForInput],
selected_tags: params[:selected_tags],
exclude_synonyms: params[:excludeSynonyms],
exclude_has_synonyms: params[:excludeHasSynonyms],
}
if limit = fetch_limit_from_params(default: nil, max: SiteSetting.max_tag_search_results)
filter_params[:limit] = limit
end
filter_params[:category] = Category.find_by_id(params[:categoryId]) if params[:categoryId]
if !params[:q].blank?
clean_name = DiscourseTagging.clean_tag(params[:q])
filter_params[:term] = clean_name
filter_params[:order_search_results] = true
else
filter_params[:order_popularity] = true
end
tags_with_counts, filter_result_context =
DiscourseTagging.filter_allowed_tags(guardian, **filter_params, with_context: true)
tags = self.class.tag_counts_json(tags_with_counts, guardian)
json_response = { results: tags }
if clean_name && !tags.find { |h| h[:id].downcase == clean_name.downcase } &&
tag = Tag.where_name(clean_name).first
json_response[:forbidden] = params[:q]
if filter_params[:exclude_synonyms] && tag.synonym?
json_response[:forbidden_message] = I18n.t(
"tags.forbidden.synonym",
tag_name: tag.target_tag.name,
)
elsif filter_params[:exclude_has_synonyms] && tag.synonyms.exists?
json_response[:forbidden_message] = I18n.t(
"tags.forbidden.has_synonyms",
tag_name: tag.name,
)
else
category_names = tag.categories.where(id: guardian.allowed_category_ids).pluck(:name)
category_names +=
Category
.joins(tag_groups: :tags)
.where(id: guardian.allowed_category_ids, "tags.id": tag.id)
.pluck(:name)
if category_names.present?
category_names.uniq!
json_response[:forbidden_message] = I18n.t(
"tags.forbidden.restricted_to",
count: category_names.count,
tag_name: tag.name,
category_names: category_names.join(", "),
)
else
json_response[:forbidden_message] = I18n.t(
"tags.forbidden.in_this_category",
tag_name: tag.name,
)
end
end
end
if required_tag_group = filter_result_context[:required_tag_group]
json_response[:required_tag_group] = required_tag_group
end
render json: json_response
end
|
#show ⇒ Object
180
181
182
|
# File 'app/controllers/tags_controller.rb', line 180
def show
show_latest
end
|
#tag_feed ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'app/controllers/tags_controller.rb', line 271
def tag_feed
discourse_expires_in 1.minute
tag_id = params[:tag_id]
@link = "#{Discourse.base_url}/tag/#{tag_id}"
@description = I18n.t("rss_by_tag", tag: tag_id)
@title = "#{SiteSetting.title} - #{@description}"
@atom_link = "#{Discourse.base_url}/tag/#{tag_id}.rss"
query = TopicQuery.new(current_user, tags: [tag_id])
latest_results = query.latest_results
@topic_list = query.create_list(:by_tag, {}, latest_results)
render "list/list", formats: [:rss]
end
|
#update ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'app/controllers/tags_controller.rb', line 188
def update
guardian.ensure_can_admin_tags!
tag = Tag.find_by_name(params[:tag_id])
raise Discourse::NotFound if tag.nil?
if (params[:tag][:id].present?)
new_tag_name = DiscourseTagging.clean_tag(params[:tag][:id])
tag.name = new_tag_name
end
tag.description = params[:tag][:description] if params[:tag]&.has_key?(:description)
if tag.save
StaffActionLogger.new(current_user).log_custom(
"renamed_tag",
previous_value: params[:tag_id],
new_value: new_tag_name,
)
render json: { tag: { id: tag.name, description: tag.description } }
else
render_json_error tag.errors.full_messages
end
end
|
#update_notifications ⇒ Object
#upload ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'app/controllers/tags_controller.rb', line 211
def upload
require "csv"
guardian.ensure_can_admin_tags!
file = params[:file] || params[:files].first
hijack do
begin
Tag.transaction do
CSV.foreach(file.tempfile) do |row|
if row.length > 2
raise Discourse::InvalidParameters.new(I18n.t("tags.upload_row_too_long"))
end
tag_name = DiscourseTagging.clean_tag(row[0])
tag_group_name = row[1] || nil
tag = Tag.find_by_name(tag_name) || Tag.create!(name: tag_name)
if tag_group_name
tag_group =
TagGroup.find_by(name: tag_group_name) || TagGroup.create!(name: tag_group_name)
tag.tag_groups << tag_group if tag.tag_groups.exclude?(tag_group)
end
end
end
render json: success_json
rescue Discourse::InvalidParameters => e
render json: failed_json.merge(errors: [e.message]), status: 422
end
end
end
|