Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Cache activity data #57

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def connect
protected

def find_verified_user
if verified_user = env['warden'].user
if (verified_user = env['warden'].user)
verified_user
else
reject_unauthorized_connection
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@user = params[:id].to_i == -1 ? System.new : User.find(params[:id])
end

def index
Expand Down
48 changes: 48 additions & 0 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,54 @@ class Activity
vote.create
].freeze

# outer key: defines the key of an activity ('.' replaced with '_')
# => this way each key can cache different data
# inner key: caching_key
# inner value: method(s) to call on an activity (via send) to get the cache value
CACHE_CONFIG = {
app_create: {
app_name: 'trackable.name',
user_username: 'owner.username'
},
comment_create: {
comment_content: 'trackable.content',
user_username: 'owner.username',
stampable_name: 'recipient.stampable_name'
},
domain_create: {
domain_name: 'trackable.name',
user_username: 'owner.username'
},
stamp_accept: {
stampable_name: 'trackable.stampable_name',
stampable_type: 'trackable.type'
},
stamp_archive: {
stampable_name: 'trackable.stampable_name',
stampable_type: 'trackable.type'
},
stamp_create: {
stampable_name: 'trackable.stampable_name',
stampable_type: 'trackable.type'
},
stamp_deny: {
stampable_name: 'trackable.stampable_name',
stampable_type: 'trackable.type'
},
stamp_dispute: {
stampable_name: 'trackable.stampable_name',
stampable_type: 'trackable.type'
},
user_signup: {
user_username: 'trackable.username'
},
vote_create: {
user_username: 'owner.username',
stampable_name: 'recipient.stampable_name',
stampable_type: 'recipient.type'
}
}.freeze

def persisted?
false
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Comment < ApplicationRecord
include PublicActivity::Common

# :commentable can currently only be used with stamps
# comment.create activities caches data of stamps, so this would fail if used on another object
# in cache config => comment_create => stampable_name is cached
belongs_to :commentable, polymorphic: true
belongs_to :user

Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/votable/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Votable
module State
extend ActiveSupport::Concern

# rubocop:disable Metrics/BlockLength
included do
# used so boosts can reference the transition_activity
attr_accessor :transition_activity
Expand Down Expand Up @@ -54,6 +55,7 @@ module State
end
end
end
# rubocop:enable Metrics/BlockLength

def scheduled_at
created_at + ENVProxy.required_integer('STAMP_CONCLUDE_IN_HOURS').hours
Expand Down
2 changes: 1 addition & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Notification < ApplicationRecord
belongs_to :recipient, class_name: 'User'
belongs_to :reference, polymorphic: true

after_create { NotificationBroadcastWorker.perform_async(self.id) }
after_create { NotificationBroadcastWorker.perform_async(id) }

validates_presence_of %i[activity actor recipient reference]

Expand Down
7 changes: 6 additions & 1 deletion app/models/stamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def peers?
end

# must be implemented @ each subclass
# can describe a stronger connection than peers
# -> used to describe the stampable object in the UI
def stampable_name
raise NotImplementedError
end

# -> can describe a stronger connection than peers
def siblings
raise NotImplementedError
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/stamp/flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def app
stampable
end

def stampable_name
app.name
end

def siblings
peers
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/stamp/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def domain
stampable
end

def stampable_name
domain.name
end

def initial_stamp_cannot_be_0
return true if percentage != 0
return true if siblings.accepted.present?
Expand Down
8 changes: 8 additions & 0 deletions app/models/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ class System < User
def self.polymorphic_name
'System'
end

def created_at
'01-01-2019'.to_date
end

def updated_at
created_at
end
end
2 changes: 1 addition & 1 deletion app/views/activities/_comment.create.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
commented on a
= link_to activity.recipient_type, activity.recipient
of
= link_to activity.recipient.stampable.name, activity.recipient.stampable
= link_to activity.recipient.stampable_name, activity.recipient.stampable
2 changes: 1 addition & 1 deletion app/views/activities/_vote.create.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
voted for a
= link_to activity.recipient_type, activity.recipient
of
= link_to activity.recipient.stampable.name, activity.recipient.stampable
= link_to activity.recipient.stampable_name, activity.recipient.stampable
its
= activity.recipient.state
2 changes: 1 addition & 1 deletion app/views/notifications/_notification.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
= render partial: "notifications/#{notification.activity.key}", locals: { notification: notification }
= link_to notification.reference_type, notification.reference
of
= link_to notification.reference.stampable.name, notification.reference.stampable
= link_to notification.reference.stampable_name, notification.reference.stampable
2 changes: 1 addition & 1 deletion cable/config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require ::File.expand_path('../../config/environment', __FILE__)
require ::File.expand_path('../../config/environment', __FILE__)
Rails.application.eager_load!

run ActionCable.server
37 changes: 37 additions & 0 deletions config/initializers/public_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,41 @@
has_many :boosts, foreign_key: :trigger_id # a trigger can cause multiple boosts, iE stamp.accept

validates :key, presence: true, inclusion: { in: Activity::KEYS }

# key can only have one dot (.) as defined in Activity::KEYS
def config_key
key.sub('.', '_').to_sym
end

# if it is not set yet, it will fetch and set the data
# second call already retrieves the cache
# => adding caches in Activity::CACHE_CONFIG is no big deal & performant
def cache(cache_key)
value = parameters[cache_key.to_s]

return value if value.present? || !cache_key_set?(cache_key)

set_cache!
cache(cache_key)
end

def cache_key_set?(cache_key)
Activity::CACHE_CONFIG[config_key][cache_key.to_sym].present?
end

# if you want to reload all of the caches, use PublicActivity::Activity.all.each(&:set_cache!)
# though it is still performant to just get the caches whenever needed
def set_cache!
Activity::CACHE_CONFIG[config_key]&.each do |cache_key, retrievers|
parameters[cache_key.to_s] = fetch_value(retrievers)
end

save if changed?
end

def fetch_value(retrievers)
retrievers.split('.').inject(self) do |cache_value, retriever|
cache_value.send(retriever)
end
end
end
25 changes: 18 additions & 7 deletions spec/factories/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
association :owner, factory: :user
end

factory :domain_activity, parent: :activity do
association :trackable, factory: :domain

key { 'domain.create' }
end

factory :app_activity, parent: :activity do
association :trackable, factory: :app

key { 'app.create' }
end

factory :comment_activity, parent: :activity do
association :trackable, factory: :comment
recipient { trackable.commentable }
key { 'comment.create' }
end

factory :domain_activity, parent: :activity do
association :trackable, factory: :domain
key { 'domain.create' }
end

factory :signup_activity, parent: :activity do
association :trackable, factory: :user
key { 'user.signup' }
Expand All @@ -34,6 +38,13 @@
key { 'stamp.accept' }
end

factory :user_activity, parent: :activity do
association :trackable, factory: :user
owner { trackable }
recipient { nil }
key { 'user.create' }
end

factory :vote_activity, parent: :activity do
transient do
vote { create(:vote) }
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
association :commentable, factory: :label_stamp
end

# meaning: the comment & commentable were created by the same user
# meaning: the comment & commentable were created by the same user
factory :initial_comment, parent: :comment do
commentable { create(:label_stamp, creator: user) }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/stamps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

trait :with_comments do
after(:create) do |stamp, evaluator|
after(:create) do |stamp, _|
stamp.comments << FactoryBot.build(:comment, commentable: stamp, user_id: stamp.user_id)
stamp.comments << FactoryBot.build_list(:comment, 2, commentable: stamp)
stamp.save
Expand Down
25 changes: 0 additions & 25 deletions spec/models/activities_spec.rb

This file was deleted.

Loading