Direct.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <div>
  3. <div v-if="loaded && page == 'browse'" class="container messages-page p-0 p-md-2 mt-n4" style="min-height: 50vh;">
  4. <div class="col-12 col-md-8 offset-md-2 p-0 px-md-2">
  5. <div class="card shadow-none border mt-4">
  6. <div class="card-header bg-white py-4">
  7. <span class="h4 font-weight-bold mb-0">Direct Messages</span>
  8. <span class="float-right">
  9. <a class="btn btn-outline-primary font-weight-bold py-0" href="#" @click.prevent="goto('add')">New Message</a>
  10. </span>
  11. </div>
  12. <div class="card-header bg-white">
  13. <ul class="nav nav-pills nav-fill">
  14. <li class="nav-item">
  15. <a :class="[tab == 'inbox' ? 'nav-link px-4 font-weight-bold rounded-pill active' : 'nav-link px-4 font-weight-bold rounded-pill']" @click.prevent="switchTab('inbox')" href="#">Inbox</a>
  16. </li>
  17. <li class="nav-item">
  18. <a :class="[tab == 'sent' ? 'nav-link px-4 font-weight-bold rounded-pill active' : 'nav-link px-4 font-weight-bold rounded-pill']" @click.prevent="switchTab('sent')" href="#">Sent</a>
  19. </li>
  20. <li class="nav-item">
  21. <a :class="[tab == 'filtered' ? 'nav-link px-4 font-weight-bold rounded-pill active' : 'nav-link px-4 font-weight-bold rounded-pill']" @click.prevent="switchTab('filtered')" href="#">Filtered</a>
  22. </li>
  23. </ul>
  24. </div>
  25. <ul v-if="tab == 'inbox'" class="list-group list-group-flush">
  26. <div v-if="!messages.inbox.length" class="list-group-item d-flex justify-content-center align-items-center" style="min-height: 40vh;">
  27. <p class="lead mb-0">No messages found :(</p>
  28. </div>
  29. <div v-else v-for="(thread, index) in messages.inbox">
  30. <a class="list-group-item text-dark text-decoration-none border-left-0 border-right-0 border-top-0" :href="'/account/direct/t/'+thread.id">
  31. <div class="media d-flex align-items-center">
  32. <img class="mr-3 rounded-circle img-thumbnail" :src="thread.avatar" alt="Generic placeholder image" width="32px">
  33. <div class="media-body">
  34. <p class="mb-0">
  35. <span class="font-weight-bold text-truncate" :title="[thread.isLocal ? '@' + thread.username : thread.username]" data-toggle="tooltip" data-placement="bottom">
  36. {{thread.name}}
  37. </span>
  38. </p>
  39. <p class="text-muted mb-0" style="font-size:13px;font-weight: 500;">
  40. <span>
  41. <i class="far fa-comment text-primary"></i>
  42. </span>
  43. <span class="pl-1 pr-3">
  44. Received
  45. </span>
  46. <span>
  47. {{thread.timeAgo}}
  48. </span>
  49. </p>
  50. </div>
  51. <span class="float-right">
  52. <i class="fas fa-chevron-right fa-lg text-lighter"></i>
  53. </span>
  54. </div>
  55. </a>
  56. </div>
  57. </ul>
  58. <ul v-if="tab == 'sent'" class="list-group list-group-flush">
  59. <div v-if="!messages.sent.length" class="list-group-item d-flex justify-content-center align-items-center" style="min-height: 40vh;">
  60. <p class="lead mb-0">No messages found :(</p>
  61. </div>
  62. <div v-else v-for="(thread, index) in messages.sent">
  63. <a class="list-group-item text-dark text-decoration-none border-left-0 border-right-0 border-top-0" href="#" @click.prevent="loadMessage(thread.id)">
  64. <div class="media d-flex align-items-center">
  65. <img class="mr-3 rounded-circle img-thumbnail" :src="thread.avatar" alt="Generic placeholder image" width="32px">
  66. <div class="media-body">
  67. <p class="mb-0">
  68. <span class="font-weight-bold text-truncate" :title="[thread.isLocal ? '@' + thread.username : thread.username]" data-toggle="tooltip" data-placement="bottom">
  69. {{thread.name}}
  70. </span>
  71. </p>
  72. <p class="text-muted mb-0" style="font-size:13px;font-weight: 500;">
  73. <span>
  74. <i class="far fa-paper-plane text-primary"></i>
  75. </span>
  76. <span class="pl-1 pr-3">
  77. Delivered
  78. </span>
  79. <span>
  80. {{thread.timeAgo}}
  81. </span>
  82. </p>
  83. </div>
  84. <span class="float-right">
  85. <i class="fas fa-chevron-right fa-lg text-lighter"></i>
  86. </span>
  87. </div>
  88. </a>
  89. </div>
  90. </ul>
  91. <ul v-if="tab == 'filtered'" class="list-group list-group-flush">
  92. <div v-if="!messages.filtered.length" class="list-group-item d-flex justify-content-center align-items-center" style="min-height: 40vh;">
  93. <p class="lead mb-0">No messages found :(</p>
  94. </div>
  95. <div v-else v-for="(thread, index) in messages.filtered">
  96. <a class="list-group-item text-dark text-decoration-none border-left-0 border-right-0 border-top-0" href="#" @click.prevent="loadMessage(thread.id)">
  97. <div class="media d-flex align-items-center">
  98. <img class="mr-3 rounded-circle img-thumbnail" :src="thread.avatar" alt="Generic placeholder image" width="32px">
  99. <div class="media-body">
  100. <p class="mb-0">
  101. <span class="font-weight-bold text-truncate" :title="[thread.isLocal ? '@' + thread.username : thread.username]" data-toggle="tooltip" data-placement="bottom">
  102. {{thread.name}}
  103. </span>
  104. </p>
  105. <p class="text-muted mb-0" style="font-size:13px;font-weight: 500;">
  106. <span>
  107. <i class="fas fa-shield-alt" style="color:#fd9426"></i>
  108. </span>
  109. <span class="pl-1 pr-3">
  110. Filtered
  111. </span>
  112. <span>
  113. {{thread.timeAgo}}
  114. </span>
  115. </p>
  116. </div>
  117. <span class="float-right">
  118. <i class="fas fa-chevron-right fa-lg text-lighter"></i>
  119. </span>
  120. </div>
  121. </a>
  122. </div>
  123. </ul>
  124. </div>
  125. <div v-if="tab == 'inbox'" class="mt-3 text-center">
  126. <button class="btn btn-outline-primary rounded-pill btn-sm" :disabled="inboxPage == 1" @click="messagePagination('inbox', 'prev')">Prev</button>
  127. <button class="btn btn-outline-primary rounded-pill btn-sm" :disabled="messages.inbox.length != 8" @click="messagePagination('inbox', 'next')">Next</button>
  128. </div>
  129. <div v-if="tab == 'sent'" class="mt-3 text-center">
  130. <button class="btn btn-outline-primary rounded-pill btn-sm" :disabled="sentPage == 1" @click="messagePagination('sent', 'prev')">Prev</button>
  131. <button class="btn btn-outline-primary rounded-pill btn-sm" :disabled="messages.sent.length != 8" @click="messagePagination('sent', 'next')">Next</button>
  132. </div>
  133. <div v-if="tab == 'filtered'" class="mt-3 text-center">
  134. <button class="btn btn-outline-primary rounded-pill btn-sm" :disabled="filteredPage == 1" @click="messagePagination('filtered', 'prev')">Prev</button>
  135. <button class="btn btn-outline-primary rounded-pill btn-sm" :disabled="messages.filtered.length != 8" @click="messagePagination('filtered', 'next')">Next</button>
  136. </div>
  137. </div>
  138. </div>
  139. <div v-if="loaded && page == 'add'" class="container messages-page p-0 p-md-2 mt-n4" style="min-height: 60vh;">
  140. <div class="col-12 col-md-8 offset-md-2 p-0 px-md-2">
  141. <div class="card shadow-none border mt-4">
  142. <div class="card-header bg-white py-4 d-flex justify-content-between">
  143. <span class="cursor-pointer px-3" @click="goto('browse')"><i class="fas fa-chevron-left"></i></span>
  144. <span class="h4 font-weight-bold mb-0">New Direct Message</span>
  145. <span><i class="fas fa-chevron-right text-white"></i></span>
  146. </div>
  147. <div class="card-body d-flex align-items-center justify-content-center" style="height: 60vh;">
  148. <div class="">
  149. <p class="form-group">
  150. <label>To:</label>
  151. <!-- <div class="input-group pt-0">
  152. <div class="input-group-prepend">
  153. <span class="input-group-text" id="basic-addon1">@</span>
  154. </div>
  155. <input v-model="composeUsername" type="text" class="form-control" placeholder="dansup">
  156. </div> -->
  157. <autocomplete
  158. :search="composeSearch"
  159. placeholder="@dansup"
  160. aria-label="Search usernames"
  161. :get-result-value="getTagResultValue"
  162. @submit="onTagSubmitLocation"
  163. ref="autocomplete"
  164. >
  165. </autocomplete>
  166. <span class="help-text small text-muted">Select a username to send a message to.</span>
  167. </p>
  168. <hr>
  169. <!-- <p>
  170. <button type="button" class="btn btn-primary font-weight-bold btn-block" @click="composeUsernameSelect()" :disabled="!composeUsername.length">Next</button>
  171. </p> -->
  172. <ul class="text-muted">
  173. <li>You cannot message remote accounts yet.</li>
  174. </ul>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. </div>
  180. </div>
  181. </template>
  182. <style type="text/css" scoped>
  183. </style>
  184. <script type="text/javascript">
  185. import Autocomplete from '@trevoreyre/autocomplete-vue'
  186. import '@trevoreyre/autocomplete-vue/dist/style.css'
  187. export default {
  188. components: {
  189. Autocomplete
  190. },
  191. data() {
  192. return {
  193. config: window.App.config,
  194. loaded: false,
  195. profile: {},
  196. page: 'browse',
  197. pages: ['browse', 'add', 'read'],
  198. tab: 'inbox',
  199. tabs: ['inbox', 'sent', 'filtered'],
  200. inboxPage: 1,
  201. sentPage: 1,
  202. filteredPage: 1,
  203. threads: [],
  204. thread: false,
  205. threadIndex: false,
  206. replyText: '',
  207. composeUsername: '',
  208. ctxContext: null,
  209. ctxIndex: null,
  210. uploading: false,
  211. uploadProgress: null,
  212. messages: {
  213. inbox: [],
  214. sent: [],
  215. filtered: []
  216. }
  217. }
  218. },
  219. mounted() {
  220. this.fetchProfile();
  221. let self = this;
  222. axios.get('/api/direct/browse', {
  223. params: {
  224. a: 'inbox'
  225. }
  226. })
  227. .then(res => {
  228. self.loaded = true;
  229. this.threads = res.data
  230. this.messages.inbox = res.data;
  231. });
  232. },
  233. updated() {
  234. $('[data-toggle="tooltip"]').tooltip();
  235. },
  236. methods: {
  237. fetchProfile() {
  238. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  239. this.profile = res.data;
  240. window._sharedData.curUser = res.data;
  241. });
  242. },
  243. goto(l = 'browse') {
  244. this.page = l;
  245. let url = '/account/direct';
  246. switch(l) {
  247. case 'read':
  248. url = '/account/direct/t/' + this.thread.id;
  249. break;
  250. case 'add':
  251. url += '#/new';
  252. break;
  253. }
  254. window.history.pushState({},'',url);
  255. },
  256. loadMessage(id) {
  257. let url = '/account/direct/t/' + id;
  258. window.location.href = url;
  259. return;
  260. },
  261. composeUsernameSelect() {
  262. if(this.profile.username == this.composeUsername) {
  263. swal('Ooops!', 'You cannot send a direct message to yourself.', 'error');
  264. this.composeUsername = '';
  265. return;
  266. }
  267. axios.post('/api/direct/lookup', {
  268. username: this.composeUsername
  269. }).then(res => {
  270. let url = '/account/direct/t/' + res.data.id;
  271. window.location.href = url;
  272. }).catch(err => {
  273. let msg = 'The username you entered is incorrect. Please try again';
  274. swal('Ooops!', msg, 'error');
  275. this.composeUsername = '';
  276. });
  277. },
  278. truncate(t) {
  279. return _.truncate(t);
  280. },
  281. switchTab(tab) {
  282. let self = this;
  283. switch(tab) {
  284. case 'inbox':
  285. if(this.messages.inbox.length == 0) {
  286. // fetch
  287. }
  288. break;
  289. case 'sent':
  290. if(this.messages.sent.length == 0) {
  291. axios.get('/api/direct/browse', {
  292. params: {
  293. a: 'sent'
  294. }
  295. })
  296. .then(res => {
  297. self.loaded = true;
  298. self.threads = res.data
  299. self.messages.sent = res.data;
  300. });
  301. }
  302. break;
  303. case 'filtered':
  304. if(this.messages.filtered.length == 0) {
  305. axios.get('/api/direct/browse', {
  306. params: {
  307. a: 'filtered'
  308. }
  309. })
  310. .then(res => {
  311. self.loaded = true;
  312. self.threads = res.data
  313. self.messages.filtered = res.data;
  314. });
  315. }
  316. break;
  317. }
  318. this.tab = tab;
  319. },
  320. composeSearch(input) {
  321. if (input.length < 1) { return []; };
  322. let self = this;
  323. let results = [];
  324. return axios.get('/api/local/compose/tag/search', {
  325. params: {
  326. q: input
  327. }
  328. }).then(res => {
  329. return res.data;
  330. });
  331. },
  332. getTagResultValue(result) {
  333. return '@' + result.name;
  334. },
  335. onTagSubmitLocation(result) {
  336. //this.$refs.autocomplete.value = '';
  337. window.location.href = '/account/direct/t/' + result.id;
  338. return;
  339. },
  340. messagePagination(tab, dir) {
  341. if(tab == 'inbox') {
  342. this.inboxPage = dir == 'prev' ? this.inboxPage - 1 : this.inboxPage + 1;
  343. axios.get('/api/direct/browse', {
  344. params: {
  345. a: 'inbox',
  346. page: this.inboxPage
  347. }
  348. })
  349. .then(res => {
  350. self.loaded = true;
  351. this.threads = res.data
  352. this.messages.inbox = res.data;
  353. });
  354. }
  355. if(tab == 'sent') {
  356. this.sentPage = dir == 'prev' ? this.sentPage - 1 : this.sentPage + 1;
  357. axios.get('/api/direct/browse', {
  358. params: {
  359. a: 'sent',
  360. page: this.sentPage
  361. }
  362. })
  363. .then(res => {
  364. self.loaded = true;
  365. this.threads = res.data
  366. this.messages.sent = res.data;
  367. });
  368. }
  369. if(tab == 'filtered') {
  370. this.filteredPage = dir == 'prev' ? this.filteredPage - 1 : this.filteredPage + 1;
  371. axios.get('/api/direct/browse', {
  372. params: {
  373. a: 'filtered',
  374. page: this.filteredPage
  375. }
  376. })
  377. .then(res => {
  378. self.loaded = true;
  379. this.threads = res.data
  380. this.messages.filtered = res.data;
  381. });
  382. }
  383. }
  384. }
  385. }
  386. </script>