| 1 | package App::SD::Replica::hm::PushEncoder; | ||
| 2 | use Any::Moose; | ||
| 3 | |||
| 4 | extends 'App::SD::ForeignReplica::PushEncoder'; | ||
| 5 | |||
| 6 | use Params::Validate; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Path::Class; | ||
| 9 | has sync_source => ( | ||
| 10 | isa => 'App::SD::Replica::hm', | ||
| 11 | is => 'rw' | ||
| 12 | ); | ||
| 13 | |||
| 14 | sub integrate_ticket_create { | ||
| 15 | my $self = shift; | ||
| 16 | my ( $change, $changeset ) | ||
| 17 | = validate_pos( @_, { isa => 'Prophet::Change' }, { isa => 'Prophet::ChangeSet' } ); | ||
| 18 | |||
| 19 | # Build up a ticket object out of all the record's attributes | ||
| 20 | my %args = ( | ||
| 21 | owner => 'me', | ||
| 22 | group => 0, | ||
| 23 | complete => 0, | ||
| 24 | will_complete => 1, | ||
| 25 | repeat_stacking => 0, | ||
| 26 | %{ $self->_recode_props_for_create($change) } | ||
| 27 | ); | ||
| 28 | |||
| 29 | my $hm_user = $self->sync_source->user_info(email => $self->sync_source->foreign_username); | ||
| 30 | |||
| 31 | my @requesters; | ||
| 32 | if ( $args{'requestor_id'} ) { | ||
| 33 | require Email::Address; | ||
| 34 | |||
| 35 | my $pusher_is_requester = 0; | ||
| 36 | |||
| 37 | @requesters = Email::Address->parse( $args{'requestor_id'} ); | ||
| 38 | @requesters = grep { | ||
| 39 | lc( $_->address ) eq lc( $hm_user->{'email'} ) ? do { $pusher_is_requester = 1; 0 } : 1 | ||
| 40 | } @requesters; | ||
| 41 | |||
| 42 | unless ($pusher_is_requester) { | ||
| 43 | |||
| 44 | # XXX: this doesn't work, HM is too protective | ||
| 45 | # unless ( $hm_user->{'pro_account'} ) { | ||
| 46 | # warn "Only pro accounts can set requestor in HM"; | ||
| 47 | $args{'requestor_id'} = $hm_user->{'email'}; | ||
| 48 | |||
| 49 | # } | ||
| 50 | # else { | ||
| 51 | # $args{'requestor_id'} = shift(@requesters)->format; | ||
| 52 | # } | ||
| 53 | } else { | ||
| 54 | $args{'requestor_id'} = $hm_user->{'email'}; | ||
| 55 | } | ||
| 56 | if (@requesters) { | ||
| 57 | warn "A ticket has more than one requestor when HM supports only one"; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | my $task = $self->sync_source->hm->create( 'Task', %args ); | ||
| 62 | # a successful create just returns the task's data, and doesn't | ||
| 63 | # have a 'success' member at all | ||
| 64 | if ( $self->sync_source->request_failed($task) ) { | ||
| 65 | die "Couldn't create a task: " . $self->sync_source->decode_error($task); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $tid = $task->{id}; | ||
| 69 | |||
| 70 | if (@requesters) { | ||
| 71 | my $email = $self->comment_as_email( | ||
| 72 | { creator => $hm_user->{'email'}, | ||
| 73 | content => "Additional requestors: " . join( ', ', map $_->format, @requesters ), | ||
| 74 | } | ||
| 75 | ); | ||
| 76 | my $status = $self->sync_source->hm->act( | ||
| 77 | 'CreateTaskEmail', | ||
| 78 | task_id => $tid, | ||
| 79 | message => $email->as_string, | ||
| 80 | ); | ||
| 81 | warn "Couldn't add a comment on the recently created HM task" | ||
| 82 | if $self->sync_source->request_failed($status); | ||
| 83 | } | ||
| 84 | |||
| 85 | my $txns = $self->sync_source->hm->search( 'TaskTransaction', task_id => $tid ); | ||
| 86 | |||
| 87 | # lalala | ||
| 88 | $self->sync_source->record_pushed_transaction( | ||
| 89 | transaction => $txns->[0]->{id}, | ||
| 90 | changeset => $changeset, | ||
| 91 | record => $tid | ||
| 92 | ); | ||
| 93 | |||
| 94 | return $tid; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub integrate_comment { | ||
| 98 | my $self = shift; | ||
| 99 | my ( $change, $changeset ) | ||
| 100 | = validate_pos( @_, { isa => 'Prophet::Change' }, { isa => 'Prophet::ChangeSet' } ); | ||
| 101 | |||
| 102 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 103 | |||
| 104 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} ) | ||
| 105 | or die "Couldn't get remote id of SD ticket"; | ||
| 106 | |||
| 107 | my $email = $self->comment_as_email( \%props ); | ||
| 108 | my $status = $self->sync_source->hm->act( | ||
| 109 | 'CreateTaskEmail', | ||
| 110 | task_id => $ticket_id, | ||
| 111 | message => $email->as_string, | ||
| 112 | ); | ||
| 113 | return $status->{'id'} unless $self->sync_source->request_failed($status); | ||
| 114 | |||
| 115 | die "Couldn't integrate comment: " . $self->sync_source->decode_error($status); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub integrate_ticket_update { | ||
| 119 | my $self = shift; | ||
| 120 | my ( $change, $changeset ) | ||
| 121 | = validate_pos( @_, { isa => 'Prophet::Change' }, { isa => 'Prophet::ChangeSet' } ); | ||
| 122 | |||
| 123 | my %args = $self->translate_props($change); | ||
| 124 | return unless keys %args; | ||
| 125 | |||
| 126 | my $tid = $self->sync_source->remote_id_for_uuid( $change->record_uuid ) | ||
| 127 | or die "Couldn't get remote id of SD ticket"; | ||
| 128 | |||
| 129 | my ( $seen_current, $dropped_all, @new_requestors ) = ( 0, 0 ); | ||
| 130 | if ( exists $args{'requestor_id'} | ||
| 131 | && defined $args{'requestor_id'} | ||
| 132 | && length $args{'requestor_id'} ) | ||
| 133 | { | ||
| 134 | my $task = $self->sync_source->hm->read( 'Task', id => $tid ); | ||
| 135 | my $current_requestor = $self->sync_source->user_info( id => $task->{'requester_id'} ); | ||
| 136 | |||
| 137 | require Email::Address; | ||
| 138 | @new_requestors = Email::Address->parse( delete $args{'requestor_id'} ); | ||
| 139 | @new_requestors = grep { | ||
| 140 | ( lc( $_->address ) eq lc( $current_requestor->{'email'} ) ) | ||
| 141 | ? do { $seen_current = 1; 0; } | ||
| 142 | : 1 | ||
| 143 | } @new_requestors; | ||
| 144 | |||
| 145 | unless ($seen_current) { | ||
| 146 | warn "Requestor can not be changed in HM"; | ||
| 147 | } | ||
| 148 | if ( ( @new_requestors && $seen_current ) || @new_requestors > 1 ) { | ||
| 149 | warn "Can not set more than one requestor in HM"; | ||
| 150 | } | ||
| 151 | } elsif ( exists $args{'requestor_id'} ) { | ||
| 152 | $dropped_all = 1; | ||
| 153 | delete $args{'requestor_id'}; | ||
| 154 | warn "Requestor can not be empty in HM"; | ||
| 155 | } | ||
| 156 | |||
| 157 | my $txn_id; | ||
| 158 | if ( keys %args ) { | ||
| 159 | my $status = $self->sync_source->hm->act( | ||
| 160 | 'UpdateTask', | ||
| 161 | id => $tid, | ||
| 162 | %args, | ||
| 163 | ); | ||
| 164 | die "Couldn't integrate ticket update: " . $self->sync_source->decode_error($status) | ||
| 165 | if $self->sync_source->request_failed($status); | ||
| 166 | $txn_id = $status->{'id'}; | ||
| 167 | } | ||
| 168 | |||
| 169 | if (@new_requestors) { | ||
| 170 | my $comment_id = $self->record_comment( | ||
| 171 | task => $tid, | ||
| 172 | content => ( | ||
| 173 | $seen_current | ||
| 174 | ? "New requestors in addition to the current: " | ||
| 175 | : "Requestors have been changed: " | ||
| 176 | ) | ||
| 177 | . join( ', ', map $_->format, @new_requestors ), | ||
| 178 | ); | ||
| 179 | $txn_id = $comment_id if $comment_id; | ||
| 180 | } elsif ($dropped_all) { | ||
| 181 | my $comment_id = $self->record_comment( | ||
| 182 | task => $tid, | ||
| 183 | content => "All requestors have been deleted", | ||
| 184 | ); | ||
| 185 | $txn_id = $comment_id if $comment_id; | ||
| 186 | } | ||
| 187 | |||
| 188 | return $txn_id; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub record_comment { | ||
| 192 | my $self = shift; | ||
| 193 | my %args = @_; | ||
| 194 | my $tid = delete $args{'task'}; | ||
| 195 | $args{'creator'} ||= $self->sync_source->user_info->{'email'}; | ||
| 196 | |||
| 197 | my $email = $self->comment_as_email( \%args ); | ||
| 198 | my $status = $self->sync_source->hm->act( | ||
| 199 | 'CreateTaskEmail', | ||
| 200 | task_id => $tid, | ||
| 201 | message => $email->as_string, | ||
| 202 | ); | ||
| 203 | warn "Couldn't add a comment on the recently created HM task" | ||
| 204 | if $self->sync_source->request_failed($status); | ||
| 205 | return $status->{'id'}; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub integrate_attachment { | ||
| 209 | my $self = shift; | ||
| 210 | my ( $change, $changeset ) | ||
| 211 | = validate_pos( @_, { isa => 'Prophet::Change' }, { isa => 'Prophet::ChangeSet' } ); | ||
| 212 | |||
| 213 | unless ( $self->sync_source->user_info->{'pro_account'} ) { | ||
| 214 | warn "Pro account is required to push attachments"; | ||
| 215 | return; | ||
| 216 | } | ||
| 217 | |||
| 218 | my %props = $self->translate_props($change); | ||
| 219 | $props{'content'} = { | ||
| 220 | content => $props{'content'}, | ||
| 221 | filename => delete $props{'name'}, | ||
| 222 | content_type => delete $props{'content_type'}, | ||
| 223 | }; | ||
| 224 | |||
| 225 | my $ticket_id = $self->sync_source->remote_id_for_uuid( delete $props{'ticket'} ) | ||
| 226 | or die "Couldn't get remote id of SD ticket"; | ||
| 227 | |||
| 228 | my $status = $self->sync_source->hm->act( | ||
| 229 | 'CreateTaskAttachment', | ||
| 230 | task_id => $ticket_id, | ||
| 231 | %props, | ||
| 232 | ); | ||
| 233 | return $status->{'id'} unless $self->sync_source->request_failed($status); | ||
| 234 | |||
| 235 | die "Couldn't integrate attachment: " . $self->sync_source->decode_error($status); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub _recode_props_for_create { | ||
| 239 | my $self = shift; | ||
| 240 | my $attr = $self->_recode_props_for_integrate(@_); | ||
| 241 | |||
| 242 | my $source_props = $self->sync_source->props; | ||
| 243 | return $attr unless $source_props; | ||
| 244 | |||
| 245 | my %source_props = %$source_props; | ||
| 246 | for ( grep exists $source_props{$_}, qw(group owner requestor) ) { | ||
| 247 | $source_props{ $_ . '_id' } = delete $source_props{$_}; | ||
| 248 | } | ||
| 249 | |||
| 250 | if ( $source_props{'tag'} ) { | ||
| 251 | if ( defined $attr->{'tags'} && length $attr->{'tags'} ) { | ||
| 252 | $attr->{'tags'} .= ', ' . $source_props{'tag'}; | ||
| 253 | } else { | ||
| 254 | $attr->{'tags'} .= ', ' . $source_props{'tag'}; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | if ( $source_props{'tag_not'} ) { | ||
| 258 | die "TODO: not sure what to do here and with other *_not search arguments"; | ||
| 259 | } | ||
| 260 | |||
| 261 | return { %$attr, %source_props }; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub comment_as_email { | ||
| 265 | my $self = shift; | ||
| 266 | my $props = shift; | ||
| 267 | |||
| 268 | require Email::Simple; | ||
| 269 | require Email::Simple::Creator; | ||
| 270 | |||
| 271 | my $res = Email::Simple->create( | ||
| 272 | header => [ | ||
| 273 | From => $props->{'creator'}, | ||
| 274 | Date => $props->{'created'}, | ||
| 275 | ], | ||
| 276 | body => $props->{'content'}, | ||
| 277 | ); | ||
| 278 | return $res; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _recode_props_for_integrate { | ||
| 282 | my $self = shift; | ||
| 283 | my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } ); | ||
| 284 | |||
| 285 | my %props = $self->translate_props($change); | ||
| 286 | |||
| 287 | my %attr; | ||
| 288 | for my $key ( keys %props ) { | ||
| 289 | $attr{$key} = $props{$key}; | ||
| 290 | } | ||
| 291 | return \%attr; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub translate_props { | ||
| 295 | my $self = shift; | ||
| 296 | my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } ); | ||
| 297 | |||
| 298 | my %PROP_MAP = $self->sync_source->property_map('push'); | ||
| 299 | |||
| 300 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 301 | delete $props{$_} for @{ delete $PROP_MAP{'_delete'} }; | ||
| 302 | while ( my ( $k, $v ) = each %PROP_MAP ) { | ||
| 303 | next unless exists $props{$k}; | ||
| 304 | $props{$v} = delete $props{$k}; | ||
| 305 | } | ||
| 306 | return %props; | ||
| 307 | } | ||
| 308 | |||
| 309 | __PACKAGE__->meta->make_immutable; | ||
| 310 | no Any::Moose; | ||
| 311 | |||
| 312 | 1; |