DirectMessage.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <template>
  2. <div>
  3. <div v-if="loaded && page == 'read'" class="container messages-page p-0 p-md-2 mt-n4" style="min-height: 60vh;">
  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 d-flex justify-content-between align-items-center">
  7. <span>
  8. <a href="/account/direct" class="text-muted">
  9. <i class="fas fa-chevron-left fa-lg"></i>
  10. </a>
  11. </span>
  12. <span>
  13. <div class="media">
  14. <img class="mr-3 rounded-circle img-thumbnail" :src="thread.avatar" alt="Generic placeholder image" width="40px">
  15. <div class="media-body">
  16. <p class="mb-0">
  17. <span class="font-weight-bold">{{thread.name}}</span>
  18. </p>
  19. <p class="mb-0">
  20. <a v-if="!thread.isLocal" :href="'/'+thread.username" class="text-decoration-none text-muted">{{thread.username}}</a>
  21. <a v-else :href="'/'+thread.username" class="text-decoration-none text-muted">&commat;{{thread.username}}</a>
  22. </p>
  23. </div>
  24. </div>
  25. </span>
  26. <span><a href="#" class="text-muted" @click.prevent="showOptions()"><i class="fas fa-cog fa-lg"></i></a></span>
  27. </div>
  28. <ul class="list-group list-group-flush dm-wrapper" style="height:60vh;overflow-y: scroll;">
  29. <li class="list-group-item border-0">
  30. <p class="text-center small text-muted">
  31. Conversation with <span class="font-weight-bold">{{thread.username}}</span>
  32. </p>
  33. <hr>
  34. </li>
  35. <li v-if="showLoadMore && thread.messages && thread.messages.length > 5" class="list-group-item border-0 mt-n4">
  36. <p class="text-center small text-muted">
  37. <button v-if="!loadingMessages" class="btn btn-primary font-weight-bold rounded-pill btn-sm px-3" @click="loadOlderMessages()">Load Older Messages</button>
  38. <button v-else class="btn btn-primary font-weight-bold rounded-pill btn-sm px-3" disabled>Loading...</button>
  39. </p>
  40. </li>
  41. <li v-for="(convo, index) in thread.messages" class="list-group-item border-0 chat-msg cursor-pointer" @click="openCtxMenu(convo, index)">
  42. <div v-if="!convo.isAuthor" class="media d-inline-flex mb-0">
  43. <img v-if="!hideAvatars" class="mr-3 mt-2 rounded-circle img-thumbnail" :src="thread.avatar" alt="avatar" width="32px">
  44. <div class="media-body">
  45. <p v-if="convo.type == 'photo'" class="pill-to p-0 shadow">
  46. <img :src="convo.media" width="140px" style="border-radius:20px;">
  47. </p>
  48. <div v-else-if="convo.type == 'link'" class="media d-inline-flex mb-0 cursor-pointer">
  49. <div class="media-body">
  50. <div class="card mb-2 rounded border shadow" style="width:240px;" :title="convo.text">
  51. <div class="card-body p-0">
  52. <div class="media d-flex align-items-center">
  53. <div v-if="convo.meta.local" class="bg-primary mr-3 border-right p-3">
  54. <i class="fas fa-link text-white fa-2x"></i>
  55. </div>
  56. <div v-else class="bg-light mr-3 border-right p-3">
  57. <i class="fas fa-link text-lighter fa-2x"></i>
  58. </div>
  59. <div class="media-body text-muted small text-truncate pr-2 font-weight-bold">
  60. {{convo.meta.local ? convo.text.substr(8) : convo.meta.domain}}
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. <p v-else-if="convo.type == 'video'" class="pill-to p-0 shadow">
  68. <!-- <video :src="convo.media" width="140px" style="border-radius:20px;"></video> -->
  69. <span class="d-block bg-primary d-flex align-items-center justify-content-center" style="width:200px;height: 110px;border-radius: 20px;">
  70. <div class="text-center">
  71. <p class="mb-1">
  72. <i class="fas fa-play fa-2x text-white"></i>
  73. </p>
  74. <p class="mb-0 small font-weight-bold text-white">
  75. Play
  76. </p>
  77. </div>
  78. </span>
  79. </p>
  80. <p v-else-if="convo.type == 'emoji'" class="p-0 emoji-msg">
  81. {{convo.text}}
  82. </p>
  83. <p v-else :class="[largerText ? 'pill-to shadow larger-text text-break':'pill-to shadow text-break']">
  84. {{convo.text}}
  85. </p>
  86. <p v-if="!hideTimestamps" class="small text-muted font-weight-bold ml-2 d-flex align-items-center justify-content-start" data-timestamp="timestamp"> <span v-if="convo.hidden" class="mr-2 small" title="Filtered Message" data-toggle="tooltip" data-placement="bottom"><i class="fas fa-lock"></i></span> {{convo.timeAgo}}</p>
  87. </div>
  88. </div>
  89. <div v-else class="media d-inline-flex float-right mb-0">
  90. <div class="media-body">
  91. <p v-if="convo.type == 'photo'" class="pill-from p-0 shadow">
  92. <img :src="convo.media" width="140px" style="border-radius:20px;">
  93. </p>
  94. <div v-else-if="convo.type == 'link'" class="media d-inline-flex float-right mb-0 cursor-pointer">
  95. <div class="media-body">
  96. <div class="card mb-2 rounded border shadow" style="width:240px;" :title="convo.text">
  97. <div class="card-body p-0">
  98. <div class="media d-flex align-items-center">
  99. <div v-if="convo.meta.local" class="bg-primary mr-3 border-right p-3">
  100. <i class="fas fa-link text-white fa-2x"></i>
  101. </div>
  102. <div v-else class="bg-light mr-3 border-right p-3">
  103. <i class="fas fa-link text-lighter fa-2x"></i>
  104. </div>
  105. <div class="media-body text-muted small text-truncate pr-2 font-weight-bold">
  106. {{convo.meta.local ? convo.text.substr(8) : convo.meta.domain}}
  107. </div>
  108. </div>
  109. </div>
  110. </div>
  111. </div>
  112. </div>
  113. <p v-else-if="convo.type == 'video'" class="pill-from p-0 shadow">
  114. <!-- <video :src="convo.media" width="140px" style="border-radius:20px;"></video> -->
  115. <span class="rounded-pill bg-primary d-flex align-items-center justify-content-center" style="width:200px;height: 110px">
  116. <div class="text-center">
  117. <p class="mb-1">
  118. <i class="fas fa-play fa-2x text-white"></i>
  119. </p>
  120. <p class="mb-0 small font-weight-bold">
  121. Play
  122. </p>
  123. </div>
  124. </span>
  125. </p>
  126. <p v-else-if="convo.type == 'emoji'" class="p-0 emoji-msg">
  127. {{convo.text}}
  128. </p>
  129. <p v-else :class="[largerText ? 'pill-from shadow larger-text text-break':'pill-from shadow text-break']">
  130. {{convo.text}}
  131. </p>
  132. <p v-if="!hideTimestamps" class="small text-muted font-weight-bold text-right mr-2"> <span v-if="convo.hidden" class="mr-2 small" title="Filtered Message" data-toggle="tooltip" data-placement="bottom"><i class="fas fa-lock"></i></span> {{convo.timeAgo}}
  133. </p>
  134. </div>
  135. <img v-if="!hideAvatars" class="ml-3 mt-2 rounded-circle img-thumbnail" :src="profile.avatar" alt="avatar" width="32px">
  136. </div>
  137. </li>
  138. </ul>
  139. <div class="card-footer bg-white p-0">
  140. <form class="border-0 rounded-0 align-middle" method="post" action="#">
  141. <textarea class="form-control border-0 rounded-0 no-focus" name="comment" placeholder="Reply ..." autocomplete="off" autocorrect="off" style="height:86px;line-height: 18px;max-height:80px;resize: none; padding-right:115.22px;" v-model="replyText" :disabled="blocked"></textarea>
  142. <input type="button" value="Send" :class="[replyText.length ? 'd-inline-block btn btn-sm btn-primary rounded-pill font-weight-bold reply-btn text-decoration-none text-uppercase' : 'd-inline-block btn btn-sm btn-primary rounded-pill font-weight-bold reply-btn text-decoration-none text-uppercase disabled']" :disabled="replyText.length == 0" @click.prevent="sendMessage"/>
  143. </form>
  144. </div>
  145. <div class="card-footer p-0">
  146. <p class="d-flex justify-content-between align-items-center mb-0 px-3 py-1 small">
  147. <!-- <span class="font-weight-bold" style="color: #D69E2E">
  148. <i class="fas fa-circle mr-1"></i>
  149. Typing ...
  150. </span> -->
  151. <span>
  152. <!-- <span class="btn btn-primary btn-sm font-weight-bold py-0 px-3 rounded-pill" @click="uploadMedia">
  153. <i class="fas fa-share mr-1"></i>
  154. Share
  155. </span> -->
  156. <span class="btn btn-primary btn-sm font-weight-bold py-0 px-3 rounded-pill" @click="uploadMedia">
  157. <i class="fas fa-upload mr-1"></i>
  158. Add Photo/Video
  159. </span>
  160. </span>
  161. <input type="file" id="uploadMedia" class="d-none" name="uploadMedia" accept="image/jpeg,image/png,image/gif,video/mp4" >
  162. <span class="text-muted font-weight-bold">{{replyText.length}}/600</span>
  163. </p>
  164. </div>
  165. </div>
  166. </div>
  167. </div>
  168. <div v-if="loaded && page == 'options'" class="container messages-page p-0 p-md-2 mt-n4" style="min-height: 60vh;">
  169. <div class="col-12 col-md-8 offset-md-2 p-0 px-md-2">
  170. <div class="card shadow-none border mt-4">
  171. <div class="card-header bg-white d-flex justify-content-between align-items-center">
  172. <span>
  173. <a href="#" class="text-muted" @click.prevent="page='read'">
  174. <i class="fas fa-chevron-left fa-lg"></i>
  175. </a>
  176. </span>
  177. <span>
  178. <p class="mb-0 lead font-weight-bold py-2">Message Settings</p>
  179. </span>
  180. <span class="text-lighter" data-toggle="tooltip" data-placement="bottom" title="Have a nice day!"><i class="far fa-smile fa-lg"></i></span>
  181. </div>
  182. <ul class="list-group list-group-flush dm-wrapper" style="height: 698px;">
  183. <div class="list-group-item media border-bottom">
  184. <div class="d-inline-block custom-control custom-switch ml-3">
  185. <input type="checkbox" class="custom-control-input" id="customSwitch0" v-model="hideAvatars">
  186. <label class="custom-control-label" for="customSwitch0"></label>
  187. </div>
  188. <div class="d-inline-block ml-3 font-weight-bold">
  189. Hide Avatars
  190. </div>
  191. </div>
  192. <div class="list-group-item media border-bottom">
  193. <div class="d-inline-block custom-control custom-switch ml-3">
  194. <input type="checkbox" class="custom-control-input" id="customSwitch1" v-model="hideTimestamps">
  195. <label class="custom-control-label" for="customSwitch1"></label>
  196. </div>
  197. <div class="d-inline-block ml-3 font-weight-bold">
  198. Hide Timestamps
  199. </div>
  200. </div>
  201. <div class="list-group-item media border-bottom">
  202. <div class="d-inline-block custom-control custom-switch ml-3">
  203. <input type="checkbox" class="custom-control-input" id="customSwitch2" v-model="largerText">
  204. <label class="custom-control-label" for="customSwitch2"></label>
  205. </div>
  206. <div class="d-inline-block ml-3 font-weight-bold">
  207. Larger Text
  208. </div>
  209. </div>
  210. <!-- <div class="list-group-item media border-bottom">
  211. <div class="d-inline-block custom-control custom-switch ml-3">
  212. <input type="checkbox" class="custom-control-input" id="customSwitch3" v-model="autoRefresh">
  213. <label class="custom-control-label" for="customSwitch3"></label>
  214. </div>
  215. <div class="d-inline-block ml-3 font-weight-bold">
  216. Auto Refresh
  217. </div>
  218. </div> -->
  219. <div class="list-group-item media border-bottom d-flex align-items-center">
  220. <div class="d-inline-block custom-control custom-switch ml-3">
  221. <input type="checkbox" class="custom-control-input" id="customSwitch4" v-model="mutedNotifications">
  222. <label class="custom-control-label" for="customSwitch4"></label>
  223. </div>
  224. <div class="d-inline-block ml-3 font-weight-bold">
  225. Mute Notifications
  226. <p class="small mb-0">You will not receive any direct message notifications from <strong>{{thread.username}}</strong>.</p>
  227. </div>
  228. </div>
  229. </ul>
  230. </div>
  231. </div>
  232. </div>
  233. <b-modal ref="ctxModal"
  234. id="ctx-modal"
  235. hide-header
  236. hide-footer
  237. centered
  238. rounded
  239. size="sm"
  240. body-class="list-group-flush p-0 rounded">
  241. <div class="list-group text-center">
  242. <div v-if="ctxContext && ctxContext.type == 'photo'" class="list-group-item rounded cursor-pointer font-weight-bold text-dark" @click="viewOriginal()">View Original</div>
  243. <div v-if="ctxContext && ctxContext.type == 'video'" class="list-group-item rounded cursor-pointer font-weight-bold text-dark" @click="viewOriginal()">Play</div>
  244. <div v-if="ctxContext && ctxContext.type == 'link'" class="list-group-item rounded cursor-pointer" @click="clickLink()">
  245. <p class="mb-0" style="font-size:12px;">
  246. Navigate to
  247. </p>
  248. <p class="mb-0 font-weight-bold text-dark">
  249. {{this.ctxContext.meta.domain}}
  250. </p>
  251. </div>
  252. <div v-if="ctxContext && (ctxContext.type == 'text' || ctxContext.type == 'emoji' || ctxContext.type == 'link')" class="list-group-item rounded cursor-pointer text-dark" @click="copyText()">Copy</div>
  253. <div v-if="ctxContext && !ctxContext.isAuthor" class="list-group-item rounded cursor-pointer text-muted" @click="reportMessage()">Report</div>
  254. <div v-if="ctxContext && ctxContext.isAuthor" class="list-group-item rounded cursor-pointer text-muted" @click="deleteMessage()">Delete</div>
  255. <div class="list-group-item rounded cursor-pointer text-lighter" @click="closeCtxMenu()">Cancel</div>
  256. </div>
  257. </b-modal>
  258. </div>
  259. </template>
  260. <style type="text/css" scoped>
  261. .reply-btn {
  262. position: absolute;
  263. bottom: 54px;
  264. right: 20px;
  265. width: 90px;
  266. text-align: center;
  267. border-radius: 0 3px 3px 0;
  268. }
  269. .media-body .bg-primary {
  270. background: linear-gradient(135deg, #2EA2F4 0%, #0B93F6 100%) !important;
  271. }
  272. .pill-to {
  273. background:#EDF2F7;
  274. font-weight: 500;
  275. border-radius: 20px !important;
  276. padding-left: 1rem;
  277. padding-right: 1rem;
  278. padding-top: 0.5rem;
  279. padding-bottom: 0.5rem;
  280. margin-right: 3rem;
  281. margin-bottom: 0.25rem;
  282. }
  283. .pill-from {
  284. color: white !important;
  285. text-align: right !important;
  286. /*background: #53d769;*/
  287. background: linear-gradient(135deg, #2EA2F4 0%, #0B93F6 100%) !important;
  288. font-weight: 500;
  289. border-radius: 20px !important;
  290. padding-left: 1rem;
  291. padding-right: 1rem;
  292. padding-top: 0.5rem;
  293. padding-bottom: 0.5rem;
  294. margin-left: 3rem;
  295. margin-bottom: 0.25rem;
  296. }
  297. .chat-msg:hover {
  298. background: #f7fbfd;
  299. }
  300. .no-focus:focus {
  301. outline: none !important;
  302. outline-width: 0 !important;
  303. box-shadow: none;
  304. -moz-box-shadow: none;
  305. -webkit-box-shadow: none;
  306. }
  307. .emoji-msg {
  308. font-size: 4rem !important;
  309. line-height: 30px !important;
  310. margin-top: 10px !important;
  311. }
  312. .larger-text {
  313. font-size: 22px;
  314. }
  315. </style>
  316. <script type="text/javascript">
  317. export default {
  318. props: ['accountId'],
  319. data() {
  320. return {
  321. config: window.App.config,
  322. hideAvatars: true,
  323. hideTimestamps: false,
  324. largerText: false,
  325. autoRefresh: false,
  326. mutedNotifications: false,
  327. blocked: false,
  328. loaded: false,
  329. profile: {},
  330. page: 'read',
  331. pages: ['browse', 'add', 'read'],
  332. threads: [],
  333. thread: false,
  334. threadIndex: false,
  335. replyText: '',
  336. composeUsername: '',
  337. ctxContext: null,
  338. ctxIndex: null,
  339. uploading: false,
  340. uploadProgress: null,
  341. min_id: null,
  342. max_id: null,
  343. loadingMessages: false,
  344. showLoadMore: true,
  345. }
  346. },
  347. mounted() {
  348. this.fetchProfile();
  349. let self = this;
  350. axios.get('/api/direct/thread', {
  351. params: {
  352. pid: self.accountId
  353. }
  354. })
  355. .then(res => {
  356. self.loaded = true;
  357. let d = res.data;
  358. d.messages.reverse();
  359. this.thread = d;
  360. this.threads = [d];
  361. this.threadIndex = 0;
  362. let mids = d.messages.map(m => m.id);
  363. this.max_id = Math.max(...mids);
  364. this.min_id = Math.min(...mids);
  365. this.mutedNotifications = d.muted;
  366. this.markAsRead();
  367. //this.messagePoll();
  368. setTimeout(function() {
  369. let objDiv = document.querySelector('.dm-wrapper');
  370. objDiv.scrollTop = objDiv.scrollHeight;
  371. }, 300);
  372. });
  373. let options = localStorage.getItem('px_dm_options');
  374. if(options) {
  375. options = JSON.parse(options);
  376. this.hideAvatars = options.hideAvatars;
  377. this.hideTimestamps = options.hideTimestamps;
  378. this.largerText = options.largerText;
  379. }
  380. },
  381. watch: {
  382. mutedNotifications: function(v) {
  383. if(v) {
  384. axios.post('/api/direct/mute', {
  385. id: this.accountId
  386. }).then(res => {
  387. });
  388. } else {
  389. axios.post('/api/direct/unmute', {
  390. id: this.accountId
  391. }).then(res => {
  392. });
  393. }
  394. this.mutedNotifications = v;
  395. },
  396. hideAvatars: function(v) {
  397. this.hideAvatars = v;
  398. this.updateOptions();
  399. },
  400. hideTimestamps: function(v) {
  401. this.hideTimestamps = v;
  402. this.updateOptions();
  403. },
  404. largerText: function(v) {
  405. this.largerText = v;
  406. this.updateOptions();
  407. },
  408. },
  409. updated() {
  410. $('[data-toggle="tooltip"]').tooltip();
  411. },
  412. methods: {
  413. fetchProfile() {
  414. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  415. this.profile = res.data;
  416. window._sharedData.curUser = res.data;
  417. });
  418. },
  419. sendMessage() {
  420. let self = this;
  421. let rt = this.replyText;
  422. axios.post('/api/direct/create', {
  423. 'to_id': this.threads[this.threadIndex].id,
  424. 'message': rt,
  425. 'type': self.isEmoji(rt) && rt.length < 10 ? 'emoji' : 'text'
  426. }).then(res => {
  427. let msg = res.data;
  428. self.threads[self.threadIndex].messages.push(msg);
  429. let mids = self.threads[self.threadIndex].messages.map(m => m.id);
  430. this.max_id = Math.max(...mids)
  431. this.min_id = Math.min(...mids)
  432. setTimeout(function() {
  433. var objDiv = document.querySelector('.dm-wrapper');
  434. objDiv.scrollTop = objDiv.scrollHeight;
  435. }, 300);
  436. }).catch(err => {
  437. if(err.response.status == 403) {
  438. self.blocked = true;
  439. swal('Profile Unavailable', 'You cannot message this profile at this time.', 'error');
  440. }
  441. })
  442. this.replyText = '';
  443. },
  444. openCtxMenu(r, i) {
  445. this.ctxIndex = i;
  446. this.ctxContext = r;
  447. this.$refs.ctxModal.show();
  448. },
  449. closeCtxMenu() {
  450. this.$refs.ctxModal.hide();
  451. },
  452. truncate(t) {
  453. return _.truncate(t);
  454. },
  455. deleteMessage() {
  456. let self = this;
  457. let c = window.confirm('Are you sure you want to delete this message?');
  458. if(c) {
  459. axios.delete('/api/direct/message', {
  460. params: {
  461. id: self.ctxContext.id
  462. }
  463. }).then(res => {
  464. self.threads[self.threadIndex].messages.splice(self.ctxIndex,1);
  465. self.closeCtxMenu();
  466. });
  467. } else {
  468. self.closeCtxMenu();
  469. }
  470. },
  471. reportMessage() {
  472. this.closeCtxMenu();
  473. let url = '/i/report?type=post&id=' + this.ctxContext.reportId;
  474. window.location.href = url;
  475. return;
  476. },
  477. uploadMedia(event) {
  478. let self = this;
  479. $(document).on('change', '#uploadMedia', function(e) {
  480. self.handleUpload();
  481. });
  482. let el = $(event.target);
  483. el.attr('disabled', '');
  484. $('#uploadMedia').click();
  485. el.blur();
  486. el.removeAttr('disabled');
  487. },
  488. handleUpload() {
  489. let self = this;
  490. self.uploading = true;
  491. let io = document.querySelector('#uploadMedia');
  492. if(!io.files.length) {
  493. this.uploading = false;
  494. }
  495. Array.prototype.forEach.call(io.files, function(io, i) {
  496. let type = io.type;
  497. let acceptedMimes = self.config.uploader.media_types.split(',');
  498. let validated = $.inArray(type, acceptedMimes);
  499. if(validated == -1) {
  500. swal('Invalid File Type', 'The file you are trying to add is not a valid mime type. Please upload a '+self.config.uploader.media_types+' only.', 'error');
  501. self.uploading = false;
  502. return;
  503. }
  504. let form = new FormData();
  505. form.append('file', io);
  506. form.append('to_id', self.threads[self.threadIndex].id);
  507. let xhrConfig = {
  508. onUploadProgress: function(e) {
  509. let progress = Math.round( (e.loaded * 100) / e.total );
  510. self.uploadProgress = progress;
  511. }
  512. };
  513. axios.post('/api/direct/media', form, xhrConfig)
  514. .then(function(e) {
  515. self.uploadProgress = 100;
  516. self.uploading = false;
  517. let msg = {
  518. id: Date.now(),
  519. type: e.data.type,
  520. isAuthor: true,
  521. text: null,
  522. media: e.data.url,
  523. timeAgo: '1s',
  524. seen: null
  525. };
  526. self.threads[self.threadIndex].messages.push(msg);
  527. setTimeout(function() {
  528. var objDiv = document.querySelector('.dm-wrapper');
  529. objDiv.scrollTop = objDiv.scrollHeight;
  530. }, 300);
  531. }).catch(function(e) {
  532. switch(e.response.status) {
  533. case 451:
  534. self.uploading = false;
  535. io.value = null;
  536. swal('Banned Content', 'This content has been banned and cannot be uploaded.', 'error');
  537. break;
  538. default:
  539. self.uploading = false;
  540. io.value = null;
  541. swal('Oops, something went wrong!', 'An unexpected error occurred.', 'error');
  542. break;
  543. }
  544. });
  545. io.value = null;
  546. self.uploadProgress = 0;
  547. });
  548. },
  549. viewOriginal() {
  550. let url = this.ctxContext.media;
  551. window.location.href = url;
  552. return;
  553. },
  554. isEmoji(text) {
  555. const onlyEmojis = text.replace(new RegExp('[\u0000-\u1eeff]', 'g'), '')
  556. const visibleChars = text.replace(new RegExp('[\n\r\s]+|( )+', 'g'), '')
  557. return onlyEmojis.length === visibleChars.length
  558. },
  559. copyText() {
  560. window.App.util.clipboard(this.ctxContext.text);
  561. this.closeCtxMenu();
  562. return;
  563. },
  564. clickLink() {
  565. let url = this.ctxContext.text;
  566. if(this.ctxContext.meta.local != true) {
  567. url = '/i/redirect?url=' + encodeURI(this.ctxContext.text);
  568. }
  569. window.location.href = url;
  570. },
  571. markAsRead() {
  572. return;
  573. axios.post('/api/direct/read', {
  574. pid: this.accountId,
  575. sid: this.max_id
  576. }).then(res => {
  577. }).catch(err => {
  578. });
  579. },
  580. loadOlderMessages() {
  581. let self = this;
  582. this.loadingMessages = true;
  583. axios.get('/api/direct/thread', {
  584. params: {
  585. pid: this.accountId,
  586. max_id: this.min_id,
  587. }
  588. }).then(res => {
  589. let d = res.data;
  590. if(!d.messages.length) {
  591. this.showLoadMore = false;
  592. this.loadingMessages = false;
  593. return;
  594. }
  595. let cids = this.thread.messages.map(m => m.id);
  596. let m = d.messages.filter(m => {
  597. return cids.indexOf(m.id) == -1;
  598. }).reverse();
  599. let mids = m.map(m => m.id);
  600. let min_id = Math.min(...mids);
  601. if(min_id == this.min_id) {
  602. this.showLoadMore = false;
  603. this.loadingMessages = false;
  604. return;
  605. }
  606. this.min_id = min_id;
  607. this.thread.messages.unshift(...m);
  608. setTimeout(function() {
  609. self.loadingMessages = false;
  610. }, 500);
  611. }).catch(err => {
  612. this.loadingMessages = false;
  613. })
  614. },
  615. messagePoll() {
  616. let self = this;
  617. setInterval(function() {
  618. axios.get('/api/direct/thread', {
  619. params: {
  620. pid: self.accountId,
  621. min_id: self.thread.messages[self.thread.messages.length - 1].id
  622. }
  623. }).then(res => {
  624. });
  625. }, 5000);
  626. },
  627. showOptions() {
  628. this.page = 'options';
  629. },
  630. updateOptions() {
  631. let options = {
  632. v: 1,
  633. hideAvatars: this.hideAvatars,
  634. hideTimestamps: this.hideTimestamps,
  635. largerText: this.largerText
  636. }
  637. window.localStorage.setItem('px_dm_options', JSON.stringify(options));
  638. }
  639. }
  640. }
  641. </script>