Browse Source

Send rabbitmq message when comment label added

merge-requests/65/head
Deimos 6 years ago
parent
commit
850eba75e1
  1. 60
      tildes/alembic/versions/beaa57144e49_comment_labels_send_rabbitmq_message_on_.py
  2. 30
      tildes/sql/init/triggers/comment_labels/rabbitmq.sql
  3. 1
      tildes/tildes/models/comment/comment_label.py

60
tildes/alembic/versions/beaa57144e49_comment_labels_send_rabbitmq_message_on_.py

@ -0,0 +1,60 @@
"""comment_labels: Send rabbitmq message on insert
Revision ID: beaa57144e49
Revises: 380a76d4a722
Create Date: 2019-03-28 18:37:58.995944
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "beaa57144e49"
down_revision = "380a76d4a722"
branch_labels = None
depends_on = None
def upgrade():
op.execute(
"""
CREATE OR REPLACE FUNCTION send_rabbitmq_message_for_comment_label() RETURNS TRIGGER AS $$
DECLARE
affected_row RECORD;
payload TEXT;
BEGIN
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
affected_row := NEW;
ELSIF (TG_OP = 'DELETE') THEN
affected_row := OLD;
END IF;
payload := json_build_object(
'comment_id', affected_row.comment_id,
'label', affected_row.label,
'user_id', affected_row.user_id);
PERFORM send_rabbitmq_message('comment_label.' || TG_ARGV[0], payload);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
"""
)
op.execute(
"""
CREATE TRIGGER send_rabbitmq_message_for_comment_label_insert
AFTER INSERT ON comment_labels
FOR EACH ROW
EXECUTE PROCEDURE send_rabbitmq_message_for_comment_label('created');
"""
)
def downgrade():
op.execute(
"DROP TRIGGER send_rabbitmq_message_for_comment_label_insert ON comment_labels"
)
op.execute("DROP FUNCTION send_rabbitmq_message_for_comment_label")

30
tildes/sql/init/triggers/comment_labels/rabbitmq.sql

@ -0,0 +1,30 @@
-- Copyright (c) 2019 Tildes contributors <code@tildes.net>
-- SPDX-License-Identifier: AGPL-3.0-or-later
CREATE OR REPLACE FUNCTION send_rabbitmq_message_for_comment_label() RETURNS TRIGGER AS $$
DECLARE
affected_row RECORD;
payload TEXT;
BEGIN
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
affected_row := NEW;
ELSIF (TG_OP = 'DELETE') THEN
affected_row := OLD;
END IF;
payload := json_build_object(
'comment_id', affected_row.comment_id,
'label', affected_row.label,
'user_id', affected_row.user_id);
PERFORM send_rabbitmq_message('comment_label.' || TG_ARGV[0], payload);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER send_rabbitmq_message_for_comment_label_insert
AFTER INSERT ON comment_labels
FOR EACH ROW
EXECUTE PROCEDURE send_rabbitmq_message_for_comment_label('created');

1
tildes/tildes/models/comment/comment_label.py

@ -25,6 +25,7 @@ class CommentLabel(DatabaseModel):
Outgoing:
- Inserting a row for an exemplary label will set last_exemplary_label_time for
the relevant user.
- Inserting a row will send a "comment_label.created" rabbitmq message.
"""
__tablename__ = "comment_labels"

Loading…
Cancel
Save