Skip to content

Commit

Permalink
[webui][api] Refactor SendEventEmails job
Browse files Browse the repository at this point in the history
Better creation of RSS notifications
Fix partial views inclusion not to fail when used outside the
EventMailer
Remove unneeded ivars in feeds controller
  • Loading branch information
Moises Deniz Aleman authored and hennevogel committed Jun 29, 2017
1 parent 7bcbfc6 commit c4d5c1e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/api/app/controllers/webui/feeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def commits
end

def notifications
@host = ::Configuration.obs_url
@configuration = ::Configuration.first
@configuration = ::Configuration.first
@user = User.current
@notifications = User.current.combined_rss_feed_items
end
Expand Down
23 changes: 14 additions & 9 deletions src/api/app/jobs/send_event_emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ def perform
subscribers = event.subscribers
next if subscribers.empty?
EventMailer.event(subscribers, event).deliver_now

event.subscriptions.each do |subscription|
Notifications::RssFeedItem.create(
subscriber: subscription.subscriber,
event_type: event.eventtype,
event_payload: event.payload,
subscription_receiver_role: subscription.receiver_role
)
end
create_rss_notifications(event)
end
true
end

private

def create_rss_notifications(event)
event.subscriptions.each do |subscription|
Notifications::RssFeedItem.create(
subscriber: subscription.subscriber,
event_type: event.eventtype,
event_payload: event.payload,
subscription_receiver_role: subscription.receiver_role
)
end
end
end
13 changes: 5 additions & 8 deletions src/api/app/models/notifications/base.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
class Notifications::Base < ApplicationRecord
self.table_name = "notifications"

belongs_to :user
belongs_to :group
belongs_to :subscriber, polymorphic: true

def subscriber=(subscriber)
if subscriber.is_a? User
self.user = subscriber
elsif subscriber.is_a? Group
self.group = subscriber
end
serialize :event_payload, Hash

def event
@event ||= event_type.constantize.new(event_payload)
end
end

Expand Down
12 changes: 12 additions & 0 deletions src/api/app/models/notifications/rss_feed_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ def self.cleanup
end
end
end

def title
event.subject
end

def description
ApplicationController.renderer.new.render(
template: "event_mailer/#{event.template_name}",
layout: false,
format: :txt,
assigns: { e: event.expanded_payload, host: ::Configuration.obs_url, configuration: ::Configuration.first })
end
end

# == Schema Information
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/views/event_mailer/_actions.text.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Actions:
<% event['actions'].each do |a| -%>
<% if %w(submit maintenance_incident maintenance_release).include? a['type'] -%>
<%= render partial: 'submit_action', locals: { a: a } -%>
<%= render partial: 'event_mailer/submit_action', locals: { a: a } -%>
<% else -%>
<%= render partial: "#{a['type']}_action", locals: { a: a } -%>
<%= render partial: "event_mailer/#{a['type']}_action", locals: { a: a } -%>
<% end -%>
<% end -%>
4 changes: 2 additions & 2 deletions src/api/app/views/event_mailer/request_create.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Description:
<%= event['description']%>
<%-end-%>
<%= render partial: 'actions', locals: { event: event } %>
<%= render partial: 'event_mailer/actions', locals: { event: event } %>

To REVIEW against the previous version:
osc request show --diff <%= event['number']%>

To ACCEPT the request:
osc request accept <%= event['number']%> --message="reviewed ok."

To DECLINE the request:
osc request decline <%= event['number']%> --message="declined for reason xyz (see ... for background / policy / ...)."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Comment:
<%= event['comment'] %>
<% end -%>
<%= render partial: 'actions', locals: { event: event } %>
<%= render partial: 'event_mailer/actions', locals: { event: event } %>
2 changes: 1 addition & 1 deletion src/api/app/views/event_mailer/review_wanted.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Review reason set by <%= event['who'] %>:
Request description:
<%= event['description'] %>
<%= render partial: 'actions', locals: { event: event } -%>
<%= render partial: 'event_mailer/actions', locals: { event: event } -%>
7 changes: 3 additions & 4 deletions src/api/app/views/webui/feeds/notifications.rss.builder
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
xml.rss version: '2.0' do
xml.channel do
xml.title "#{@user.realname}'s (#{@user.login}) notifications"
xml.title "#{@user.realname} (#{@user.login}) notifications"
xml.description "Event's notifications from #{@configuration['title']}"
xml.link url_for only_path: false, controller: 'main', action: 'index'
xml.language "en"
Expand All @@ -9,12 +9,11 @@ xml.rss version: '2.0' do

@notifications.each do |notification|
xml.item do
xml.title notification.event_type
xml.description notification.event_payload
xml.title notification.title
xml.description notification.description
xml.category "#{notification.event_type}/#{notification.subscription_receiver_role}"
xml.pubDate notification.created_at
xml.author "#{@configuration['title']}"
xml.link url_for only_path: false, controller: 'main', action: 'index'
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/api/spec/jobs/send_event_emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
end

it "creates an rss notification for user's email" do
notification = Notifications::Base.find_by(user: user)
notification = Notifications::Base.find_by(subscriber: user)

expect(notification.type).to eq('Notifications::RssFeedItem')
expect(notification.event_type).to eq('Event::CommentForProject')
expect(notification.event_payload).to include('how are things?')
expect(notification.event_payload['comment_body']).to include('how are things?')
expect(notification.subscription_receiver_role).to eq('all')
expect(notification.delivered).to be_falsey
end

it "creates an rss notification for group's email" do
notification = Notifications::RssFeedItem.find_by(group: group)
notification = Notifications::RssFeedItem.find_by(subscriber: group)

expect(notification.type).to eq('Notifications::RssFeedItem')
expect(notification.event_type).to eq('Event::CommentForProject')
expect(notification.event_payload).to include('how are things?')
expect(notification.event_payload['comment_body']).to include('how are things?')
expect(notification.subscription_receiver_role).to eq('all')
expect(notification.delivered).to be_falsey
end
Expand Down

0 comments on commit c4d5c1e

Please sign in to comment.