Skip to content

Commit 9df7910

Browse files
committed
introducing dirty state of the event & video based on comparison to avoid update metadata call in opencast
1 parent 6cbb41e commit 9df7910

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

lib/Models/Videos.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,11 @@ public function haveCoursePerm(string $course_perm, string $user_id = null)
719719
* Updates the metadata related to this video in both opencast and local and runs republish-metadata workflow
720720
*
721721
* @param object $event the updated version of the event
722+
* @param boolean $check_dirty_state whether to check for dirty state, meaning if any of the fields that need the opencast update has changed!
722723
*
723724
* @return boolean the result of updating process
724725
*/
725-
public function updateMetadata($event)
726+
public function updateMetadata($event, $check_dirty_state = true)
726727
{
727728
$api_event_client = ApiEventsClient::getInstance($this->config_id);
728729

@@ -737,21 +738,34 @@ public function updateMetadata($event)
737738
'subject', 'language', 'description', 'startDate'];
738739
$metadata = [];
739740

741+
// A dirty state flag to check if any of the fields that need the opencast update has changed!
742+
$is_dirty = false;
743+
740744
foreach ($allowed_metadata_fields as $field_name) {
741745
if (isset($event[$field_name])) {
746+
$current_value = $this->getValue($field_name) ?? null;
742747
$value = $event[$field_name];
743748
$id = $field_name;
744749
if ($field_name == 'subject') {
745750
$id = 'subjects';
746751
$value = [$value];
752+
$current_value = [$current_value];
747753
}
748754
if ($field_name == 'presenters') {
749755
$id = 'creator';
750-
$value = array_map('trim', explode(',', $value));
756+
$value = array_filter(array_map('trim', explode(',', $value)));
757+
$current_value = array_filter(array_map('trim', explode(',', $current_value)));
751758
}
752759
if ($field_name == 'contributors') {
753760
$id = 'contributor';
754-
$value = array_map('trim', explode(',', $value));
761+
$value = array_filter(array_map('trim', explode(',', $value)));
762+
$current_value = array_filter(array_map('trim', explode(',', $current_value)));
763+
}
764+
765+
// If it is not yet marked dirty, check for dirty state!
766+
if (!$is_dirty) {
767+
// Assuming that the current value is not the same as the requested value!
768+
$is_dirty = !(json_encode($current_value) === json_encode($value));
755769
}
756770

757771
$metadata[] = [
@@ -761,6 +775,11 @@ public function updateMetadata($event)
761775
}
762776
}
763777

778+
if ($check_dirty_state && !$is_dirty) {
779+
// nothing changed, no need to update!
780+
return true;
781+
}
782+
764783
$success = false;
765784
$response = $api_event_client->updateMetadata($this->episode, $metadata);
766785
$republish = in_array($response['code'], [200, 204]) === true;

lib/Routes/Video/VideoUpdate.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,6 @@ public function __invoke(Request $request, Response $response, $args)
9191
'text' => _('Das Video wurde erfolgreich aktualisiert.')
9292
];
9393

94-
// If changes are only related to local event properties, we don't need to update the metadata on the Opencast server.
95-
if (isset($event['has_metadata_changes']) && $event['has_metadata_changes'] === false) {
96-
return $this->createResponse([
97-
'message' => $message,
98-
], $response->withStatus(200));
99-
}
100-
10194
$update = $video->updateMetadata($event);
10295

10396
if ($update !== true) {

vueapp/components/Videos/Actions/VideoEdit.vue

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ export default {
139139
visibility: null,
140140
visible_timestamp: null,
141141
use_timestamp: false,
142-
originalEvent: null
143142
}
144143
},
145144
@@ -182,24 +181,6 @@ export default {
182181
};
183182
}
184183
185-
// Decide whether the actual metadata has been changed or not, in order to check if the update metadata against opencast server is needed.
186-
let has_metadata_changes = false;
187-
let non_metadata_fields = ['tags', 'seminar_visibility', 'cid', 'playlist_token']; // These fields are not part of the metadata.
188-
for (const field in this.event) {
189-
if (non_metadata_fields.includes(field)) {
190-
continue;
191-
}
192-
193-
let event_value = JSON.stringify(this.event[field]);
194-
let original_event_value = JSON.stringify(this.originalEvent[field]);
195-
if (event_value !== original_event_value) {
196-
has_metadata_changes = true;
197-
break;
198-
}
199-
}
200-
201-
this.event.has_metadata_changes = has_metadata_changes;
202-
203184
await this.$store.dispatch('updateVideo', this.event)
204185
.then(({ data }) => {
205186
this.$store.dispatch('addMessage', data.message);
@@ -257,8 +238,6 @@ export default {
257238
this.visibility = this.event.seminar_visibility?.visibility;
258239
this.visible_timestamp = this.event.seminar_visibility?.visible_timestamp;
259240
this.checkVisibility();
260-
// Make sure originalEvent is a non-reactive copy of the event.
261-
this.originalEvent = JSON.parse(JSON.stringify(this.event));
262241
}
263242
}
264243
</script>

0 commit comments

Comments
 (0)