Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling likes on posts #116

Open
wants to merge 5 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
6 changes: 4 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ def after_sign_out_path_for(_resource_or_scope)
new_user_session_path
end

# If getting started is active AND the users has not completed the getting_started page
def current_user_redirect_path
# If getting started is active AND the users has not completed the getting_started page
if current_user.getting_started? && !current_user.basic_profile_present?
user_profile = UserServices::Profile.new(current_user)

if user_profile.onboard_user?
getting_started_path
else
stream_path
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class LikesController < ApplicationController
include ApplicationHelper
include PostInteractionRender

before_action :authenticate_user!, except: :index
before_action :authenticate_user!

rescue_from Diaspora::Exceptions::NonPublic do
authenticate_user!
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/workers/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ApplicationJob < ActiveJob::Base
retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if the underlying records are no longer available
discard_on ActiveJob::DeserializationError
discard_on ActiveJob::DeserializationError, ActiveRecord::RecordNotUnique

sidekiq_options retry: 5
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/workers/gather_o_embed_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def perform(post_id, url, retry_count=1)
# we had a chance to run the job.
# On the other hand sometimes the job runs before the Post is
# fully persisted. So we just reduce the amount of retries.
GatherOEmbedData.perform_in(1.minute, post_id, url, retry_count + 1) unless retry_count > 3
Workers::GatherOEmbedData.perform_in(1.minute, post_id, url, retry_count + 1) unless retry_count > 3
end
end
end
83 changes: 77 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# frozen_string_literal: true

class User < ApplicationRecord
include Connecting
include Querying
include SocialActions
include Profile

# attr_accessor :plain_otp_secret

# encrypts :otp_secret
Expand Down Expand Up @@ -170,6 +165,82 @@ def seed_aspects
aq
end

def share_with(person, aspect)
UserServices::Connecting.new(self).share_with(person, aspect)
end

def disconnect(contact)
UserServices::Connecting.new(self).disconnect(contact)
end

def disconnected_by(person)
UserServices::Connecting.new(self).disconnected_by(person)
end

def comment!(target, text, opts={})
UserServices::SocialActions.new(self).comment!(target, text, opts)
end

def participate!(target, opts={})
UserServices::SocialActions.new(self).participate!(target, opts)
end

def participate_in_poll!(target, answer, opts={})
UserServices::SocialActions.new(self).participate_in_poll!(target, answer, opts)
end

def like!(target, opts={})
UserServices::SocialActions.new(self).like!(target, opts)
end

def like_comment!(target, opts={})
UserServices::SocialActions.new(self).like_comment!(target, opts)
end

def reshare!(target, opts={})
UserServices::SocialActions.new(self).reshare!(target, opts)
end

def find_visible_shareable_by_id(klass, id, opts={})
UserServices::Querying.new(self).find_visible_shareable_by_id(klass, id, opts)
end

def visible_shareables(klass, opts={})
UserServices::Querying.new(self).visible_shareables(klass, opts)
end

def posts_from(person, with_order: true)
UserServices::Querying.new(self).posts_from(person, with_order: with_order)
end

def photos_from(_person, opts={})
UserServices::Querying.new(self).photos_from(person, opts)
end

def contact_for(person)
UserServices::Querying.new(self).contact_for(person)
end

def block_for(person)
UserServices::Querying.new(self).block_for(person)
end

def aspects_with_shareable(base_class_name_or_class, shareable_id)
UserServices::Querying.new(self).aspects_with_shareable(base_class_name_or_class, shareable_id)
end

def contact_for_person_id(person_id)
UserServices::Querying.new(self).contact_for_person_id(person_id)
end

def people_in_aspects(requested_aspects, opts={})
UserServices::Querying.new(self).people_in_aspects(requested_aspects, opts)
end

def aspects_with_person(person)
UserServices::Querying.new(self).aspects_with_person(person)
end

def send_welcome_message
return unless AppConfig.settings.welcome_message.enabled? && AppConfig.admins.account?

Expand Down Expand Up @@ -242,7 +313,7 @@ def has_hidden_shareables_of_type?(t=Post)
alias send_reset_password_instructions! send_reset_password_instructions

def send_reset_password_instructions
ResetPasswordJob.perform_later(self)
Workers::ResetPasswordJob.perform_later(self)
end

def strip_and_downcase_username
Expand Down
35 changes: 0 additions & 35 deletions app/models/user/profile.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
# frozen_string_literal: true

class User
module Connecting
module UserServices
class Connecting
def initialize(user)
@user = user
end

# This will create a contact on the side of the sharer and the sharee.
# @param [Person] person The person to start sharing with.
# @param [Aspect] aspect The aspect to add them to.
# @return [Contact] The newly made contact for the passed in person.
def share_with(person, aspect)
return if blocks.exists?(person_id: person.id)
return if user.blocks.exists?(person_id: person.id)

contact = contacts.find_or_initialize_by(person_id: person.id)
contact = user.contacts.find_or_initialize_by(person_id: person.id)
return nil unless contact.valid?

needs_dispatch = !contact.receiving?
contact.receiving = true
contact.aspects << aspect
contact.save

if needs_dispatch
Diaspora::Federation::Dispatcher.defer_dispatch(self, contact)
deliver_profile_update(subscriber_ids: [person.id]) unless person.local?
end
dispatch_contact(contact, person) if needs_dispatch

Notifications::StartedSharing.where(recipient_id: id, target: person.id, unread: true)
# rubocop: disable Rails::SkipsModelValidations
Notifications::StartedSharing.where(recipient_id: user.id, target: person.id, unread: true)
.update_all(unread: false)
# rubocop: enable Rails::SkipsModelValidations

contact
end

def disconnect(contact)
logger.info "event=disconnect user=#{diaspora_handle} target=#{contact.person.diaspora_handle}"

if contact.person.local?
raise "FATAL: user entry is missing from the DB. Aborting" if contact.person.owner.nil?

contact.person.owner.disconnected_by(contact.user.person)
else
Diaspora::Federated::ContactRetraction.for(contact).defer_dispatch(self)
Diaspora::Federated::ContactRetraction.for(contact).defer_dispatch(user)
end

contact.aspect_memberships.delete_all
Expand All @@ -45,14 +46,24 @@ def disconnect(contact)
end

def disconnected_by(person)
logger.info "event=disconnected_by user=#{diaspora_handle} target=#{person.diaspora_handle}"
contact_for(person).try {|contact|
user.contact_for(person).try {|contact|
disconnect_contact(contact, direction: :sharing, destroy: !contact.receiving)
}
end

private

attr_reader :user

def dispatch_contact(contact, person)
Diaspora::Federation::Dispatcher.defer_dispatch(user, contact)
deliver_profile_update(subscriber_ids: [person.id]) unless person.local?
end

def deliver_profile_update(opts)
Profile.new(user).deliver_profile_update(opts)
end

def disconnect_contact(contact, direction:, destroy:)
if destroy
contact.destroy
Expand Down
49 changes: 49 additions & 0 deletions app/models/user_services/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module UserServices
class Profile
# @param [User] user up profiles for the user
def initialize(user)
@user = user
end

def onboard_user?
user.getting_started? && !basic_profile_present?
end

def update_profile_with_omniauth(user_info)
update_profile(user.profile.from_omniauth_hash(user_info))
end

def update_profile(params)
if photo = params.delete(:photo)
photo.update(pending: false) if photo.pending
params[:image_url] = photo.url(:thumb_large)
params[:image_url_medium] = photo.url(:thumb_medium)
params[:image_url_small] = photo.url(:thumb_small)
end

params.stringify_keys!
params.slice!(*(Profile.column_names + %w[tag_string date]))
if user.profile.update(params)
deliver_profile_update
true
else
false
end
end

def deliver_profile_update(opts={})
Diaspora::Federation::Dispatcher.defer_dispatch(user, user.profile, opts)
end

# A user should follow at least one person or should have a profile image
def basic_profile_present?
user.tag_followings.any? || user.profile[:image_url]
end

private

attr_reader :user
end
end
Loading
Loading