diff --git a/Gemfile b/Gemfile index cf3eecb1..36426058 100644 --- a/Gemfile +++ b/Gemfile @@ -51,4 +51,6 @@ group :development, :test do gem 'pry-rails' gem 'rubocop' gem 'tunemygc' + gem 'faker' + gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index fd414eb2..62e6e8c0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -109,6 +109,8 @@ GEM factory_girl_rails (4.8.0) factory_girl (~> 4.8.0) railties (>= 3.0.0) + faker (1.7.3) + i18n (~> 0.5) globalid (0.3.7) activesupport (>= 4.1.0) httparty (0.14.0) @@ -272,6 +274,7 @@ GEM thor (0.19.4) thread_safe (0.3.5) tilt (2.0.5) + timecop (0.8.1) tunemygc (1.0.69) tzinfo (1.2.2) thread_safe (~> 0.1) @@ -301,6 +304,7 @@ DEPENDENCIES dotenv-rails exception_notification factory_girl_rails + faker httparty jquery-rails jquery-ui-rails @@ -327,6 +331,7 @@ DEPENDENCIES slack-notifier snorlax sucker_punch + timecop tunemygc uglifier @@ -334,4 +339,4 @@ RUBY VERSION ruby 2.3.0p0 BUNDLED WITH - 1.13.7 + 1.14.6 diff --git a/app/mailers/map_activity_mailer.rb b/app/mailers/map_activity_mailer.rb index 781bf73f..977ece4f 100644 --- a/app/mailers/map_activity_mailer.rb +++ b/app/mailers/map_activity_mailer.rb @@ -2,9 +2,10 @@ class MapActivityMailer < ApplicationMailer default from: 'team@metamaps.cc' - def daily_summary(user, summary_data) + def daily_summary(user, map, summary_data) @user = user + @map = map @summary_data = summary_data - mail(to: user.email, subject: 'some subject line') + mail(to: user.email, subject: MapActivityService.subject_line(map)) end end diff --git a/app/models/synapse.rb b/app/models/synapse.rb index e8378f0e..4def4147 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -68,13 +68,13 @@ class Synapse < ApplicationRecord output += %(\n) output end - + protected - + def set_perm_by_defer permission = defer_to_map.permission if defer_to_map end - + def after_created_async follow_ids = NotificationService.notify_followers(topic1, TOPIC_CONNECTED_1, self) NotificationService.notify_followers(topic2, TOPIC_CONNECTED_2, self, nil, follow_ids) @@ -93,7 +93,7 @@ class Synapse < ApplicationRecord end end end - + def before_destroyed # hard to know how to do this yet, because the synapse actually gets destroyed #NotificationService.notify_followers(topic1, 'topic_disconnected', self) diff --git a/app/services/map_activity_service.rb b/app/services/map_activity_service.rb index 7dee21fc..b443099e 100644 --- a/app/services/map_activity_service.rb +++ b/app/services/map_activity_service.rb @@ -1,33 +1,37 @@ class MapActivityService - def self.summarize_data(map, user, till = DateTime.now) + def self.subject_line(map) + 'Activity on map ' + map.name + end + + def self.summarize_data(map, user, until_moment = DateTime.now) results = { stats: {} } - since = till - 24.hours + since = until_moment - 24.hours message_count = Message.where(resource: map) - .where("created_at > ? AND created_at < ?", since, till) + .where("created_at > ? AND created_at < ?", since, until_moment) .where.not(user: user).count if message_count > 0 results[:stats][:messages_sent] = message_count end moved_count = Event.where(kind: 'topic_moved_on_map', map: map) - .where("created_at > ? AND created_at < ?", since, till) + .where("created_at > ? AND created_at < ?", since, until_moment) .where.not(user: user).group(:eventable_id).count if moved_count.keys.length > 0 results[:stats][:topics_moved] = moved_count.keys.length end topics_added_events = Event.where(kind: 'topic_added_to_map', map: map) - .where("created_at > ? AND created_at < ?", since, till) + .where("created_at > ? AND created_at < ?", since, until_moment) .where.not(user: user) .order(:created_at) topics_removed_events = Event.where(kind: 'topic_removed_from_map', map: map) - .where("created_at > ? AND created_at < ?", since, till) + .where("created_at > ? AND created_at < ?", since, until_moment) .where.not(user: user) .order(:created_at) @@ -54,12 +58,12 @@ class MapActivityService end synapses_added_events = Event.where(kind: 'synapse_added_to_map', map: map) - .where("created_at > ? AND created_at < ?", since, till) + .where("created_at > ? AND created_at < ?", since, until_moment) .where.not(user: user) .order(:created_at) synapses_removed_events = Event.where(kind: 'synapse_removed_from_map', map: map) - .where("created_at > ? AND created_at < ?", since, till) + .where("created_at > ? AND created_at < ?", since, until_moment) .where.not(user: user) .order(:created_at) diff --git a/app/views/map_activity_mailer/daily_summary.html.erb b/app/views/map_activity_mailer/daily_summary.html.erb index 81ca2fd4..e19e9401 100644 --- a/app/views/map_activity_mailer/daily_summary.html.erb +++ b/app/views/map_activity_mailer/daily_summary.html.erb @@ -1,7 +1,56 @@ +<% button_style = "background-color:#4fc059;border-radius:2px;color:white;display:inline-block;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;min-height:29px;line-height:29px;min-width:54px;outline:0px;padding:0 8px;text-align:center;text-decoration:none" %> + -
- +
+

Hey <%= @user.name %>, there was activity by others in the last 24 hours on map + <%= link_to @map.name, map_url(@map) %> +

+

# of messages: <%= @summary_data[:stats][:messages_sent] || 0 %>

+

# of topics added: <%= @summary_data[:stats][:topics_added] || 0 %>

+

# of topics moved: <%= @summary_data[:stats][:topics_moved] || 0%>

+

# of topics removed: <%= @summary_data[:stats][:topics_removed] || 0 %>

+

# of synapses added: <%= @summary_data[:stats][:synapses_added] || 0 %>

+

# of synapses removed: <%= @summary_data[:stats][:synapses_removed] || 0 %>


-

Make sense with Metamaps

+ <% if @summary_data[:topics_added] %> +

Topics Added

+ + <% end %> + + <% if @summary_data[:topics_removed] %> +

Topics Removed

+ + <% end %> + + <% if @summary_data[:synapses_added] %> +

Synapses Added

+ + <% end %> + + <% if @summary_data[:synapses_removed] %> +

Synapses Removed

+ + <% end %> + + <%= link_to 'Visit Map', map_url(@map), style: button_style %> + +
+

Make sense with Metamaps

<%= render partial: 'shared/mailer_unsubscribe_link' %>
diff --git a/app/views/shared/_mailer_unsubscribe_link.html.erb b/app/views/shared/_mailer_unsubscribe_link.html.erb index 56730dd9..5aab4689 100644 --- a/app/views/shared/_mailer_unsubscribe_link.html.erb +++ b/app/views/shared/_mailer_unsubscribe_link.html.erb @@ -1,3 +1,3 @@ diff --git a/lib/tasks/emails.rake b/lib/tasks/emails.rake index fcf805fe..b2a7303a 100644 --- a/lib/tasks/emails.rake +++ b/lib/tasks/emails.rake @@ -5,16 +5,16 @@ namespace :metamaps do end def summarize_map_activity - Map.all.find_each do |map| - map.followers.each do |user| - # add logging and rescue-ing - # and a notification of failure - next unless MapPolicy.new(user, map).show? # just in case the permission changed - next unless user.emails_allowed - summary_data = MapActivityService.summarize_data(map, user) - next if summary_data[:stats].blank? - MapActivityMailer.daily_summary(user, summary_data).deliver_later - end + Follow.where(followed_type: 'Map').find_each do |follow| + map = follow.followed + user = follow.user + # add logging and rescue-ing + # and a notification of failure + next unless MapPolicy.new(user, map).show? # just in case the permission changed + next unless user.emails_allowed + summary_data = MapActivityService.summarize_data(map, user) + next if summary_data[:stats].blank? + MapActivityMailer.daily_summary(user, map, summary_data).deliver_later end end end diff --git a/spec/mailers/previews/map_activity_mailer_preview.rb b/spec/mailers/previews/map_activity_mailer_preview.rb index c3f6eab7..3d943eee 100644 --- a/spec/mailers/previews/map_activity_mailer_preview.rb +++ b/spec/mailers/previews/map_activity_mailer_preview.rb @@ -2,9 +2,108 @@ # Preview all emails at http://localhost:3000/rails/mailers/map_activity_mailer class MapActivityMailerPreview < ActionMailer::Preview def daily_summary - user = User.first - map = Map.first + user = generate_user + map = generate_map + generate_recent_activity_on_map(map) summary_data = MapActivityService.summarize_data(map, user) - MapActivityMailer.daily_summary(user, summary_data) + MapActivityMailer.daily_summary(user, map, summary_data) end + + private + def generate_recent_activity_on_map(map) + mapping = nil + mapping2 = nil + mapping3 = nil + mapping4 = nil + mapping5 = nil + mapping6 = nil + mapping7 = nil + mapping8 = nil + mapping9 = nil + mapping10 = nil + + Timecop.freeze(2.days.ago) do + mapping = topic_added_to_map(map) + mapping2 = topic_added_to_map(map) + mapping3 = topic_added_to_map(map) + mapping4 = topic_added_to_map(map) + mapping5 = topic_added_to_map(map) + mapping6 = topic_added_to_map(map) + mapping7 = topic_added_to_map(map) + mapping8 = topic_added_to_map(map) + mapping9 = synapse_added_to_map(map, mapping.mappable, mapping2.mappable) + mapping10 = synapse_added_to_map(map, mapping.mappable, mapping8.mappable) + end + Timecop.return + + Timecop.freeze(2.hours.ago) do + topic_moved_on_map(mapping7) + topic_moved_on_map(mapping8) + generate_message(map) + generate_message(map) + generate_message(map) + synapse_added_to_map(map, mapping7.mappable, mapping8.mappable) + synapse_added_to_map(map, mapping.mappable, mapping8.mappable) + synapse_removed_from_map(mapping9) + synapse_removed_from_map(mapping10) + end + Timecop.return + + Timecop.freeze(30.minutes.ago) do + topic_removed_from_map(mapping3) + topic_removed_from_map(mapping4) + topic_removed_from_map(mapping5) + topic_removed_from_map(mapping6) + topic_added_to_map(map) + topic_added_to_map(map) + topic_added_to_map(map) + topic_added_to_map(map) + topic_added_to_map(map) + topic_added_to_map(map) + topic_added_to_map(map) + topic_added_to_map(map) + end + Timecop.return + end + + def generate_user + User.create(name: Faker::Name.name, email: Faker::Internet.email, password: "password", password_confirmation: "password", joinedwithcode: 'qwertyui') + end + + def generate_map + Map.create(name: Faker::HarryPotter.book, permission: 'commons', arranged: false, user: generate_user) + end + + def topic_added_to_map(map) + user = generate_user + topic = Topic.create(name: Faker::Friends.quote, permission: 'commons', user: user) + mapping = Mapping.create(map: map, mappable: topic, user: user) + end + + def topic_moved_on_map(mapping) + meta = { 'x': 10, 'y': 20, 'mapping_id': mapping.id } + Events::TopicMovedOnMap.publish!(mapping.mappable, mapping.map, generate_user, meta) + end + + def topic_removed_from_map(mapping) + user = generate_user + mapping.updated_by = user + mapping.destroy + end + + def synapse_added_to_map(map, topic1, topic2) + user = generate_user + topic = Synapse.create(desc: 'describes', permission: 'commons', user: user, topic1: topic1, topic2: topic2) + mapping = Mapping.create(map: map, mappable: topic, user: user) + end + + def synapse_removed_from_map(mapping) + user = generate_user + mapping.updated_by = user + mapping.destroy + end + + def generate_message(map) + Message.create(message: Faker::HarryPotter.quote, resource: map, user: generate_user) + end end diff --git a/spec/services/map_activity_service.rb b/spec/services/map_activity_service.rb index a187aa13..172dcf07 100644 --- a/spec/services/map_activity_service.rb +++ b/spec/services/map_activity_service.rb @@ -4,10 +4,11 @@ RSpec.describe MapActivityService do let(:map) { create(:map, created_at: 1.week.ago) } let(:other_user) { create(:user) } let(:email_user) { create(:user) } + let(:empty_response) { {stats:{}} } it 'includes nothing if nothing happened' do response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq({}) + expect(response).to eq (empty_response) end describe 'topics added to map' do @@ -46,7 +47,7 @@ RSpec.describe MapActivityService do mapping2 = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago) Event.where(kind: 'topic_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first.update_columns(created_at: 5.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes a topic added outside the last 24 hours' do @@ -54,7 +55,7 @@ RSpec.describe MapActivityService do mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago) Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes topics added by the user who will receive the data' do @@ -98,7 +99,7 @@ RSpec.describe MapActivityService do event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {}) event.update(created_at: 25.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes ones moved by the user who will receive the data' do @@ -106,7 +107,7 @@ RSpec.describe MapActivityService do event = Events::TopicMovedOnMap.publish!(topic, map, email_user, {}) event.update(created_at: 5.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end end @@ -132,7 +133,7 @@ RSpec.describe MapActivityService do mapping.destroy Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes topics removed by the user who will receive the data' do @@ -191,7 +192,7 @@ RSpec.describe MapActivityService do mapping2 = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago) Event.where(kind: 'synapse_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first.update_columns(created_at: 5.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes a synapse added outside the last 24 hours' do @@ -199,7 +200,7 @@ RSpec.describe MapActivityService do mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago) Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes synapses added by the user who will receive the data' do @@ -238,7 +239,7 @@ RSpec.describe MapActivityService do mapping.destroy Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago) response = MapActivityService.summarize_data(map, email_user) - expect(response[:stats]).to eq ({}) + expect(response).to eq (empty_response) end it 'excludes synapses removed by the user who will receive the data' do