-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSocialMedia.js
128 lines (98 loc) · 4.35 KB
/
getSocialMedia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//initialize and setup the SDK
window.fbAsyncInit = function() {
FB.init({
appId : '931960383805197', //Production
// appId : '2344879292496049', //Test
autoLogAppEvents : true,
version : 'v4.0',
});
};
function getFacebookData() {
var promise = new Promise(function(resolve, reject) {
let dbUserStructure = {};
let userData={};
//authenticate user
FB.login(ifAuthorized);
//handle user response
function ifAuthorized(response){ //response is {name: "Pia Soy", id: "10156251899951436"}
if (response.authResponse) {
DB.authenticateFB(response)
.then(getUserData)
} else {
reject(
'User cancelled login or did not fully authorize.',
$('#loadingScreenDiv').hide(),
$('#noAuthorizedLogin').modal('show'),
$('#refreshPage').on('click', function() {location.reload()})
);
}
}
//call the Graph API
function getUserData () {
FB.api(
'/me',
'GET',
// {"fields":"id,name,first_name,picture.type(large),likes{name,category}"},
{"fields":"id,name,first_name,picture.type(large)"},
handleUserData
);
}
//handle FB user data
function handleUserData(response) {
userData = response;
// console.log(userData)
getEncodedProfilePic(userData.picture.data.url)
}
//handle FB profile pic data
function getEncodedProfilePic(downloadURL) {
var imageURL = "test"
if (location.hostname === 'localhost') {
downloadURL = 'https://cors-anywhere.herokuapp.com/' + downloadURL
}
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET',downloadURL,true)
xmlHTTP.responseType = 'arraybuffer';
xmlHTTP.onload = function(e) {
var arr = new Uint8Array(this.response)
var raw = String.fromCharCode.apply(null,arr)
var b64 = btoa(raw)
var dataURL='data:image/jpeg;base64,'+b64
// document.getElementById('testImage').src=dataURL;
createDBUserStructure(dataURL);
}
xmlHTTP.send();
}
//create database structure from FB user data
function createDBUserStructure (dataURL) {
// let likeNames =[];
// let likeCategories =[];
// if (userData.likes) {
//loop through userData.likes.data array, push every fb page liked
// userData.likes.data.forEach(function(likeItem){likeNames.push(likeItem.name)});
//loop through userData.likes.data array, push every category of fb page liked
// userData.likes.data.forEach(function(likeItem){likeCategories.push(likeItem.category)});
//remove duplicate categories
// }
// let filteredLikeCategories = likeCategories.filter(function (item, index){
// return likeCategories.indexOf(item) === index;
// })
//create structure object
dbUserStructure["id"] = userData.id;
dbUserStructure["name"] = userData.first_name;
dbUserStructure["dataURL"] = dataURL
// console.log('likeNames: ', likeNames)
// if (likeNames.length) {
// dbUserStructure["likes"] = likeNames
// }
// if (filteredLikeCategories.length) {
// dbUserStructure["categories"] = filteredLikeCategories;
// }
resolve(dbUserStructure)
}
});
return promise
// //emulate next function
// promise.then(function(dbValue) {
// console.log("dbValue", dbValue);
// });
}