diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 98f024e9e..029db51e1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,16 +7,17 @@ class ApplicationController < ActionController::Base protect_from_forgery :except => :receive # before_filter :mobile_except_ipad - before_filter :set_contacts_and_status, :except => [:create, :update] + before_filter :set_contacts_notifications_and_status, :except => [:create, :update] before_filter :count_requests before_filter :set_invites before_filter :set_locale - def set_contacts_and_status + def set_contacts_notifications_and_status if current_user @aspect = nil @aspects = current_user.aspects.fields(:name) @aspects_dropdown_array = @aspects.collect{|x| [x.to_s, x.id]} + @notifications = Notification.for(current_user) end end diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb new file mode 100644 index 000000000..ac4d8f1a2 --- /dev/null +++ b/app/controllers/notifications_controller.rb @@ -0,0 +1,21 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class NotificationsController < ApplicationController + before_filter :authenticate_user! + + def destroy + note = Notification.find_by_user_id_and_id(current_user.id, params[:id]) + if note + note.delete + render :nothing => true + else + render :nothing => true, :code => 404 + end + end + + def index + + end +end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb new file mode 100644 index 000000000..24c3de2c6 --- /dev/null +++ b/app/helpers/notifications_helper.rb @@ -0,0 +1,22 @@ +module NotificationsHelper + def glue_text(kind) + translation = "notifications.#{kind.underscore}_glue" + t(translation) + end + + def object_link(note) + kind = note.kind.underscore + translation = t("notifications.#{kind.underscore}_link") + case kind + when 'request' + link_to translation, aspects_manage_path + when 'status_message' + link_to translation, status_message_path(note.object_id) + when 'commnent' + link_to translation, object_path(note.object_id) + when 'photo' + link_to translation, photo_path(note.object_id) + else + end + end +end diff --git a/app/models/notification.rb b/app/models/notification.rb index 887922894..7a9684a88 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -16,8 +16,7 @@ class Notification attr_accessible :object_id, :kind, :user_id, :person_id - def for(user, opts={}) - query = opts.merge(:user_id => user) - self.all(opts) + def self.for(user, opts={}) + self.all(opts.merge(:user_id => user.id)) end end diff --git a/app/models/service.rb b/app/models/service.rb index eb0d48a1a..779adc1e8 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -2,7 +2,6 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. - class Service include MongoMapper::Document include ActionView::Helpers::TextHelper diff --git a/app/views/notifications/index.html.haml b/app/views/notifications/index.html.haml new file mode 100644 index 000000000..35c44d3ef --- /dev/null +++ b/app/views/notifications/index.html.haml @@ -0,0 +1,6 @@ +%ul + - @notifications.each do |note| + %li + = link_to "#{note.person.name.titleize}", person_path(note.person) + = glue_text(note.kind) + = object_link(note) diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index ef1c50da2..eff256ef5 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -178,6 +178,14 @@ en: remove_aspect: "Delete this aspect" confirm_remove_aspect: "Are you sure you want to delete this aspect?" add_existing: "Add an existing contact" + notifications: + status_message_glue: "posted a new" + status_message_link: "message" + request_glue: "sent you a contact request" + request_link: "request" + comment_glue: "commented on your" + comment_link: "post" + also_commnet_glue: "commented on your contact's" users: edit: export_data: "Export Data" diff --git a/config/routes.rb b/config/routes.rb index 067443185..9c0b7c0d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,8 @@ Diaspora::Application.routes.draw do resources :comments, :only => [:create] resources :requests, :only => [:destroy, :create] resources :services + + resources :notifications, :only => [:destroy, :index] resources :posts, :only => [:show], :path => '/p/' resources :people do resources :status_messages diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb new file mode 100644 index 000000000..8879c946f --- /dev/null +++ b/spec/controllers/notifications_controller_spec.rb @@ -0,0 +1,35 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe NotificationsController do + + let!(:user) { make_user } + let!(:aspect) { user.aspects.create(:name => "AWESOME!!") } + + before do + sign_in :user, user + + end + + describe '#destroy' do + it 'removes a notification' do + note = Notification.create(:user_id => user.id) + delete :destroy, :id => note.id + Notification.count.should == 0 + end + + it 'only lets you delete your own notifications' do + user2 = make_user + + Notification.create(:user_id => user.id) + note = Notification.create(:user_id => user2.id) + + delete :destroy, :id => note.id + + Notification.count.should == 2 + end + end +end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 35bfb7b25..af54d8391 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -10,7 +10,8 @@ describe Notification do @sm = Factory(:status_message) @person = Factory(:person) @user = make_user - @note = Notification.new(:object_id => @sm.id, :kind => @sm.class.name, :person => @person, :user => @user) + @opts = {:object_id => @sm.id, :kind => @sm.class.name, :person_id => @person.id, :user_id => @user.id} + @note = Notification.new(@opts) end it 'contains a type' do @@ -26,7 +27,18 @@ describe Notification do end describe '.for' do + it 'returns all of a users notifications' do + user2 = make_user + Notification.create(@opts) + Notification.create(@opts) + Notification.create(@opts) + Notification.create(@opts) + + @opts.delete(:user_id) + Notification.create(@opts.merge(:user_id => user2.id)) + + Notification.for(@user).count.should == 4 + end end - end