{
$Rubric::User::VERSION
=
'0.152'
;
}
__PACKAGE__->table(
'users'
);
__PACKAGE__->columns(
All
=>
qw(username password email created verification_code reset_code)
);
__PACKAGE__->has_many(
entries
=>
'Rubric::Entry'
);
__PACKAGE__->set_sql(
tags
=> <<
''
);
SELECT DISTINCT tag
FROM entrytags
WHERE entry IN (SELECT id FROM entries WHERE username = ?)
AND tag NOT LIKE
'@%%'
ORDER BY tag
sub
tags {
my
(
$self
) =
@_
;
my
$sth
=
$self
->sql_tags;
$sth
->execute(
$self
->username);
my
$tags
=
$sth
->fetchall_arrayref;
[
map
{
@$_
}
@$tags
];
}
__PACKAGE__->set_sql(
tags_counted
=> <<
''
);
SELECT DISTINCT tag, COUNT(*) AS count
FROM entrytags
WHERE entry IN (SELECT id FROM entries WHERE username = ?)
AND tag NOT LIKE
'@%%'
GROUP BY tag
ORDER BY tag
sub
tags_counted {
my
(
$self
) =
@_
;
my
$sth
=
$self
->sql_tags_counted;
$sth
->execute(
$self
->username);
my
$tags
=
$sth
->fetchall_arrayref;
return
$tags
;
}
sub
related_tags {
my
(
$self
,
$tags
,
$context
) =
@_
;
$tags
= [
keys
%$tags
]
if
ref
$tags
eq
'HASH'
;
return
unless
$tags
and
my
@tags
=
@$tags
;
return
[]
if
(
grep
{
$_
eq
'@private'
}
@$tags
)
and ((
$context
->{user}||
''
) ne
$self
->username);
my
$query
=
q|
SELECT DISTINCT tag FROM entrytags
WHERE entry IN (SELECT id FROM entries WHERE username = ?)
AND tag NOT IN (|
.
join
(
','
,
map
{
$self
->db_Main->quote(
$_
) }
@tags
) .
q|)
AND tag NOT LIKE '@%'
AND |
.
join
' AND '
,
map
{
"entry IN (SELECT entry FROM entrytags WHERE tag=$_)"
}
map
{
$self
->db_Main->quote(
$_
) }
@tags
;
$self
->db_Main->selectcol_arrayref(
$query
,
undef
,
$self
->username);
}
sub
related_tags_counted {
my
(
$self
,
$tags
,
$context
) =
@_
;
return
unless
$tags
;
$tags
= [
keys
%$tags
]
if
ref
$tags
eq
'HASH'
;
return
unless
my
@tags
=
@$tags
;
return
[]
if
(
grep
{
$_
eq
'@private'
}
@$tags
)
and ((
$context
->{user}||
''
) ne
$self
->username);
my
$query
=
q|
SELECT DISTINCT tag, COUNT(*) AS count
FROM entrytags
WHERE entry IN (SELECT id FROM entries WHERE username = ?)
AND tag NOT IN (|
.
join
(
','
,
map
{
$self
->db_Main->quote(
$_
) }
@tags
) .
q|)
AND tag NOT LIKE '@%'
AND |
.
join
' AND '
,
map
{
"entry IN (SELECT entry FROM entrytags WHERE tag=$_)"
}
map
{
$self
->db_Main->quote(
$_
) }
@tags
;
$query
.=
" GROUP BY tag"
;
$self
->db_Main->selectall_arrayref(
$query
,
undef
,
$self
->username);
}
__PACKAGE__->has_a(
created
=>
'Time::Piece'
,
deflate
=>
'epoch'
);
__PACKAGE__->add_trigger(
before_create
=> \
&_create_times
);
sub
_create_times {
my
$self
=
shift
;
$self
->created(
scalar
gmtime
)
unless
defined
$self
->{created};
}
sub
quick_entry {
my
(
$self
,
$entry
) =
@_
;
return
unless
$entry
->{title};
$entry
->{tags} = Rubric::Entry->tags_from_string(
$entry
->{tags});
my
$link
;
if
(
$entry
->{uri}) {
$link
=
eval
{ Rubric::Link->find_or_create({
uri
=>
$entry
->{uri} }) };
return
unless
$link
;
}
my
$new_entry
=
$entry
->{entryid}
? Rubric::Entry->retrieve(
$entry
->{entryid})
:
$link
? Rubric::Entry->find_or_create({
link
=>
$link
,
username
=>
$self
})
: Rubric::Entry->create({
username
=>
$self
});
$new_entry
->
link
(
$link
);
$new_entry
->title(
$entry
->{title});
$new_entry
->description(
$entry
->{description});
$new_entry
->body(
$entry
->{body} ||
undef
);
$new_entry
->update;
$new_entry
->set_new_tags(
$entry
->{tags});
return
$new_entry
;
}
sub
verify {
my
(
$self
,
$code
) =
@_
;
return
unless
$self
->verification_code;
if
(
$code
and
$code
eq
$self
->verification_code) {
$self
->verification_code(
undef
);
$self
->update;
return
1;
}
return
;
}
sub
reset_password {
my
(
$self
,
$code
) =
@_
;
return
unless
$self
->reset_code;
if
(
$code
and
$code
eq
$self
->reset_code) {
my
$password
=
$self
->randomize_password;
$self
->reset_code(
undef
);
$self
->update;
return
$password
;
}
return
;
}
sub
__random_string {
my
$length
= 15;
my
@legal
= (
'a'
..
'z'
,
'A'
..
'Z'
, 0..9);
my
$string
=
join
''
,
map
{
@legal
[
rand
@legal
] } 1 ..
$length
;
return
wantarray
? (md5_hex(
$string
),
$string
) : md5_hex(
$string
);
}
sub
randomize_password {
my
(
$self
) =
@_
;
my
(
$pass_md5
,
$password
) =
$self
->__random_string;
$self
->password(
$pass_md5
);
$self
->update;
return
$password
;
}
sub
randomize_reset_code {
my
(
$self
) =
@_
;
my
$reset_code
=
$self
->__random_string;
$self
->reset_code(
$reset_code
);
$self
->update;
}
sub
randomize_verification_code {
my
(
$self
) =
@_
;
my
$verification_code
=
$self
->__random_string;
$self
->verification_code(
$verification_code
);
$self
->update;
}
1;