Skip to content

Commit 705db7e

Browse files
committed
intsgram-private-api-migration
optimized-get-chat
1 parent cfe416d commit 705db7e

10 files changed

Lines changed: 640 additions & 856 deletions

File tree

browser/css/app.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ button, button:focus {
238238
border-radius: 50%;
239239
}
240240

241+
.chat .chat-image{
242+
max-height: 400px;
243+
}
244+
241245
.message.outward .content {
242246
background: linear-gradient(to top right, #0371e7, #1888ff);
243247
color: #efefef;
@@ -301,6 +305,10 @@ button, button:focus {
301305
font-style: normal;
302306
}
303307

308+
.heart{
309+
height: 80px;
310+
}
311+
304312
.new-message {
305313
position: relative;
306314
width: 100%;

browser/js/funcs.js

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3434
function 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

3939
function getChatTitle (chat_) {
40-
return chat_._params.threadTitle;
40+
return chat_.thread_title;
4141
}
4242

4343
function 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

5050
function 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

5959
function 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

7878
function 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

110110
function 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

115115
function isActive (chat_) {
116-
return chat_.id === window.chat.id;
116+
return chat_.thread_id === window.chat.thread_id;
117117
}
118118

119119
function 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

141139
function 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

182180
function 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

191189
function addAttachmentSender (chat_) {
@@ -200,22 +198,24 @@ function addAttachmentSender (chat_) {
200198
}
201199

202200
function 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

243243
function 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

249249
function 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

285285
function 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

311311
function 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

364382
function 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

375395
function 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
}

browser/js/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ document.addEventListener('drop', function (event) {
5050
// This code runs once the DOM is loaded (just in case you missed it).
5151
document.addEventListener('DOMContentLoaded', () => {
5252
ipcRenderer.on('loggedInUser', (evt, user) => {
53-
window.loggedInUserId = user.id;
53+
window.loggedInUserId = user.pk;
5454
window.loggedInUser = user;
5555
setProfilePic();
5656
});
5757

5858
ipcRenderer.on('chatList', (evt, chats_) => {
5959
const lengthMismatch = window.chats.length !== chats_.length;
60-
if (!window.chats.length || lengthMismatch || window.chats[0].items[0].id !== chats_[0].items[0].id) {
60+
if (!window.chats.length || lengthMismatch || window.chats[0].items[0].item_id !== chats_[0].items[0].item_id) {
6161
window.chats = chats_;
6262
renderChatList(window.chats);
6363
}
@@ -71,24 +71,25 @@ document.addEventListener('DOMContentLoaded', () => {
7171
});
7272

7373
ipcRenderer.on('chat', (evt, chat_) => {
74+
if (!chat_) return;
7475
let isNewMessage = (
7576
!window.chat.items || !window.chat.items.length ||
76-
!chat_.items.length || window.chat.items[0].id != chat_.items[0].id ||
77+
!chat_.items.length || window.chat.items[0].item_id != chat_.items[0].item_id ||
7778
window.chat.items.length != chat_.items.length ||
7879
getIsSeenText(chat_) != getIsSeenText(window.chat) ||
79-
chat_.items[0].id != chat_._params.lastSeenAt[window.loggedInUserId].item_id
80+
chat_.items[0].item_id != chat_.last_seen_at[window.loggedInUserId].item_id
8081
);
81-
let currentChat = isCurrentChat(chat_);
82-
if (currentChat) {
82+
let chatWithNewMessage = isNewMessage && isCurrentChat(chat_);
83+
if (chatWithNewMessage) {
8384
// reassign currentChatId, for cases of new chats/dummy chats.
84-
window.currentChatId = chat_.id;
85+
window.currentChatId = chat_.thread_id;
8586
}
86-
if (isNewMessage && currentChat && !window.gettingOlderMessages) {
87+
if (chatWithNewMessage && !window.gettingOlderMessages) {
8788
renderChat(chat_);
8889
}
8990
});
9091

91-
ipcRenderer.on('olderMessages', (_, {chatId, messages}) => {
92+
ipcRenderer.on('olderMessages', (_, { chatId, messages }) => {
9293
const previous = window.olderMessages[chatId] || [];
9394
window.olderMessages[chatId] = previous.concat(messages);
9495
if (canRenderOlderMessages(chatId)) {
@@ -135,10 +136,10 @@ document.addEventListener('DOMContentLoaded', () => {
135136
let searchForm = document.querySelector('.header input[name=search]');
136137
searchForm.onkeyup = (e) => {
137138
const value = searchForm.value;
138-
const trimmedValue = value.trim();
139+
const trimmedValue = value.trim();
139140

140141
if (trimmedValue.length > 3) {
141-
ipcRenderer.send('searchUsers', searchForm.value);
142+
ipcRenderer.send('searchUsers', searchForm.value);
142143
} else if (trimmedValue.length === 0) {
143144
renderChatList(window.chats);
144145
}
@@ -189,7 +190,7 @@ document.addEventListener('DOMContentLoaded', () => {
189190
}
190191
this.style.height = textBoxHeight;
191192
}
192-
193+
193194
const tx = document.getElementById('messageText');
194195
const scrHeight = tx.scrollHeight - 6;
195196
tx.setAttribute('style', 'height:' + (scrHeight).toString() + 'px;overflow-y:hidden;');

0 commit comments

Comments
 (0)