| 1 | package App::SD::Replica::hm::PullEncoder; | ||
| 2 | use Any::Moose; | ||
| 3 | extends 'App::SD::ForeignReplica::PullEncoder'; | ||
| 4 | use Params::Validate qw(:all); | ||
| 5 | use Memoize; | ||
| 6 | |||
| 7 | has sync_source => ( | ||
| 8 | isa => 'App::SD::Replica::hm', | ||
| 9 | is => 'rw', | ||
| 10 | ); | ||
| 11 | |||
| 12 | sub ticket_id { | ||
| 13 | my $self = shift; | ||
| 14 | my $ticket = shift; | ||
| 15 | return $ticket->{id}; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub ticket_last_modified { | ||
| 19 | my $self = shift; | ||
| 20 | my $ticket = shift; | ||
| 21 | return App::SD::Util::string_to_datetime( $ticket->{modified_at} ); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub transcode_one_txn { | ||
| 25 | my ( $self, $txn_wrapper, $previous_state, $ticket_final ) = (@_); | ||
| 26 | |||
| 27 | my $txn = $txn_wrapper->{object}; | ||
| 28 | |||
| 29 | my @changesets; | ||
| 30 | |||
| 31 | my $changeset = Prophet::ChangeSet->new( | ||
| 32 | { original_source_uuid => $self->sync_source->uuid, | ||
| 33 | original_sequence_no => $txn->{'id'}, | ||
| 34 | creator => $self->sync_source->user_info( id => $txn->{'created_by'} )->{'email'}, | ||
| 35 | } | ||
| 36 | ); | ||
| 37 | |||
| 38 | my $method = 'recode_' . $txn->{type}; | ||
| 39 | unless ( $self->can($method) ) { | ||
| 40 | die "Unknown change type $txn->{type}."; | ||
| 41 | } | ||
| 42 | |||
| 43 | my $change = $self->$method( task => $ticket_final, transaction => $txn ); | ||
| 44 | |||
| 45 | $changeset->add_change( { change => $change } ); | ||
| 46 | |||
| 47 | # XXX is this tested at all? this method doesn't exist | ||
| 48 | for my $email ( @{ $txn->{email_entries} } ) { | ||
| 49 | if ( my $sub = $self->can( '_recode_email_' . 'blah' ) ) { | ||
| 50 | $sub->( | ||
| 51 | $self, | ||
| 52 | previous_state => $previous_state, | ||
| 53 | email => $email, | ||
| 54 | txn => $txn, | ||
| 55 | changeset => $changeset, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | $self->translate_props($changeset); | ||
| 61 | return $changeset; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub find_matching_tickets { | ||
| 65 | my $self = shift; | ||
| 66 | my %args = (); | ||
| 67 | |||
| 68 | if ( my $props = $self->sync_source->props ) { | ||
| 69 | while ( my ( $k, $v ) = each %$props ) { $args{$k} = $v } | ||
| 70 | } | ||
| 71 | |||
| 72 | unless ( keys %args ) { | ||
| 73 | %args = ( | ||
| 74 | owner => 'me', | ||
| 75 | group => 0, | ||
| 76 | requestor => 'me', | ||
| 77 | not_complete => 1, | ||
| 78 | ); | ||
| 79 | } | ||
| 80 | |||
| 81 | my $status = $self->sync_source->hm->act( 'TaskSearch', %args ); | ||
| 82 | unless ( $status->{'success'} ) { | ||
| 83 | die "couldn't search"; | ||
| 84 | } | ||
| 85 | return $status->{content}{tasks}; | ||
| 86 | } | ||
| 87 | |||
| 88 | # hiveminder transaction ~= prophet changeset | ||
| 89 | # hiveminder taskhistory ~= prophet change | ||
| 90 | # hiveminder taskemail ~= prophet change | ||
| 91 | # | ||
| 92 | sub find_matching_transactions { | ||
| 93 | my $self = shift; | ||
| 94 | my %args = validate( @_, { ticket => 1, starting_transaction => 1 } ); | ||
| 95 | |||
| 96 | my $txns = $self->sync_source->hm->search( 'TaskTransaction', task_id => $args{ticket}->{id} ) | ||
| 97 | || []; | ||
| 98 | my @matched; | ||
| 99 | for my $txn (@$txns) { | ||
| 100 | |||
| 101 | # Skip things we know we don't want | ||
| 102 | next if $txn->{'id'} < $args{'starting_transaction'}; | ||
| 103 | |||
| 104 | # Skip things we've pushed | ||
| 105 | next if $self->sync_source->foreign_transaction_originated_locally( $txn->{'id'}, $args{ticket}->{id} ); | ||
| 106 | |||
| 107 | $txn->{history_entries} | ||
| 108 | = $self->sync_source->hm->search( 'TaskHistory', transaction_id => $txn->{'id'} ); | ||
| 109 | $txn->{email_entries} | ||
| 110 | = $self->sync_source->hm->search( 'TaskEmail', transaction_id => $txn->{'id'} ); | ||
| 111 | push @matched, | ||
| 112 | { | ||
| 113 | timestamp => App::SD::Util::string_to_datetime( $txn->{modified_at} ), | ||
| 114 | serial => $txn->{id}, | ||
| 115 | object => $txn | ||
| 116 | }; | ||
| 117 | } | ||
| 118 | return \@matched; | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | sub add_prop_change { | ||
| 123 | my $self = shift; | ||
| 124 | my %args = validate( @_, { history_entry => 1, previous_state => 1, change => 1 } ); | ||
| 125 | |||
| 126 | no warnings 'uninitialized'; | ||
| 127 | my $field = qq{$args{'history_entry'}{'field'}} ||''; | ||
| 128 | my $old = qq{$args{'history_entry'}{'old_value'}} ||''; | ||
| 129 | my $new = qq{$args{'history_entry'}{'new_value'}} ||''; | ||
| 130 | |||
| 131 | if ( $args{'previous_state'}->{$field} eq $new ) { | ||
| 132 | $args{'previous_state'}->{$field} = $old; | ||
| 133 | } else { | ||
| 134 | $args{'previous_state'}->{$field} = $old; | ||
| 135 | warn "$field: ". $args{'previous_state'}->{$field} . " != " . $new . "\n\n"; | ||
| 136 | } | ||
| 137 | |||
| 138 | $args{change}->add_prop_change( name => $field, old => $old, new => $new ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub recode_create { | ||
| 142 | my $self = shift; | ||
| 143 | my %args = validate( @_, { task => 1, transaction => 1 } ); | ||
| 144 | |||
| 145 | my $source = $self->sync_source; | ||
| 146 | my $res = Prophet::Change->new( | ||
| 147 | { record_type => 'ticket', | ||
| 148 | record_uuid => $source->uuid_for_remote_id( $args{'task'}->{'id'} ), | ||
| 149 | change_type => 'add_file' | ||
| 150 | } | ||
| 151 | ); | ||
| 152 | |||
| 153 | $args{'task'}{ $source->uuid . '-' . $_ } = delete $args{'task'}->{$_} | ||
| 154 | for qw(id record_locator); | ||
| 155 | |||
| 156 | while ( my ( $k, $v ) = each %{ $args{'task'} } ) { | ||
| 157 | $res->add_prop_change( name => $k, old => undef, new => $v ); | ||
| 158 | } | ||
| 159 | |||
| 160 | return $res; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub recode_update { | ||
| 164 | my $self = shift; | ||
| 165 | my %args = validate( @_, { task => 1, transaction => 1 } ); | ||
| 166 | |||
| 167 | # In Hiveminder, a changeset has only one change | ||
| 168 | my $res = Prophet::Change->new( | ||
| 169 | { record_type => 'ticket', | ||
| 170 | record_uuid => $self->sync_source->uuid_for_remote_id( $args{'task'}->{'id'} ), | ||
| 171 | change_type => 'update_file' | ||
| 172 | } | ||
| 173 | ); | ||
| 174 | |||
| 175 | for my $entry ( @{ $args{'transaction'}->{'history_entries'} } ) { | ||
| 176 | $self->add_prop_change( | ||
| 177 | change => $res, | ||
| 178 | history_entry => $entry, | ||
| 179 | previous_state => $args{'task'}, | ||
| 180 | ); | ||
| 181 | } | ||
| 182 | return $res; | ||
| 183 | } | ||
| 184 | |||
| 185 | # This is a comment, basically. | ||
| 186 | sub recode_email { | ||
| 187 | my $self = shift; | ||
| 188 | my %args = validate( @_, { task => 1, transaction => 1 } ); | ||
| 189 | |||
| 190 | # I *think* we should only ever have one email entry at a time, but let's | ||
| 191 | # check to make sure | ||
| 192 | if ( scalar @{$args{'transaction'}->{'email_entries'}} > 1 ) { | ||
| 193 | use Data::Dumper; | ||
| 194 | die "more than one entry in email_entries:\n" | ||
| 195 | . Dumper($args{'transaction'}->{'email_entries'}); | ||
| 196 | } | ||
| 197 | |||
| 198 | my $ticket_uuid = $self->sync_source->uuid_for_remote_id( $args{'transaction'}->{'task_id'} ); | ||
| 199 | |||
| 200 | my $comment = Prophet::Change->new( | ||
| 201 | { record_type => 'comment', | ||
| 202 | record_uuid => $self->sync_source->uuid_for_remote_id( | ||
| 203 | $args{'transaction'}->{'id'} ), | ||
| 204 | change_type => 'add_file', | ||
| 205 | } | ||
| 206 | ); | ||
| 207 | # we're assuming non-multipart messages... | ||
| 208 | use Email::MIME; | ||
| 209 | my $parsed = Email::MIME->new( | ||
| 210 | $args{'transaction'}->{'email_entries'}->[0]->{'message'} ); | ||
| 211 | my $content = $parsed->body_str(); | ||
| 212 | # we ignore the headers on the email since we can get sender from elsewhere | ||
| 213 | |||
| 214 | $comment->add_prop_change( | ||
| 215 | name => 'content', | ||
| 216 | new => $content, | ||
| 217 | ); | ||
| 218 | |||
| 219 | $comment->add_prop_change( | ||
| 220 | name => 'created', | ||
| 221 | new => $args{'txn'}->{'modified_at'}, # already UTC and correct format | ||
| 222 | ); | ||
| 223 | |||
| 224 | $comment->add_prop_change( name => 'ticket', new => $ticket_uuid ); | ||
| 225 | |||
| 226 | # XXX do we always want to be using text/plain? | ||
| 227 | $comment->add_prop_change( | ||
| 228 | name => 'content_type', | ||
| 229 | new => 'text/plain', | ||
| 230 | ); | ||
| 231 | |||
| 232 | return $comment; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub translate_props { | ||
| 236 | my $self = shift; | ||
| 237 | my $changeset = shift; | ||
| 238 | |||
| 239 | my %PROP_MAP = $self->sync_source->property_map('pull'); | ||
| 240 | |||
| 241 | for my $change ( $changeset->changes ) { | ||
| 242 | next unless $change->record_type eq 'ticket'; | ||
| 243 | my @new_props; | ||
| 244 | for my $prop ( $change->prop_changes ) { | ||
| 245 | $prop->name( $PROP_MAP{ lc( $prop->name ) } ) if $PROP_MAP{ lc( $prop->name ) }; | ||
| 246 | next if ( $prop->name eq '_delete' ); | ||
| 247 | |||
| 248 | if ( $prop->name =~ /^(?:reporter|owner|next_action_by)$/ ) { | ||
| 249 | $prop->old_value( $self->sync_source->user_info( id => $prop->old_value )->{'email'} ) if ($prop->old_value); | ||
| 250 | $prop->new_value( $self->sync_source->user_info( id => $prop->new_value )->{'email'} ) if ($prop->new_value); | ||
| 251 | } | ||
| 252 | |||
| 253 | if ($prop->name =~ /^(?:due|completed_at|created_at)$/) { | ||
| 254 | no warnings 'uninitialized'; | ||
| 255 | $prop->old_value(App::SD::Util::string_to_datetime($prop->old_value).""); | ||
| 256 | $prop->new_value(App::SD::Util::string_to_datetime($prop->new_value).""); | ||
| 257 | |||
| 258 | } | ||
| 259 | |||
| 260 | # XXX, TODO, FIXME: this doesn't work any more as id stored as SOURCE_UUID-id property | ||
| 261 | if ( $prop->name eq 'id' ) { | ||
| 262 | $prop->old_value( $prop->old_value . '@' . $changeset->original_source_uuid ) | ||
| 263 | if ( $prop->old_value || '' ) =~ /^\d+$/; | ||
| 264 | $prop->old_value( $prop->new_value . '@' . $changeset->original_source_uuid ) | ||
| 265 | if ( $prop->new_value || '' ) =~ /^\d+$/; | ||
| 266 | |||
| 267 | } | ||
| 268 | push @new_props, $prop; | ||
| 269 | |||
| 270 | } | ||
| 271 | $change->prop_changes( \@new_props ); | ||
| 272 | |||
| 273 | } | ||
| 274 | return $changeset; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub translate_ticket_state { | ||
| 278 | my $self = shift; | ||
| 279 | |||
| 280 | my $props = shift; | ||
| 281 | |||
| 282 | my $translated = {%$props}; | ||
| 283 | $translated->{status} = (delete $translated->{complete}) ? 'closed' : 'open'; | ||
| 284 | |||
| 285 | return $props, $translated; | ||
| 286 | |||
| 287 | } | ||
| 288 | __PACKAGE__->meta->make_immutable; | ||
| 289 | no Any::Moose; | ||
| 290 | 1; |