Skip to content

Commit 6c294ef

Browse files
authored
Merge pull request #71 from ahmadfh/master
fixes to prevent duplicate data when cloning the app or the plugin
2 parents bc73c1a + c51c387 commit 6c294ef

File tree

1 file changed

+77
-48
lines changed

1 file changed

+77
-48
lines changed

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

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
ContentHome.socialAppId;
1515
ContentHome.parentThreadId;
1616
ContentHome.modalPopupThreadId;
17+
ContentHome.originalAppId;
18+
ContentHome.originalInstanceId;
1719
var datastoreWriteKey;
1820
var appId;
1921
var instanceId;
@@ -23,57 +25,72 @@
2325
datastoreWriteKey = context.datastoreWriteKey;
2426
appId = context.appId;
2527
instanceId = context.instanceId;
26-
});
27-
Buildfire.datastore.get('Social', function (err, data) {
28-
console.log('Get data in content section socail App Id------------------', err, data);
29-
if (data && data.data && data.data.socialAppId) {
30-
ContentHome.socialAppId = data.data.socialAppId;
31-
ContentHome.parentThreadId = data.data.parentThreadId;
32-
$scope.$digest();
28+
ContentHome.originalAppId = context.appId;
29+
ContentHome.originalInstanceId = context.instanceId;
3330

34-
//update the plugin name on social
35-
if (ContentHome.socialAppId && ContentHome.parentThreadId) {
36-
Buildfire.getContext(function (err, context) {
37-
if (err) {
38-
console.error("Error occurred while getting buildfire context");
39-
} else {
40-
console.log('buildfire get context response::: ', context);
41-
instanceId = context && context.instanceId;
31+
Buildfire.datastore.get('Social', function (err, data) {
32+
console.log('Get data in content section socail App Id------------------', err, data);
33+
if (data && data.data && data.data.socialAppId && data.data.parentThreadId) {
34+
ContentHome.socialAppId = data.data.socialAppId;
35+
ContentHome.parentThreadId = data.data.parentThreadId;
36+
var _storedAppId = data.data.appId;
37+
var _storedInstanceId = data.data.instanceId;
38+
$scope.$digest();
4239

43-
var updateThreadTitle = function () {
44-
Buildfire.datastore.getWithDynamicData('pluginInfo', function (err, result) {
45-
if (result && result.id) {
46-
pluginTitle = result.data && result.data._buildfire && result.data._buildfire.myDynamicPluginCollection && result.data._buildfire.myDynamicPluginCollection.result && result.data._buildfire.myDynamicPluginCollection.result.length && result.data._buildfire.myDynamicPluginCollection.result[0].data && result.data._buildfire.myDynamicPluginCollection.result[0].data.title;
47-
context.pluginTitle = pluginTitle;
40+
//update the plugin name on social
41+
var updateThreadTitle = function () {
42+
Buildfire.datastore.getWithDynamicData('pluginInfo', function (err, result) {
43+
if (result && result.id) {
44+
pluginTitle = result.data && result.data._buildfire && result.data._buildfire.myDynamicPluginCollection && result.data._buildfire.myDynamicPluginCollection.result && result.data._buildfire.myDynamicPluginCollection.result.length && result.data._buildfire.myDynamicPluginCollection.result[0].data && result.data._buildfire.myDynamicPluginCollection.result[0].data.title;
45+
context.pluginTitle = pluginTitle;
4846

49-
SocialDataStore.getThreadByUniqueLink(ContentHome.socialAppId, context).then(
50-
function (parentThreadRes) {
51-
console.log('Parent ThreadId -------success----', parentThreadRes);
52-
},
53-
function (error) {
54-
console.log('Parent thread callback error------', error);
55-
}
56-
);
47+
SocialDataStore.getThreadByUniqueLink(ContentHome.socialAppId, context).then(
48+
function (parentThreadRes) {
49+
console.log('Parent ThreadId -------success----', parentThreadRes);
50+
},
51+
function (error) {
52+
console.log('Parent thread callback error------', error);
5753
}
58-
});
59-
};
60-
updateThreadTitle();
61-
}
62-
});
63-
}
64-
console.log('Content------------------------social App id, parent id', ContentHome.socialAppId, ContentHome.parentThreadId);
65-
}
66-
else {
67-
Buildfire.getContext(function (err, context) {
68-
if (err) {
69-
console.error("Error occurred while getting buildfire context");
54+
);
55+
}
56+
});
57+
};
58+
59+
/*
60+
* We want the appId to be saved in the datastore to prevent the duplicate data in the plugin when
61+
* we clone the app, so we can compare the original appId with the one from buildfire.getContext
62+
* we also need to compare the instanceId to because we might clone the plugin itself not the app
63+
* and in this case the appId will remain the same but the instanceId will be changed
64+
*/
65+
66+
if (!_storedAppId || !_storedInstanceId) {
67+
Buildfire.datastore.save({
68+
socialAppId: ContentHome.socialAppId,
69+
parentThreadId: ContentHome.parentThreadId,
70+
appId: ContentHome.originalAppId,
71+
instanceId: ContentHome.originalInstanceId
72+
}, 'Social', function (err, data) {
73+
console.log('Data saved using datastore-------------', err, data);
74+
});
75+
updateThreadTitle();
76+
77+
} else if (ContentHome.originalAppId != _storedAppId || ContentHome.originalInstanceId != _storedInstanceId) {
78+
/*
79+
* check if the context appId is not the same as the one in the datastore
80+
* or if the current plugin instance is not the same as old one in myDynamicPluginCollection
81+
* and reset the app
82+
* */
83+
ContentHome.forceResetApp();
84+
7085
} else {
71-
console.log('buildfire get context response::: ', context);
72-
instanceId = context && context.instanceId;
73-
addApplication(context);
86+
updateThreadTitle();
7487
}
75-
});
76-
}
88+
89+
console.log('Content------------------------social App id, parent id', ContentHome.socialAppId, ContentHome.parentThreadId);
90+
}
91+
else {
92+
addApplication(context);
93+
}});
7794
});
7895

7996
function addApplication(context) {
@@ -102,7 +119,9 @@
102119
ContentHome.parentThreadId = parentThreadRes.data.result._id;
103120
Buildfire.datastore.save({
104121
socialAppId: response.data.result,
105-
parentThreadId: parentThreadRes.data.result._id
122+
parentThreadId: parentThreadRes.data.result._id,
123+
appId : ContentHome.originalAppId,
124+
instanceId : ContentHome.originalInstanceId
106125
}, 'Social', function (err, data) {
107126
console.log('Data saved using datastore-------------', err, data);
108127
// Buildfire.messaging.sendMessageToWidget({'name': EVENTS.APP_RESET, 'data': data});
@@ -129,15 +148,15 @@
129148
if (appId && instanceId && datastoreWriteKey) {
130149
var context = {
131150
appId: appId,
132-
instanceId: instanceId + new Date().getTime(),
151+
instanceId: instanceId + new Date().getTime(),
133152
datastoreWriteKey: datastoreWriteKey
134153
};
135154
addApplication(context);
136155
} else {
137156
Buildfire.getContext(function (err, context) {
138157
datastoreWriteKey = context.datastoreWriteKey;
139158
appId = context.appId;
140-
instanceId = context.instanceId + new Date().getTime();
159+
instanceId = context.instanceId + new Date().getTime();
141160
addApplication(context);
142161
});
143162
}
@@ -146,6 +165,16 @@
146165
});
147166
};
148167

168+
ContentHome.forceResetApp = function () {
169+
// resetting the app when the app or plugin is cloned
170+
Buildfire.getContext(function (err, context) {
171+
datastoreWriteKey = context.datastoreWriteKey;
172+
appId = context.appId;
173+
instanceId = context.instanceId;
174+
addApplication(context);
175+
});
176+
};
177+
149178
ContentHome.height = window.innerHeight;
150179
ContentHome.noMore = false;
151180
console.log('inside init method of content controller:::::::: ');

0 commit comments

Comments
 (0)