Merge remote-tracking branch 'official/master' into saltation
[gitorious:taladars-gitorious-saltation.git] / app / controllers / events_controller.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
4 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
5 #   Copyright (C) 2008 Johan Sørensen <johan@johansorensen.com>
6 #   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
7 #
8 #   This program is free software: you can redistribute it and/or modify
9 #   it under the terms of the GNU Affero General Public License as published by
10 #   the Free Software Foundation, either version 3 of the License, or
11 #   (at your option) any later version.
12 #
13 #   This program is distributed in the hope that it will be useful,
14 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #   GNU Affero General Public License for more details.
17 #
18 #   You should have received a copy of the GNU Affero General Public License
19 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #++
21
22 class EventsController < ApplicationController
23   def index
24     @events = Event.visibility_all.paginate(:all, :order => "events.created_at desc",
25                   :page => params[:page], :include => [:user])
26     @atom_auto_discovery_url = events_path(:format => :atom)
27     
28     respond_to do |if_format_is|
29       if_format_is.html {}
30       if_format_is.atom {}
31     end
32   end
33   
34   def commits
35     @event = Event.find(params[:id])
36     @repository = @event.target
37     @project = @repository.project
38     if @event.action == Action::PUSH
39       render_old_style_push
40     else
41       render_new_style_push
42     end
43   end
44
45   # TODO: Remove when old push events are removed
46   def render_old_style_push
47     @commit_count = @event.events.count
48     @branch_name = @event.data
49     if stale?(:etag => @event, :last_modified => @event.created_at)
50       @commits = @event.events.commits
51       respond_to do |wants|
52         wants.js
53       end
54       expires_in 30.minutes
55     end
56   end
57
58   def render_new_style_push
59     event_data = PushEventLogger.parse_event_data(@event.data)
60     @branch_name = event_data[:branch]
61     @commit_count = event_data[:commit_count].to_i
62     first_sha = event_data[:start_sha]
63     last_sha = event_data[:end_sha]
64     if stale?(:etag => @event, :last_modified => @event.created_at)
65       @commits = Gitorious::Commit.load_commits_between(@event.target.git, first_sha, last_sha, @event.id)[0,Event::MAX_COMMIT_EVENTS + 1]
66       respond_to do |wants|
67         wants.js
68       end
69       expires_in 30.minutes
70     end
71   end
72 end