Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deletes spam comments, posts and local spam accounts. Place into Diasporas root, edit and run. Don't forget to set necessary environment variables.
#!/usr/bin/env ruby
# List of spam accounts
spam_accounts = %w(spamacc@podA spamacc@podB spamacc@mypod)
# Delete comments even if spammer isn't a local user or spam isn't on a
# local users account.
# And delete posts of users in spam_accounts that aren't local.
always_delete = true
# Keep empty (%w() or []) to retract spam comments from remote accounts
# for all local users
retract_for = %w(userA userB)
###########################################################################
# Load diaspora environment
ENV['RAILS_ENV'] ||= "production"
require_relative 'config/environment'
local_spammers, remote_spammers = Person.where(diaspora_handle: spam_accounts).partition(&:local?)
# Retract all comments of local spammers and close their accounts
local_spammers.each do |spammer|
Comment.where(author_id: spammer.id).each do |comment|
spammer.owner.retract(comment)
end
spammer.owner.close_account!
end
# Retract all spam comments on posts of local users and delete the rest
Comment.where(author_id: remote_spammers.map(&:id)).each do |comment|
post_author = comment.parent.author
if post_author.local? && (retract_for.include?(post_author.owner.username) || retract_for.empty?)
post_author.owner.retract(comment)
elsif always_delete
comment.destroy
end
end
# Destroy posts of remote users if wanted
if always_delete
Post.where(author_id: remote_spammers.map(&:id)).each(&:destroy)
end
@dmorley

This comment has been minimized.

Copy link

dmorley commented Feb 23, 2014

Getting a

"/home/david/.rvm/gems/ruby-2.0.0-p353@diaspora/gems/activerecord-3.2.17/lib/active_record/relation/delegation.rb:40:in `partition': wrong number of arguments (1 for 0) (ArgumentError)
    from /home/david/.rvm/gems/ruby-2.0.0-p353@diaspora/gems/activerecord-3.2.17/lib/active_record/relation/delegation.rb:40:in `method_missing'
    from spam.rb:19:in `<main>'" 

error, looking for answers and not finding much on that error

@jhass

This comment has been minimized.

Copy link
Owner Author

jhass commented Feb 23, 2014

Whoops, missed a &, Try the updated version.

@jaywink

This comment has been minimized.

Copy link

jaywink commented Feb 23, 2014

I got it working by changing line 19 to this:

local_spammers, remote_spammers = Person.where(diaspora_handle: spam_accounts).partition { |account| account.local? }

But & would be better?

@jhass

This comment has been minimized.

Copy link
Owner Author

jhass commented Feb 23, 2014

It's really the same thing, just a shortcut to write what you wrote ;)

@ajya

This comment has been minimized.

Copy link

ajya commented May 5, 2018

if anyone else needs, I have a forked version that also deletes spam "likes" and local profiles of spammers, https://gist.github.com/ajya/bdeb1c69330c85bcb400297c319d5be4/revisions#diff-79fdb7c3dc82d2ad8e5713fe5f2dfaf7

@Vertux

This comment has been minimized.

Copy link

Vertux commented Mar 27, 2019

@ajya
@jhass
These scripts are very helpfull, thank you very much.
I have a little suggestion for further improvement. If the script encounters an account entry which was already deleted/blocked, it throws an error and stops the execution. It would be nice it would handle this stituation by skipping those entries and continue execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.