This repository was archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcontact_photo_helper.js
71 lines (59 loc) · 1.9 KB
/
contact_photo_helper.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
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
(function(exports) {
'use strict';
function getThumbnail(contact) {
return getOnePhoto(contact, 'begin');
}
function getFullResolution(contact) {
return getOnePhoto(contact, 'end');
}
function getOnePhoto(contact, position) {
if (!contact || !contact.photo || !contact.photo.length) {
return null;
}
if (contact.photo.length === 1) {
return contact.photo[0];
}
// For FB Linked contacts we need to give preference to the local photo
// [0] is the full resolution photo and [1] is the thumbnail
var photos = contact.photo;
var category = contact.category;
if (Array.isArray(category) && category.indexOf('fb_linked') !== -1) {
// Check whether we have a new linked contact or a legacy contact
if (photos.length >= 4) {
return photos[(position == 'begin') ? 1 : 0];
}
// In the case of a legacy contact we always return full resolution
// in order to ensure we are always giving preference to local photo
return photos[0];
}
photos = photosBySize(contact);
var index = (position == 'begin') ? 0 : photos.length - 1;
return photos[index];
}
function photosBySize(contact) {
var photos = contact.photo.slice(0);
photos.sort(function(p1, p2) {
if (size(p1) < size(p2)) {
return -1;
}
if (size(p1) > size(p2)) {
return 1;
}
return 0;
});
return photos;
}
// For legacy purpose we support data URLs and blobs
function size(photo) {
if (typeof photo == 'string') {
return photo.length; // length of the data URL
}
return photo.size; // size of the blob in bytes
}
exports.ContactPhotoHelper = {
getThumbnail: getThumbnail,
getFullResolution: getFullResolution
};
})(window);