1
0

domain-blocks.blade.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. @extends('settings.template-vue')
  2. @section('section')
  3. <div>
  4. <div class="d-flex justify-content-between align-items-center">
  5. <div class="title d-flex align-items-center" style="gap: 1rem;">
  6. <p class="mb-0"><a href="/settings/privacy"><i class="far fa-chevron-left fa-lg"></i></a></p>
  7. <h3 class="font-weight-bold mb-0">Domain Blocks</h3>
  8. </div>
  9. </div>
  10. <p class="mt-3 mb-n2 small">You can block entire domains, this prevents users on that instance from interacting with your content and from you seeing content from that domain on public feeds.</p>
  11. <hr />
  12. <div v-if="!loaded" class="d-flex justify-content-center align-items-center flex-grow-1">
  13. <b-spinner />
  14. </div>
  15. <div v-else>
  16. <div class="mb-3 d-flex flex-column flex-md-row justify-content-between align-items-center" style="gap: 2rem;">
  17. <div style="width: 60%;">
  18. <div class="input-group align-items-center">
  19. <input class="form-control form-control-sm rounded-lg" v-model="q" placeholder="Search by domain..." style="padding-right: 60px;" :disabled="!blocks || !blocks.length">
  20. <div style="margin-left: -60px;width: 60px;z-index:3">
  21. <button class="btn btn-link" type="button" style="font-size: 12px;text-decoration: none;" v-html="q && q.length ? 'Clear': '&nbsp;'" @click="searchAction()"></button>
  22. </div>
  23. </div>
  24. </div>
  25. <button type="button" class="btn btn-outline-primary btn-sm font-weight-bold px-3 flex-grow" @click="openModal">
  26. <i class="fas fa-plus mr-1"></i> New Block
  27. </button>
  28. </div>
  29. <div v-if="blocks && blocks.length" class="list-group">
  30. <div
  31. v-for="(item, idx) in chunks[index]"
  32. class="list-group-item">
  33. <div class="d-flex justify-content-between align-items-center font-weight-bold">
  34. <span>
  35. <span v-text="item"></span>
  36. </span>
  37. <span class="btn-group">
  38. <button type="button" class="btn btn-link btn-sm px-3 font-weight-bold" @click="handleUnblock(item)">Unblock</button>
  39. </span>
  40. </div>
  41. </div>
  42. </div>
  43. <nav v-if="blocks && blocks.length && chunks && chunks.length > 1" class="mt-3" aria-label="Domain block pagination">
  44. <ul class="pagination justify-content-center" style="gap: 1rem">
  45. <li
  46. class="page-item"
  47. :class="[ !index ? 'disabled' : 'font-weight-bold' ]"
  48. :disabled="!index"
  49. @click="paginate('prev')">
  50. <span class="page-link px-5 rounded-lg">Previous</span>
  51. </li>
  52. <li
  53. class="page-item"
  54. :class="[ index + 1 === chunks.length ? 'disabled' : 'font-weight-bold' ]"
  55. @click="paginate('next')">
  56. <span class="page-link px-5 rounded-lg" href="#">Next</span>
  57. </li>
  58. </ul>
  59. </nav>
  60. <div v-if="!blocks || !blocks.length">
  61. <hr />
  62. <p class="lead text-center font-weight-bold">You are not blocking any domains.</p>
  63. </div>
  64. </div>
  65. </div>
  66. @endsection
  67. @push('scripts')
  68. <script type="text/javascript">
  69. let app = new Vue({
  70. el: '#content',
  71. data: {
  72. loaded: false,
  73. q: undefined,
  74. blocks: [],
  75. filteredBlocks: [],
  76. chunks: [],
  77. index: 0,
  78. pagination: [],
  79. },
  80. watch: {
  81. q: function(newVal, oldVal) {
  82. this.filterResults(newVal)
  83. }
  84. },
  85. mounted() {
  86. this.fetchBlocks()
  87. },
  88. methods: {
  89. fetchBlocks() {
  90. axios.get('/api/v1/domain_blocks', { params: { 'limit': 200 }})
  91. .then(res => {
  92. let pages = false
  93. if(res.headers?.link) {
  94. pages = this.parseLinkHeader(res.headers['link'])
  95. }
  96. this.blocks = res.data
  97. if(!pages || !pages.hasOwnProperty('next')) {
  98. this.buildList()
  99. } else {
  100. this.handlePagination(pages)
  101. }
  102. })
  103. .catch(err => {
  104. console.log(err.response)
  105. })
  106. },
  107. handlePagination(pages) {
  108. if(!pages || !pages.hasOwnProperty('next')) {
  109. this.buildList()
  110. return
  111. }
  112. this.pagination = pages
  113. this.fetchPagination()
  114. },
  115. buildList() {
  116. this.index = 0
  117. this.chunks = this.chunkify(this.blocks)
  118. this.loaded = true
  119. },
  120. buildSearchList() {
  121. this.index = 0
  122. this.chunks = this.chunkify(this.filteredBlocks)
  123. this.loaded = true
  124. },
  125. fetchPagination() {
  126. axios.get(this.pagination.next)
  127. .then(res => {
  128. let pages = false
  129. if(res.headers?.link) {
  130. pages = this.parseLinkHeader(res.headers['link'])
  131. }
  132. this.blocks.push(...res.data)
  133. if(!pages || !pages.hasOwnProperty('next')) {
  134. this.buildList()
  135. } else {
  136. this.handlePagination(pages)
  137. }
  138. })
  139. .catch(err => {
  140. this.buildList()
  141. })
  142. },
  143. handleUnblock(domain) {
  144. this.loaded = false
  145. axios.delete('/api/v1/domain_blocks', {
  146. params: {
  147. domain: domain
  148. }
  149. })
  150. .then(res => {
  151. this.blocks = this.blocks.filter(d => d != domain)
  152. this.buildList()
  153. })
  154. .catch(err => {
  155. this.buildList()
  156. })
  157. },
  158. filterResults(query) {
  159. this.loaded = false
  160. let formattedQuery = query.trim().toLowerCase()
  161. this.filteredBlocks = this.blocks.filter(domain => domain.toLowerCase().startsWith(formattedQuery))
  162. this.buildSearchList()
  163. },
  164. searchAction($event) {
  165. event.currentTarget.blur()
  166. this.q = ''
  167. },
  168. openModal() {
  169. swal({
  170. title: 'Domain Block',
  171. text: 'Add domain to block, must start with https://',
  172. content: "input",
  173. button: {
  174. text: "Block",
  175. closeModal: false,
  176. }
  177. }).then(val => {
  178. if (!val) {
  179. swal.stopLoading()
  180. swal.close()
  181. return
  182. }
  183. axios.post('/api/v1/domain_blocks', { domain: val })
  184. .then(res => {
  185. let parsedUrl = new URL(val)
  186. swal.stopLoading()
  187. swal.close()
  188. this.index = 0
  189. this.blocks.unshift(parsedUrl.hostname)
  190. this.buildList()
  191. })
  192. .catch(err => {
  193. swal.stopLoading()
  194. swal.close()
  195. if(err.response?.data?.message || err.response?.data?.error) {
  196. swal('Error', err.response?.data?.message ?? err.response?.data?.error, 'error')
  197. }
  198. })
  199. })
  200. },
  201. chunkify(arr, len = 10) {
  202. var chunks = [],
  203. i = 0,
  204. n = arr.length
  205. while (i < n) {
  206. chunks.push(arr.slice(i, i += len))
  207. }
  208. return chunks
  209. },
  210. paginate(dir) {
  211. if(dir === 'prev' && this.index > 0) {
  212. this.index--
  213. return
  214. }
  215. if(dir === 'next' && this.index + 1 < this.chunks.length) {
  216. this.index++
  217. return
  218. }
  219. },
  220. parseLinkHeader(linkHeader) {
  221. const links = {}
  222. if (!linkHeader) {
  223. return links
  224. }
  225. linkHeader.split(',').forEach(part => {
  226. const match = part.match(/<([^>]+)>;\s*rel="([^"]+)"/)
  227. if (match) {
  228. const url = match[1]
  229. const rel = match[2]
  230. if (rel === 'prev' || rel === 'next') {
  231. links[rel] = url
  232. }
  233. }
  234. })
  235. return links
  236. }
  237. }
  238. })
  239. </script>
  240. @endpush