Foorum
view release on metacpan or search on metacpan
lib/Foorum/ResultSet/User.pm view on Meta::CPAN
my @forum_roles = $schema->resultset('UserForum')
->search( { user_id => $user->user_id } )->all;
foreach (@forum_roles) {
$roles->{ $_->forum_id }->{ $_->status } = 1;
}
# user profile photo
my $profile_photo = $schema->resultset('UserProfilePhoto')
->find( { user_id => $user->user_id, } );
if ($profile_photo) {
$profile_photo = $profile_photo->{_column_data};
if ( $profile_photo->{type} eq 'upload' ) {
my $profile_photo_upload = $schema->resultset('Upload')
->get( $profile_photo->{value} );
$profile_photo->{upload} = $profile_photo_upload
if ($profile_photo_upload);
}
}
$user = $user->{_column_data};
$user->{details} = $user_details;
$user->{roles} = $roles;
$user->{profile_photo} = $profile_photo;
return $user;
}
sub delete_cache_by_user {
my ( $self, $user ) = @_;
return unless ($user);
my $schema = $self->result_source->schema;
my $cache = $schema->cache();
my @ckeys;
push @ckeys,
'user|'
. Object::Signature::signature( { user_id => $user->{user_id} } );
push @ckeys,
'user|'
. Object::Signature::signature( { username => $user->{username} } );
push @ckeys,
'user|' . Object::Signature::signature( { email => $user->{email} } );
foreach my $ckey (@ckeys) {
$cache->remove($ckey);
}
return 1;
}
sub delete_cache_by_user_cond {
my ( $self, $cond ) = @_;
my $user = $self->get($cond);
$self->delete_cache_by_user($user);
}
# call this update will delete cache.
sub update_user {
my ( $self, $user, $update ) = @_;
$self->delete_cache_by_user($user);
$self->search( { user_id => $user->{user_id} } )->update($update);
}
# update threads and replies count
sub update_threads_and_replies {
my ( $self, $user ) = @_;
my $schema = $self->result_source->schema;
# get $threads + $replies
my $total = $schema->resultset('Comment')->count(
{ author_id => $user->{user_id},
object_type => 'topic',
}
);
my $replies = $schema->resultset('Comment')->count(
{ author_id => $user->{user_id},
object_type => 'topic',
reply_to => 0,
}
);
$self->update_user( $user,
{ threads => $total - $replies, replies => $replies } );
}
# get user_settings
# we don't merge it into sub get_from_db is because it's not used so frequently
sub get_user_settings {
my ( $self, $user ) = @_;
my $schema = $self->result_source->schema;
my $cache = $schema->cache();
# this cachekey would be delete from Controller/Settings.pm
my $cachekey = 'user|user_settings|user_id=' . $user->{user_id};
my $cacheval = $cache->get($cachekey);
if ($cacheval) {
$cacheval = $cacheval->{val};
} else {
my $settings_rs = $schema->resultset('UserSettings')
->search( { user_id => $user->{user_id} } );
$cacheval = {};
while ( my $rs = $settings_rs->next ) {
$cacheval->{ $rs->type } = $rs->value;
}
$cache->set( $cachekey, { val => $cacheval, 1 => 2 } )
; # for empty $cacheval
}
# if not stored in db, we use default value;
my $default = {
'send_starred_notification' => 'Y',
'show_email_public' => 'Y',
};
my $ret = { %$default, %$cacheval }; # merge
return $ret;
}
sub validate_username {
my ( $self, $username ) = @_;
return 'LENGTH' if ( length($username) < 6 or length($username) > 20 );
( run in 1.736 second using v1.01-cache-2.11-cpan-97f6503c9c8 )