|
| 1 | +package Tweetalyst::API::User; |
| 2 | +use Moose; |
| 3 | +use Digest::SHA1 qw/sha1_hex/; |
| 4 | +use namespace::autoclean; |
| 5 | + |
| 6 | +with 'Tweetalyst::API::WithDBIC'; |
| 7 | + |
| 8 | +#sub _build_resultset_constraints { return +{ username => {like => 'test%'} } }; |
| 9 | + |
| 10 | +# this returns who follows our user. |
| 11 | +# # Each element is a hash of usernames and gravatars |
| 12 | +sub get_followers_for { |
| 13 | + my ($self, $user) = @_; |
| 14 | + my $rs = $self->resultset->search( |
| 15 | + { |
| 16 | + 'follow_sources.destination' => $user |
| 17 | + }, |
| 18 | + { |
| 19 | + join => 'follow_sources', |
| 20 | + select => [qw/username gravatar/], |
| 21 | + }, |
| 22 | + ); |
| 23 | + $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
| 24 | + my %hash; |
| 25 | + while ( my $row = $rs->next ) { |
| 26 | + $hash{$row->{username}} = $row; |
| 27 | + } |
| 28 | + return \%hash; |
| 29 | + |
| 30 | +# return Model->selectall_hashref( |
| 31 | +# 'SELECT username, gravatar FROM user, follow |
| 32 | +#WHERE user.username = follow.source |
| 33 | +#AND follow.destination = ?', |
| 34 | +# 'username', {} , $_[0], |
| 35 | +# ); |
| 36 | +} |
| 37 | + |
| 38 | +# this returns who our user follows |
| 39 | +sub get_followed_by { |
| 40 | + my ($self, $user) = @_; |
| 41 | + my $rs = $self->resultset->search( |
| 42 | + { |
| 43 | + 'follow_destinations.source' => $user |
| 44 | + }, |
| 45 | + { |
| 46 | + join => 'follow_destinations', |
| 47 | + select => [qw/username gravatar/], |
| 48 | + }, |
| 49 | + ); |
| 50 | + $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
| 51 | + my %hash; |
| 52 | + while ( my $row = $rs->next ) { |
| 53 | + $hash{$row->{username}} = $row; |
| 54 | + } |
| 55 | + return \%hash; |
| 56 | +# return Model->selectall_hashref( |
| 57 | +# 'select username, gravatar from user, follow |
| 58 | +#where user.username = follow.destination |
| 59 | +#and follow.source = ?', |
| 60 | +# 'username', {}, $_[0], |
| 61 | +# ); |
| 62 | +} |
| 63 | + |
| 64 | +sub validate { |
| 65 | + my ($self, $user, $pass, $pass2, $routes) = @_; |
| 66 | + return 'username field must not be blank' unless $user && length $user; |
| 67 | + return 'password field must not be blank' unless $pass && length $pass; |
| 68 | + return 'please re-type your password' unless $pass2 && length $pass2; |
| 69 | + return "passwords don't match" unless $pass eq $pass2; |
| 70 | + return 'sorry, this user already exists' |
| 71 | + if $self->resultset->find($user); |
| 72 | + |
| 73 | + # let's not allow usernames that are part of a valid route |
| 74 | + #return 'sorry, invalid username' |
| 75 | + #if grep { length $_->name && index($user, $_->name) == 0 } @$routes; |
| 76 | + |
| 77 | + return; |
| 78 | + |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +sub create_user { |
| 83 | + my ($self, $username, $password, $email, $gravatar, $bio) = @_; |
| 84 | + |
| 85 | + $self->resultset->create({ |
| 86 | + username => $username, |
| 87 | + password => sha1_hex($password), |
| 88 | + email => $email, |
| 89 | + gravatar => sha1_hex($gravatar), |
| 90 | + bio => $bio, |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +__PACKAGE__->meta->make_immutable(); |
| 95 | + |
| 96 | +1; |
0 commit comments