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..781bf73f
--- /dev/null
+++ b/app/mailers/map_activity_mailer.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+class MapActivityMailer < ApplicationMailer
+ default from: 'team@metamaps.cc'
+
+ def daily_summary(user, summary_data)
+ @user = user
+ @summary_data = summary_data
+ mail(to: user.email, subject: 'some subject line')
+ 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/services/map_activity_service.rb b/app/services/map_activity_service.rb
new file mode 100644
index 00000000..7dee21fc
--- /dev/null
+++ b/app/services/map_activity_service.rb
@@ -0,0 +1,90 @@
+class MapActivityService
+
+ def self.summarize_data(map, user, till = DateTime.now)
+ results = {
+ stats: {}
+ }
+
+ since = till - 24.hours
+
+ message_count = Message.where(resource: map)
+ .where("created_at > ? AND created_at < ?", since, till)
+ .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.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.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.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
+ 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
+ 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, till)
+ .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.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
+ 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
+ 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..81ca2fd4
--- /dev/null
+++ b/app/views/map_activity_mailer/daily_summary.html.erb
@@ -0,0 +1,7 @@
+
+
+
+
+
Make sense with Metamaps
+ <%= render partial: 'shared/mailer_unsubscribe_link' %>
+
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..fcf805fe
--- /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
+ 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
+ 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..c3f6eab7
--- /dev/null
+++ b/spec/mailers/previews/map_activity_mailer_preview.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+# 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
+ summary_data = MapActivityService.summarize_data(map, user)
+ MapActivityMailer.daily_summary(user, summary_data)
+ end
+end
diff --git a/spec/services/map_activity_service.rb b/spec/services/map_activity_service.rb
new file mode 100644
index 00000000..a187aa13
--- /dev/null
+++ b/spec/services/map_activity_service.rb
@@ -0,0 +1,286 @@
+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) }
+
+ it 'includes nothing if nothing happened' do
+ response = MapActivityService.summarize_data(map, email_user)
+ expect(response[:stats]).to eq({})
+ 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[:stats]).to eq ({})
+ 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[:stats]).to eq ({})
+ 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)
+ 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)
+ 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)
+ 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 ({})
+ end
+
+ it 'excludes ones moved by the user who will receive the data' do
+ topic = create(:topic)
+ 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 ({})
+ 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[:stats]).to eq ({})
+ 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[:stats]).to eq ({})
+ 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[:stats]).to eq ({})
+ 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[:stats]).to eq ({})
+ 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
+
+ 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