# encoding: utf-8 module Sinatra module NotificationHandler extend Hexacta def new_notification post '/notification' do if params["user_ids"].blank? notify(authenticated(User),params["title"],params["message"],params["label"]) else for id in params["user_ids"] notify_to(User.find(:id => id),authenticated(User),params["title"],params["message"],params["label"]) end end redirect back end end def edit_notification post '/notifications/:id' do |id| Notification.where(:user_id => authenticated(User).id, :id => id).update(:read_date => Date.today) 200 end end def read_notifications post '/notifications' do Notification.where(:user_id => authenticated(User).id, :id => params["ids"]).update(:read_date => Date.today) redirect back end end def get_notification 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 end def delete_notification delete '/notifications/:id' do |id| Notification.find(:user_id => authenticated(User).id, :id => id).destroy.to_hash.to_json.to_s end end def delete_notifications delete '/notifications' do Notification.where(:user_id => authenticated(User).id, :id => params["ids"]).destroy.to_s end end def get_notifications 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(:read_date,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 p "Setting up notifications directory..." setup_dir("/app/views/#{Hexacta::GEM_FILE_DIR}/notifications") symlink("/lib/sinatra/views/notifications.slim","/app/views/#{Hexacta::GEM_FILE_DIR}/notifications.slim") symlink_all("/lib/sinatra/views/notifications","/app/views/#{Hexacta::GEM_FILE_DIR}/notifications") end register NotificationHandler end