get text attachments directly instead of via the api

This commit is contained in:
Hannes Mannerheim 2015-09-21 00:58:11 +02:00
parent d35368039c
commit dcd4f55c69
2 changed files with 30 additions and 24 deletions

View File

@ -453,17 +453,22 @@ class QvitterPlugin extends Plugin {
$twitter_status['repeated_id'] = $repeated->id; $twitter_status['repeated_id'] = $repeated->id;
} }
// thumb urls // more metadata about attachments
// find all thumbs // get all attachments first, and put all the extra meta data in an array
$attachments = $notice->attachments(); $attachments = $notice->attachments();
$attachment_url_to_id = array(); $attachment_url_to_id = array();
if (!empty($attachments)) { if (!empty($attachments)) {
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if(is_object($attachment)) { if(is_object($attachment)) {
try { try {
$enclosure_o = $attachment->getEnclosure(); $enclosure_o = $attachment->getEnclosure();
$thumb = $attachment->getThumbnail();
// add id to all attachments
$attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
// add data about thumbnails
$thumb = $attachment->getThumbnail();
$large_thumb = $attachment->getThumbnail(1000,3000,false); $large_thumb = $attachment->getThumbnail(1000,3000,false);
if(method_exists('File_thumbnail','url')) { if(method_exists('File_thumbnail','url')) {
$thumb_url = File_thumbnail::url($thumb->filename); $thumb_url = File_thumbnail::url($thumb->filename);
@ -472,7 +477,6 @@ class QvitterPlugin extends Plugin {
$thumb_url = $thumb->getUrl(); $thumb_url = $thumb->getUrl();
$large_thumb_url = $large_thumb->getUrl(); $large_thumb_url = $large_thumb->getUrl();
} }
$attachment_url_to_id[$enclosure_o->url]['id'] = $attachment->id;
$attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url; $attachment_url_to_id[$enclosure_o->url]['thumb_url'] = $thumb_url;
$attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $large_thumb_url; $attachment_url_to_id[$enclosure_o->url]['large_thumb_url'] = $large_thumb_url;
$attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width; $attachment_url_to_id[$enclosure_o->url]['width'] = $attachment->width;
@ -489,6 +493,7 @@ class QvitterPlugin extends Plugin {
} }
} }
// this applies to older versions of gnu social, i think
} catch (ServerException $e) { } catch (ServerException $e) {
$thumb = File_thumbnail::getKV('file_id', $attachment->id); $thumb = File_thumbnail::getKV('file_id', $attachment->id);
if ($thumb instanceof File_thumbnail) { if ($thumb instanceof File_thumbnail) {
@ -515,18 +520,11 @@ class QvitterPlugin extends Plugin {
} }
} }
// add thumbs to $twitter_status // add the extra meta data to $twitter_status
if (!empty($twitter_status['attachments'])) { if (!empty($twitter_status['attachments'])) {
foreach ($twitter_status['attachments'] as &$attachment) { foreach ($twitter_status['attachments'] as &$attachment) {
if (!empty($attachment_url_to_id[$attachment['url']])) { if (!empty($attachment_url_to_id[$attachment['url']])) {
$attachment['id'] = $attachment_url_to_id[$attachment['url']]['id']; $attachment = array_merge($attachment,$attachment_url_to_id[$attachment['url']]);
$attachment['width'] = $attachment_url_to_id[$attachment['url']]['width'];
$attachment['height'] = $attachment_url_to_id[$attachment['url']]['height'];
$attachment['thumb_url'] = $attachment_url_to_id[$attachment['url']]['thumb_url'];
$attachment['large_thumb_url'] = $attachment_url_to_id[$attachment['url']]['large_thumb_url'];
if(isset($attachment_url_to_id[$attachment['url']]['animated'])) {
$attachment['animated'] = $attachment_url_to_id[$attachment['url']]['animated'];
}
} }
} }
} }

View File

@ -812,8 +812,7 @@ function expand_queet(q,doScrolling) {
q.prev().addClass('next-expanded'); q.prev().addClass('next-expanded');
// if shortened queet, get full text // if shortened queet, get full text
if(q.children('.queet').find('span.attachment.more').length>0) { if(q.children('.queet').find('span.attachment.more').length>0 && q.data('attachments') != 'undefined') {
var attachmentId = q.children('.queet').find('span.attachment.more').attr('data-attachment-id');
// get full html for queet, first try localstorage cache // get full html for queet, first try localstorage cache
var cacheData = localStorageObjectCache_GET('fullQueetHtml',qid); var cacheData = localStorageObjectCache_GET('fullQueetHtml',qid);
@ -822,11 +821,20 @@ function expand_queet(q,doScrolling) {
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML())); q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML()));
} }
else { else {
getFromAPI("attachment/" + attachmentId + ".json",function(data){
if(data) { var attachmentId = q.children('.queet').find('span.attachment.more').attr('data-attachment-id');
localStorageObjectCache_STORE('fullQueetHtml',qid,data);
q.children('.queet').find('.queet-text').html($.trim(data)); // the url to the text/html attachment is in an array in an attribute
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML())); $.each(q.data('attachments'), function(k,attachment) {
if(attachment.id == attachmentId) {
$.get(attachment.url,function(data){
if(data) {
localStorageObjectCache_STORE('fullQueetHtml',qid,data);
q.children('.queet').find('.queet-text').html($.trim(data));
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML()));
}
});
return false;
} }
}); });
} }
@ -1723,7 +1731,7 @@ function buildQueetHtml(obj, idInStream, extraClassesThisRun, requeeted_by, isCo
var attachmentNum = 0; var attachmentNum = 0;
if(typeof obj.attachments != "undefined") { if(typeof obj.attachments != "undefined") {
$.each(obj.attachments, function(){ $.each(obj.attachments, function(){
if(this.id != null) { // if there's an id we assume this is a image or video if(typeof this.thumb_url != 'undefined' && this.thumb_url !== null) { // if there's a thumb_url we assume this is a image or video
var bigThumbW = 1000; var bigThumbW = 1000;
var bigThumbH = 3000; var bigThumbH = 3000;
if(bigThumbW > window.siteMaxThumbnailSize) { if(bigThumbW > window.siteMaxThumbnailSize) {
@ -1754,11 +1762,11 @@ function buildQueetHtml(obj, idInStream, extraClassesThisRun, requeeted_by, isCo
// animated gifs always get default small non-animated thumbnail // animated gifs always get default small non-animated thumbnail
if(this.animated === true && typeof this.thumb_url != 'undefined') { if(this.animated === true) {
var img_url = this.thumb_url; var img_url = this.thumb_url;
} }
// if no dimensions are set, go with default thumb // if no dimensions are set, go with default thumb
else if(this.width === null && this.height === null && typeof this.thumb_url != 'undefined') { else if(this.width === null && this.height === null) {
var img_url = this.thumb_url; var img_url = this.thumb_url;
} }
// large images get large thumbnail // large images get large thumbnail