Class: TopicTimestampChanger

Inherits:
Object
  • Object
show all
Defined in:
app/services/topic_timestamp_changer.rb

Defined Under Namespace

Classes: InvalidTimestampError

Instance Method Summary collapse

Constructor Details

#initialize(timestamp:, topic: nil, topic_id: nil) ⇒ TopicTimestampChanger

Returns a new instance of TopicTimestampChanger.



7
8
9
10
11
12
13
14
15
16
# File 'app/services/topic_timestamp_changer.rb', line 7

def initialize(timestamp:, topic: nil, topic_id: nil)
  @topic = topic || Topic.with_deleted.find(topic_id)
  @posts = @topic.posts
  @current_timestamp = Time.zone.now
  @timestamp = Time.zone.at(timestamp)

  raise InvalidTimestampError if @timestamp.to_f > @current_timestamp.to_f

  @time_difference = calculate_time_difference
end

Instance Method Details

#change!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/topic_timestamp_changer.rb', line 18

def change!
  ActiveRecord::Base.transaction do
    last_posted_at = @timestamp

    @posts.each do |post|
      if post.is_first_post?
        update_post(post, @timestamp)
      else
        new_created_at = Time.at(post.created_at.to_f + @time_difference)
        new_created_at = @current_timestamp if new_created_at > @current_timestamp
        last_posted_at = new_created_at if new_created_at > last_posted_at
        update_post(post, new_created_at)
      end
    end

    @topic.reset_bumped_at
    update_topic(last_posted_at)

    yield(@topic) if block_given?
  end

  # Burst the cache for stats
  [AdminDashboardData, About].each { |klass| Discourse.redis.del klass.stats_cache_key }
end