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
This commit is contained in:
parent
55b031ccb7
commit
b740fef8fe
15 changed files with 724 additions and 15 deletions
2
Gemfile
2
Gemfile
|
@ -51,4 +51,6 @@ group :development, :test do
|
|||
gem 'pry-rails'
|
||||
gem 'rubocop'
|
||||
gem 'tunemygc'
|
||||
gem 'faker'
|
||||
gem 'timecop'
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
11
app/mailers/map_activity_mailer.rb
Normal file
11
app/mailers/map_activity_mailer.rb
Normal file
|
@ -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
|
|
@ -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')
|
||||
|
|
|
@ -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)
|
||||
|
|
98
app/services/map_activity_service.rb
Normal file
98
app/services/map_activity_service.rb
Normal file
|
@ -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
|
57
app/views/map_activity_mailer/daily_summary.html.erb
Normal file
57
app/views/map_activity_mailer/daily_summary.html.erb
Normal file
|
@ -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" %>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<div style="padding: 16px; background: white; text-align: left; font-family: Arial">
|
||||
<p>Hey <%= @user.name %>, there was activity by others in the last 24 hours on map
|
||||
<%= link_to @map.name, map_url(@map) %>
|
||||
</p>
|
||||
<p># of messages: <%= @summary_data[:stats][:messages_sent] || 0 %></p>
|
||||
<p># of topics added: <%= @summary_data[:stats][:topics_added] || 0 %></p>
|
||||
<p># of topics moved: <%= @summary_data[:stats][:topics_moved] || 0%></p>
|
||||
<p># of topics removed: <%= @summary_data[:stats][:topics_removed] || 0 %></p>
|
||||
<p># of synapses added: <%= @summary_data[:stats][:synapses_added] || 0 %></p>
|
||||
<p># of synapses removed: <%= @summary_data[:stats][:synapses_removed] || 0 %></p>
|
||||
<hr>
|
||||
<% if @summary_data[:topics_added] %>
|
||||
<h2>Topics Added</h2>
|
||||
<ul>
|
||||
<% @summary_data[:topics_added].each do |event| %>
|
||||
<li><%= event.eventable.name %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
||||
<% if @summary_data[:topics_removed] %>
|
||||
<h2>Topics Removed</h2>
|
||||
<ul>
|
||||
<% @summary_data[:topics_removed].each do |event| %>
|
||||
<li><%= event.eventable.name %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
||||
<% if @summary_data[:synapses_added] %>
|
||||
<h2>Synapses Added</h2>
|
||||
<ul>
|
||||
<% @summary_data[:synapses_added].each do |event| %>
|
||||
<li><%= event.eventable.topic1.name %> -> <%= event.eventable.topic2.name %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
||||
<% if @summary_data[:synapses_removed] %>
|
||||
<h2>Synapses Removed</h2>
|
||||
<ul>
|
||||
<% @summary_data[:synapses_removed].each do |event| %>
|
||||
<li><%= event.eventable.topic1.name %> -> <%= event.eventable.topic2.name %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Visit Map', map_url(@map), style: button_style %>
|
||||
|
||||
<hr>
|
||||
<p style="font-size: 14px;">Make sense with Metamaps</p>
|
||||
<%= link_to 'Unfollow this map', unfollow_from_email_map_url(@map) %>
|
||||
<%= render partial: 'shared/mailer_unsubscribe_link' %>
|
||||
</div>
|
|
@ -1,3 +1,3 @@
|
|||
<div class="unsubscribe-link">
|
||||
<%= link_to 'Click here to unsubscribe from all Metamaps emails', unsubscribe_notifications_url(protocol: Rails.env.production? ? :https : :http) %>
|
||||
<%= link_to 'Unsubscribe from all Metamaps emails', unsubscribe_notifications_url(protocol: Rails.env.production? ? :https : :http) %>
|
||||
</div>
|
||||
|
|
|
@ -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
|
||||
|
|
20
lib/tasks/emails.rake
Normal file
20
lib/tasks/emails.rake
Normal file
|
@ -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
|
8
spec/factories/message.rb
Normal file
8
spec/factories/message.rb
Normal file
|
@ -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
|
6
spec/mailers/map_activity_mailer_spec.rb
Normal file
6
spec/mailers/map_activity_mailer_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MapActivityMailer, type: :mailer do
|
||||
|
||||
end
|
109
spec/mailers/previews/map_activity_mailer_preview.rb
Normal file
109
spec/mailers/previews/map_activity_mailer_preview.rb
Normal file
|
@ -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
|
398
spec/services/map_activity_service_spec.rb
Normal file
398
spec/services/map_activity_service_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue