sensitivecontent.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. window.buildAttachmentHTML = function(attachments){
  2. var attachmentHTML = '';
  3. var oembedHTML = '';
  4. var quotedNotices = [];
  5. var attachmentNum = 0;
  6. var oembedNum = 0;
  7. var urlsToHide = [];
  8. if(typeof attachments != "undefined") {
  9. $.each(attachments, function(){
  10. // quoted notices
  11. if(typeof this.quoted_notice != 'undefined') {
  12. var quotedContent = this.quoted_notice.content;
  13. // quoted notice's attachments' thumb urls
  14. var quotedAttachmentsHTML = '';
  15. var quotedAttachmentsHTMLbefore = '';
  16. var quotedAttachmentsHTMLafter = '';
  17. if(typeof this.quoted_notice.attachments != 'undefined' && this.quoted_notice.attachments.length > 0) {
  18. quotedAttachmentsHTML += '<div class="quoted-notice-attachments quoted-notice-attachments-num-' + this.quoted_notice.attachments.length + '">'
  19. $.each(this.quoted_notice.attachments,function(k,qAttach){
  20. quotedAttachmentsHTML += '<div class="quoted-notice-img-container" style="background-image:url(\'' + qAttach.thumb_url + '\')"><img class="quoted-notice-img" src="' + qAttach.thumb_url + '" /></div>';
  21. // remove attachment string from content
  22. quotedContent = quotedContent.split(window.siteInstanceURL + 'attachment/' + qAttach.attachment_id).join('');
  23. });
  24. quotedAttachmentsHTML += '</div>';
  25. // if there is only one attachment, it goes before, otherwise after
  26. if(this.quoted_notice.attachments.length == 1) {
  27. quotedAttachmentsHTMLbefore = quotedAttachmentsHTML;
  28. }
  29. else {
  30. quotedAttachmentsHTMLafter = quotedAttachmentsHTML;
  31. }
  32. }
  33. var quotedNoticeHTML = quotedAttachmentsHTMLbefore + '\
  34. <div class="quoted-notice-header">\
  35. <span class="quoted-notice-author-fullname">' + this.quoted_notice.fullname + '</span>\
  36. <span class="quoted-notice-author-nickname">' + this.quoted_notice.nickname + '</span>\
  37. </div>\
  38. <div class="quoted-notice-body">' + $.trim(quotedContent) + '</div>\
  39. ' + quotedAttachmentsHTMLafter;
  40. quotedNotices.push({
  41. url: this.url,
  42. html: quotedNoticeHTML,
  43. href: window.siteInstanceURL + 'notice/' + this.quoted_notice.id,
  44. class:'quoted-notice'
  45. });
  46. }
  47. // if we have Twitter oembed data, we add is as quotes
  48. else if(typeof this.oembed != 'undefined'
  49. && this.oembed !== false
  50. && this.oembed.provider == 'Twitter') {
  51. var twitterHTML = '<div class="oembed-item-header">\
  52. <span class="oembed-item-title">' + this.oembed.author_name + '</span>\
  53. <span class="oembed-username">' + this.oembed.title + '</span>\
  54. </div>\
  55. <div class="oembed-item-body">' + this.oembed.oembedHTML + '</div>\
  56. <div class="oembed-item-footer">\
  57. <span class="oembed-item-provider">' + this.oembed.provider + '</span>\
  58. </div>';
  59. quotedNotices.push({
  60. url: this.url,
  61. html: twitterHTML,
  62. href: this.url,
  63. class:'oembed-item'
  64. });
  65. }
  66. // if we have other oembed data (but not for photos and youtube, we handle those later)
  67. else if(typeof this.oembed != 'undefined'
  68. && this.oembed !== false
  69. && this.oembed.title !== null
  70. && this.oembed.provider != 'YouTube'
  71. && this.oembed.provider != 'Vimeo'
  72. && this.oembed.type != 'photo') {
  73. var oembedImage = '';
  74. // only local images
  75. if(typeof this.thumb_url != 'undefined'
  76. && this.thumb_url !== null
  77. && isLocalURL(this.thumb_url)) {
  78. oembedImage = '<div class="oembed-img-container" style="background-image:url(\'' + this.thumb_url + '\')"><img class="oembed-img" src="' + this.thumb_url + '" /></div>';
  79. }
  80. var oembedBody = '';
  81. // don't add body if html it's too similar (80%) to the title (wordpress does this..)
  82. if(this.oembed.oembedHTML !== null
  83. && this.oembed.oembedHTML.length > 0) {
  84. if(this.oembed.oembedHTML.length > 200) {
  85. this.oembed.oembedHTML = this.oembed.oembedHTML.substring(0,200) + '…';
  86. }
  87. if(stringSimilarity(this.oembed.oembedHTML,this.oembed.title.substring(0,200)) < 80) {
  88. oembedBody = this.oembed.oembedHTML;
  89. }
  90. }
  91. if(this.oembed.provider === null) {
  92. var oembedProvider = this.url;
  93. var oembedProviderURL = '';
  94. }
  95. else {
  96. var oembedProvider = this.oembed.provider;
  97. var oembedProviderURL = removeProtocolFromUrl(this.oembed.provider_url);
  98. // remove trailing /
  99. if(oembedProviderURL.slice(-1) == '/') {
  100. oembedProviderURL = oembedProviderURL.slice(0,-1);
  101. }
  102. }
  103. // If the oembed data is generated by Qvitter, we know a better way of showing the title
  104. var oembedTitle = this.oembed.title;
  105. var oembedTitleHTML = '<span class="oembed-item-title">' + oembedTitle + '</span>';
  106. if(oembedTitle.slice(-10) == ' (Qvitter)') {
  107. var oembedTimePosted = parseTwitterLongDate(oembedTitle.slice(0,-10));
  108. var oembedGNUsocialUsername = this.oembed.author_name.substring(this.oembed.author_name.lastIndexOf('(')+1,this.oembed.author_name.lastIndexOf(')'));
  109. var oembedGNUsocialFullname = this.oembed.author_name.slice(0,-(oembedGNUsocialUsername.length+3));
  110. oembedTitleHTML = '<span class="oembed-item-title">' + oembedGNUsocialFullname + '</span>\
  111. <span class="oembed-username">@' + oembedGNUsocialUsername + '</span>';
  112. }
  113. oembedHTML += '<a href="' + this.url + '" class="oembed-item">\
  114. ' + oembedImage + '\
  115. <div class="oembed-item-header">\
  116. ' + oembedTitleHTML + '\
  117. </div>\
  118. <div class="oembed-item-body">' + oembedBody + '</div>\
  119. <div class="oembed-item-footer">\
  120. <span class="oembed-item-provider">' + oembedProvider + '</span>\
  121. <span class="oembed-item-provider-url">' + oembedProviderURL + '</span>\
  122. </div>\
  123. </a>';
  124. oembedNum++;
  125. }
  126. // if there's a local thumb_url we assume this is a image or video
  127. else if(typeof this.thumb_url != 'undefined'
  128. && this.thumb_url !== null
  129. && isLocalURL(this.thumb_url)) {
  130. var bigThumbW = 1000;
  131. var bigThumbH = 3000;
  132. if(bigThumbW > window.siteMaxThumbnailSize) {
  133. bigThumbW = window.siteMaxThumbnailSize;
  134. }
  135. if(bigThumbH > window.siteMaxThumbnailSize) {
  136. bigThumbH = window.siteMaxThumbnailSize;
  137. }
  138. // very long landscape images should not have background-size:cover
  139. var noCoverClass='';
  140. if(this.width/this.height > 2) {
  141. noCoverClass=' no-cover';
  142. }
  143. // play button for videos and animated gifs
  144. var playButtonClass = '';
  145. if(typeof this.animated != 'undefined' && this.animated === true
  146. || (this.url.indexOf('://www.youtube.com') > -1 || this.url.indexOf('://youtu.be') > -1)
  147. || this.url.indexOf('://vimeo.com') > -1) {
  148. playButtonClass = ' play-button';
  149. }
  150. // gif-class
  151. var animatedGifClass = '';
  152. if(typeof this.animated != 'undefined' && this.animated === true) {
  153. var animatedGifClass = ' animated-gif';
  154. }
  155. // animated gifs always get default small non-animated thumbnail
  156. if(this.animated === true) {
  157. var img_url = this.thumb_url;
  158. }
  159. // if no dimensions are set, go with default thumb
  160. else if(this.width === null && this.height === null) {
  161. var img_url = this.thumb_url;
  162. }
  163. // large images get large thumbnail
  164. else if(this.width > 1000) {
  165. var img_url = this.large_thumb_url;
  166. }
  167. // no thumbnails for small local images
  168. else if (this.url.indexOf(window.siteInstanceURL) === 0) {
  169. var img_url = this.url;
  170. }
  171. // small thumbnail for small remote images
  172. else {
  173. var img_url = this.thumb_url;
  174. }
  175. var urlToHide = window.siteInstanceURL + 'attachment/' + this.id;
  176. attachmentHTML += '<a data-local-attachment-url="' + urlToHide + '" style="background-image:url(\'' + img_url + '\')" class="thumb-container' + noCoverClass + playButtonClass + animatedGifClass + ' ' + CSSclassNameByHostFromURL(this.url) + '" href="' + this.url + '"><img class="attachment-thumb" data-mime-type="' + this.mimetype + '" src="' + img_url + '"/ data-width="' + this.width + '" data-height="' + this.height + '" data-full-image-url="' + this.url + '" data-thumb-url="' + img_url + '"></a>';
  177. urlsToHide.push(urlToHide); // hide this attachment url from the queet text
  178. attachmentNum++;
  179. }
  180. else if (this.mimetype == 'image/svg+xml') {
  181. var urlToHide = window.siteInstanceURL + 'attachment/' + this.id;
  182. attachmentHTML += '<a data-local-attachment-url="' + urlToHide + '" style="background-image:url(\'' + this.url + '\')" class="thumb-container" href="' + this.url + '"><img class="attachment-thumb" data-mime-type="' + this.mimetype + '" src="' + this.url + '"/></a>';
  183. urlsToHide.push(urlToHide); // hide this attachment url from the queet text
  184. attachmentNum++;
  185. }
  186. });
  187. }
  188. return { html: '<div class="oembed-data oembed-num-' + oembedNum + '">' + oembedHTML + '</div><div class="queet-thumbs thumb-num-' + attachmentNum + '"><div class="sensitive-blocker">&nbsp;</div>' + attachmentHTML + '</div>',
  189. urlsToHide: urlsToHide,
  190. quotedNotices: quotedNotices
  191. };
  192. }
  193. window.sensitiveContentOriginalBuildQueetHtml = window.buildQueetHtml;
  194. window.buildQueetHtml = function(obj, idInStream, extraClasses, requeeted_by, isConversation) {
  195. //add tags to json if they don't exit
  196. if (obj.tags && obj.tags.length > 0) {
  197. for (var tagI = 0; tagI < obj.tags.length; ++tagI) {
  198. extraClasses += (' data-tag-' + obj.tags[tagI]);
  199. if (window.loggedIn.hide_sensitive && obj.tags[tagI] === 'nsfw') extraClasses += ' sensitive-notice';
  200. }
  201. }
  202. return window.sensitiveContentOriginalBuildQueetHtml(obj, idInStream, extraClasses, requeeted_by, isConversation);
  203. }