Changesets can be listed by changeset number.
The Git repository is here.
Changeset 473
Add simple topic title blacklist.
- Comitted by: rool
- Date: Thursday January 31 09:48:46 2019 (over 5 years ago)
Affected files:
- rool/rails/beast/trunk/db/migrate/049_add_title_blacklist.rb
- rool/rails/beast/trunk/app/controllers/blacklist_controller.rb (diff)
- rool/rails/beast/trunk/app/models/blacklist.rb (diff)
- rool/rails/beast/trunk/app/models/post.rb (diff)
- rool/rails/beast/trunk/app/models/topic.rb (diff)
- rool/rails/beast/trunk/app/views/blacklist/_form.rhtml (diff)
- rool/rails/beast/trunk/app/views/blacklist/show.rhtml (diff)
rool/rails/beast/trunk/app/controllers/blacklist_controller.rb:
prev. | current | |
# | ||
class BlacklistController < ApplicationController | ||
@@hubssolib_permissions = HubSsoLib::Permissions.new({ | ||
9 | :show => [ :admin, :webmaster ], | |
:new => [ :admin, :webmaster ], | ||
:create => [ :admin, :webmaster ], | ||
:edit => [ :admin, :webmaster ], | ||
... | ... | |
end | ||
def create | ||
31 | | |
32 | set_list_to( params[ :blacklist ] ) | |
flash[ :notice ] = 'Blacklist created' | ||
redirect_to( blacklist_path() ) | ||
end | ||
... | ... | |
end | ||
def update | ||
41 | | |
42 | set_list_to( params[ :blacklist ] ) | |
flash[ :notice ] = 'Blacklist updated' | ||
redirect_to( blacklist_path() ) | ||
end | ||
def destroy | ||
47 | | |
48 | set_list_to( { :list => '', :title_list => '' } ) | |
flash[ :notice ] = 'Blacklist emptied' | ||
redirect_to( blacklist_path() ) | ||
end | ||
private | ||
54 | | |
55 | def set_list_to( options ) | |
56 | list = options[ :list ] | |
57 | title_list = options[ :title_list ] | |
58 | ||
blacklist = if Blacklist.count > 0 | ||
Blacklist.find(:first) | ||
else | ||
Blacklist.new | ||
end | ||
61 | | |
65 | blacklist.list = list | |
66 | blacklist.title_list = title_list | |
67 | ||
blacklist.save! | ||
end | ||
rool/rails/beast/trunk/app/models/blacklist.rb:
prev. | current | |
# | ||
class Blacklist < ActiveRecord::Base | ||
9 | | |
9 | before_save :downcase_and_strip | |
11 | protected | |
12 | ||
13 | def downcase_and_strip | |
14 | self.list = tidy(self.list) | |
15 | self.title_list = tidy(self.title_list) | |
16 | end | |
17 | ||
18 | private | |
19 | ||
20 | def tidy(text) | |
21 | (text || '').split("\n").map { |item| item.strip.downcase }.join("\n") | |
22 | end | |
23 | ||
end |
rool/rails/beast/trunk/app/models/post.rb:
prev. | current | |
prohibited = false | ||
blacklist.split("\n").each do |item| | ||
32 | | |
32 | if downcase_body.include?(item) | |
prohibited = true | ||
break | ||
end |
rool/rails/beast/trunk/app/models/topic.rb:
prev. | current | |
end | ||
belongs_to :replied_by_user, :foreign_key => "replied_by", :class_name => "User" | ||
14 | | |
14 | ||
validates_presence_of :forum, :user, :title | ||
16 | validate :title_cannot_contain_blacklisted_strings | |
17 | ||
before_create :set_default_replied_at_and_sticky | ||
after_save :set_post_topic_id | ||
... | ... | |
def voices | ||
posts.map { |p| p.user_id }.uniq.size | ||
end | ||
26 | | |
28 | ||
def hit! | ||
self.class.increment_counter :hits, id | ||
end | ||
... | ... | |
def editable_by?(user) | ||
user && (user.id == user_id || user.admin? || user.moderator_of?(forum_id)) | ||
end | ||
46 | | |
48 | ||
protected | ||
def set_default_replied_at_and_sticky | ||
self.replied_at = Time.now.utc | ||
... | ... | |
def set_post_topic_id | ||
Post.update_all ['forum_id = ?', forum_id], ['topic_id = ?', id] | ||
end | ||
58 | ||
59 | def title_cannot_contain_blacklisted_strings | |
60 | downcase_title = title.downcase rescue '' | |
61 | blacklist = Blacklist.find(:first).title_list rescue '' | |
62 | prohibited = false | |
63 | ||
64 | blacklist.split("\n").each do |item| | |
65 | if downcase_title.include?(item) | |
66 | prohibited = true | |
67 | break | |
68 | end | |
69 | end | |
70 | ||
71 | errors.add(:title, "contains prohibited text") if prohibited == true | |
72 | end | |
end |
rool/rails/beast/trunk/app/views/blacklist/_form.rhtml:
prev. | current | |
</p> | ||
<p id="blacklist_list"> | ||
5 | ||
5 | <label>Post bodies</label><br /> | |
<%= form.text_area :list, :cols => 60, :rows => 20 %> | ||
<br /> | ||
8 | ||
8 | <span>Enter prohibited post body strings, separated by newlines. String matches are case-insensitive with no wildcards.</span> | |
</p> | ||
10 | ||
11 | <p id="blacklist_title_list"> | |
12 | <label>Topic titles</label><br /> | |
13 | <%= form.text_area :title_list, :cols => 60, :rows => 10 %> | |
14 | <br /> | |
15 | <span>Enter prohibited topic title strings, separated by newlines. String matches are case-insensitive with no wildcards.</span> | |
16 | </p> |
rool/rails/beast/trunk/app/views/blacklist/show.rhtml:
prev. | current | |
Blacklist | ||
</h1> | ||
8 | <h2>Post bodies</h2> | |
9 | ||
<table> | ||
<% @blacklist.list.split( "\n" ).each_with_index do | item, index | -%> | ||
<tr> | ||
... | ... | |
</tr> | ||
<% end -%> | ||
</table> | ||
17 | ||
18 | <br /> | |
19 | <h2>Topic titles</h2> | |
20 | ||
21 | <table> | |
22 | <% @blacklist.title_list.split( "\n" ).each_with_index do | item, index | -%> | |
23 | <tr> | |
24 | <th align="right"><%= index + 1 %></th><td align="left"><%= h( item.downcase ) %></td> | |
25 | </tr> | |
26 | <% end -%> | |
27 | </table> | |
28 | ||
29 | <p> | |
30 | <%= link_to "Edit blacklist", edit_blacklist_path() %> | |
31 | </p> |