Skip Navigation
Lemmy.ca's Main Community @lemmy.ca

Sorry, everyone's favorite fediverse chick is now being filtered

Sorry everyone I know how much you love the attention she gives you, but I've implemented some quick and dirty filtering for private messaging.

We now have the ability to automatically mark PM's as deleted or read, depending on content inside of them. If we accidentally filter something you legitimately wanted (ie, not Nicole) please let me know.

If any other instances would like to implement this, here's the code. Note that you'll need to set your hostname at the top here for some reason I haven't exactly identified.

 
    
SET lemmy.protocol_and_hostname = 'https://lemmy.ca/';

CREATE TABLE private_message_filters (
    id SERIAL PRIMARY KEY,
    phrase TEXT NOT NULL,
    behavior VARCHAR(10) NOT NULL CHECK (behavior IN ('delete', 'mark_read'))
);

CREATE OR REPLACE FUNCTION filter_private_messages()
RETURNS trigger AS $$
DECLARE
    banned_phrase_record private_message_filters%ROWTYPE;
BEGIN
    FOR banned_phrase_record IN 
        SELECT * FROM private_message_filters
    LOOP
        IF LOWER(TRIM(NEW.content)) ILIKE '%' || LOWER(TRIM(banned_phrase_record.phrase)) || '%' THEN
            IF banned_phrase_record.behavior = 'delete' THEN
                NEW.deleted := true;
                RETURN NEW;
            ELSIF banned_phrase_record.behavior = 'mark_read' THEN
                NEW.read := true;
                RETURN NEW;
            END IF;
        END IF;
    END LOOP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_filter_private_messages
AFTER INSERT ON private_message
FOR EACH ROW
EXECUTE FUNCTION filter_private_messages();

  

To add filter words:

 
    
insert into private_message_filters (behavior, phrase) values ('delete', 'spamtestdelete');
insert into private_message_filters (behavior, phrase) values ('mark_read', 'spamtestread');

  

If you want to quickly disable / enable filtering while testing:

 
    
ALTER TABLE private_message DISABLE TRIGGER trg_filter_private_messages;
ALTER TABLE private_message ENABLE TRIGGER trg_filter_private_messages;

  

I'll leave it up to you to figure out what phrases to filter on. MAKE SURE YOU TEST. If there's an error, private messaging could break completely. You should not get an error message from the UI while sending a message with a banned word.

41 comments
  • But I love her and she loves me.

    • I saw her send DMs to Not Dr. Wesker, too.

      I'm sorry to be the bearer of bad news.

    • I love her, she loves me

      I'm her he, she's my she

      Though for looks she's only fair

      And her figure isn't there

      Her figure is much bigger in the bank, well I declare

      Her papa, he has dough, he has dough, and

      And she's his only child, and she's awfully wild

      So I love her and she loves me

  • This is sorta OK for this one case, but we really need more hardened and easy solutions as the spam problem will only get worse. The DMs are very vulnerable, which is why services like bsky disable them for all new accounts for example.

    Went ahead and opened some issues

    • 100% agreed, this was intended as a quick fix and not a long term solution. There should also be rate limiting on both the sending + receiving instance. Maybe the ability to mark an account as an "Authorized bulk sender" but otherwise if you send more than X DMs to Y different people within Z hours, you get blocked.

      Both your issues LGTM, thanks for writing them up!

  • I still got the notification via Android, but the message was gone and was curious if y'all had finally nuked this problem.

    But, finally get Nicole'd and my only chance for a real, meaningful relationship goes up in smoke. Thanks Obama.

  • You can also just insert arbitrary strings into the local_site_url_blocklist table as Lemmy only checks the validity of the URLs when editing the site through the API. This has the advantage of preventing the messages even getting written to the DB. Example:

      sql
        
    insert into local_site_url_blocklist (url) values ('Hi, I''m Nicole! But you can call me the Fediverse Chick :D');
    
      
  • I’ve got DMs from her for almost two years, every 3 months, like clockwork. The photos changed over time, which I found interesting.

    I won’t be sad to not get any more DMs though. They’re annoying.

41 comments