Skip to content

Commit 2f27ab0

Browse files
committed
adds push user preference and implements sending #1
1 parent 3cd904d commit 2f27ab0

File tree

15 files changed

+241
-16
lines changed

15 files changed

+241
-16
lines changed

app/controllers/expo_push_tokens_controller.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class ExpoPushTokensController < ApplicationController
22
before_action :require_login
3-
accept_api_auth :create
3+
accept_api_auth :create, :destroy_all
44

55
def create
66
@token = ExpoPushToken.new user: User.current, token: params[:token]
@@ -14,4 +14,19 @@ def create
1414
end
1515
end
1616
end
17+
18+
def destroy_all
19+
user_id = params[:user_id].to_i if User.current.admin? and params[:user_id].present?
20+
user_id ||= User.current.id
21+
ExpoPushToken.where(user_id: user_id).delete_all
22+
respond_to do |format|
23+
format.api { head :ok }
24+
format.html {
25+
flash[:notice] = l(:notice_account_updated)
26+
redirect_to (User.current.admin? and user_id != User.current.id) ?
27+
edit_user_path(id: user_id) :
28+
my_account_path
29+
}
30+
end
31+
end
1732
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p>
2+
<%= labelled_fields_for :pref, @user.pref do |pref_fields| %>
3+
<%= pref_fields.select :push_notifications, ['disabled', 'enabled', 'enabled_no_email'].map{|k| [t(".push_notifications.#{k}"), k]}, include_blank: false %>
4+
<% end %>
5+
<em class="info">
6+
<% count = @user.expo_push_tokens.count %>
7+
<%= t '.x_devices_registered', count: count %>
8+
<%= link_to t(".delete#{'_all' if count > 1}"), expo_push_tokens_path(user_id: @user.id), confirm: t("text_are_you_sure"), method: :delete if count > 0 %>
9+
</em>
10+
</p>
11+

config/locales/en.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
en:
2+
hooks:
3+
redmine_expo_push:
4+
my_account_preferences:
5+
delete: Remove
6+
delete_all: Remove all
7+
push_notifications:
8+
enabled: Enabled
9+
disabled: Disabled
10+
enabled_no_email: Enabled, no emails
11+
x_devices_registered:
12+
zero: Currently no devices are registered.
13+
one: Currently one device is registered.
14+
other: "Currently %{count} devices are registered."

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
resources :expo_push_tokens, only: %i(create)
1+
resources :expo_push_tokens, only: %i(create)
2+
delete "expo_push_tokens", to: "expo_push_tokens#destroy_all"

init.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
require 'redmine'
22

3+
require 'redmine_expo_push'
4+
require 'redmine_expo_push/hooks'
5+
36
Rails.configuration.to_prepare do
47
RedmineExpoPush.setup
58
end

lib/redmine_expo_push.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module RedmineExpoPush
22
def self.setup
3-
# RedmineExpoPush::Patches::MailerPatch.apply
3+
RedmineExpoPush::Patches::MailerPatch.apply
44
RedmineExpoPush::Patches::UserPatch.apply
5+
RedmineExpoPush::Patches::UserPreferencePatch.apply
56
end
67
end

lib/redmine_expo_push/hooks.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module RedmineExpoPush
2+
class Hooks < Redmine::Hook::ViewListener
3+
4+
# TODO it would be nice to have a hook in users/mail_notifications instead.
5+
render_on :view_my_account_preferences, partial: "hooks/redmine_expo_push/my_account_preferences"
6+
end
7+
end

lib/redmine_expo_push/notification.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
module RedmineExpoPush
22
class Notification
3+
attr_reader :title, :body, :data
4+
35
def initialize(title: nil, body: nil, data: {})
46
@title = title
57
@body = body
68
@data = data
79
@recipients = []
810
end
911

12+
def self.for(email)
13+
new title: email.subject, body: email.text_part&.body&.decoded
14+
end
15+
1016
def add_recipient(user)
1117
@recipients << user
1218
end
@@ -18,7 +24,7 @@ def deliver
1824
messages = @recipients.map do |user|
1925
ExpoPushToken.where(user: user).map do |token|
2026
{
21-
to: token,
27+
to: token.token,
2228
title: @title,
2329
body: @body,
2430
data: @data
@@ -27,7 +33,11 @@ def deliver
2733
end
2834
messages.flatten!
2935

30-
Exponent::Push::Client.new(gzip: true).publish messages
36+
begin
37+
Exponent::Push::Client.new(gzip: true).publish messages
38+
rescue Exception
39+
Rails.logger.error "error sending push notifications:\n#{$!}\n" + $!.backtrace.join("\n")
40+
end
3141
end
3242
end
3343
end

lib/redmine_expo_push/patches/mailer_patch.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module RedmineExpoPush
22
module Patches
3-
# this is a draft, just in case we want to add general push notification
4-
# support for all redmine notifications
53
module MailerPatch
64

75
def self.apply
@@ -22,13 +20,10 @@ def deliver_mail(mail, &block)
2220
skip_receivers = []
2321
receivers.each do |addr|
2422
user = User.having_mail(addr).first
25-
if user.present?
26-
cfg = RedmineExpoPush::UserConfig.new(user)
27-
if cfg.send_push_notifications?
28-
notification ||= Notification.new(mail)
29-
notification.add_recipient user
30-
skip_receivers << addr if cfg.skip_emails?
31-
end
23+
if user.present? and user.send_push_notifications?
24+
notification ||= RedmineExpoPush::Notification.for(mail)
25+
notification.add_recipient user
26+
skip_receivers << addr if user.push_skip_emails?
3227
end
3328
end
3429
mail.send "#{field}=", (receivers - skip_receivers)

lib/redmine_expo_push/patches/user_patch.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
# frozen_string_literal: true
2+
13
module RedmineExpoPush
24
module Patches
35
module UserPatch
46
def self.apply
5-
User.prepend self unless User < self
7+
unless User < self
8+
User.prepend self
9+
User.class_eval do
10+
has_many :expo_push_tokens
11+
end
12+
end
13+
end
14+
15+
def push_device_registered?
16+
expo_push_tokens.any?
17+
end
18+
19+
def send_push_notifications?
20+
pref.push_notifications != "disabled"
21+
end
22+
23+
def push_skip_emails?
24+
pref.push_notifications == "enabled_no_email"
625
end
726

827
def remove_references_before_destroy

0 commit comments

Comments
 (0)