Browse Source

Prevent inserting subsequent topic visits

This should prevent a few strange behaviors related to topic visits,
such as "losing" new comments if you accidentally double-click when
entering a topic's comments.

Currently, the "grace period" is set to 2 minutes, and no new visits
will be stored until the previous visit is at least that old.
merge-requests/92/head
Deimos 5 years ago
parent
commit
0fe14d3c37
  1. 51
      tildes/alembic/versions/34753d8124b4_prevent_inserting_subsequent_topic_.py
  2. 24
      tildes/sql/init/triggers/topic_visits/topic_visits.sql

51
tildes/alembic/versions/34753d8124b4_prevent_inserting_subsequent_topic_.py

@ -0,0 +1,51 @@
"""Prevent inserting subsequent topic_visits
Revision ID: 34753d8124b4
Revises: 19400b1efe8b
Create Date: 2020-01-30 19:37:55.095357
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "34753d8124b4"
down_revision = "19400b1efe8b"
branch_labels = None
depends_on = None
def upgrade():
op.execute(
"""
create or replace function prevent_recent_repeat_visits() returns trigger as $$
begin
perform * from topic_visits
where user_id = NEW.user_id
and topic_id = NEW.topic_id
and visit_time >= now() - interval '2 minutes';
if (FOUND) then
return null;
else
return NEW;
end if;
end;
$$ language plpgsql;
"""
)
op.execute(
"""
create trigger prevent_recent_repeat_visits_insert
before insert on topic_visits
for each row
execute procedure prevent_recent_repeat_visits();
"""
)
def downgrade():
op.execute("drop trigger prevent_recent_repeat_visits_insert on topic_visits")
op.execute("drop function prevent_recent_repeat_visits")

24
tildes/sql/init/triggers/topic_visits/topic_visits.sql

@ -0,0 +1,24 @@
-- Copyright (c) 2020 Tildes contributors <code@tildes.net>
-- SPDX-License-Identifier: AGPL-3.0-or-later
-- prevent inserting another visit immediately after an existing one
create or replace function prevent_recent_repeat_visits() returns trigger as $$
begin
perform * from topic_visits
where user_id = NEW.user_id
and topic_id = NEW.topic_id
and visit_time >= now() - interval '2 minutes';
if (FOUND) then
return null;
else
return NEW;
end if;
end;
$$ language plpgsql;
create trigger prevent_recent_repeat_visits_insert
before insert on topic_visits
for each row
execute procedure prevent_recent_repeat_visits();
Loading…
Cancel
Save