File size: 4,591 Bytes
19918ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import server from './server.js'
import Account from './Account.js'
import Attachment from './Attachment.js'
import Card from './Card.js'
import Host from './Host.js'
import HtmlDate from './HtmlDate.js'
import {
  details,
  div,
  input,
  p,
  summary
} from 'https://unpkg.com/ez-html-elements'

/** Creates a Status object from the JSON returned from the server. */
function Status (status) {
  // Status PRIVATE:
  const account = Account(status.account)
  const createdAt = HtmlDate(status.created_at)

  const attachmentListHtml = (as, isSensitive) =>
    as.map((a) => Attachment(a, isSensitive).html()).join('')

  function html () {
    const mediaSection =
      status.media_attachments && status.media_attachments.length > 0
        ? attachmentListHtml(status.media_attachments, status.sensitive)
        : ''
    const cardHtml = status.card ? Card(status.card).html() : ''
    const contentSection = cardHtml + status.content
    return status.spoiler_text
      ? details(summary(status.spoiler_text), contentSection + mediaSection)
      : div(contentSection + mediaSection)
  }

  function filterStatus ($status) {
    $status.querySelectorAll('a.hashtag').forEach((a) => {
      a.href = a.href.replace(/^.*\/tags\/(.+)$/, '#tags/$1')
      a.removeAttribute('target') // stay in current window
    })
    $status.querySelectorAll('a.u-url').forEach((a) => {
      const parsed = a.href.match(/^https:\/\/(.+)\/@(.+)$/)
      if (parsed) {
        const [, mentionedServer, mentionedPerson] = parsed
        a.innerHTML =
          Host(mentionedServer).faviconHtml() +
          '@' +
          mentionedPerson +
          '@' +
          mentionedServer
      }
    })
  }

  async function fetchAndAddAfter ($summary) {
    const inReplyTo = Status(await server.status(status.in_reply_to_id))
    inReplyTo.addAfter($summary)
  }
  const favId = `fav-${status.id}`
  const boostId = `boost-${status.id}`
  const bookmarkId = `bookmark-${status.id}`

  const favHtml = input(['fav'], {
    id: favId,
    type: 'checkbox',
    checked: status.favourited
  })
  const boostHtml = input(['boost'], {
    id: boostId,
    type: 'checkbox',
    checked: status.reblogged
  })
  const bookmarkHtml = input(['bookmark'], {
    id: bookmarkId,
    type: 'checkbox',
    checked: status.bookmarked
  })
  const header = p(
    account.html(),
    createdAt.html(),
    favHtml,
    boostHtml,
    bookmarkHtml
  )
  function nextTick (callback) {
    setTimeout(callback, 0)
  }
  function addListeners () {
    nextTick(() => {
      const $fav = document.querySelector('#' + favId)
      const $boost = document.querySelector('#' + boostId)
      const $bookmark = document.querySelector('#' + bookmarkId)
      $fav.addEventListener('change', () => {
        server.fav(status.id, $fav.checked)
      })
      $boost.addEventListener('change', () => {
        server.boost(status.id, $boost.checked)
      })
      $bookmark.addEventListener('change', () => {
        server.bookmark(status.id, $bookmark.checked)
      })
    })
  }

  // Status PUBLIC:
  return Object.freeze({
    /** Insert into the given parent element the HTML text for this post and any preceding posts in the thread. */
    thread: async ($parent) => {
      if (status.reblog) {
        $parent.insertAdjacentHTML('beforeend', header + '♻️')
        addListeners()
        const $subArticle = document.createElement('article')
        Status(status.reblog).thread($subArticle)
        $parent.append($subArticle)
        return
      }

      $parent.insertAdjacentHTML('beforeend', header + html())
      addListeners()
      filterStatus($parent.lastElementChild)
      if (status.in_reply_to_id) {
        const $details = document.createElement('details')
        const $summary = document.createElement('summary')
        $summary.innerText = '🧵'
        $details.append($summary)
        $parent.insertAdjacentElement('afterbegin', $details)
        fetchAndAddAfter($summary, status.in_reply_to_id) // no await, so happens asynchronously
      }
    },

    /** Insert after the given sibling element the HTML for this post. */
    addAfter: ($sibling) => {
      $sibling.insertAdjacentHTML('afterend', html())
      filterStatus($sibling.nextElementSibling)
      if (!account.sameId(status.in_reply_to_account_id)) {
        $sibling.insertAdjacentHTML('afterend', header)
        addListeners()
      }
      if (status.in_reply_to_id) {
        fetchAndAddAfter($sibling, status.in_reply_to_id) // no await, so happens asynchronously
      }
    }
  })
}

export default Status