Direct.vue 13 KB

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