| 1 | package Vee::Schema::Forums; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use base 'DBIx::Class'; |
|---|
| 6 | |
|---|
| 7 | =head1 NAME |
|---|
| 8 | |
|---|
| 9 | Vee::Schema::Forums - DBIC class for the C<forums> table |
|---|
| 10 | |
|---|
| 11 | =cut |
|---|
| 12 | |
|---|
| 13 | __PACKAGE__->load_components(qw/ ResultSetManager Core /); |
|---|
| 14 | __PACKAGE__->table('forums'); |
|---|
| 15 | __PACKAGE__->add_columns(qw/ |
|---|
| 16 | id name last_post_id thread_count post_count flags accessibility description |
|---|
| 17 | /); |
|---|
| 18 | __PACKAGE__->set_primary_key('id'); |
|---|
| 19 | |
|---|
| 20 | __PACKAGE__->has_many(threads => 'Vee::Schema::Threads', 'forum_id'); |
|---|
| 21 | __PACKAGE__->might_have(last_post => 'Vee::Schema::Posts', { 'foreign.id' => 'self.last_post_id' }); |
|---|
| 22 | |
|---|
| 23 | =head2 unread_ids($user) |
|---|
| 24 | |
|---|
| 25 | Returns a list of all forum ids the given user has not read. |
|---|
| 26 | |
|---|
| 27 | =cut |
|---|
| 28 | |
|---|
| 29 | sub unread_ids : ResultSet { |
|---|
| 30 | my ($self, $user) = @_; |
|---|
| 31 | |
|---|
| 32 | # ->get_column doesn't seem to work here; it tries 'SELECT id' and I get |
|---|
| 33 | # ambiguity errors |
|---|
| 34 | return map { $_->id } $self->search( { |
|---|
| 35 | 'threads.last_post_time' => { '>' => $user->thread_view_cutoff }, |
|---|
| 36 | 'thread_views.last_viewed' => [ undef, \'< threads.last_post_time' ], |
|---|
| 37 | }, { |
|---|
| 38 | # XXX: This is very naughty, but the only way to stuff the user id in |
|---|
| 39 | # the ON clause |
|---|
| 40 | from => [ |
|---|
| 41 | { me => 'forums' }, |
|---|
| 42 | [ |
|---|
| 43 | [ |
|---|
| 44 | { threads => 'threads' }, |
|---|
| 45 | [ |
|---|
| 46 | { |
|---|
| 47 | thread_views => 'thread_views', |
|---|
| 48 | -join_type => 'left' |
|---|
| 49 | }, { # ON |
|---|
| 50 | 'thread_views.thread_id' => 'threads.id', |
|---|
| 51 | 'thread_views.user_id' => $user->id, |
|---|
| 52 | }, |
|---|
| 53 | ], |
|---|
| 54 | ], |
|---|
| 55 | { 'me.id' => 'threads.forum_id' }, |
|---|
| 56 | ], |
|---|
| 57 | ], |
|---|
| 58 | group_by => 'me.id', |
|---|
| 59 | columns => [qw[ me.id ]], |
|---|
| 60 | } )->all; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | =head1 SEE ALSO |
|---|
| 64 | |
|---|
| 65 | L<Vee::Schema>, L<DBIx::Class> |
|---|
| 66 | |
|---|
| 67 | =head1 AUTHOR |
|---|
| 68 | |
|---|
| 69 | Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>) |
|---|
| 70 | |
|---|
| 71 | See the included F<AUTHORS> file for a full list of contributers. |
|---|
| 72 | |
|---|
| 73 | =head1 LICENSE |
|---|
| 74 | |
|---|
| 75 | See the included F<LICENSE> file. |
|---|
| 76 | |
|---|
| 77 | =cut |
|---|
| 78 | |
|---|
| 79 | 1; |
|---|