@@ -10,10 +10,10 @@ function format (number) {
1010 return number > 9 ? '' + number : '0' + number ;
1111}
1212
13- function formatTime ( time ) {
13+ function formatTime ( unixTime ) {
1414 const MONTHS = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' ,
1515 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ;
16- let date = new Date ( time ) ;
16+ let date = new Date ( parseInt ( unixTime . toString ( ) . slice ( 0 , 13 ) ) ) ;
1717 let hours = format ( date . getHours ( ) ) ;
1818 let minutes = format ( date . getMinutes ( ) ) ;
1919 let day = format ( date . getDate ( ) ) ;
@@ -32,35 +32,35 @@ function dom (content) {
3232}
3333
3434function getUsernames ( chat_ , shouldTruncate ) {
35- let usernames = chat_ . accounts . map ( ( acc ) => acc . _params . username ) . join ( ', ' ) ;
35+ let usernames = chat_ . users . map ( ( acc ) => acc . username ) . join ( ', ' ) ;
3636 return usernames ;
3737}
3838
3939function getChatTitle ( chat_ ) {
40- return chat_ . _params . threadTitle ;
40+ return chat_ . thread_title ;
4141}
4242
4343function isGroupChat ( chat_ ) {
44- if ( chat_ && chat_ . _params && chat_ . _params . isGroup ) {
44+ if ( chat_ && chat_ . is_group ) {
4545 return true ;
4646 }
4747 return false ;
4848}
4949
5050function getChatThumbnail ( chat_ ) {
51- if ( chat_ . accounts [ 0 ] && ! isGroupChat ( chat_ ) ) {
52- return chat_ . accounts [ 0 ] . _params . picture ;
51+ if ( chat_ . users [ 0 ] && ! isGroupChat ( chat_ ) ) {
52+ return chat_ . users [ 0 ] . profile_pic_url ;
5353 }
54- return chat_ . accounts . map ( ( account ) => {
55- return account . _params . picture ;
54+ return chat_ . users . map ( ( user ) => {
55+ return user . profile_pic_url ;
5656 } ) ;
5757}
5858
5959function isCurrentChat ( chat_ ) {
6060 if ( window . currentChatId === DUMMY_CHAT_ID ) {
61- return ! window . chatListHash [ chat_ . id ] ;
61+ return ! window . chatListHash [ chat_ . thread_id ] ;
6262 } else {
63- return chat_ . id === window . currentChatId ;
63+ return chat_ . thread_id === window . currentChatId ;
6464 }
6565}
6666
@@ -76,7 +76,7 @@ function setActive (el) {
7676}
7777
7878function getMsgDirection ( message ) {
79- if ( message . _params . accountId == window . loggedInUserId ) return 'outward' ;
79+ if ( message . user_id == window . loggedInUserId ) return 'outward' ;
8080 else return 'inward' ;
8181}
8282
@@ -108,22 +108,20 @@ function canRenderOlderMessages (chatId) {
108108}
109109
110110function getMsgPreview ( chat_ ) {
111- let msgPreview = chat_ . items [ 0 ] . _params . text || 'Media message' ;
111+ let msgPreview = chat_ . items [ 0 ] . text || 'Media message' ;
112112 return truncate ( msgPreview , 25 ) ;
113113}
114114
115115function isActive ( chat_ ) {
116- return chat_ . id === window . chat . id ;
116+ return chat_ . thread_id === window . chat . thread_id ;
117117}
118118
119119function markAsRead ( id , li ) {
120120 let chat_ = unreadChats [ id ] ;
121121 if ( chat_ ) {
122- chat_ . thread_id = chat_ . id ;
123122 if ( window . shouldSendSeenFlags ) {
124123 ipcRenderer . send ( 'markAsRead' , chat_ ) ;
125124 }
126-
127125 delete unreadChats [ id ] ;
128126 }
129127 li . classList . remove ( 'notification' ) ;
@@ -140,7 +138,7 @@ function resetMessageTextArea () {
140138
141139function sendMessage ( message , accounts , chatId , trackerKey ) {
142140 const isNewChat = ! chatId ;
143- let users = accounts . map ( ( account ) => account . id ) ;
141+ let users = accounts . map ( ( account ) => account . pk ) ;
144142 ipcRenderer . send ( 'message' , { message, isNewChat, users, chatId, trackerKey } ) ;
145143}
146144
@@ -150,10 +148,10 @@ function submitMessage (chat_) {
150148 const sendingAt = new Date ( ) ;
151149 const tackerKey = sendingAt . getTime ( ) ;
152150 if ( message . trim ( ) ) {
153- sendMessage ( message , chat_ . accounts , chat_ . id , tackerKey ) ;
151+ sendMessage ( message , chat_ . users , chat_ . thread_id , tackerKey ) ;
154152 resetMessageTextArea ( ) ;
155153 const sendingNow = createSendingMessage ( message , 'text' , tackerKey ) ;
156- queueInSending ( chat_ . id , sendingNow ) ;
154+ queueInSending ( chat_ . thread_id , sendingNow ) ;
157155
158156 //Rendering current text
159157 let div = renderMessage ( message , 'outward' ) ;
@@ -181,11 +179,11 @@ function removeSubmitHandler () {
181179
182180function sendAttachment ( filePath , chat_ ) {
183181 // @todo : pass this as argument instead
184- window . notifiedChatId = chat_ . id ;
182+ window . notifiedChatId = chat_ . thread_id ;
185183 notify ( 'Your file is being uploaded' , true ) ;
186184
187- let recipients = chat_ . accounts . map ( ( account ) => account . id ) ;
188- ipcRenderer . send ( 'upload' , { filePath, recipients, isNewChat : ! chat_ . id , chatId : chat_ . id } ) ;
185+ let recipients = chat_ . users . map ( ( user ) => user . pk ) ;
186+ ipcRenderer . send ( 'upload' , { filePath, recipients, isNewChat : ! chat_ . thread_id , chatId : chat_ . thread_id } ) ;
189187}
190188
191189function addAttachmentSender ( chat_ ) {
@@ -200,22 +198,24 @@ function addAttachmentSender (chat_) {
200198}
201199
202200function addNotification ( el , chat_ ) {
203- if ( chat_ . items [ 0 ] . _params . accountId == window . loggedInUserId ) {
201+ if ( chat_ . items [ 0 ] . user_id == window . loggedInUserId ) {
204202 return ;
205203 }
206204
207205 const isNew = (
208- ( window . chatListHash [ chat_ . id ] &&
209- window . chatListHash [ chat_ . id ] . items [ 0 ] . id !== chat_ . items [ 0 ] . id ) ||
210- ( chat_ . _params . lastSeenAt &&
211- chat_ . _params . lastSeenAt [ window . loggedInUserId ] &&
212- chat_ . items [ 0 ] . id != chat_ . _params . lastSeenAt [ window . loggedInUserId ] . item_id
206+ ( window . chatListHash [ chat_ . thread_id ] &&
207+ window . chatListHash [ chat_ . thread_id ] . items [ 0 ] . item_id !== chat_ . items [ 0 ] . item_id ) ||
208+ ( chat_ . last_seen_at &&
209+ chat_ . last_seen_at [ window . loggedInUserId ] &&
210+ chat_ . items [ 0 ] . item_id != chat_ . last_seen_at [ window . loggedInUserId ] . item_id
213211 ) ) ;
214- if ( isNew ) unreadChats [ chat_ . id ] = chat_ ;
212+ if ( isNew ) {
213+ unreadChats [ chat_ . thread_id ] = chat_ ;
214+ }
215215
216- if ( unreadChats [ chat_ . id ] ) {
217- if ( chat_ . id === window . chat . id && document . hasFocus ( ) ) {
218- markAsRead ( chat_ . id , el ) ;
216+ if ( unreadChats [ chat_ . thread_id ] ) {
217+ if ( chat_ . thread_id === window . chat . thread_id && document . hasFocus ( ) ) {
218+ markAsRead ( chat_ . thread_id , el ) ;
219219 } else {
220220 el . classList . add ( 'notification' ) ;
221221 // @todo pass this as an argument instead
@@ -241,28 +241,28 @@ function notify (message, noBadgeCountIncrease) {
241241}
242242
243243function registerChatUser ( chat_ ) {
244- if ( chat_ . accounts . length === 1 ) {
245- window . chatUsers [ chat_ . accounts [ 0 ] . id ] = chat_ . id ;
244+ if ( chat_ . users . length === 1 ) {
245+ window . chatUsers [ chat_ . users [ 0 ] . pk ] = chat_ . thread_id ;
246246 }
247247}
248248
249249function getIsSeenText ( chat_ ) {
250250 let text = '' ;
251- if ( ! chat_ . items || ! chat_ . items . length || chat_ . items [ 0 ] . _params . accountId != window . loggedInUserId ) {
251+ if ( ! chat_ . items || ! chat_ . items . length || chat_ . items [ 0 ] . user_id != window . loggedInUserId ) {
252252 return '' ;
253253 }
254254
255- let seenBy = chat_ . accounts . filter ( ( account ) => {
255+ let seenBy = chat_ . users . filter ( ( user ) => {
256256 return (
257- chat_ . _params . itemsSeenAt [ account . id ] &&
258- chat_ . _params . itemsSeenAt [ account . id ] . itemId === chat_ . items [ 0 ] . id
257+ chat_ . last_seen_at [ user . pk ] &&
258+ chat_ . last_seen_at [ user . pk ] . item_id === chat_ . items [ 0 ] . item_id
259259 ) ;
260260 } ) ;
261261
262- if ( seenBy . length === chat_ . accounts . length ) {
262+ if ( seenBy . length === chat_ . users . length ) {
263263 text = 'seen' ;
264264 } else if ( seenBy . length ) {
265- text = `👁 ${ getUsernames ( { accounts : seenBy } ) } ` ;
265+ text = `👁 ${ getUsernames ( { users : seenBy } ) } ` ;
266266 }
267267 return text ;
268268}
@@ -283,7 +283,7 @@ function quoteText (text) {
283283}
284284
285285function setProfilePic ( ) {
286- const url = window . loggedInUser . _params . profilePicUrl ;
286+ const url = window . loggedInUser . profile_pic_url ;
287287 const settingsButton = document . querySelector ( '.settings' ) ;
288288 settingsButton . style . backgroundImage = `url(${ url } )` ;
289289}
@@ -310,12 +310,12 @@ function downloadFile (urlOfFile) {
310310
311311function getHTMLElement ( media ) {
312312 let mediaContent ;
313- if ( media . videos ) {
314- mediaContent = `<video width="${ media . videos [ 0 ] . width } " controls>
315- <source src="${ media . videos [ 0 ] . url } " type="video/mp4">
313+ if ( media . video_versions ) {
314+ mediaContent = `<video width="${ media . video_versions [ 0 ] . width } " controls>
315+ <source src="${ media . video_versions [ 0 ] . url } " type="video/mp4">
316316 </video>` ;
317317 } else {
318- mediaContent = `<img src="${ media . images [ 0 ] . url } ">` ;
318+ mediaContent = `<img src="${ media . image_versions2 [ 0 ] . url } ">` ;
319319 }
320320 return mediaContent ;
321321}
@@ -335,9 +335,25 @@ function removeChatFromChats (chatId) {
335335 if ( window . currentChatId === chatId || window . currentChatId === DUMMY_CHAT_ID ) {
336336 resetChatScreen ( ) ;
337337 }
338+
339+ let chatUsers = Object . getOwnPropertyNames ( window . chatUsers ) ;
340+ chatUsers . forEach ( ( userPk ) => {
341+ if ( window . chatUsers [ userPk ] === chatId ) {
342+ delete window . chatUsers [ userPk ] ;
343+ }
344+ } ) ;
345+
346+ delete window . chatListHash [ chatId ] ;
347+
348+ window . chats = window . chats . filter ( ( chat ) => {
349+ if ( chat . thread_id !== chatId ) {
350+ return true ;
351+ }
352+ return false ;
353+ } ) ;
338354
339355 window . chats = window . chats . filter ( ( chat ) => {
340- if ( chat . id !== chatId ) {
356+ if ( chat . thread_id !== chatId ) {
341357 return true ;
342358 }
343359 return false ;
@@ -358,28 +374,30 @@ function queueInSending (chatId, message) {
358374 if ( ! window . messageInQueue [ chatId ] ) {
359375 window . messageInQueue [ chatId ] = [ ] ;
360376 }
377+ const lb = window . messageInQueue [ chatId ] . length ;
361378 window . messageInQueue [ chatId ] . push ( message ) ;
379+ const la = window . messageInQueue [ chatId ] . length ;
362380}
363381
364382function dequeueFromSending ( sentObj ) {
365383 const { chatId, trackerKey } = sentObj ;
366384 if ( ! window . messageInQueue [ chatId ] ) {
367- window . messageInQueue [ chatId ] = window . messageInQueue [ 'new-chat' ] ;
385+ window . messageInQueue [ chatId ] = window . messageInQueue [ 'new-chat' ] . slice ( ) ;
368386 delete window . messageInQueue [ 'new-chat' ] ;
369387 }
370388 let queue = window . messageInQueue [ chatId ] ;
389+ const lb = queue . length ;
371390 queue = queue . filter ( ( messageQueued ) => messageQueued . trackerKey !== trackerKey ) ;
391+ const la = queue . length ;
372392 window . messageInQueue [ chatId ] = queue ;
373393}
374394
375395function createSendingMessage ( message , type , trackerKey ) {
376- return { _params :
377- {
378- text : message ,
379- type : type ,
380- accountId : window . loggedInUserId ,
381- created : undefined
382- } ,
383- trackerKey
396+ return {
397+ text : message ,
398+ item_type : type ,
399+ user_id : window . loggedInUserId ,
400+ timestamp : undefined ,
401+ trackerKey
384402 } ;
385403}
0 commit comments