Class: PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/posts_controller.rb

Constant Summary collapse

MARKDOWN_TOPIC_PAGE_SIZE =
100
MAX_POST_REPLIES =
20
DELETED_POSTS_MAX_LIMIT =
100

Constants inherited from ApplicationController

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

Constants included from CanonicalURL::ControllerExtensions

CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS

Instance Attribute Summary

Attributes inherited from ApplicationController

#theme_id

Instance Method Summary collapse

Methods inherited from ApplicationController

#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

Methods included from ThemeResolver

resolve_theme_id

Methods included from ReadOnlyMixin

#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

Methods included from CanonicalURL::ControllerExtensions

#canonical_url, #default_canonical, included

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Instance Method Details

#by_dateObject



306
307
308
309
# File 'app/controllers/posts_controller.rb', line 306

def by_date
  post = find_post_from_params_by_date
  display_post(post)
end

#by_numberObject



301
302
303
304
# File 'app/controllers/posts_controller.rb', line 301

def by_number
  post = find_post_from_params_by_number
  display_post(post)
end

#cookedObject



173
174
175
# File 'app/controllers/posts_controller.rb', line 173

def cooked
  render json: { cooked: find_post_from_params.cooked }
end

#createObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/controllers/posts_controller.rb', line 199

def create
  manager_params = create_params
  manager_params[:first_post_checks] = !is_api?
  manager_params[:advance_draft] = !is_api?

  manager = NewPostManager.new(current_user, manager_params)

  json =
    if is_api?
      memoized_payload =
        DistributedMemoizer.memoize(signature_for(manager_params), 120) do
          MultiJson.dump(serialize_data(manager.perform, NewPostResultSerializer, root: false))
        end

      JSON.parse(memoized_payload)
    else
      serialize_data(manager.perform, NewPostResultSerializer, root: false)
    end

  backwards_compatible_json(json)
end

#deleted_postsObject



703
704
705
706
707
708
709
710
711
712
713
714
# File 'app/controllers/posts_controller.rb', line 703

def deleted_posts
  params.permit(:offset, :limit)
  guardian.ensure_can_see_deleted_posts!

  user = fetch_user_from_params
  offset = [params[:offset].to_i, 0].max
  limit = fetch_limit_from_params(default: 60, max: DELETED_POSTS_MAX_LIMIT)

  posts = user_posts(guardian, user.id, offset: offset, limit: limit).where.not(deleted_at: nil)

  render_serialized(posts, AdminUserActionSerializer)
end

#destroyObject



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
362
363
364
365
366
# File 'app/controllers/posts_controller.rb', line 331

def destroy
  post = find_post_from_params
  force_destroy = ActiveModel::Type::Boolean.new.cast(params[:force_destroy])

  if force_destroy
    if !guardian.can_permanently_delete?(post)
      return render_json_error post.cannot_permanently_delete_reason(current_user), status: 403
    end
  else
    guardian.ensure_can_delete!(post)
  end

  unless guardian.can_moderate_topic?(post.topic)
    RateLimiter.new(
      current_user,
      "delete_post_per_min",
      SiteSetting.max_post_deletions_per_minute,
      1.minute,
    ).performed!
    RateLimiter.new(
      current_user,
      "delete_post_per_day",
      SiteSetting.max_post_deletions_per_day,
      1.day,
    ).performed!
  end

  PostDestroyer.new(
    current_user,
    post,
    context: params[:context],
    force_destroy: force_destroy,
  ).destroy

  render body: nil
end

#destroy_bookmarkObject



646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'app/controllers/posts_controller.rb', line 646

def destroy_bookmark
  params.require(:post_id)

  bookmark_id =
    Bookmark.where(
      bookmarkable_id: params[:post_id],
      bookmarkable_type: "Post",
      user_id: current_user.id,
    ).pick(:id)
  destroyed_bookmark = BookmarkManager.new(current_user).destroy(bookmark_id)

  render json:
           success_json.merge(BookmarkManager.(destroyed_bookmark, current_user))
end

#destroy_manyObject



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'app/controllers/posts_controller.rb', line 400

def destroy_many
  params.require(:post_ids)
  agree_with_first_reply_flag = (params[:agree_with_first_reply_flag] || true).to_s == "true"

  posts = Post.where(id: post_ids_including_replies).order(:id)
  raise Discourse::InvalidParameters.new(:post_ids) if posts.blank?

  # Make sure we can delete the posts
  posts.each { |p| guardian.ensure_can_delete!(p) }

  Post.transaction do
    posts.each_with_index do |p, i|
      PostDestroyer.new(
        current_user,
        p,
        defer_flags: !(agree_with_first_reply_flag && i == 0),
      ).destroy
    end
  end

  render body: nil
end

#expand_embedObject



368
369
370
371
372
# File 'app/controllers/posts_controller.rb', line 368

def expand_embed
  render json: { cooked: TopicEmbed.expanded_for(find_post_from_params) }
rescue StandardError
  render_json_error I18n.t("errors.embed.load_from_remote")
end

#hide_revisionObject



487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'app/controllers/posts_controller.rb', line 487

def hide_revision
  post_revision = find_post_revision_from_params
  guardian.ensure_can_hide_post_revision!(post_revision)

  post_revision.hide!

  post = find_post_from_params
  post.public_version -= 1
  post.save

  post.publish_change_to_clients!(:revised)

  render body: nil
end

#latestObject



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
135
136
137
# File 'app/controllers/posts_controller.rb', line 61

def latest
  params.permit(:before)
  last_post_id = params[:before].to_i
  last_post_id = nil if last_post_id <= 0

  if params[:id] == "private_posts"
    raise Discourse::NotFound if current_user.nil?

    allowed_private_topics = TopicAllowedUser.where(user_id: current_user.id).select(:topic_id)

    allowed_groups = GroupUser.where(user_id: current_user.id).select(:group_id)
    allowed_private_topics_by_group =
      TopicAllowedGroup.where(group_id: allowed_groups).select(:topic_id)

    all_allowed =
      Topic
        .where(id: allowed_private_topics)
        .or(Topic.where(id: allowed_private_topics_by_group))
        .select(:id)

    posts =
      Post
        .private_posts
        .order(id: :desc)
        .includes(topic: :category)
        .includes(user: %i[primary_group flair_group])
        .includes(:reply_to_user)
        .limit(50)
    rss_description = I18n.t("rss_description.private_posts")

    posts = posts.where(topic_id: all_allowed) if !current_user.admin?
  else
    posts =
      Post
        .public_posts
        .visible
        .where(post_type: Post.types[:regular])
        .order(id: :desc)
        .includes(topic: :category)
        .includes(user: %i[primary_group flair_group])
        .includes(:reply_to_user)
        .where("categories.id" => Category.secured(guardian).select(:id))
        .limit(50)

    rss_description = I18n.t("rss_description.posts")
    @use_canonical = true
  end

  posts = posts.where("posts.id <= ?", last_post_id) if last_post_id

  posts = posts.to_a

  counts = PostAction.counts_for(posts, current_user)

  respond_to do |format|
    format.rss do
      @posts = posts
      @title = "#{SiteSetting.title} - #{rss_description}"
      @link = Discourse.base_url
      @description = rss_description
      render "posts/latest", formats: [:rss]
    end
    format.json do
      render_json_dump(
        serialize_data(
          posts,
          PostSerializer,
          scope: guardian,
          root: params[:id],
          add_raw: true,
          add_title: true,
          all_post_actions: counts,
        ),
      )
    end
  end
end

#latest_revisionObject



477
478
479
480
481
482
483
484
485
# File 'app/controllers/posts_controller.rb', line 477

def latest_revision
  post = find_post_from_params
  raise Discourse::NotFound if post.hidden && !guardian.can_view_hidden_post_revisions?

  post_revision = find_latest_post_revision_from_params
  post_revision_serializer =
    PostRevisionSerializer.new(post_revision, scope: guardian, root: false)
  render_json_dump(post_revision_serializer)
end

#lockedObject



612
613
614
615
616
617
# File 'app/controllers/posts_controller.rb', line 612

def locked
  post = find_post_from_params
  locker = PostLocker.new(post, current_user)
  params[:locked] === "true" ? locker.lock : locker.unlock
  render_json_dump(locked: post.locked?)
end

#markdown_idObject



32
33
34
# File 'app/controllers/posts_controller.rb', line 32

def markdown_id
  markdown Post.find_by(id: params[:id].to_i)
end

#markdown_numObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/posts_controller.rb', line 36

def markdown_num
  if params[:revision].present?
    post_revision = find_post_revision_from_topic_id
    render plain: post_revision.modifications[:raw].last
  elsif params[:post_number].present?
    markdown Post.find_by(
               topic_id: params[:topic_id].to_i,
               post_number: params[:post_number].to_i,
             )
  else
    opts = params.slice(:page)
    opts[:limit] = MARKDOWN_TOPIC_PAGE_SIZE
    topic_view = TopicView.new(params[:topic_id], current_user, opts)
    content = topic_view.posts.map { |p| <<~MD }
        #{p.user.username} | #{p.updated_at} | ##{p.post_number}

        #{p.raw}

        -------------------------

      MD
    render plain: content.join
  end
end

#merge_postsObject



423
424
425
426
427
428
429
430
431
# File 'app/controllers/posts_controller.rb', line 423

def merge_posts
  params.require(:post_ids)
  posts = Post.where(id: params[:post_ids]).order(:id)
  raise Discourse::InvalidParameters.new(:post_ids) if posts.pluck(:id) == params[:post_ids]
  PostMerger.new(current_user, posts).merge
  render body: nil
rescue PostMerger::CannotMergeError => e
  render_json_error(e.message)
end

#noticeObject



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'app/controllers/posts_controller.rb', line 619

def notice
  post = find_post_from_params
  raise Discourse::NotFound unless guardian.can_edit_staff_notes?(post.topic)

  old_notice = post.custom_fields[Post::NOTICE]

  if params[:notice].present?
    post.custom_fields[Post::NOTICE] = {
      type: Post.notices[:custom],
      raw: params[:notice],
      cooked: PrettyText.cook(params[:notice], features: { onebox: false }),
    }
  else
    post.custom_fields.delete(Post::NOTICE)
  end

  post.save_custom_fields

  StaffActionLogger.new(current_user).log_post_staff_note(
    post,
    old_value: old_notice&.[]("raw"),
    new_value: params[:notice],
  )

  render body: nil
end

#pendingObject



716
717
718
719
720
721
722
723
724
725
726
# File 'app/controllers/posts_controller.rb', line 716

def pending
  params.require(:username)
  user = fetch_user_from_params
  raise Discourse::NotFound unless guardian.can_edit_user?(user)

  render_serialized(
    user.pending_posts.order(created_at: :desc),
    PendingPostSerializer,
    root: :pending_posts,
  )
end

#permanently_delete_revisionsObject



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'app/controllers/posts_controller.rb', line 502

def permanently_delete_revisions
  guardian.ensure_can_permanently_delete_post_revisions!

  post = find_post_from_params
  raise Discourse::InvalidParameters.new(:post) if post.blank?
  raise Discourse::NotFound if post.revisions.blank?

  RateLimiter.new(
    current_user,
    "admin_permanently_delete_post_revisions",
    20,
    1.minute,
    apply_limit_to_staff: true,
  ).performed!

  ActiveRecord::Base.transaction do
    updated_at = Time.zone.now
    post.revisions.destroy_all
    post.update(version: 1, public_version: 1, last_version_at: updated_at)
    StaffActionLogger.new(current_user).log_permanently_delete_post_revisions(post)
  end

  post.rebake!

  render body: nil
end

#post_typeObject



671
672
673
674
675
676
677
678
679
680
# File 'app/controllers/posts_controller.rb', line 671

def post_type
  guardian.ensure_can_change_post_type!
  post = find_post_from_params
  params.require(:post_type)
  raise Discourse::InvalidParameters.new(:post_type) if Post.types[params[:post_type].to_i].blank?

  post.revise(current_user, post_type: params[:post_type].to_i)

  render body: nil
end

#raw_emailObject



177
178
179
180
181
182
183
# File 'app/controllers/posts_controller.rb', line 177

def raw_email
  params.require(:id)
  post = Post.unscoped.find(params[:id].to_i)
  guardian.ensure_can_view_raw_email!(post)
  text, html = Email.extract_parts(post.raw_email)
  render json: { raw_email: post.raw_email, text_part: text, html_part: html }
end

#rebakeObject



682
683
684
685
686
687
688
689
# File 'app/controllers/posts_controller.rb', line 682

def rebake
  guardian.ensure_can_rebake!

  post = find_post_from_params
  post.rebake!(invalidate_oneboxes: true, invalidate_broken_images: true)

  render body: nil
end

#recoverObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/controllers/posts_controller.rb', line 374

def recover
  post = find_post_from_params
  guardian.ensure_can_recover_post!(post)

  unless guardian.can_moderate_topic?(post.topic)
    RateLimiter.new(
      current_user,
      "delete_post_per_min",
      SiteSetting.max_post_deletions_per_minute,
      1.minute,
    ).performed!
    RateLimiter.new(
      current_user,
      "delete_post_per_day",
      SiteSetting.max_post_deletions_per_day,
      1.day,
    ).performed!
  end

  destroyer = PostDestroyer.new(current_user, post)
  destroyer.recover
  post.reload

  render_post_json(post)
end

#repliesObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'app/controllers/posts_controller.rb', line 435

def replies
  params.permit(:after)

  after = [params[:after].to_i, 1].max
  post = find_post_from_params

  post_ids =
    post
      .replies
      .secured(guardian)
      .where(post_number: after + 1..)
      .limit(MAX_POST_REPLIES)
      .pluck(:id)

  if post_ids.blank?
    render_json_dump []
  else
    topic_view =
      TopicView.new(
        post.topic,
        current_user,
        post_ids:,
        include_related: false,
        include_suggested: false,
      )

    render_json_dump(
      TopicViewPostsSerializer.new(topic_view, scope: guardian).post_stream[:posts],
    )
  end
end

#reply_historyObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'app/controllers/posts_controller.rb', line 311

def reply_history
  post = find_post_from_params

  topic_view =
    TopicView.new(
      post.topic,
      current_user,
      include_suggested: false,
      include_related: false,
      reply_history_for: post.id,
    )

  render_json_dump(TopicViewPostsSerializer.new(topic_view, scope: guardian).post_stream[:posts])
end

#reply_idsObject



326
327
328
329
# File 'app/controllers/posts_controller.rb', line 326

def reply_ids
  post = find_post_from_params
  render json: post.reply_ids(guardian).to_json
end

#revertObject



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'app/controllers/posts_controller.rb', line 544

def revert
  raise Discourse::NotFound unless guardian.is_staff?

  post_id = params[:id] || params[:post_id]
  revision = params[:revision].to_i
  raise Discourse::InvalidParameters.new(:revision) if revision < 2

  post_revision = PostRevision.find_by(post_id: post_id, number: revision)
  raise Discourse::NotFound unless post_revision

  post = find_post_from_params
  raise Discourse::NotFound if post.blank?

  post_revision.post = post
  guardian.ensure_can_see!(post_revision)
  guardian.ensure_can_edit!(post)
  if post_revision.modifications["raw"].blank? && post_revision.modifications["title"].blank? &&
       post_revision.modifications["category_id"].blank?
    return render_json_error(I18n.t("revert_version_same"))
  end

  topic = Topic.with_deleted.find(post.topic_id)

  changes = {}
  changes[:raw] = post_revision.modifications["raw"][0] if post_revision.modifications[
    "raw"
  ].present? && post_revision.modifications["raw"][0] != post.raw
  if post.is_first_post?
    changes[:title] = post_revision.modifications["title"][0] if post_revision.modifications[
      "title"
    ].present? && post_revision.modifications["title"][0] != topic.title
    changes[:category_id] = post_revision.modifications["category_id"][
      0
    ] if post_revision.modifications["category_id"].present? &&
      post_revision.modifications["category_id"][0] != topic.category.id
  end
  return render_json_error(I18n.t("revert_version_same")) if changes.length <= 0
  changes[:edit_reason] = I18n.with_locale(SiteSetting.default_locale) do
    I18n.t("reverted_to_version", version: post_revision.number.to_i - 1)
  end

  revisor = PostRevisor.new(post, topic)
  revisor.revise!(current_user, changes)

  return render_json_error(post) if post.errors.present?
  return render_json_error(topic) if topic.errors.present?

  post_serializer = PostSerializer.new(post, scope: guardian, root: false)
  post_serializer.draft_sequence = DraftSequence.current(current_user, topic.draft_key)

  link_counts = TopicLink.counts_for(guardian, topic, [post])
  post_serializer.single_post_link_counts = link_counts[post.id] if link_counts.present?

  result = { post: post_serializer.as_json }
  if post.is_first_post?
    result[:topic] = BasicTopicSerializer.new(
      topic,
      scope: guardian,
      root: false,
    ).as_json if post_revision.modifications["title"].present?
    result[:category_id] = post_revision.modifications["category_id"][
      0
    ] if post_revision.modifications["category_id"].present?
  end

  render_json_dump(result)
end

#revisionsObject



467
468
469
470
471
472
473
474
475
# File 'app/controllers/posts_controller.rb', line 467

def revisions
  post = find_post_from_params
  raise Discourse::NotFound if post.hidden && !guardian.can_view_hidden_post_revisions?

  post_revision = find_post_revision_from_params
  post_revision_serializer =
    PostRevisionSerializer.new(post_revision, scope: guardian, root: false)
  render_json_dump(post_revision_serializer)
end


185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/posts_controller.rb', line 185

def short_link
  post = Post.find_by(id: params[:post_id].to_i)
  raise Discourse::NotFound unless post

  # Stuff the user in the request object, because that's what IncomingLink wants
  if params[:user_id]
    user = User.find_by(id: params[:user_id].to_i)
    request["u"] = user.username_lower if user
  end

  guardian.ensure_can_see!(post)
  redirect_to path(post.url)
end

#showObject



296
297
298
299
# File 'app/controllers/posts_controller.rb', line 296

def show
  post = find_post_from_params
  display_post(post)
end

#show_revisionObject



529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'app/controllers/posts_controller.rb', line 529

def show_revision
  post_revision = find_post_revision_from_params
  guardian.ensure_can_show_post_revision!(post_revision)

  post_revision.show!

  post = find_post_from_params
  post.public_version += 1
  post.save

  post.publish_change_to_clients!(:revised)

  render body: nil
end

#unhideObject



691
692
693
694
695
696
697
698
699
# File 'app/controllers/posts_controller.rb', line 691

def unhide
  post = find_post_from_params

  guardian.ensure_can_unhide!(post)

  post.unhide!

  render body: nil
end

#updateObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/controllers/posts_controller.rb', line 221

def update
  params.require(:post)

  post = Post.where(id: params[:id])
  post = post.with_deleted if guardian.is_staff?
  post = post.first

  raise Discourse::NotFound if post.blank?

  post.image_sizes = params[:image_sizes] if params[:image_sizes].present?

  if !guardian.public_send("can_edit?", post) && post.user_id == current_user.id &&
       post.edit_time_limit_expired?(current_user)
    return render_json_error(I18n.t("too_late_to_edit"))
  end

  guardian.ensure_can_edit!(post)

  changes = { raw: params[:post][:raw], edit_reason: params[:post][:edit_reason] }

  Post.plugin_permitted_update_params.keys.each { |param| changes[param] = params[:post][param] }

  # keep `raw_old` for backwards compatibility
  original_text = params[:post][:original_text] || params[:post][:raw_old]
  if original_text.present? && original_text != post.raw
    return render_json_error(I18n.t("edit_conflict"), status: 409)
  end

  # to stay consistent with the create api, we allow for title & category changes here
  if post.is_first_post?
    changes[:title] = params[:title] if params[:title]
    changes[:category_id] = params[:post][:category_id] if params[:post][:category_id]

    if changes[:category_id] && changes[:category_id].to_i != post.topic.category_id.to_i
      category = Category.find_by(id: changes[:category_id])
      if category || (changes[:category_id].to_i == 0)
        guardian.ensure_can_move_topic_to_category!(category)
      else
        return render_json_error(I18n.t("category.errors.not_found"))
      end
    end
  end

  # We don't need to validate edits to small action posts by staff
  opts = {}
  if post.post_type == Post.types[:small_action] && current_user.staff?
    opts[:skip_validations] = true
  end

  topic = post.topic
  topic = Topic.with_deleted.find(post.topic_id) if guardian.is_staff?

  revisor = PostRevisor.new(post, topic)
  revisor.revise!(current_user, changes, opts)

  return render_json_error(post) if post.errors.present?
  return render_json_error(topic) if topic.errors.present?

  post_serializer = PostSerializer.new(post, scope: guardian, root: false, add_raw: true)
  post_serializer.draft_sequence = DraftSequence.current(current_user, topic.draft_key)
  link_counts = TopicLink.counts_for(guardian, topic, [post])
  post_serializer.single_post_link_counts = link_counts[post.id] if link_counts.present?

  result = { post: post_serializer.as_json }
  if revisor.category_changed.present?
    result[:category] = BasicCategorySerializer.new(
      revisor.category_changed,
      scope: guardian,
      root: false,
    ).as_json
  end

  render_json_dump(result)
end

#user_posts_feedObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/posts_controller.rb', line 139

def user_posts_feed
  params.require(:username)
  user = fetch_user_from_params
  raise Discourse::NotFound unless guardian.can_see_profile?(user)

  posts =
    Post
      .public_posts
      .visible
      .where(user_id: user.id)
      .where(post_type: Post.types[:regular])
      .order(created_at: :desc)
      .includes(:user)
      .includes(topic: :category)
      .limit(50)

  posts = posts.reject { |post| !guardian.can_see?(post) || post.topic.blank? }

  respond_to do |format|
    format.rss do
      @posts = posts
      @title =
        "#{SiteSetting.title} - #{I18n.t("rss_description.user_posts", username: user.username)}"
      @link = "#{user.full_url}/activity"
      @description = I18n.t("rss_description.user_posts", username: user.username)
      render "posts/latest", formats: [:rss]
    end

    format.json do
      render_json_dump(serialize_data(posts, PostSerializer, scope: guardian, add_excerpt: true))
    end
  end
end

#wikiObject



661
662
663
664
665
666
667
668
669
# File 'app/controllers/posts_controller.rb', line 661

def wiki
  post = find_post_from_params
  params.require(:wiki)
  guardian.ensure_can_wiki!(post)

  post.revise(current_user, wiki: params[:wiki])

  render body: nil
end