From: Chris W. <la...@us...> - 2005-03-03 13:09:24
|
Update of /cvsroot/openinteract/OpenInteract2/pkg/comments/OpenInteract2/Action In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5517/OpenInteract2/Action Modified Files: Comments.pm Log Message: OIN-112: allow users to unsubscribe themselves from a thread; remove the 'remember me?' cookie if they don't select it Index: Comments.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/pkg/comments/OpenInteract2/Action/Comments.pm,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** Comments.pm 28 Feb 2005 01:56:12 -0000 1.13 --- Comments.pm 3 Mar 2005 13:08:57 -0000 1.14 *************** *** 18,21 **** --- 18,22 ---- my $DEFAULT_ECODE_LINE_LENGTH = 70; + my $DELIMITER = ':::'; # Display some summary info (plus a link) for an object *************** *** 237,243 **** # This is internal-only: reads ! # form_settings ! # comment ! # pre_escaped sub _show_editable { --- 238,244 ---- # This is internal-only: reads ! # form_settings (\%) ! # comment (object) ! # pre_escaped ($) sub _show_editable { *************** *** 245,252 **** $log ||= get_logger( LOG_APP ); ! my $form_settings => $self->param( 'form_settings' ); ! my $comment = $self->param( 'comment' ); ! ! my %params = ( form_settings => $form_settings ); --- 246,251 ---- $log ||= get_logger( LOG_APP ); ! my $comment = $self->param( 'comment' ); ! my %params = (); *************** *** 268,273 **** } ! $params{comment} = $comment; ! $params{object} = $self->_fetch_object_for_comment( $comment ); return $self->generate_content( \%params, { name => 'comments::comment_form_page' } ); --- 267,281 ---- } ! $params{comment} = $comment; ! $params{object} = $self->_fetch_object_for_comment( $comment ); ! ! my $remember_poster = $self->param( 'remember_poster' ); ! $params{remember_poster} = ! $remember_poster || ( $self->_has_cookie() ) ? 'yes' : ''; ! ! my $is_subscribed = $self->param( 'is_subscribed' ); ! $params{is_subscribed} = ! $is_subscribed || ( $self->_is_subscribed( $comment ) ) ? 'yes' : 'no'; ! return $self->generate_content( \%params, { name => 'comments::comment_form_page' } ); *************** *** 293,311 **** sub show_empty_form { my ( $self ) = @_; ! my %params = ( comment => $self->_fill_in_default_comment ); return $self->generate_content( \%params, { name => 'comments::comment_form' } ); } sub _fill_in_default_comment { my ( $self ) = @_; my $comment = OpenInteract2::Comment->new(); ! my $cookie_name = $self->param( 'cookie_name' ) || 'comment_info'; ! my $poster_info = CTX->request->cookie( $cookie_name ); if ( $poster_info ) { ! ( $comment->{poster_name}, ! $comment->{poster_url}, ! $comment->{poster_email} ) = split ':::', $poster_info; } my $object = $self->param( 'object' ); if ( $object ) { --- 301,346 ---- sub show_empty_form { my ( $self ) = @_; ! my $comment = $self->_fill_in_default_comment(); ! my $has_cookie = $self->_has_cookie(); ! my $is_subscribed = ( $has_cookie ) ! ? $self->_is_subscribed( $comment ) : 0; ! my %params = ( ! comment => $comment, ! remember_poster => $has_cookie ? 'yes' : '', ! is_subscribed => $is_subscribed ? 'yes' : 'no', ! ); return $self->generate_content( \%params, { name => 'comments::comment_form' } ); } + sub _is_subscribed { + my ( $self, $comment ) = @_; + my ( $c_class, $c_id ) = ( $comment->{class}, $comment->{object_id} ); + return 0 unless ( $c_class and $c_id ); + my $poster_info = $self->_get_info_from_cookie(); + return 0 unless ( $poster_info->{email} ); + my $items = OpenInteract2::CommentNotify->fetch_group({ + where => 'class = ? AND object_id = ? AND email = ?', + value => [ $c_class, $c_id, lc $poster_info->{email} ] + }); + return ( ref $items and scalar @{ $items } ); + } + + sub _has_cookie { + my ( $self ) = @_; + my $poster_info = $self->_get_info_from_cookie; + return ( $poster_info ) ? 1 : 0; + } + sub _fill_in_default_comment { my ( $self ) = @_; my $comment = OpenInteract2::Comment->new(); ! my $poster_info = $self->_get_info_from_cookie; if ( $poster_info ) { ! $comment->{poster_name} = $poster_info->{name}; ! $comment->{poster_url} = $poster_info->{url}; ! $comment->{poster_email} = $poster_info->{email}; } + my $object = $self->param( 'object' ); if ( $object ) { *************** *** 314,318 **** } else { ! $comment->{class} = $self->param( 'class' ); $comment->{object_id} = $self->param( 'object_id' ); } --- 349,353 ---- } else { ! $comment->{class} = $self->param( 'class' ); $comment->{object_id} = $self->param( 'object_id' ); } *************** *** 320,323 **** --- 355,391 ---- } + sub _get_cookie_name { + my ( $self ) = @_; + return $self->param( 'cookie_name' ) || + $self->param( 'default_cookie_name' ) || + 'comment_info'; + } + + sub _get_info_from_cookie { + my ( $self ) = @_; + my $cookie_name = $self->_get_cookie_name(); + my $poster_info = CTX->request->cookie( $cookie_name ); + return unless ( $poster_info ); + my ( $name, $url, $email ) = split $DELIMITER, $poster_info; + return { name => $name, url => $url, email => $email }; + } + + sub _store_cookie { + my ( $self, $name, $url, $email ) = @_; + my $cookie_info = join( $DELIMITER, $name, $url, $email ); + OpenInteract2::Cookie->create({ + name => $self->_get_cookie_name(), + value => $cookie_info, + expires => '+6M', + HEADER => 'yes', + }); + } + + sub _remove_cookie { + my ( $self ) = @_; + OpenInteract2::Cookie->expire( $self->_get_cookie_name() ); + } + + sub add { my ( $self ) = @_; *************** *** 396,403 **** # Non-object form widgets ! my %form_settings = ( ! remember_poster => scalar( $request->param( 'remember_poster' ) ), ! comment_notify => scalar( $request->param( 'comment_notify' ) ) ! ); my %required = ( --- 464,471 ---- # Non-object form widgets ! $self->param( remember_poster => scalar( $request->param( 'remember_poster' ) ) ); ! $self->param( is_subscribed => scalar( $request->param( 'is_subscribed' ) ) ); ! ! # check requirements my %required = ( *************** *** 411,416 **** } ! $self->param( comment => $comment ); ! $self->param( form_settings => \%form_settings ); if ( scalar @required_missing ) { --- 479,483 ---- } ! $self->param( comment => $comment ); if ( scalar @required_missing ) { *************** *** 435,452 **** my $remember = $request->param( 'remember_poster' ); if ( $remember eq 'yes' ) { ! my $cookie_info = join( ':::', $comment->{poster_name}, ! $comment->{poster_url}, ! $comment->{poster_email} ); ! my $cookie_name = $self->param( 'cookie_name' ) || ! $self->param( 'default_cookie_name' ) || ! 'comment_info'; ! OpenInteract2::Cookie->create({ ! name => $cookie_name, ! value => $cookie_info, ! expires => '+6M', ! HEADER => 'yes', ! }); ! $log->is_info && ! $log->info( "Created 'remember' cookie with '$cookie_info'" ); } --- 502,512 ---- my $remember = $request->param( 'remember_poster' ); if ( $remember eq 'yes' ) { ! $self->_store_cookie( $comment->{poster_name}, ! $comment->{poster_url}, ! $comment->{poster_email} ); ! $log->is_info && $log->info( "Created 'remember' cookie" ); ! } ! else { ! $self->_remove_cookie(); } *************** *** 468,477 **** $self->_add_auto_notifications( $comment ); $self->_process_notifications( $comment ); ! my $notify_status = $self->_add_user_notification( $comment ); ! if ( $notify_status ) { ! $self->add_status( $notify_status ); ! } } - return $self->list_by_object; } --- 528,533 ---- $self->_add_auto_notifications( $comment ); $self->_process_notifications( $comment ); ! $self->_check_user_notification( $comment ); } return $self->list_by_object; } *************** *** 554,567 **** # further posts. Return value is the status they see. ! sub _add_user_notification { my ( $self, $comment ) = @_; $log ||= get_logger( LOG_APP ); my $request = CTX->request; ! my $user_notify = $request->param( 'comment_notify' ); ! unless ( $user_notify eq 'yes' ) { ! $log->is_info && ! $log->info( "User has elected not to be notified of new ", ! "messages in thread" ); return; } --- 610,630 ---- # further posts. Return value is the status they see. ! sub _check_user_notification { my ( $self, $comment ) = @_; $log ||= get_logger( LOG_APP ); my $request = CTX->request; ! my $is_subscribed = $self->_is_subscribed( $comment ); ! my $user_sub_option = $request->param( 'is_subscribed' ); ! ! if ( $is_subscribed and $user_sub_option eq 'yes' ) { ! $log->is_debug && ! $log->debug( "User subscribes and wants to keep notification" ); ! return; ! } ! elsif ( ! $is_subscribed and $user_sub_option eq 'no' ) { ! $log->is_debug && ! $log->debug( "User is not subscriber and has elected not ", ! "to be notified of new messages in thread" ); return; } *************** *** 570,574 **** # return an error message ! unless ( $comment->{poster_email} ) { $log->is_info && $log->info( "Posted wants to be notified but didn't ", --- 633,638 ---- # return an error message ! my $email = $comment->{poster_email}; ! unless ( $email ) { $log->is_info && $log->info( "Posted wants to be notified but didn't ", *************** *** 578,587 **** } ! unless ( $comment->{poster_email} =~ /$Mail::Sendmail::address_rx/ ) { $log->is_info && $log->info( "Posted wants to be notified but provided a bad ", ! "email address: '$comment->{poster_email}'" ); ! $self->add_error_key( 'comments.error.cannot_add_notify_bad_email', ! $comment->{poster_email} ); return; } --- 642,651 ---- } ! unless ( $email =~ /$Mail::Sendmail::address_rx/ ) { $log->is_info && $log->info( "Posted wants to be notified but provided a bad ", ! "email address: '$email'" ); ! $self->add_error_key( ! 'comments.error.cannot_add_notify_bad_email', $email ); return; } *************** *** 590,629 **** # is already registered ! my $notes = eval { OpenInteract2::CommentNotify->fetch_group({ ! where => 'class = ? AND object_id = ?', ! value => [ $comment->{class}, $comment->{object_id} ], }) }; if ( $@ ) { $log->error( "Error fetching notifications for: ", ! "'$comment->{class}: $comment->{object_id}': $@" ); $self->add_error_key( 'comments.error.cannot_add_notify_error_dupe_check' ); ! return } ! my %existing_mail = map { lc $_->{email} => 1 } @{ $notes }; ! if ( $existing_mail{ lc $comment->{poster_email} } ) { ! $log->is_info && ! $log->info( "No notification added: already exists in thread" ); $self->add_error_key( 'comments.error.cannot_add_notify_is_dupe' ); - return; } ! eval { ! OpenInteract2::CommentNotify->new({ ! class => $comment->{class}, ! object_id => $comment->{object_id}, ! name => $comment->{poster_name}, ! email => lc $comment->{poster_email} ! })->save() ! }; ! if ( $@ ) { ! $log->error( "Failed to save notification: $@" ); ! $self->add_error_key( 'comments.error.cannot_add_notify_persist', "$@" ); ! return; } - $self->add_status_key( 'comments.status.add_notify_ok' ); - return; } --- 654,707 ---- # is already registered ! my $notifications = eval { OpenInteract2::CommentNotify->fetch_group({ ! where => 'class = ? AND object_id = ? AND email = ?', ! value => [ $comment->{class}, $comment->{object_id}, lc $email ], }) }; if ( $@ ) { $log->error( "Error fetching notifications for: ", ! "$comment->{class}; $comment->{object_id}; $email: $@" ); $self->add_error_key( 'comments.error.cannot_add_notify_error_dupe_check' ); ! return; } + my $existing_notifications = + ( ref $notifications eq 'ARRAY' and scalar @{ $notifications } ); ! if ( $is_subscribed and $existing_notifications ) { ! my $notify = $notifications->[0]; ! eval { $notify->remove }; ! if ( $@ ) { ! $log->error( "Cannot remove notification: $@" ); ! $self->add_error_key( 'comments.error.cannot_remove_notify' ); ! } ! else { ! $self->add_status_key( 'comments.status.remove_notify_ok' ); ! } ! } ! ! elsif ( $existing_notifications ) { ! $log->is_debug && ! $log->debug( "No notification added: already exists in thread" ); $self->add_error_key( 'comments.error.cannot_add_notify_is_dupe' ); } ! else { ! eval { ! OpenInteract2::CommentNotify->new({ ! class => $comment->{class}, ! object_id => $comment->{object_id}, ! name => $comment->{poster_name}, ! email => lc $comment->{poster_email} ! })->save() ! }; ! if ( $@ ) { ! $log->error( "Failed to save notification: $@" ); ! $self->add_error_key( 'comments.error.cannot_add_notify_persist', "$@" ); ! } ! else { ! $self->add_status_key( 'comments.status.add_notify_ok' ); ! } } } |