mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: add API endpoint to destroy_timings only of last post
Previously API only allowed you to nuke all timings from a topic, new API is less punishing and allows you just to remove 1 post.
This commit is contained in:
parent
296928ec04
commit
80ceb57c76
@ -250,7 +250,12 @@ class TopicsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy_timings
|
def destroy_timings
|
||||||
|
if params[:last].to_s == "1"
|
||||||
|
PostTiming.destroy_last_for(current_user, params[:topic_id])
|
||||||
|
else
|
||||||
PostTiming.destroy_for(current_user.id, [params[:topic_id].to_i])
|
PostTiming.destroy_for(current_user.id, [params[:topic_id].to_i])
|
||||||
|
end
|
||||||
|
|
||||||
render body: nil
|
render body: nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,6 +64,25 @@ class PostTiming < ActiveRecord::Base
|
|||||||
record_new_timing(args) if rows == 0
|
record_new_timing(args) if rows == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.destroy_last_for(user, topic_id)
|
||||||
|
topic = Topic.find(topic_id)
|
||||||
|
post_number = user.staff? ? topic.highest_staff_post_number : topic.highest_post_number
|
||||||
|
|
||||||
|
last_read = post_number - 1
|
||||||
|
|
||||||
|
PostTiming.transaction do
|
||||||
|
PostTiming.where("topic_id = ? AND user_id = ? AND post_number > ?", topic.id, user.id, last_read).delete_all
|
||||||
|
if last_read < 1
|
||||||
|
last_read = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
TopicUser.where(user_id: user.id, topic_id: topic.id).update_all(
|
||||||
|
highest_seen_post_number: last_read,
|
||||||
|
last_read_post_number: last_read
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.destroy_for(user_id, topic_ids)
|
def self.destroy_for(user_id, topic_ids)
|
||||||
PostTiming.transaction do
|
PostTiming.transaction do
|
||||||
PostTiming
|
PostTiming
|
||||||
|
@ -529,6 +529,44 @@ RSpec.describe TopicsController do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'for last post only' do
|
||||||
|
|
||||||
|
it 'should allow you to retain topic timing but remove last post only' do
|
||||||
|
post1 = create_post
|
||||||
|
topic = post1.topic
|
||||||
|
|
||||||
|
post2 = create_post(topic_id: topic.id)
|
||||||
|
|
||||||
|
PostTiming.create!(topic: topic, user: user, post_number: 1, msecs: 100)
|
||||||
|
PostTiming.create!(topic: topic, user: user, post_number: 2, msecs: 100)
|
||||||
|
|
||||||
|
TopicUser.create!(
|
||||||
|
topic: topic,
|
||||||
|
user: user,
|
||||||
|
last_read_post_number: 2,
|
||||||
|
highest_seen_post_number: 2
|
||||||
|
)
|
||||||
|
|
||||||
|
sign_in(user)
|
||||||
|
|
||||||
|
delete "/t/#{topic.id}/timings.json?last=1"
|
||||||
|
|
||||||
|
expect(PostTiming.where(topic: topic, user: user, post_number: 2).exists?).to eq(false)
|
||||||
|
expect(PostTiming.where(topic: topic, user: user, post_number: 1).exists?).to eq(true)
|
||||||
|
|
||||||
|
expect(TopicUser.where(topic: topic, user: user, last_read_post_number: 1, highest_seen_post_number: 1).exists?).to eq(true)
|
||||||
|
|
||||||
|
PostDestroyer.new(Fabricate(:admin), post2).destroy
|
||||||
|
|
||||||
|
delete "/t/#{topic.id}/timings.json?last=1"
|
||||||
|
|
||||||
|
expect(PostTiming.where(topic: topic, user: user, post_number: 1).exists?).to eq(false)
|
||||||
|
expect(TopicUser.where(topic: topic, user: user, last_read_post_number: nil, highest_seen_post_number: nil).exists?).to eq(true)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
context 'when logged in' do
|
context 'when logged in' do
|
||||||
before do
|
before do
|
||||||
@user = sign_in(Fabricate(:user))
|
@user = sign_in(Fabricate(:user))
|
||||||
|
Loading…
Reference in New Issue
Block a user