From b740fef8fe19b57452efa0661d4fc6a29e7819f7 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Mon, 6 Mar 2017 22:42:22 -0500 Subject: [PATCH] daily map activity emails (#1081) * data prepared, task setup * add the basics of the email template * cover granular permissions * unfollow this map * break out permissions tests better * rename so test runs --- Gemfile | 2 + Gemfile.lock | 7 +- app/mailers/application_mailer.rb | 4 - app/mailers/map_activity_mailer.rb | 11 + app/models/mapping.rb | 7 +- app/models/synapse.rb | 8 +- app/services/map_activity_service.rb | 98 +++++ .../daily_summary.html.erb | 57 +++ .../shared/_mailer_unsubscribe_link.html.erb | 2 +- config/initializers/mailboxer.rb | 2 - lib/tasks/emails.rake | 20 + spec/factories/message.rb | 8 + spec/mailers/map_activity_mailer_spec.rb | 6 + .../previews/map_activity_mailer_preview.rb | 109 +++++ spec/services/map_activity_service_spec.rb | 398 ++++++++++++++++++ 15 files changed, 724 insertions(+), 15 deletions(-) create mode 100644 app/mailers/map_activity_mailer.rb create mode 100644 app/services/map_activity_service.rb create mode 100644 app/views/map_activity_mailer/daily_summary.html.erb create mode 100644 lib/tasks/emails.rake create mode 100644 spec/factories/message.rb create mode 100644 spec/mailers/map_activity_mailer_spec.rb create mode 100644 spec/mailers/previews/map_activity_mailer_preview.rb create mode 100644 spec/services/map_activity_service_spec.rb 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/application_mailer.rb b/app/mailers/application_mailer.rb index 112b28ab..fdd2fa85 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -3,10 +3,6 @@ class ApplicationMailer < ActionMailer::Base default from: 'team@metamaps.cc' layout 'mailer' - def deliver - raise NotImplementedError('Please use Mailboxer to send your emails.') - end - class << self def mail_for_notification(notification) case notification.notification_code diff --git a/app/mailers/map_activity_mailer.rb b/app/mailers/map_activity_mailer.rb new file mode 100644 index 00000000..977ece4f --- /dev/null +++ b/app/mailers/map_activity_mailer.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +class MapActivityMailer < ApplicationMailer + default from: 'team@metamaps.cc' + + def daily_summary(user, map, summary_data) + @user = user + @map = map + @summary_data = summary_data + mail(to: user.email, subject: MapActivityService.subject_line(map)) + end +end diff --git a/app/models/mapping.rb b/app/models/mapping.rb index f8430bde..a49555a3 100644 --- a/app/models/mapping.rb +++ b/app/models/mapping.rb @@ -41,10 +41,11 @@ class Mapping < ApplicationRecord topic2: mappable.topic2.filtered, mapping_id: id ) - Events::SynapseAddedToMap.publish!(mappable, map, user, nil) + meta = { 'mapping_id': id } + Events::SynapseAddedToMap.publish!(mappable, map, user, meta) end end - + def after_created_async FollowService.follow(map, user, 'contributed') end @@ -57,7 +58,7 @@ class Mapping < ApplicationRecord ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicMoved', id: mappable.id, mapping_id: id, x: xloc, y: yloc end end - + def after_updated_async if (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?) FollowService.follow(map, updated_by, 'contributed') 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 new file mode 100644 index 00000000..51424b68 --- /dev/null +++ b/app/services/map_activity_service.rb @@ -0,0 +1,98 @@ +class MapActivityService + + def self.subject_line(map) + 'Activity on map ' + map.name + end + + def self.summarize_data(map, user, until_moment = DateTime.now) + results = { + stats: {} + } + + since = until_moment - 24.hours + + scoped_topic_ids = TopicPolicy::Scope.new(user, map.topics).resolve.map(&:id) + scoped_synapse_ids = SynapsePolicy::Scope.new(user, map.synapses).resolve.map(&:id) + + message_count = Message.where(resource: map) + .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, until_moment) + .where(eventable_id: scoped_topic_ids) + .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, 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, until_moment) + .where.not(user: user) + .order(:created_at) + + topics_added_to_include = {} + topics_added_events.each do |ta| + num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count + num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count + topics_added_to_include[ta.eventable_id] = ta if num_adds > num_removes && scoped_topic_ids.include?(ta.eventable.id) + end + if topics_added_to_include.keys.length > 0 + results[:stats][:topics_added] = topics_added_to_include.keys.length + results[:topics_added] = topics_added_to_include.values + end + + topics_removed_to_include = {} + topics_removed_events.each do |ta| + num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count + num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count + topics_removed_to_include[ta.eventable_id] = ta if num_removes > num_adds && TopicPolicy.new(user, ta.eventable).show? + end + if topics_removed_to_include.keys.length > 0 + results[:stats][:topics_removed] = topics_removed_to_include.keys.length + results[:topics_removed] = topics_removed_to_include.values + end + + synapses_added_events = Event.where(kind: 'synapse_added_to_map', map: map) + .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, until_moment) + .where.not(user: user) + .order(:created_at) + + synapses_added_to_include = {} + synapses_added_events.each do |ta| + num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count + num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count + synapses_added_to_include[ta.eventable_id] = ta if num_adds > num_removes && scoped_synapse_ids.include?(ta.eventable.id) + end + if synapses_added_to_include.keys.length > 0 + results[:stats][:synapses_added] = synapses_added_to_include.keys.length + results[:synapses_added] = synapses_added_to_include.values + end + + synapses_removed_to_include = {} + synapses_removed_events.each do |ta| + num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count + num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count + synapses_removed_to_include[ta.eventable_id] = ta if num_removes > num_adds && SynapsePolicy.new(user, ta.eventable).show? + end + if synapses_removed_to_include.keys.length > 0 + results[:stats][:synapses_removed] = synapses_removed_to_include.keys.length + results[:synapses_removed] = synapses_removed_to_include.values + end + + results + end +end diff --git a/app/views/map_activity_mailer/daily_summary.html.erb b/app/views/map_activity_mailer/daily_summary.html.erb new file mode 100644 index 00000000..bd9f79da --- /dev/null +++ b/app/views/map_activity_mailer/daily_summary.html.erb @@ -0,0 +1,57 @@ +<% 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 %>

+
+ <% 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

+ <%= link_to 'Unfollow this map', unfollow_from_email_map_url(@map) %> + <%= 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/config/initializers/mailboxer.rb b/config/initializers/mailboxer.rb index b09caec2..b8abd079 100644 --- a/config/initializers/mailboxer.rb +++ b/config/initializers/mailboxer.rb @@ -15,8 +15,6 @@ MAP_ACCESS_REQUEST = 'ACCESS_REQUEST' MAP_INVITE_TO_EDIT = 'INVITE_TO_EDIT' # these ones are new -# this one's a catch all for occurences on the map -# MAP_ACTIVITY = 'MAP_ACTIVITY' # MAP_RECEIVED_TOPIC # MAP_LOST_TOPIC # MAP_TOPIC_MOVED diff --git a/lib/tasks/emails.rake b/lib/tasks/emails.rake new file mode 100644 index 00000000..b2a7303a --- /dev/null +++ b/lib/tasks/emails.rake @@ -0,0 +1,20 @@ +namespace :metamaps do + desc "delivers recent map activity digest emails to users" + task deliver_map_activity_emails: :environment do + summarize_map_activity + end + + def summarize_map_activity + 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/factories/message.rb b/spec/factories/message.rb new file mode 100644 index 00000000..a0930fec --- /dev/null +++ b/spec/factories/message.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +FactoryGirl.define do + factory :message do + association :resource, factory: :map + user + sequence(:message) { |n| "Cool Message ##{n}" } + end +end diff --git a/spec/mailers/map_activity_mailer_spec.rb b/spec/mailers/map_activity_mailer_spec.rb new file mode 100644 index 00000000..897a38c0 --- /dev/null +++ b/spec/mailers/map_activity_mailer_spec.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe MapActivityMailer, type: :mailer do + +end diff --git a/spec/mailers/previews/map_activity_mailer_preview.rb b/spec/mailers/previews/map_activity_mailer_preview.rb new file mode 100644 index 00000000..3d943eee --- /dev/null +++ b/spec/mailers/previews/map_activity_mailer_preview.rb @@ -0,0 +1,109 @@ +# frozen_string_literal: true +# Preview all emails at http://localhost:3000/rails/mailers/map_activity_mailer +class MapActivityMailerPreview < ActionMailer::Preview + def daily_summary + user = generate_user + map = generate_map + generate_recent_activity_on_map(map) + summary_data = MapActivityService.summarize_data(map, user) + 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_spec.rb b/spec/services/map_activity_service_spec.rb new file mode 100644 index 00000000..fe283e01 --- /dev/null +++ b/spec/services/map_activity_service_spec.rb @@ -0,0 +1,398 @@ +require 'rails_helper' + +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).to eq (empty_response) + end + + describe 'topics added to map' do + it 'includes a topic added within the last 24 hours' do + topic = create(:topic) + mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 6.hours.ago) + event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id) + event.update_columns(created_at: 6.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_added]).to eq(1) + expect(response[:topics_added]).to eq([event]) + end + + it 'includes a topic added, then removed, then re-added within the last 24 hours' do + topic = create(:topic) + mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 6.hours.ago) + Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 6.hours.ago) + mapping.updated_by = other_user + mapping.destroy + Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 5.hours.ago) + mapping2 = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 4.hours.ago) + event = Event.where("meta->>'mapping_id' = ?", mapping2.id.to_s).first + event.update_columns(created_at: 4.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_added]).to eq(1) + expect(response[:topics_added]).to eq([event]) + end + + it 'excludes a topic removed then re-added within the last 24 hours' do + topic = create(:topic) + 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) + mapping.updated_by = other_user + mapping.destroy + Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 6.hours.ago) + 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).to eq (empty_response) + end + + it 'excludes a topic added outside the last 24 hours' do + topic = create(:topic) + 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).to eq (empty_response) + end + + it 'excludes topics added by the user who will receive the data' do + topic = create(:topic) + topic2 = create(:topic) + mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago) + event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id) + event.update_columns(created_at: 5.hours.ago) + mapping2 = create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 5.hours.ago) + Event.find_by(kind: 'topic_added_to_map', eventable_id: topic2.id).update_columns(created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_added]).to eq(1) + expect(response[:topics_added]).to eq([event]) + end + end + + describe 'topics moved on map' do + it 'includes ones moved within the last 24 hours' do + topic = create(:topic) + create(:mapping, user: email_user, map: map, mappable: topic, created_at: 5.hours.ago) + event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {}) + event.update(created_at: 6.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_moved]).to eq(1) + end + + it 'only includes each topic that was moved in the count once' do + topic = create(:topic) + topic2 = create(:topic) + create(:mapping, user: email_user, map: map, mappable: topic, created_at: 5.hours.ago) + create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 5.hours.ago) + event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {}) + event.update(created_at: 6.hours.ago) + event2 = Events::TopicMovedOnMap.publish!(topic, map, other_user, {}) + event2.update(created_at: 5.hours.ago) + event3 = Events::TopicMovedOnMap.publish!(topic2, map, other_user, {}) + event3.update(created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_moved]).to eq(2) + end + + it 'excludes ones moved outside the last 24 hours' do + topic = create(:topic) + create(:mapping, user: email_user, map: map, mappable: topic, created_at: 5.hours.ago) + event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {}) + event.update(created_at: 25.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response).to eq (empty_response) + end + + it 'excludes ones moved by the user who will receive the data' do + topic = create(:topic) + create(:mapping, user: email_user, map: map, mappable: topic, created_at: 5.hours.ago) + event = Events::TopicMovedOnMap.publish!(topic, map, email_user, {}) + event.update(created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response).to eq (empty_response) + end + end + + describe 'topics removed from map' do + it 'includes a topic removed within the last 24 hours' do + topic = create(:topic) + 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) + mapping.updated_by = other_user + mapping.destroy + event = Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id) + event.update_columns(created_at: 6.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_removed]).to eq(1) + expect(response[:topics_removed]).to eq([event]) + end + + it 'excludes a topic removed outside the last 24 hours' do + topic = create(:topic) + mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 26.hours.ago) + Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 26.hours.ago) + mapping.updated_by = other_user + 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).to eq (empty_response) + end + + it 'excludes topics removed by the user who will receive the data' do + topic = create(:topic) + topic2 = create(:topic) + 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) + mapping2 = create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 25.hours.ago) + Event.find_by(kind: 'topic_added_to_map', eventable_id: topic2.id).update_columns(created_at: 25.hours.ago) + mapping.updated_by = other_user + mapping.destroy + mapping2.updated_by = email_user + mapping2.destroy + event = Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id) + event.update_columns(created_at: 5.hours.ago) + Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic2.id).update_columns(created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:topics_removed]).to eq(1) + expect(response[:topics_removed]).to eq([event]) + end + end + + describe 'synapses added to map' do + it 'includes a synapse added within the last 24 hours' do + synapse = create(:synapse) + mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 6.hours.ago) + event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id) + event.update_columns(created_at: 6.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:synapses_added]).to eq(1) + expect(response[:synapses_added]).to eq([event]) + end + + it 'includes a synapse added, then removed, then re-added within the last 24 hours' do + synapse = create(:synapse) + mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 6.hours.ago) + Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 6.hours.ago) + mapping.updated_by = other_user + mapping.destroy + Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 5.hours.ago) + mapping2 = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 4.hours.ago) + event = Event.where(kind: 'synapse_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first + event.update_columns(created_at: 4.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:synapses_added]).to eq(1) + expect(response[:synapses_added]).to eq([event]) + end + + it 'excludes a synapse removed then re-added within the last 24 hours' do + synapse = create(:synapse) + 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) + mapping.updated_by = other_user + mapping.destroy + Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 6.hours.ago) + 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).to eq (empty_response) + end + + it 'excludes a synapse added outside the last 24 hours' do + synapse = create(:synapse) + 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).to eq (empty_response) + end + + it 'excludes synapses added by the user who will receive the data' do + synapse = create(:synapse) + synapse2 = create(:synapse) + mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago) + event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id) + event.update_columns(created_at: 5.hours.ago) + mapping2 = create(:mapping, user: email_user, map: map, mappable: synapse2, created_at: 5.hours.ago) + Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:synapses_added]).to eq(1) + expect(response[:synapses_added]).to eq([event]) + end + end + + describe 'synapses removed from map' do + it 'includes a synapse removed within the last 24 hours' do + synapse = create(:synapse) + 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) + mapping.updated_by = other_user + mapping.destroy + event = Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id) + event.update_columns(created_at: 6.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:synapses_removed]).to eq(1) + expect(response[:synapses_removed]).to eq([event]) + end + + it 'excludes a synapse removed outside the last 24 hours' do + synapse = create(:synapse) + 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) + mapping.updated_by = other_user + 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).to eq (empty_response) + end + + it 'excludes synapses removed by the user who will receive the data' do + synapse = create(:synapse) + synapse2 = create(:synapse) + 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) + mapping2 = create(:mapping, user: email_user, map: map, mappable: synapse2, created_at: 25.hours.ago) + Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse2.id).update_columns(created_at: 25.hours.ago) + mapping.updated_by = other_user + mapping.destroy + mapping2.updated_by = email_user + mapping2.destroy + event = Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id) + event.update_columns(created_at: 5.hours.ago) + Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:synapses_removed]).to eq(1) + expect(response[:synapses_removed]).to eq([event]) + end + end + + it 'handles permissions for topics added' do + new_topic = nil + new_private_topic = nil + + Timecop.freeze(10.hours.ago) do + new_topic = create(:topic, permission: 'commons', user: other_user) + create(:mapping, map: map, mappable: new_topic, user: other_user) + new_private_topic = create(:topic, permission: 'private', user: other_user) + create(:mapping, map: map, mappable: new_private_topic, user: other_user) + end + Timecop.return + + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats]).to eq({ + topics_added: 1 + }) + expect(response[:topics_added].map(&:eventable_id)).to include(new_topic.id) + expect(response[:topics_added].map(&:eventable_id)).to_not include(new_private_topic.id) + end + + it 'handles permissions for topics removed' do + old_topic = nil + old_private_topic = nil + old_topic_mapping = nil + old_private_topic_mapping = nil + + Timecop.freeze(2.days.ago) do + old_topic = create(:topic, permission: 'commons', user: other_user) + old_topic_mapping = create(:mapping, map: map, mappable: old_topic, user: other_user) + old_private_topic = create(:topic, permission: 'private', user: other_user) + old_private_topic_mapping = create(:mapping, map: map, mappable: old_private_topic, user: other_user) + end + Timecop.return + + Timecop.freeze(10.hours.ago) do + # visible + old_topic_mapping.updated_by = other_user + old_topic_mapping.destroy + # not visible + old_private_topic_mapping.updated_by = other_user + old_private_topic_mapping.destroy + end + Timecop.return + + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats]).to eq({ + topics_removed: 1 + }) + expect(response[:topics_removed].map(&:eventable_id)).to include(old_topic.id) + expect(response[:topics_removed].map(&:eventable_id)).to_not include(old_private_topic.id) + end + + it 'handles permissions for synapses added' do + new_synapse = nil + new_private_synapse = nil + + Timecop.freeze(10.hours.ago) do + # visible + new_synapse = create(:synapse, permission: 'commons', user: other_user) + create(:mapping, map: map, mappable: new_synapse, user: other_user) + # not visible + new_private_synapse = create(:synapse, permission: 'private', user: other_user) + create(:mapping, map: map, mappable: new_private_synapse, user: other_user) + end + Timecop.return + + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats]).to eq({ + synapses_added: 1 + }) + expect(response[:synapses_added].map(&:eventable_id)).to include(new_synapse.id) + expect(response[:synapses_added].map(&:eventable_id)).to_not include(new_private_synapse.id) + end + + it 'handles permissions for synapses removed' do + old_synapse = nil + old_private_synapse = nil + old_synapse_mapping = nil + old_private_synapse_mapping = nil + + Timecop.freeze(2.days.ago) do + old_synapse = create(:synapse, permission: 'commons', user: other_user) + old_synapse_mapping = create(:mapping, map: map, mappable: old_synapse, user: other_user) + old_private_synapse = create(:synapse, permission: 'private', user: other_user) + old_private_synapse_mapping = create(:mapping, map: map, mappable: old_private_synapse, user: other_user) + end + Timecop.return + + Timecop.freeze(10.hours.ago) do + # visible + old_synapse_mapping.updated_by = other_user + old_synapse_mapping.destroy + # not visible + old_private_synapse_mapping.updated_by = other_user + old_private_synapse_mapping.destroy + end + Timecop.return + + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats]).to eq({ + synapses_removed: 1 + }) + expect(response[:synapses_removed].map(&:eventable_id)).to include(old_synapse.id) + expect(response[:synapses_removed].map(&:eventable_id)).to_not include(old_private_synapse.id) + end + + describe 'messages in the map chat' do + it 'counts messages within the last 24 hours' do + create(:message, resource: map, created_at: 6.hours.ago) + create(:message, resource: map, created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:messages_sent]).to eq(2) + end + + it 'does not count messages outside the last 24 hours' do + create(:message, resource: map, created_at: 25.hours.ago) + create(:message, resource: map, created_at: 5.hours.ago) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:messages_sent]).to eq(1) + end + + it 'does not count messages sent by the person who will receive the data' do + create(:message, resource: map, created_at: 5.hours.ago, user: other_user) + create(:message, resource: map, created_at: 5.hours.ago, user: email_user) + response = MapActivityService.summarize_data(map, email_user) + expect(response[:stats][:messages_sent]).to eq(1) + end + end +end