File size: 1,304 Bytes
36a3951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  a,
  details,
  figure,
  figcaption,
  img,
  summary
} from 'https://unpkg.com/ez-html-elements'

/** Create an attachment object from the JSON returned from the server. */
export default (attachment, isSensitive) => {
  // Attachment PRIVATE:
  /** Generate HTML for the media item */
  function mediaHtml () {
    if (!attachment.meta || !attachment.meta.small) {
      return ''
    }
    const { width, height } = attachment.meta.small
    switch (attachment.type) {
      case 'image':
      case 'video':
      case 'gifv':
        return figure(
          a(
            { href: attachment.url },
            img({
              alt: attachment.description || 'attachment',
              src: attachment.preview_url,
              width,
              height
            })
          ) + figcaption(attachment.description)
        )

      default:
        console.warn(`Unrecognized media attachment type "${attachment.type}"`)
        return ''
    }
  }

  // Attachment PUBLIC:
  return Object.freeze({
    /** Generate HTML text */
    html: () => {
      const media = mediaHtml()
      if (media === '') {
        return ''
      }
      if (!isSensitive) {
        return media
      }
      return details(summary('⚠️🫣 ' + attachment.description), media)
    }
  })
}