# encoding: utf-8 module Sinatra module NotificationHandler extend Hexacta def enable_notifications p "Enabling notifications..." NotificationHandler.setup_dir("/app/views/#{Hexacta::GEM_FILE_DIR}/notifications") NotificationHandler.symlink("/lib/sinatra/views/notifications.slim","/app/views/#{Hexacta::GEM_FILE_DIR}/notifications.slim") NotificationHandler.symlink_all("/lib/sinatra/views/notifications","/app/views/#{Hexacta::GEM_FILE_DIR}/notifications") post '/notification' do if params[:user_ids].blank? NotificationSender.instance.send_to_subscriptors(authenticated(User),params[:title],params[:message],params[:label]) else for id in params[:user_ids] NotificationSender.instance.send_to(User.find(:id => id),authenticated(User),params[:title],params[:message],params[:label]) end end redirect back end post '/notifications/:id' do |id| Notification.where(:user_id => authenticated(User).id, :id => id).update(:read_date => Date.today) 200 end post '/notifications' do Notification.where(:user_id => authenticated(User).id, :id => params[:ids]).update(:read_date => Date.today) redirect back end get '/notifications/:id' do |id| notification = Notification.find(:id => id) redirect "/notifications" if notification.nil? slim "#{Hexacta::GEM_FILE_DIR}/notifications/view".to_sym, locals: { :notification => notification } end delete '/notifications/:id' do |id| Notification.find(:user_id => authenticated(User).id, :id => id).destroy.to_hash.to_json.to_s end delete '/notifications' do Notification.where(:user_id => authenticated(User).id, :id => params[:ids]).destroy.to_s end get '/notifications' do filters = params.select { |attribute| Notification.columns.include?(attribute.to_sym) } params[:limit] = "20" if params[:limit].to_i < 20 params[:limit] = "100" if params[:limit].to_i > 100 limit = params[:limit].to_i offset = params[:offset].to_i query = Notification.where(:user_id => authenticated(User).id).where(filters.to_filter).order(Sequel.desc(:id)) slim "#{Hexacta::GEM_FILE_DIR}/notifications".to_sym, locals: { :notifications => query.limit(limit).offset(offset*limit).all, :total => query.count, :filters => params, :query => query } end end end register NotificationHandler end