filters.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {filter} from './helpers';
  18. import {MimeService} from './services/mime';
  19. import {WebClientService} from './services/webclient';
  20. angular.module('3ema.filters', [])
  21. /**
  22. * Escape HTML by replacing special characters with HTML entities.
  23. */
  24. .filter('escapeHtml', function() {
  25. const map = {
  26. '&': '&amp;',
  27. '<': '&lt;',
  28. '>': '&gt;',
  29. '"': '&quot;',
  30. "'": '&#039;',
  31. };
  32. return (text: string) => {
  33. if (text === undefined || text === null) {
  34. text = '';
  35. }
  36. return text.replace(/[&<>"']/g, (m) => map[m]);
  37. };
  38. })
  39. /**
  40. * Convert newline characters with a <br> tag.
  41. */
  42. .filter('nlToBr', function() {
  43. return (text, enabled: boolean) => {
  44. if (enabled) {
  45. text = text.replace(/\n/g, '<br>');
  46. }
  47. return text;
  48. };
  49. })
  50. /**
  51. * Convert links in text to <a> tags.
  52. */
  53. .filter('linkify', function() {
  54. const autolinker = new Autolinker({
  55. // Open links in new window
  56. newWindow: true,
  57. // Don't strip protocol prefix
  58. stripPrefix: false,
  59. // Don't truncate links
  60. truncate: 99999,
  61. // Add class name to linked links
  62. className: 'autolinked',
  63. // Link urls
  64. urls: true,
  65. // Link e-mails
  66. email: true,
  67. // Don't link phone numbers (doesn't work reliably)
  68. phone: false,
  69. // Don't link twitter handles
  70. twitter: false,
  71. // Don't link hashtags
  72. hashtag: false,
  73. });
  74. return (text) => autolinker.link(text);
  75. })
  76. /**
  77. * Convert emoji unicode characters to images.
  78. * Reference: http://git.emojione.com/demos/latest/index.html#js
  79. *
  80. * Set the `imgTag` parameter to `true` to use inline PNGs instead of sprites.
  81. */
  82. .filter('emojify', function() {
  83. return function(text, imgTag = false) {
  84. if (text !== null) {
  85. emojione.sprites = imgTag !== true;
  86. emojione.imagePathPNG = 'img/emojione/';
  87. emojione.imageType = 'png';
  88. return emojione.unicodeToImage(text);
  89. } else {
  90. return text;
  91. }
  92. };
  93. })
  94. /**
  95. * Convert markdown elements to html elements
  96. */
  97. .filter('markify', function() {
  98. return function(text) {
  99. if (text !== null) {
  100. text = text.replace(/\B\*([^\r\n]+?)\*\B/g, '<span class="text-bold">$1</span>');
  101. text = text.replace(/\b_([^\r\n]+?)_\b/g, '<span class="text-italic">$1</span>');
  102. text = text.replace(/\B~([^\r\n]+?)~\B/g, '<span class="text-strike">$1</span>');
  103. return text;
  104. }
  105. return text;
  106. };
  107. })
  108. /**
  109. * Reverse an array.
  110. */
  111. .filter('reverse', function() {
  112. return (list) => list.slice().reverse();
  113. })
  114. /**
  115. * Return whether receiver has corresponding data.
  116. */
  117. .filter('hasData', function() {
  118. return function(obj, receivers) {
  119. const valid = (receiver) => receivers.get(receiver.type).has(receiver.id);
  120. return filter(obj, valid);
  121. };
  122. })
  123. /**
  124. * Return whether item has a corresponding contact.
  125. */
  126. .filter('hasContact', function() {
  127. return function(obj, contacts) {
  128. const valid = (item) => contacts.has(item.id);
  129. return filter(obj, valid);
  130. };
  131. })
  132. /**
  133. * Return whether contact is not me.
  134. */
  135. .filter('isNotMe', ['WebClientService', function(webClientService: WebClientService) {
  136. return function(obj: threema.Receiver) {
  137. const valid = (contact: threema.Receiver) => contact.id !== webClientService.receivers.me.id;
  138. return filter(obj, valid);
  139. };
  140. }])
  141. /**
  142. * Filter for duration formatting.
  143. */
  144. .filter('duration', function() {
  145. return function(seconds) {
  146. const left = Math.floor(seconds / 60);
  147. const right = seconds % 60;
  148. const padLeft = left < 10 ? '0' : '';
  149. const padRight = right < 10 ? '0' : '';
  150. return padLeft + left + ':' + padRight + right;
  151. };
  152. })
  153. .filter('bufferToUrl', ['$sce', '$log', function($sce, $log) {
  154. const logTag = '[filters.bufferToUrl]';
  155. return function(buffer: ArrayBuffer, mimeType) {
  156. if (!buffer) {
  157. $log.error(logTag, 'Could not apply bufferToUrl filter: buffer is', buffer);
  158. return '';
  159. }
  160. let binary = '';
  161. const bytes = new Uint8Array(buffer);
  162. const len = bytes.byteLength;
  163. for (let i = 0; i < len; i++) {
  164. binary += String.fromCharCode(bytes[i]);
  165. }
  166. return $sce.trustAsResourceUrl('data:' + mimeType + ';base64,' + btoa(binary));
  167. };
  168. }])
  169. .filter('mapLink', function() {
  170. return function(location: threema.LocationInfo) {
  171. return 'https://www.openstreetmap.org/?mlat='
  172. + location.lat + '&mlon='
  173. + location.lon;
  174. };
  175. })
  176. /**
  177. * Convert message state to material icon class.
  178. */
  179. .filter('messageStateIcon', function() {
  180. return (message: threema.Message) => {
  181. if (!message) {
  182. return '';
  183. }
  184. if (!message.isOutbox) {
  185. switch (message.state) {
  186. case 'user-ack':
  187. return 'thumb_up';
  188. case 'user-dec':
  189. return 'thumb_down';
  190. default:
  191. return 'reply';
  192. }
  193. }
  194. switch (message.state) {
  195. case 'pending':
  196. case 'sending':
  197. return 'file_upload';
  198. case 'sent':
  199. return 'email';
  200. case 'delivered':
  201. return 'move_to_inbox';
  202. case 'read':
  203. return 'visibility';
  204. case 'send-failed':
  205. return 'report_problem';
  206. case 'user-ack':
  207. return 'thumb_up';
  208. case 'user-dec':
  209. return 'thumb_down';
  210. default:
  211. return '';
  212. }
  213. };
  214. })
  215. .filter('fileSize', function() {
  216. return (size: number) => {
  217. if (!size) {
  218. return '';
  219. }
  220. let i = Math.floor( Math.log(size) / Math.log(1024) );
  221. let x = (size / Math.pow(1024, i)).toFixed(2);
  222. return (x + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]);
  223. };
  224. })
  225. /**
  226. * Return the MIME type label.
  227. */
  228. .filter('mimeTypeLabel', ['MimeService', function(mimeService: MimeService) {
  229. return (mimeType: string) => mimeService.getLabel(mimeType);
  230. }])
  231. /**
  232. * Return the MIME type icon URL.
  233. */
  234. .filter('mimeTypeIcon', ['MimeService', function(mimeService: MimeService) {
  235. return (mimeType: string) => mimeService.getIconUrl(mimeType);
  236. }])
  237. /**
  238. * Convert ID-Array to (Display-)Name-String, separated by ','
  239. */
  240. .filter('idsToNames', ['WebClientService', function (webClientService: WebClientService) {
  241. return(ids: string[]) => {
  242. let names: string[] = [];
  243. for (let id of ids) {
  244. this.contactReceiver = webClientService.contacts.get(id);
  245. names.push(this.contactReceiver.displayName);
  246. }
  247. return names.join(', ');
  248. };
  249. }])
  250. ;