|
1 | 1 | # rubocop:disable Metrics/ClassLength |
2 | 2 | # rubocop:disable Metrics/MethodLength |
3 | 3 | class PostsController < ApplicationController |
| 4 | + include DraftManagement |
| 5 | + |
4 | 6 | before_action :authenticate_user!, except: [:document, :help_center, :show] |
5 | 7 | before_action :set_post, only: [:toggle_comments, :feature, :lock, :unlock] |
6 | 8 | before_action :set_scoped_post, only: [:change_category, :show, :edit, :update, :close, :reopen, :delete, :restore] |
@@ -111,7 +113,7 @@ def create |
111 | 113 | Rails.cache.delete "community_user/#{current_user.community_user.id}/metric/#{key}" |
112 | 114 | end |
113 | 115 |
|
114 | | - do_draft_delete(URI(request.referer || '').path) |
| 116 | + do_delete_draft(current_user, URI(request.referer || '').path) |
115 | 117 |
|
116 | 118 | redirect_to helpers.generic_show_link(@post) |
117 | 119 | else |
@@ -261,7 +263,7 @@ def update |
261 | 263 | PostHistory.redact(@post, current_user) |
262 | 264 | end |
263 | 265 | Rails.cache.delete "community_user/#{current_user.community_user.id}/metric/E" |
264 | | - do_draft_delete(URI(request.referer || '').path) |
| 266 | + do_delete_draft(current_user, URI(request.referer || '').path) |
265 | 267 | redirect_to post_path(@post) |
266 | 268 | end |
267 | 269 |
|
@@ -300,7 +302,7 @@ def update |
300 | 302 | message += " on '#{@post.parent.title}'" |
301 | 303 | end |
302 | 304 | @post.user.create_notification message, suggested_edit_url(edit, host: @post.community.host) |
303 | | - do_draft_delete(URI(request.referer || '').path) |
| 305 | + do_delete_draft(current_user, URI(request.referer || '').path) |
304 | 306 | redirect_to post_path(@post) |
305 | 307 | else |
306 | 308 | @post.errors.copy!(edit.errors) |
@@ -631,41 +633,13 @@ def feature |
631 | 633 | render json: { status: 'success', success: true } |
632 | 634 | end |
633 | 635 |
|
634 | | - # saving by-field is kept for backwards compatibility with old drafts |
635 | 636 | def save_draft |
636 | | - expiration_time = 86_400 * 7 |
637 | | - |
638 | | - base_key = "saved_post.#{current_user.id}.#{params[:path]}" |
639 | | - |
640 | | - [:body, :comment, :excerpt, :license, :tag_name, :tags, :title].each do |key| |
641 | | - next unless params.key?(key) |
642 | | - |
643 | | - key_name = [:body, :saved_at].include?(key) ? base_key : "#{base_key}.#{key}" |
644 | | - |
645 | | - if key == :tags |
646 | | - valid_tags = params[key]&.select(&:present?) |
647 | | - |
648 | | - RequestContext.redis.del(key_name) |
649 | | - |
650 | | - if valid_tags.present? |
651 | | - RequestContext.redis.sadd(key_name, valid_tags) |
652 | | - end |
653 | | - else |
654 | | - RequestContext.redis.set(key_name, params[key]) |
655 | | - end |
656 | | - |
657 | | - RequestContext.redis.expire(key_name, expiration_time) |
658 | | - end |
659 | | - |
660 | | - saved_at_key = "saved_post_at.#{current_user.id}.#{params[:path]}" |
661 | | - RequestContext.redis.set(saved_at_key, DateTime.now.iso8601) |
662 | | - RequestContext.redis.expire(saved_at_key, expiration_time) |
663 | | - |
| 637 | + base_key = do_save_draft(current_user, params[:path]) |
664 | 638 | render json: { status: 'success', success: true, key: base_key } |
665 | 639 | end |
666 | 640 |
|
667 | 641 | def delete_draft |
668 | | - do_draft_delete(params[:path]) |
| 642 | + do_delete_draft(current_user, params[:path]) |
669 | 643 | render json: { status: 'success', success: true } |
670 | 644 | end |
671 | 645 |
|
@@ -726,19 +700,6 @@ def edit_checks |
726 | 700 | def unless_locked |
727 | 701 | check_if_locked(@post) |
728 | 702 | end |
729 | | - |
730 | | - # Attempts to actually delete a post draft |
731 | | - # @param path [String] draft path to delete |
732 | | - # @return [Boolean] status of the operation |
733 | | - def do_draft_delete(path) |
734 | | - keys = [:body, :comment, :excerpt, :license, :saved_at, :tags, :tag_name, :title].map do |key| |
735 | | - pfx = key == :saved_at ? 'saved_post_at' : 'saved_post' |
736 | | - base = "#{pfx}.#{current_user.id}.#{path}" |
737 | | - [:body, :saved_at].include?(key) ? base : "#{base}.#{key}" |
738 | | - end |
739 | | - |
740 | | - RequestContext.redis.del(*keys) |
741 | | - end |
742 | 703 | end |
743 | 704 | # rubocop:enable Metrics/MethodLength |
744 | 705 | # rubocop:enable Metrics/ClassLength |
0 commit comments