Skip to content

Commit

Permalink
add email notifications for file updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-koba-mobidev committed Mar 28, 2018
1 parent 6399829 commit 5009113
Show file tree
Hide file tree
Showing 50 changed files with 361 additions and 62 deletions.
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
boxroom (0.0.3)
boxroom (0.0.4)
acts_as_tree (~> 2.7)
bulma-rails (~> 0.6.2)
cells-erb (~> 0.1.0)
Expand Down Expand Up @@ -114,7 +114,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
loofah (2.2.0)
loofah (2.2.2)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.0)
Expand Down Expand Up @@ -154,8 +154,8 @@ GEM
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.1.5)
actionpack (= 5.1.5)
activesupport (= 5.1.5)
Expand Down Expand Up @@ -198,20 +198,20 @@ GEM
trailblazer-macro (>= 2.1.0.beta2, < 2.2.0)
trailblazer-macro-contract (>= 2.1.0.beta2, < 2.2.0)
trailblazer-operation (>= 0.2.4, < 0.3.0)
trailblazer-activity (0.5.1)
trailblazer-activity (0.5.2)
hirb
trailblazer-context
trailblazer-cells (0.0.3)
cells (>= 4.1.0.rc1, < 5.0.0)
trailblazer-context (0.1.2)
trailblazer-loader (0.1.2)
trailblazer-macro (2.1.0.beta3)
trailblazer-macro-contract (2.1.0.beta2)
trailblazer-macro (2.1.0.beta4)
trailblazer-macro-contract (2.1.0.beta3)
declarative
reform (>= 2.2.0, < 3.0.0)
trailblazer-operation (>= 0.2.4, < 0.3.0)
trailblazer-operation (0.2.4)
trailblazer-activity (>= 0.5.1, < 0.7.0)
trailblazer-operation (0.2.5)
trailblazer-activity (>= 0.5.2, < 0.7.0)
trailblazer-context (>= 0.1.1, < 0.3.0)
trailblazer-rails (2.1.0)
reform-rails (>= 0.1.4, < 0.2.0)
Expand Down
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sharing files in a web browser. It lets users
- share files
- search folders and files
- sort files and folders
- notify by email when files in specific folders are created, updated or removed

Admins can:
- manage users
Expand Down Expand Up @@ -44,6 +45,7 @@ Boxroom.configure do |config|
config.show_settings = true
config.show_shared_files = true
config.show_search = true
config.enable_notifications = true # notify by email when files in specific folders are created, updated or removed
end
```

Expand Down Expand Up @@ -71,9 +73,83 @@ end
```
- if you modify your users without callbacks in any place you should also take care of `boxroom_users` table yourself.

## Contributing
## Mail settings

Boxroom sends email on the following occasions:

* When inviting new users
* On a reset password request
* When a file is shared

For the application to be able to send email, a few things have to be set up. Depending on the environment
you're working in, either open `config/environments/development.rb` or `config/environments/production.rb`.
Uncomment the following lines and fill in the correct settings of your mail server:

```ruby
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mailhost',
port: 587,
user_name: 'user_name',
password: 'password',
authentication: 'plain'
}
```

Also uncomment the following and replace `localhost:3000` with the host name the app will be running under:

```ruby
config.action_mailer.default_url_options = { host: 'localhost:3000' }
```

Lastly, you have to choose the address emails will be sent from. You can do
this by uncommenting and adjusting the following:

```ruby
ActionMailer::Base.default from: 'Boxroom <[email protected]>'
```


## Languages

Thanks to [Rob Halff](https://github.com/rhalff), [Marcus Ilgner](https://github.com/milgner),
[Jessica Marcon](https://github.com/marcontwm), [Arnaud Sellenet](https://github.com/demental),
[Burnaby John](https://github.com/john-coding) and [marcosantoniocaro](https://github.com/marcosantoniocaro)
Boxroom is now available in Dutch, German, Italian, French, Simplified Chinese and Spanish.

English is the default. To change the language, open `config/application.rb` and set the language you desire:

```ruby
config.i18n.default_locale = :en # English
config.i18n.default_locale = :nl # Dutch
config.i18n.default_locale = :de # German
config.i18n.default_locale = :it # Italian
config.i18n.default_locale = :fr # French
config.i18n.default_locale = :'zh-CN' # Simplified Chinese
config.i18n.default_locale = :es # Spanish
```

It would be great to have many more languages. I am waiting for your pull requests.

## Contributing and Development
Please feel free to leave an issue or PR.

- Use `docker-compose up -d` to launch mailcatcher on [http://localhost:1080](http://localhost:1080)
- Update mail settings in `config/environments/development.rb`
```ruby
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'localhost',
port: 1025
}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

ActionMailer::Base.default from: 'Boxroom <[email protected]>'
```

## Testing
- run migrations `bin/rails db:migrate RAILS_ENV=test`
- run tests `bin/rails test`
Expand All @@ -82,6 +158,7 @@ Please feel free to leave an issue or PR.
The engine is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

## Roadmap:
- tests for notifications
- support s3
- batch actions
- tag files
Expand Down
2 changes: 1 addition & 1 deletion app/concepts/boxroom/folder/view/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<%= link_to '<span class="icon is-small"><i class="fa fa-clipboard"></i></span>',
{controller: :clipboard, action: :create, id: file.id, type: 'file', folder_id: file.folder, authenticity_token: form_authenticity_token},
method: :post, title: t(:add_to_clipboard)
%>&nbsp;
%>
<% if Boxroom.configuration.show_shared_files %>
<%= link_to '<span class="icon is-small"><i class="fa fa-share"></i></span>', new_file_share_link_path(file), title: t(:share) %>
<% end %>
Expand Down
7 changes: 7 additions & 0 deletions app/concepts/boxroom/user_file/contract/notify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Boxroom::UserFile::Contract
class Notify < Reform::Form
property :id, virtual: true

validates :id, presence: true
end
end
27 changes: 27 additions & 0 deletions app/concepts/boxroom/user_file/operations/notify_create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Boxroom
class UserFile < ActiveRecord::Base
class NotifyCreate < ::Trailblazer::Operation
step Trailblazer::Operation::Contract::Build(constant: Boxroom::UserFile::Contract::Notify)
step Trailblazer::Operation::Contract::Validate()
step :model!
step :check
step :notify

def model!(options, params:, **)
options['model'] = UserFile.find(params[:id])
end

def check(options, params:, **)
return false unless Boxroom.configuration.enable_notifications
return false unless options['model'].folder.notify_create
true
end

def notify(options, params:, **)
options['model'].folder.notify_emails.gsub(',', ' ').split.each do |email|
UserFileMailer.notify_create(options['model'], email).deliver_later
end
end
end
end
end
27 changes: 27 additions & 0 deletions app/concepts/boxroom/user_file/operations/notify_remove.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Boxroom
class UserFile < ActiveRecord::Base
class NotifyRemove < ::Trailblazer::Operation
step Trailblazer::Operation::Contract::Build(constant: Boxroom::UserFile::Contract::Notify)
step Trailblazer::Operation::Contract::Validate()
step :model!
step :check
step :notify

def model!(options, params:, **)
options['model'] = UserFile.find(params[:id])
end

def check(options, params:, **)
return false unless Boxroom.configuration.enable_notifications
return false unless options['model'].folder.notify_remove
true
end

def notify(options, params:, **)
options['model'].folder.notify_emails.gsub(',', ' ').split.each do |email|
UserFileMailer.notify_remove(options['model'].attachment_file_name, options['model'].folder, email).deliver_later
end
end
end
end
end
27 changes: 27 additions & 0 deletions app/concepts/boxroom/user_file/operations/notify_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Boxroom
class UserFile < ActiveRecord::Base
class NotifyUpdate < ::Trailblazer::Operation
step Trailblazer::Operation::Contract::Build(constant: Boxroom::UserFile::Contract::Notify)
step Trailblazer::Operation::Contract::Validate()
step :model!
step :check
step :notify

def model!(options, params:, **)
options['model'] = UserFile.find(params[:id])
end

def check(options, params:, **)
return false unless Boxroom.configuration.enable_notifications
return false unless options['model'].folder.notify_update
true
end

def notify(options, params:, **)
options['model'].folder.notify_emails.gsub(',', ' ').split.each do |email|
UserFileMailer.notify_update(options['model'], email).deliver_later
end
end
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/boxroom/files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def create
File.open("#{Rails.root}/#{Boxroom.configuration.uploads_path}/#{Rails.env}/#{existing_file.id}/original/#{existing_file.id}", "ab") {|f| f.write(permitted_params.user_file["attachment"].read)}
else
@file = @target_folder.user_files.create(permitted_params.user_file)
UserFile::NotifyCreate.(params: {id: @file.id})
end

head :ok
Expand All @@ -46,6 +47,7 @@ def edit
# @file and @folder are set in require_existing_file
def update
if @file.update_attributes(permitted_params.user_file)
UserFile::NotifyUpdate.(params: {id: @file.id})
redirect_to edit_file_url(@file), :notice => t(:your_changes_were_saved)
else
render :action => 'edit'
Expand All @@ -54,6 +56,7 @@ def update

# @file and @folder are set in require_existing_file
def destroy
UserFile::NotifyRemove.(params: {id: @file.id})
@file.destroy
redirect_to @folder
end
Expand Down
14 changes: 7 additions & 7 deletions app/controllers/boxroom/folders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module Boxroom
class FoldersController < Boxroom::ApplicationController
include Boxroom::BaseController

before_action :require_existing_folder, :only => [:show, :edit, :update, :destroy]
before_action :require_existing_target_folder, :only => [:new, :create]
before_action :require_folder_isnt_root_folder, :only => [:edit, :update, :destroy]
before_action :require_existing_folder, only: [:show, :edit, :update, :destroy]
before_action :require_existing_target_folder, only: [:new, :create]
before_action :require_folder_isnt_root_folder, only: [:edit, :update, :destroy]

before_action :require_create_permission, :only => [:new, :create]
before_action :require_read_permission, :only => :show
before_action :require_update_permission, :only => [:edit, :update]
before_action :require_delete_permission, :only => :destroy
before_action :require_create_permission, only: [:new, :create]
before_action :require_read_permission, only: :show
before_action :require_update_permission, only: [:edit, :update]
before_action :require_delete_permission, only: :destroy

def index
redirect_to Folder.root
Expand Down
19 changes: 19 additions & 0 deletions app/mailers/boxroom/user_file_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Boxroom
class UserFileMailer < ActionMailer::Base
def notify_create(user_file, email)
@user_file = user_file
mail(to: email, subject: t(:file_added))
end

def notify_update(user_file, email)
@user_file = user_file
mail(to: email, subject: t(:file_updated))
end

def notify_remove(file_name, folder, email)
@file_name = file_name
@folder = folder
mail(to: email, subject: t(:file_removed))
end
end
end
2 changes: 1 addition & 1 deletion app/models/boxroom/permitted_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PermittedParams < Struct.new(:params, :current_user)
end

def folder_attributes
[:name]
[:name, :notify_emails, :notify_create, :notify_update, :notify_remove]
end

def group_attributes
Expand Down
10 changes: 5 additions & 5 deletions app/models/boxroom/user_file.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Boxroom
class UserFile < ActiveRecord::Base
has_attached_file :attachment, :path => ":rails_root/#{Boxroom.configuration.uploads_path}/:rails_env/:id/:style/:id", :restricted_characters => Boxroom::RESTRICTED_CHARACTERS
has_attached_file :attachment, path: ":rails_root/#{Boxroom.configuration.uploads_path}/:rails_env/:id/:style/:id", restricted_characters: Boxroom::RESTRICTED_CHARACTERS
do_not_validate_attachment_file_type :attachment

belongs_to :folder
has_many :share_links, :dependent => :destroy
has_many :share_links, dependent: :destroy

validates_attachment_presence :attachment, :message => I18n.t(:blank, :scope => [:activerecord, :errors, :messages])
validates_attachment_presence :attachment, message: I18n.t(:blank, scope: [:activerecord, :errors, :messages])
validates_presence_of :folder_id
validates_uniqueness_of :attachment_file_name, :scope => 'folder_id', :message => I18n.t(:exists_already, :scope => [:activerecord, :errors, :messages])
validates_format_of :attachment_file_name, :with => /\A[^\/\\\?\*:|"<>]+\z/, :message => I18n.t(:invalid_characters, :scope => [:activerecord, :errors, :messages])
validates_uniqueness_of :attachment_file_name, scope: 'folder_id', message: I18n.t(:exists_already, scope: [:activerecord, :errors, :messages])
validates_format_of :attachment_file_name, with: /\A[^\/\\\?\*:|"<>]+\z/, message: I18n.t(:invalid_characters, scope: [:activerecord, :errors, :messages])

def copy(target_folder)
new_file = self.dup
Expand Down
28 changes: 28 additions & 0 deletions app/views/boxroom/folders/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@
<div class="control">
<%= f.text_field :name, class: 'input' %>
</div>
<% if Boxroom.configuration.enable_notifications %>
<section class="section">
<h2 class="subtitle"> <%= t :edit_notifications %> </h2>
<%= f.label :notify_emails, class: 'label' %>
<div class="control">
<%= f.text_field :notify_emails, placeholder: t(:notify_emails_list), class: 'input' %>
</div>

<div class="control">
<label class="checkbox">
<%= f.check_box :notify_create %>
<%= t :notify_create %>
</label>
</div>
<div class="control">
<label class="checkbox">
<%= f.check_box :notify_update %>
<%= t :notify_update %>
</label>
</div>
<div class="control">
<label class="checkbox">
<%= f.check_box :notify_remove %>
<%= t :notify_remove %>
</label>
</div>
</section>
<% end %>
</div>
<div class="field is-grouped">
<div class="control">
Expand Down
Loading

0 comments on commit 5009113

Please sign in to comment.