filters.ts 7.5 KB

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