Skip to content

Instantly share code, notes, and snippets.

@arunthampi
Last active April 18, 2016 19:02
Show Gist options
  • Save arunthampi/0775c941af484dc17a2ad4dde070220e to your computer and use it in GitHub Desktop.
Save arunthampi/0775c941af484dc17a2ad4dde070220e to your computer and use it in GitHub Desktop.
class IdentifyMixpanelUserJob
include Sidekiq::Worker
def perform(user_id, attributes = {})
email = nil
mixpanel_params = {}
user = User.find(user_id)
email = user.email
mixpanel_params = {
'$email' => user.email,
nickname: user.nickname,
created: user.created_at.as_json,
ip: user.current_sign_in_ip,
'$full_name' => user.full_name,
'$first_name' => user.first_name,
'$last_name' => user.last_name,
private_beta: user.profile.private_beta,
referral_code: user.profile.referral_code,
signed_in_via_oauth: user.signed_in_via_oauth
}
distinct_id = attributes.delete('distinct_id') || attributes.delete(:distinct_id)
if(mixpanel_properties = user.profile.mixpanel_properties).present?
mixpanel_params.merge!(mixpanel_properties)
end
mixpanel_params.merge!(attributes)
mixpanel = Mixpanel::Tracker.new(Settings.mixpanel_token)
mixpanel.alias(user.id, distinct_id) if distinct_id
mixpanel.people.set user.id, mixpanel_params
end
end
class Team < ActiveRecord::Base
has_many :team_memberships
has_many :members, through: :team_memberships, source: :user
has_one :bot
validates_presence_of :name, :uid, :url
validates_uniqueness_of :uid
validates_url :url, no_local: true, allow_nil: true
def to_param
self.uid
end
def import_users!
return if bot.blank?
slack_client = Slack.new(bot.token)
json_list = slack_client.call('users.list', :get)
if json_list['ok'] == true
Team.with_advisory_lock("team-import-#{self.uid}") do
json_list['members'].each do |user|
new_record = false
if(existing_membership = self.team_memberships.find_by_user_uid(user['id'])).blank?
u = User.new
new_record = true
else
u = existing_membership.user
end
u.nickname = user['name']
if u.email.blank?
u.email = user['profile']['email']
end
u.first_name = user['profile']['first_name']
u.last_name = user['profile']['last_name']
u.full_name = user['profile']['real_name']
u.timezone = user['tz']
u.timezone_description = user['tz_label']
u.timezone_offset = user['tz_offset'].to_i
u.save!
membership_type = TeamMembership.membership_type_from_hash(user)
if existing_membership.present?
existing_membership.update_attribute(:membership_type, membership_type)
else
TeamMembership.create!(user: u, team: self, membership_type: membership_type, user_uid: user['id'])
end
if new_record || u.changed_attributes.has_key?('email')
IdentifyMixpanelUserJob.new.perform(u.id)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment