DirectMessage.vue 23 KB

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