Changesets can be listed by changeset number.
The Git repository is here.
Changeset 471
Add 'quick and dirty' blacklist mechanism
- Comitted by: rool
- Date: Saturday January 26 09:02:22 2019 (over 5 years ago)
Affected files:
- rool/rails/beast/trunk/app/views/blacklist/
- rool/rails/beast/trunk/app/controllers/blacklist_controller.rb
- rool/rails/beast/trunk/app/models/blacklist.rb
- rool/rails/beast/trunk/app/views/blacklist/_form.rhtml
- rool/rails/beast/trunk/app/views/blacklist/edit.rhtml
- rool/rails/beast/trunk/app/views/blacklist/new.rhtml
- rool/rails/beast/trunk/app/views/blacklist/show.rhtml
- rool/rails/beast/trunk/db/migrate/048_add_blacklist.rb
- rool/rails/beast/trunk/app/controllers/posts_controller.rb (diff)
- rool/rails/beast/trunk/app/controllers/topics_controller.rb (diff)
- rool/rails/beast/trunk/app/models/post.rb (diff)
- rool/rails/beast/trunk/app/views/forums/index.rhtml (diff)
- rool/rails/beast/trunk/config/routes.rb (diff)
- rool/rails/beast/trunk/db/schema.rb (diff)
rool/rails/beast/trunk/app/controllers/posts_controller.rb:
prev. | current | |
index_initialise | ||
render_posts_or_xml | ||
end | ||
21 | | |
21 | ||
# Backwards compatibility with RForum global feed via a routing hack. | ||
# | ||
def index_rss | ||
... | ... | |
format.xml { head :created, :location => formatted_post_url(:forum_id => params[:forum_id], :topic_id => params[:topic_id], :id => @post, :format => :xml) } | ||
end | ||
rescue ActiveRecord::RecordInvalid | ||
76 | | |
76 | flash[:bad_reply] = 'Your reply was empty, or contained prohibited words' | |
respond_to do |format| | ||
format.html do | ||
redirect_to(long_topic_path('reply-form')) | ||
... | ... | |
format.xml { render :xml => @post.errors.to_xml, :status => 400 } | ||
end | ||
end | ||
84 | | |
84 | ||
def edit | ||
86 | | |
86 | respond_to do |format| | |
format.html | ||
format.js | ||
end | ||
end | ||
91 | | |
91 | ||
def update | ||
@post.attributes = params[:post] | ||
@post.save! | ||
rescue ActiveRecord::RecordInvalid | ||
96 | | |
96 | flash[:bad_reply] = 'Your edited post was empty, or contained prohibited words' | |
ensure | ||
respond_to do |format| | ||
format.html do | ||
... | ... | |
def authorized? | ||
action_name == 'create' || @post.editable_by?(current_user) | ||
end | ||
132 | | |
132 | ||
def find_post | ||
@post = Post.find_by_id_and_topic_id_and_forum_id(params[:id], params[:topic_id], params[:forum_id]) || raise(ActiveRecord::RecordNotFound) | ||
end | ||
136 | | |
136 | ||
def render_posts_or_xml(template_name = action_name) | ||
respond_to do |format| | ||
format.html { render :action => "#{template_name}.rhtml" } | ||
... | ... | |
options[:anchor] = anchor unless (anchor.nil?) | ||
options[:page] = (params[:page] || '1').to_i.to_s if (params.has_key?(:page)) | ||
options[:posts_per_page] = (params[:posts_per_page] || '25').to_i.to_s if (params.has_key?(:posts_per_page)) | ||
154 | | |
154 | ||
topic_path(options) | ||
end | ||
rool/rails/beast/trunk/app/controllers/topics_controller.rb:
prev. | current | |
def new | ||
@topic = Topic.new | ||
end | ||
29 | | |
29 | ||
def show | ||
respond_to do |format| | ||
format.html do | ||
... | ... | |
end | ||
end | ||
end | ||
53 | | |
53 | ||
def create | ||
# this is icky - move the topic/first post workings into the topic model? | ||
Topic.transaction do | ||
... | ... | |
format.html { redirect_to topic_path(@forum, @topic) } | ||
format.xml { head :created, :location => formatted_topic_url(:forum_id => @forum, :id => @topic, :format => :xml) } | ||
end | ||
68 | rescue ActiveRecord::RecordInvalid | |
69 | flash[:error] = "Your topic's first post was empty, or contained prohibited words" | |
70 | respond_to do |format| | |
71 | format.html { redirect_to forum_path(@forum) } | |
72 | format.xml { render :xml => @post.errors.to_xml, :status => 400 } | |
73 | end | |
end | ||
69 | | |
75 | ||
def update | ||
@topic.attributes = params[:topic] | ||
assign_protected | ||
... | ... | |
format.xml { head 200 } | ||
end | ||
end | ||
79 | | |
85 | ||
def destroy | ||
@topic.destroy | ||
flash[:notice] = "Topic '#{CGI::escapeHTML @topic.title}' was deleted." | ||
... | ... | |
format.xml { head 200 } | ||
end | ||
end | ||
88 | | |
94 | ||
protected | ||
def assign_protected | ||
@topic.user = current_user if @topic.new_record? | ||
# admins and moderators can sticky and lock topics | ||
return unless admin? or current_user.moderator_of?(@topic.forum) | ||
94 | | |
100 | @topic.sticky, @topic.locked = params[:topic][:sticky], params[:topic][:locked] | |
# only admins can move | ||
return unless admin? | ||
@topic.forum_id = params[:topic][:forum_id] if params[:topic][:forum_id] | ||
end | ||
99 | | |
105 | ||
def find_forum_and_topic | ||
@forum = Forum.find(params[:forum_id]) | ||
@topic = @forum.topics.find(params[:id]) if params[:id] | ||
end | ||
104 | | |
110 | ||
def authorized? | ||
%w(new create).include?(action_name) || @topic.editable_by?(current_user) | ||
end |
rool/rails/beast/trunk/app/models/post.rb:
prev. | current | |
after_destroy { |r| t = Topic.find(r.topic_id) ; Topic.update_all(['replied_at = ?, replied_by = ?, last_post_id = ?', t.posts.last.created_at, t.posts.last.user_id, t.posts.last.id], ['id = ?', t.id]) if t.posts.last } | ||
validates_presence_of :user_id, :body | ||
12 | validate :body_cannot_contain_blacklisted_strings | |
13 | ||
attr_accessible :body | ||
13 | | |
15 | ||
def editable_by?(user) | ||
user && (user.id == user_id || user.admin? || user.moderator_of?(topic.forum_id)) | ||
end | ||
17 | | |
19 | ||
def to_xml(options = {}) | ||
options[:except] ||= [] | ||
options[:except] << :topic_title << :forum_name | ||
super | ||
end | ||
25 | ||
26 | def body_cannot_contain_blacklisted_strings | |
27 | downcase_body = body.downcase rescue '' | |
28 | blacklist = Blacklist.find(:first).list rescue '' | |
29 | prohibited = false | |
30 | ||
31 | blacklist.split("\n").each do |item| | |
32 | if downcase_body.include?(item.strip.downcase) | |
33 | prohibited = true | |
34 | break | |
35 | end | |
36 | end | |
37 | ||
38 | errors.add(:body, "contains prohibited text") if prohibited == true | |
39 | end | |
end |
rool/rails/beast/trunk/app/views/forums/index.rhtml:
prev. | current | |
<% if admin? %> | ||
<h3>Admin</h3> | ||
5 | ||
5 | <ul> | |
6 | <li><%= link_to 'Create New Forum', new_forum_path, :class => "utility" %></li> | |
7 | <li><%= link_to 'Edit Blacklist', edit_blacklist_path, :class => "utility" %></li> | |
8 | </ul> | |
<% end %> | ||
<% end %> | ||
... | ... | |
<h1 style="margin-top:0;">Forums</h1> | ||
<p class="subtitle"> | ||
<%= feed_icon_tag "Recent Posts", formatted_all_posts_path(:format => 'rss') %> | ||
13 | ||
16 | <%= number_with_delimiter(Topic.count) %> topics, <%= number_with_delimiter(Post.count) %> posts, | |
<%= number_with_delimiter(User.count(:conditions => "posts_count>0")) %> voices | ||
15 | | |
18 | ||
</p> | ||
<table border="0" cellspacing="0" cellpadding="0" class="wide forums"> | ||
... | ... | |
--> | ||
<th class="la" width="30%">Last Post</th> | ||
</tr> | ||
27 | ||
30 | <% for forum in @forums do %> | |
<tr> | ||
<td class="vat c1"> | ||
<% if recent_forum_activity(forum) %> | ||
... | ... | |
<%= link_to h(forum.name), forum_path(forum), :class => "title" %> | ||
<div class="posts"> | ||
41 | | |
44 | <%= number_with_delimiter(forum.topics_count) %> topics, | |
<%= number_with_delimiter(forum.posts_count) %> posts | ||
</div> | ||
<div class="desc"> |
rool/rails/beast/trunk/config/routes.rb:
prev. | current | |
map.home '/', :controller => 'forums', :action => 'index' | ||
map.resources :sessions | ||
5 | | |
5 | ||
map.resources :users, :member => { :admin => :post } do |user| | ||
user.resources :moderators | ||
end | ||
... | ... | |
topic.resources :posts, :monitorships | ||
end | ||
end | ||
15 | | |
15 | ||
16 | map.resource :blacklist | |
17 | ||
# Fake DELETE requests to the monitorships controller result in a | ||
# routing error with default Beast routes. | ||
18 | | |
20 | ||
map.connect '/forums/:forum_id/topics/:topic_id/monitorships/destroy', :controller => 'monitorships', :action => 'destroy' | ||
map.resources :posts, :name_prefix => 'all_', :collection => { :search => :get } | ||
... | ... | |
end | ||
map.exceptions '/logged_exceptions/:action/:id', :controller => 'logged_exceptions', :action => 'index', :id => nil | ||
38 | | |
40 | ||
# Old-fashioned route for backwards compatibility with RForum's | ||
# global feed location | ||
41 | | |
42 | | |
43 | # | |
44 | map.exceptions '/feed/global', :controller => 'posts', :action => 'index_rss' | |
end |
rool/rails/beast/trunk/db/schema.rb:
prev. | current | |
# migrations feature of ActiveRecord to incrementally modify your database, and | ||
# then regenerate this schema definition. | ||
5 | ||
5 | ActiveRecord::Schema.define(:version => 48) do | |
7 | create_table "blacklists", :force => true do |t| | |
8 | t.column "list", :text, :default => "" | |
9 | end | |
10 | ||
create_table "forums", :force => true do |t| | ||
t.column "name", :string | ||
t.column "description", :string |