Skip to content

Commit b83ce93

Browse files
authored
Merge pull request #173 from BuildFire/feat/update-single-video
Feat/update single video - PLUG-2315
2 parents 191c631 + 2da313e commit b83ce93

6 files changed

Lines changed: 175 additions & 23 deletions

File tree

control/content/app.services.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@
4141
}
4242
};
4343
}])
44+
.factory('YoutubeApi', ['$q', '$http', 'STATUS_CODE', 'STATUS_MESSAGES', 'PROXY_SERVER',
45+
function ($q, $http, STATUS_CODE, STATUS_MESSAGES, PROXY_SERVER) {
46+
var getProxyServerUrl = function () {
47+
return PROXY_SERVER.serverUrl;
48+
};
49+
var getSingleVideoDetails = function (videoId) {
50+
var deferred = $q.defer();
51+
if (!videoId) {
52+
deferred.reject(new Error({
53+
code: STATUS_CODE.UNDEFINED_VIDEO_ID,
54+
message: STATUS_MESSAGES.UNDEFINED_VIDEO_ID
55+
}));
56+
} else {
57+
$http.post(getProxyServerUrl() + '/video', {
58+
id: videoId
59+
})
60+
.success(function (response) {
61+
if (response.statusCode == 200)
62+
deferred.resolve(response.video);
63+
else
64+
deferred.resolve(null);
65+
})
66+
.error(function (error) {
67+
deferred.reject(error);
68+
});
69+
}
70+
return deferred.promise;
71+
};
72+
73+
return {
74+
getSingleVideoDetails: getSingleVideoDetails,
75+
};
76+
}])
4477
.factory("Utils", ["$http", "YOUTUBE_KEYS", function ($http, YOUTUBE_KEYS) {
4578
return {
4679
extractSingleVideoId: function (url) {

control/content/controllers/content.home.controller.js

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"LAYOUTS",
1717
"$rootScope",
1818
"PROXY_SERVER",
19+
"YoutubeApi",
1920
function(
2021
$scope,
2122
Buildfire,
@@ -30,7 +31,8 @@
3031
$timeout,
3132
LAYOUTS,
3233
$rootScope,
33-
PROXY_SERVER
34+
PROXY_SERVER,
35+
YoutubeApi
3436
) {
3537
var _data = {
3638
content: {
@@ -123,6 +125,66 @@
123125
return angular.equals(data, ContentHome.masterData);
124126
}
125127

128+
// bind the video data to the scope so it can be used in the search engine
129+
function setActiveVideoData(options) {
130+
const { id, title, description, keywords, imageUrl } = options;
131+
ContentHome.activeVideo = {
132+
id,
133+
title,
134+
description,
135+
keywords,
136+
imageUrl,
137+
data: {
138+
feed: {
139+
id,
140+
title,
141+
description,
142+
keywords,
143+
image_url: imageUrl,
144+
}
145+
}
146+
}
147+
}
148+
149+
ContentHome.updateSingleVideo = () => {
150+
$scope.refreshButtonsLoading = true;
151+
YoutubeApi.getSingleVideoDetails(ContentHome.data.content.videoID)
152+
.then((res) => {
153+
const videoOptions = {
154+
id: ContentHome.data.content.videoID,
155+
title: res.snippet.title,
156+
description: res.snippet.description,
157+
keywords: res.snippet.tags ? res.snippet.tags.join(',') : "",
158+
imageUrl: res.snippet.thumbnails.default.url,
159+
}
160+
setActiveVideoData(videoOptions);
161+
ContentHome.indexFeed((err) => {
162+
$scope.refreshButtonsLoading = false;
163+
if (!$scope.$$phase) $scope.$digest();
164+
if (err) {
165+
buildfire.dialog.toast({
166+
message: "Something went wrong, please try again.",
167+
type: "danger",
168+
});
169+
return console.error(err);
170+
} else {
171+
buildfire.dialog.toast({
172+
message: "Successfully updated global search.",
173+
type: "success",
174+
});
175+
}
176+
});
177+
}).catch((err) => {
178+
$scope.refreshButtonsLoading = false;
179+
if (!$scope.$$phase) $scope.$digest();
180+
console.error(err);
181+
buildfire.dialog.toast({
182+
message: "Something went wrong, please try again.",
183+
type: "danger",
184+
});
185+
});
186+
}
187+
126188
/*
127189
* Go pull any previously saved data
128190
* */
@@ -176,7 +238,7 @@
176238
ContentHome.handleLoaderDialog();
177239
}
178240
var success = function(result) {
179-
$scope.loading = false;
241+
$scope.refreshButtonsLoading = false;
180242
if (newObj.content.rssUrl && ContentHome.masterData.content.rssUrl !== newObj.content.rssUrl) {
181243
ContentHome.handleLoaderDialog("Indexing Data", "Indexing data for search results, please wait...", true);
182244
ContentHome.indexFeed((err) => {
@@ -196,7 +258,7 @@
196258
}
197259
},
198260
error = function(err) {
199-
$scope.loading = false;
261+
$scope.refreshButtonsLoading = false;
200262
ContentHome.handleLoaderDialog();
201263
console.error("Error while saving data : ", err);
202264
};
@@ -315,12 +377,14 @@
315377
return console.error(err);
316378
}
317379

318-
ContentHome.activeVideo = {
319-
...response.items[0].snippet,
380+
const videoOptions = {
320381
id: response.items[0].id,
382+
title: response.items[0].snippet.title,
383+
description: response.items[0].snippet.description,
321384
keywords: response.items[0].snippet.tags ? response.items[0].snippet.tags.join(',') : "",
322-
imageUrl: response.items[0].snippet.thumbnails.medium.url,
385+
imageUrl: response.items[0].snippet.thumbnails.default.url,
323386
}
387+
setActiveVideoData(videoOptions);
324388

325389
ContentHome.data.content.rssUrl = ContentHome.rssLink;
326390
ContentHome.data.content.type = ContentHome.contentType;
@@ -575,7 +639,7 @@
575639
}
576640

577641
ContentHome.updateCachedVideos = function() {
578-
$scope.loading = true;
642+
$scope.refreshButtonsLoading = true;
579643
ContentHome.data.content.videoThumbnailVersion = Date.now();
580644
}
581645

control/content/searchEngine.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var searchEngine = {
4343
description: video.description,
4444
keywords: video.keywords,
4545
imageUrl: video.imageUrl,
46+
data: video.data ? video.data : {}
4647
}, callback);
4748
},
4849
deleteSingleVideo(videoID, callback) {

control/content/templates/home.html

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</div>
2525
<div class="col-md-3 pull-right">
2626
<button class="btn btn-success pull-right stretch"
27-
ng-disabled="loading"
27+
ng-disabled="refreshButtonsLoading"
2828
ng-click="ContentHome.validateRssLink()">
2929
Save
3030
</button>
@@ -75,6 +75,26 @@
7575

7676
</div>
7777
</div>
78+
<div class="item clearfix row margin-bottom-fifteen" ng-show="(ContentHome.contentType === ContentHome.CONTENT_TYPE.SINGLE_VIDEO)">
79+
<div class="labels col-md-4 padding-right-zero pull-left">
80+
<span class="tooltip-container">
81+
Update Global Search
82+
<span class="btn-info-icon btn-primary">
83+
<span class="cp-tooltip">
84+
Refresh video details in the global search result if you've updated it on your YouTube channel.
85+
</span>
86+
</span>
87+
</span>
88+
</div>
89+
<div class="main pull-right col-md-8 padding-left-zero row">
90+
<div class="col-md-3 pull-right">
91+
<button class="stretch btn btn-primary btn-outlined pull-right"
92+
ng-disabled="refreshButtonsLoading"
93+
ng-click="ContentHome.updateSingleVideo()"> Refresh Now
94+
</button>
95+
</div>
96+
</div>
97+
</div>
7898
<div class="item clearfix row margin-bottom-fifteen" ng-show="(ContentHome.contentType == ContentHome.CONTENT_TYPE.CHANNEL_FEED) || (ContentHome.contentType == ContentHome.CONTENT_TYPE.PLAYLIST_FEED)">
7999
<div class="labels col-md-4 padding-right-zero pull-left">
80100
<span class="tooltip-container">
@@ -89,7 +109,7 @@
89109
<div class="main pull-right col-md-8 padding-left-zero row">
90110
<div class="col-md-3 pull-right">
91111
<button class="stretch btn btn-primary btn-outlined pull-right"
92-
ng-disabled="loading"
112+
ng-disabled="refreshButtonsLoading"
93113
ng-click="ContentHome.updateCachedVideos()"> Refresh Now
94114
</button>
95115
</div>

widget/controllers/widget.feed.controller.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
var compareDataFromCacheAndYouTube = function(result){
5656
var isUnchanged=false;
5757
if(cache.items && result.items && result.items.length){
58+
result.items = result.items.filter(item => (
59+
item.snippet &&
60+
item.snippet.thumbnails &&
61+
Object.keys(item.snippet.thumbnails).length));
62+
5863
if(cache.items.length == result.items.length){
5964
var flag=false;
6065
for (let i = 0; i < cache.items.length; i++) {
@@ -96,6 +101,9 @@
96101
*/
97102
var initData = function(isRefresh) {
98103
var success = function(result) {
104+
if ($scope.deeplinkData && !$scope.isDeeplinkItemOpened) {
105+
processDeeplink($scope.deeplinkData, false);
106+
}
99107
cache.getCache(function(err, data) {
100108
// if the rss feed url has changed, ignore the cache and update when fetched. Also, if forcedCleanupv2 is false, it will skip cache and proceed with fetching.
101109
if (err || !data || data.rssUrl != result.data.content.rssUrl || !data.forcedCleanupv2) {
@@ -104,7 +112,7 @@
104112
getFeedVideosSuccess(data);
105113
checkForNewDataFromYouTube(data);
106114
}
107-
if ($scope.deeplinkData) {
115+
if ($scope.deeplinkData && !$scope.isDeeplinkItemOpened) {
108116
processDeeplink($scope.deeplinkData, false);
109117
}
110118
if (!$rootScope.$$phase) $rootScope.$digest();
@@ -190,22 +198,41 @@
190198
$scope.isDeeplinkItemOpened = false;
191199
// show the deeplink skeleton if the deeplink is present
192200
buildfire.deeplink.getData(function (data) {
193-
if (data && data.link) {
201+
if (data && (data.link || data.feed)) {
194202
$rootScope.showFeed = false;
195203
toggleDeeplinkSkeleton(true);
196204
$scope.deeplinkData = data;
197205
}
198206
});
199207
buildfire.deeplink.onUpdate(function (data) {
200-
if (data && data.link) {
208+
if (data && (data.link || data.feed)) {
201209
$scope.deeplinkData = data;
202210
toggleDeeplinkSkeleton(true);
203211
processDeeplink(data, true);
204212
}
205213
});
206214

207215
function processDeeplink (data, pushToHistory=true) {
208-
if (data && data.link) {
216+
if (data && data.feed) {
217+
let id = data.feed.id;
218+
if (id.indexOf("yt:video") > -1) {
219+
id = id.slice(id.lastIndexOf(":") + 1, id.length);
220+
}
221+
if (data.timeIndex) video.seekTo = data.timeIndex;
222+
$scope.isDeeplinkItemOpened = true;
223+
WidgetFeed.openDetailsPage({
224+
...data.feed, id,
225+
snippet: {
226+
title: data.feed.title,
227+
description:data.feed.description,
228+
thumbnails: {
229+
default: {
230+
url: data.feed.image_url
231+
}
232+
}
233+
},
234+
}, pushToHistory);
235+
} else if (data && data.link) {
209236
let linkD = data.link;
210237
if (linkD.indexOf("yt:video") > -1) {
211238
linkD = linkD.slice(linkD.lastIndexOf(":") + 1, linkD.length);
@@ -255,6 +282,11 @@
255282
var getFeedVideosSuccess = function(result) {
256283
// double check that result is not null
257284
if (result && result.items && result.items.length) {
285+
result.items = result.items.filter(item => (
286+
item.snippet &&
287+
item.snippet.thumbnails &&
288+
Object.keys(item.snippet.thumbnails).length));
289+
258290
$rootScope.showEmptyState = false;
259291
} else {
260292
$rootScope.showEmptyState = true;
@@ -457,7 +489,7 @@
457489
if (WidgetFeed.screenAnimationInProgress) return;
458490
WidgetFeed.screenAnimationInProgress = true;
459491

460-
if (video.snippet.resourceId && video.snippet.resourceId.videoId) {
492+
if (video.snippet && video.snippet.resourceId && video.snippet.resourceId.videoId) {
461493
video.id = video.snippet.resourceId.videoId;
462494
}
463495
VideoCache.setCache({...video, pushToHistory});
@@ -473,7 +505,7 @@
473505
$rootScope.loading = false;
474506
WidgetFeed.screenAnimationInProgress = false;
475507
viewedVideos.markViewed($scope, video);
476-
const videoId = (video.snippet.resourceId && video.snippet.resourceId.videoId) ? video.snippet.resourceId.videoId : video.id;
508+
const videoId = (video.snippet && video.snippet.resourceId && video.snippet.resourceId.videoId) ? video.snippet.resourceId.videoId : video.id;
477509
Location.goTo("#/video/" + videoId);
478510

479511
setTimeout(() => {

widget/controllers/widget.single.controller.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@
3535
WidgetSingle.data = null;
3636
WidgetSingle.video = null;
3737
WidgetSingle.viewSource = function(WidgetSingle) {
38-
if (
39-
WidgetSingle &&
40-
WidgetSingle.video &&
41-
WidgetSingle.video.snippet &&
42-
WidgetSingle.video.snippet.resourceId &&
43-
WidgetSingle.video.snippet.resourceId.videoId
44-
) {
45-
buildfire.navigation.openWindow(`https://www.youtube.com/watch?v=${WidgetSingle.video.snippet.resourceId.videoId}`, '_system');
38+
if (WidgetSingle && WidgetSingle.video) {
39+
if (
40+
WidgetSingle.video.snippet &&
41+
WidgetSingle.video.snippet.resourceId &&
42+
WidgetSingle.video.snippet.resourceId.videoId
43+
) {
44+
buildfire.navigation.openWindow(`https://www.youtube.com/watch?v=${WidgetSingle.video.snippet.resourceId.videoId}`, '_system');
45+
} else if (WidgetSingle.video.id) {
46+
buildfire.navigation.openWindow(`https://www.youtube.com/watch?v=${WidgetSingle.video.id}`, '_system');
47+
}
4648
}
4749
};
4850

0 commit comments

Comments
 (0)