NekoX/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java

10923 lines
544 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2013-10-25 17:19:00 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2013-10-25 17:19:00 +02:00
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
2013-10-25 17:19:00 +02:00
import android.app.Activity;
2015-11-26 22:04:02 +01:00
import android.content.Context;
2017-03-31 01:58:05 +02:00
import android.content.Intent;
2013-10-25 17:19:00 +02:00
import android.content.SharedPreferences;
2019-07-18 15:01:39 +02:00
import android.location.Location;
2018-07-30 04:07:02 +02:00
import android.os.Build;
2015-05-21 23:27:27 +02:00
import android.os.Bundle;
2018-07-30 04:07:02 +02:00
import android.os.SystemClock;
2017-03-31 01:58:05 +02:00
import android.telephony.TelephonyManager;
import android.text.TextUtils;
2018-07-30 04:07:02 +02:00
import android.util.LongSparseArray;
2014-10-10 19:16:39 +02:00
import android.util.SparseArray;
2018-07-30 04:07:02 +02:00
import android.util.SparseBooleanArray;
2015-09-24 22:52:02 +02:00
import android.util.SparseIntArray;
2015-06-29 19:12:11 +02:00
import android.widget.Toast;
2013-10-25 17:19:00 +02:00
2015-11-26 22:04:02 +01:00
import org.telegram.SQLite.SQLiteCursor;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.support.SparseLongArray;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.voip.VoIPService;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.NativeByteBuffer;
2019-01-23 18:03:33 +01:00
import org.telegram.tgnet.RequestDelegate;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.AlertDialog;
2015-02-27 20:57:58 +01:00
import org.telegram.ui.ActionBar.BaseFragment;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.ActionBar.Theme;
2015-05-21 23:27:27 +02:00
import org.telegram.ui.ChatActivity;
2015-09-24 22:52:02 +02:00
import org.telegram.ui.Components.AlertsCreator;
2018-07-30 04:07:02 +02:00
import org.telegram.ui.DialogsActivity;
2015-05-21 23:27:27 +02:00
import org.telegram.ui.ProfileActivity;
2013-10-25 17:19:00 +02:00
2019-01-23 18:03:33 +01:00
import java.io.File;
2013-10-25 17:19:00 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
2015-11-26 22:04:02 +01:00
import java.util.Locale;
2013-10-25 17:19:00 +02:00
import java.util.concurrent.ConcurrentHashMap;
2018-07-30 04:07:02 +02:00
import java.util.concurrent.CountDownLatch;
2013-10-25 17:19:00 +02:00
2019-05-14 14:08:05 +02:00
import androidx.core.app.NotificationManagerCompat;
2019-03-03 21:40:48 +01:00
2019-07-18 15:01:39 +02:00
public class MessagesController extends BaseController implements NotificationCenter.NotificationCenterDelegate {
2015-01-02 23:15:07 +01:00
private ConcurrentHashMap<Integer, TLRPC.Chat> chats = new ConcurrentHashMap<>(100, 1.0f, 2);
private ConcurrentHashMap<Integer, TLRPC.EncryptedChat> encryptedChats = new ConcurrentHashMap<>(10, 1.0f, 2);
private ConcurrentHashMap<Integer, TLRPC.User> users = new ConcurrentHashMap<>(100, 1.0f, 2);
2017-07-08 18:32:04 +02:00
private ConcurrentHashMap<String, TLObject> objectsByUsernames = new ConcurrentHashMap<>(100, 1.0f, 2);
2015-01-02 23:15:07 +01:00
2015-10-29 18:10:07 +01:00
private ArrayList<Integer> joiningToChannels = new ArrayList<>();
2018-07-30 04:07:02 +02:00
private SparseArray<TLRPC.ExportedChatInvite> exportedChats = new SparseArray<>();
2017-12-08 18:35:59 +01:00
public ArrayList<TLRPC.RecentMeUrl> hintDialogs = new ArrayList<>();
2019-05-14 14:08:05 +02:00
private SparseArray<ArrayList<TLRPC.Dialog>> dialogsByFolder = new SparseArray<>();
protected ArrayList<TLRPC.Dialog> allDialogs = new ArrayList<>();
public ArrayList<TLRPC.Dialog> dialogsForward = new ArrayList<>();
public ArrayList<TLRPC.Dialog> dialogsServerOnly = new ArrayList<>();
public ArrayList<TLRPC.Dialog> dialogsCanAddUsers = new ArrayList<>();
public ArrayList<TLRPC.Dialog> dialogsChannelsOnly = new ArrayList<>();
public ArrayList<TLRPC.Dialog> dialogsUsersOnly = new ArrayList<>();
public ArrayList<TLRPC.Dialog> dialogsGroupsOnly = new ArrayList<>();
2018-07-30 04:07:02 +02:00
public int unreadUnmutedDialogs;
2015-11-26 22:04:02 +01:00
public ConcurrentHashMap<Long, Integer> dialogs_read_inbox_max = new ConcurrentHashMap<>(100, 1.0f, 2);
public ConcurrentHashMap<Long, Integer> dialogs_read_outbox_max = new ConcurrentHashMap<>(100, 1.0f, 2);
2019-05-14 14:08:05 +02:00
public LongSparseArray<TLRPC.Dialog> dialogs_dict = new LongSparseArray<>();
2018-07-30 04:07:02 +02:00
public LongSparseArray<MessageObject> dialogMessage = new LongSparseArray<>();
public LongSparseArray<MessageObject> dialogMessagesByRandomIds = new LongSparseArray<>();
2019-05-14 14:08:05 +02:00
public LongSparseArray<Integer> deletedHistory = new LongSparseArray<>();
2018-07-30 04:07:02 +02:00
public SparseArray<MessageObject> dialogMessagesByIds = new SparseArray<>();
2015-01-02 23:15:07 +01:00
public ConcurrentHashMap<Long, ArrayList<PrintingUser>> printingUsers = new ConcurrentHashMap<>(20, 1.0f, 2);
2018-07-30 04:07:02 +02:00
public LongSparseArray<CharSequence> printingStrings = new LongSparseArray<>();
public LongSparseArray<Integer> printingStringsTypes = new LongSparseArray<>();
public SparseArray<LongSparseArray<Boolean>> sendingTypings = new SparseArray<>();
2015-01-02 23:15:07 +01:00
public ConcurrentHashMap<Integer, Integer> onlinePrivacy = new ConcurrentHashMap<>(20, 1.0f, 2);
2017-03-31 01:58:05 +02:00
private int lastPrintingStringCount;
2013-10-25 17:19:00 +02:00
2019-05-14 14:08:05 +02:00
private boolean dialogsInTransaction;
2018-07-30 04:07:02 +02:00
private LongSparseArray<Boolean> loadingPeerSettings = new LongSparseArray<>();
2016-03-16 13:26:32 +01:00
private ArrayList<Long> createdDialogIds = new ArrayList<>();
2017-07-08 18:32:04 +02:00
private ArrayList<Long> createdDialogMainThreadIds = new ArrayList<>();
2018-07-30 04:07:02 +02:00
private ArrayList<Long> visibleDialogMainThreadIds = new ArrayList<>();
2016-03-06 02:49:31 +01:00
2015-09-24 22:52:02 +02:00
private SparseIntArray shortPollChannels = new SparseIntArray();
private SparseIntArray needShortPollChannels = new SparseIntArray();
2019-01-23 18:03:33 +01:00
private SparseIntArray shortPollOnlines = new SparseIntArray();
private SparseIntArray needShortPollOnlines = new SparseIntArray();
2019-05-14 14:08:05 +02:00
private LongSparseArray<TLRPC.Dialog> deletingDialogs = new LongSparseArray<>();
private LongSparseArray<TLRPC.Dialog> clearingHistoryDialogs = new LongSparseArray<>();
2015-09-24 22:52:02 +02:00
public boolean loadingBlockedUsers = false;
2018-08-27 10:33:11 +02:00
public SparseIntArray blockedUsers = new SparseIntArray();
2015-09-24 22:52:02 +02:00
private SparseArray<ArrayList<Integer>> channelViewsToSend = new SparseArray<>();
2019-01-23 18:03:33 +01:00
private LongSparseArray<SparseArray<MessageObject>> pollsToCheck = new LongSparseArray<>();
private int pollsToCheckSize;
2015-09-24 22:52:02 +02:00
private long lastViewsCheckTime;
2018-07-30 04:07:02 +02:00
private SparseArray<ArrayList<TLRPC.Updates>> updatesQueueChannels = new SparseArray<>();
private SparseLongArray updatesStartWaitTimeChannels = new SparseLongArray();
private SparseIntArray channelsPts = new SparseIntArray();
private SparseBooleanArray gettingDifferenceChannels = new SparseBooleanArray();
2015-09-24 22:52:02 +02:00
2018-07-30 04:07:02 +02:00
private SparseBooleanArray gettingUnknownChannels = new SparseBooleanArray();
2019-05-14 14:08:05 +02:00
private LongSparseArray<Boolean> gettingUnknownDialogs = new LongSparseArray<>();
2018-07-30 04:07:02 +02:00
private SparseBooleanArray checkingLastMessagesDialogs = new SparseBooleanArray();
private ArrayList<TLRPC.Updates> updatesQueueSeq = new ArrayList<>();
private ArrayList<TLRPC.Updates> updatesQueuePts = new ArrayList<>();
private ArrayList<TLRPC.Updates> updatesQueueQts = new ArrayList<>();
2017-03-31 01:58:05 +02:00
private long updatesStartWaitTimeSeq;
private long updatesStartWaitTimePts;
private long updatesStartWaitTimeQts;
2019-05-14 14:08:05 +02:00
private SparseArray<TLRPC.UserFull> fullUsers = new SparseArray<>();
2019-06-04 12:14:50 +02:00
private SparseArray<TLRPC.ChatFull> fullChats = new SparseArray<>();
2015-01-02 23:15:07 +01:00
private ArrayList<Integer> loadingFullUsers = new ArrayList<>();
private ArrayList<Integer> loadedFullUsers = new ArrayList<>();
private ArrayList<Integer> loadingFullChats = new ArrayList<>();
2015-11-26 22:04:02 +01:00
private ArrayList<Integer> loadingFullParticipants = new ArrayList<>();
private ArrayList<Integer> loadedFullParticipants = new ArrayList<>();
2015-01-02 23:15:07 +01:00
private ArrayList<Integer> loadedFullChats = new ArrayList<>();
2018-07-30 04:07:02 +02:00
private SparseArray<ArrayList<Integer>> channelAdmins = new SparseArray<>();
2017-12-08 18:35:59 +01:00
private SparseIntArray loadingChannelAdmins = new SparseIntArray();
2013-10-25 17:19:00 +02:00
2019-01-23 18:03:33 +01:00
private SparseIntArray migratedChats = new SparseIntArray();
2016-03-06 02:49:31 +01:00
private HashMap<String, ArrayList<MessageObject>> reloadingWebpages = new HashMap<>();
2018-07-30 04:07:02 +02:00
private LongSparseArray<ArrayList<MessageObject>> reloadingWebpagesPending = new LongSparseArray<>();
2016-03-06 02:49:31 +01:00
2018-07-30 04:07:02 +02:00
private LongSparseArray<ArrayList<Integer>> reloadingMessages = new LongSparseArray<>();
private ArrayList<ReadTask> readTasks = new ArrayList<>();
private LongSparseArray<ReadTask> readTasksMap = new LongSparseArray<>();
2015-01-09 13:50:15 +01:00
2017-03-31 01:58:05 +02:00
private boolean gettingNewDeleteTask;
private int currentDeletingTaskTime;
private ArrayList<Integer> currentDeletingTaskMids;
private int currentDeletingTaskChannelId;
2017-03-31 01:58:05 +02:00
private Runnable currentDeleteTaskRunnable;
2013-10-25 17:19:00 +02:00
2019-05-14 14:08:05 +02:00
public boolean dialogsLoaded;
private SparseIntArray nextDialogsCacheOffset = new SparseIntArray();
private SparseBooleanArray loadingDialogs = new SparseBooleanArray();
private SparseBooleanArray dialogsEndReached = new SparseBooleanArray();
private SparseBooleanArray serverDialogsEndReached = new SparseBooleanArray();
2018-07-30 04:07:02 +02:00
private boolean loadingUnreadDialogs;
2017-03-31 01:58:05 +02:00
private boolean migratingDialogs;
public boolean gettingDifference;
2017-12-08 18:35:59 +01:00
private boolean getDifferenceFirstSync = true;
2017-03-31 01:58:05 +02:00
public boolean updatingState;
public boolean firstGettingTask;
public boolean registeringForPush;
2018-07-30 04:07:02 +02:00
private long lastPushRegisterSendTime;
2017-12-08 18:35:59 +01:00
private boolean resetingDialogs;
private TLRPC.TL_messages_peerDialogs resetDialogsPinned;
private TLRPC.messages_Dialogs resetDialogsAll;
2019-05-14 14:08:05 +02:00
private SparseIntArray loadingPinnedDialogs = new SparseIntArray();
2018-07-30 04:07:02 +02:00
private int loadingNotificationSettings;
2019-01-23 18:03:33 +01:00
private boolean loadingNotificationSignUpSettings;
2018-07-30 04:07:02 +02:00
private int nextProxyInfoCheckTime;
private boolean checkingProxyInfo;
private int checkingProxyInfoRequestId;
2019-07-18 15:01:39 +02:00
private int lastCheckProxyId;
2019-05-14 14:08:05 +02:00
private TLRPC.Dialog proxyDialog;
2018-07-30 04:07:02 +02:00
private boolean isLeftProxyChannel;
private long proxyDialogId;
2019-03-03 21:40:48 +01:00
private String proxyDialogAddress;
2018-07-30 04:07:02 +02:00
private boolean checkingTosUpdate;
private int nextTosCheckTime;
public int secretWebpagePreview;
public boolean suggestContacts = true;
private volatile static long lastThemeCheckTime;
2018-08-27 10:33:11 +02:00
private Runnable themeCheckRunnable = Theme::checkAutoNightThemeConditions;
2018-07-30 04:07:02 +02:00
private volatile static long lastPasswordCheckTime;
2019-07-18 15:01:39 +02:00
private Runnable passwordCheckRunnable = () -> getUserConfig().checkSavedPassword();
2016-03-06 02:49:31 +01:00
2017-03-31 01:58:05 +02:00
private long lastStatusUpdateTime;
private int statusRequest;
private int statusSettingState;
private boolean offlineSent;
private String uploadingAvatar;
2019-02-08 03:30:32 +01:00
private String uploadingWallpaper;
private boolean uploadingWallpaperBlurred;
private boolean uploadingWallpaperMotion;
2018-07-30 04:07:02 +02:00
public boolean enableJoined;
public String linkPrefix;
public int maxGroupCount;
2014-08-06 01:17:40 +02:00
public int maxBroadcastCount = 100;
2018-07-30 04:07:02 +02:00
public int maxMegagroupCount;
2015-11-26 22:04:02 +01:00
public int minGroupConvertSize = 200;
2018-07-30 04:07:02 +02:00
public int maxEditTime;
2016-05-25 23:49:47 +02:00
public int ratingDecay;
2018-07-30 04:07:02 +02:00
public int revokeTimeLimit;
public int revokeTimePmLimit;
public boolean canRevokePmInbox;
public int maxRecentStickersCount;
public int maxFaveStickersCount;
public int maxRecentGifsCount;
public int callReceiveTimeout;
public int callRingTimeout;
public int callConnectTimeout;
public int callPacketTimeout;
public int maxPinnedDialogsCount;
2019-05-14 14:08:05 +02:00
public int maxFolderPinnedDialogsCount;
2018-07-30 04:07:02 +02:00
public int mapProvider;
public int availableMapProviders;
public String mapKey;
public int maxMessageLength;
public int maxCaptionLength;
public boolean blockedCountry;
public boolean defaultP2pContacts;
public boolean preloadFeaturedStickers;
public String venueSearchBot;
public String gifSearchBot;
public String imageSearchBot;
public String dcDomainName;
public int webFileDatacenterId;
2019-01-23 18:03:33 +01:00
public String suggestedLangCode;
2017-12-08 18:35:59 +01:00
private String installReferer;
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
private SharedPreferences notificationsPreferences;
private SharedPreferences mainPreferences;
private SharedPreferences emojiPreferences;
private class UserActionUpdatesSeq extends TLRPC.Updates {
}
private class UserActionUpdatesPts extends TLRPC.Updates {
}
2013-10-25 17:19:00 +02:00
public static final int UPDATE_MASK_NAME = 1;
public static final int UPDATE_MASK_AVATAR = 2;
public static final int UPDATE_MASK_STATUS = 4;
public static final int UPDATE_MASK_CHAT_AVATAR = 8;
public static final int UPDATE_MASK_CHAT_NAME = 16;
public static final int UPDATE_MASK_CHAT_MEMBERS = 32;
public static final int UPDATE_MASK_USER_PRINT = 64;
public static final int UPDATE_MASK_USER_PHONE = 128;
public static final int UPDATE_MASK_READ_DIALOG_MESSAGE = 256;
2014-11-11 23:16:17 +01:00
public static final int UPDATE_MASK_SELECT_DIALOG = 512;
public static final int UPDATE_MASK_PHONE = 1024;
public static final int UPDATE_MASK_NEW_MESSAGE = 2048;
public static final int UPDATE_MASK_SEND_STATE = 4096;
2019-01-23 18:03:33 +01:00
public static final int UPDATE_MASK_CHAT = 8192;
//public static final int UPDATE_MASK_CHAT_ADMINS = 16384;
2018-07-30 04:07:02 +02:00
public static final int UPDATE_MASK_MESSAGE_TEXT = 32768;
2019-05-14 14:08:05 +02:00
public static final int UPDATE_MASK_CHECK = 65536;
public static final int UPDATE_MASK_REORDER = 131072;
public static final int UPDATE_MASK_ALL = UPDATE_MASK_AVATAR | UPDATE_MASK_STATUS | UPDATE_MASK_NAME | UPDATE_MASK_CHAT_AVATAR | UPDATE_MASK_CHAT_NAME | UPDATE_MASK_CHAT_MEMBERS | UPDATE_MASK_USER_PRINT | UPDATE_MASK_USER_PHONE | UPDATE_MASK_READ_DIALOG_MESSAGE | UPDATE_MASK_PHONE;
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
private class ReadTask {
public long dialogId;
public int maxId;
public int maxDate;
public long sendRequestTime;
}
2013-10-25 17:19:00 +02:00
public static class PrintingUser {
public long lastTime;
public int userId;
public TLRPC.SendMessageAction action;
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
private final Comparator<TLRPC.Dialog> dialogComparator = (dialog1, dialog2) -> {
if (dialog1 instanceof TLRPC.TL_dialogFolder && !(dialog2 instanceof TLRPC.TL_dialogFolder)) {
return -1;
} else if (!(dialog1 instanceof TLRPC.TL_dialogFolder) && dialog2 instanceof TLRPC.TL_dialogFolder) {
return 1;
} else if (!dialog1.pinned && dialog2.pinned) {
return 1;
} else if (dialog1.pinned && !dialog2.pinned) {
return -1;
} else if (dialog1.pinned && dialog2.pinned) {
if (dialog1.pinnedNum < dialog2.pinnedNum) {
return 1;
2019-07-18 15:01:39 +02:00
} else if (dialog1.pinnedNum > dialog2.pinnedNum) {
return -1;
2019-07-18 15:01:39 +02:00
} else {
return 0;
}
}
2019-07-18 15:01:39 +02:00
TLRPC.DraftMessage draftMessage = getMediaDataController().getDraft(dialog1.id);
int date1 = draftMessage != null && draftMessage.date >= dialog1.last_message_date ? draftMessage.date : dialog1.last_message_date;
draftMessage = getMediaDataController().getDraft(dialog2.id);
int date2 = draftMessage != null && draftMessage.date >= dialog2.last_message_date ? draftMessage.date : dialog2.last_message_date;
if (date1 < date2) {
return 1;
} else if (date1 > date2) {
return -1;
}
return 0;
};
2018-08-27 10:33:11 +02:00
private final Comparator<TLRPC.Update> updatesComparator = (lhs, rhs) -> {
int ltype = getUpdateType(lhs);
int rtype = getUpdateType(rhs);
if (ltype != rtype) {
return AndroidUtilities.compare(ltype, rtype);
} else if (ltype == 0) {
return AndroidUtilities.compare(getUpdatePts(lhs), getUpdatePts(rhs));
} else if (ltype == 1) {
return AndroidUtilities.compare(getUpdateQts(lhs), getUpdateQts(rhs));
} else if (ltype == 2) {
int lChannel = getUpdateChannelId(lhs);
int rChannel = getUpdateChannelId(rhs);
if (lChannel == rChannel) {
2018-07-30 04:07:02 +02:00
return AndroidUtilities.compare(getUpdatePts(lhs), getUpdatePts(rhs));
2018-08-27 10:33:11 +02:00
} else {
return AndroidUtilities.compare(lChannel, rChannel);
}
}
2018-08-27 10:33:11 +02:00
return 0;
};
2018-07-30 04:07:02 +02:00
private static volatile MessagesController[] Instance = new MessagesController[UserConfig.MAX_ACCOUNT_COUNT];
public static MessagesController getInstance(int num) {
MessagesController localInstance = Instance[num];
if (localInstance == null) {
synchronized (MessagesController.class) {
2018-07-30 04:07:02 +02:00
localInstance = Instance[num];
if (localInstance == null) {
2018-07-30 04:07:02 +02:00
Instance[num] = localInstance = new MessagesController(num);
}
}
}
return localInstance;
}
2018-07-30 04:07:02 +02:00
public static SharedPreferences getNotificationsSettings(int account) {
return getInstance(account).notificationsPreferences;
}
public static SharedPreferences getGlobalNotificationsSettings() {
return getInstance(0).notificationsPreferences;
}
public static SharedPreferences getMainSettings(int account) {
return getInstance(account).mainPreferences;
}
public static SharedPreferences getGlobalMainSettings() {
return getInstance(0).mainPreferences;
}
public static SharedPreferences getEmojiSettings(int account) {
return getInstance(account).emojiPreferences;
}
public static SharedPreferences getGlobalEmojiSettings() {
return getInstance(0).emojiPreferences;
}
public MessagesController(int num) {
2019-07-18 15:01:39 +02:00
super(num);
2018-07-30 04:07:02 +02:00
currentAccount = num;
ImageLoader.getInstance();
2019-07-18 15:01:39 +02:00
getMessagesStorage();
getLocationController();
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-07-18 15:01:39 +02:00
MessagesController messagesController = getMessagesController();
getNotificationCenter().addObserver(messagesController, NotificationCenter.FileDidUpload);
getNotificationCenter().addObserver(messagesController, NotificationCenter.FileDidFailUpload);
getNotificationCenter().addObserver(messagesController, NotificationCenter.fileDidLoad);
getNotificationCenter().addObserver(messagesController, NotificationCenter.fileDidFailedLoad);
getNotificationCenter().addObserver(messagesController, NotificationCenter.messageReceivedByServer);
getNotificationCenter().addObserver(messagesController, NotificationCenter.updateMessageMedia);
2017-12-08 18:35:59 +01:00
});
2013-10-25 17:19:00 +02:00
addSupportUser();
2018-07-30 04:07:02 +02:00
if (currentAccount == 0) {
notificationsPreferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
mainPreferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
emojiPreferences = ApplicationLoader.applicationContext.getSharedPreferences("emoji", Activity.MODE_PRIVATE);
} else {
notificationsPreferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications" + currentAccount, Activity.MODE_PRIVATE);
mainPreferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig" + currentAccount, Activity.MODE_PRIVATE);
emojiPreferences = ApplicationLoader.applicationContext.getSharedPreferences("emoji" + currentAccount, Activity.MODE_PRIVATE);
}
enableJoined = notificationsPreferences.getBoolean("EnableContactJoined", true);
secretWebpagePreview = mainPreferences.getInt("secretWebpage2", 2);
maxGroupCount = mainPreferences.getInt("maxGroupCount", 200);
maxMegagroupCount = mainPreferences.getInt("maxMegagroupCount", 10000);
maxRecentGifsCount = mainPreferences.getInt("maxRecentGifsCount", 200);
maxRecentStickersCount = mainPreferences.getInt("maxRecentStickersCount", 30);
maxFaveStickersCount = mainPreferences.getInt("maxFaveStickersCount", 5);
maxEditTime = mainPreferences.getInt("maxEditTime", 3600);
ratingDecay = mainPreferences.getInt("ratingDecay", 2419200);
linkPrefix = mainPreferences.getString("linkPrefix", "t.me");
callReceiveTimeout = mainPreferences.getInt("callReceiveTimeout", 20000);
callRingTimeout = mainPreferences.getInt("callRingTimeout", 90000);
callConnectTimeout = mainPreferences.getInt("callConnectTimeout", 30000);
callPacketTimeout = mainPreferences.getInt("callPacketTimeout", 10000);
maxPinnedDialogsCount = mainPreferences.getInt("maxPinnedDialogsCount", 5);
2019-05-14 14:08:05 +02:00
maxFolderPinnedDialogsCount = mainPreferences.getInt("maxFolderPinnedDialogsCount", 100);
2018-07-30 04:07:02 +02:00
maxMessageLength = mainPreferences.getInt("maxMessageLength", 4096);
2019-01-23 18:03:33 +01:00
maxCaptionLength = mainPreferences.getInt("maxCaptionLength", 1024);
2018-07-30 04:07:02 +02:00
mapProvider = mainPreferences.getInt("mapProvider", 0);
availableMapProviders = mainPreferences.getInt("availableMapProviders", 3);
mapKey = mainPreferences.getString("pk", null);
installReferer = mainPreferences.getString("installReferer", null);
defaultP2pContacts = mainPreferences.getBoolean("defaultP2pContacts", false);
revokeTimeLimit = mainPreferences.getInt("revokeTimeLimit", revokeTimeLimit);
revokeTimePmLimit = mainPreferences.getInt("revokeTimePmLimit", revokeTimePmLimit);
canRevokePmInbox = mainPreferences.getBoolean("canRevokePmInbox", canRevokePmInbox);
preloadFeaturedStickers = mainPreferences.getBoolean("preloadFeaturedStickers", false);
proxyDialogId = mainPreferences.getLong("proxy_dialog", 0);
2019-03-03 21:40:48 +01:00
proxyDialogAddress = mainPreferences.getString("proxyDialogAddress", null);
2018-07-30 04:07:02 +02:00
nextTosCheckTime = notificationsPreferences.getInt("nextTosCheckTime", 0);
venueSearchBot = mainPreferences.getString("venueSearchBot", "foursquare");
gifSearchBot = mainPreferences.getString("gifSearchBot", "gif");
imageSearchBot = mainPreferences.getString("imageSearchBot", "pic");
blockedCountry = mainPreferences.getBoolean("blockedCountry", false);
dcDomainName = mainPreferences.getString("dcDomainName", ConnectionsManager.native_isTestBackend(currentAccount) != 0 ? "tapv2.stel.com" : "apv2.stel.com");
webFileDatacenterId = mainPreferences.getInt("webFileDatacenterId", ConnectionsManager.native_isTestBackend(currentAccount) != 0 ? 2 : 4);
2019-01-23 18:03:33 +01:00
suggestedLangCode = mainPreferences.getString("suggestedLangCode", "en");
2013-10-25 17:19:00 +02:00
}
2014-08-06 01:17:40 +02:00
public void updateConfig(final TLRPC.TL_config config) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-07-18 15:01:39 +02:00
getDownloadController().loadAutoDownloadConfig(false);
2018-08-27 10:33:11 +02:00
maxMegagroupCount = config.megagroup_size_max;
maxGroupCount = config.chat_size_max;
maxEditTime = config.edit_time_limit;
ratingDecay = config.rating_e_decay;
maxRecentGifsCount = config.saved_gifs_limit;
maxRecentStickersCount = config.stickers_recent_limit;
maxFaveStickersCount = config.stickers_faved_limit;
revokeTimeLimit = config.revoke_time_limit;
revokeTimePmLimit = config.revoke_pm_time_limit;
canRevokePmInbox = config.revoke_pm_inbox;
linkPrefix = config.me_url_prefix;
if (linkPrefix.endsWith("/")) {
linkPrefix = linkPrefix.substring(0, linkPrefix.length() - 1);
}
if (linkPrefix.startsWith("https://")) {
linkPrefix = linkPrefix.substring(8);
} else if (linkPrefix.startsWith("http://")) {
linkPrefix = linkPrefix.substring(7);
}
callReceiveTimeout = config.call_receive_timeout_ms;
callRingTimeout = config.call_ring_timeout_ms;
callConnectTimeout = config.call_connect_timeout_ms;
callPacketTimeout = config.call_packet_timeout_ms;
maxPinnedDialogsCount = config.pinned_dialogs_count_max;
2019-05-14 14:08:05 +02:00
maxFolderPinnedDialogsCount = config.pinned_infolder_count_max;
2018-08-27 10:33:11 +02:00
maxMessageLength = config.message_length_max;
maxCaptionLength = config.caption_length_max;
defaultP2pContacts = config.default_p2p_contacts;
preloadFeaturedStickers = config.preload_featured_stickers;
if (config.venue_search_username != null) {
venueSearchBot = config.venue_search_username;
}
if (config.gif_search_username != null) {
gifSearchBot = config.gif_search_username;
}
if (imageSearchBot != null) {
imageSearchBot = config.img_search_username;
}
blockedCountry = config.blocked_mode;
dcDomainName = config.dc_txt_domain_name;
webFileDatacenterId = config.webfile_dc_id;
2019-06-04 12:14:50 +02:00
if (suggestedLangCode == null || !suggestedLangCode.equals(config.suggested_lang_code)) {
suggestedLangCode = config.suggested_lang_code;
LocaleController.getInstance().loadRemoteLanguages(currentAccount);
}
2018-08-27 10:33:11 +02:00
if (config.static_maps_provider == null) {
config.static_maps_provider = "google";
}
mapKey = null;
mapProvider = 0;
availableMapProviders = 0;
String[] providers = config.static_maps_provider.split(",");
for (int a = 0; a < providers.length; a++) {
String[] mapArgs = providers[a].split("\\+");
if (mapArgs.length > 0) {
String[] typeAndKey = mapArgs[0].split(":");
if (typeAndKey.length > 0) {
if ("yandex".equals(typeAndKey[0])) {
if (a == 0) {
if (mapArgs.length > 1) {
mapProvider = 3;
} else {
mapProvider = 1;
2018-07-30 04:07:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
availableMapProviders |= 4;
} else if ("google".equals(typeAndKey[0])) {
if (a == 0) {
if (mapArgs.length > 1) {
mapProvider = 4;
}
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
availableMapProviders |= 1;
} else if ("telegram".equals(typeAndKey[0])) {
if (a == 0) {
mapProvider = 2;
}
availableMapProviders |= 2;
}
if (typeAndKey.length > 1) {
mapKey = typeAndKey[1];
}
}
}
}
SharedPreferences.Editor editor = mainPreferences.edit();
editor.putInt("maxGroupCount", maxGroupCount);
editor.putInt("maxMegagroupCount", maxMegagroupCount);
editor.putInt("maxEditTime", maxEditTime);
editor.putInt("ratingDecay", ratingDecay);
editor.putInt("maxRecentGifsCount", maxRecentGifsCount);
editor.putInt("maxRecentStickersCount", maxRecentStickersCount);
editor.putInt("maxFaveStickersCount", maxFaveStickersCount);
editor.putInt("callReceiveTimeout", callReceiveTimeout);
editor.putInt("callRingTimeout", callRingTimeout);
editor.putInt("callConnectTimeout", callConnectTimeout);
editor.putInt("callPacketTimeout", callPacketTimeout);
editor.putString("linkPrefix", linkPrefix);
editor.putInt("maxPinnedDialogsCount", maxPinnedDialogsCount);
2019-05-14 14:08:05 +02:00
editor.putInt("maxFolderPinnedDialogsCount", maxFolderPinnedDialogsCount);
2018-08-27 10:33:11 +02:00
editor.putInt("maxMessageLength", maxMessageLength);
editor.putInt("maxCaptionLength", maxCaptionLength);
editor.putBoolean("defaultP2pContacts", defaultP2pContacts);
editor.putBoolean("preloadFeaturedStickers", preloadFeaturedStickers);
editor.putInt("revokeTimeLimit", revokeTimeLimit);
editor.putInt("revokeTimePmLimit", revokeTimePmLimit);
editor.putInt("mapProvider", mapProvider);
if (mapKey != null) {
editor.putString("pk", mapKey);
} else {
editor.remove("pk");
}
editor.putBoolean("canRevokePmInbox", canRevokePmInbox);
editor.putBoolean("blockedCountry", blockedCountry);
editor.putString("venueSearchBot", venueSearchBot);
editor.putString("gifSearchBot", gifSearchBot);
editor.putString("imageSearchBot", imageSearchBot);
editor.putString("dcDomainName", dcDomainName);
editor.putInt("webFileDatacenterId", webFileDatacenterId);
2019-01-23 18:03:33 +01:00
editor.putString("suggestedLangCode", suggestedLangCode);
2018-08-27 10:33:11 +02:00
editor.commit();
2014-08-06 01:17:40 +02:00
2019-01-23 18:03:33 +01:00
LocaleController.getInstance().checkUpdateForCurrentRemoteLocale(currentAccount, config.lang_pack_version, config.base_lang_pack_version);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.configLoaded);
2018-07-30 04:07:02 +02:00
});
2015-02-27 20:57:58 +01:00
}
2013-10-25 17:19:00 +02:00
public void addSupportUser() {
2015-06-29 19:12:11 +02:00
TLRPC.TL_userForeign_old2 user = new TLRPC.TL_userForeign_old2();
2013-10-25 17:19:00 +02:00
user.phone = "333";
user.id = 333000;
user.first_name = "Telegram";
user.last_name = "";
user.status = null;
user.photo = new TLRPC.TL_userProfilePhotoEmpty();
putUser(user, true);
2014-08-29 23:06:04 +02:00
2015-06-29 19:12:11 +02:00
user = new TLRPC.TL_userForeign_old2();
2014-08-29 23:06:04 +02:00
user.phone = "42777";
user.id = 777000;
2019-05-14 14:08:05 +02:00
user.verified = true;
2014-08-29 23:06:04 +02:00
user.first_name = "Telegram";
user.last_name = "Notifications";
user.status = null;
user.photo = new TLRPC.TL_userProfilePhotoEmpty();
putUser(user, true);
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
public TLRPC.InputUser getInputUser(TLRPC.User user) {
if (user == null) {
2015-09-24 22:52:02 +02:00
return new TLRPC.TL_inputUserEmpty();
}
2015-05-21 23:27:27 +02:00
TLRPC.InputUser inputUser;
2019-07-18 15:01:39 +02:00
if (user.id == getUserConfig().getClientUserId()) {
inputUser = new TLRPC.TL_inputUserSelf();
} else {
2015-09-02 00:14:21 +02:00
inputUser = new TLRPC.TL_inputUser();
inputUser.user_id = user.id;
2015-09-02 00:14:21 +02:00
inputUser.access_hash = user.access_hash;
}
return inputUser;
}
2018-07-30 04:07:02 +02:00
public TLRPC.InputUser getInputUser(int user_id) {
TLRPC.User user = getInstance(UserConfig.selectedAccount).getUser(user_id);
2015-09-24 22:52:02 +02:00
return getInputUser(user);
}
public static TLRPC.InputChannel getInputChannel(TLRPC.Chat chat) {
if (chat instanceof TLRPC.TL_channel || chat instanceof TLRPC.TL_channelForbidden) {
TLRPC.InputChannel inputChat = new TLRPC.TL_inputChannel();
inputChat.channel_id = chat.id;
inputChat.access_hash = chat.access_hash;
return inputChat;
} else {
return new TLRPC.TL_inputChannelEmpty();
}
}
2018-07-30 04:07:02 +02:00
public TLRPC.InputChannel getInputChannel(int chatId) {
return getInputChannel(getChat(chatId));
2015-09-24 22:52:02 +02:00
}
2018-07-30 04:07:02 +02:00
public TLRPC.InputPeer getInputPeer(int id) {
2015-09-24 22:52:02 +02:00
TLRPC.InputPeer inputPeer;
if (id < 0) {
2018-07-30 04:07:02 +02:00
TLRPC.Chat chat = getChat(-id);
2015-09-24 22:52:02 +02:00
if (ChatObject.isChannel(chat)) {
inputPeer = new TLRPC.TL_inputPeerChannel();
inputPeer.channel_id = -id;
inputPeer.access_hash = chat.access_hash;
} else {
inputPeer = new TLRPC.TL_inputPeerChat();
inputPeer.chat_id = -id;
}
} else {
2018-07-30 04:07:02 +02:00
TLRPC.User user = getUser(id);
2015-09-24 22:52:02 +02:00
inputPeer = new TLRPC.TL_inputPeerUser();
inputPeer.user_id = id;
if (user != null) {
inputPeer.access_hash = user.access_hash;
}
}
return inputPeer;
}
2018-07-30 04:07:02 +02:00
public TLRPC.Peer getPeer(int id) {
2015-09-24 22:52:02 +02:00
TLRPC.Peer inputPeer;
if (id < 0) {
2018-07-30 04:07:02 +02:00
TLRPC.Chat chat = getChat(-id);
2015-09-24 22:52:02 +02:00
if (chat instanceof TLRPC.TL_channel || chat instanceof TLRPC.TL_channelForbidden) {
inputPeer = new TLRPC.TL_peerChannel();
inputPeer.channel_id = -id;
} else {
inputPeer = new TLRPC.TL_peerChat();
inputPeer.chat_id = -id;
}
} else {
2018-07-30 04:07:02 +02:00
TLRPC.User user = getUser(id);
2015-09-24 22:52:02 +02:00
inputPeer = new TLRPC.TL_peerUser();
inputPeer.user_id = id;
}
return inputPeer;
}
2013-10-25 17:19:00 +02:00
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.FileDidUpload) {
2015-05-21 23:27:27 +02:00
final String location = (String) args[0];
final TLRPC.InputFile file = (TLRPC.InputFile) args[1];
if (uploadingAvatar != null && uploadingAvatar.equals(location)) {
TLRPC.TL_photos_uploadProfilePhoto req = new TLRPC.TL_photos_uploadProfilePhoto();
req.file = file;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
2019-07-18 15:01:39 +02:00
TLRPC.User user = getUser(getUserConfig().getClientUserId());
2018-08-27 10:33:11 +02:00
if (user == null) {
2019-07-18 15:01:39 +02:00
user = getUserConfig().getCurrentUser();
2018-08-27 10:33:11 +02:00
putUser(user, true);
} else {
2019-07-18 15:01:39 +02:00
getUserConfig().setCurrentUser(user);
2018-08-27 10:33:11 +02:00
}
if (user == null) {
return;
}
TLRPC.TL_photos_photo photo = (TLRPC.TL_photos_photo) response;
ArrayList<TLRPC.PhotoSize> sizes = photo.photo.sizes;
TLRPC.PhotoSize smallSize = FileLoader.getClosestPhotoSizeWithSize(sizes, 100);
TLRPC.PhotoSize bigSize = FileLoader.getClosestPhotoSizeWithSize(sizes, 1000);
user.photo = new TLRPC.TL_userProfilePhoto();
user.photo.photo_id = photo.photo.id;
if (smallSize != null) {
user.photo.photo_small = smallSize.location;
}
if (bigSize != null) {
user.photo.photo_big = bigSize.location;
} else if (smallSize != null) {
user.photo.photo_small = smallSize.location;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().clearUserPhotos(user.id);
2018-08-27 10:33:11 +02:00
ArrayList<TLRPC.User> users = new ArrayList<>();
users.add(user);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(users, null, false, true);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.mainUserInfoChanged);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_AVATAR);
getUserConfig().saveConfig(true);
2018-08-27 10:33:11 +02:00
});
}
});
2019-02-08 03:30:32 +01:00
} else if (uploadingWallpaper != null && uploadingWallpaper.equals(location)) {
TLRPC.TL_account_uploadWallPaper req = new TLRPC.TL_account_uploadWallPaper();
req.file = file;
req.mime_type = "image/jpeg";
final TLRPC.TL_wallPaperSettings settings = new TLRPC.TL_wallPaperSettings();
settings.blur = uploadingWallpaperBlurred;
settings.motion = uploadingWallpaperMotion;
req.settings = settings;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-02-08 03:30:32 +01:00
TLRPC.TL_wallPaper wallPaper = (TLRPC.TL_wallPaper) response;
File path = new File(ApplicationLoader.getFilesDirFixed(), uploadingWallpaperBlurred ? "wallpaper_original.jpg" : "wallpaper.jpg");
if (wallPaper != null) {
try {
AndroidUtilities.copyFile(path, FileLoader.getPathToAttach(wallPaper.document, true));
} catch (Exception ignore) {
}
}
AndroidUtilities.runOnUIThread(() -> {
if (uploadingWallpaper != null && wallPaper != null) {
wallPaper.settings = settings;
wallPaper.flags |= 4;
SharedPreferences preferences = getGlobalMainSettings();
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("selectedBackground2", wallPaper.id);
editor.commit();
ArrayList<TLRPC.WallPaper> wallpapers = new ArrayList<>();
wallpapers.add(wallPaper);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putWallpapers(wallpapers, 2);
2019-02-08 03:30:32 +01:00
TLRPC.PhotoSize image = FileLoader.getClosestPhotoSizeWithSize(wallPaper.document.thumbs, 320);
if (image != null) {
String newKey = image.location.volume_id + "_" + image.location.local_id + "@100_100";
String oldKey = Utilities.MD5(path.getAbsolutePath()) + "@100_100";
2019-05-14 14:08:05 +02:00
ImageLoader.getInstance().replaceImageInCache(oldKey, newKey, ImageLocation.getForDocument(image, wallPaper.document), false);
2019-02-08 03:30:32 +01:00
}
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.wallpapersNeedReload, wallPaper.id);
}
});
});
}
} else if (id == NotificationCenter.FileDidFailUpload) {
final String location = (String) args[0];
if (uploadingAvatar != null && uploadingAvatar.equals(location)) {
uploadingAvatar = null;
2019-02-08 03:30:32 +01:00
} else if (uploadingWallpaper != null && uploadingWallpaper.equals(location)) {
uploadingWallpaper = null;
}
} else if (id == NotificationCenter.messageReceivedByServer) {
2015-05-21 23:27:27 +02:00
Integer msgId = (Integer) args[0];
2015-10-29 18:10:07 +01:00
Integer newMsgId = (Integer) args[1];
2015-09-24 22:52:02 +02:00
Long did = (Long) args[3];
MessageObject obj = dialogMessage.get(did);
2017-12-08 18:35:59 +01:00
if (obj != null && (obj.getId() == msgId || obj.messageOwner.local_id == msgId)) {
2013-10-25 17:19:00 +02:00
obj.messageOwner.id = newMsgId;
obj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
2017-12-08 18:35:59 +01:00
}
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2017-12-08 18:35:59 +01:00
if (dialog != null && dialog.top_message == msgId) {
dialog.top_message = newMsgId;
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
}
2018-07-30 04:07:02 +02:00
obj = dialogMessagesByIds.get(msgId);
dialogMessagesByIds.remove(msgId);
2015-10-29 18:10:07 +01:00
if (obj != null) {
dialogMessagesByIds.put(newMsgId, obj);
}
} else if (id == NotificationCenter.updateMessageMedia) {
TLRPC.Message message = (TLRPC.Message) args[0];
MessageObject existMessageObject = dialogMessagesByIds.get(message.id);
if (existMessageObject != null) {
existMessageObject.messageOwner.media = message.media;
if (message.media.ttl_seconds != 0 && (message.media.photo instanceof TLRPC.TL_photoEmpty || message.media.document instanceof TLRPC.TL_documentEmpty)) {
existMessageObject.setType();
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.notificationsSettingsUpdated);
}
}
2013-10-25 17:19:00 +02:00
}
}
public void cleanup() {
2019-07-18 15:01:39 +02:00
getContactsController().cleanup();
MediaController.getInstance().cleanup();
2019-07-18 15:01:39 +02:00
getNotificationsController().cleanup();
getSendMessagesHelper().cleanup();
getSecretChatHelper().cleanup();
getLocationController().cleanup();
getMediaDataController().cleanup();
2018-07-30 04:07:02 +02:00
DialogsActivity.dialogsLoaded[currentAccount] = false;
SharedPreferences.Editor editor = notificationsPreferences.edit();
editor.clear().commit();
editor = emojiPreferences.edit();
editor.putLong("lastGifLoadTime", 0).putLong("lastStickersLoadTime", 0).putLong("lastStickersLoadTimeMask", 0).putLong("lastStickersLoadTimeFavs", 0).commit();
editor = mainPreferences.edit();
2019-05-14 14:08:05 +02:00
editor.remove("archivehint").remove("archivehint_l").remove("gifhint").remove("soundHint").remove("dcDomainName").remove("webFileDatacenterId").commit();
2016-03-06 02:49:31 +01:00
reloadingWebpages.clear();
reloadingWebpagesPending.clear();
2013-10-25 17:19:00 +02:00
dialogs_dict.clear();
2015-11-26 22:04:02 +01:00
dialogs_read_inbox_max.clear();
2019-05-14 14:08:05 +02:00
loadingPinnedDialogs.clear();
dialogs_read_outbox_max.clear();
exportedChats.clear();
2017-03-31 01:58:05 +02:00
fullUsers.clear();
2019-06-04 12:14:50 +02:00
fullChats.clear();
2019-05-14 14:08:05 +02:00
dialogsByFolder.clear();
2018-07-30 04:07:02 +02:00
unreadUnmutedDialogs = 0;
2015-10-29 18:10:07 +01:00
joiningToChannels.clear();
2019-01-23 18:03:33 +01:00
migratedChats.clear();
2015-09-24 22:52:02 +02:00
channelViewsToSend.clear();
2019-01-23 18:03:33 +01:00
pollsToCheck.clear();
pollsToCheckSize = 0;
2013-10-25 17:19:00 +02:00
dialogsServerOnly.clear();
2017-12-08 18:35:59 +01:00
dialogsForward.clear();
2019-05-14 14:08:05 +02:00
allDialogs.clear();
2019-03-03 21:40:48 +01:00
dialogsCanAddUsers.clear();
2019-02-08 03:30:32 +01:00
dialogsChannelsOnly.clear();
2019-03-03 21:40:48 +01:00
dialogsGroupsOnly.clear();
2019-02-08 03:30:32 +01:00
dialogsUsersOnly.clear();
2016-03-06 02:49:31 +01:00
dialogMessagesByIds.clear();
dialogMessagesByRandomIds.clear();
2017-12-08 18:35:59 +01:00
channelAdmins.clear();
loadingChannelAdmins.clear();
2013-10-25 17:19:00 +02:00
users.clear();
2017-07-08 18:32:04 +02:00
objectsByUsernames.clear();
2013-10-25 17:19:00 +02:00
chats.clear();
dialogMessage.clear();
2019-05-14 14:08:05 +02:00
deletedHistory.clear();
2013-10-25 17:19:00 +02:00
printingUsers.clear();
printingStrings.clear();
printingStringsTypes.clear();
2014-11-21 01:14:44 +01:00
onlinePrivacy.clear();
2016-03-16 13:26:32 +01:00
loadingPeerSettings.clear();
2019-01-23 18:03:33 +01:00
deletingDialogs.clear();
clearingHistoryDialogs.clear();
lastPrintingStringCount = 0;
2019-01-23 18:03:33 +01:00
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
readTasks.clear();
readTasksMap.clear();
updatesQueueSeq.clear();
updatesQueuePts.clear();
updatesQueueQts.clear();
gettingUnknownChannels.clear();
2019-05-14 14:08:05 +02:00
gettingUnknownDialogs.clear();
2018-08-27 10:33:11 +02:00
updatesStartWaitTimeSeq = 0;
updatesStartWaitTimePts = 0;
updatesStartWaitTimeQts = 0;
createdDialogIds.clear();
gettingDifference = false;
resetDialogsPinned = null;
resetDialogsAll = null;
});
2017-07-08 18:32:04 +02:00
createdDialogMainThreadIds.clear();
2018-07-30 04:07:02 +02:00
visibleDialogMainThreadIds.clear();
blockedUsers.clear();
2014-08-23 01:22:33 +02:00
sendingTypings.clear();
loadingFullUsers.clear();
loadedFullUsers.clear();
2015-01-09 13:50:15 +01:00
reloadingMessages.clear();
2015-02-01 19:51:02 +01:00
loadingFullChats.clear();
2015-11-26 22:04:02 +01:00
loadingFullParticipants.clear();
loadedFullParticipants.clear();
2015-02-01 19:51:02 +01:00
loadedFullChats.clear();
2013-10-25 17:19:00 +02:00
2019-05-14 14:08:05 +02:00
dialogsLoaded = false;
nextDialogsCacheOffset.clear();
loadingDialogs.clear();
dialogsEndReached.clear();
serverDialogsEndReached.clear();
2018-07-30 04:07:02 +02:00
checkingTosUpdate = false;
nextTosCheckTime = 0;
nextProxyInfoCheckTime = 0;
checkingProxyInfo = false;
loadingUnreadDialogs = false;
2013-10-25 17:19:00 +02:00
currentDeletingTaskTime = 0;
currentDeletingTaskMids = null;
currentDeletingTaskChannelId = 0;
2013-10-25 17:19:00 +02:00
gettingNewDeleteTask = false;
loadingBlockedUsers = false;
2013-10-25 17:19:00 +02:00
firstGettingTask = false;
updatingState = false;
2017-12-08 18:35:59 +01:00
resetingDialogs = false;
2013-10-25 17:19:00 +02:00
lastStatusUpdateTime = 0;
offlineSent = false;
2013-10-25 17:19:00 +02:00
registeringForPush = false;
2017-12-08 18:35:59 +01:00
getDifferenceFirstSync = true;
2013-10-25 17:19:00 +02:00
uploadingAvatar = null;
2019-02-08 03:30:32 +01:00
uploadingWallpaper = null;
statusRequest = 0;
statusSettingState = 0;
2014-10-14 10:13:16 +02:00
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
2019-07-18 15:01:39 +02:00
getConnectionsManager().setIsUpdating(false);
2018-08-27 10:33:11 +02:00
updatesQueueChannels.clear();
updatesStartWaitTimeChannels.clear();
gettingDifferenceChannels.clear();
channelsPts.clear();
shortPollChannels.clear();
needShortPollChannels.clear();
2019-01-23 18:03:33 +01:00
shortPollOnlines.clear();
needShortPollOnlines.clear();
2015-09-24 22:52:02 +02:00
});
2014-10-14 10:13:16 +02:00
if (currentDeleteTaskRunnable != null) {
Utilities.stageQueue.cancelRunnable(currentDeleteTaskRunnable);
currentDeleteTaskRunnable = null;
}
2013-10-25 17:19:00 +02:00
addSupportUser();
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2013-10-25 17:19:00 +02:00
}
public TLRPC.User getUser(Integer id) {
return users.get(id);
}
2017-07-08 18:32:04 +02:00
public TLObject getUserOrChat(String username) {
2015-05-21 23:27:27 +02:00
if (username == null || username.length() == 0) {
return null;
}
2017-07-08 18:32:04 +02:00
return objectsByUsernames.get(username.toLowerCase());
}
public ConcurrentHashMap<Integer, TLRPC.User> getUsers() {
return users;
}
2019-07-18 15:01:39 +02:00
public ConcurrentHashMap<Integer, TLRPC.Chat> getChats() {
return chats;
}
public TLRPC.Chat getChat(Integer id) {
return chats.get(id);
}
public TLRPC.EncryptedChat getEncryptedChat(Integer id) {
return encryptedChats.get(id);
}
2017-03-31 01:58:05 +02:00
public TLRPC.EncryptedChat getEncryptedChatDB(int chat_id, boolean created) {
TLRPC.EncryptedChat chat = encryptedChats.get(chat_id);
2017-03-31 01:58:05 +02:00
if (chat == null || created && (chat instanceof TLRPC.TL_encryptedChatWaiting || chat instanceof TLRPC.TL_encryptedChatRequested)) {
2018-07-30 04:07:02 +02:00
CountDownLatch countDownLatch = new CountDownLatch(1);
2015-01-02 23:15:07 +01:00
ArrayList<TLObject> result = new ArrayList<>();
2019-07-18 15:01:39 +02:00
getMessagesStorage().getEncryptedChat(chat_id, countDownLatch, result);
try {
2018-07-30 04:07:02 +02:00
countDownLatch.await();
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
if (result.size() == 2) {
2015-05-21 23:27:27 +02:00
chat = (TLRPC.EncryptedChat) result.get(0);
TLRPC.User user = (TLRPC.User) result.get(1);
putEncryptedChat(chat, false);
putUser(user, true);
}
}
return chat;
}
2017-07-08 18:32:04 +02:00
public boolean isDialogCreated(long dialog_id) {
return createdDialogMainThreadIds.contains(dialog_id);
}
2018-07-30 04:07:02 +02:00
public boolean isDialogVisible(long dialog_id) {
return visibleDialogMainThreadIds.contains(dialog_id);
}
public void setLastVisibleDialogId(final long dialog_id, final boolean set) {
if (set) {
if (visibleDialogMainThreadIds.contains(dialog_id)) {
return;
}
visibleDialogMainThreadIds.add(dialog_id);
} else {
visibleDialogMainThreadIds.remove(dialog_id);
}
}
2019-01-23 18:03:33 +01:00
public void setLastCreatedDialogId(final long dialogId, final boolean set) {
2017-07-08 18:32:04 +02:00
if (set) {
2019-01-23 18:03:33 +01:00
if (createdDialogMainThreadIds.contains(dialogId)) {
2018-07-30 04:07:02 +02:00
return;
}
2019-01-23 18:03:33 +01:00
createdDialogMainThreadIds.add(dialogId);
2017-07-08 18:32:04 +02:00
} else {
2019-01-23 18:03:33 +01:00
createdDialogMainThreadIds.remove(dialogId);
SparseArray<MessageObject> array = pollsToCheck.get(dialogId);
if (array != null) {
for (int a = 0, N = array.size(); a < N; a++) {
MessageObject object = array.valueAt(a);
object.pollVisibleOnScreen = false;
}
}
2017-07-08 18:32:04 +02:00
}
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
if (set) {
2019-01-23 18:03:33 +01:00
if (createdDialogIds.contains(dialogId)) {
2018-08-27 10:33:11 +02:00
return;
2016-03-06 02:49:31 +01:00
}
2019-01-23 18:03:33 +01:00
createdDialogIds.add(dialogId);
2018-08-27 10:33:11 +02:00
} else {
2019-01-23 18:03:33 +01:00
createdDialogIds.remove(dialogId);
2016-03-06 02:49:31 +01:00
}
});
}
public TLRPC.ExportedChatInvite getExportedInvite(int chat_id) {
return exportedChats.get(chat_id);
}
public boolean putUser(TLRPC.User user, boolean fromCache) {
if (user == null) {
return false;
}
2014-11-17 03:44:57 +01:00
fromCache = fromCache && user.id / 1000 != 333 && user.id != 777000;
TLRPC.User oldUser = users.get(user.id);
2017-12-08 18:35:59 +01:00
if (oldUser == user) {
return false;
}
2017-03-31 01:58:05 +02:00
if (oldUser != null && !TextUtils.isEmpty(oldUser.username)) {
2017-07-08 18:32:04 +02:00
objectsByUsernames.remove(oldUser.username.toLowerCase());
}
2017-03-31 01:58:05 +02:00
if (!TextUtils.isEmpty(user.username)) {
2017-07-08 18:32:04 +02:00
objectsByUsernames.put(user.username.toLowerCase(), user);
}
2016-03-06 02:49:31 +01:00
if (user.min) {
if (oldUser != null) {
if (!fromCache) {
2017-12-08 18:35:59 +01:00
if (user.bot) {
if (user.username != null) {
oldUser.username = user.username;
oldUser.flags |= 8;
} else {
oldUser.flags = oldUser.flags & ~8;
oldUser.username = null;
}
2016-03-16 13:26:32 +01:00
}
2016-03-06 02:49:31 +01:00
if (user.photo != null) {
oldUser.photo = user.photo;
oldUser.flags |= 32;
} else {
oldUser.flags = oldUser.flags &~ 32;
2017-12-08 18:35:59 +01:00
oldUser.photo = null;
2016-03-06 02:49:31 +01:00
}
}
} else {
users.put(user.id, user);
}
2016-03-06 02:49:31 +01:00
} else {
if (!fromCache) {
users.put(user.id, user);
2019-07-18 15:01:39 +02:00
if (user.id == getUserConfig().getClientUserId()) {
getUserConfig().setCurrentUser(user);
getUserConfig().saveConfig(true);
2016-03-06 02:49:31 +01:00
}
if (oldUser != null && user.status != null && oldUser.status != null && user.status.expires != oldUser.status.expires) {
return true;
}
} else if (oldUser == null) {
users.put(user.id, user);
2016-03-16 13:26:32 +01:00
} else if (oldUser.min) {
user.min = false;
2017-12-08 18:35:59 +01:00
if (oldUser.bot) {
if (oldUser.username != null) {
user.username = oldUser.username;
user.flags |= 8;
} else {
user.flags = user.flags & ~8;
user.username = null;
}
2016-03-16 13:26:32 +01:00
}
if (oldUser.photo != null) {
user.photo = oldUser.photo;
user.flags |= 32;
} else {
user.flags = user.flags &~ 32;
2017-12-08 18:35:59 +01:00
user.photo = null;
2016-03-16 13:26:32 +01:00
}
users.put(user.id, user);
}
}
return false;
}
public void putUsers(ArrayList<TLRPC.User> users, boolean fromCache) {
if (users == null || users.isEmpty()) {
return;
}
boolean updateStatus = false;
2015-09-02 00:14:21 +02:00
int count = users.size();
for (int a = 0; a < count; a++) {
TLRPC.User user = users.get(a);
if (putUser(user, fromCache)) {
updateStatus = true;
}
}
if (updateStatus) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS));
}
}
2017-07-08 18:32:04 +02:00
public void putChat(final TLRPC.Chat chat, boolean fromCache) {
if (chat == null) {
return;
}
2015-09-02 00:14:21 +02:00
TLRPC.Chat oldChat = chats.get(chat.id);
2017-12-08 18:35:59 +01:00
if (oldChat == chat) {
return;
}
2017-07-08 18:32:04 +02:00
if (oldChat != null && !TextUtils.isEmpty(oldChat.username)) {
objectsByUsernames.remove(oldChat.username.toLowerCase());
}
if (!TextUtils.isEmpty(chat.username)) {
objectsByUsernames.put(chat.username.toLowerCase(), chat);
}
2016-03-16 13:26:32 +01:00
if (chat.min) {
if (oldChat != null) {
if (!fromCache) {
oldChat.title = chat.title;
oldChat.photo = chat.photo;
oldChat.broadcast = chat.broadcast;
oldChat.verified = chat.verified;
oldChat.megagroup = chat.megagroup;
2019-01-23 18:03:33 +01:00
if (chat.default_banned_rights != null) {
oldChat.default_banned_rights = chat.default_banned_rights;
oldChat.flags |= 262144;
}
if (chat.admin_rights != null) {
oldChat.admin_rights = chat.admin_rights;
oldChat.flags |= 16384;
}
if (chat.banned_rights != null) {
oldChat.banned_rights = chat.banned_rights;
oldChat.flags |= 32768;
}
2016-03-16 13:26:32 +01:00
if (chat.username != null) {
oldChat.username = chat.username;
oldChat.flags |= 64;
} else {
oldChat.flags = oldChat.flags &~ 64;
2017-12-08 18:35:59 +01:00
oldChat.username = null;
}
if (chat.participants_count != 0) {
oldChat.participants_count = chat.participants_count;
2016-03-16 13:26:32 +01:00
}
}
} else {
chats.put(chat.id, chat);
}
} else {
if (!fromCache) {
2017-07-08 18:32:04 +02:00
if (oldChat != null) {
if (chat.version != oldChat.version) {
loadedFullChats.remove((Integer) chat.id);
}
2017-12-08 18:35:59 +01:00
if (oldChat.participants_count != 0 && chat.participants_count == 0) {
chat.participants_count = oldChat.participants_count;
chat.flags |= 131072;
}
2019-01-23 18:03:33 +01:00
2017-07-08 18:32:04 +02:00
int oldFlags = oldChat.banned_rights != null ? oldChat.banned_rights.flags : 0;
int newFlags = chat.banned_rights != null ? chat.banned_rights.flags : 0;
2019-01-23 18:03:33 +01:00
int oldFlags2 = oldChat.default_banned_rights != null ? oldChat.default_banned_rights.flags : 0;
int newFlags2 = chat.default_banned_rights != null ? chat.default_banned_rights.flags : 0;
oldChat.default_banned_rights = chat.default_banned_rights;
if (oldChat.default_banned_rights == null) {
oldChat.flags &=~ 262144;
} else {
oldChat.flags |= 262144;
}
oldChat.banned_rights = chat.banned_rights;
if (oldChat.banned_rights == null) {
oldChat.flags &=~ 32768;
} else {
oldChat.flags |= 32768;
}
oldChat.admin_rights = chat.admin_rights;
if (oldChat.admin_rights == null) {
oldChat.flags &=~ 16384;
} else {
oldChat.flags |= 16384;
}
if (oldFlags != newFlags || oldFlags2 != newFlags2) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.channelRightsUpdated, chat));
2017-07-08 18:32:04 +02:00
}
2016-03-16 13:26:32 +01:00
}
chats.put(chat.id, chat);
} else if (oldChat == null) {
chats.put(chat.id, chat);
} else if (oldChat.min) {
chat.min = false;
chat.title = oldChat.title;
chat.photo = oldChat.photo;
chat.broadcast = oldChat.broadcast;
chat.verified = oldChat.verified;
chat.megagroup = oldChat.megagroup;
2019-01-23 18:03:33 +01:00
if (oldChat.default_banned_rights != null) {
chat.default_banned_rights = oldChat.default_banned_rights;
chat.flags |= 262144;
}
if (oldChat.admin_rights != null) {
chat.admin_rights = oldChat.admin_rights;
chat.flags |= 16384;
}
if (oldChat.banned_rights != null) {
chat.banned_rights = oldChat.banned_rights;
chat.flags |= 32768;
}
2016-03-16 13:26:32 +01:00
if (oldChat.username != null) {
chat.username = oldChat.username;
chat.flags |= 64;
} else {
chat.flags = chat.flags &~ 64;
2017-12-08 18:35:59 +01:00
chat.username = null;
}
if (oldChat.participants_count != 0 && chat.participants_count == 0) {
chat.participants_count = oldChat.participants_count;
chat.flags |= 131072;
2016-03-16 13:26:32 +01:00
}
chats.put(chat.id, chat);
2015-09-02 00:14:21 +02:00
}
}
}
public void putChats(ArrayList<TLRPC.Chat> chats, boolean fromCache) {
if (chats == null || chats.isEmpty()) {
return;
}
2015-09-02 00:14:21 +02:00
int count = chats.size();
for (int a = 0; a < count; a++) {
TLRPC.Chat chat = chats.get(a);
putChat(chat, fromCache);
}
}
2017-12-08 18:35:59 +01:00
public void setReferer(String referer) {
if (referer == null) {
return;
}
installReferer = referer;
2018-07-30 04:07:02 +02:00
mainPreferences.edit().putString("installReferer", referer).commit();
2017-12-08 18:35:59 +01:00
}
public void putEncryptedChat(TLRPC.EncryptedChat encryptedChat, boolean fromCache) {
if (encryptedChat == null) {
return;
}
if (fromCache) {
encryptedChats.putIfAbsent(encryptedChat.id, encryptedChat);
} else {
encryptedChats.put(encryptedChat.id, encryptedChat);
}
}
public void putEncryptedChats(ArrayList<TLRPC.EncryptedChat> encryptedChats, boolean fromCache) {
if (encryptedChats == null || encryptedChats.isEmpty()) {
return;
}
2015-09-02 00:14:21 +02:00
int count = encryptedChats.size();
for (int a = 0; a < count; a++) {
TLRPC.EncryptedChat encryptedChat = encryptedChats.get(a);
putEncryptedChat(encryptedChat, fromCache);
}
}
2019-05-14 14:08:05 +02:00
public TLRPC.UserFull getUserFull(int uid) {
2017-03-31 01:58:05 +02:00
return fullUsers.get(uid);
2016-03-16 13:26:32 +01:00
}
2019-06-04 12:14:50 +02:00
public TLRPC.ChatFull getChatFull(int chatId) {
return fullChats.get(chatId);
}
public void cancelLoadFullUser(int uid) {
loadingFullUsers.remove((Integer) uid);
}
2014-11-19 16:17:24 +01:00
public void cancelLoadFullChat(int cid) {
loadingFullChats.remove((Integer) cid);
}
protected void clearFullUsers() {
loadedFullUsers.clear();
loadedFullChats.clear();
}
2019-05-14 14:08:05 +02:00
private void reloadDialogsReadValue(ArrayList<TLRPC.Dialog> dialogs, long did) {
2017-03-31 01:58:05 +02:00
if (did == 0 && (dialogs == null || dialogs.isEmpty())) {
return;
}
TLRPC.TL_messages_getPeerDialogs req = new TLRPC.TL_messages_getPeerDialogs();
if (dialogs != null) {
for (int a = 0; a < dialogs.size(); a++) {
2017-03-31 01:58:05 +02:00
TLRPC.InputPeer inputPeer = getInputPeer((int) dialogs.get(a).id);
if (inputPeer instanceof TLRPC.TL_inputPeerChannel && inputPeer.access_hash == 0) {
continue;
}
2018-07-30 04:07:02 +02:00
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = inputPeer;
req.peers.add(inputDialogPeer);
}
} else {
2017-03-31 01:58:05 +02:00
TLRPC.InputPeer inputPeer = getInputPeer((int) did);
if (inputPeer instanceof TLRPC.TL_inputPeerChannel && inputPeer.access_hash == 0) {
return;
}
2018-07-30 04:07:02 +02:00
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = inputPeer;
req.peers.add(inputDialogPeer);
2017-03-31 01:58:05 +02:00
}
if (req.peers.isEmpty()) {
return;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
TLRPC.TL_messages_peerDialogs res = (TLRPC.TL_messages_peerDialogs) response;
ArrayList<TLRPC.Update> arrayList = new ArrayList<>();
for (int a = 0; a < res.dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = res.dialogs.get(a);
2018-08-27 10:33:11 +02:00
if (dialog.read_inbox_max_id == 0) {
dialog.read_inbox_max_id = 1;
}
if (dialog.read_outbox_max_id == 0) {
dialog.read_outbox_max_id = 1;
}
2019-05-14 14:08:05 +02:00
DialogObject.initDialog(dialog);
2018-08-27 10:33:11 +02:00
Integer value = dialogs_read_inbox_max.get(dialog.id);
if (value == null) {
value = 0;
}
dialogs_read_inbox_max.put(dialog.id, Math.max(dialog.read_inbox_max_id, value));
if (value == 0) {
if (dialog.peer.channel_id != 0) {
TLRPC.TL_updateReadChannelInbox update = new TLRPC.TL_updateReadChannelInbox();
update.channel_id = dialog.peer.channel_id;
update.max_id = dialog.read_inbox_max_id;
arrayList.add(update);
} else {
TLRPC.TL_updateReadHistoryInbox update = new TLRPC.TL_updateReadHistoryInbox();
update.peer = dialog.peer;
update.max_id = dialog.read_inbox_max_id;
arrayList.add(update);
}
2018-08-27 10:33:11 +02:00
}
2018-08-27 10:33:11 +02:00
value = dialogs_read_outbox_max.get(dialog.id);
if (value == null) {
value = 0;
}
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(dialog.id, Math.max(dialog.read_outbox_max_id, value));
if (value == 0) {
if (dialog.peer.channel_id != 0) {
TLRPC.TL_updateReadChannelOutbox update = new TLRPC.TL_updateReadChannelOutbox();
update.channel_id = dialog.peer.channel_id;
update.max_id = dialog.read_outbox_max_id;
arrayList.add(update);
} else {
TLRPC.TL_updateReadHistoryOutbox update = new TLRPC.TL_updateReadHistoryOutbox();
update.peer = dialog.peer;
update.max_id = dialog.read_outbox_max_id;
arrayList.add(update);
}
}
}
2018-08-27 10:33:11 +02:00
if (!arrayList.isEmpty()) {
2019-07-18 15:01:39 +02:00
processUpdateArray(arrayList, null, null, false, 0);
2018-08-27 10:33:11 +02:00
}
}
});
}
2017-12-08 18:35:59 +01:00
public boolean isChannelAdmin(int chatId, int uid) {
ArrayList<Integer> array = channelAdmins.get(chatId);
return array != null && array.indexOf(uid) >= 0;
}
public void loadChannelAdmins(final int chatId, final boolean cache) {
if (loadingChannelAdmins.indexOfKey(chatId) >= 0) {
return;
}
loadingChannelAdmins.put(chatId, 0);
if (cache) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().loadChannelAdmins(chatId);
2017-12-08 18:35:59 +01:00
} else {
TLRPC.TL_channels_getParticipants req = new TLRPC.TL_channels_getParticipants();
ArrayList<Integer> array = channelAdmins.get(chatId);
if (array != null) {
long acc = 0;
for (int a = 0; a < array.size(); a++) {
acc = ((acc * 20261) + 0x80000000L + array.get(a)) % 0x80000000L;
}
req.hash = (int) acc;
}
req.channel = getInputChannel(chatId);
req.limit = 100;
req.filter = new TLRPC.TL_channelParticipantsAdmins();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response instanceof TLRPC.TL_channels_channelParticipants) {
TLRPC.TL_channels_channelParticipants participants = (TLRPC.TL_channels_channelParticipants) response;
final ArrayList<Integer> array1 = new ArrayList<>(participants.participants.size());
for (int a = 0; a < participants.participants.size(); a++) {
array1.add(participants.participants.get(a).user_id);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
processLoadedChannelAdmins(array1, chatId, false);
2017-12-08 18:35:59 +01:00
}
});
}
}
public void processLoadedChannelAdmins(final ArrayList<Integer> array, final int chatId, final boolean cache) {
Collections.sort(array);
if (!cache) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putChannelAdmins(chatId, array);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
loadingChannelAdmins.delete(chatId);
channelAdmins.put(chatId, array);
if (cache) {
loadChannelAdmins(chatId, false);
2017-12-08 18:35:59 +01:00
}
});
}
public void loadFullChat(final int chat_id, final int classGuid, boolean force) {
2017-12-08 18:35:59 +01:00
boolean loaded = loadedFullChats.contains(chat_id);
if (loadingFullChats.contains(chat_id) || !force && loaded) {
2014-11-19 16:17:24 +01:00
return;
}
loadingFullChats.add(chat_id);
2015-09-24 22:52:02 +02:00
TLObject request;
2017-03-31 01:58:05 +02:00
final long dialog_id = -chat_id;
2015-11-26 22:04:02 +01:00
final TLRPC.Chat chat = getChat(chat_id);
2017-12-08 18:35:59 +01:00
if (ChatObject.isChannel(chat)) {
2015-09-24 22:52:02 +02:00
TLRPC.TL_channels_getFullChannel req = new TLRPC.TL_channels_getFullChannel();
2017-12-08 18:35:59 +01:00
req.channel = getInputChannel(chat);
2015-09-24 22:52:02 +02:00
request = req;
2017-12-08 18:35:59 +01:00
if (chat.megagroup) {
loadChannelAdmins(chat_id, !loaded);
}
2015-09-24 22:52:02 +02:00
} else {
TLRPC.TL_messages_getFullChat req = new TLRPC.TL_messages_getFullChat();
req.chat_id = chat_id;
request = req;
2017-03-31 01:58:05 +02:00
if (dialogs_read_inbox_max.get(dialog_id) == null || dialogs_read_outbox_max.get(dialog_id) == null) {
reloadDialogsReadValue(null, dialog_id);
}
2015-09-24 22:52:02 +02:00
}
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(request, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
final TLRPC.TL_messages_chatFull res = (TLRPC.TL_messages_chatFull) response;
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, res.chats, true, true);
getMessagesStorage().updateChatInfo(res.full_chat, false);
2018-08-27 10:33:11 +02:00
if (ChatObject.isChannel(chat)) {
Integer value = dialogs_read_inbox_max.get(dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-08-27 10:33:11 +02:00
}
2018-08-27 10:33:11 +02:00
dialogs_read_inbox_max.put(dialog_id, Math.max(res.full_chat.read_inbox_max_id, value));
if (value == 0) {
ArrayList<TLRPC.Update> arrayList = new ArrayList<>();
TLRPC.TL_updateReadChannelInbox update = new TLRPC.TL_updateReadChannelInbox();
update.channel_id = chat_id;
update.max_id = res.full_chat.read_inbox_max_id;
arrayList.add(update);
2019-07-18 15:01:39 +02:00
processUpdateArray(arrayList, null, null, false, 0);
2018-08-27 10:33:11 +02:00
}
2018-08-27 10:33:11 +02:00
value = dialogs_read_outbox_max.get(dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(true, dialog_id);
2018-08-27 10:33:11 +02:00
}
dialogs_read_outbox_max.put(dialog_id, Math.max(res.full_chat.read_outbox_max_id, value));
if (value == 0) {
ArrayList<TLRPC.Update> arrayList = new ArrayList<>();
TLRPC.TL_updateReadChannelOutbox update = new TLRPC.TL_updateReadChannelOutbox();
update.channel_id = chat_id;
update.max_id = res.full_chat.read_outbox_max_id;
arrayList.add(update);
2019-07-18 15:01:39 +02:00
processUpdateArray(arrayList, null, null, false, 0);
}
2018-08-27 10:33:11 +02:00
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-06-04 12:14:50 +02:00
fullChats.put(chat_id, res.full_chat);
2018-08-27 10:33:11 +02:00
applyDialogNotificationsSettings(-chat_id, res.full_chat.notify_settings);
for (int a = 0; a < res.full_chat.bot_info.size(); a++) {
TLRPC.BotInfo botInfo = res.full_chat.bot_info.get(a);
2019-07-18 15:01:39 +02:00
getMediaDataController().putBotInfo(botInfo);
2018-08-27 10:33:11 +02:00
}
exportedChats.put(chat_id, res.full_chat.exported_invite);
loadingFullChats.remove((Integer) chat_id);
loadedFullChats.add(chat_id);
2014-11-19 16:17:24 +01:00
2018-08-27 10:33:11 +02:00
putUsers(res.users, false);
putChats(res.chats, false);
if (res.full_chat.stickerset != null) {
2019-07-18 15:01:39 +02:00
getMediaDataController().getGroupStickerSetById(res.full_chat.stickerset);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoDidLoad, res.full_chat, classGuid, false, null);
2018-08-27 10:33:11 +02:00
});
} else {
AndroidUtilities.runOnUIThread(() -> {
checkChannelError(error.text, chat_id);
loadingFullChats.remove((Integer) chat_id);
});
2014-11-19 16:17:24 +01:00
}
});
if (classGuid != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
2014-11-19 16:17:24 +01:00
}
}
2016-03-16 13:26:32 +01:00
public void loadFullUser(final TLRPC.User user, final int classGuid, boolean force) {
if (user == null || loadingFullUsers.contains(user.id) || !force && loadedFullUsers.contains(user.id)) {
return;
}
loadingFullUsers.add(user.id);
TLRPC.TL_users_getFullUser req = new TLRPC.TL_users_getFullUser();
req.id = getInputUser(user);
2017-03-31 01:58:05 +02:00
long dialog_id = user.id;
if (dialogs_read_inbox_max.get(dialog_id) == null || dialogs_read_outbox_max.get(dialog_id) == null) {
reloadDialogsReadValue(null, dialog_id);
}
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
2019-05-14 14:08:05 +02:00
TLRPC.UserFull userFull = (TLRPC.UserFull) response;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateUserInfo(userFull, false);
2019-01-23 18:03:33 +01:00
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-07-18 15:01:39 +02:00
savePeerSettings(userFull.user.id, userFull.settings, false);
2018-08-27 10:33:11 +02:00
applyDialogNotificationsSettings(user.id, userFull.notify_settings);
if (userFull.bot_info instanceof TLRPC.TL_botInfo) {
2019-07-18 15:01:39 +02:00
getMediaDataController().putBotInfo(userFull.bot_info);
2018-08-27 10:33:11 +02:00
}
int index = blockedUsers.indexOfKey(user.id);
if (userFull.blocked) {
if (index < 0) {
SparseIntArray ids = new SparseIntArray();
ids.put(user.id, 1);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putBlockedUsers(ids, false);
2018-08-27 10:33:11 +02:00
blockedUsers.put(user.id, 1);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.blockedUsersDidLoad);
}
2018-08-27 10:33:11 +02:00
} else {
if (index >= 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().deleteBlockedUser(user.id);
2018-08-27 10:33:11 +02:00
blockedUsers.removeAt(index);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.blockedUsersDidLoad);
}
2018-08-27 10:33:11 +02:00
}
fullUsers.put(user.id, userFull);
loadingFullUsers.remove((Integer) user.id);
loadedFullUsers.add(user.id);
String names = user.first_name + user.last_name + user.username;
ArrayList<TLRPC.User> users = new ArrayList<>();
users.add(userFull.user);
putUsers(users, false);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(users, null, false, true);
2018-08-27 10:33:11 +02:00
if (names != null && !names.equals(userFull.user.first_name + userFull.user.last_name + userFull.user.username)) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_NAME);
2018-08-27 10:33:11 +02:00
}
if (userFull.bot_info instanceof TLRPC.TL_botInfo) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.botInfoDidLoad, userFull.bot_info, classGuid);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.userInfoDidLoad, user.id, userFull, null);
2018-08-27 10:33:11 +02:00
});
} else {
AndroidUtilities.runOnUIThread(() -> loadingFullUsers.remove((Integer) user.id));
}
});
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
}
2015-01-09 13:50:15 +01:00
private void reloadMessages(final ArrayList<Integer> mids, final long dialog_id) {
2015-10-29 18:10:07 +01:00
if (mids.isEmpty()) {
return;
}
2015-09-24 22:52:02 +02:00
TLObject request;
final ArrayList<Integer> result = new ArrayList<>();
2018-07-30 04:07:02 +02:00
final TLRPC.Chat chat = ChatObject.getChatByDialog(dialog_id, currentAccount);
2015-09-24 22:52:02 +02:00
if (ChatObject.isChannel(chat)) {
TLRPC.TL_channels_getMessages req = new TLRPC.TL_channels_getMessages();
req.channel = getInputChannel(chat);
req.id = result;
request = req;
} else {
TLRPC.TL_messages_getMessages req = new TLRPC.TL_messages_getMessages();
req.id = result;
request = req;
}
ArrayList<Integer> arrayList = reloadingMessages.get(dialog_id);
for (int a = 0; a < mids.size(); a++) {
Integer mid = mids.get(a);
if (arrayList != null && arrayList.contains(mid)) {
2015-01-09 13:50:15 +01:00
continue;
}
2015-09-24 22:52:02 +02:00
result.add(mid);
2015-01-09 13:50:15 +01:00
}
2015-09-24 22:52:02 +02:00
if (result.isEmpty()) {
2015-01-09 13:50:15 +01:00
return;
}
2015-09-24 22:52:02 +02:00
if (arrayList == null) {
arrayList = new ArrayList<>();
reloadingMessages.put(dialog_id, arrayList);
}
arrayList.addAll(result);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(request, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
2015-11-26 22:04:02 +01:00
2018-08-27 10:33:11 +02:00
final SparseArray<TLRPC.User> usersLocal = new SparseArray<>();
for (int a = 0; a < messagesRes.users.size(); a++) {
TLRPC.User u = messagesRes.users.get(a);
usersLocal.put(u.id, u);
}
final SparseArray<TLRPC.Chat> chatsLocal = new SparseArray<>();
for (int a = 0; a < messagesRes.chats.size(); a++) {
TLRPC.Chat c = messagesRes.chats.get(a);
chatsLocal.put(c.id, c);
}
2015-01-09 13:50:15 +01:00
2018-08-27 10:33:11 +02:00
Integer inboxValue = dialogs_read_inbox_max.get(dialog_id);
if (inboxValue == null) {
2019-07-18 15:01:39 +02:00
inboxValue = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_inbox_max.put(dialog_id, inboxValue);
}
2018-08-27 10:33:11 +02:00
Integer outboxValue = dialogs_read_outbox_max.get(dialog_id);
if (outboxValue == null) {
2019-07-18 15:01:39 +02:00
outboxValue = getMessagesStorage().getDialogReadMax(true, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(dialog_id, outboxValue);
}
2018-08-27 10:33:11 +02:00
final ArrayList<MessageObject> objects = new ArrayList<>();
for (int a = 0; a < messagesRes.messages.size(); a++) {
TLRPC.Message message = messagesRes.messages.get(a);
if (chat != null && chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
2015-01-09 13:50:15 +01:00
}
2018-08-27 10:33:11 +02:00
message.dialog_id = dialog_id;
message.unread = (message.out ? outboxValue : inboxValue) < message.id;
objects.add(new MessageObject(currentAccount, message, usersLocal, chatsLocal, true));
}
ImageLoader.saveMessagesThumbs(messagesRes.messages);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(messagesRes, dialog_id, -1, 0, false);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
ArrayList<Integer> arrayList1 = reloadingMessages.get(dialog_id);
if (arrayList1 != null) {
arrayList1.removeAll(result);
if (arrayList1.isEmpty()) {
reloadingMessages.remove(dialog_id);
}
}
MessageObject dialogObj = dialogMessage.get(dialog_id);
if (dialogObj != null) {
for (int a = 0; a < objects.size(); a++) {
MessageObject obj = objects.get(a);
if (dialogObj != null && dialogObj.getId() == obj.getId()) {
dialogMessage.put(dialog_id, obj);
if (obj.messageOwner.to_id.channel_id == 0) {
MessageObject obj2 = dialogMessagesByIds.get(obj.getId());
dialogMessagesByIds.remove(obj.getId());
if (obj2 != null) {
dialogMessagesByIds.put(obj2.getId(), obj2);
}
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
break;
}
2015-01-09 13:50:15 +01:00
}
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.replaceMessagesObjects, dialog_id, objects);
2018-08-27 10:33:11 +02:00
});
2015-01-09 13:50:15 +01:00
}
});
}
2019-07-18 15:01:39 +02:00
public void hidePeerSettingsBar(final long dialogId, TLRPC.User currentUser, TLRPC.Chat currentChat) {
2016-03-16 13:26:32 +01:00
if (currentUser == null && currentChat == null) {
return;
}
2018-07-30 04:07:02 +02:00
SharedPreferences.Editor editor = notificationsPreferences.edit();
2019-07-18 15:01:39 +02:00
editor.putInt("dialog_bar_vis3" + dialogId, 3);
2016-03-16 13:26:32 +01:00
editor.commit();
2017-03-31 01:58:05 +02:00
if ((int) dialogId != 0) {
2019-07-18 15:01:39 +02:00
TLRPC.TL_messages_hidePeerSettingsBar req = new TLRPC.TL_messages_hidePeerSettingsBar();
2017-03-31 01:58:05 +02:00
if (currentUser != null) {
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(currentUser.id);
2017-03-31 01:58:05 +02:00
} else if (currentChat != null) {
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(-currentChat.id);
2016-03-16 13:26:32 +01:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2017-03-31 01:58:05 +02:00
});
}
2016-03-16 13:26:32 +01:00
}
2019-07-18 15:01:39 +02:00
public void reportSpam(final long dialogId, TLRPC.User currentUser, TLRPC.Chat currentChat, TLRPC.EncryptedChat currentEncryptedChat, boolean geo) {
2017-03-31 01:58:05 +02:00
if (currentUser == null && currentChat == null && currentEncryptedChat == null) {
2016-03-16 13:26:32 +01:00
return;
}
2018-07-30 04:07:02 +02:00
SharedPreferences.Editor editor = notificationsPreferences.edit();
2019-07-18 15:01:39 +02:00
editor.putInt("dialog_bar_vis3" + dialogId, 3);
2016-03-16 13:26:32 +01:00
editor.commit();
2017-03-31 01:58:05 +02:00
if ((int) dialogId == 0) {
if (currentEncryptedChat == null || currentEncryptedChat.access_hash == 0) {
return;
}
TLRPC.TL_messages_reportEncryptedSpam req = new TLRPC.TL_messages_reportEncryptedSpam();
req.peer = new TLRPC.TL_inputEncryptedChat();
req.peer.chat_id = currentEncryptedChat.id;
req.peer.access_hash = currentEncryptedChat.access_hash;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2016-03-16 13:26:32 +01:00
2017-03-31 01:58:05 +02:00
}, ConnectionsManager.RequestFlagFailOnServerErrors);
} else {
2019-07-18 15:01:39 +02:00
TLRPC.TL_account_reportPeer req = new TLRPC.TL_account_reportPeer();
2017-03-31 01:58:05 +02:00
if (currentChat != null) {
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(-currentChat.id);
2017-03-31 01:58:05 +02:00
} else if (currentUser != null) {
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(currentUser.id);
2016-03-16 13:26:32 +01:00
}
2019-07-18 15:01:39 +02:00
if (geo) {
req.reason = new TLRPC.TL_inputReportReasonGeoIrrelevant();
} else {
req.reason = new TLRPC.TL_inputReportReasonSpam();
}
getConnectionsManager().sendRequest(req, (response, error) -> {
2017-03-31 01:58:05 +02:00
}, ConnectionsManager.RequestFlagFailOnServerErrors);
}
2016-03-16 13:26:32 +01:00
}
2019-07-18 15:01:39 +02:00
private void savePeerSettings(long dialogId, TLRPC.TL_peerSettings settings, boolean update) {
if (settings == null || notificationsPreferences.getInt("dialog_bar_vis3" + dialogId, 0) == 3) {
return;
}
SharedPreferences.Editor editor = notificationsPreferences.edit();
boolean bar_hidden = !settings.report_spam && !settings.add_contact && !settings.block_contact && !settings.share_contact && !settings.report_geo;
if (BuildVars.LOGS_ENABLED) {
FileLog.d("peer settings loaded for " + dialogId + " add = " + settings.add_contact + " block = " + settings.block_contact + " spam = " + settings.report_spam + " share = " + settings.share_contact + " geo = " + settings.report_geo + " hide = " + bar_hidden);
}
editor.putInt("dialog_bar_vis3" + dialogId, bar_hidden ? 1 : 2);
editor.putBoolean("dialog_bar_share" + dialogId, settings.share_contact);
editor.putBoolean("dialog_bar_report" + dialogId, settings.report_spam);
editor.putBoolean("dialog_bar_add" + dialogId, settings.add_contact);
editor.putBoolean("dialog_bar_block" + dialogId, settings.block_contact);
editor.putBoolean("dialog_bar_exception" + dialogId, settings.need_contacts_exception);
editor.putBoolean("dialog_bar_location" + dialogId, settings.report_geo);
editor.commit();
getNotificationCenter().postNotificationName(NotificationCenter.peerSettingsDidLoad, dialogId);
}
2017-03-31 01:58:05 +02:00
public void loadPeerSettings(TLRPC.User currentUser, TLRPC.Chat currentChat) {
if (currentUser == null && currentChat == null) {
return;
}
final long dialogId;
if (currentUser != null) {
dialogId = currentUser.id;
} else {
dialogId = -currentChat.id;
}
2018-07-30 04:07:02 +02:00
if (loadingPeerSettings.indexOfKey(dialogId) >= 0) {
2016-03-16 13:26:32 +01:00
return;
}
loadingPeerSettings.put(dialogId, true);
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("request spam button for " + dialogId);
}
2019-07-18 15:01:39 +02:00
int vis = notificationsPreferences.getInt("dialog_bar_vis3" + dialogId, 0);
if (vis == 1 || vis == 3) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-07-18 15:01:39 +02:00
FileLog.d("dialog bar already hidden for " + dialogId);
2018-07-30 04:07:02 +02:00
}
2016-03-16 13:26:32 +01:00
return;
}
TLRPC.TL_messages_getPeerSettings req = new TLRPC.TL_messages_getPeerSettings();
if (currentUser != null) {
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(currentUser.id);
2016-03-16 13:26:32 +01:00
} else if (currentChat != null) {
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(-currentChat.id);
2016-03-16 13:26:32 +01:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
loadingPeerSettings.remove(dialogId);
if (response != null) {
2019-07-18 15:01:39 +02:00
savePeerSettings(dialogId, (TLRPC.TL_peerSettings) response, false);
2016-03-16 13:26:32 +01:00
}
2018-08-27 10:33:11 +02:00
}));
2016-03-16 13:26:32 +01:00
}
2015-09-24 22:52:02 +02:00
protected void processNewChannelDifferenceParams(int pts, int pts_count, int channelId) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("processNewChannelDifferenceParams pts = " + pts + " pts_count = " + pts_count + " channeldId = " + channelId);
}
2018-07-30 04:07:02 +02:00
int channelPts = channelsPts.get(channelId);
if (channelPts == 0) {
2019-07-18 15:01:39 +02:00
channelPts = getMessagesStorage().getChannelPtsSync(channelId);
if (channelPts == 0) {
channelPts = 1;
}
channelsPts.put(channelId, channelPts);
}
if (channelPts + pts_count == pts) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("APPLY CHANNEL PTS");
}
channelsPts.put(channelId, pts);
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveChannelPts(channelId, pts);
} else if (channelPts != pts) {
2018-07-30 04:07:02 +02:00
long updatesStartWaitTime = updatesStartWaitTimeChannels.get(channelId);
boolean gettingDifferenceChannel = gettingDifferenceChannels.get(channelId);
if (gettingDifferenceChannel || updatesStartWaitTime == 0 || Math.abs(System.currentTimeMillis() - updatesStartWaitTime) <= 1500) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("ADD CHANNEL UPDATE TO QUEUE pts = " + pts + " pts_count = " + pts_count);
}
if (updatesStartWaitTime == 0) {
updatesStartWaitTimeChannels.put(channelId, System.currentTimeMillis());
}
UserActionUpdatesPts updates = new UserActionUpdatesPts();
updates.pts = pts;
updates.pts_count = pts_count;
updates.chat_id = channelId;
ArrayList<TLRPC.Updates> arrayList = updatesQueueChannels.get(channelId);
if (arrayList == null) {
arrayList = new ArrayList<>();
updatesQueueChannels.put(channelId, arrayList);
2015-09-24 22:52:02 +02:00
}
arrayList.add(updates);
} else {
getChannelDifference(channelId);
2015-09-24 22:52:02 +02:00
}
}
}
protected void processNewDifferenceParams(int seq, int pts, int date, int pts_count) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("processNewDifferenceParams seq = " + seq + " pts = " + pts + " date = " + date + " pts_count = " + pts_count);
}
if (pts != -1) {
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastPtsValue() + pts_count == pts) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("APPLY PTS");
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastPtsValue(pts);
getMessagesStorage().saveDiffParams(getMessagesStorage().getLastSeqValue(), getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue());
} else if (getMessagesStorage().getLastPtsValue() != pts) {
2015-11-26 22:04:02 +01:00
if (gettingDifference || updatesStartWaitTimePts == 0 || Math.abs(System.currentTimeMillis() - updatesStartWaitTimePts) <= 1500) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("ADD UPDATE TO QUEUE pts = " + pts + " pts_count = " + pts_count);
}
if (updatesStartWaitTimePts == 0) {
updatesStartWaitTimePts = System.currentTimeMillis();
}
UserActionUpdatesPts updates = new UserActionUpdatesPts();
2015-03-20 00:03:19 +01:00
updates.pts = pts;
updates.pts_count = pts_count;
updatesQueuePts.add(updates);
} else {
getDifference();
}
}
}
if (seq != -1) {
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastSeqValue() + 1 == seq) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("APPLY SEQ");
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastSeqValue(seq);
if (date != -1) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastDateValue(date);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveDiffParams(getMessagesStorage().getLastSeqValue(), getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue());
} else if (getMessagesStorage().getLastSeqValue() != seq) {
2015-11-26 22:04:02 +01:00
if (gettingDifference || updatesStartWaitTimeSeq == 0 || Math.abs(System.currentTimeMillis() - updatesStartWaitTimeSeq) <= 1500) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("ADD UPDATE TO QUEUE seq = " + seq);
}
if (updatesStartWaitTimeSeq == 0) {
updatesStartWaitTimeSeq = System.currentTimeMillis();
}
UserActionUpdatesSeq updates = new UserActionUpdatesSeq();
updates.seq = seq;
updatesQueueSeq.add(updates);
} else {
getDifference();
}
}
}
}
public void didAddedNewTask(final int minDate, final SparseArray<ArrayList<Long>> mids) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
if (currentDeletingTaskMids == null && !gettingNewDeleteTask || currentDeletingTaskTime != 0 && minDate < currentDeletingTaskTime) {
getNewDeleteTask(null, 0);
2014-10-10 19:16:39 +02:00
}
});
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didCreatedNewDeleteTask, mids));
2013-10-25 17:19:00 +02:00
}
public void getNewDeleteTask(final ArrayList<Integer> oldTask, final int channelId) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
gettingNewDeleteTask = true;
2019-07-18 15:01:39 +02:00
getMessagesStorage().getNewTask(oldTask, channelId);
2013-10-25 17:19:00 +02:00
});
}
2014-10-14 10:13:16 +02:00
private boolean checkDeletingTask(boolean runnable) {
2019-07-18 15:01:39 +02:00
int currentServerTime = getConnectionsManager().getCurrentTime();
2013-10-25 17:19:00 +02:00
2014-10-14 10:13:16 +02:00
if (currentDeletingTaskMids != null && (runnable || currentDeletingTaskTime != 0 && currentDeletingTaskTime <= currentServerTime)) {
2013-10-25 17:19:00 +02:00
currentDeletingTaskTime = 0;
2014-10-14 10:13:16 +02:00
if (currentDeleteTaskRunnable != null && !runnable) {
Utilities.stageQueue.cancelRunnable(currentDeleteTaskRunnable);
}
currentDeleteTaskRunnable = null;
final ArrayList<Integer> mids = new ArrayList<>(currentDeletingTaskMids);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (!mids.isEmpty() && mids.get(0) > 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().emptyMessagesMedia(mids);
2018-08-27 10:33:11 +02:00
} else {
deleteMessages(mids, null, null, 0, false);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
getNewDeleteTask(mids, currentDeletingTaskChannelId);
currentDeletingTaskTime = 0;
currentDeletingTaskMids = null;
});
2013-10-25 17:19:00 +02:00
});
2014-10-14 10:13:16 +02:00
return true;
2013-10-25 17:19:00 +02:00
}
2014-10-14 10:13:16 +02:00
return false;
2013-10-25 17:19:00 +02:00
}
public void processLoadedDeleteTask(final int taskTime, final ArrayList<Integer> messages, final int channelId) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
gettingNewDeleteTask = false;
if (messages != null) {
currentDeletingTaskTime = taskTime;
currentDeletingTaskMids = messages;
if (currentDeleteTaskRunnable != null) {
Utilities.stageQueue.cancelRunnable(currentDeleteTaskRunnable);
currentDeleteTaskRunnable = null;
}
if (!checkDeletingTask(false)) {
currentDeleteTaskRunnable = () -> checkDeletingTask(true);
2019-07-18 15:01:39 +02:00
int currentServerTime = getConnectionsManager().getCurrentTime();
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(currentDeleteTaskRunnable, (long) Math.abs(currentServerTime - currentDeletingTaskTime) * 1000);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
currentDeletingTaskTime = 0;
currentDeletingTaskMids = null;
2013-10-25 17:19:00 +02:00
}
});
}
2017-12-08 18:35:59 +01:00
public void loadDialogPhotos(final int did, final int count, final long max_id, final boolean fromCache, final int classGuid) {
2013-12-20 20:25:49 +01:00
if (fromCache) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().getDialogPhotos(did, count, max_id, classGuid);
2013-12-20 20:25:49 +01:00
} else {
if (did > 0) {
TLRPC.User user = getUser(did);
if (user == null) {
return;
2013-12-20 20:25:49 +01:00
}
TLRPC.TL_photos_getUserPhotos req = new TLRPC.TL_photos_getUserPhotos();
req.limit = count;
2017-12-08 18:35:59 +01:00
req.offset = 0;
req.max_id = (int) max_id;
req.user_id = getInputUser(user);
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.photos_Photos res = (TLRPC.photos_Photos) response;
processLoadedUserPhotos(res, did, count, max_id, false, classGuid);
}
});
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
} else if (did < 0) {
TLRPC.TL_messages_search req = new TLRPC.TL_messages_search();
req.filter = new TLRPC.TL_inputMessagesFilterChatPhotos();
req.limit = count;
2017-12-08 18:35:59 +01:00
req.offset_id = (int) max_id;
req.q = "";
2018-07-30 04:07:02 +02:00
req.peer = getInputPeer(did);
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.messages_Messages messages = (TLRPC.messages_Messages) response;
TLRPC.TL_photos_photos res = new TLRPC.TL_photos_photos();
res.count = messages.count;
res.users.addAll(messages.users);
for (int a = 0; a < messages.messages.size(); a++) {
TLRPC.Message message = messages.messages.get(a);
if (message.action == null || message.action.photo == null) {
continue;
}
2018-08-27 10:33:11 +02:00
res.photos.add(message.action.photo);
}
2018-08-27 10:33:11 +02:00
processLoadedUserPhotos(res, did, count, max_id, false, classGuid);
}
});
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
}
2013-12-20 20:25:49 +01:00
}
}
public void blockUser(int user_id) {
final TLRPC.User user = getUser(user_id);
2018-08-27 10:33:11 +02:00
if (user == null || blockedUsers.indexOfKey(user_id) >= 0) {
return;
}
2018-08-27 10:33:11 +02:00
blockedUsers.put(user_id, 1);
2016-05-25 23:49:47 +02:00
if (user.bot) {
2019-07-18 15:01:39 +02:00
getMediaDataController().removeInline(user_id);
2016-05-25 23:49:47 +02:00
} else {
2019-07-18 15:01:39 +02:00
getMediaDataController().removePeer(user_id);
2016-05-25 23:49:47 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.blockedUsersDidLoad);
TLRPC.TL_contacts_block req = new TLRPC.TL_contacts_block();
2016-03-16 13:26:32 +01:00
req.id = getInputUser(user);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
SparseIntArray ids = new SparseIntArray();
ids.put(user.id, 1);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putBlockedUsers(ids, false);
}
});
}
2019-01-23 18:03:33 +01:00
public void setUserBannedRole(final int chatId, TLRPC.User user, TLRPC.TL_chatBannedRights rights, final boolean isChannel, final BaseFragment parentFragment) {
2017-07-08 18:32:04 +02:00
if (user == null || rights == null) {
return;
}
final TLRPC.TL_channels_editBanned req = new TLRPC.TL_channels_editBanned();
2018-07-30 04:07:02 +02:00
req.channel = getInputChannel(chatId);
req.user_id = getInputUser(user);
2017-07-08 18:32:04 +02:00
req.banned_rights = rights;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
processUpdates((TLRPC.Updates) response, false);
AndroidUtilities.runOnUIThread(() -> loadFullChat(chatId, 0, true), 1000);
} else {
2019-01-23 18:03:33 +01:00
AndroidUtilities.runOnUIThread(() -> AlertsCreator.processError(currentAccount, error, parentFragment, req, isChannel));
2018-08-27 10:33:11 +02:00
}
2017-07-08 18:32:04 +02:00
});
}
2019-01-23 18:03:33 +01:00
public void setDefaultBannedRole(final int chatId, TLRPC.TL_chatBannedRights rights, final boolean isChannel, final BaseFragment parentFragment) {
if (rights == null) {
2017-07-08 18:32:04 +02:00
return;
}
2019-01-23 18:03:33 +01:00
final TLRPC.TL_messages_editChatDefaultBannedRights req = new TLRPC.TL_messages_editChatDefaultBannedRights();
req.peer = getInputPeer(-chatId);
req.banned_rights = rights;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
processUpdates((TLRPC.Updates) response, false);
AndroidUtilities.runOnUIThread(() -> loadFullChat(chatId, 0, true), 1000);
} else {
2019-01-23 18:03:33 +01:00
AndroidUtilities.runOnUIThread(() -> AlertsCreator.processError(currentAccount, error, parentFragment, req, isChannel));
2017-07-08 18:32:04 +02:00
}
});
}
2019-01-23 18:03:33 +01:00
public void setUserAdminRole(final int chatId, TLRPC.User user, TLRPC.TL_chatAdminRights rights, final boolean isChannel, final BaseFragment parentFragment, boolean addingNew) {
if (user == null || rights == null) {
return;
}
TLRPC.Chat chat = getChat(chatId);
if (ChatObject.isChannel(chat)) {
final TLRPC.TL_channels_editAdmin req = new TLRPC.TL_channels_editAdmin();
req.channel = getInputChannel(chat);
req.user_id = getInputUser(user);
req.admin_rights = rights;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (error == null) {
processUpdates((TLRPC.Updates) response, false);
AndroidUtilities.runOnUIThread(() -> loadFullChat(chatId, 0, true), 1000);
} else {
AndroidUtilities.runOnUIThread(() -> AlertsCreator.processError(currentAccount, error, parentFragment, req, isChannel));
}
});
} else {
TLRPC.TL_messages_editChatAdmin req = new TLRPC.TL_messages_editChatAdmin();
req.chat_id = chatId;
req.user_id = getInputUser(user);
req.is_admin = rights.change_info || rights.delete_messages || rights.ban_users || rights.invite_users || rights.pin_messages || rights.add_admins;
RequestDelegate requestDelegate = (response, error) -> {
if (error == null) {
AndroidUtilities.runOnUIThread(() -> loadFullChat(chatId, 0, true), 1000);
} else {
AndroidUtilities.runOnUIThread(() -> AlertsCreator.processError(currentAccount, error, parentFragment, req, false));
}
};
if (req.is_admin && addingNew) {
2019-07-18 15:01:39 +02:00
addUserToChat(chatId, user, null, 0, null, parentFragment, () -> getConnectionsManager().sendRequest(req, requestDelegate));
2019-01-23 18:03:33 +01:00
} else {
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, requestDelegate);
2019-01-23 18:03:33 +01:00
}
}
}
public void unblockUser(int user_id) {
TLRPC.TL_contacts_unblock req = new TLRPC.TL_contacts_unblock();
2016-03-16 13:26:32 +01:00
final TLRPC.User user = getUser(user_id);
if (user == null) {
return;
}
2018-08-27 10:33:11 +02:00
blockedUsers.delete(user.id);
2016-03-16 13:26:32 +01:00
req.id = getInputUser(user);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.blockedUsersDidLoad);
getConnectionsManager().sendRequest(req, (response, error) -> getMessagesStorage().deleteBlockedUser(user.id));
}
public void getBlockedUsers(boolean cache) {
2019-07-18 15:01:39 +02:00
if (!getUserConfig().isClientActivated() || loadingBlockedUsers) {
return;
}
loadingBlockedUsers = true;
if (cache) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().getBlockedUsers();
} else {
TLRPC.TL_contacts_getBlocked req = new TLRPC.TL_contacts_getBlocked();
req.offset = 0;
req.limit = 200;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
SparseIntArray blocked = new SparseIntArray();
ArrayList<TLRPC.User> users = null;
if (error == null) {
final TLRPC.contacts_Blocked res = (TLRPC.contacts_Blocked) response;
for (TLRPC.TL_contactBlocked contactBlocked : res.blocked) {
blocked.put(contactBlocked.user_id, 1);
}
2018-08-27 10:33:11 +02:00
users = res.users;
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, null, true, true);
getMessagesStorage().putBlockedUsers(blocked, true);
}
2018-08-27 10:33:11 +02:00
processLoadedBlockedUsers(blocked, users, false);
});
}
}
2018-08-27 10:33:11 +02:00
public void processLoadedBlockedUsers(final SparseIntArray ids, final ArrayList<TLRPC.User> users, final boolean cache) {
AndroidUtilities.runOnUIThread(() -> {
if (users != null) {
putUsers(users, cache);
}
2018-08-27 10:33:11 +02:00
loadingBlockedUsers = false;
2019-07-18 15:01:39 +02:00
if (ids.size() == 0 && cache && !getUserConfig().blockedUsersLoaded) {
2018-08-27 10:33:11 +02:00
getBlockedUsers(false);
return;
} else if (!cache) {
2019-07-18 15:01:39 +02:00
getUserConfig().blockedUsersLoaded = true;
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
}
blockedUsers = ids;
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.blockedUsersDidLoad);
});
}
public void deleteUserPhoto(TLRPC.InputPhoto photo) {
if (photo == null) {
TLRPC.TL_photos_updateProfilePhoto req = new TLRPC.TL_photos_updateProfilePhoto();
req.id = new TLRPC.TL_inputPhotoEmpty();
2019-07-18 15:01:39 +02:00
getUserConfig().getCurrentUser().photo = new TLRPC.TL_userProfilePhotoEmpty();
TLRPC.User user = getUser(getUserConfig().getClientUserId());
if (user == null) {
2019-07-18 15:01:39 +02:00
user = getUserConfig().getCurrentUser();
}
if (user == null) {
return;
}
2019-07-18 15:01:39 +02:00
user.photo = getUserConfig().getCurrentUser().photo;
getNotificationCenter().postNotificationName(NotificationCenter.mainUserInfoChanged);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_ALL);
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
2019-07-18 15:01:39 +02:00
TLRPC.User user1 = getUser(getUserConfig().getClientUserId());
2018-08-27 10:33:11 +02:00
if (user1 == null) {
2019-07-18 15:01:39 +02:00
user1 = getUserConfig().getCurrentUser();
2018-08-27 10:33:11 +02:00
putUser(user1, false);
} else {
2019-07-18 15:01:39 +02:00
getUserConfig().setCurrentUser(user1);
}
2018-08-27 10:33:11 +02:00
if (user1 == null) {
return;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().clearUserPhotos(user1.id);
2018-08-27 10:33:11 +02:00
ArrayList<TLRPC.User> users = new ArrayList<>();
users.add(user1);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(users, null, false, true);
2018-08-27 10:33:11 +02:00
user1.photo = (TLRPC.UserProfilePhoto) response;
AndroidUtilities.runOnUIThread(() -> {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.mainUserInfoChanged);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_ALL);
getUserConfig().saveConfig(true);
2018-08-27 10:33:11 +02:00
});
}
});
} else {
TLRPC.TL_photos_deletePhotos req = new TLRPC.TL_photos_deletePhotos();
req.id.add(photo);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
});
}
}
2017-12-08 18:35:59 +01:00
public void processLoadedUserPhotos(final TLRPC.photos_Photos res, final int did, final int count, final long max_id, final boolean fromCache, final int classGuid) {
2013-12-20 20:25:49 +01:00
if (!fromCache) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, null, true, true);
getMessagesStorage().putDialogPhotos(did, res);
2013-12-20 20:25:49 +01:00
} else if (res == null || res.photos.isEmpty()) {
2017-12-08 18:35:59 +01:00
loadDialogPhotos(did, count, max_id, false, classGuid);
2013-12-20 20:25:49 +01:00
return;
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(res.users, fromCache);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogPhotosLoaded, did, count, fromCache, classGuid, res.photos);
2013-12-20 20:25:49 +01:00
});
}
2019-01-23 18:03:33 +01:00
public void uploadAndApplyUserAvatar(TLRPC.FileLocation location) {
if (location == null) {
return;
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
uploadingAvatar = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + location.volume_id + "_" + location.local_id + ".jpg";
2019-07-18 15:01:39 +02:00
getFileLoader().uploadFile(uploadingAvatar, false, true, ConnectionsManager.FileTypePhoto);
2013-10-25 17:19:00 +02:00
}
2019-02-08 03:30:32 +01:00
public void saveWallpaperToServer(File path, long wallPaperId, long accessHash, boolean isBlurred, boolean isMotion, int backgroundColor, float intesity, boolean install, long taskId) {
if (uploadingWallpaper != null) {
File finalPath = new File(ApplicationLoader.getFilesDirFixed(), uploadingWallpaperBlurred ? "wallpaper_original.jpg" : "wallpaper.jpg");
if (path != null && (path.getAbsolutePath().equals(uploadingWallpaper) || path.equals(finalPath))) {
uploadingWallpaperMotion = isMotion;
uploadingWallpaperBlurred = isBlurred;
return;
}
2019-07-18 15:01:39 +02:00
getFileLoader().cancelUploadFile(uploadingWallpaper, false);
2019-02-08 03:30:32 +01:00
uploadingWallpaper = null;
}
if (path != null) {
uploadingWallpaper = path.getAbsolutePath();
uploadingWallpaperMotion = isMotion;
uploadingWallpaperBlurred = isBlurred;
2019-07-18 15:01:39 +02:00
getFileLoader().uploadFile(uploadingWallpaper, false, true, ConnectionsManager.FileTypePhoto);
2019-02-08 03:30:32 +01:00
} else if (accessHash != 0) {
TLRPC.TL_inputWallPaper inputWallPaper = new TLRPC.TL_inputWallPaper();
inputWallPaper.id = wallPaperId;
inputWallPaper.access_hash = accessHash;
TLRPC.TL_wallPaperSettings settings = new TLRPC.TL_wallPaperSettings();
settings.blur = isBlurred;
settings.motion = isMotion;
if (backgroundColor != 0) {
settings.background_color = backgroundColor;
settings.flags |= 1;
settings.intensity = (int) (intesity * 100);
settings.flags |= 8;
}
TLObject req;
if (install) {
TLRPC.TL_account_installWallPaper request = new TLRPC.TL_account_installWallPaper();
request.wallpaper = inputWallPaper;
request.settings = settings;
req = request;
} else {
TLRPC.TL_account_saveWallPaper request = new TLRPC.TL_account_saveWallPaper();
request.wallpaper = inputWallPaper;
request.settings = settings;
req = request;
}
final long newTaskId;
if (taskId != 0) {
newTaskId = taskId;
} else {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(44);
data.writeInt32(12);
data.writeInt64(wallPaperId);
data.writeInt64(accessHash);
data.writeBool(isBlurred);
data.writeBool(isMotion);
data.writeInt32(backgroundColor);
data.writeDouble(intesity);
data.writeBool(install);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2019-02-08 03:30:32 +01:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
getMessagesStorage().removePendingTask(newTaskId);
2019-02-08 03:30:32 +01:00
if (!install && uploadingWallpaper != null) {
SharedPreferences preferences = getGlobalMainSettings();
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("selectedBackground2", wallPaperId);
editor.commit();
}
});
}
}
2016-03-16 13:26:32 +01:00
public void markChannelDialogMessageAsDeleted(ArrayList<Integer> messages, final int channelId) {
MessageObject obj = dialogMessage.get((long) -channelId);
if (obj != null) {
for (int a = 0; a < messages.size(); a++) {
Integer id = messages.get(a);
if (obj.getId() == id) {
obj.deleted = true;
break;
}
}
}
}
2017-03-31 01:58:05 +02:00
public void deleteMessages(ArrayList<Integer> messages, ArrayList<Long> randoms, TLRPC.EncryptedChat encryptedChat, final int channelId, boolean forAll) {
deleteMessages(messages, randoms, encryptedChat, channelId, forAll, 0, null);
}
public void deleteMessages(ArrayList<Integer> messages, ArrayList<Long> randoms, TLRPC.EncryptedChat encryptedChat, final int channelId, boolean forAll, long taskId, TLObject taskRequest) {
if ((messages == null || messages.isEmpty()) && taskRequest == null) {
return;
}
2017-03-31 01:58:05 +02:00
ArrayList<Integer> toSend = null;
if (taskId == 0) {
if (channelId == 0) {
for (int a = 0; a < messages.size(); a++) {
Integer id = messages.get(a);
MessageObject obj = dialogMessagesByIds.get(id);
if (obj != null) {
obj.deleted = true;
}
2015-09-24 22:52:02 +02:00
}
2017-03-31 01:58:05 +02:00
} else {
markChannelDialogMessageAsDeleted(messages, channelId);
2015-09-24 22:52:02 +02:00
}
2017-03-31 01:58:05 +02:00
toSend = new ArrayList<>();
for (int a = 0; a < messages.size(); a++) {
Integer mid = messages.get(a);
if (mid > 0) {
toSend.add(mid);
}
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().markMessagesAsDeleted(messages, true, channelId);
getMessagesStorage().updateDialogsWithDeletedMessages(messages, null, true, channelId);
getNotificationCenter().postNotificationName(NotificationCenter.messagesDeleted, messages, channelId);
2013-10-25 17:19:00 +02:00
}
2017-03-31 01:58:05 +02:00
final long newTaskId;
2015-09-24 22:52:02 +02:00
if (channelId != 0) {
2017-03-31 01:58:05 +02:00
TLRPC.TL_channels_deleteMessages req;
if (taskRequest != null) {
req = (TLRPC.TL_channels_deleteMessages) taskRequest;
newTaskId = taskId;
} else {
req = new TLRPC.TL_channels_deleteMessages();
req.id = toSend;
req.channel = getInputChannel(channelId);
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(8 + req.getObjectSize());
data.writeInt32(7);
data.writeInt32(channelId);
req.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2017-03-31 01:58:05 +02:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
processNewChannelDifferenceParams(res.pts, res.pts_count, channelId);
}
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
}
2015-09-24 22:52:02 +02:00
});
} else {
if (randoms != null && encryptedChat != null && !randoms.isEmpty()) {
2019-07-18 15:01:39 +02:00
getSecretChatHelper().sendMessagesDeleteMessage(encryptedChat, randoms, null);
2013-10-25 17:19:00 +02:00
}
2017-03-31 01:58:05 +02:00
TLRPC.TL_messages_deleteMessages req;
if (taskRequest != null) {
req = (TLRPC.TL_messages_deleteMessages) taskRequest;
newTaskId = taskId;
} else {
req = new TLRPC.TL_messages_deleteMessages();
req.id = toSend;
req.revoke = forAll;
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(8 + req.getObjectSize());
data.writeInt32(7);
data.writeInt32(channelId);
req.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2017-03-31 01:58:05 +02:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
}
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2015-09-24 22:52:02 +02:00
}
});
}
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
public void pinMessage(TLRPC.Chat chat, TLRPC.User user, int id, boolean notify) {
if (chat == null && user == null) {
return;
}
TLRPC.TL_messages_updatePinnedMessage req = new TLRPC.TL_messages_updatePinnedMessage();
req.peer = getInputPeer(chat != null ? -chat.id : user.id);
2016-03-16 13:26:32 +01:00
req.id = id;
req.silent = !notify;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.Updates updates = (TLRPC.Updates) response;
processUpdates(updates, false);
2016-03-16 13:26:32 +01:00
}
});
}
public void deleteUserChannelHistory(final TLRPC.Chat chat, final TLRPC.User user, int offset) {
if (offset == 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().deleteUserChannelHistory(chat.id, user.id);
2016-03-16 13:26:32 +01:00
}
TLRPC.TL_channels_deleteUserHistory req = new TLRPC.TL_channels_deleteUserHistory();
req.channel = getInputChannel(chat);
req.user_id = getInputUser(user);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
if (res.offset > 0) {
deleteUserChannelHistory(chat, user, res.offset);
2016-03-16 13:26:32 +01:00
}
2018-08-27 10:33:11 +02:00
processNewChannelDifferenceParams(res.pts, res.pts_count, chat.id);
2016-03-16 13:26:32 +01:00
}
});
}
2019-05-14 14:08:05 +02:00
public ArrayList<TLRPC.Dialog> getAllDialogs() {
return allDialogs;
}
public boolean isDialogsEndReached(int folderId) {
return dialogsEndReached.get(folderId);
}
public boolean isLoadingDialogs(int folderId) {
return loadingDialogs.get(folderId);
}
public boolean isServerDialogsEndReached(int folderId) {
return serverDialogsEndReached.get(folderId);
}
public boolean hasHiddenArchive() {
return SharedConfig.archiveHidden && dialogs_dict.get(DialogObject.makeFolderDialogId(1)) != null;
}
public ArrayList<TLRPC.Dialog> getDialogs(int folderId) {
ArrayList<TLRPC.Dialog> dialogs = dialogsByFolder.get(folderId);
if (dialogs == null) {
return new ArrayList<>();
}
return dialogs;
}
private void removeDialog(TLRPC.Dialog dialog) {
if (dialog == null) {
return;
}
long did = dialog.id;
if (dialogsServerOnly.remove(dialog) && DialogObject.isChannel(dialog)) {
Utilities.stageQueue.postRunnable(() -> {
channelsPts.delete(-(int) did);
shortPollChannels.delete(-(int) did);
needShortPollChannels.delete(-(int) did);
shortPollOnlines.delete(-(int) did);
needShortPollOnlines.delete(-(int) did);
});
}
allDialogs.remove(dialog);
dialogsCanAddUsers.remove(dialog);
dialogsChannelsOnly.remove(dialog);
dialogsGroupsOnly.remove(dialog);
dialogsUsersOnly.remove(dialog);
dialogsForward.remove(dialog);
dialogs_dict.remove(did);
dialogs_read_inbox_max.remove(did);
dialogs_read_outbox_max.remove(did);
ArrayList<TLRPC.Dialog> dialogs = dialogsByFolder.get(dialog.folder_id);
if (dialogs != null) {
dialogs.remove(dialog);
}
}
public void deleteDialog(final long did, final int onlyHistory) {
2019-05-14 14:08:05 +02:00
deleteDialog(did, onlyHistory, false);
}
2019-05-14 14:08:05 +02:00
public void deleteDialog(final long did, final int onlyHistory, boolean revoke) {
deleteDialog(did, true, onlyHistory, 0, revoke, null, 0);
}
public void setDialogsInTransaction(boolean transaction) {
dialogsInTransaction = transaction;
if (!transaction) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2019-05-14 14:08:05 +02:00
}
}
2014-11-20 15:45:33 +01:00
2019-05-14 14:08:05 +02:00
protected void deleteDialog(final long did, final boolean first, final int onlyHistory, final int max_id, boolean revoke, TLRPC.InputPeer peer, final long taskId) {
2015-12-09 19:27:52 +01:00
if (onlyHistory == 2) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().deleteDialog(did, onlyHistory);
2015-12-09 19:27:52 +01:00
return;
}
if (onlyHistory == 0 || onlyHistory == 3) {
2019-07-18 15:01:39 +02:00
getMediaDataController().uninstallShortcut(did);
2016-05-25 23:49:47 +02:00
}
2019-05-14 14:08:05 +02:00
int lower_part = (int) did;
int high_id = (int) (did >> 32);
int max_id_delete = max_id;
2015-12-09 19:27:52 +01:00
2015-11-26 22:04:02 +01:00
if (first) {
2018-07-30 04:07:02 +02:00
boolean isProxyDialog = false;
2019-07-18 15:01:39 +02:00
getMessagesStorage().deleteDialog(did, onlyHistory);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2019-06-04 12:14:50 +02:00
if (onlyHistory == 0 || onlyHistory == 3) {
2019-07-18 15:01:39 +02:00
getNotificationsController().deleteNotificationChannel(did);
2019-06-04 12:14:50 +02:00
}
2014-11-21 01:14:44 +01:00
if (dialog != null) {
2015-11-26 22:04:02 +01:00
if (max_id_delete == 0) {
max_id_delete = Math.max(0, dialog.top_message);
2019-05-14 14:08:05 +02:00
max_id_delete = Math.max(max_id_delete, dialog.read_inbox_max_id);
max_id_delete = Math.max(max_id_delete, dialog.read_outbox_max_id);
2015-11-26 22:04:02 +01:00
}
if (onlyHistory == 0 || onlyHistory == 3) {
2018-07-30 04:07:02 +02:00
if (isProxyDialog = (proxyDialog != null && proxyDialog.id == did)) {
isLeftProxyChannel = true;
if (proxyDialog.id < 0) {
TLRPC.Chat chat = getChat(-(int) proxyDialog.id);
if (chat != null) {
chat.left = true;
}
2018-07-30 04:07:02 +02:00
}
sortDialogs(null);
} else {
2019-05-14 14:08:05 +02:00
removeDialog(dialog);
int offset = nextDialogsCacheOffset.get(dialog.folder_id, 0);
if (offset > 0) {
nextDialogsCacheOffset.put(dialog.folder_id, offset - 1);
2018-07-30 04:07:02 +02:00
}
2015-09-24 22:52:02 +02:00
}
2014-11-21 01:14:44 +01:00
} else {
dialog.unread_count = 0;
}
2018-07-30 04:07:02 +02:00
if (!isProxyDialog) {
int lastMessageId;
MessageObject object = dialogMessage.get(dialog.id);
dialogMessage.remove(dialog.id);
if (object != null) {
lastMessageId = object.getId();
dialogMessagesByIds.remove(object.getId());
} else {
2018-07-30 04:07:02 +02:00
lastMessageId = dialog.top_message;
object = dialogMessagesByIds.get(dialog.top_message);
dialogMessagesByIds.remove(dialog.top_message);
}
if (object != null && object.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(object.messageOwner.random_id);
}
if (onlyHistory == 1 && lower_part != 0 && lastMessageId > 0) {
TLRPC.TL_messageService message = new TLRPC.TL_messageService();
message.id = dialog.top_message;
2019-07-18 15:01:39 +02:00
message.out = getUserConfig().getClientUserId() == did;
message.from_id = getUserConfig().getClientUserId();
2018-07-30 04:07:02 +02:00
message.flags |= 256;
message.action = new TLRPC.TL_messageActionHistoryClear();
message.date = dialog.last_message_date;
2019-01-23 18:03:33 +01:00
message.dialog_id = lower_part;
2018-07-30 04:07:02 +02:00
if (lower_part > 0) {
message.to_id = new TLRPC.TL_peerUser();
message.to_id.user_id = lower_part;
} else {
2018-07-30 04:07:02 +02:00
TLRPC.Chat chat = getChat(-lower_part);
if (ChatObject.isChannel(chat)) {
message.to_id = new TLRPC.TL_peerChannel();
message.to_id.channel_id = -lower_part;
} else {
message.to_id = new TLRPC.TL_peerChat();
message.to_id.chat_id = -lower_part;
}
}
2018-07-30 04:07:02 +02:00
final MessageObject obj = new MessageObject(currentAccount, message, createdDialogIds.contains(message.dialog_id));
final ArrayList<MessageObject> objArr = new ArrayList<>();
objArr.add(obj);
ArrayList<TLRPC.Message> arr = new ArrayList<>();
arr.add(message);
updateInterfaceWithMessages(did, objArr);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(arr, false, true, false, 0);
2018-07-30 04:07:02 +02:00
} else {
dialog.top_message = 0;
}
}
}
2019-05-14 14:08:05 +02:00
if (!dialogsInTransaction) {
if (isProxyDialog) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2019-05-14 14:08:05 +02:00
} else {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
getNotificationCenter().postNotificationName(NotificationCenter.removeAllMessagesFromDialog, did, false);
2019-05-14 14:08:05 +02:00
}
2018-07-30 04:07:02 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> getNotificationsController().removeNotificationsForDialog(did)));
2014-11-20 15:45:33 +01:00
}
if (high_id == 1 || onlyHistory == 3) {
2014-11-20 15:45:33 +01:00
return;
}
if (lower_part != 0) {
2019-05-14 14:08:05 +02:00
if (peer == null) {
peer = getInputPeer(lower_part);
}
2017-12-08 18:35:59 +01:00
if (peer == null) {
2015-09-24 22:52:02 +02:00
return;
}
2019-05-14 14:08:05 +02:00
final long newTaskId;
if (!(peer instanceof TLRPC.TL_inputPeerChannel) || onlyHistory != 0) {
if (max_id_delete > 0 && max_id_delete != Integer.MAX_VALUE) {
deletedHistory.put(did, max_id_delete);
}
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(4 + 8 + 4 + 4 + 4 + 4 + peer.getObjectSize());
data.writeInt32(13);
data.writeInt64(did);
data.writeBool(first);
data.writeInt32(onlyHistory);
data.writeInt32(max_id_delete);
data.writeBool(revoke);
peer.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2019-05-14 14:08:05 +02:00
} else {
newTaskId = taskId;
}
} else {
newTaskId = taskId;
}
2017-12-08 18:35:59 +01:00
if (peer instanceof TLRPC.TL_inputPeerChannel) {
if (onlyHistory == 0) {
2019-05-14 14:08:05 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-05-14 14:08:05 +02:00
}
2017-12-08 18:35:59 +01:00
return;
}
TLRPC.TL_channels_deleteHistory req = new TLRPC.TL_channels_deleteHistory();
req.channel = new TLRPC.TL_inputChannel();
req.channel.channel_id = peer.channel_id;
req.channel.access_hash = peer.access_hash;
req.max_id = max_id_delete > 0 ? max_id_delete : Integer.MAX_VALUE;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-05-14 14:08:05 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-05-14 14:08:05 +02:00
}
AndroidUtilities.runOnUIThread(() -> deletedHistory.remove(did));
2017-12-08 18:35:59 +01:00
}, ConnectionsManager.RequestFlagInvokeAfter);
} else {
TLRPC.TL_messages_deleteHistory req = new TLRPC.TL_messages_deleteHistory();
req.peer = peer;
req.max_id = (onlyHistory == 0 ? Integer.MAX_VALUE : max_id_delete);
req.just_clear = onlyHistory != 0;
2019-05-14 14:08:05 +02:00
req.revoke = revoke;
2017-12-08 18:35:59 +01:00
final int max_id_delete_final = max_id_delete;
2019-05-14 14:08:05 +02:00
final TLRPC.InputPeer peerFinal = peer;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-05-14 14:08:05 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-05-14 14:08:05 +02:00
}
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
if (res.offset > 0) {
2019-05-14 14:08:05 +02:00
deleteDialog(did, false, onlyHistory, max_id_delete_final, revoke, peerFinal, 0);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
2019-07-18 15:01:39 +02:00
getMessagesStorage().onDeleteQueryComplete(did);
2013-10-25 17:19:00 +02:00
}
2017-12-08 18:35:59 +01:00
}, ConnectionsManager.RequestFlagInvokeAfter);
}
2014-11-20 15:45:33 +01:00
} else {
2015-12-09 19:27:52 +01:00
if (onlyHistory == 1) {
2019-07-18 15:01:39 +02:00
getSecretChatHelper().sendClearHistoryMessage(getEncryptedChat(high_id), null);
2014-11-20 15:45:33 +01:00
} else {
2019-07-18 15:01:39 +02:00
getSecretChatHelper().declineSecretChat(high_id);
2013-10-25 17:19:00 +02:00
}
}
}
2019-01-23 18:03:33 +01:00
public void saveGif(Object parentObject, TLRPC.Document document) {
2019-05-14 14:08:05 +02:00
if (parentObject == null || !MessageObject.isGifDocument(document)) {
2019-01-23 18:03:33 +01:00
return;
}
2016-03-16 13:26:32 +01:00
TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
req.id = new TLRPC.TL_inputDocument();
2016-10-11 13:57:01 +02:00
req.id.id = document.id;
req.id.access_hash = document.access_hash;
2019-01-23 18:03:33 +01:00
req.id.file_reference = document.file_reference;
if (req.id.file_reference == null) {
req.id.file_reference = new byte[0];
}
2016-10-11 13:57:01 +02:00
req.unsave = false;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (error != null && FileRefController.isFileRefError(error.text) && parentObject != null) {
2019-07-18 15:01:39 +02:00
getFileRefController().requestReference(parentObject, req);
2019-01-23 18:03:33 +01:00
}
2016-10-11 13:57:01 +02:00
});
}
2019-01-23 18:03:33 +01:00
public void saveRecentSticker(Object parentObject, TLRPC.Document document, boolean asMask) {
if (parentObject == null || document == null) {
return;
}
2016-10-11 13:57:01 +02:00
TLRPC.TL_messages_saveRecentSticker req = new TLRPC.TL_messages_saveRecentSticker();
req.id = new TLRPC.TL_inputDocument();
req.id.id = document.id;
req.id.access_hash = document.access_hash;
2019-01-23 18:03:33 +01:00
req.id.file_reference = document.file_reference;
if (req.id.file_reference == null) {
req.id.file_reference = new byte[0];
}
2016-03-16 13:26:32 +01:00
req.unsave = false;
2016-10-11 13:57:01 +02:00
req.attached = asMask;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (error != null && FileRefController.isFileRefError(error.text) && parentObject != null) {
2019-07-18 15:01:39 +02:00
getFileRefController().requestReference(parentObject, req);
2019-01-23 18:03:33 +01:00
}
2016-03-16 13:26:32 +01:00
});
}
2015-11-26 22:04:02 +01:00
public void loadChannelParticipants(final Integer chat_id) {
if (loadingFullParticipants.contains(chat_id) || loadedFullParticipants.contains(chat_id)) {
return;
}
loadingFullParticipants.add(chat_id);
final TLRPC.TL_channels_getParticipants req = new TLRPC.TL_channels_getParticipants();
2016-03-16 13:26:32 +01:00
req.channel = getInputChannel(chat_id);
2015-11-26 22:04:02 +01:00
req.filter = new TLRPC.TL_channelParticipantsRecent();
req.offset = 0;
req.limit = 32;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_channels_channelParticipants res = (TLRPC.TL_channels_channelParticipants) response;
putUsers(res.users, false);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, null, true, true);
getMessagesStorage().updateChannelUsers(chat_id, res.participants);
2018-08-27 10:33:11 +02:00
loadedFullParticipants.add(chat_id);
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
loadingFullParticipants.remove(chat_id);
}));
2015-11-26 22:04:02 +01:00
}
2018-07-30 04:07:02 +02:00
public void loadChatInfo(final int chat_id, CountDownLatch countDownLatch, boolean force) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().loadChatInfo(chat_id, countDownLatch, force, false);
2013-10-25 17:19:00 +02:00
}
2016-03-16 13:26:32 +01:00
public void processChatInfo(int chat_id, final TLRPC.ChatFull info, final ArrayList<TLRPC.User> usersArr, final boolean fromCache, boolean force, final boolean byChannelUsers, final MessageObject pinnedMessageObject) {
2015-11-26 22:04:02 +01:00
if (fromCache && chat_id > 0 && !byChannelUsers) {
2015-09-24 22:52:02 +02:00
loadFullChat(chat_id, 0, force);
2014-11-19 16:17:24 +01:00
}
if (info != null) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(usersArr, fromCache);
if (info.stickerset != null) {
2019-07-18 15:01:39 +02:00
getMediaDataController().getGroupStickerSetById(info.stickerset);
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoDidLoad, info, 0, byChannelUsers, pinnedMessageObject);
2013-10-25 17:19:00 +02:00
});
}
}
2019-01-23 18:03:33 +01:00
public void loadUserInfo(TLRPC.User user, boolean force, int classGuid) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().loadUserInfo(user, force, classGuid);
2019-01-23 18:03:33 +01:00
}
2019-05-14 14:08:05 +02:00
public void processUserInfo(TLRPC.User user, final TLRPC.UserFull info, final boolean fromCache, boolean force, final MessageObject pinnedMessageObject, int classGuid) {
2019-01-23 18:03:33 +01:00
if (fromCache) {
loadFullUser(user, classGuid, force);
}
if (info != null) {
if (fullUsers.get(user.id) == null) {
fullUsers.put(user.id, info);
}
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.userInfoDidLoad, user.id, info, pinnedMessageObject));
2019-01-23 18:03:33 +01:00
}
}
2013-10-25 17:19:00 +02:00
public void updateTimerProc() {
long currentTime = System.currentTimeMillis();
2014-10-14 10:13:16 +02:00
checkDeletingTask(false);
2018-07-30 04:07:02 +02:00
checkReadTasks();
2013-10-25 17:19:00 +02:00
2019-07-18 15:01:39 +02:00
if (getUserConfig().isClientActivated()) {
if (getConnectionsManager().getPauseTime() == 0 && ApplicationLoader.isScreenOn && !ApplicationLoader.mainInterfacePausedStageQueue) {
2017-03-31 01:58:05 +02:00
if (ApplicationLoader.mainInterfacePausedStageQueueTime != 0 && Math.abs(ApplicationLoader.mainInterfacePausedStageQueueTime - System.currentTimeMillis()) > 1000) {
if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || Math.abs(System.currentTimeMillis() - lastStatusUpdateTime) >= 55000 || offlineSent)) {
statusSettingState = 1;
2017-03-31 01:58:05 +02:00
if (statusRequest != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().cancelRequest(statusRequest, true);
2017-03-31 01:58:05 +02:00
}
2017-03-31 01:58:05 +02:00
TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus();
req.offline = false;
2019-07-18 15:01:39 +02:00
statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
lastStatusUpdateTime = System.currentTimeMillis();
offlineSent = false;
statusSettingState = 0;
} else {
if (lastStatusUpdateTime != 0) {
lastStatusUpdateTime += 5000;
}
}
2018-08-27 10:33:11 +02:00
statusRequest = 0;
2017-03-31 01:58:05 +02:00
});
}
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
} else if (statusSettingState != 2 && !offlineSent && Math.abs(System.currentTimeMillis() - getConnectionsManager().getPauseTime()) >= 2000) {
statusSettingState = 2;
if (statusRequest != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().cancelRequest(statusRequest, true);
}
2013-10-25 17:19:00 +02:00
TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus();
req.offline = true;
2019-07-18 15:01:39 +02:00
statusRequest = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
offlineSent = true;
} else {
if (lastStatusUpdateTime != 0) {
lastStatusUpdateTime += 5000;
}
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
statusRequest = 0;
});
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
if (updatesQueueChannels.size() != 0) {
for (int a = 0; a < updatesQueueChannels.size(); a++) {
int key = updatesQueueChannels.keyAt(a);
long updatesStartWaitTime = updatesStartWaitTimeChannels.valueAt(a);
if (updatesStartWaitTime + 1500 < currentTime) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("QUEUE CHANNEL " + key + " UPDATES WAIT TIMEOUT - CHECK QUEUE");
}
2015-09-24 22:52:02 +02:00
processChannelsUpdatesQueue(key, 0);
}
}
}
for (int a = 0; a < 3; a++) {
if (getUpdatesStartTime(a) != 0 && getUpdatesStartTime(a) + 1500 < currentTime) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d(a + " QUEUE UPDATES WAIT TIMEOUT - CHECK QUEUE");
}
processUpdatesQueue(a, 0);
}
}
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
if (Math.abs(System.currentTimeMillis() - lastViewsCheckTime) >= 5000) {
2015-09-24 22:52:02 +02:00
lastViewsCheckTime = System.currentTimeMillis();
2019-01-23 18:03:33 +01:00
if (channelViewsToSend.size() != 0) {
for (int a = 0; a < channelViewsToSend.size(); a++) {
final int key = channelViewsToSend.keyAt(a);
final TLRPC.TL_messages_getMessagesViews req = new TLRPC.TL_messages_getMessagesViews();
req.peer = getInputPeer(key);
req.id = channelViewsToSend.valueAt(a);
req.increment = a == 0;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-05-14 14:08:05 +02:00
if (response != null) {
2019-01-23 18:03:33 +01:00
TLRPC.Vector vector = (TLRPC.Vector) response;
final SparseArray<SparseIntArray> channelViews = new SparseArray<>();
SparseIntArray array = channelViews.get(key);
if (array == null) {
array = new SparseIntArray();
channelViews.put(key, array);
}
for (int a1 = 0; a1 < req.id.size(); a1++) {
if (a1 >= vector.objects.size()) {
break;
}
array.put(req.id.get(a1), (Integer) vector.objects.get(a1));
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putChannelViews(channelViews, req.peer instanceof TLRPC.TL_inputPeerChannel);
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didUpdatedMessagesViews, channelViews));
2019-01-23 18:03:33 +01:00
}
});
}
channelViewsToSend.clear();
}
if (pollsToCheckSize > 0) {
AndroidUtilities.runOnUIThread(() -> {
long time = SystemClock.uptimeMillis();
for (int a = 0, N = pollsToCheck.size(); a < N; a++) {
SparseArray<MessageObject> array = pollsToCheck.valueAt(a);
2018-08-27 10:33:11 +02:00
if (array == null) {
2019-01-23 18:03:33 +01:00
continue;
2018-08-27 10:33:11 +02:00
}
2019-01-23 18:03:33 +01:00
for (int b = 0, N2 = array.size(); b < N2; b++) {
MessageObject messageObject = array.valueAt(b);
if (Math.abs(time - messageObject.pollLastCheckTime) < 30000) {
if (!messageObject.pollVisibleOnScreen) {
array.remove(messageObject.getId());
N2--;
b--;
}
} else {
messageObject.pollLastCheckTime = time;
TLRPC.TL_messages_getPollResults req = new TLRPC.TL_messages_getPollResults();
req.peer = getInputPeer((int) messageObject.getDialogId());
req.msg_id = messageObject.getId();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (error == null) {
processUpdates((TLRPC.Updates) response, false);
}
});
2015-09-24 22:52:02 +02:00
}
}
2019-01-23 18:03:33 +01:00
if (array.size() == 0) {
pollsToCheck.remove(pollsToCheck.keyAt(a));
N--;
a--;
}
2017-12-08 18:35:59 +01:00
}
2019-01-23 18:03:33 +01:00
pollsToCheckSize = pollsToCheck.size();
2017-12-08 18:35:59 +01:00
});
2015-09-24 22:52:02 +02:00
}
}
2014-11-21 01:14:44 +01:00
if (!onlinePrivacy.isEmpty()) {
ArrayList<Integer> toRemove = null;
2019-07-18 15:01:39 +02:00
int currentServerTime = getConnectionsManager().getCurrentTime();
2014-11-21 01:14:44 +01:00
for (ConcurrentHashMap.Entry<Integer, Integer> entry : onlinePrivacy.entrySet()) {
if (entry.getValue() < currentServerTime - 30) {
if (toRemove == null) {
2015-01-02 23:15:07 +01:00
toRemove = new ArrayList<>();
2014-11-21 01:14:44 +01:00
}
toRemove.add(entry.getKey());
}
}
if (toRemove != null) {
for (Integer uid : toRemove) {
onlinePrivacy.remove(uid);
}
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS));
2014-11-21 01:14:44 +01:00
}
}
2015-09-24 22:52:02 +02:00
if (shortPollChannels.size() != 0) {
for (int a = 0; a < shortPollChannels.size(); a++) {
int key = shortPollChannels.keyAt(a);
2018-07-30 04:07:02 +02:00
int timeout = shortPollChannels.valueAt(a);
2015-09-24 22:52:02 +02:00
if (timeout < System.currentTimeMillis() / 1000) {
shortPollChannels.delete(key);
2019-01-23 18:03:33 +01:00
a--;
2015-11-26 22:04:02 +01:00
if (needShortPollChannels.indexOfKey(key) >= 0) {
2015-09-24 22:52:02 +02:00
getChannelDifference(key);
}
}
}
}
2019-01-23 18:03:33 +01:00
if (shortPollOnlines.size() != 0) {
long time = SystemClock.uptimeMillis() / 1000;
for (int a = 0; a < shortPollOnlines.size(); a++) {
int key = shortPollOnlines.keyAt(a);
int timeout = shortPollOnlines.valueAt(a);
if (timeout < time) {
if (needShortPollChannels.indexOfKey(key) >= 0) {
shortPollOnlines.put(key, (int) (time + 60 * 5));
} else {
shortPollOnlines.delete(key);
a--;
}
TLRPC.TL_messages_getOnlines req = new TLRPC.TL_messages_getOnlines();
req.peer = getInputPeer(-key);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (response != null) {
TLRPC.TL_chatOnlines res = (TLRPC.TL_chatOnlines) response;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatOnlineCount(key, res.onlines);
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.chatOnlineCountDidLoad, key, res.onlines));
2019-01-23 18:03:33 +01:00
}
});
}
}
}
if (!printingUsers.isEmpty() || lastPrintingStringCount != printingUsers.size()) {
boolean updated = false;
2015-01-02 23:15:07 +01:00
ArrayList<Long> keys = new ArrayList<>(printingUsers.keySet());
2013-10-25 17:19:00 +02:00
for (int b = 0; b < keys.size(); b++) {
2018-07-30 04:07:02 +02:00
long key = keys.get(b);
2013-10-25 17:19:00 +02:00
ArrayList<PrintingUser> arr = printingUsers.get(key);
2018-07-30 04:07:02 +02:00
if (arr != null) {
for (int a = 0; a < arr.size(); a++) {
PrintingUser user = arr.get(a);
int timeToRemove;
if (user.action instanceof TLRPC.TL_sendMessageGamePlayAction) {
timeToRemove = 30000;
} else {
timeToRemove = 5900;
}
if (user.lastTime + timeToRemove < currentTime) {
updated = true;
arr.remove(user);
a--;
}
2013-10-25 17:19:00 +02:00
}
}
2018-07-30 04:07:02 +02:00
if (arr == null || arr.isEmpty()) {
2013-10-25 17:19:00 +02:00
printingUsers.remove(key);
keys.remove(b);
b--;
}
}
updatePrintingStrings();
if (updated) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT));
2013-10-25 17:19:00 +02:00
}
}
2018-07-30 04:07:02 +02:00
if (Theme.selectedAutoNightType == Theme.AUTO_NIGHT_TYPE_SCHEDULED && Math.abs(currentTime - lastThemeCheckTime) >= 60) {
AndroidUtilities.runOnUIThread(themeCheckRunnable);
lastThemeCheckTime = currentTime;
}
2019-07-18 15:01:39 +02:00
if (getUserConfig().savedPasswordHash != null && Math.abs(currentTime - lastPasswordCheckTime) >= 60) {
2018-07-30 04:07:02 +02:00
AndroidUtilities.runOnUIThread(passwordCheckRunnable);
lastPasswordCheckTime = currentTime;
}
if (lastPushRegisterSendTime != 0 && Math.abs(SystemClock.elapsedRealtime() - lastPushRegisterSendTime) >= 3 * 60 * 60 * 1000) {
2019-01-23 18:03:33 +01:00
GcmPushListenerService.sendRegistrationToServer(SharedConfig.pushString);
2018-07-30 04:07:02 +02:00
}
2019-07-18 15:01:39 +02:00
getLocationController().update();
2018-07-30 04:07:02 +02:00
checkProxyInfoInternal(false);
checkTosUpdate();
}
private void checkTosUpdate() {
2019-07-18 15:01:39 +02:00
if (nextTosCheckTime > getConnectionsManager().getCurrentTime() || checkingTosUpdate || !getUserConfig().isClientActivated()) {
2018-07-30 04:07:02 +02:00
return;
}
checkingTosUpdate = true;
TLRPC.TL_help_getTermsOfServiceUpdate req = new TLRPC.TL_help_getTermsOfServiceUpdate();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
checkingTosUpdate = false;
if (response instanceof TLRPC.TL_help_termsOfServiceUpdateEmpty) {
TLRPC.TL_help_termsOfServiceUpdateEmpty res = (TLRPC.TL_help_termsOfServiceUpdateEmpty) response;
nextTosCheckTime = res.expires;
} else if (response instanceof TLRPC.TL_help_termsOfServiceUpdate) {
final TLRPC.TL_help_termsOfServiceUpdate res = (TLRPC.TL_help_termsOfServiceUpdate) response;
nextTosCheckTime = res.expires;
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.needShowAlert, 4, res.terms_of_service));
2018-08-27 10:33:11 +02:00
} else {
2019-07-18 15:01:39 +02:00
nextTosCheckTime = getConnectionsManager().getCurrentTime() + 60 * 60;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
notificationsPreferences.edit().putInt("nextTosCheckTime", nextTosCheckTime).commit();
2018-07-30 04:07:02 +02:00
});
}
public void checkProxyInfo(final boolean reset) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> checkProxyInfoInternal(reset));
2018-07-30 04:07:02 +02:00
}
private void checkProxyInfoInternal(boolean reset) {
if (reset && checkingProxyInfo) {
checkingProxyInfo = false;
}
2019-07-18 15:01:39 +02:00
if (!reset && nextProxyInfoCheckTime > getConnectionsManager().getCurrentTime() || checkingProxyInfo) {
2018-07-30 04:07:02 +02:00
return;
}
2019-03-03 21:40:48 +01:00
if (checkingProxyInfoRequestId != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().cancelRequest(checkingProxyInfoRequestId, true);
2019-03-03 21:40:48 +01:00
checkingProxyInfoRequestId = 0;
}
2018-07-30 04:07:02 +02:00
SharedPreferences preferences = getGlobalMainSettings();
boolean enabled = preferences.getBoolean("proxy_enabled", false);
String proxyAddress = preferences.getString("proxy_ip", "");
String proxySecret = preferences.getString("proxy_secret", "");
2019-03-03 21:40:48 +01:00
int removeCurrent = 0;
if (proxyDialogId != 0 && proxyDialogAddress != null && !proxyDialogAddress.equals(proxyAddress + proxySecret)) {
removeCurrent = 1;
}
2019-07-18 15:01:39 +02:00
lastCheckProxyId++;
2018-07-30 04:07:02 +02:00
if (enabled && !TextUtils.isEmpty(proxyAddress) && !TextUtils.isEmpty(proxySecret)) {
checkingProxyInfo = true;
2019-07-18 15:01:39 +02:00
int checkProxyId = lastCheckProxyId;
2018-07-30 04:07:02 +02:00
TLRPC.TL_help_getProxyData req = new TLRPC.TL_help_getProxyData();
2019-07-18 15:01:39 +02:00
checkingProxyInfoRequestId = getConnectionsManager().sendRequest(req, (response, error) -> {
if (checkProxyId != lastCheckProxyId) {
2018-08-27 10:33:11 +02:00
return;
}
boolean noDialog = false;
if (response instanceof TLRPC.TL_help_proxyDataEmpty) {
TLRPC.TL_help_proxyDataEmpty res = (TLRPC.TL_help_proxyDataEmpty) response;
nextProxyInfoCheckTime = res.expires;
noDialog = true;
} else if (response instanceof TLRPC.TL_help_proxyDataPromo) {
final TLRPC.TL_help_proxyDataPromo res = (TLRPC.TL_help_proxyDataPromo) response;
final long did;
if (res.peer.user_id != 0) {
did = res.peer.user_id;
} else if (res.peer.chat_id != 0) {
did = -res.peer.chat_id;
for (int a = 0; a < res.chats.size(); a++) {
TLRPC.Chat chat = res.chats.get(a);
if (chat.id == res.peer.chat_id) {
if (chat.kicked || chat.restricted) {
noDialog = true;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
break;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
} else {
did = -res.peer.channel_id;
for (int a = 0; a < res.chats.size(); a++) {
TLRPC.Chat chat = res.chats.get(a);
if (chat.id == res.peer.channel_id) {
if (chat.kicked || chat.restricted) {
noDialog = true;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
break;
2018-07-30 04:07:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
proxyDialogId = did;
2019-03-03 21:40:48 +01:00
proxyDialogAddress = proxyAddress + proxySecret;
getGlobalMainSettings().edit().putLong("proxy_dialog", proxyDialogId).putString("proxyDialogAddress", proxyDialogAddress).commit();
2018-08-27 10:33:11 +02:00
nextProxyInfoCheckTime = res.expires;
if (!noDialog) {
AndroidUtilities.runOnUIThread(() -> {
proxyDialog = dialogs_dict.get(did);
if (proxyDialog != null) {
checkingProxyInfo = false;
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2018-08-27 10:33:11 +02:00
} else {
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>();
for (int a = 0; a < res.users.size(); a++) {
TLRPC.User u = res.users.get(a);
usersDict.put(u.id, u);
}
for (int a = 0; a < res.chats.size(); a++) {
TLRPC.Chat c = res.chats.get(a);
chatsDict.put(c.id, c);
}
TLRPC.TL_messages_getPeerDialogs req1 = new TLRPC.TL_messages_getPeerDialogs();
TLRPC.TL_inputDialogPeer peer = new TLRPC.TL_inputDialogPeer();
if (res.peer.user_id != 0) {
peer.peer = new TLRPC.TL_inputPeerUser();
peer.peer.user_id = res.peer.user_id;
TLRPC.User user = usersDict.get(res.peer.user_id);
if (user != null) {
peer.peer.access_hash = user.access_hash;
}
} else if (res.peer.chat_id != 0) {
peer.peer = new TLRPC.TL_inputPeerChat();
peer.peer.chat_id = res.peer.chat_id;
TLRPC.Chat chat = chatsDict.get(res.peer.chat_id);
if (chat != null) {
peer.peer.access_hash = chat.access_hash;
}
} else {
peer.peer = new TLRPC.TL_inputPeerChannel();
peer.peer.channel_id = res.peer.channel_id;
TLRPC.Chat chat = chatsDict.get(res.peer.channel_id);
if (chat != null) {
peer.peer.access_hash = chat.access_hash;
}
}
2018-07-30 04:07:02 +02:00
2018-08-27 10:33:11 +02:00
req1.peers.add(peer);
2019-07-18 15:01:39 +02:00
checkingProxyInfoRequestId = getConnectionsManager().sendRequest(req1, (response1, error1) -> {
if (checkProxyId != lastCheckProxyId) {
2018-08-27 10:33:11 +02:00
return;
}
checkingProxyInfoRequestId = 0;
final TLRPC.TL_messages_peerDialogs res2 = (TLRPC.TL_messages_peerDialogs) response1;
if (res2 != null && !res2.dialogs.isEmpty()) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, res.chats, true, true);
2018-08-27 10:33:11 +02:00
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
dialogs.chats = res2.chats;
dialogs.users = res2.users;
dialogs.dialogs = res2.dialogs;
dialogs.messages = res2.messages;
2019-07-18 15:01:39 +02:00
getMessagesStorage().putDialogs(dialogs, 2);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(res.users, false);
putChats(res.chats, false);
putUsers(res2.users, false);
putChats(res2.chats, false);
2019-07-18 15:01:39 +02:00
if (proxyDialog != null) {
int lowerId = (int) proxyDialog.id;
if (lowerId < 0) {
TLRPC.Chat chat = getChat(-lowerId);
if (ChatObject.isNotInChat(chat) || chat.restricted) {
removeDialog(proxyDialog);
}
} else {
removeDialog(proxyDialog);
}
}
2018-08-27 10:33:11 +02:00
proxyDialog = res2.dialogs.get(0);
proxyDialog.id = did;
2019-05-14 14:08:05 +02:00
proxyDialog.folder_id = 0;
2018-08-27 10:33:11 +02:00
if (DialogObject.isChannel(proxyDialog)) {
channelsPts.put(-(int) proxyDialog.id, proxyDialog.pts);
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
Integer value = dialogs_read_inbox_max.get(proxyDialog.id);
if (value == null) {
value = 0;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
dialogs_read_inbox_max.put(proxyDialog.id, Math.max(value, proxyDialog.read_inbox_max_id));
value = dialogs_read_outbox_max.get(proxyDialog.id);
if (value == null) {
value = 0;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(proxyDialog.id, Math.max(value, proxyDialog.read_outbox_max_id));
dialogs_dict.put(did, proxyDialog);
if (!res2.messages.isEmpty()) {
final SparseArray<TLRPC.User> usersDict1 = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict1 = new SparseArray<>();
for (int a = 0; a < res2.users.size(); a++) {
TLRPC.User u = res2.users.get(a);
usersDict1.put(u.id, u);
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
for (int a = 0; a < res2.chats.size(); a++) {
TLRPC.Chat c = res2.chats.get(a);
chatsDict1.put(c.id, c);
}
MessageObject messageObject = new MessageObject(currentAccount, res2.messages.get(0), usersDict1, chatsDict1, false);
dialogMessage.put(did, messageObject);
if (proxyDialog.last_message_date == 0) {
proxyDialog.last_message_date = messageObject.messageOwner.date;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2018-08-27 10:33:11 +02:00
});
} else {
AndroidUtilities.runOnUIThread(() -> {
if (proxyDialog != null) {
2019-06-04 12:14:50 +02:00
int lowerId = (int) proxyDialog.id;
if (lowerId < 0) {
TLRPC.Chat chat = getChat(-lowerId);
if (ChatObject.isNotInChat(chat) || chat.restricted) {
2019-05-14 14:08:05 +02:00
removeDialog(proxyDialog);
2018-08-27 10:33:11 +02:00
}
2019-06-04 12:14:50 +02:00
} else {
removeDialog(proxyDialog);
2018-08-27 10:33:11 +02:00
}
proxyDialog = null;
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-07-30 04:07:02 +02:00
}
});
}
2018-08-27 10:33:11 +02:00
checkingProxyInfo = false;
});
2018-07-30 04:07:02 +02:00
}
});
}
2018-08-27 10:33:11 +02:00
} else {
2019-07-18 15:01:39 +02:00
nextProxyInfoCheckTime = getConnectionsManager().getCurrentTime() + 60 * 60;
2018-08-27 10:33:11 +02:00
noDialog = true;
}
if (noDialog) {
proxyDialogId = 0;
2019-03-03 21:40:48 +01:00
getGlobalMainSettings().edit().putLong("proxy_dialog", proxyDialogId).remove("proxyDialogAddress").commit();
2018-08-27 10:33:11 +02:00
checkingProxyInfoRequestId = 0;
checkingProxyInfo = false;
AndroidUtilities.runOnUIThread(() -> {
if (proxyDialog != null) {
2019-06-04 12:14:50 +02:00
int lowerId = (int) proxyDialog.id;
if (lowerId < 0) {
TLRPC.Chat chat = getChat(-lowerId);
if (ChatObject.isNotInChat(chat) || chat.restricted) {
2019-05-14 14:08:05 +02:00
removeDialog(proxyDialog);
2018-08-27 10:33:11 +02:00
}
2019-06-04 12:14:50 +02:00
} else {
removeDialog(proxyDialog);
2018-08-27 10:33:11 +02:00
}
proxyDialog = null;
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
}
});
2018-07-30 04:07:02 +02:00
}
});
} else {
2019-03-03 21:40:48 +01:00
removeCurrent = 2;
}
if (removeCurrent != 0) {
2018-07-30 04:07:02 +02:00
proxyDialogId = 0;
2019-03-03 21:40:48 +01:00
proxyDialogAddress = null;
getGlobalMainSettings().edit().putLong("proxy_dialog", proxyDialogId).remove("proxyDialogAddress").commit();
2019-07-18 15:01:39 +02:00
nextProxyInfoCheckTime = getConnectionsManager().getCurrentTime() + 60 * 60;
2019-03-03 21:40:48 +01:00
if (removeCurrent == 2) {
checkingProxyInfo = false;
if (checkingProxyInfoRequestId != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().cancelRequest(checkingProxyInfoRequestId, true);
2019-03-03 21:40:48 +01:00
checkingProxyInfoRequestId = 0;
}
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (proxyDialog != null) {
2019-06-04 12:14:50 +02:00
int lowerId = (int) proxyDialog.id;
if (lowerId < 0) {
TLRPC.Chat chat = getChat(-lowerId);
if (ChatObject.isNotInChat(chat) || chat.restricted) {
2019-05-14 14:08:05 +02:00
removeDialog(proxyDialog);
2018-07-30 04:07:02 +02:00
}
2019-06-04 12:14:50 +02:00
} else {
removeDialog(proxyDialog);
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
proxyDialog = null;
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-07-30 04:07:02 +02:00
}
});
}
}
2019-06-04 12:14:50 +02:00
public boolean isProxyDialog(long did, boolean checkLeft) {
return proxyDialog != null && proxyDialog.id == did && (!checkLeft || isLeftProxyChannel);
2013-10-25 17:19:00 +02:00
}
private String getUserNameForTyping(TLRPC.User user) {
if (user == null) {
return "";
}
if (user.first_name != null && user.first_name.length() > 0) {
return user.first_name;
} else if (user.last_name != null && user.last_name.length() > 0) {
return user.last_name;
}
return "";
}
private void updatePrintingStrings() {
2018-07-30 04:07:02 +02:00
final LongSparseArray<CharSequence> newPrintingStrings = new LongSparseArray<>();
final LongSparseArray<Integer> newPrintingStringsTypes = new LongSparseArray<>();
2013-10-25 17:19:00 +02:00
for (HashMap.Entry<Long, ArrayList<PrintingUser>> entry : printingUsers.entrySet()) {
long key = entry.getKey();
ArrayList<PrintingUser> arr = entry.getValue();
int lower_id = (int) key;
if (lower_id > 0 || lower_id == 0 || arr.size() == 1) {
PrintingUser pu = arr.get(0);
TLRPC.User user = getUser(pu.userId);
if (user == null) {
2017-12-08 18:35:59 +01:00
continue;
}
2016-04-22 15:49:00 +02:00
if (pu.action instanceof TLRPC.TL_sendMessageRecordAudioAction) {
if (lower_id < 0) {
newPrintingStrings.put(key, LocaleController.formatString("IsRecordingAudio", R.string.IsRecordingAudio, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("RecordingAudio", R.string.RecordingAudio));
}
newPrintingStringsTypes.put(key, 1);
2017-07-08 18:32:04 +02:00
} else if (pu.action instanceof TLRPC.TL_sendMessageRecordRoundAction || pu.action instanceof TLRPC.TL_sendMessageUploadRoundAction) {
if (lower_id < 0) {
newPrintingStrings.put(key, LocaleController.formatString("IsRecordingRound", R.string.IsRecordingRound, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("RecordingRound", R.string.RecordingRound));
}
newPrintingStringsTypes.put(key, 4);
2016-04-22 15:49:00 +02:00
} else if (pu.action instanceof TLRPC.TL_sendMessageUploadAudioAction) {
if (lower_id < 0) {
newPrintingStrings.put(key, LocaleController.formatString("IsSendingAudio", R.string.IsSendingAudio, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("SendingAudio", R.string.SendingAudio));
}
newPrintingStringsTypes.put(key, 2);
} else if (pu.action instanceof TLRPC.TL_sendMessageUploadVideoAction || pu.action instanceof TLRPC.TL_sendMessageRecordVideoAction) {
if (lower_id < 0) {
newPrintingStrings.put(key, LocaleController.formatString("IsSendingVideo", R.string.IsSendingVideo, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("SendingVideoStatus", R.string.SendingVideoStatus));
}
newPrintingStringsTypes.put(key, 2);
} else if (pu.action instanceof TLRPC.TL_sendMessageUploadDocumentAction) {
if (lower_id < 0) {
newPrintingStrings.put(key, LocaleController.formatString("IsSendingFile", R.string.IsSendingFile, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("SendingFile", R.string.SendingFile));
}
newPrintingStringsTypes.put(key, 2);
} else if (pu.action instanceof TLRPC.TL_sendMessageUploadPhotoAction) {
if (lower_id < 0) {
newPrintingStrings.put(key, LocaleController.formatString("IsSendingPhoto", R.string.IsSendingPhoto, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("SendingPhoto", R.string.SendingPhoto));
}
newPrintingStringsTypes.put(key, 2);
2016-10-11 13:57:01 +02:00
} else if (pu.action instanceof TLRPC.TL_sendMessageGamePlayAction) {
2017-03-31 01:58:05 +02:00
if (lower_id < 0) {
2016-10-11 13:57:01 +02:00
newPrintingStrings.put(key, LocaleController.formatString("IsSendingGame", R.string.IsSendingGame, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("SendingGame", R.string.SendingGame));
}
2017-03-31 01:58:05 +02:00
newPrintingStringsTypes.put(key, 3);
} else {
if (lower_id < 0) {
2017-12-08 18:35:59 +01:00
newPrintingStrings.put(key, LocaleController.formatString("IsTypingGroup", R.string.IsTypingGroup, getUserNameForTyping(user)));
} else {
newPrintingStrings.put(key, LocaleController.getString("Typing", R.string.Typing));
}
newPrintingStringsTypes.put(key, 0);
}
2013-10-25 17:19:00 +02:00
} else {
int count = 0;
2017-12-08 18:35:59 +01:00
StringBuilder label = new StringBuilder();
2013-10-25 17:19:00 +02:00
for (PrintingUser pu : arr) {
TLRPC.User user = getUser(pu.userId);
2013-10-25 17:19:00 +02:00
if (user != null) {
if (label.length() != 0) {
2017-12-08 18:35:59 +01:00
label.append(", ");
2013-10-25 17:19:00 +02:00
}
2017-12-08 18:35:59 +01:00
label.append(getUserNameForTyping(user));
2013-10-25 17:19:00 +02:00
count++;
}
if (count == 2) {
break;
}
}
if (label.length() != 0) {
2015-10-29 18:10:07 +01:00
if (count == 1) {
2017-12-08 18:35:59 +01:00
newPrintingStrings.put(key, LocaleController.formatString("IsTypingGroup", R.string.IsTypingGroup, label.toString()));
2013-10-25 17:19:00 +02:00
} else {
2015-10-29 18:10:07 +01:00
if (arr.size() > 2) {
2017-12-08 18:35:59 +01:00
String plural = LocaleController.getPluralString("AndMoreTypingGroup", arr.size() - 2);
2019-01-23 18:03:33 +01:00
try {
newPrintingStrings.put(key, String.format(plural, label.toString(), arr.size() - 2));
} catch (Exception e) {
newPrintingStrings.put(key, "LOC_ERR: AndMoreTypingGroup");
}
2015-10-29 18:10:07 +01:00
} else {
2017-12-08 18:35:59 +01:00
newPrintingStrings.put(key, LocaleController.formatString("AreTypingGroup", R.string.AreTypingGroup, label.toString()));
2015-10-29 18:10:07 +01:00
}
2013-10-25 17:19:00 +02:00
}
newPrintingStringsTypes.put(key, 0);
2013-10-25 17:19:00 +02:00
}
}
}
lastPrintingStringCount = newPrintingStrings.size();
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
printingStrings = newPrintingStrings;
printingStringsTypes = newPrintingStringsTypes;
2013-10-25 17:19:00 +02:00
});
}
public void cancelTyping(int action, long dialog_id) {
2018-07-30 04:07:02 +02:00
LongSparseArray<Boolean> typings = sendingTypings.get(action);
if (typings != null) {
typings.remove(dialog_id);
}
2014-08-23 01:22:33 +02:00
}
public void sendTyping(final long dialog_id, final int action, int classGuid) {
2013-10-25 17:19:00 +02:00
if (dialog_id == 0) {
return;
}
2018-07-30 04:07:02 +02:00
LongSparseArray<Boolean> typings = sendingTypings.get(action);
if (typings != null && typings.get(dialog_id) != null) {
2014-08-23 01:22:33 +02:00
return;
}
if (typings == null) {
2018-07-30 04:07:02 +02:00
typings = new LongSparseArray<>();
sendingTypings.put(action, typings);
}
2015-05-21 23:27:27 +02:00
int lower_part = (int) dialog_id;
int high_id = (int) (dialog_id >> 32);
2013-10-25 17:19:00 +02:00
if (lower_part != 0) {
if (high_id == 1) {
return;
}
2013-10-25 17:19:00 +02:00
TLRPC.TL_messages_setTyping req = new TLRPC.TL_messages_setTyping();
2015-09-24 22:52:02 +02:00
req.peer = getInputPeer(lower_part);
2015-11-26 22:04:02 +01:00
if (req.peer instanceof TLRPC.TL_inputPeerChannel) {
2016-03-16 13:26:32 +01:00
TLRPC.Chat chat = getChat(req.peer.channel_id);
2015-11-26 22:04:02 +01:00
if (chat == null || !chat.megagroup) {
return;
}
}
if (req.peer == null) {
2015-09-24 22:52:02 +02:00
return;
2013-10-25 17:19:00 +02:00
}
if (action == 0) {
req.action = new TLRPC.TL_sendMessageTypingAction();
} else if (action == 1) {
req.action = new TLRPC.TL_sendMessageRecordAudioAction();
} else if (action == 2) {
req.action = new TLRPC.TL_sendMessageCancelAction();
} else if (action == 3) {
req.action = new TLRPC.TL_sendMessageUploadDocumentAction();
} else if (action == 4) {
req.action = new TLRPC.TL_sendMessageUploadPhotoAction();
} else if (action == 5) {
req.action = new TLRPC.TL_sendMessageUploadVideoAction();
2017-03-31 01:58:05 +02:00
} else if (action == 6) {
req.action = new TLRPC.TL_sendMessageGamePlayAction();
2017-07-08 18:32:04 +02:00
} else if (action == 7) {
req.action = new TLRPC.TL_sendMessageRecordRoundAction();
} else if (action == 8) {
req.action = new TLRPC.TL_sendMessageUploadRoundAction();
2018-07-30 04:07:02 +02:00
} else if (action == 9) {
req.action = new TLRPC.TL_sendMessageUploadAudioAction();
}
typings.put(dialog_id, true);
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
LongSparseArray<Boolean> typings1 = sendingTypings.get(action);
if (typings1 != null) {
typings1.remove(dialog_id);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
}), ConnectionsManager.RequestFlagFailOnServerErrors);
if (classGuid != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
}
2013-10-25 17:19:00 +02:00
} else {
if (action != 0) {
return;
}
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
if (chat.auth_key != null && chat.auth_key.length > 1 && chat instanceof TLRPC.TL_encryptedChat) {
TLRPC.TL_messages_setEncryptedTyping req = new TLRPC.TL_messages_setEncryptedTyping();
req.peer = new TLRPC.TL_inputEncryptedChat();
req.peer.chat_id = chat.id;
req.peer.access_hash = chat.access_hash;
req.typing = true;
typings.put(dialog_id, true);
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
LongSparseArray<Boolean> typings12 = sendingTypings.get(action);
if (typings12 != null) {
typings12.remove(dialog_id);
}
2018-08-27 10:33:11 +02:00
}), ConnectionsManager.RequestFlagFailOnServerErrors);
if (classGuid != 0) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
}
2013-10-25 17:19:00 +02:00
}
}
}
2019-05-14 14:08:05 +02:00
protected void removeDeletedMessagesFromArray(final long dialog_id, ArrayList<TLRPC.Message> messages) {
int maxDeletedId = deletedHistory.get(dialog_id, 0);
if (maxDeletedId == 0) {
return;
}
for (int a = 0, N = messages.size(); a < N; a++) {
TLRPC.Message message = messages.get(a);
if (message.id <= maxDeletedId) {
messages.remove(a);
a--;
N--;
}
}
}
2017-03-31 01:58:05 +02:00
public void loadMessages(final long dialog_id, final int count, final int max_id, final int offset_date, boolean fromCache, int midDate, final int classGuid, final int load_type, final int last_message_id, final boolean isChannel, final int loadIndex) {
2017-12-08 18:35:59 +01:00
loadMessages(dialog_id, count, max_id, offset_date, fromCache, midDate, classGuid, load_type, last_message_id, isChannel, loadIndex, 0, 0, 0, false, 0);
2015-12-09 19:27:52 +01:00
}
2018-07-30 04:07:02 +02:00
public void loadMessages(final long dialog_id, final int count, final int max_id, final int offset_date, final boolean fromCache, final int midDate, final int classGuid, final int load_type, final int last_message_id, final boolean isChannel, final int loadIndex, final int first_unread, final int unread_count, final int last_date, final boolean queryFromServer, final int mentionsCount) {
loadMessagesInternal(dialog_id, count, max_id, offset_date, fromCache, midDate, classGuid, load_type, last_message_id, isChannel, loadIndex, first_unread, unread_count, last_date, queryFromServer, mentionsCount, true);
}
private void loadMessagesInternal(final long dialog_id, final int count, final int max_id, final int offset_date, final boolean fromCache, final int midDate, final int classGuid, final int load_type, final int last_message_id, final boolean isChannel, final int loadIndex, final int first_unread, final int unread_count, final int last_date, final boolean queryFromServer, final int mentionsCount, boolean loadDialog) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("load messages in chat " + dialog_id + " count " + count + " max_id " + max_id + " cache " + fromCache + " mindate = " + midDate + " guid " + classGuid + " load_type " + load_type + " last_message_id " + last_message_id + " index " + loadIndex + " firstUnread " + first_unread + " unread_count " + unread_count + " last_date " + last_date + " queryFromServer " + queryFromServer);
}
2015-05-21 23:27:27 +02:00
int lower_part = (int) dialog_id;
2013-10-25 17:19:00 +02:00
if (fromCache || lower_part == 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().getMessages(dialog_id, count, max_id, offset_date, midDate, classGuid, load_type, isChannel, loadIndex);
2013-10-25 17:19:00 +02:00
} else {
2018-07-30 04:07:02 +02:00
if (loadDialog && (load_type == 3 || load_type == 2) && last_message_id == 0) {
TLRPC.TL_messages_getPeerDialogs req = new TLRPC.TL_messages_getPeerDialogs();
TLRPC.InputPeer inputPeer = getInputPeer((int) dialog_id);
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = inputPeer;
req.peers.add(inputDialogPeer);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
TLRPC.TL_messages_peerDialogs res = (TLRPC.TL_messages_peerDialogs) response;
if (!res.dialogs.isEmpty()) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = res.dialogs.get(0);
2018-07-30 04:07:02 +02:00
2018-08-27 10:33:11 +02:00
if (dialog.top_message != 0) {
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
dialogs.chats = res.chats;
dialogs.users = res.users;
dialogs.dialogs = res.dialogs;
dialogs.messages = res.messages;
2019-07-18 15:01:39 +02:00
getMessagesStorage().putDialogs(dialogs, 0);
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
loadMessagesInternal(dialog_id, count, max_id, offset_date, false, midDate, classGuid, load_type, dialog.top_message, isChannel, loadIndex, first_unread, dialog.unread_count, last_date, queryFromServer, dialog.unread_mentions_count, false);
2018-07-30 04:07:02 +02:00
}
}
});
return;
}
TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory();
req.peer = getInputPeer(lower_part);
2017-03-31 01:58:05 +02:00
if (load_type == 4) {
req.add_offset = -count + 5;
} else if (load_type == 3) {
req.add_offset = -count / 2;
} else if (load_type == 1) {
req.add_offset = -count - 1;
} else if (load_type == 2 && max_id != 0) {
req.add_offset = -count + 6;
} else {
if (lower_part < 0 && max_id != 0) {
TLRPC.Chat chat = getChat(-lower_part);
if (ChatObject.isChannel(chat)) {
2015-09-24 22:52:02 +02:00
req.add_offset = -1;
req.limit += 1;
}
2015-05-21 23:27:27 +02:00
}
2014-11-18 06:01:04 +01:00
}
req.limit = count;
req.offset_id = max_id;
2017-03-31 01:58:05 +02:00
req.offset_date = offset_date;
2019-07-18 15:01:39 +02:00
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
final TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
2019-05-14 14:08:05 +02:00
removeDeletedMessagesFromArray(dialog_id, res.messages);
2018-08-27 10:33:11 +02:00
if (res.messages.size() > count) {
res.messages.remove(0);
}
int mid = max_id;
if (offset_date != 0 && !res.messages.isEmpty()) {
mid = res.messages.get(res.messages.size() - 1).id;
for (int a = res.messages.size() - 1; a >= 0; a--) {
TLRPC.Message message = res.messages.get(a);
if (message.date > offset_date) {
mid = message.id;
break;
2017-03-31 01:58:05 +02:00
}
}
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
processLoadedMessages(res, dialog_id, count, mid, offset_date, false, classGuid, first_unread, last_message_id, unread_count, last_date, load_type, isChannel, false, loadIndex, queryFromServer, mentionsCount);
2013-10-25 17:19:00 +02:00
}
});
2019-07-18 15:01:39 +02:00
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
2013-10-25 17:19:00 +02:00
}
}
2016-03-06 02:49:31 +01:00
public void reloadWebPages(final long dialog_id, HashMap<String, ArrayList<MessageObject>> webpagesToReload) {
for (HashMap.Entry<String, ArrayList<MessageObject>> entry : webpagesToReload.entrySet()) {
final String url = entry.getKey();
final ArrayList<MessageObject> messages = entry.getValue();
ArrayList<MessageObject> arrayList = reloadingWebpages.get(url);
if (arrayList == null) {
arrayList = new ArrayList<>();
reloadingWebpages.put(url, arrayList);
}
arrayList.addAll(messages);
TLRPC.TL_messages_getWebPagePreview req = new TLRPC.TL_messages_getWebPagePreview();
req.message = url;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
ArrayList<MessageObject> arrayList1 = reloadingWebpages.remove(url);
if (arrayList1 == null) {
return;
}
TLRPC.TL_messages_messages messagesRes = new TLRPC.TL_messages_messages();
if (!(response instanceof TLRPC.TL_messageMediaWebPage)) {
for (int a = 0; a < arrayList1.size(); a++) {
arrayList1.get(a).messageOwner.media.webpage = new TLRPC.TL_webPageEmpty();
messagesRes.messages.add(arrayList1.get(a).messageOwner);
}
} else {
TLRPC.TL_messageMediaWebPage media = (TLRPC.TL_messageMediaWebPage) response;
if (media.webpage instanceof TLRPC.TL_webPage || media.webpage instanceof TLRPC.TL_webPageEmpty) {
for (int a = 0; a < arrayList1.size(); a++) {
arrayList1.get(a).messageOwner.media.webpage = media.webpage;
if (a == 0) {
ImageLoader.saveMessageThumbs(arrayList1.get(a).messageOwner);
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
messagesRes.messages.add(arrayList1.get(a).messageOwner);
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
} else {
reloadingWebpagesPending.put(media.webpage.id, arrayList1);
}
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
if (!messagesRes.messages.isEmpty()) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(messagesRes, dialog_id, -2, 0, false);
getNotificationCenter().postNotificationName(NotificationCenter.replaceMessagesObjects, dialog_id, arrayList1);
2018-08-27 10:33:11 +02:00
}
}));
2016-03-06 02:49:31 +01:00
}
}
2017-03-31 01:58:05 +02:00
public void processLoadedMessages(final TLRPC.messages_Messages messagesRes, final long dialog_id, final int count, final int max_id, final int offset_date, final boolean isCache, final int classGuid,
2017-12-08 18:35:59 +01:00
final int first_unread, final int last_message_id, final int unread_count, final int last_date, final int load_type, final boolean isChannel, final boolean isEnd, final int loadIndex, final boolean queryFromServer, final int mentionsCount) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("processLoadedMessages size " + messagesRes.messages.size() + " in chat " + dialog_id + " count " + count + " max_id " + max_id + " cache " + isCache + " guid " + classGuid + " load_type " + load_type + " last_message_id " + last_message_id + " isChannel " + isChannel + " index " + loadIndex + " firstUnread " + first_unread + " unread_count " + unread_count + " last_date " + last_date + " queryFromServer " + queryFromServer);
}
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
boolean createDialog = false;
boolean isMegagroup = false;
if (messagesRes instanceof TLRPC.TL_messages_channelMessages) {
int channelId = -(int) dialog_id;
int channelPts = channelsPts.get(channelId);
if (channelPts == 0) {
2019-07-18 15:01:39 +02:00
channelPts = getMessagesStorage().getChannelPtsSync(channelId);
2018-07-30 04:07:02 +02:00
if (channelPts == 0) {
2018-08-27 10:33:11 +02:00
channelsPts.put(channelId, messagesRes.pts);
createDialog = true;
if (needShortPollChannels.indexOfKey(channelId) >= 0 && shortPollChannels.indexOfKey(channelId) < 0) {
getChannelDifference(channelId, 2, 0, null);
} else {
getChannelDifference(channelId);
2015-11-26 22:04:02 +01:00
}
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
for (int a = 0; a < messagesRes.chats.size(); a++) {
TLRPC.Chat chat = messagesRes.chats.get(a);
if (chat.id == channelId) {
isMegagroup = chat.megagroup;
break;
}
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
}
int lower_id = (int) dialog_id;
int high_id = (int) (dialog_id >> 32);
if (!isCache) {
ImageLoader.saveMessagesThumbs(messagesRes.messages);
}
if (high_id != 1 && lower_id != 0 && isCache && messagesRes.messages.size() == 0) {
AndroidUtilities.runOnUIThread(() -> loadMessages(dialog_id, count, load_type == 2 && queryFromServer ? first_unread : max_id, offset_date, false, 0, classGuid, load_type, last_message_id, isChannel, loadIndex, first_unread, unread_count, last_date, queryFromServer, mentionsCount));
return;
}
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>();
for (int a = 0; a < messagesRes.users.size(); a++) {
TLRPC.User u = messagesRes.users.get(a);
usersDict.put(u.id, u);
}
for (int a = 0; a < messagesRes.chats.size(); a++) {
TLRPC.Chat c = messagesRes.chats.get(a);
chatsDict.put(c.id, c);
}
int size = messagesRes.messages.size();
if (!isCache) {
Integer inboxValue = dialogs_read_inbox_max.get(dialog_id);
if (inboxValue == null) {
2019-07-18 15:01:39 +02:00
inboxValue = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_inbox_max.put(dialog_id, inboxValue);
2015-06-29 19:12:11 +02:00
}
2018-08-27 10:33:11 +02:00
Integer outboxValue = dialogs_read_outbox_max.get(dialog_id);
if (outboxValue == null) {
2019-07-18 15:01:39 +02:00
outboxValue = getMessagesStorage().getDialogReadMax(true, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(dialog_id, outboxValue);
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
for (int a = 0; a < size; a++) {
TLRPC.Message message = messagesRes.messages.get(a);
if (isMegagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
2018-08-27 10:33:11 +02:00
if (message.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
TLRPC.User user = usersDict.get(message.action.user_id);
if (user != null && user.bot) {
message.reply_markup = new TLRPC.TL_replyKeyboardHide();
message.flags |= 64;
2015-06-29 19:12:11 +02:00
}
}
2018-08-27 10:33:11 +02:00
if (message.action instanceof TLRPC.TL_messageActionChatMigrateTo || message.action instanceof TLRPC.TL_messageActionChannelCreate) {
message.unread = false;
message.media_unread = false;
} else {
message.unread = (message.out ? outboxValue : inboxValue) < message.id;
}
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(messagesRes, dialog_id, load_type, max_id, createDialog);
2018-08-27 10:33:11 +02:00
}
final ArrayList<MessageObject> objects = new ArrayList<>();
final ArrayList<Integer> messagesToReload = new ArrayList<>();
final HashMap<String, ArrayList<MessageObject>> webpagesToReload = new HashMap<>();
TLRPC.InputChannel inputChannel = null;
for (int a = 0; a < size; a++) {
TLRPC.Message message = messagesRes.messages.get(a);
message.dialog_id = dialog_id;
MessageObject messageObject = new MessageObject(currentAccount, message, usersDict, chatsDict, true);
objects.add(messageObject);
if (isCache) {
2019-06-04 12:14:50 +02:00
if (message.legacy && message.layer < TLRPC.LAYER) {
messagesToReload.add(message.id);
} else if (message.media instanceof TLRPC.TL_messageMediaUnsupported) {
2018-08-27 10:33:11 +02:00
if (message.media.bytes != null && (message.media.bytes.length == 0 || message.media.bytes.length == 1 && message.media.bytes[0] < TLRPC.LAYER)) {
messagesToReload.add(message.id);
}
2019-06-04 12:14:50 +02:00
}
if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
2019-07-18 15:01:39 +02:00
if (message.media.webpage instanceof TLRPC.TL_webPagePending && message.media.webpage.date <= getConnectionsManager().getCurrentTime()) {
2018-08-27 10:33:11 +02:00
messagesToReload.add(message.id);
} else if (message.media.webpage instanceof TLRPC.TL_webPageUrlPending) {
ArrayList<MessageObject> arrayList = webpagesToReload.get(message.media.webpage.url);
if (arrayList == null) {
arrayList = new ArrayList<>();
webpagesToReload.put(message.media.webpage.url, arrayList);
2015-01-09 13:50:15 +01:00
}
2018-08-27 10:33:11 +02:00
arrayList.add(messageObject);
2015-01-09 13:50:15 +01:00
}
}
}
2018-08-27 10:33:11 +02:00
}
AndroidUtilities.runOnUIThread(() -> {
putUsers(messagesRes.users, isCache);
putChats(messagesRes.chats, isCache);
int first_unread_final = Integer.MAX_VALUE;
if (queryFromServer && load_type == 2) {
for (int a = 0; a < messagesRes.messages.size(); a++) {
TLRPC.Message message = messagesRes.messages.get(a);
if (!message.out && message.id > first_unread && message.id < first_unread_final) {
first_unread_final = message.id;
2016-03-06 02:49:31 +01:00
}
2014-10-31 20:02:29 +01:00
}
2018-08-27 10:33:11 +02:00
}
if (first_unread_final == Integer.MAX_VALUE) {
first_unread_final = first_unread;
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.messagesDidLoad, dialog_id, count, objects, isCache, first_unread_final, last_message_id, unread_count, last_date, load_type, isEnd, classGuid, loadIndex, max_id, mentionsCount);
2018-08-27 10:33:11 +02:00
if (!messagesToReload.isEmpty()) {
reloadMessages(messagesToReload, dialog_id);
}
if (!webpagesToReload.isEmpty()) {
reloadWebPages(dialog_id, webpagesToReload);
}
});
2013-10-25 17:19:00 +02:00
});
}
2017-12-08 18:35:59 +01:00
public void loadHintDialogs() {
if (!hintDialogs.isEmpty() || TextUtils.isEmpty(installReferer)) {
return;
}
TLRPC.TL_help_getRecentMeUrls req = new TLRPC.TL_help_getRecentMeUrls();
req.referer = installReferer;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
AndroidUtilities.runOnUIThread(() -> {
/*installReferer = null;
mainPreferences.edit().remove("installReferer").commit();*/
TLRPC.TL_help_recentMeUrls res = (TLRPC.TL_help_recentMeUrls) response;
putUsers(res.users, false);
putChats(res.chats, false);
hintDialogs.clear();
hintDialogs.addAll(res.urls);
2017-12-08 18:35:59 +01:00
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
});
2017-12-08 18:35:59 +01:00
}
});
}
2019-05-14 14:08:05 +02:00
private TLRPC.TL_dialogFolder ensureFolderDialogExists(int folderId, boolean[] folderCreated) {
if (folderId == 0) {
return null;
}
long folderDialogId = DialogObject.makeFolderDialogId(folderId);
TLRPC.Dialog dialog = dialogs_dict.get(folderDialogId);
if (dialog instanceof TLRPC.TL_dialogFolder) {
if (folderCreated != null) {
folderCreated[0] = false;
}
return (TLRPC.TL_dialogFolder) dialog;
}
if (folderCreated != null) {
folderCreated[0] = true;
}
TLRPC.TL_dialogFolder dialogFolder = new TLRPC.TL_dialogFolder();
dialogFolder.id = folderDialogId;
dialogFolder.peer = new TLRPC.TL_peerUser();
dialogFolder.folder = new TLRPC.TL_folder();
dialogFolder.folder.id = folderId;
dialogFolder.folder.title = LocaleController.getString("ArchivedChats", R.string.ArchivedChats);
dialogFolder.pinned = true;
int maxPinnedNum = 0;
for (int a = 0; a < allDialogs.size(); a++) {
TLRPC.Dialog d = allDialogs.get(a);
if (!d.pinned) {
break;
}
maxPinnedNum = Math.max(d.pinnedNum, maxPinnedNum);
}
dialogFolder.pinnedNum = maxPinnedNum + 1;
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
dialogs.dialogs.add(dialogFolder);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putDialogs(dialogs, 1);
2019-05-14 14:08:05 +02:00
dialogs_dict.put(folderDialogId, dialogFolder);
allDialogs.add(0, dialogFolder);
return dialogFolder;
}
private void removeFolder(int folderId) {
long dialogId = DialogObject.makeFolderDialogId(folderId);
TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
if (dialog == null) {
2013-10-25 17:19:00 +02:00
return;
}
2019-05-14 14:08:05 +02:00
dialogs_dict.remove(dialogId);
allDialogs.remove(dialog);
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
getNotificationCenter().postNotificationName(NotificationCenter.folderBecomeEmpty, folderId);
2019-05-14 14:08:05 +02:00
}
protected void onFolderEmpty(int folderId) {
2019-07-18 15:01:39 +02:00
int[] dialogsLoadOffset = getUserConfig().getDialogLoadOffsets(folderId);
2019-05-14 14:08:05 +02:00
if (dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] == Integer.MAX_VALUE) {
removeFolder(folderId);
} else {
loadDialogs(folderId, 0, 10, false, () -> removeFolder(folderId));
}
}
public void checkIfFolderEmpty(int folderId) {
if (folderId == 0) {
return;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().checkIfFolderEmpty(folderId);
2019-05-14 14:08:05 +02:00
}
public int addDialogToFolder(long dialogId, int folderId, int pinnedNum, long taskId) {
ArrayList<Long> arrayList = new ArrayList<>(1);
arrayList.add(dialogId);
return addDialogToFolder(arrayList, folderId, pinnedNum, null, taskId);
}
public int addDialogToFolder(ArrayList<Long> dialogIds, int folderId, int pinnedNum, ArrayList<TLRPC.TL_inputFolderPeer> peers, long taskId) {
TLRPC.TL_folders_editPeerFolders req = new TLRPC.TL_folders_editPeerFolders();
boolean[] folderCreated = null;
final long newTaskId;
if (taskId == 0) {
boolean added = false;
2019-07-18 15:01:39 +02:00
int selfUserId = getUserConfig().getClientUserId();
2019-05-14 14:08:05 +02:00
int size = 0;
for (int a = 0, N = dialogIds.size(); a < N; a++) {
long dialogId = dialogIds.get(a);
if (!DialogObject.isPeerDialogId(dialogId) && !DialogObject.isSecretDialogId(dialogId)) {
continue;
}
2019-06-04 12:14:50 +02:00
if (folderId == 1 && (dialogId == selfUserId || dialogId == 777000 || isProxyDialog(dialogId, false))) {
2019-05-14 14:08:05 +02:00
continue;
}
TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
if (dialog == null) {
continue;
}
added = true;
dialog.folder_id = folderId;
if (pinnedNum > 0) {
dialog.pinned = true;
dialog.pinnedNum = pinnedNum;
} else {
dialog.pinned = false;
dialog.pinnedNum = 0;
}
if (folderCreated == null) {
folderCreated = new boolean[1];
ensureFolderDialogExists(folderId, folderCreated);
}
if (DialogObject.isSecretDialogId(dialogId)) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogsFolderId(null, null, dialogId, folderId);
2019-05-14 14:08:05 +02:00
} else {
TLRPC.TL_inputFolderPeer folderPeer = new TLRPC.TL_inputFolderPeer();
folderPeer.folder_id = folderId;
folderPeer.peer = getInputPeer((int) dialogId);
req.folder_peers.add(folderPeer);
size += folderPeer.getObjectSize();
}
}
if (!added) {
return 0;
}
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2019-05-14 14:08:05 +02:00
if (size != 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(4 + 4 + 4 + size);
data.writeInt32(17);
data.writeInt32(folderId);
data.writeInt32(req.folder_peers.size());
for (int a = 0, N = req.folder_peers.size(); a < N; a++) {
req.folder_peers.get(a).serializeToStream(data);
}
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2019-05-14 14:08:05 +02:00
} else {
newTaskId = 0;
}
} else {
req.folder_peers = peers;
newTaskId = taskId;
}
if (!req.folder_peers.isEmpty()) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-05-14 14:08:05 +02:00
if (error == null) {
processUpdates((TLRPC.Updates) response, false);
}
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-05-14 14:08:05 +02:00
}
});
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogsFolderId(null, req.folder_peers, 0, folderId);
2019-05-14 14:08:05 +02:00
}
return folderCreated == null ? 0 : (folderCreated[0] ? 2 : 1);
}
public void loadDialogs(final int folderId, final int offset, final int count, boolean fromCache) {
loadDialogs(folderId, offset, count, fromCache, null);
}
public void loadDialogs(final int folderId, final int offset, final int count, boolean fromCache, Runnable onEmptyCallback) {
if (loadingDialogs.get(folderId) || resetingDialogs) {
return;
}
loadingDialogs.put(folderId, true);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-05-14 14:08:05 +02:00
FileLog.d("folderId = " + folderId + " load cacheOffset = " + offset + " count = " + count + " cache = " + fromCache);
2018-07-30 04:07:02 +02:00
}
2013-10-25 17:19:00 +02:00
if (fromCache) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().getDialogs(folderId, offset == 0 ? 0 : nextDialogsCacheOffset.get(folderId, 0), count);
2013-10-25 17:19:00 +02:00
} else {
TLRPC.TL_messages_getDialogs req = new TLRPC.TL_messages_getDialogs();
req.limit = count;
2017-03-31 01:58:05 +02:00
req.exclude_pinned = true;
2019-05-14 14:08:05 +02:00
if (folderId != 0) {
req.flags |= 2;
req.folder_id = folderId;
}
2019-07-18 15:01:39 +02:00
int[] dialogsLoadOffset = getUserConfig().getDialogLoadOffsets(folderId);
2019-05-14 14:08:05 +02:00
if (dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] != -1) {
if (dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] == Integer.MAX_VALUE) {
dialogsEndReached.put(folderId, true);
serverDialogsEndReached.put(folderId, true);
loadingDialogs.put(folderId, false);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2017-07-08 18:32:04 +02:00
return;
2017-03-31 01:58:05 +02:00
}
2019-05-14 14:08:05 +02:00
req.offset_id = dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId];
req.offset_date = dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetDate];
2017-07-08 18:32:04 +02:00
if (req.offset_id == 0) {
req.offset_peer = new TLRPC.TL_inputPeerEmpty();
} else {
2019-05-14 14:08:05 +02:00
if (dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetChannelId] != 0) {
2017-07-08 18:32:04 +02:00
req.offset_peer = new TLRPC.TL_inputPeerChannel();
2019-05-14 14:08:05 +02:00
req.offset_peer.channel_id = dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetChannelId];
} else if (dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetUserId] != 0) {
2017-07-08 18:32:04 +02:00
req.offset_peer = new TLRPC.TL_inputPeerUser();
2019-05-14 14:08:05 +02:00
req.offset_peer.user_id = dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetUserId];
2017-07-08 18:32:04 +02:00
} else {
req.offset_peer = new TLRPC.TL_inputPeerChat();
2019-05-14 14:08:05 +02:00
req.offset_peer.chat_id = dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetChatId];
2017-07-08 18:32:04 +02:00
}
2019-05-14 14:08:05 +02:00
req.offset_peer.access_hash = ((long) dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetAccess_1]) | ((long) dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetAccess_1] << 32);
2017-07-08 18:32:04 +02:00
}
} else {
boolean found = false;
2019-05-14 14:08:05 +02:00
ArrayList<TLRPC.Dialog> dialogs = getDialogs(folderId);
2017-07-08 18:32:04 +02:00
for (int a = dialogs.size() - 1; a >= 0; a--) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs.get(a);
2017-07-08 18:32:04 +02:00
if (dialog.pinned) {
continue;
}
int lower_id = (int) dialog.id;
int high_id = (int) (dialog.id >> 32);
if (lower_id != 0 && high_id != 1 && dialog.top_message > 0) {
MessageObject message = dialogMessage.get(dialog.id);
if (message != null && message.getId() > 0) {
req.offset_date = message.messageOwner.date;
req.offset_id = message.messageOwner.id;
int id;
if (message.messageOwner.to_id.channel_id != 0) {
id = -message.messageOwner.to_id.channel_id;
} else if (message.messageOwner.to_id.chat_id != 0) {
id = -message.messageOwner.to_id.chat_id;
} else {
id = message.messageOwner.to_id.user_id;
}
req.offset_peer = getInputPeer(id);
found = true;
break;
2015-11-26 22:04:02 +01:00
}
}
}
2017-07-08 18:32:04 +02:00
if (!found) {
req.offset_peer = new TLRPC.TL_inputPeerEmpty();
}
2015-11-26 22:04:02 +01:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
final TLRPC.messages_Dialogs dialogsRes = (TLRPC.messages_Dialogs) response;
2019-05-14 14:08:05 +02:00
processLoadedDialogs(dialogsRes, null, folderId, 0, count, 0, false, false, false);
if (onEmptyCallback != null && dialogsRes.dialogs.isEmpty()) {
AndroidUtilities.runOnUIThread(onEmptyCallback);
}
2013-10-25 17:19:00 +02:00
}
});
}
}
2018-07-30 04:07:02 +02:00
public void loadGlobalNotificationsSettings() {
2019-07-18 15:01:39 +02:00
if (loadingNotificationSettings == 0 && !getUserConfig().notificationsSettingsLoaded) {
2019-01-23 18:03:33 +01:00
SharedPreferences preferences = MessagesController.getNotificationsSettings(currentAccount);
SharedPreferences.Editor editor1 = null;
if (preferences.contains("EnableGroup")) {
boolean enabled = preferences.getBoolean("EnableGroup", true);
if (editor1 == null) {
editor1 = preferences.edit();
}
if (!enabled) {
editor1.putInt("EnableGroup2", Integer.MAX_VALUE);
editor1.putInt("EnableChannel2", Integer.MAX_VALUE);
}
editor1.remove("EnableGroup").commit();
2018-07-30 04:07:02 +02:00
}
2019-01-23 18:03:33 +01:00
if (preferences.contains("EnableAll")) {
boolean enabled = preferences.getBoolean("EnableAll", true);
if (editor1 == null) {
editor1 = preferences.edit();
}
if (!enabled) {
editor1.putInt("EnableAll2", Integer.MAX_VALUE);
}
editor1.remove("EnableAll").commit();
}
if (editor1 != null) {
editor1.commit();
}
loadingNotificationSettings = 3;
for (int a = 0; a < 3; a++) {
TLRPC.TL_account_getNotifySettings req = new TLRPC.TL_account_getNotifySettings();
if (a == 0) {
req.peer = new TLRPC.TL_inputNotifyChats();
} else if (a == 1) {
req.peer = new TLRPC.TL_inputNotifyUsers();
} else if (a == 2) {
req.peer = new TLRPC.TL_inputNotifyBroadcasts();
}
final int type = a;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2019-01-23 18:03:33 +01:00
if (response != null) {
loadingNotificationSettings--;
TLRPC.TL_peerNotifySettings notify_settings = (TLRPC.TL_peerNotifySettings) response;
SharedPreferences.Editor editor = notificationsPreferences.edit();
if (type == 0) {
if ((notify_settings.flags & 1) != 0) {
editor.putBoolean("EnablePreviewGroup", notify_settings.show_previews);
}
if ((notify_settings.flags & 2) != 0) {
2018-08-27 10:33:11 +02:00
/*if (notify_settings.silent) {
editor.putString("GroupSoundPath", "NoSound");
} else {
editor.remove("GroupSoundPath");
}*/
2019-01-23 18:03:33 +01:00
}
if ((notify_settings.flags & 4) != 0) {
editor.putInt("EnableGroup2", notify_settings.mute_until);
}
} else if (type == 1) {
if ((notify_settings.flags & 1) != 0) {
editor.putBoolean("EnablePreviewAll", notify_settings.show_previews);
}
if ((notify_settings.flags & 2) != 0) {
2018-08-27 10:33:11 +02:00
/*if (notify_settings.silent) {
editor.putString("GlobalSoundPath", "NoSound");
} else {
editor.remove("GlobalSoundPath");
}*/
2019-01-23 18:03:33 +01:00
}
if ((notify_settings.flags & 4) != 0) {
editor.putInt("EnableAll2", notify_settings.mute_until);
}
} else if (type == 2) {
if ((notify_settings.flags & 1) != 0) {
editor.putBoolean("EnablePreviewChannel", notify_settings.show_previews);
}
if ((notify_settings.flags & 2) != 0) {
/*if (notify_settings.silent) {
editor.putString("ChannelSoundPath", "NoSound");
} else {
editor.remove("ChannelSoundPath");
}*/
}
if ((notify_settings.flags & 4) != 0) {
editor.putInt("EnableChannel2", notify_settings.mute_until);
}
2018-08-27 10:33:11 +02:00
}
2019-01-23 18:03:33 +01:00
editor.commit();
if (loadingNotificationSettings == 0) {
2019-07-18 15:01:39 +02:00
getUserConfig().notificationsSettingsLoaded = true;
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
}
}
2019-01-23 18:03:33 +01:00
}));
}
}
2019-07-18 15:01:39 +02:00
if (!getUserConfig().notificationsSignUpSettingsLoaded) {
2019-01-23 18:03:33 +01:00
loadSignUpNotificationsSettings();
}
}
public void loadSignUpNotificationsSettings() {
if (!loadingNotificationSignUpSettings) {
loadingNotificationSignUpSettings = true;
TLRPC.TL_account_getContactSignUpNotification req = new TLRPC.TL_account_getContactSignUpNotification();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2019-01-23 18:03:33 +01:00
loadingNotificationSignUpSettings = false;
SharedPreferences.Editor editor = notificationsPreferences.edit();
enableJoined = response instanceof TLRPC.TL_boolFalse;
editor.putBoolean("EnableContactJoined", enableJoined);
editor.commit();
2019-07-18 15:01:39 +02:00
getUserConfig().notificationsSignUpSettingsLoaded = true;
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
}));
2018-07-30 04:07:02 +02:00
}
}
2017-12-08 18:35:59 +01:00
public void forceResetDialogs() {
2019-07-18 15:01:39 +02:00
resetDialogs(true, getMessagesStorage().getLastSeqValue(), getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue());
getNotificationsController().deleteAllNotificationChannels();
2017-12-08 18:35:59 +01:00
}
2019-05-14 14:08:05 +02:00
protected void loadUnknownDialog(final TLRPC.InputPeer peer, final long taskId) {
if (peer == null) {
return;
}
long dialogId = DialogObject.getPeerDialogId(peer);
if (gettingUnknownDialogs.indexOfKey(dialogId) >= 0) {
return;
}
gettingUnknownDialogs.put(dialogId, true);
2019-06-04 12:14:50 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("load unknown dialog " + dialogId);
}
2019-05-14 14:08:05 +02:00
TLRPC.TL_messages_getPeerDialogs req = new TLRPC.TL_messages_getPeerDialogs();
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = peer;
req.peers.add(inputDialogPeer);
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(4 + peer.getObjectSize());
data.writeInt32(15);
peer.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2019-05-14 14:08:05 +02:00
} else {
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-05-14 14:08:05 +02:00
if (response != null) {
TLRPC.TL_messages_peerDialogs res = (TLRPC.TL_messages_peerDialogs) response;
if (!res.dialogs.isEmpty()) {
TLRPC.TL_dialog dialog = (TLRPC.TL_dialog) res.dialogs.get(0);
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
dialogs.dialogs.addAll(res.dialogs);
dialogs.messages.addAll(res.messages);
dialogs.users.addAll(res.users);
dialogs.chats.addAll(res.chats);
processLoadedDialogs(dialogs, null, dialog.folder_id, 0, 1, DIALOGS_LOAD_TYPE_UNKNOWN, false, false, false);
}
}
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-05-14 14:08:05 +02:00
}
gettingUnknownDialogs.delete(dialogId);
});
}
private void fetchFolderInLoadedPinnedDialogs(TLRPC.TL_messages_peerDialogs res) {
for (int a = 0, N = res.dialogs.size(); a < N; a++) {
TLRPC.Dialog dialog = res.dialogs.get(a);
if (dialog instanceof TLRPC.TL_dialogFolder) {
TLRPC.TL_dialogFolder dialogFolder = (TLRPC.TL_dialogFolder) dialog;
long folderTopDialogId = DialogObject.getPeerDialogId(dialog.peer);
2019-06-04 12:14:50 +02:00
if (dialogFolder.top_message == 0 || folderTopDialogId == 0) {
res.dialogs.remove(dialogFolder);
continue;
}
2019-05-14 14:08:05 +02:00
for (int b = 0, N2 = res.messages.size(); b < N2; b++) {
TLRPC.Message message = res.messages.get(b);
long messageDialogId = MessageObject.getDialogId(message);
if (folderTopDialogId == messageDialogId && dialog.top_message == message.id) {
TLRPC.TL_dialog newDialog = new TLRPC.TL_dialog();
newDialog.peer = dialog.peer;
newDialog.top_message = dialog.top_message;
newDialog.folder_id = dialogFolder.folder.id;
newDialog.flags |= 16;
res.dialogs.add(newDialog);
TLRPC.InputPeer inputPeer;
if (dialog.peer instanceof TLRPC.TL_peerChannel) {
inputPeer = new TLRPC.TL_inputPeerChannel();
inputPeer.channel_id = dialog.peer.channel_id;
for (int c = 0, N3 = res.chats.size(); c < N3; c++) {
TLRPC.Chat chat = res.chats.get(c);
if (chat.id == inputPeer.channel_id) {
inputPeer.access_hash = chat.access_hash;
break;
}
}
} else if (dialog.peer instanceof TLRPC.TL_peerChat) {
inputPeer = new TLRPC.TL_inputPeerChat();
inputPeer.chat_id = dialog.peer.chat_id;
} else {
inputPeer = new TLRPC.TL_inputPeerUser();
inputPeer.user_id = dialog.peer.user_id;
for (int c = 0, N3 = res.users.size(); c < N3; c++) {
TLRPC.User user = res.users.get(c);
if (user.id == inputPeer.user_id) {
inputPeer.access_hash = user.access_hash;
break;
}
}
}
loadUnknownDialog(inputPeer, 0);
break;
}
}
2019-06-04 12:14:50 +02:00
break;
2019-05-14 14:08:05 +02:00
}
}
}
2017-12-08 18:35:59 +01:00
private void resetDialogs(boolean query, final int seq, final int newPts, final int date, final int qts) {
if (query) {
if (resetingDialogs) {
return;
}
2019-07-18 15:01:39 +02:00
getUserConfig().setPinnedDialogsLoaded(1, false);
2017-12-08 18:35:59 +01:00
resetingDialogs = true;
TLRPC.TL_messages_getPinnedDialogs req = new TLRPC.TL_messages_getPinnedDialogs();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
resetDialogsPinned = (TLRPC.TL_messages_peerDialogs) response;
2019-05-14 14:08:05 +02:00
for (int a = 0; a < resetDialogsPinned.dialogs.size(); a++) {
TLRPC.Dialog d = resetDialogsPinned.dialogs.get(a);
d.pinned = true;
}
2018-08-27 10:33:11 +02:00
resetDialogs(false, seq, newPts, date, qts);
2017-12-08 18:35:59 +01:00
}
});
TLRPC.TL_messages_getDialogs req2 = new TLRPC.TL_messages_getDialogs();
req2.limit = 100;
req2.exclude_pinned = true;
req2.offset_peer = new TLRPC.TL_inputPeerEmpty();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req2, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
resetDialogsAll = (TLRPC.messages_Dialogs) response;
resetDialogs(false, seq, newPts, date, qts);
2017-12-08 18:35:59 +01:00
}
});
} else if (resetDialogsPinned != null && resetDialogsAll != null) {
int messagesCount = resetDialogsAll.messages.size();
int dialogsCount = resetDialogsAll.dialogs.size();
2019-05-14 14:08:05 +02:00
fetchFolderInLoadedPinnedDialogs(resetDialogsPinned);
2017-12-08 18:35:59 +01:00
resetDialogsAll.dialogs.addAll(resetDialogsPinned.dialogs);
resetDialogsAll.messages.addAll(resetDialogsPinned.messages);
resetDialogsAll.users.addAll(resetDialogsPinned.users);
resetDialogsAll.chats.addAll(resetDialogsPinned.chats);
2019-05-14 14:08:05 +02:00
final LongSparseArray<TLRPC.Dialog> new_dialogs_dict = new LongSparseArray<>();
2018-07-30 04:07:02 +02:00
final LongSparseArray<MessageObject> new_dialogMessage = new LongSparseArray<>();
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>();
2017-12-08 18:35:59 +01:00
for (int a = 0; a < resetDialogsAll.users.size(); a++) {
TLRPC.User u = resetDialogsAll.users.get(a);
usersDict.put(u.id, u);
}
for (int a = 0; a < resetDialogsAll.chats.size(); a++) {
TLRPC.Chat c = resetDialogsAll.chats.get(a);
chatsDict.put(c.id, c);
}
TLRPC.Message lastMessage = null;
for (int a = 0; a < resetDialogsAll.messages.size(); a++) {
TLRPC.Message message = resetDialogsAll.messages.get(a);
if (a < messagesCount) {
if (lastMessage == null || message.date < lastMessage.date) {
lastMessage = message;
}
}
if (message.to_id.channel_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.channel_id);
if (chat != null && chat.left) {
continue;
}
if (chat != null && chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
} else if (message.to_id.chat_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.chat_id);
if (chat != null && chat.migrated_to != null) {
continue;
}
}
2018-07-30 04:07:02 +02:00
MessageObject messageObject = new MessageObject(currentAccount, message, usersDict, chatsDict, false);
2017-12-08 18:35:59 +01:00
new_dialogMessage.put(messageObject.getDialogId(), messageObject);
}
for (int a = 0; a < resetDialogsAll.dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog d = resetDialogsAll.dialogs.get(a);
DialogObject.initDialog(d);
2017-12-08 18:35:59 +01:00
if (d.id == 0) {
continue;
}
if (d.last_message_date == 0) {
MessageObject mess = new_dialogMessage.get(d.id);
if (mess != null) {
d.last_message_date = mess.messageOwner.date;
}
}
if (DialogObject.isChannel(d)) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.left) {
continue;
}
channelsPts.put(-(int) d.id, d.pts);
} else if ((int) d.id < 0) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.migrated_to != null) {
continue;
}
}
new_dialogs_dict.put(d.id, d);
Integer value = dialogs_read_inbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_inbox_max.put(d.id, Math.max(value, d.read_inbox_max_id));
value = dialogs_read_outbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_outbox_max.put(d.id, Math.max(value, d.read_outbox_max_id));
}
ImageLoader.saveMessagesThumbs(resetDialogsAll.messages);
for (int a = 0; a < resetDialogsAll.messages.size(); a++) {
TLRPC.Message message = resetDialogsAll.messages.get(a);
if (message.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
TLRPC.User user = usersDict.get(message.action.user_id);
if (user != null && user.bot) {
message.reply_markup = new TLRPC.TL_replyKeyboardHide();
message.flags |= 64;
}
}
if (message.action instanceof TLRPC.TL_messageActionChatMigrateTo || message.action instanceof TLRPC.TL_messageActionChannelCreate) {
message.unread = false;
message.media_unread = false;
} else {
ConcurrentHashMap<Long, Integer> read_max = message.out ? dialogs_read_outbox_max : dialogs_read_inbox_max;
Integer value = read_max.get(message.dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
2017-12-08 18:35:59 +01:00
read_max.put(message.dialog_id, value);
}
message.unread = value < message.id;
}
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().resetDialogs(resetDialogsAll, messagesCount, seq, newPts, date, qts, new_dialogs_dict, new_dialogMessage, lastMessage, dialogsCount);
2017-12-08 18:35:59 +01:00
resetDialogsPinned = null;
resetDialogsAll = null;
}
}
2019-05-14 14:08:05 +02:00
protected void completeDialogsReset(final TLRPC.messages_Dialogs dialogsRes, final int messagesCount, final int seq, final int newPts, final int date, final int qts, final LongSparseArray<TLRPC.Dialog> new_dialogs_dict, final LongSparseArray<MessageObject> new_dialogMessage, final TLRPC.Message lastMessage) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
gettingDifference = false;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastPtsValue(newPts);
getMessagesStorage().setLastDateValue(date);
getMessagesStorage().setLastQtsValue(qts);
2018-08-27 10:33:11 +02:00
getDifference();
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
resetingDialogs = false;
applyDialogsNotificationsSettings(dialogsRes.dialogs);
2019-07-18 15:01:39 +02:00
if (!getUserConfig().draftsLoaded) {
getMediaDataController().loadDrafts();
2018-08-27 10:33:11 +02:00
}
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
putUsers(dialogsRes.users, false);
putChats(dialogsRes.chats, false);
2017-12-08 18:35:59 +01:00
2019-05-14 14:08:05 +02:00
for (int a = 0; a < allDialogs.size(); a++) {
TLRPC.Dialog oldDialog = allDialogs.get(a);
if (!DialogObject.isSecretDialogId(oldDialog.id)) {
2018-08-27 10:33:11 +02:00
dialogs_dict.remove(oldDialog.id);
MessageObject messageObject = dialogMessage.get(oldDialog.id);
dialogMessage.remove(oldDialog.id);
if (messageObject != null) {
dialogMessagesByIds.remove(messageObject.getId());
if (messageObject.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(messageObject.messageOwner.random_id);
}
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
}
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
for (int a = 0; a < new_dialogs_dict.size(); a++) {
long key = new_dialogs_dict.keyAt(a);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog value = new_dialogs_dict.valueAt(a);
2018-08-27 10:33:11 +02:00
if (value.draft instanceof TLRPC.TL_draftMessage) {
2019-07-18 15:01:39 +02:00
getMediaDataController().saveDraft(value.id, value.draft, null, false);
2018-08-27 10:33:11 +02:00
}
dialogs_dict.put(key, value);
MessageObject messageObject = new_dialogMessage.get(value.id);
dialogMessage.put(key, messageObject);
if (messageObject != null && messageObject.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(messageObject.getId(), messageObject);
if (messageObject.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(messageObject.messageOwner.random_id, messageObject);
2017-12-08 18:35:59 +01:00
}
}
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
allDialogs.clear();
2018-08-27 10:33:11 +02:00
for (int a = 0, size = dialogs_dict.size(); a < size; a++) {
2019-05-14 14:08:05 +02:00
allDialogs.add(dialogs_dict.valueAt(a));
2018-08-27 10:33:11 +02:00
}
sortDialogs(null);
2019-05-14 14:08:05 +02:00
dialogsEndReached.put(0, true);
serverDialogsEndReached.put(0, false);
dialogsEndReached.put(1, true);
serverDialogsEndReached.put(1, false);
2018-08-27 10:33:11 +02:00
2019-07-18 15:01:39 +02:00
int totalDialogsLoadCount = getUserConfig().getTotalDialogsCount(0);
int[] dialogsLoadOffset = getUserConfig().getDialogLoadOffsets(0);
2019-05-14 14:08:05 +02:00
if (totalDialogsLoadCount < 400 && dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] != -1 && dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] != Integer.MAX_VALUE) {
loadDialogs(0, 100, 0, false);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
});
2017-12-08 18:35:59 +01:00
});
}
2015-11-26 22:04:02 +01:00
private void migrateDialogs(final int offset, final int offsetDate, final int offsetUser, final int offsetChat, final int offsetChannel, final long accessPeer) {
if (migratingDialogs || offset == -1) {
return;
}
migratingDialogs = true;
TLRPC.TL_messages_getDialogs req = new TLRPC.TL_messages_getDialogs();
2017-07-08 18:32:04 +02:00
req.exclude_pinned = true;
2015-11-26 22:04:02 +01:00
req.limit = 100;
req.offset_id = offset;
req.offset_date = offsetDate;
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("start migrate with id " + offset + " date " + LocaleController.getInstance().formatterStats.format((long) offsetDate * 1000));
}
2015-11-26 22:04:02 +01:00
if (offset == 0) {
req.offset_peer = new TLRPC.TL_inputPeerEmpty();
} else {
if (offsetChannel != 0) {
req.offset_peer = new TLRPC.TL_inputPeerChannel();
req.offset_peer.channel_id = offsetChannel;
} else if (offsetUser != 0) {
req.offset_peer = new TLRPC.TL_inputPeerUser();
req.offset_peer.user_id = offsetUser;
} else {
req.offset_peer = new TLRPC.TL_inputPeerChat();
req.offset_peer.chat_id = offsetChat;
}
req.offset_peer.access_hash = accessPeer;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
final TLRPC.messages_Dialogs dialogsRes = (TLRPC.messages_Dialogs) response;
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
2018-08-27 10:33:11 +02:00
try {
int offsetId;
2019-07-18 15:01:39 +02:00
int totalDialogsLoadCount = getUserConfig().getTotalDialogsCount(0);
getUserConfig().setTotalDialogsCount(0, totalDialogsLoadCount + dialogsRes.dialogs.size());
2018-08-27 10:33:11 +02:00
TLRPC.Message lastMessage = null;
for (int a = 0; a < dialogsRes.messages.size(); a++) {
TLRPC.Message message = dialogsRes.messages.get(a);
if (BuildVars.LOGS_ENABLED) {
FileLog.d("search migrate id " + message.id + " date " + LocaleController.getInstance().formatterStats.format((long) message.date * 1000));
}
if (lastMessage == null || message.date < lastMessage.date) {
lastMessage = message;
}
}
if (BuildVars.LOGS_ENABLED) {
FileLog.d("migrate step with id " + lastMessage.id + " date " + LocaleController.getInstance().formatterStats.format((long) lastMessage.date * 1000));
}
if (dialogsRes.dialogs.size() >= 100) {
offsetId = lastMessage.id;
} else {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("migrate stop due to not 100 dialogs");
}
2019-05-14 14:08:05 +02:00
for (int i = 0; i < 2; i++) {
2019-07-18 15:01:39 +02:00
getUserConfig().setDialogsLoadOffset(i,
2019-05-14 14:08:05 +02:00
Integer.MAX_VALUE,
2019-07-18 15:01:39 +02:00
getUserConfig().migrateOffsetDate,
getUserConfig().migrateOffsetUserId,
getUserConfig().migrateOffsetChatId,
getUserConfig().migrateOffsetChannelId,
getUserConfig().migrateOffsetAccess);
2019-05-14 14:08:05 +02:00
}
2018-08-27 10:33:11 +02:00
offsetId = -1;
}
StringBuilder dids = new StringBuilder(dialogsRes.dialogs.size() * 12);
2019-05-14 14:08:05 +02:00
LongSparseArray<TLRPC.Dialog> dialogHashMap = new LongSparseArray<>();
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogsRes.dialogs.get(a);
DialogObject.initDialog(dialog);
2018-08-27 10:33:11 +02:00
if (dids.length() > 0) {
dids.append(",");
}
dids.append(dialog.id);
dialogHashMap.put(dialog.id, dialog);
}
2019-07-18 15:01:39 +02:00
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT did, folder_id FROM dialogs WHERE did IN (%s)", dids.toString()));
2018-08-27 10:33:11 +02:00
while (cursor.next()) {
long did = cursor.longValue(0);
2019-05-14 14:08:05 +02:00
int folder_id = cursor.intValue(1);
TLRPC.Dialog dialog = dialogHashMap.get(did);
if (dialog.folder_id != folder_id) {
continue;
}
2018-08-27 10:33:11 +02:00
dialogHashMap.remove(did);
if (dialog != null) {
dialogsRes.dialogs.remove(dialog);
2017-07-08 18:32:04 +02:00
for (int a = 0; a < dialogsRes.messages.size(); a++) {
TLRPC.Message message = dialogsRes.messages.get(a);
2018-08-27 10:33:11 +02:00
if (MessageObject.getDialogId(message) != did) {
continue;
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
dialogsRes.messages.remove(a);
a--;
if (message.id == dialog.top_message) {
dialog.top_message = 0;
break;
2015-11-26 22:04:02 +01:00
}
}
2018-08-27 10:33:11 +02:00
}
}
cursor.dispose();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("migrate found missing dialogs " + dialogsRes.dialogs.size());
}
2019-07-18 15:01:39 +02:00
cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT min(date) FROM dialogs WHERE date != 0 AND did >> 32 IN (0, -1)");
2018-08-27 10:33:11 +02:00
if (cursor.next()) {
int date = Math.max(1441062000, cursor.intValue(0));
for (int a = 0; a < dialogsRes.messages.size(); a++) {
TLRPC.Message message = dialogsRes.messages.get(a);
if (message.date < date) {
if (offset != -1) {
2019-05-14 14:08:05 +02:00
for (int i = 0; i < 2; i++) {
2019-07-18 15:01:39 +02:00
getUserConfig().setDialogsLoadOffset(i,
getUserConfig().migrateOffsetId,
getUserConfig().migrateOffsetDate,
getUserConfig().migrateOffsetUserId,
getUserConfig().migrateOffsetChatId,
getUserConfig().migrateOffsetChannelId,
getUserConfig().migrateOffsetAccess);
2019-05-14 14:08:05 +02:00
}
2017-07-08 18:32:04 +02:00
offsetId = -1;
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("migrate stop due to reached loaded dialogs " + LocaleController.getInstance().formatterStats.format((long) date * 1000));
}
2017-07-08 18:32:04 +02:00
}
2018-08-27 10:33:11 +02:00
dialogsRes.messages.remove(a);
a--;
long did = MessageObject.getDialogId(message);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogHashMap.get(did);
2018-08-27 10:33:11 +02:00
dialogHashMap.remove(did);
if (dialog != null) {
dialogsRes.dialogs.remove(dialog);
2017-07-08 18:32:04 +02:00
}
}
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
if (lastMessage != null && lastMessage.date < date && offset != -1) {
2019-05-14 14:08:05 +02:00
for (int i = 0; i < 2; i++) {
2019-07-18 15:01:39 +02:00
getUserConfig().setDialogsLoadOffset(i,
getUserConfig().migrateOffsetId,
getUserConfig().migrateOffsetDate,
getUserConfig().migrateOffsetUserId,
getUserConfig().migrateOffsetChatId,
getUserConfig().migrateOffsetChannelId,
getUserConfig().migrateOffsetAccess);
2019-05-14 14:08:05 +02:00
}
2018-08-27 10:33:11 +02:00
offsetId = -1;
if (BuildVars.LOGS_ENABLED) {
FileLog.d("migrate stop due to reached loaded dialogs " + LocaleController.getInstance().formatterStats.format((long) date * 1000));
}
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
cursor.dispose();
2015-09-24 22:52:02 +02:00
2019-07-18 15:01:39 +02:00
getUserConfig().migrateOffsetDate = lastMessage.date;
2017-07-08 18:32:04 +02:00
if (lastMessage.to_id.channel_id != 0) {
2019-07-18 15:01:39 +02:00
getUserConfig().migrateOffsetChannelId = lastMessage.to_id.channel_id;
getUserConfig().migrateOffsetChatId = 0;
getUserConfig().migrateOffsetUserId = 0;
2017-07-08 18:32:04 +02:00
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat chat = dialogsRes.chats.get(a);
2019-07-18 15:01:39 +02:00
if (chat.id == getUserConfig().migrateOffsetChannelId) {
getUserConfig().migrateOffsetAccess = chat.access_hash;
2017-07-08 18:32:04 +02:00
break;
}
}
} else if (lastMessage.to_id.chat_id != 0) {
2019-07-18 15:01:39 +02:00
getUserConfig().migrateOffsetChatId = lastMessage.to_id.chat_id;
getUserConfig().migrateOffsetChannelId = 0;
getUserConfig().migrateOffsetUserId = 0;
2017-07-08 18:32:04 +02:00
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat chat = dialogsRes.chats.get(a);
2019-07-18 15:01:39 +02:00
if (chat.id == getUserConfig().migrateOffsetChatId) {
getUserConfig().migrateOffsetAccess = chat.access_hash;
2017-07-08 18:32:04 +02:00
break;
}
}
} else if (lastMessage.to_id.user_id != 0) {
2019-07-18 15:01:39 +02:00
getUserConfig().migrateOffsetUserId = lastMessage.to_id.user_id;
getUserConfig().migrateOffsetChatId = 0;
getUserConfig().migrateOffsetChannelId = 0;
2017-07-08 18:32:04 +02:00
for (int a = 0; a < dialogsRes.users.size(); a++) {
TLRPC.User user = dialogsRes.users.get(a);
2019-07-18 15:01:39 +02:00
if (user.id == getUserConfig().migrateOffsetUserId) {
getUserConfig().migrateOffsetAccess = user.access_hash;
2017-07-08 18:32:04 +02:00
break;
}
}
}
2018-08-27 10:33:11 +02:00
2019-05-14 14:08:05 +02:00
processLoadedDialogs(dialogsRes, null, 0, offsetId, 0, 0, false, true, false);
2018-08-27 10:33:11 +02:00
} catch (Exception e) {
FileLog.e(e);
AndroidUtilities.runOnUIThread(() -> migratingDialogs = false);
2017-07-08 18:32:04 +02:00
}
2018-08-27 10:33:11 +02:00
});
} else {
AndroidUtilities.runOnUIThread(() -> migratingDialogs = false);
}
});
}
2017-07-08 18:32:04 +02:00
2019-05-14 14:08:05 +02:00
private int DIALOGS_LOAD_TYPE_CACHE = 1;
private int DIALOGS_LOAD_TYPE_CHANNEL = 2;
private int DIALOGS_LOAD_TYPE_UNKNOWN = 3;
public void processLoadedDialogs(final TLRPC.messages_Dialogs dialogsRes, final ArrayList<TLRPC.EncryptedChat> encChats, final int folderId, final int offset, final int count, final int loadType, final boolean resetEnd, final boolean migrate, final boolean fromCache) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
if (!firstGettingTask) {
getNewDeleteTask(null, 0);
firstGettingTask = true;
}
if (BuildVars.LOGS_ENABLED) {
2019-05-14 14:08:05 +02:00
FileLog.d("loaded folderId " + folderId + " loadType " + loadType + " count " + dialogsRes.dialogs.size());
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
int[] dialogsLoadOffset = getUserConfig().getDialogLoadOffsets(folderId);
2019-05-14 14:08:05 +02:00
if (loadType == DIALOGS_LOAD_TYPE_CACHE && dialogsRes.dialogs.size() == 0) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(dialogsRes.users, true);
2019-05-14 14:08:05 +02:00
loadingDialogs.put(folderId, false);
2018-08-27 10:33:11 +02:00
if (resetEnd) {
2019-05-14 14:08:05 +02:00
dialogsEndReached.put(folderId, false);
serverDialogsEndReached.put(folderId, false);
} else if (dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] == Integer.MAX_VALUE) {
dialogsEndReached.put(folderId, true);
serverDialogsEndReached.put(folderId, true);
2018-08-27 10:33:11 +02:00
} else {
2019-05-14 14:08:05 +02:00
loadDialogs(folderId, 0, count, false);
2015-09-24 22:52:02 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
});
return;
}
2019-05-14 14:08:05 +02:00
final LongSparseArray<TLRPC.Dialog> new_dialogs_dict = new LongSparseArray<>();
2019-02-08 03:30:32 +01:00
final SparseArray<TLRPC.EncryptedChat> enc_chats_dict;
2018-08-27 10:33:11 +02:00
final LongSparseArray<MessageObject> new_dialogMessage = new LongSparseArray<>();
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>();
for (int a = 0; a < dialogsRes.users.size(); a++) {
TLRPC.User u = dialogsRes.users.get(a);
usersDict.put(u.id, u);
}
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat c = dialogsRes.chats.get(a);
chatsDict.put(c.id, c);
}
2019-02-08 03:30:32 +01:00
if (encChats != null) {
enc_chats_dict = new SparseArray<>();
for (int a = 0, N = encChats.size(); a < N; a++) {
TLRPC.EncryptedChat encryptedChat = encChats.get(a);
enc_chats_dict.put(encryptedChat.id, encryptedChat);
}
} else {
enc_chats_dict = null;
}
2019-05-14 14:08:05 +02:00
if (loadType == DIALOGS_LOAD_TYPE_CACHE) {
nextDialogsCacheOffset.put(folderId, offset + count);
2018-08-27 10:33:11 +02:00
}
TLRPC.Message lastMessage = null;
for (int a = 0; a < dialogsRes.messages.size(); a++) {
TLRPC.Message message = dialogsRes.messages.get(a);
if (lastMessage == null || message.date < lastMessage.date) {
lastMessage = message;
}
if (message.to_id.channel_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.channel_id);
if (chat != null && chat.left && (proxyDialogId == 0 || proxyDialogId != -chat.id)) {
2015-09-24 22:52:02 +02:00
continue;
}
2018-08-27 10:33:11 +02:00
if (chat != null && chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
} else if (message.to_id.chat_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.chat_id);
if (chat != null && chat.migrated_to != null) {
continue;
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
}
MessageObject messageObject = new MessageObject(currentAccount, message, usersDict, chatsDict, false);
new_dialogMessage.put(messageObject.getDialogId(), messageObject);
}
2019-05-14 14:08:05 +02:00
if (!fromCache && !migrate && dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId] != -1 && loadType == 0) {
2019-07-18 15:01:39 +02:00
int totalDialogsLoadCount = getUserConfig().getTotalDialogsCount(folderId);
2019-05-14 14:08:05 +02:00
int dialogsLoadOffsetId;
int dialogsLoadOffsetDate = 0;
int dialogsLoadOffsetChannelId = 0;
int dialogsLoadOffsetChatId = 0;
int dialogsLoadOffsetUserId = 0;
long dialogsLoadOffsetAccess = 0;
if (lastMessage != null && lastMessage.id != dialogsLoadOffset[UserConfig.i_dialogsLoadOffsetId]) {
totalDialogsLoadCount += dialogsRes.dialogs.size();
dialogsLoadOffsetId = lastMessage.id;
dialogsLoadOffsetDate = lastMessage.date;
2018-08-27 10:33:11 +02:00
if (lastMessage.to_id.channel_id != 0) {
2019-05-14 14:08:05 +02:00
dialogsLoadOffsetChannelId = lastMessage.to_id.channel_id;
dialogsLoadOffsetChatId = 0;
dialogsLoadOffsetUserId = 0;
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat chat = dialogsRes.chats.get(a);
2019-05-14 14:08:05 +02:00
if (chat.id == dialogsLoadOffsetChannelId) {
dialogsLoadOffsetAccess = chat.access_hash;
2018-08-27 10:33:11 +02:00
break;
}
2018-08-27 10:33:11 +02:00
}
} else if (lastMessage.to_id.chat_id != 0) {
2019-05-14 14:08:05 +02:00
dialogsLoadOffsetChatId = lastMessage.to_id.chat_id;
dialogsLoadOffsetChannelId = 0;
dialogsLoadOffsetUserId = 0;
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat chat = dialogsRes.chats.get(a);
2019-05-14 14:08:05 +02:00
if (chat.id == dialogsLoadOffsetChatId) {
dialogsLoadOffsetAccess = chat.access_hash;
2018-08-27 10:33:11 +02:00
break;
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
} else if (lastMessage.to_id.user_id != 0) {
2019-05-14 14:08:05 +02:00
dialogsLoadOffsetUserId = lastMessage.to_id.user_id;
dialogsLoadOffsetChatId = 0;
dialogsLoadOffsetChannelId = 0;
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.users.size(); a++) {
TLRPC.User user = dialogsRes.users.get(a);
2019-05-14 14:08:05 +02:00
if (user.id == dialogsLoadOffsetUserId) {
dialogsLoadOffsetAccess = user.access_hash;
2018-08-27 10:33:11 +02:00
break;
}
2015-11-26 22:04:02 +01:00
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
2019-05-14 14:08:05 +02:00
dialogsLoadOffsetId = Integer.MAX_VALUE;
}
2019-07-18 15:01:39 +02:00
getUserConfig().setDialogsLoadOffset(folderId,
2019-05-14 14:08:05 +02:00
dialogsLoadOffsetId,
dialogsLoadOffsetDate,
dialogsLoadOffsetUserId,
dialogsLoadOffsetChatId,
dialogsLoadOffsetChannelId,
dialogsLoadOffsetAccess);
2019-07-18 15:01:39 +02:00
getUserConfig().setTotalDialogsCount(folderId, totalDialogsLoadCount);
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
}
2015-11-26 22:04:02 +01:00
2019-05-14 14:08:05 +02:00
final ArrayList<TLRPC.Dialog> dialogsToReload = new ArrayList<>();
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog d = dialogsRes.dialogs.get(a);
DialogObject.initDialog(d);
2018-08-27 10:33:11 +02:00
if (d.id == 0) {
continue;
}
2019-02-08 03:30:32 +01:00
int lower_id = (int) d.id;
int high_id = (int) (d.id >> 32);
if (lower_id == 0 && enc_chats_dict != null) {
if (enc_chats_dict.get(high_id) == null) {
continue;
}
}
2018-08-27 10:33:11 +02:00
if (proxyDialogId != 0 && proxyDialogId == d.id) {
proxyDialog = d;
}
if (d.last_message_date == 0) {
MessageObject mess = new_dialogMessage.get(d.id);
if (mess != null) {
d.last_message_date = mess.messageOwner.date;
}
}
boolean allowCheck = true;
if (DialogObject.isChannel(d)) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null) {
if (!chat.megagroup) {
allowCheck = false;
}
if (chat.left && (proxyDialogId == 0 || proxyDialogId != d.id)) {
continue;
}
}
channelsPts.put(-(int) d.id, d.pts);
} else if ((int) d.id < 0) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.migrated_to != null) {
continue;
}
}
new_dialogs_dict.put(d.id, d);
2019-05-14 14:08:05 +02:00
if (allowCheck && loadType == DIALOGS_LOAD_TYPE_CACHE && (d.read_outbox_max_id == 0 || d.read_inbox_max_id == 0) && d.top_message != 0) {
2018-08-27 10:33:11 +02:00
dialogsToReload.add(d);
}
Integer value = dialogs_read_inbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_inbox_max.put(d.id, Math.max(value, d.read_inbox_max_id));
value = dialogs_read_outbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_outbox_max.put(d.id, Math.max(value, d.read_outbox_max_id));
}
2019-05-14 14:08:05 +02:00
if (loadType != DIALOGS_LOAD_TYPE_CACHE) {
2018-08-27 10:33:11 +02:00
ImageLoader.saveMessagesThumbs(dialogsRes.messages);
for (int a = 0; a < dialogsRes.messages.size(); a++) {
TLRPC.Message message = dialogsRes.messages.get(a);
if (message.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
TLRPC.User user = usersDict.get(message.action.user_id);
if (user != null && user.bot) {
message.reply_markup = new TLRPC.TL_replyKeyboardHide();
message.flags |= 64;
}
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
if (message.action instanceof TLRPC.TL_messageActionChatMigrateTo || message.action instanceof TLRPC.TL_messageActionChannelCreate) {
message.unread = false;
message.media_unread = false;
} else {
ConcurrentHashMap<Long, Integer> read_max = message.out ? dialogs_read_outbox_max : dialogs_read_inbox_max;
Integer value = read_max.get(message.dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
2018-08-27 10:33:11 +02:00
read_max.put(message.dialog_id, value);
}
message.unread = value < message.id;
}
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putDialogs(dialogsRes, 0);
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
if (loadType == DIALOGS_LOAD_TYPE_CHANNEL) {
2018-08-27 10:33:11 +02:00
TLRPC.Chat chat = dialogsRes.chats.get(0);
getChannelDifference(chat.id);
checkChannelInviter(chat.id);
}
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
2019-05-14 14:08:05 +02:00
if (loadType != DIALOGS_LOAD_TYPE_CACHE) {
2018-08-27 10:33:11 +02:00
applyDialogsNotificationsSettings(dialogsRes.dialogs);
2019-07-18 15:01:39 +02:00
if (!getUserConfig().draftsLoaded) {
getMediaDataController().loadDrafts();
2018-08-27 10:33:11 +02:00
}
}
2019-05-14 14:08:05 +02:00
putUsers(dialogsRes.users, loadType == DIALOGS_LOAD_TYPE_CACHE);
putChats(dialogsRes.chats, loadType == DIALOGS_LOAD_TYPE_CACHE);
2018-08-27 10:33:11 +02:00
if (encChats != null) {
for (int a = 0; a < encChats.size(); a++) {
TLRPC.EncryptedChat encryptedChat = encChats.get(a);
if (encryptedChat instanceof TLRPC.TL_encryptedChat && AndroidUtilities.getMyLayerVersion(encryptedChat.layer) < SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
2019-07-18 15:01:39 +02:00
getSecretChatHelper().sendNotifyLayerMessage(encryptedChat, null);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
putEncryptedChat(encryptedChat, true);
2015-09-24 22:52:02 +02:00
}
}
2019-05-14 14:08:05 +02:00
if (!migrate && loadType != DIALOGS_LOAD_TYPE_UNKNOWN && loadType != DIALOGS_LOAD_TYPE_CHANNEL) {
loadingDialogs.put(folderId, false);
}
2018-08-27 10:33:11 +02:00
boolean added = false;
2019-05-14 14:08:05 +02:00
dialogsLoaded = true;
2015-09-24 22:52:02 +02:00
2019-05-14 14:08:05 +02:00
int archivedDialogsCount = 0;
int lastDialogDate = migrate && !allDialogs.isEmpty() ? allDialogs.get(allDialogs.size() - 1).last_message_date : 0;
2018-08-27 10:33:11 +02:00
for (int a = 0; a < new_dialogs_dict.size(); a++) {
long key = new_dialogs_dict.keyAt(a);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog value = new_dialogs_dict.valueAt(a);
TLRPC.Dialog currentDialog;
if (loadType != DIALOGS_LOAD_TYPE_UNKNOWN) {
currentDialog = dialogs_dict.get(key);
} else {
currentDialog = null;
}
if (migrate && currentDialog != null) {
currentDialog.folder_id = value.folder_id;
}
2018-08-27 10:33:11 +02:00
if (migrate && lastDialogDate != 0 && value.last_message_date < lastDialogDate) {
continue;
}
2019-05-14 14:08:05 +02:00
if (loadType != DIALOGS_LOAD_TYPE_CACHE && value.draft instanceof TLRPC.TL_draftMessage) {
2019-07-18 15:01:39 +02:00
getMediaDataController().saveDraft(value.id, value.draft, null, false);
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
if (value.folder_id != folderId) {
archivedDialogsCount++;
}
2018-08-27 10:33:11 +02:00
if (currentDialog == null) {
added = true;
dialogs_dict.put(key, value);
MessageObject messageObject = new_dialogMessage.get(value.id);
dialogMessage.put(key, messageObject);
if (messageObject != null && messageObject.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(messageObject.getId(), messageObject);
if (messageObject.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(messageObject.messageOwner.random_id, messageObject);
2015-09-24 22:52:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
} else {
2019-05-14 14:08:05 +02:00
if (loadType != DIALOGS_LOAD_TYPE_CACHE) {
2018-08-27 10:33:11 +02:00
currentDialog.notify_settings = value.notify_settings;
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
currentDialog.pinned = value.pinned;
currentDialog.pinnedNum = value.pinnedNum;
MessageObject oldMsg = dialogMessage.get(key);
if (oldMsg != null && oldMsg.deleted || oldMsg == null || currentDialog.top_message > 0) {
if (value.top_message >= currentDialog.top_message) {
2015-09-24 22:52:02 +02:00
dialogs_dict.put(key, value);
MessageObject messageObject = new_dialogMessage.get(value.id);
dialogMessage.put(key, messageObject);
if (messageObject != null && messageObject.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(messageObject.getId(), messageObject);
2018-08-27 10:33:11 +02:00
if (messageObject != null && messageObject.messageOwner.random_id != 0) {
2016-03-06 02:49:31 +01:00
dialogMessagesByRandomIds.put(messageObject.messageOwner.random_id, messageObject);
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
if (oldMsg != null) {
dialogMessagesByIds.remove(oldMsg.getId());
if (oldMsg.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(oldMsg.messageOwner.random_id);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
}
}
} else {
MessageObject newMsg = new_dialogMessage.get(value.id);
if (oldMsg.deleted || newMsg == null || newMsg.messageOwner.date > oldMsg.messageOwner.date) {
dialogs_dict.put(key, value);
dialogMessage.put(key, newMsg);
if (newMsg != null && newMsg.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(newMsg.getId(), newMsg);
if (newMsg != null && newMsg.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(newMsg.messageOwner.random_id, newMsg);
2013-10-25 17:19:00 +02:00
}
}
2018-08-27 10:33:11 +02:00
dialogMessagesByIds.remove(oldMsg.getId());
if (oldMsg.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(oldMsg.messageOwner.random_id);
}
2013-10-25 17:19:00 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
}
2013-10-25 17:19:00 +02:00
2019-05-14 14:08:05 +02:00
allDialogs.clear();
2018-08-27 10:33:11 +02:00
for (int a = 0, size = dialogs_dict.size(); a < size; a++) {
2019-05-14 14:08:05 +02:00
allDialogs.add(dialogs_dict.valueAt(a));
2018-08-27 10:33:11 +02:00
}
sortDialogs(migrate ? chatsDict : null);
2015-09-24 22:52:02 +02:00
2019-05-14 14:08:05 +02:00
if (loadType != DIALOGS_LOAD_TYPE_CHANNEL && loadType != DIALOGS_LOAD_TYPE_UNKNOWN) {
2018-08-27 10:33:11 +02:00
if (!migrate) {
2019-05-14 14:08:05 +02:00
dialogsEndReached.put(folderId, (dialogsRes.dialogs.size() == 0 || dialogsRes.dialogs.size() != count) && loadType == 0);
if (archivedDialogsCount > 0 && archivedDialogsCount < 20 && folderId == 0) {
dialogsEndReached.put(1, true);
2019-07-18 15:01:39 +02:00
int[] dialogsLoadOffsetArchived = getUserConfig().getDialogLoadOffsets(folderId);
2019-05-14 14:08:05 +02:00
if (dialogsLoadOffsetArchived[UserConfig.i_dialogsLoadOffsetId] == Integer.MAX_VALUE) {
serverDialogsEndReached.put(1, true);
}
}
2018-08-27 10:33:11 +02:00
if (!fromCache) {
2019-05-14 14:08:05 +02:00
serverDialogsEndReached.put(folderId, (dialogsRes.dialogs.size() == 0 || dialogsRes.dialogs.size() != count) && loadType == 0);
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
}
}
2019-07-18 15:01:39 +02:00
int totalDialogsLoadCount = getUserConfig().getTotalDialogsCount(folderId);
int[] dialogsLoadOffset2 = getUserConfig().getDialogLoadOffsets(folderId);
2019-05-14 14:08:05 +02:00
if (!fromCache && !migrate && totalDialogsLoadCount < 400 && dialogsLoadOffset2[UserConfig.i_dialogsLoadOffsetId] != -1 && dialogsLoadOffset2[UserConfig.i_dialogsLoadOffsetId] != Integer.MAX_VALUE) {
loadDialogs(0, 100, folderId, false);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2015-10-29 18:10:07 +01:00
2018-08-27 10:33:11 +02:00
if (migrate) {
2019-07-18 15:01:39 +02:00
getUserConfig().migrateOffsetId = offset;
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
migratingDialogs = false;
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.needReloadRecentDialogsSearch);
2018-08-27 10:33:11 +02:00
} else {
generateUpdateMessage();
2019-05-14 14:08:05 +02:00
if (!added && loadType == DIALOGS_LOAD_TYPE_CACHE) {
loadDialogs(folderId, 0, count, false);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
migrateDialogs(getUserConfig().migrateOffsetId, getUserConfig().migrateOffsetDate, getUserConfig().migrateOffsetUserId, getUserConfig().migrateOffsetChatId, getUserConfig().migrateOffsetChannelId, getUserConfig().migrateOffsetAccess);
2018-08-27 10:33:11 +02:00
if (!dialogsToReload.isEmpty()) {
reloadDialogsReadValue(dialogsToReload, 0);
}
loadUnreadDialogs();
});
2013-10-25 17:19:00 +02:00
});
}
2015-11-26 22:04:02 +01:00
private void applyDialogNotificationsSettings(long dialog_id, TLRPC.PeerNotifySettings notify_settings) {
2019-01-23 18:03:33 +01:00
if (notify_settings == null) {
return;
}
2018-07-30 04:07:02 +02:00
int currentValue = notificationsPreferences.getInt("notify2_" + dialog_id, -1);
int currentValue2 = notificationsPreferences.getInt("notifyuntil_" + dialog_id, 0);
SharedPreferences.Editor editor = notificationsPreferences.edit();
2016-03-06 02:49:31 +01:00
boolean updated = false;
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(dialog_id);
2015-11-26 22:04:02 +01:00
if (dialog != null) {
dialog.notify_settings = notify_settings;
}
2018-07-30 04:07:02 +02:00
if ((notify_settings.flags & 2) != 0) {
editor.putBoolean("silent_" + dialog_id, notify_settings.silent);
} else {
editor.remove("silent_" + dialog_id);
}
if ((notify_settings.flags & 4) != 0) {
2019-07-18 15:01:39 +02:00
if (notify_settings.mute_until > getConnectionsManager().getCurrentTime()) {
2018-07-30 04:07:02 +02:00
int until = 0;
2019-07-18 15:01:39 +02:00
if (notify_settings.mute_until > getConnectionsManager().getCurrentTime() + 60 * 60 * 24 * 365) {
2018-07-30 04:07:02 +02:00
if (currentValue != 2) {
updated = true;
editor.putInt("notify2_" + dialog_id, 2);
if (dialog != null) {
dialog.notify_settings.mute_until = Integer.MAX_VALUE;
}
}
} else {
if (currentValue != 3 || currentValue2 != notify_settings.mute_until) {
updated = true;
editor.putInt("notify2_" + dialog_id, 3);
editor.putInt("notifyuntil_" + dialog_id, notify_settings.mute_until);
if (dialog != null) {
dialog.notify_settings.mute_until = until;
}
2015-11-26 22:04:02 +01:00
}
2018-07-30 04:07:02 +02:00
until = notify_settings.mute_until;
2015-11-26 22:04:02 +01:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, ((long) until << 32) | 1);
getNotificationsController().removeNotificationsForDialog(dialog_id);
2015-11-26 22:04:02 +01:00
} else {
2018-07-30 04:07:02 +02:00
if (currentValue != 0 && currentValue != 1) {
2016-03-06 02:49:31 +01:00
updated = true;
2015-11-26 22:04:02 +01:00
if (dialog != null) {
2018-07-30 04:07:02 +02:00
dialog.notify_settings.mute_until = 0;
2015-11-26 22:04:02 +01:00
}
2018-07-30 04:07:02 +02:00
editor.putInt("notify2_" + dialog_id, 0);
2015-11-26 22:04:02 +01:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, 0);
2015-11-26 22:04:02 +01:00
}
} else {
2018-07-30 04:07:02 +02:00
if (currentValue != -1) {
2016-03-06 02:49:31 +01:00
updated = true;
2015-11-26 22:04:02 +01:00
if (dialog != null) {
dialog.notify_settings.mute_until = 0;
}
editor.remove("notify2_" + dialog_id);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, 0);
2015-11-26 22:04:02 +01:00
}
2016-03-06 02:49:31 +01:00
editor.commit();
if (updated) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.notificationsSettingsUpdated);
2015-11-26 22:04:02 +01:00
}
}
2019-05-14 14:08:05 +02:00
private void applyDialogsNotificationsSettings(ArrayList<TLRPC.Dialog> dialogs) {
2015-09-24 22:52:02 +02:00
SharedPreferences.Editor editor = null;
2015-11-26 22:04:02 +01:00
for (int a = 0; a < dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs.get(a);
2015-09-24 22:52:02 +02:00
if (dialog.peer != null && dialog.notify_settings instanceof TLRPC.TL_peerNotifySettings) {
if (editor == null) {
2018-07-30 04:07:02 +02:00
editor = notificationsPreferences.edit();
2015-09-24 22:52:02 +02:00
}
int dialog_id;
if (dialog.peer.user_id != 0) {
dialog_id = dialog.peer.user_id;
} else if (dialog.peer.chat_id != 0) {
dialog_id = -dialog.peer.chat_id;
} else {
dialog_id = -dialog.peer.channel_id;
}
2018-07-30 04:07:02 +02:00
if ((dialog.notify_settings.flags & 2) != 0) {
editor.putBoolean("silent_" + dialog_id, dialog.notify_settings.silent);
} else {
editor.remove("silent_" + dialog_id);
}
if ((dialog.notify_settings.flags & 4) != 0) {
2019-07-18 15:01:39 +02:00
if (dialog.notify_settings.mute_until > getConnectionsManager().getCurrentTime()) {
if (dialog.notify_settings.mute_until > getConnectionsManager().getCurrentTime() + 60 * 60 * 24 * 365) {
2018-07-30 04:07:02 +02:00
editor.putInt("notify2_" + dialog_id, 2);
dialog.notify_settings.mute_until = Integer.MAX_VALUE;
} else {
editor.putInt("notify2_" + dialog_id, 3);
editor.putInt("notifyuntil_" + dialog_id, dialog.notify_settings.mute_until);
}
2015-09-24 22:52:02 +02:00
} else {
2018-07-30 04:07:02 +02:00
editor.putInt("notify2_" + dialog_id, 0);
2015-09-24 22:52:02 +02:00
}
2015-11-26 22:04:02 +01:00
} else {
editor.remove("notify2_" + dialog_id);
2015-09-24 22:52:02 +02:00
}
}
}
if (editor != null) {
editor.commit();
}
}
2017-12-08 18:35:59 +01:00
public void reloadMentionsCountForChannels(final ArrayList<Integer> arrayList) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0; a < arrayList.size(); a++) {
final long dialog_id = -arrayList.get(a);
TLRPC.TL_messages_getUnreadMentions req = new TLRPC.TL_messages_getUnreadMentions();
req.peer = getInputPeer((int) dialog_id);
req.limit = 1;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
if (res != null) {
int newCount;
if (res.count != 0) {
newCount = res.count;
} else {
newCount = res.messages.size();
2017-12-08 18:35:59 +01:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().resetMentionsCount(dialog_id, newCount);
2018-08-27 10:33:11 +02:00
}
}));
2017-12-08 18:35:59 +01:00
}
});
}
2018-07-30 04:07:02 +02:00
public void processDialogsUpdateRead(final LongSparseArray<Integer> dialogsToUpdate, final LongSparseArray<Integer> dialogsMentionsToUpdate) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (dialogsToUpdate != null) {
for (int a = 0; a < dialogsToUpdate.size(); a++) {
long dialogId = dialogsToUpdate.keyAt(a);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog currentDialog = dialogs_dict.get(dialogId);
2018-08-27 10:33:11 +02:00
if (currentDialog != null) {
int prevCount = currentDialog.unread_count;
currentDialog.unread_count = dialogsToUpdate.valueAt(a);
if (prevCount != 0 && currentDialog.unread_count == 0 && !isDialogMuted(dialogId)) {
unreadUnmutedDialogs--;
} else if (prevCount == 0 && !currentDialog.unread_mark && currentDialog.unread_count != 0 && !isDialogMuted(dialogId)) {
unreadUnmutedDialogs++;
2017-12-08 18:35:59 +01:00
}
}
}
2018-08-27 10:33:11 +02:00
}
if (dialogsMentionsToUpdate != null) {
for (int a = 0; a < dialogsMentionsToUpdate.size(); a++) {
long dialogId = dialogsMentionsToUpdate.keyAt(a);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog currentDialog = dialogs_dict.get(dialogId);
2018-08-27 10:33:11 +02:00
if (currentDialog != null) {
currentDialog.unread_mentions_count = dialogsMentionsToUpdate.valueAt(a);
if (createdDialogMainThreadIds.contains(currentDialog.id)) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateMentionsCount, currentDialog.id, currentDialog.unread_mentions_count);
2017-12-08 18:35:59 +01:00
}
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
2018-08-27 10:33:11 +02:00
if (dialogsToUpdate != null) {
2019-07-18 15:01:39 +02:00
getNotificationsController().processDialogsUpdateRead(dialogsToUpdate);
2015-09-24 22:52:02 +02:00
}
});
}
2019-05-14 14:08:05 +02:00
protected void checkLastDialogMessage(final TLRPC.Dialog dialog, final TLRPC.InputPeer peer, long taskId) {
final int lower_id = (int) dialog.id;
2018-07-30 04:07:02 +02:00
if (lower_id == 0 || checkingLastMessagesDialogs.indexOfKey(lower_id) >= 0) {
return;
}
TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory();
req.peer = peer == null ? getInputPeer(lower_id) : peer;
2019-05-14 14:08:05 +02:00
if (req.peer == null) {
return;
}
req.limit = 1;
checkingLastMessagesDialogs.put(lower_id, true);
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
2019-05-14 14:08:05 +02:00
data = new NativeByteBuffer(60 + req.peer.getObjectSize());
data.writeInt32(14);
data.writeInt64(dialog.id);
data.writeInt32(dialog.top_message);
data.writeInt32(dialog.read_inbox_max_id);
data.writeInt32(dialog.read_outbox_max_id);
data.writeInt32(dialog.unread_count);
data.writeInt32(dialog.last_message_date);
data.writeInt32(dialog.pts);
data.writeInt32(dialog.flags);
2017-03-31 01:58:05 +02:00
data.writeBool(dialog.pinned);
data.writeInt32(dialog.pinnedNum);
2017-12-08 18:35:59 +01:00
data.writeInt32(dialog.unread_mentions_count);
2018-07-30 04:07:02 +02:00
data.writeBool(dialog.unread_mark);
2019-05-14 14:08:05 +02:00
data.writeInt32(dialog.folder_id);
peer.serializeToStream(data);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
} else {
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
2019-05-14 14:08:05 +02:00
removeDeletedMessagesFromArray(lower_id, res.messages);
2018-08-27 10:33:11 +02:00
if (!res.messages.isEmpty()) {
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
TLRPC.Message newMessage = res.messages.get(0);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog newDialog = new TLRPC.TL_dialog();
2018-08-27 10:33:11 +02:00
newDialog.flags = dialog.flags;
newDialog.top_message = newMessage.id;
newDialog.last_message_date = newMessage.date;
newDialog.notify_settings = dialog.notify_settings;
newDialog.pts = dialog.pts;
newDialog.unread_count = dialog.unread_count;
newDialog.unread_mark = dialog.unread_mark;
newDialog.unread_mentions_count = dialog.unread_mentions_count;
newDialog.read_inbox_max_id = dialog.read_inbox_max_id;
newDialog.read_outbox_max_id = dialog.read_outbox_max_id;
newDialog.pinned = dialog.pinned;
newDialog.pinnedNum = dialog.pinnedNum;
2019-05-14 14:08:05 +02:00
newDialog.folder_id = dialog.folder_id;
2018-08-27 10:33:11 +02:00
newMessage.dialog_id = newDialog.id = dialog.id;
dialogs.users.addAll(res.users);
dialogs.chats.addAll(res.chats);
dialogs.dialogs.add(newDialog);
dialogs.messages.addAll(res.messages);
dialogs.count = 1;
processDialogsUpdate(dialogs, null);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(res.messages, true, true, false, getDownloadController().getAutodownloadMask(), true);
2018-08-27 10:33:11 +02:00
} else {
AndroidUtilities.runOnUIThread(() -> {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog currentDialog = dialogs_dict.get(dialog.id);
2018-08-27 10:33:11 +02:00
if (currentDialog != null && currentDialog.top_message == 0) {
deleteDialog(dialog.id, 3);
}
});
}
}
2018-08-27 10:33:11 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2018-08-27 10:33:11 +02:00
}
AndroidUtilities.runOnUIThread(() -> checkingLastMessagesDialogs.delete(lower_id));
});
}
2015-09-24 22:52:02 +02:00
public void processDialogsUpdate(final TLRPC.messages_Dialogs dialogsRes, ArrayList<TLRPC.EncryptedChat> encChats) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
2019-05-14 14:08:05 +02:00
final LongSparseArray<TLRPC.Dialog> new_dialogs_dict = new LongSparseArray<>();
2018-08-27 10:33:11 +02:00
final LongSparseArray<MessageObject> new_dialogMessage = new LongSparseArray<>();
final SparseArray<TLRPC.User> usersDict = new SparseArray<>(dialogsRes.users.size());
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>(dialogsRes.chats.size());
final LongSparseArray<Integer> dialogsToUpdate = new LongSparseArray<>();
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.users.size(); a++) {
TLRPC.User u = dialogsRes.users.get(a);
usersDict.put(u.id, u);
}
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat c = dialogsRes.chats.get(a);
chatsDict.put(c.id, c);
}
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogsRes.messages.size(); a++) {
TLRPC.Message message = dialogsRes.messages.get(a);
if (proxyDialogId == 0 || proxyDialogId != message.dialog_id) {
if (message.to_id.channel_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.channel_id);
if (chat != null && chat.left) {
continue;
}
} else if (message.to_id.chat_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.chat_id);
if (chat != null && chat.migrated_to != null) {
continue;
2013-10-25 17:19:00 +02:00
}
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
MessageObject messageObject = new MessageObject(currentAccount, message, usersDict, chatsDict, false);
new_dialogMessage.put(messageObject.getDialogId(), messageObject);
}
for (int a = 0; a < dialogsRes.dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog d = dialogsRes.dialogs.get(a);
DialogObject.initDialog(d);
2018-08-27 10:33:11 +02:00
if (proxyDialogId == 0 || proxyDialogId != d.id) {
if (DialogObject.isChannel(d)) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.left) {
continue;
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
} else if ((int) d.id < 0) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.migrated_to != null) {
continue;
2013-10-25 17:19:00 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
if (d.last_message_date == 0) {
MessageObject mess = new_dialogMessage.get(d.id);
if (mess != null) {
d.last_message_date = mess.messageOwner.date;
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
}
new_dialogs_dict.put(d.id, d);
dialogsToUpdate.put(d.id, d.unread_count);
2018-08-27 10:33:11 +02:00
Integer value = dialogs_read_inbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_inbox_max.put(d.id, Math.max(value, d.read_inbox_max_id));
value = dialogs_read_outbox_max.get(d.id);
if (value == null) {
value = 0;
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(d.id, Math.max(value, d.read_outbox_max_id));
}
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(dialogsRes.users, true);
putChats(dialogsRes.chats, true);
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
for (int a = 0; a < new_dialogs_dict.size(); a++) {
long key = new_dialogs_dict.keyAt(a);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog value = new_dialogs_dict.valueAt(a);
TLRPC.Dialog currentDialog = dialogs_dict.get(key);
2018-08-27 10:33:11 +02:00
if (currentDialog == null) {
2019-05-14 14:08:05 +02:00
int offset = nextDialogsCacheOffset.get(value.folder_id, 0) + 1;
nextDialogsCacheOffset.put(value.folder_id, offset);
2018-08-27 10:33:11 +02:00
dialogs_dict.put(key, value);
MessageObject messageObject = new_dialogMessage.get(value.id);
dialogMessage.put(key, messageObject);
if (messageObject != null && messageObject.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(messageObject.getId(), messageObject);
if (messageObject.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(messageObject.messageOwner.random_id, messageObject);
}
}
} else {
currentDialog.unread_count = value.unread_count;
if (currentDialog.unread_mentions_count != value.unread_mentions_count) {
currentDialog.unread_mentions_count = value.unread_mentions_count;
if (createdDialogMainThreadIds.contains(currentDialog.id)) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateMentionsCount, currentDialog.id, currentDialog.unread_mentions_count);
2018-08-27 10:33:11 +02:00
}
}
MessageObject oldMsg = dialogMessage.get(key);
if (oldMsg == null || currentDialog.top_message > 0) {
if (oldMsg != null && oldMsg.deleted || value.top_message > currentDialog.top_message) {
2013-10-25 17:19:00 +02:00
dialogs_dict.put(key, value);
2015-09-24 22:52:02 +02:00
MessageObject messageObject = new_dialogMessage.get(value.id);
dialogMessage.put(key, messageObject);
if (messageObject != null && messageObject.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(messageObject.getId(), messageObject);
2016-03-06 02:49:31 +01:00
if (messageObject.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(messageObject.messageOwner.random_id, messageObject);
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
if (oldMsg != null) {
dialogMessagesByIds.remove(oldMsg.getId());
if (oldMsg.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(oldMsg.messageOwner.random_id);
2017-12-08 18:35:59 +01:00
}
}
2018-08-27 10:33:11 +02:00
if (messageObject == null) {
checkLastDialogMessage(value, null, 0);
}
}
} else {
MessageObject newMsg = new_dialogMessage.get(value.id);
if (oldMsg.deleted || newMsg == null || newMsg.messageOwner.date > oldMsg.messageOwner.date) {
dialogs_dict.put(key, value);
dialogMessage.put(key, newMsg);
if (newMsg != null && newMsg.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(newMsg.getId(), newMsg);
if (newMsg.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(newMsg.messageOwner.random_id, newMsg);
2013-10-25 17:19:00 +02:00
}
}
2018-08-27 10:33:11 +02:00
dialogMessagesByIds.remove(oldMsg.getId());
if (oldMsg.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(oldMsg.messageOwner.random_id);
}
2013-10-25 17:19:00 +02:00
}
}
}
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
allDialogs.clear();
2018-08-27 10:33:11 +02:00
for (int a = 0, size = dialogs_dict.size(); a < size; a++) {
2019-05-14 14:08:05 +02:00
allDialogs.add(dialogs_dict.valueAt(a));
2018-08-27 10:33:11 +02:00
}
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
getNotificationsController().processDialogsUpdateRead(dialogsToUpdate);
2018-08-27 10:33:11 +02:00
});
2013-10-25 17:19:00 +02:00
});
}
2019-01-23 18:03:33 +01:00
public void addToViewsQueue(MessageObject messageObject) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
2019-01-23 18:03:33 +01:00
int peer = (int) messageObject.getDialogId();
int id = messageObject.getId();
2018-08-27 10:33:11 +02:00
ArrayList<Integer> ids = channelViewsToSend.get(peer);
if (ids == null) {
ids = new ArrayList<>();
channelViewsToSend.put(peer, ids);
}
2019-01-23 18:03:33 +01:00
if (!ids.contains(id)) {
ids.add(id);
2015-09-24 22:52:02 +02:00
}
});
}
2019-01-23 18:03:33 +01:00
public void addToPollsQueue(long dialogId, ArrayList<MessageObject> visibleObjects) {
SparseArray<MessageObject> array = pollsToCheck.get(dialogId);
if (array == null) {
array = new SparseArray<>();
pollsToCheck.put(dialogId, array);
pollsToCheckSize++;
}
for (int a = 0, N = array.size(); a < N; a++) {
MessageObject object = array.valueAt(a);
object.pollVisibleOnScreen = false;
}
for (int a = 0, N = visibleObjects.size(); a < N; a++) {
MessageObject messageObject = visibleObjects.get(a);
if (messageObject.type != MessageObject.TYPE_POLL) {
continue;
}
int id = messageObject.getId();
MessageObject object = array.get(id);
if (object != null) {
object.pollVisibleOnScreen = true;
} else {
array.put(id, messageObject);
}
}
}
2016-03-06 02:49:31 +01:00
public void markMessageContentAsRead(final MessageObject messageObject) {
2015-09-24 22:52:02 +02:00
ArrayList<Long> arrayList = new ArrayList<>();
2016-03-06 02:49:31 +01:00
long messageId = messageObject.getId();
if (messageObject.messageOwner.to_id.channel_id != 0) {
messageId |= ((long) messageObject.messageOwner.to_id.channel_id) << 32;
2015-09-24 22:52:02 +02:00
}
2017-12-08 18:35:59 +01:00
if (messageObject.messageOwner.mentioned) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().markMentionMessageAsRead(messageObject.getId(), messageObject.messageOwner.to_id.channel_id, messageObject.getDialogId());
2017-12-08 18:35:59 +01:00
}
2015-09-24 22:52:02 +02:00
arrayList.add(messageId);
2019-07-18 15:01:39 +02:00
getMessagesStorage().markMessagesContentAsRead(arrayList, 0);
getNotificationCenter().postNotificationName(NotificationCenter.messagesReadContent, arrayList);
2016-03-06 02:49:31 +01:00
if (messageObject.getId() < 0) {
markMessageAsRead(messageObject.getDialogId(), messageObject.messageOwner.random_id, Integer.MIN_VALUE);
2017-12-08 18:35:59 +01:00
} else {
if (messageObject.messageOwner.to_id.channel_id != 0) {
TLRPC.TL_channels_readMessageContents req = new TLRPC.TL_channels_readMessageContents();
req.channel = getInputChannel(messageObject.messageOwner.to_id.channel_id);
if (req.channel == null) {
return;
}
req.id.add(messageObject.getId());
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2017-12-08 18:35:59 +01:00
});
} else {
TLRPC.TL_messages_readMessageContents req = new TLRPC.TL_messages_readMessageContents();
req.id.add(messageObject.getId());
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
2017-12-08 18:35:59 +01:00
}
});
}
}
}
public void markMentionMessageAsRead(final int mid, final int channelId, final long did) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().markMentionMessageAsRead(mid, channelId, did);
2017-12-08 18:35:59 +01:00
if (channelId != 0) {
TLRPC.TL_channels_readMessageContents req = new TLRPC.TL_channels_readMessageContents();
req.channel = getInputChannel(channelId);
if (req.channel == null) {
return;
}
req.id.add(mid);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2017-12-08 18:35:59 +01:00
});
2016-03-06 02:49:31 +01:00
} else {
TLRPC.TL_messages_readMessageContents req = new TLRPC.TL_messages_readMessageContents();
2017-12-08 18:35:59 +01:00
req.id.add(mid);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
}
2016-03-06 02:49:31 +01:00
});
}
}
2019-01-23 18:03:33 +01:00
public void markMessageAsRead(final int mid, final int channelId, TLRPC.InputChannel inputChannel, int ttl, long taskId) {
if (mid == 0 || ttl <= 0) {
return;
}
2019-01-23 18:03:33 +01:00
if (channelId != 0 && inputChannel == null) {
inputChannel = getInputChannel(channelId);
if (inputChannel == null) {
return;
}
}
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(16 + (inputChannel != null ? inputChannel.getObjectSize() : 0));
data.writeInt32(11);
data.writeInt32(mid);
data.writeInt32(channelId);
data.writeInt32(ttl);
if (channelId != 0) {
inputChannel.serializeToStream(data);
}
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2019-01-23 18:03:33 +01:00
} else {
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
int time = getConnectionsManager().getCurrentTime();
getMessagesStorage().createTaskForMid(mid, channelId, time, time, ttl, false);
2017-12-08 18:35:59 +01:00
if (channelId != 0) {
TLRPC.TL_channels_readMessageContents req = new TLRPC.TL_channels_readMessageContents();
2019-01-23 18:03:33 +01:00
req.channel = inputChannel;
2017-12-08 18:35:59 +01:00
req.id.add(mid);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-01-23 18:03:33 +01:00
}
2017-12-08 18:35:59 +01:00
});
} else {
TLRPC.TL_messages_readMessageContents req = new TLRPC.TL_messages_readMessageContents();
req.id.add(mid);
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
2017-12-08 18:35:59 +01:00
}
2019-01-23 18:03:33 +01:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-01-23 18:03:33 +01:00
}
2017-12-08 18:35:59 +01:00
});
}
}
2014-10-22 22:01:07 +02:00
public void markMessageAsRead(final long dialog_id, final long random_id, int ttl) {
2016-03-06 02:49:31 +01:00
if (random_id == 0 || dialog_id == 0 || ttl <= 0 && ttl != Integer.MIN_VALUE) {
return;
}
2015-05-21 23:27:27 +02:00
int lower_part = (int) dialog_id;
int high_id = (int) (dialog_id >> 32);
if (lower_part != 0) {
return;
}
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
if (chat == null) {
return;
}
2015-01-02 23:15:07 +01:00
ArrayList<Long> random_ids = new ArrayList<>();
random_ids.add(random_id);
2019-07-18 15:01:39 +02:00
getSecretChatHelper().sendMessagesReadMessage(chat, random_ids, null);
2016-03-06 02:49:31 +01:00
if (ttl > 0) {
2019-07-18 15:01:39 +02:00
int time = getConnectionsManager().getCurrentTime();
getMessagesStorage().createTaskForSecretChat(chat.id, time, time, 0, random_ids);
2016-03-06 02:49:31 +01:00
}
}
2018-07-30 04:07:02 +02:00
private void completeReadTask(ReadTask task) {
int lower_part = (int) task.dialogId;
int high_id = (int) (task.dialogId >> 32);
2013-10-25 17:19:00 +02:00
if (lower_part != 0) {
2015-09-24 22:52:02 +02:00
TLRPC.InputPeer inputPeer = getInputPeer(lower_part);
TLObject req;
if (inputPeer instanceof TLRPC.TL_inputPeerChannel) {
TLRPC.TL_channels_readHistory request = new TLRPC.TL_channels_readHistory();
request.channel = getInputChannel(-lower_part);
2018-07-30 04:07:02 +02:00
request.max_id = task.maxId;
2015-09-24 22:52:02 +02:00
req = request;
2013-10-25 17:19:00 +02:00
} else {
2015-09-24 22:52:02 +02:00
TLRPC.TL_messages_readHistory request = new TLRPC.TL_messages_readHistory();
request.peer = inputPeer;
2018-07-30 04:07:02 +02:00
request.max_id = task.maxId;
2015-09-24 22:52:02 +02:00
req = request;
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
if (response instanceof TLRPC.TL_messages_affectedMessages) {
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
2018-07-30 04:07:02 +02:00
}
}
});
} else {
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
if (chat.auth_key != null && chat.auth_key.length > 1 && chat instanceof TLRPC.TL_encryptedChat) {
TLRPC.TL_messages_readEncryptedHistory req = new TLRPC.TL_messages_readEncryptedHistory();
req.peer = new TLRPC.TL_inputEncryptedChat();
req.peer.chat_id = chat.id;
req.peer.access_hash = chat.access_hash;
req.max_date = task.maxDate;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-07-30 04:07:02 +02:00
});
}
}
}
private void checkReadTasks() {
long time = SystemClock.elapsedRealtime();
for (int a = 0, size = readTasks.size(); a < size; a++) {
ReadTask task = readTasks.get(a);
if (task.sendRequestTime > time) {
continue;
}
completeReadTask(task);
readTasks.remove(a);
readTasksMap.remove(task.dialogId);
a--;
size--;
}
}
public void markDialogAsReadNow(final long dialogId) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
ReadTask currentReadTask = readTasksMap.get(dialogId);
if (currentReadTask == null) {
return;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
completeReadTask(currentReadTask);
readTasks.remove(currentReadTask);
readTasksMap.remove(dialogId);
2018-07-30 04:07:02 +02:00
});
}
public void markMentionsAsRead(long dialogId) {
if ((int) dialogId == 0) {
return;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().resetMentionsCount(dialogId, 0);
2018-07-30 04:07:02 +02:00
TLRPC.TL_messages_readMentions req = new TLRPC.TL_messages_readMentions();
2019-07-18 15:01:39 +02:00
req.peer = getInputPeer((int) dialogId);
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-07-30 04:07:02 +02:00
});
}
public void markDialogAsRead(final long dialogId, final int maxPositiveId, final int maxNegativeId, final int maxDate, final boolean popup, final int countDiff, final boolean readNow) {
int lower_part = (int) dialogId;
int high_id = (int) (dialogId >> 32);
boolean createReadTask;
2019-07-18 15:01:39 +02:00
boolean countMessages = getNotificationsController().showBadgeMessages;
2018-07-30 04:07:02 +02:00
if (lower_part != 0) {
if (maxPositiveId == 0 || high_id == 1) {
return;
}
long maxMessageId = maxPositiveId;
long minMessageId = maxNegativeId;
boolean isChannel = false;
if (lower_part < 0) {
TLRPC.Chat chat = getChat(-lower_part);
if (ChatObject.isChannel(chat)) {
maxMessageId |= ((long) -lower_part) << 32;
minMessageId |= ((long) -lower_part) << 32;
isChannel = true;
}
}
Integer value = dialogs_read_inbox_max.get(dialogId);
2015-11-26 22:04:02 +01:00
if (value == null) {
value = 0;
}
2018-07-30 04:07:02 +02:00
dialogs_read_inbox_max.put(dialogId, Math.max(value, maxPositiveId));
2015-09-24 22:52:02 +02:00
2019-07-18 15:01:39 +02:00
getMessagesStorage().processPendingRead(dialogId, maxMessageId, minMessageId, isChannel);
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
2018-08-27 10:33:11 +02:00
if (dialog != null) {
int prevCount = dialog.unread_count;
if (countDiff == 0 || maxPositiveId >= dialog.top_message) {
dialog.unread_count = 0;
} else {
dialog.unread_count = Math.max(dialog.unread_count - countDiff, 0);
if (maxPositiveId != Integer.MIN_VALUE && dialog.unread_count > dialog.top_message - maxPositiveId) {
dialog.unread_count = dialog.top_message - maxPositiveId;
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
if (dialog.folder_id != 0) {
TLRPC.Dialog folder = dialogs_dict.get(DialogObject.makeFolderDialogId(dialog.folder_id));
if (folder != null) {
if (countMessages) {
if (isDialogMuted(dialog.id)) {
folder.unread_count -= (prevCount - dialog.unread_count);
} else {
folder.unread_mentions_count -= (prevCount - dialog.unread_count);
}
} else if (dialog.unread_count == 0) {
if (isDialogMuted(dialog.id)) {
folder.unread_count--;
} else {
folder.unread_mentions_count--;
}
}
}
}
2018-08-27 10:33:11 +02:00
if ((prevCount != 0 || dialog.unread_mark) && dialog.unread_count == 0 && !isDialogMuted(dialogId)) {
unreadUnmutedDialogs--;
}
if (dialog.unread_mark) {
dialog.unread_mark = false;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogUnread(dialog.id, false);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
if (!popup) {
2019-07-18 15:01:39 +02:00
getNotificationsController().processReadMessages(null, dialogId, 0, maxPositiveId, false);
2018-08-27 10:33:11 +02:00
LongSparseArray<Integer> dialogsToUpdate = new LongSparseArray<>(1);
dialogsToUpdate.put(dialogId, 0);
2019-07-18 15:01:39 +02:00
getNotificationsController().processDialogsUpdateRead(dialogsToUpdate);
2018-08-27 10:33:11 +02:00
} else {
2019-07-18 15:01:39 +02:00
getNotificationsController().processReadMessages(null, dialogId, 0, maxPositiveId, true);
2018-08-27 10:33:11 +02:00
LongSparseArray<Integer> dialogsToUpdate = new LongSparseArray<>(1);
dialogsToUpdate.put(dialogId, -1);
2019-07-18 15:01:39 +02:00
getNotificationsController().processDialogsUpdateRead(dialogsToUpdate);
2018-08-27 10:33:11 +02:00
}
}));
2015-11-26 22:04:02 +01:00
2018-07-30 04:07:02 +02:00
createReadTask = maxPositiveId != Integer.MAX_VALUE;
2013-10-25 17:19:00 +02:00
} else {
2018-07-30 04:07:02 +02:00
if (maxDate == 0) {
return;
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
createReadTask = true;
2018-07-30 04:07:02 +02:00
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
2019-07-18 15:01:39 +02:00
getMessagesStorage().processPendingRead(dialogId, maxPositiveId, maxNegativeId, false);
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> {
getNotificationsController().processReadMessages(null, dialogId, maxDate, 0, popup);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
2018-08-27 10:33:11 +02:00
if (dialog != null) {
int prevCount = dialog.unread_count;
if (countDiff == 0 || maxNegativeId <= dialog.top_message) {
dialog.unread_count = 0;
} else {
dialog.unread_count = Math.max(dialog.unread_count - countDiff, 0);
if (maxNegativeId != Integer.MAX_VALUE && dialog.unread_count > maxNegativeId - dialog.top_message) {
dialog.unread_count = maxNegativeId - dialog.top_message;
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
if (dialog.folder_id != 0) {
TLRPC.Dialog folder = dialogs_dict.get(DialogObject.makeFolderDialogId(dialog.folder_id));
if (folder != null) {
if (countMessages) {
if (isDialogMuted(dialog.id)) {
folder.unread_count -= (prevCount - dialog.unread_count);
} else {
folder.unread_mentions_count -= (prevCount - dialog.unread_count);
}
} else if (dialog.unread_count == 0) {
if (isDialogMuted(dialog.id)) {
folder.unread_count--;
} else {
folder.unread_mentions_count--;
}
}
}
}
2018-08-27 10:33:11 +02:00
if ((prevCount != 0 || dialog.unread_mark) && dialog.unread_count == 0 && !isDialogMuted(dialogId)) {
unreadUnmutedDialogs--;
}
if (dialog.unread_mark) {
dialog.unread_mark = false;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogUnread(dialog.id, false);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
LongSparseArray<Integer> dialogsToUpdate = new LongSparseArray<>(1);
dialogsToUpdate.put(dialogId, 0);
2019-07-18 15:01:39 +02:00
getNotificationsController().processDialogsUpdateRead(dialogsToUpdate);
2018-08-27 10:33:11 +02:00
}));
2018-07-30 04:07:02 +02:00
if (chat != null && chat.ttl > 0) {
2019-07-18 15:01:39 +02:00
int serverTime = Math.max(getConnectionsManager().getCurrentTime(), maxDate);
getMessagesStorage().createTaskForSecretChat(chat.id, serverTime, serverTime, 0, null);
2018-07-30 04:07:02 +02:00
}
}
if (createReadTask) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
ReadTask currentReadTask = readTasksMap.get(dialogId);
if (currentReadTask == null) {
currentReadTask = new ReadTask();
currentReadTask.dialogId = dialogId;
currentReadTask.sendRequestTime = SystemClock.elapsedRealtime() + 5000;
if (!readNow) {
readTasksMap.put(dialogId, currentReadTask);
readTasks.add(currentReadTask);
2018-07-30 04:07:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
currentReadTask.maxDate = maxDate;
currentReadTask.maxId = maxPositiveId;
if (readNow) {
completeReadTask(currentReadTask);
}
2018-07-30 04:07:02 +02:00
});
}
}
2019-07-18 15:01:39 +02:00
public int createChat(String title, ArrayList<Integer> selectedContacts, final String about, int type, Location location, String locationAddress, final BaseFragment fragment) {
2015-09-24 22:52:02 +02:00
if (type == ChatObject.CHAT_TYPE_BROADCAST) {
TLRPC.TL_chat chat = new TLRPC.TL_chat();
2019-07-18 15:01:39 +02:00
chat.id = getUserConfig().lastBroadcastId;
chat.title = title;
chat.photo = new TLRPC.TL_chatPhotoEmpty();
chat.participants_count = selectedContacts.size();
2015-05-21 23:27:27 +02:00
chat.date = (int) (System.currentTimeMillis() / 1000);
chat.version = 1;
2019-07-18 15:01:39 +02:00
getUserConfig().lastBroadcastId--;
putChat(chat, false);
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Chat> chatsArrays = new ArrayList<>();
chatsArrays.add(chat);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(null, chatsArrays, true, true);
2015-09-24 22:52:02 +02:00
TLRPC.TL_chatFull chatFull = new TLRPC.TL_chatFull();
chatFull.id = chat.id;
chatFull.chat_photo = new TLRPC.TL_photoEmpty();
2018-07-30 04:07:02 +02:00
chatFull.notify_settings = new TLRPC.TL_peerNotifySettingsEmpty_layer77();
2015-09-24 22:52:02 +02:00
chatFull.exported_invite = new TLRPC.TL_chatInviteEmpty();
chatFull.participants = new TLRPC.TL_chatParticipants();
chatFull.participants.chat_id = chat.id;
2019-07-18 15:01:39 +02:00
chatFull.participants.admin_id = getUserConfig().getClientUserId();
2015-09-24 22:52:02 +02:00
chatFull.participants.version = 1;
2016-03-06 02:49:31 +01:00
for (int a = 0; a < selectedContacts.size(); a++) {
TLRPC.TL_chatParticipant participant = new TLRPC.TL_chatParticipant();
2016-03-06 02:49:31 +01:00
participant.user_id = selectedContacts.get(a);
2019-07-18 15:01:39 +02:00
participant.inviter_id = getUserConfig().getClientUserId();
2015-05-21 23:27:27 +02:00
participant.date = (int) (System.currentTimeMillis() / 1000);
2015-09-24 22:52:02 +02:00
chatFull.participants.participants.add(participant);
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(chatFull, false);
TLRPC.TL_messageService newMsg = new TLRPC.TL_messageService();
newMsg.action = new TLRPC.TL_messageActionCreatedBroadcastList();
2019-07-18 15:01:39 +02:00
newMsg.local_id = newMsg.id = getUserConfig().getNewMessageId();
newMsg.from_id = getUserConfig().getClientUserId();
newMsg.dialog_id = AndroidUtilities.makeBroadcastId(chat.id);
newMsg.to_id = new TLRPC.TL_peerChat();
newMsg.to_id.chat_id = chat.id;
2019-07-18 15:01:39 +02:00
newMsg.date = getConnectionsManager().getCurrentTime();
newMsg.random_id = 0;
2015-09-24 22:52:02 +02:00
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
2019-07-18 15:01:39 +02:00
getUserConfig().saveConfig(false);
2018-07-30 04:07:02 +02:00
MessageObject newMsgObj = new MessageObject(currentAccount, newMsg, users, true);
newMsgObj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
2015-01-02 23:15:07 +01:00
ArrayList<MessageObject> objArr = new ArrayList<>();
objArr.add(newMsgObj);
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Message> arr = new ArrayList<>();
arr.add(newMsg);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(arr, false, true, false, 0);
updateInterfaceWithMessages(newMsg.dialog_id, objArr);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
getNotificationCenter().postNotificationName(NotificationCenter.chatDidCreated, chat.id);
return 0;
2015-09-24 22:52:02 +02:00
} else if (type == ChatObject.CHAT_TYPE_CHAT) {
2017-03-31 01:58:05 +02:00
final TLRPC.TL_messages_createChat req = new TLRPC.TL_messages_createChat();
req.title = title;
2016-03-06 02:49:31 +01:00
for (int a = 0; a < selectedContacts.size(); a++) {
TLRPC.User user = getUser(selectedContacts.get(a));
if (user == null) {
continue;
}
req.users.add(getInputUser(user));
}
2019-07-18 15:01:39 +02:00
return getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
AndroidUtilities.runOnUIThread(() -> {
AlertsCreator.processError(currentAccount, error, fragment, req);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatDidFailCreate);
2013-10-25 17:19:00 +02:00
});
2018-08-27 10:33:11 +02:00
return;
}
2018-08-27 10:33:11 +02:00
final TLRPC.Updates updates = (TLRPC.Updates) response;
processUpdates(updates, false);
AndroidUtilities.runOnUIThread(() -> {
putUsers(updates.users, false);
putChats(updates.chats, false);
if (updates.chats != null && !updates.chats.isEmpty()) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatDidCreated, updates.chats.get(0).id);
2018-08-27 10:33:11 +02:00
} else {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatDidFailCreate);
2018-08-27 10:33:11 +02:00
}
});
2016-03-16 13:26:32 +01:00
}, ConnectionsManager.RequestFlagFailOnServerErrors);
2015-11-26 22:04:02 +01:00
} else if (type == ChatObject.CHAT_TYPE_CHANNEL || type == ChatObject.CHAT_TYPE_MEGAGROUP) {
2017-03-31 01:58:05 +02:00
final TLRPC.TL_channels_createChannel req = new TLRPC.TL_channels_createChannel();
2015-09-24 22:52:02 +02:00
req.title = title;
2019-06-04 12:14:50 +02:00
req.about = about != null ? about : "";
2015-11-26 22:04:02 +01:00
if (type == ChatObject.CHAT_TYPE_MEGAGROUP) {
req.megagroup = true;
} else {
req.broadcast = true;
2015-09-24 22:52:02 +02:00
}
2019-07-18 15:01:39 +02:00
if (location != null) {
req.geo_point = new TLRPC.TL_inputGeoPoint();
req.geo_point.lat = location.getLatitude();
req.geo_point._long = location.getLongitude();
req.address = locationAddress;
req.flags |= 4;
}
return getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
AndroidUtilities.runOnUIThread(() -> {
AlertsCreator.processError(currentAccount, error, fragment, req);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatDidFailCreate);
2015-09-24 22:52:02 +02:00
});
2018-08-27 10:33:11 +02:00
return;
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
final TLRPC.Updates updates = (TLRPC.Updates) response;
processUpdates(updates, false);
AndroidUtilities.runOnUIThread(() -> {
putUsers(updates.users, false);
putChats(updates.chats, false);
if (updates.chats != null && !updates.chats.isEmpty()) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatDidCreated, updates.chats.get(0).id);
2018-08-27 10:33:11 +02:00
} else {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatDidFailCreate);
2018-08-27 10:33:11 +02:00
}
});
2016-03-16 13:26:32 +01:00
}, ConnectionsManager.RequestFlagFailOnServerErrors);
2015-09-24 22:52:02 +02:00
}
return 0;
}
2019-01-23 18:03:33 +01:00
public void convertToMegaGroup(final Context context, int chat_id, MessagesStorage.IntCallback convertRunnable) {
2015-11-26 22:04:02 +01:00
TLRPC.TL_messages_migrateChat req = new TLRPC.TL_messages_migrateChat();
req.chat_id = chat_id;
2019-01-23 18:03:33 +01:00
final AlertDialog progressDialog = new AlertDialog(context, 3);
2019-07-18 15:01:39 +02:00
final int reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
AndroidUtilities.runOnUIThread(() -> {
if (!((Activity) context).isFinishing()) {
try {
progressDialog.dismiss();
} catch (Exception e) {
FileLog.e(e);
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
}
});
TLRPC.Updates updates = (TLRPC.Updates) response;
processUpdates((TLRPC.Updates) response, false);
2019-01-23 18:03:33 +01:00
AndroidUtilities.runOnUIThread(() -> {
if (convertRunnable != null) {
for (int a = 0; a < updates.chats.size(); a++) {
TLRPC.Chat chat = updates.chats.get(a);
if (ChatObject.isChannel(chat)) {
convertRunnable.run(chat.id);
break;
}
}
}
});
2018-08-27 10:33:11 +02:00
} else {
AndroidUtilities.runOnUIThread(() -> {
if (!((Activity) context).isFinishing()) {
try {
progressDialog.dismiss();
} catch (Exception e) {
FileLog.e(e);
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
builder.setMessage(LocaleController.getString("ErrorOccurred", R.string.ErrorOccurred));
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
builder.show().setCanceledOnTouchOutside(true);
}
});
2015-11-26 22:04:02 +01:00
}
});
2019-07-18 15:01:39 +02:00
progressDialog.setOnCancelListener(dialog -> getConnectionsManager().cancelRequest(reqId, true));
2015-11-26 22:04:02 +01:00
try {
progressDialog.show();
} catch (Exception e) {
//don't promt
}
}
public void addUsersToChannel(int chat_id, ArrayList<TLRPC.InputUser> users, final BaseFragment fragment) {
2015-09-24 22:52:02 +02:00
if (users == null || users.isEmpty()) {
return;
}
2017-03-31 01:58:05 +02:00
final TLRPC.TL_channels_inviteToChannel req = new TLRPC.TL_channels_inviteToChannel();
2015-09-24 22:52:02 +02:00
req.channel = getInputChannel(chat_id);
req.users = users;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
AndroidUtilities.runOnUIThread(() -> AlertsCreator.processError(currentAccount, error, fragment, req, true));
return;
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
processUpdates((TLRPC.Updates) response, false);
2015-09-24 22:52:02 +02:00
});
}
2017-12-08 18:35:59 +01:00
public void toogleChannelSignatures(int chat_id, boolean enabled) {
TLRPC.TL_channels_toggleSignatures req = new TLRPC.TL_channels_toggleSignatures();
req.channel = getInputChannel(chat_id);
req.enabled = enabled;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
processUpdates((TLRPC.Updates) response, false);
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT));
2017-12-08 18:35:59 +01:00
}
}, ConnectionsManager.RequestFlagInvokeAfter);
}
public void toogleChannelInvitesHistory(int chat_id, boolean enabled) {
TLRPC.TL_channels_togglePreHistoryHidden req = new TLRPC.TL_channels_togglePreHistoryHidden();
2016-03-06 02:49:31 +01:00
req.channel = getInputChannel(chat_id);
req.enabled = enabled;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
processUpdates((TLRPC.Updates) response, false);
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT));
2016-03-06 02:49:31 +01:00
}
}, ConnectionsManager.RequestFlagInvokeAfter);
}
2019-01-23 18:03:33 +01:00
public void updateChatAbout(int chat_id, final String about, final TLRPC.ChatFull info) {
2015-09-24 22:52:02 +02:00
if (info == null) {
return;
}
2019-01-23 18:03:33 +01:00
TLRPC.TL_messages_editChatAbout req = new TLRPC.TL_messages_editChatAbout();
req.peer = getInputPeer(-chat_id);
2015-09-24 22:52:02 +02:00
req.about = about;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response instanceof TLRPC.TL_boolTrue) {
AndroidUtilities.runOnUIThread(() -> {
info.about = about;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(info, false);
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoDidLoad, info, 0, false, null);
2018-08-27 10:33:11 +02:00
});
2015-09-24 22:52:02 +02:00
}
}, ConnectionsManager.RequestFlagInvokeAfter);
}
public void updateChannelUserName(final int chat_id, final String userName) {
TLRPC.TL_channels_updateUsername req = new TLRPC.TL_channels_updateUsername();
req.channel = getInputChannel(chat_id);
req.username = userName;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response instanceof TLRPC.TL_boolTrue) {
AndroidUtilities.runOnUIThread(() -> {
TLRPC.Chat chat = getChat(chat_id);
if (userName.length() != 0) {
chat.flags |= TLRPC.CHAT_FLAG_IS_PUBLIC;
} else {
chat.flags &= ~TLRPC.CHAT_FLAG_IS_PUBLIC;
}
chat.username = userName;
ArrayList<TLRPC.Chat> arrayList = new ArrayList<>();
arrayList.add(chat);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(null, arrayList, true, true);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT);
2018-08-27 10:33:11 +02:00
});
2015-09-24 22:52:02 +02:00
}
}, ConnectionsManager.RequestFlagInvokeAfter);
2013-10-25 17:19:00 +02:00
}
2015-06-29 19:12:11 +02:00
public void sendBotStart(final TLRPC.User user, String botHash) {
2015-11-26 22:04:02 +01:00
if (user == null) {
return;
}
2015-06-29 19:12:11 +02:00
TLRPC.TL_messages_startBot req = new TLRPC.TL_messages_startBot();
req.bot = getInputUser(user);
2018-08-27 10:33:11 +02:00
req.peer = getInputPeer(user.id);
req.start_param = botHash;
req.random_id = Utilities.random.nextLong();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
return;
2015-06-29 19:12:11 +02:00
}
2018-08-27 10:33:11 +02:00
processUpdates((TLRPC.Updates) response, false);
2015-06-29 19:12:11 +02:00
});
}
2019-01-23 18:03:33 +01:00
public boolean isJoiningChannel(final int chat_id) {
return joiningToChannels.contains(chat_id);
2015-11-26 22:04:02 +01:00
}
2019-01-23 18:03:33 +01:00
public void addUserToChat(final int chat_id, final TLRPC.User user, final TLRPC.ChatFull info, int count_fwd, String botHash, final BaseFragment fragment, final Runnable onFinishRunnable) {
if (user == null) {
return;
}
if (chat_id > 0) {
2017-03-31 01:58:05 +02:00
final TLObject request;
2015-06-29 19:12:11 +02:00
2018-07-30 04:07:02 +02:00
final boolean isChannel = ChatObject.isChannel(chat_id, currentAccount);
2016-03-16 13:26:32 +01:00
final boolean isMegagroup = isChannel && getChat(chat_id).megagroup;
2015-09-24 22:52:02 +02:00
final TLRPC.InputUser inputUser = getInputUser(user);
2015-11-26 22:04:02 +01:00
if (botHash == null || isChannel && !isMegagroup) {
2015-09-24 22:52:02 +02:00
if (isChannel) {
if (inputUser instanceof TLRPC.TL_inputUserSelf) {
2015-10-29 18:10:07 +01:00
if (joiningToChannels.contains(chat_id)) {
return;
}
2015-09-24 22:52:02 +02:00
TLRPC.TL_channels_joinChannel req = new TLRPC.TL_channels_joinChannel();
req.channel = getInputChannel(chat_id);
request = req;
2015-10-29 18:10:07 +01:00
joiningToChannels.add(chat_id);
2015-09-24 22:52:02 +02:00
} else {
2017-12-08 18:35:59 +01:00
TLRPC.TL_channels_inviteToChannel req = new TLRPC.TL_channels_inviteToChannel();
req.channel = getInputChannel(chat_id);
req.users.add(inputUser);
request = req;
2015-09-24 22:52:02 +02:00
}
} else {
TLRPC.TL_messages_addChatUser req = new TLRPC.TL_messages_addChatUser();
req.chat_id = chat_id;
req.fwd_limit = count_fwd;
req.user_id = inputUser;
request = req;
}
2015-06-29 19:12:11 +02:00
} else {
TLRPC.TL_messages_startBot req = new TLRPC.TL_messages_startBot();
2015-09-24 22:52:02 +02:00
req.bot = inputUser;
2015-11-26 22:04:02 +01:00
if (isChannel) {
req.peer = getInputPeer(-chat_id);
} else {
req.peer = new TLRPC.TL_inputPeerChat();
req.peer.chat_id = chat_id;
}
2015-06-29 19:12:11 +02:00
req.start_param = botHash;
req.random_id = Utilities.random.nextLong();
request = req;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(request, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (isChannel && inputUser instanceof TLRPC.TL_inputUserSelf) {
AndroidUtilities.runOnUIThread(() -> joiningToChannels.remove((Integer) chat_id));
}
if (error != null) {
2019-06-04 12:14:50 +02:00
AndroidUtilities.runOnUIThread(() -> {
AlertsCreator.processError(currentAccount, error, fragment, request, isChannel && !isMegagroup);
if (isChannel && inputUser instanceof TLRPC.TL_inputUserSelf) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT);
2019-06-04 12:14:50 +02:00
}
});
2018-08-27 10:33:11 +02:00
return;
}
boolean hasJoinMessage = false;
TLRPC.Updates updates = (TLRPC.Updates) response;
for (int a = 0; a < updates.updates.size(); a++) {
TLRPC.Update update = updates.updates.get(a);
if (update instanceof TLRPC.TL_updateNewChannelMessage) {
if (((TLRPC.TL_updateNewChannelMessage) update).message.action instanceof TLRPC.TL_messageActionChatAddUser) {
hasJoinMessage = true;
break;
2015-09-24 22:52:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
processUpdates(updates, false);
if (isChannel) {
if (!hasJoinMessage && inputUser instanceof TLRPC.TL_inputUserSelf) {
generateJoinMessage(chat_id, true);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> loadFullChat(chat_id, 0, true), 1000);
}
if (isChannel && inputUser instanceof TLRPC.TL_inputUserSelf) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateDialogsWithDeletedMessages(new ArrayList<>(), null, true, chat_id);
}
2019-01-23 18:03:33 +01:00
if (onFinishRunnable != null) {
AndroidUtilities.runOnUIThread(onFinishRunnable);
}
});
} else {
2015-09-24 22:52:02 +02:00
if (info instanceof TLRPC.TL_chatFull) {
2016-03-06 02:49:31 +01:00
for (int a = 0; a < info.participants.participants.size(); a++) {
if (info.participants.participants.get(a).user_id == user.id) {
return;
}
}
TLRPC.Chat chat = getChat(chat_id);
chat.participants_count++;
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Chat> chatArrayList = new ArrayList<>();
chatArrayList.add(chat);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(null, chatArrayList, true, true);
TLRPC.TL_chatParticipant newPart = new TLRPC.TL_chatParticipant();
newPart.user_id = user.id;
2019-07-18 15:01:39 +02:00
newPart.inviter_id = getUserConfig().getClientUserId();
newPart.date = getConnectionsManager().getCurrentTime();
2015-09-24 22:52:02 +02:00
info.participants.participants.add(0, newPart);
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(info, true);
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoDidLoad, info, 0, false, null);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
2013-10-25 17:19:00 +02:00
}
}
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
public void deleteUserFromChat(final int chat_id, final TLRPC.User user, final TLRPC.ChatFull info) {
2019-05-14 14:08:05 +02:00
deleteUserFromChat(chat_id, user, info, false, false);
2017-12-08 18:35:59 +01:00
}
2019-05-14 14:08:05 +02:00
public void deleteUserFromChat(final int chat_id, final TLRPC.User user, final TLRPC.ChatFull info, boolean forceDelete, boolean revoke) {
if (user == null) {
return;
}
if (chat_id > 0) {
2015-09-24 22:52:02 +02:00
final TLRPC.InputUser inputUser = getInputUser(user);
TLObject request;
TLRPC.Chat chat = getChat(chat_id);
final boolean isChannel = ChatObject.isChannel(chat);
if (isChannel) {
if (inputUser instanceof TLRPC.TL_inputUserSelf) {
2017-12-08 18:35:59 +01:00
if (chat.creator && forceDelete) {
2015-09-24 22:52:02 +02:00
TLRPC.TL_channels_deleteChannel req = new TLRPC.TL_channels_deleteChannel();
req.channel = getInputChannel(chat);
request = req;
} else {
TLRPC.TL_channels_leaveChannel req = new TLRPC.TL_channels_leaveChannel();
req.channel = getInputChannel(chat);
request = req;
}
} else {
2017-07-08 18:32:04 +02:00
TLRPC.TL_channels_editBanned req = new TLRPC.TL_channels_editBanned();
2015-09-24 22:52:02 +02:00
req.channel = getInputChannel(chat);
req.user_id = inputUser;
2019-01-23 18:03:33 +01:00
req.banned_rights = new TLRPC.TL_chatBannedRights();
2017-07-08 18:32:04 +02:00
req.banned_rights.view_messages = true;
req.banned_rights.send_media = true;
req.banned_rights.send_messages = true;
req.banned_rights.send_stickers = true;
req.banned_rights.send_gifs = true;
req.banned_rights.send_games = true;
req.banned_rights.send_inline = true;
req.banned_rights.embed_links = true;
2019-01-23 18:03:33 +01:00
req.banned_rights.pin_messages = true;
req.banned_rights.send_polls = true;
req.banned_rights.invite_users = true;
req.banned_rights.change_info = true;
2015-09-24 22:52:02 +02:00
request = req;
}
} else {
TLRPC.TL_messages_deleteChatUser req = new TLRPC.TL_messages_deleteChatUser();
req.chat_id = chat_id;
req.user_id = getInputUser(user);
request = req;
}
2019-07-18 15:01:39 +02:00
if (user.id == getUserConfig().getClientUserId()) {
2019-05-14 14:08:05 +02:00
deleteDialog(-chat_id, 0, revoke);
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(request, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
return;
}
final TLRPC.Updates updates = (TLRPC.Updates) response;
processUpdates(updates, false);
if (isChannel && !(inputUser instanceof TLRPC.TL_inputUserSelf)) {
AndroidUtilities.runOnUIThread(() -> loadFullChat(chat_id, 0, true), 1000);
}
2015-11-26 22:04:02 +01:00
}, ConnectionsManager.RequestFlagInvokeAfter);
} else {
2015-09-24 22:52:02 +02:00
if (info instanceof TLRPC.TL_chatFull) {
TLRPC.Chat chat = getChat(chat_id);
chat.participants_count--;
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Chat> chatArrayList = new ArrayList<>();
chatArrayList.add(chat);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(null, chatArrayList, true, true);
boolean changed = false;
2015-09-24 22:52:02 +02:00
for (int a = 0; a < info.participants.participants.size(); a++) {
2015-11-26 22:04:02 +01:00
TLRPC.ChatParticipant p = info.participants.participants.get(a);
2015-05-21 23:27:27 +02:00
if (p.user_id == user.id) {
2015-09-24 22:52:02 +02:00
info.participants.participants.remove(a);
2015-05-21 23:27:27 +02:00
changed = true;
break;
}
}
2015-05-21 23:27:27 +02:00
if (changed) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(info, true);
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoDidLoad, info, 0, false, null);
2015-05-21 23:27:27 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
2013-10-25 17:19:00 +02:00
}
}
2013-10-25 17:19:00 +02:00
}
public void changeChatTitle(int chat_id, String title) {
if (chat_id > 0) {
2015-09-24 22:52:02 +02:00
TLObject request;
2018-07-30 04:07:02 +02:00
if (ChatObject.isChannel(chat_id, currentAccount)) {
2015-09-24 22:52:02 +02:00
TLRPC.TL_channels_editTitle req = new TLRPC.TL_channels_editTitle();
req.channel = getInputChannel(chat_id);
req.title = title;
request = req;
} else {
TLRPC.TL_messages_editChatTitle req = new TLRPC.TL_messages_editChatTitle();
req.chat_id = chat_id;
req.title = title;
request = req;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(request, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
return;
}
2018-08-27 10:33:11 +02:00
processUpdates((TLRPC.Updates) response, false);
2015-09-24 22:52:02 +02:00
}, ConnectionsManager.RequestFlagInvokeAfter);
} else {
TLRPC.Chat chat = getChat(chat_id);
chat.title = title;
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Chat> chatArrayList = new ArrayList<>();
chatArrayList.add(chat);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(null, chatArrayList, true, true);
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_NAME);
}
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
public void changeChatAvatar(int chat_id, TLRPC.InputFile uploadedAvatar, TLRPC.FileLocation smallSize, TLRPC.FileLocation bigSize) {
2015-09-24 22:52:02 +02:00
TLObject request;
2018-07-30 04:07:02 +02:00
if (ChatObject.isChannel(chat_id, currentAccount)) {
2015-09-24 22:52:02 +02:00
TLRPC.TL_channels_editPhoto req = new TLRPC.TL_channels_editPhoto();
req.channel = getInputChannel(chat_id);
if (uploadedAvatar != null) {
req.photo = new TLRPC.TL_inputChatUploadedPhoto();
req.photo.file = uploadedAvatar;
} else {
req.photo = new TLRPC.TL_inputChatPhotoEmpty();
}
request = req;
2013-10-25 17:19:00 +02:00
} else {
2015-09-24 22:52:02 +02:00
TLRPC.TL_messages_editChatPhoto req = new TLRPC.TL_messages_editChatPhoto();
req.chat_id = chat_id;
if (uploadedAvatar != null) {
req.photo = new TLRPC.TL_inputChatUploadedPhoto();
req.photo.file = uploadedAvatar;
} else {
req.photo = new TLRPC.TL_inputChatPhotoEmpty();
}
request = req;
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(request, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error != null) {
return;
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
TLRPC.Updates updates = (TLRPC.Updates) response;
TLRPC.Photo photo = null;
for (int a = 0, N = updates.updates.size(); a < N; a++) {
TLRPC.Update update = updates.updates.get(a);
if (update instanceof TLRPC.TL_updateNewChannelMessage) {
TLRPC.Message message = ((TLRPC.TL_updateNewChannelMessage) update).message;
if (message.action instanceof TLRPC.TL_messageActionChatEditPhoto && message.action.photo instanceof TLRPC.TL_photo) {
photo = message.action.photo;
break;
}
} else if (update instanceof TLRPC.TL_updateNewMessage) {
TLRPC.Message message = ((TLRPC.TL_updateNewMessage) update).message;
if (message.action instanceof TLRPC.TL_messageActionChatEditPhoto && message.action.photo instanceof TLRPC.TL_photo) {
photo = message.action.photo;
break;
}
}
}
if (photo != null) {
TLRPC.PhotoSize small = FileLoader.getClosestPhotoSizeWithSize(photo.sizes, 150);
if (small != null && smallSize != null) {
File destFile = FileLoader.getPathToAttach(small, true);
File src = FileLoader.getPathToAttach(smallSize, true);
src.renameTo(destFile);
String oldKey = smallSize.volume_id + "_" + smallSize.local_id + "@50_50";
String newKey = small.location.volume_id + "_" + small.location.local_id + "@50_50";
2019-05-14 14:08:05 +02:00
ImageLoader.getInstance().replaceImageInCache(oldKey, newKey, ImageLocation.getForPhoto(small, photo), true);
2019-01-23 18:03:33 +01:00
}
TLRPC.PhotoSize big = FileLoader.getClosestPhotoSizeWithSize(photo.sizes, 800);
if (big != null && bigSize != null) {
File destFile = FileLoader.getPathToAttach(big, true);
File src = FileLoader.getPathToAttach(bigSize, true);
src.renameTo(destFile);
}
}
processUpdates(updates, false);
2015-09-24 22:52:02 +02:00
}, ConnectionsManager.RequestFlagInvokeAfter);
2013-10-25 17:19:00 +02:00
}
public void unregistedPush() {
2019-07-18 15:01:39 +02:00
if (getUserConfig().registeredForPush && SharedConfig.pushString.length() == 0) {
TLRPC.TL_account_unregisterDevice req = new TLRPC.TL_account_unregisterDevice();
2018-07-30 04:07:02 +02:00
req.token = SharedConfig.pushString;
req.token_type = 2;
2018-07-30 04:07:02 +02:00
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
UserConfig userConfig = UserConfig.getInstance(a);
if (a != currentAccount && userConfig.isClientActivated()) {
req.other_uids.add(userConfig.getClientUserId());
}
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
});
2013-10-25 17:19:00 +02:00
}
2014-06-13 12:42:21 +02:00
}
2018-07-30 04:07:02 +02:00
public void performLogout(int type) {
if (type == 1) {
2015-05-21 23:27:27 +02:00
unregistedPush();
TLRPC.TL_auth_logOut req = new TLRPC.TL_auth_logOut();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> getConnectionsManager().cleanup(false));
2015-05-21 23:27:27 +02:00
} else {
2019-07-18 15:01:39 +02:00
getConnectionsManager().cleanup(type == 2);
2015-05-21 23:27:27 +02:00
}
2019-07-18 15:01:39 +02:00
getUserConfig().clearConfig();
getNotificationCenter().postNotificationName(NotificationCenter.appDidLogout);
getMessagesStorage().cleanup(false);
cleanup();
2019-07-18 15:01:39 +02:00
getContactsController().deleteUnknownAppAccounts();
2013-10-25 17:19:00 +02:00
}
public void generateUpdateMessage() {
2018-07-30 04:07:02 +02:00
if (BuildVars.DEBUG_VERSION || SharedConfig.lastUpdateVersion == null || SharedConfig.lastUpdateVersion.equals(BuildVars.BUILD_VERSION_STRING)) {
2016-03-06 02:49:31 +01:00
return;
}
TLRPC.TL_help_getAppChangelog req = new TLRPC.TL_help_getAppChangelog();
2018-07-30 04:07:02 +02:00
req.prev_app_version = SharedConfig.lastUpdateVersion;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
SharedConfig.lastUpdateVersion = BuildVars.BUILD_VERSION_STRING;
SharedConfig.saveConfig();
}
if (response instanceof TLRPC.Updates) {
processUpdates((TLRPC.Updates) response, false);
}
});
}
2013-10-25 17:19:00 +02:00
public void registerForPush(final String regid) {
2019-07-18 15:01:39 +02:00
if (TextUtils.isEmpty(regid) || registeringForPush || getUserConfig().getClientUserId() == 0) {
2013-10-25 17:19:00 +02:00
return;
}
2019-07-18 15:01:39 +02:00
if (getUserConfig().registeredForPush && regid.equals(SharedConfig.pushString)) {
2013-10-25 17:19:00 +02:00
return;
}
registeringForPush = true;
2018-07-30 04:07:02 +02:00
lastPushRegisterSendTime = SystemClock.elapsedRealtime();
if (SharedConfig.pushAuthKey == null) {
SharedConfig.pushAuthKey = new byte[256];
Utilities.random.nextBytes(SharedConfig.pushAuthKey);
SharedConfig.saveConfig();
}
2013-10-25 17:19:00 +02:00
TLRPC.TL_account_registerDevice req = new TLRPC.TL_account_registerDevice();
2013-12-20 20:25:49 +01:00
req.token_type = 2;
req.token = regid;
2019-07-18 15:01:39 +02:00
req.no_muted = false;
2018-07-30 04:07:02 +02:00
req.secret = SharedConfig.pushAuthKey;
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
UserConfig userConfig = UserConfig.getInstance(a);
if (a != currentAccount && userConfig.isClientActivated()) {
2018-08-27 10:33:11 +02:00
int uid = userConfig.getClientUserId();
req.other_uids.add(uid);
if (BuildVars.LOGS_ENABLED) {
FileLog.d("add other uid = " + uid + " for account " + currentAccount);
}
2018-07-30 04:07:02 +02:00
}
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response instanceof TLRPC.TL_boolTrue) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("account " + currentAccount + " registered for push");
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getUserConfig().registeredForPush = true;
2018-08-27 10:33:11 +02:00
SharedConfig.pushString = regid;
2019-07-18 15:01:39 +02:00
getUserConfig().saveConfig(false);
2016-04-22 15:49:00 +02:00
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> registeringForPush = false);
2016-04-22 15:49:00 +02:00
});
2013-10-25 17:19:00 +02:00
}
public void loadCurrentState() {
if (updatingState) {
return;
}
updatingState = true;
TLRPC.TL_updates_getState req = new TLRPC.TL_updates_getState();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
updatingState = false;
if (error == null) {
TLRPC.TL_updates_state res = (TLRPC.TL_updates_state) response;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastDateValue(res.date);
getMessagesStorage().setLastPtsValue(res.pts);
getMessagesStorage().setLastSeqValue(res.seq);
getMessagesStorage().setLastQtsValue(res.qts);
2018-08-27 10:33:11 +02:00
for (int a = 0; a < 3; a++) {
processUpdatesQueue(a, 2);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveDiffParams(getMessagesStorage().getLastSeqValue(), getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue());
2018-08-27 10:33:11 +02:00
} else {
if (error.code != 401) {
loadCurrentState();
2013-10-25 17:19:00 +02:00
}
}
});
2013-10-25 17:19:00 +02:00
}
private int getUpdateSeq(TLRPC.Updates updates) {
if (updates instanceof TLRPC.TL_updatesCombined) {
return updates.seq_start;
} else {
return updates.seq;
}
}
private void setUpdatesStartTime(int type, long time) {
if (type == 0) {
updatesStartWaitTimeSeq = time;
} else if (type == 1) {
updatesStartWaitTimePts = time;
} else if (type == 2) {
updatesStartWaitTimeQts = time;
}
}
public long getUpdatesStartTime(int type) {
if (type == 0) {
return updatesStartWaitTimeSeq;
} else if (type == 1) {
return updatesStartWaitTimePts;
} else if (type == 2) {
return updatesStartWaitTimeQts;
}
return 0;
}
private int isValidUpdate(TLRPC.Updates updates, int type) {
if (type == 0) {
int seq = getUpdateSeq(updates);
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastSeqValue() + 1 == seq || getMessagesStorage().getLastSeqValue() == seq) {
return 0;
2019-07-18 15:01:39 +02:00
} else if (getMessagesStorage().getLastSeqValue() < seq) {
return 1;
} else {
return 2;
}
} else if (type == 1) {
2019-07-18 15:01:39 +02:00
if (updates.pts <= getMessagesStorage().getLastPtsValue()) {
return 2;
2019-07-18 15:01:39 +02:00
} else if (getMessagesStorage().getLastPtsValue() + updates.pts_count == updates.pts) {
return 0;
} else {
return 1;
}
} else if (type == 2) {
2019-07-18 15:01:39 +02:00
if (updates.pts <= getMessagesStorage().getLastQtsValue()) {
return 2;
2019-07-18 15:01:39 +02:00
} else if (getMessagesStorage().getLastQtsValue() + updates.updates.size() == updates.pts) {
return 0;
} else {
return 1;
}
}
return 0;
}
2015-09-24 22:52:02 +02:00
private void processChannelsUpdatesQueue(int channelId, int state) {
ArrayList<TLRPC.Updates> updatesQueue = updatesQueueChannels.get(channelId);
if (updatesQueue == null) {
return;
}
2018-07-30 04:07:02 +02:00
int channelPts = channelsPts.get(channelId);
if (updatesQueue.isEmpty() || channelPts == 0) {
2015-09-24 22:52:02 +02:00
updatesQueueChannels.remove(channelId);
2015-10-29 18:10:07 +01:00
return;
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
Collections.sort(updatesQueue, (updates, updates2) -> AndroidUtilities.compare(updates.pts, updates2.pts));
2015-09-24 22:52:02 +02:00
boolean anyProceed = false;
if (state == 2) {
channelsPts.put(channelId, updatesQueue.get(0).pts);
}
for (int a = 0; a < updatesQueue.size(); a++) {
TLRPC.Updates updates = updatesQueue.get(a);
int updateState;
if (updates.pts <= channelPts) {
updateState = 2;
} else if (channelPts + updates.pts_count == updates.pts) {
updateState = 0;
} else {
updateState = 1;
}
if (updateState == 0) {
processUpdates(updates, true);
anyProceed = true;
updatesQueue.remove(a);
a--;
} else if (updateState == 1) {
2018-07-30 04:07:02 +02:00
long updatesStartWaitTime = updatesStartWaitTimeChannels.get(channelId);
if (updatesStartWaitTime != 0 && (anyProceed || Math.abs(System.currentTimeMillis() - updatesStartWaitTime) <= 1500)) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("HOLE IN CHANNEL " + channelId + " UPDATES QUEUE - will wait more time");
}
2015-09-24 22:52:02 +02:00
if (anyProceed) {
updatesStartWaitTimeChannels.put(channelId, System.currentTimeMillis());
}
return;
} else {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("HOLE IN CHANNEL " + channelId + " UPDATES QUEUE - getChannelDifference ");
}
updatesStartWaitTimeChannels.delete(channelId);
2015-09-24 22:52:02 +02:00
updatesQueueChannels.remove(channelId);
getChannelDifference(channelId);
return;
}
} else {
updatesQueue.remove(a);
a--;
}
}
updatesQueueChannels.remove(channelId);
2018-07-30 04:07:02 +02:00
updatesStartWaitTimeChannels.delete(channelId);
if (BuildVars.LOGS_ENABLED) {
FileLog.d("UPDATES CHANNEL " + channelId + " QUEUE PROCEED - OK");
}
2015-09-24 22:52:02 +02:00
}
private void processUpdatesQueue(int type, int state) {
ArrayList<TLRPC.Updates> updatesQueue = null;
if (type == 0) {
updatesQueue = updatesQueueSeq;
2018-08-27 10:33:11 +02:00
Collections.sort(updatesQueue, (updates, updates2) -> AndroidUtilities.compare(getUpdateSeq(updates), getUpdateSeq(updates2)));
} else if (type == 1) {
updatesQueue = updatesQueuePts;
2018-08-27 10:33:11 +02:00
Collections.sort(updatesQueue, (updates, updates2) -> AndroidUtilities.compare(updates.pts, updates2.pts));
} else if (type == 2) {
updatesQueue = updatesQueueQts;
2018-08-27 10:33:11 +02:00
Collections.sort(updatesQueue, (updates, updates2) -> AndroidUtilities.compare(updates.pts, updates2.pts));
}
2015-05-21 23:27:27 +02:00
if (updatesQueue != null && !updatesQueue.isEmpty()) {
boolean anyProceed = false;
2014-07-23 01:27:00 +02:00
if (state == 2) {
TLRPC.Updates updates = updatesQueue.get(0);
if (type == 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastSeqValue(getUpdateSeq(updates));
} else if (type == 1) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastPtsValue(updates.pts);
2015-05-21 23:27:27 +02:00
} else {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastQtsValue(updates.pts);
}
2014-07-23 01:27:00 +02:00
}
for (int a = 0; a < updatesQueue.size(); a++) {
TLRPC.Updates updates = updatesQueue.get(a);
int updateState = isValidUpdate(updates, type);
if (updateState == 0) {
processUpdates(updates, true);
anyProceed = true;
updatesQueue.remove(a);
a--;
} else if (updateState == 1) {
2015-11-26 22:04:02 +01:00
if (getUpdatesStartTime(type) != 0 && (anyProceed || Math.abs(System.currentTimeMillis() - getUpdatesStartTime(type)) <= 1500)) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("HOLE IN UPDATES QUEUE - will wait more time");
}
if (anyProceed) {
setUpdatesStartTime(type, System.currentTimeMillis());
}
2015-09-24 22:52:02 +02:00
return;
} else {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("HOLE IN UPDATES QUEUE - getDifference");
}
2015-09-24 22:52:02 +02:00
setUpdatesStartTime(type, 0);
updatesQueue.clear();
getDifference();
return;
}
} else {
updatesQueue.remove(a);
a--;
}
}
updatesQueue.clear();
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("UPDATES QUEUE PROCEED - OK");
}
2015-09-24 22:52:02 +02:00
}
setUpdatesStartTime(type, 0);
}
protected void loadUnknownChannel(final TLRPC.Chat channel, final long taskId) {
2018-07-30 04:07:02 +02:00
if (!(channel instanceof TLRPC.TL_channel) || gettingUnknownChannels.indexOfKey(channel.id) >= 0) {
return;
}
2017-03-31 01:58:05 +02:00
if (channel.access_hash == 0) {
if (taskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(taskId);
2017-03-31 01:58:05 +02:00
}
return;
}
TLRPC.TL_inputPeerChannel inputPeer = new TLRPC.TL_inputPeerChannel();
inputPeer.channel_id = channel.id;
inputPeer.access_hash = channel.access_hash;
2017-03-31 01:58:05 +02:00
gettingUnknownChannels.put(channel.id, true);
TLRPC.TL_messages_getPeerDialogs req = new TLRPC.TL_messages_getPeerDialogs();
2018-07-30 04:07:02 +02:00
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = inputPeer;
req.peers.add(inputDialogPeer);
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(4 + channel.getObjectSize());
data.writeInt32(0);
channel.serializeToStream(data);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
} else {
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
TLRPC.TL_messages_peerDialogs res = (TLRPC.TL_messages_peerDialogs) response;
if (!res.dialogs.isEmpty() && !res.chats.isEmpty()) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_dialog dialog = (TLRPC.TL_dialog) res.dialogs.get(0);
2018-08-27 10:33:11 +02:00
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
dialogs.dialogs.addAll(res.dialogs);
dialogs.messages.addAll(res.messages);
dialogs.users.addAll(res.users);
dialogs.chats.addAll(res.chats);
2019-05-14 14:08:05 +02:00
processLoadedDialogs(dialogs, null, dialog.folder_id, 0, 1, DIALOGS_LOAD_TYPE_CHANNEL, false, false, false);
}
}
2018-08-27 10:33:11 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2018-08-27 10:33:11 +02:00
}
gettingUnknownChannels.delete(channel.id);
});
}
2019-01-23 18:03:33 +01:00
public void startShortPoll(TLRPC.Chat chat, final boolean stop) {
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
if (stop) {
2019-01-23 18:03:33 +01:00
needShortPollChannels.delete(chat.id);
if (chat.megagroup) {
needShortPollOnlines.delete(chat.id);
}
2018-08-27 10:33:11 +02:00
} else {
2019-01-23 18:03:33 +01:00
needShortPollChannels.put(chat.id, 0);
if (shortPollChannels.indexOfKey(chat.id) < 0) {
getChannelDifference(chat.id, 3, 0, null);
}
if (chat.megagroup) {
needShortPollOnlines.put(chat.id, 0);
if (shortPollOnlines.indexOfKey(chat.id) < 0) {
shortPollOnlines.put(chat.id, 0);
}
2015-09-24 22:52:02 +02:00
}
}
});
}
private void getChannelDifference(final int channelId) {
2017-03-31 01:58:05 +02:00
getChannelDifference(channelId, 0, 0, null);
2015-09-24 22:52:02 +02:00
}
2019-03-03 21:40:48 +01:00
public static boolean isSupportUser(TLRPC.User user) {
2019-05-14 14:08:05 +02:00
return user != null && (user.support || user.id == 777000 ||
user.id == 333000 || user.id == 4240000 || user.id == 4244000 ||
2019-03-03 21:40:48 +01:00
user.id == 4245000 || user.id == 4246000 || user.id == 410000 ||
user.id == 420000 || user.id == 431000 || user.id == 431415000 ||
user.id == 434000 || user.id == 4243000 || user.id == 439000 ||
user.id == 449000 || user.id == 450000 || user.id == 452000 ||
user.id == 454000 || user.id == 4254000 || user.id == 455000 ||
user.id == 460000 || user.id == 470000 || user.id == 479000 ||
user.id == 796000 || user.id == 482000 || user.id == 490000 ||
user.id == 496000 || user.id == 497000 || user.id == 498000 ||
user.id == 4298000);
2017-12-08 18:35:59 +01:00
}
2017-03-31 01:58:05 +02:00
protected void getChannelDifference(final int channelId, final int newDialogType, final long taskId, TLRPC.InputChannel inputChannel) {
2018-07-30 04:07:02 +02:00
boolean gettingDifferenceChannel = gettingDifferenceChannels.get(channelId);
2015-12-09 19:27:52 +01:00
if (gettingDifferenceChannel) {
return;
}
int limit = 100;
2018-07-30 04:07:02 +02:00
int channelPts;
2015-12-09 19:27:52 +01:00
if (newDialogType == 1) {
channelPts = channelsPts.get(channelId);
2018-07-30 04:07:02 +02:00
if (channelPts != 0) {
2015-12-09 19:27:52 +01:00
return;
}
2015-09-24 22:52:02 +02:00
channelPts = 1;
2015-12-09 19:27:52 +01:00
limit = 1;
2015-09-24 22:52:02 +02:00
} else {
channelPts = channelsPts.get(channelId);
2018-07-30 04:07:02 +02:00
if (channelPts == 0) {
2019-07-18 15:01:39 +02:00
channelPts = getMessagesStorage().getChannelPtsSync(channelId);
2015-09-24 22:52:02 +02:00
if (channelPts != 0) {
channelsPts.put(channelId, channelPts);
}
2017-03-31 01:58:05 +02:00
if (channelPts == 0 && (newDialogType == 2 || newDialogType == 3)) {
2016-03-16 13:26:32 +01:00
return;
2015-12-09 19:27:52 +01:00
}
2015-09-24 22:52:02 +02:00
}
2015-12-09 19:27:52 +01:00
if (channelPts == 0) {
2015-09-24 22:52:02 +02:00
return;
}
}
2017-03-31 01:58:05 +02:00
if (inputChannel == null) {
2017-12-08 18:35:59 +01:00
TLRPC.Chat chat = getChat(channelId);
if (chat == null) {
2019-07-18 15:01:39 +02:00
chat = getMessagesStorage().getChatSync(channelId);
2017-12-08 18:35:59 +01:00
if (chat != null) {
putChat(chat, true);
}
}
inputChannel = getInputChannel(chat);
2017-03-31 01:58:05 +02:00
}
if (inputChannel == null || inputChannel.access_hash == 0) {
if (taskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(taskId);
2017-03-31 01:58:05 +02:00
}
return;
}
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
2017-03-31 01:58:05 +02:00
data = new NativeByteBuffer(12 + inputChannel.getObjectSize());
data.writeInt32(6);
data.writeInt32(channelId);
data.writeInt32(newDialogType);
2017-03-31 01:58:05 +02:00
inputChannel.serializeToStream(data);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
} else {
newTaskId = taskId;
}
2015-09-24 22:52:02 +02:00
gettingDifferenceChannels.put(channelId, true);
TLRPC.TL_updates_getChannelDifference req = new TLRPC.TL_updates_getChannelDifference();
2017-03-31 01:58:05 +02:00
req.channel = inputChannel;
2015-09-24 22:52:02 +02:00
req.filter = new TLRPC.TL_channelMessagesFilterEmpty();
req.pts = channelPts;
2015-12-09 19:27:52 +01:00
req.limit = limit;
2017-03-31 01:58:05 +02:00
req.force = newDialogType != 3;
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("start getChannelDifference with pts = " + channelPts + " channelId = " + channelId);
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
if (response != null) {
2018-08-27 10:33:11 +02:00
final TLRPC.updates_ChannelDifference res = (TLRPC.updates_ChannelDifference) response;
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
for (int a = 0; a < res.users.size(); a++) {
TLRPC.User user = res.users.get(a);
usersDict.put(user.id, user);
}
TLRPC.Chat channel = null;
for (int a = 0; a < res.chats.size(); a++) {
TLRPC.Chat chat = res.chats.get(a);
if (chat.id == channelId) {
channel = chat;
break;
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
final TLRPC.Chat channelFinal = channel;
final ArrayList<TLRPC.TL_updateMessageID> msgUpdates = new ArrayList<>();
if (!res.other_updates.isEmpty()) {
for (int a = 0; a < res.other_updates.size(); a++) {
TLRPC.Update upd = res.other_updates.get(a);
if (upd instanceof TLRPC.TL_updateMessageID) {
msgUpdates.add((TLRPC.TL_updateMessageID) upd);
res.other_updates.remove(a);
a--;
2015-11-26 22:04:02 +01:00
}
}
2018-08-27 10:33:11 +02:00
}
2015-09-24 22:52:02 +02:00
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, res.chats, true, true);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(res.users, false);
putChats(res.chats, false);
});
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
2018-08-27 10:33:11 +02:00
if (!msgUpdates.isEmpty()) {
final SparseArray<long[]> corrected = new SparseArray<>();
for (TLRPC.TL_updateMessageID update : msgUpdates) {
2019-07-18 15:01:39 +02:00
long[] ids = getMessagesStorage().updateMessageStateAndId(update.random_id, null, update.id, 0, false, channelId);
2018-08-27 10:33:11 +02:00
if (ids != null) {
corrected.put(update.id, ids);
2015-09-24 22:52:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
if (corrected.size() != 0) {
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0; a < corrected.size(); a++) {
int newId = corrected.keyAt(a);
long[] ids = corrected.valueAt(a);
int oldId = (int) ids[1];
2019-07-18 15:01:39 +02:00
getSendMessagesHelper().processSentMessage(oldId);
getNotificationCenter().postNotificationName(NotificationCenter.messageReceivedByServer, oldId, newId, null, ids[0], 0L, -1);
2018-08-27 10:33:11 +02:00
}
});
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
if (res instanceof TLRPC.TL_updates_channelDifference || res instanceof TLRPC.TL_updates_channelDifferenceEmpty) {
if (!res.new_messages.isEmpty()) {
final LongSparseArray<ArrayList<MessageObject>> messages = new LongSparseArray<>();
ImageLoader.saveMessagesThumbs(res.new_messages);
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
final ArrayList<MessageObject> pushMessages = new ArrayList<>();
long dialog_id = -channelId;
Integer inboxValue = dialogs_read_inbox_max.get(dialog_id);
if (inboxValue == null) {
2019-07-18 15:01:39 +02:00
inboxValue = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_inbox_max.put(dialog_id, inboxValue);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
Integer outboxValue = dialogs_read_outbox_max.get(dialog_id);
if (outboxValue == null) {
2019-07-18 15:01:39 +02:00
outboxValue = getMessagesStorage().getDialogReadMax(true, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(dialog_id, outboxValue);
}
2018-08-27 10:33:11 +02:00
for (int a = 0; a < res.new_messages.size(); a++) {
TLRPC.Message message = res.new_messages.get(a);
message.unread = !(channelFinal != null && channelFinal.left || (message.out ? outboxValue : inboxValue) >= message.id || message.action instanceof TLRPC.TL_messageActionChannelCreate);
if (channelFinal != null && channelFinal.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
2018-08-27 10:33:11 +02:00
MessageObject obj = new MessageObject(currentAccount, message, usersDict, createdDialogIds.contains(dialog_id));
if (!obj.isOut() && obj.isUnread()) {
pushMessages.add(obj);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
long uid = -channelId;
ArrayList<MessageObject> arr = messages.get(uid);
if (arr == null) {
arr = new ArrayList<>();
messages.put(uid, arr);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
arr.add(obj);
}
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0; a < messages.size(); a++) {
long key = messages.keyAt(a);
ArrayList<MessageObject> value = messages.valueAt(a);
updateInterfaceWithMessages(key, value);
2015-09-24 22:52:02 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
});
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
2018-08-27 10:33:11 +02:00
if (!pushMessages.isEmpty()) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationsController().processNewMessages(pushMessages, true, false, null));
2018-07-30 04:07:02 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(res.new_messages, true, false, false, getDownloadController().getAutodownloadMask());
2018-08-27 10:33:11 +02:00
});
}
2018-08-27 10:33:11 +02:00
if (!res.other_updates.isEmpty()) {
2019-07-18 15:01:39 +02:00
processUpdateArray(res.other_updates, res.users, res.chats, true, 0);
2018-08-27 10:33:11 +02:00
}
processChannelsUpdatesQueue(channelId, 1);
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveChannelPts(channelId, res.pts);
2018-08-27 10:33:11 +02:00
} else if (res instanceof TLRPC.TL_updates_channelDifferenceTooLong) {
long dialog_id = -channelId;
Integer inboxValue = dialogs_read_inbox_max.get(dialog_id);
if (inboxValue == null) {
2019-07-18 15:01:39 +02:00
inboxValue = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_inbox_max.put(dialog_id, inboxValue);
}
Integer outboxValue = dialogs_read_outbox_max.get(dialog_id);
if (outboxValue == null) {
2019-07-18 15:01:39 +02:00
outboxValue = getMessagesStorage().getDialogReadMax(true, dialog_id);
2018-08-27 10:33:11 +02:00
dialogs_read_outbox_max.put(dialog_id, outboxValue);
}
for (int a = 0; a < res.messages.size(); a++) {
TLRPC.Message message = res.messages.get(a);
message.dialog_id = -channelId;
message.unread = !(message.action instanceof TLRPC.TL_messageActionChannelCreate || channelFinal != null && channelFinal.left || (message.out ? outboxValue : inboxValue) >= message.id);
if (channelFinal != null && channelFinal.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().overwriteChannel(channelId, (TLRPC.TL_updates_channelDifferenceTooLong) res, newDialogType);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
gettingDifferenceChannels.delete(channelId);
channelsPts.put(channelId, res.pts);
if ((res.flags & 2) != 0) {
shortPollChannels.put(channelId, (int) (System.currentTimeMillis() / 1000) + res.timeout);
}
if (!res.isFinal) {
getChannelDifference(channelId);
}
if (BuildVars.LOGS_ENABLED) {
FileLog.d("received channel difference with pts = " + res.pts + " channelId = " + channelId);
FileLog.d("new_messages = " + res.new_messages.size() + " messages = " + res.messages.size() + " users = " + res.users.size() + " chats = " + res.chats.size() + " other updates = " + res.other_updates.size());
}
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2016-03-16 13:26:32 +01:00
}
});
2018-08-27 10:33:11 +02:00
});
2019-01-23 18:03:33 +01:00
} else if (error != null) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> checkChannelError(error.text, channelId));
gettingDifferenceChannels.delete(channelId);
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
}
}
2015-09-24 22:52:02 +02:00
});
}
2016-03-16 13:26:32 +01:00
private void checkChannelError(String text, int channelId) {
switch (text) {
case "CHANNEL_PRIVATE":
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoCantLoad, channelId, 0);
2016-03-16 13:26:32 +01:00
break;
case "CHANNEL_PUBLIC_GROUP_NA":
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoCantLoad, channelId, 1);
2016-03-16 13:26:32 +01:00
break;
case "USER_BANNED_IN_CHANNEL":
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.chatInfoCantLoad, channelId, 2);
2016-03-16 13:26:32 +01:00
break;
}
}
2013-10-25 17:19:00 +02:00
public void getDifference() {
2019-07-18 15:01:39 +02:00
getDifference(getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue(), false);
2015-09-24 22:52:02 +02:00
}
2017-12-08 18:35:59 +01:00
public void getDifference(int pts, final int date, final int qts, boolean slice) {
2018-07-30 04:07:02 +02:00
registerForPush(SharedConfig.pushString);
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastPtsValue() == 0) {
2013-10-25 17:19:00 +02:00
loadCurrentState();
return;
}
2015-09-24 22:52:02 +02:00
if (!slice && gettingDifference) {
2013-10-25 17:19:00 +02:00
return;
}
gettingDifference = true;
TLRPC.TL_updates_getDifference req = new TLRPC.TL_updates_getDifference();
2015-09-24 22:52:02 +02:00
req.pts = pts;
req.date = date;
req.qts = qts;
2017-12-08 18:35:59 +01:00
if (getDifferenceFirstSync) {
req.flags |= 1;
2019-01-23 18:03:33 +01:00
if (ApplicationLoader.isConnectedOrConnectingToWiFi()) {
2017-12-08 18:35:59 +01:00
req.pts_total_limit = 5000;
} else {
req.pts_total_limit = 1000;
}
getDifferenceFirstSync = false;
}
2015-04-09 20:00:14 +02:00
if (req.date == 0) {
2019-07-18 15:01:39 +02:00
req.date = getConnectionsManager().getCurrentTime();
2015-04-09 20:00:14 +02:00
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("start getDifference with date = " + date + " pts = " + pts + " qts = " + qts);
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().setIsUpdating(true);
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (error == null) {
final TLRPC.updates_Difference res = (TLRPC.updates_Difference) response;
if (res instanceof TLRPC.TL_updates_differenceTooLong) {
AndroidUtilities.runOnUIThread(() -> {
loadedFullUsers.clear();
loadedFullChats.clear();
2019-07-18 15:01:39 +02:00
resetDialogs(true, getMessagesStorage().getLastSeqValue(), res.pts, date, qts);
2018-08-27 10:33:11 +02:00
});
} else {
if (res instanceof TLRPC.TL_updates_differenceSlice) {
getDifference(res.intermediate_state.pts, res.intermediate_state.date, res.intermediate_state.qts, true);
}
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>();
for (int a = 0; a < res.users.size(); a++) {
TLRPC.User user = res.users.get(a);
usersDict.put(user.id, user);
}
for (int a = 0; a < res.chats.size(); a++) {
TLRPC.Chat chat = res.chats.get(a);
chatsDict.put(chat.id, chat);
}
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
final ArrayList<TLRPC.TL_updateMessageID> msgUpdates = new ArrayList<>();
if (!res.other_updates.isEmpty()) {
for (int a = 0; a < res.other_updates.size(); a++) {
TLRPC.Update upd = res.other_updates.get(a);
if (upd instanceof TLRPC.TL_updateMessageID) {
msgUpdates.add((TLRPC.TL_updateMessageID) upd);
res.other_updates.remove(a);
a--;
} else if (getUpdateType(upd) == 2) {
int channelId = getUpdateChannelId(upd);
int channelPts = channelsPts.get(channelId);
if (channelPts == 0) {
2019-07-18 15:01:39 +02:00
channelPts = getMessagesStorage().getChannelPtsSync(channelId);
2018-08-27 10:33:11 +02:00
if (channelPts != 0) {
channelsPts.put(channelId, channelPts);
}
}
if (channelPts != 0 && getUpdatePts(upd) <= channelPts) {
2017-12-08 18:35:59 +01:00
res.other_updates.remove(a);
a--;
}
2013-10-25 17:19:00 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
AndroidUtilities.runOnUIThread(() -> {
loadedFullUsers.clear();
loadedFullChats.clear();
putUsers(res.users, false);
putChats(res.chats, false);
});
2013-10-25 17:19:00 +02:00
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
getMessagesStorage().putUsersAndChats(res.users, res.chats, true, false);
2018-08-27 10:33:11 +02:00
if (!msgUpdates.isEmpty()) {
final SparseArray<long[]> corrected = new SparseArray<>();
for (int a = 0; a < msgUpdates.size(); a++) {
TLRPC.TL_updateMessageID update = msgUpdates.get(a);
2019-07-18 15:01:39 +02:00
long[] ids = getMessagesStorage().updateMessageStateAndId(update.random_id, null, update.id, 0, false, 0);
2018-08-27 10:33:11 +02:00
if (ids != null) {
corrected.put(update.id, ids);
}
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
if (corrected.size() != 0) {
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0; a < corrected.size(); a++) {
int newId = corrected.keyAt(a);
long[] ids = corrected.valueAt(a);
int oldId = (int) ids[1];
2019-07-18 15:01:39 +02:00
getSendMessagesHelper().processSentMessage(oldId);
getNotificationCenter().postNotificationName(NotificationCenter.messageReceivedByServer, oldId, newId, null, ids[0], 0L, -1);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
});
}
}
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
Utilities.stageQueue.postRunnable(() -> {
if (!res.new_messages.isEmpty() || !res.new_encrypted_messages.isEmpty()) {
final LongSparseArray<ArrayList<MessageObject>> messages = new LongSparseArray<>();
for (int b = 0; b < res.new_encrypted_messages.size(); b++) {
TLRPC.EncryptedMessage encryptedMessage = res.new_encrypted_messages.get(b);
2019-07-18 15:01:39 +02:00
ArrayList<TLRPC.Message> decryptedMessages = getSecretChatHelper().decryptMessage(encryptedMessage);
2018-08-27 10:33:11 +02:00
if (decryptedMessages != null && !decryptedMessages.isEmpty()) {
res.new_messages.addAll(decryptedMessages);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
}
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
ImageLoader.saveMessagesThumbs(res.new_messages);
2013-10-25 17:19:00 +02:00
2018-08-27 10:33:11 +02:00
final ArrayList<MessageObject> pushMessages = new ArrayList<>();
2019-07-18 15:01:39 +02:00
int clientUserId = getUserConfig().getClientUserId();
2018-08-27 10:33:11 +02:00
for (int a = 0; a < res.new_messages.size(); a++) {
TLRPC.Message message = res.new_messages.get(a);
if (message.dialog_id == 0) {
if (message.to_id.chat_id != 0) {
message.dialog_id = -message.to_id.chat_id;
} else {
2019-07-18 15:01:39 +02:00
if (message.to_id.user_id == getUserConfig().getClientUserId()) {
2018-08-27 10:33:11 +02:00
message.to_id.user_id = message.from_id;
}
message.dialog_id = message.to_id.user_id;
}
2018-08-27 10:33:11 +02:00
}
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
if ((int) message.dialog_id != 0) {
if (message.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
TLRPC.User user = usersDict.get(message.action.user_id);
if (user != null && user.bot) {
message.reply_markup = new TLRPC.TL_replyKeyboardHide();
message.flags |= 64;
2017-12-08 18:35:59 +01:00
}
}
2018-08-27 10:33:11 +02:00
if (message.action instanceof TLRPC.TL_messageActionChatMigrateTo || message.action instanceof TLRPC.TL_messageActionChannelCreate) {
message.unread = false;
message.media_unread = false;
} else {
ConcurrentHashMap<Long, Integer> read_max = message.out ? dialogs_read_outbox_max : dialogs_read_inbox_max;
Integer value = read_max.get(message.dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
2018-08-27 10:33:11 +02:00
read_max.put(message.dialog_id, value);
}
message.unread = value < message.id;
2018-07-30 04:07:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
if (message.dialog_id == clientUserId) {
message.unread = false;
message.media_unread = false;
message.out = true;
}
MessageObject obj = new MessageObject(currentAccount, message, usersDict, chatsDict, createdDialogIds.contains(message.dialog_id));
if (!obj.isOut() && obj.isUnread()) {
pushMessages.add(obj);
}
ArrayList<MessageObject> arr = messages.get(message.dialog_id);
if (arr == null) {
arr = new ArrayList<>();
messages.put(message.dialog_id, arr);
}
arr.add(obj);
}
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0; a < messages.size(); a++) {
long key = messages.keyAt(a);
ArrayList<MessageObject> value = messages.valueAt(a);
updateInterfaceWithMessages(key, value);
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
});
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
2018-08-27 10:33:11 +02:00
if (!pushMessages.isEmpty()) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationsController().processNewMessages(pushMessages, !(res instanceof TLRPC.TL_updates_differenceSlice), false, null));
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(res.new_messages, true, false, false, getDownloadController().getAutodownloadMask());
2017-12-08 18:35:59 +01:00
});
2018-08-27 10:33:11 +02:00
2019-07-18 15:01:39 +02:00
getSecretChatHelper().processPendingEncMessages();
2018-08-27 10:33:11 +02:00
}
if (!res.other_updates.isEmpty()) {
2019-07-18 15:01:39 +02:00
processUpdateArray(res.other_updates, res.users, res.chats, true, 0);
2018-08-27 10:33:11 +02:00
}
if (res instanceof TLRPC.TL_updates_difference) {
gettingDifference = false;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastSeqValue(res.state.seq);
getMessagesStorage().setLastDateValue(res.state.date);
getMessagesStorage().setLastPtsValue(res.state.pts);
getMessagesStorage().setLastQtsValue(res.state.qts);
getConnectionsManager().setIsUpdating(false);
2018-08-27 10:33:11 +02:00
for (int a = 0; a < 3; a++) {
processUpdatesQueue(a, 1);
}
} else if (res instanceof TLRPC.TL_updates_differenceSlice) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastDateValue(res.intermediate_state.date);
getMessagesStorage().setLastPtsValue(res.intermediate_state.pts);
getMessagesStorage().setLastQtsValue(res.intermediate_state.qts);
2018-08-27 10:33:11 +02:00
} else if (res instanceof TLRPC.TL_updates_differenceEmpty) {
gettingDifference = false;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastSeqValue(res.seq);
getMessagesStorage().setLastDateValue(res.date);
getConnectionsManager().setIsUpdating(false);
2018-08-27 10:33:11 +02:00
for (int a = 0; a < 3; a++) {
processUpdatesQueue(a, 1);
}
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveDiffParams(getMessagesStorage().getLastSeqValue(), getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue());
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-07-18 15:01:39 +02:00
FileLog.d("received difference with date = " + getMessagesStorage().getLastDateValue() + " pts = " + getMessagesStorage().getLastPtsValue() + " seq = " + getMessagesStorage().getLastSeqValue() + " messages = " + res.new_messages.size() + " users = " + res.users.size() + " chats = " + res.chats.size() + " other updates = " + res.other_updates.size());
2017-12-08 18:35:59 +01:00
}
});
2018-08-27 10:33:11 +02:00
});
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
gettingDifference = false;
2019-07-18 15:01:39 +02:00
getConnectionsManager().setIsUpdating(false);
2015-09-24 22:52:02 +02:00
}
});
}
2018-07-30 04:07:02 +02:00
public void markDialogAsUnread(long did, TLRPC.InputPeer peer, long taskId) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2018-07-30 04:07:02 +02:00
if (dialog != null) {
dialog.unread_mark = true;
if (dialog.unread_count == 0 && !isDialogMuted(did)) {
unreadUnmutedDialogs++;
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
getMessagesStorage().setDialogUnread(did, true);
2018-07-30 04:07:02 +02:00
}
int lower_id = (int) did;
if (lower_id != 0) {
TLRPC.TL_messages_markDialogUnread req = new TLRPC.TL_messages_markDialogUnread();
req.unread = true;
if (peer == null) {
peer = getInputPeer(lower_id);
}
if (peer instanceof TLRPC.TL_inputPeerEmpty) {
return;
}
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = peer;
req.peer = inputDialogPeer;
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(12 + peer.getObjectSize());
data.writeInt32(9);
data.writeInt64(did);
peer.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2018-07-30 04:07:02 +02:00
} else {
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2018-07-30 04:07:02 +02:00
}
});
}
}
public void loadUnreadDialogs() {
2019-07-18 15:01:39 +02:00
if (loadingUnreadDialogs || getUserConfig().unreadDialogsLoaded) {
2018-07-30 04:07:02 +02:00
return;
}
loadingUnreadDialogs = true;
TLRPC.TL_messages_getDialogUnreadMarks req = new TLRPC.TL_messages_getDialogUnreadMarks();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
final TLRPC.Vector vector = (TLRPC.Vector) response;
for (int a = 0, size = vector.objects.size(); a < size; a++) {
TLRPC.DialogPeer peer = (TLRPC.DialogPeer) vector.objects.get(a);
if (peer instanceof TLRPC.TL_dialogPeer) {
TLRPC.TL_dialogPeer dialogPeer = (TLRPC.TL_dialogPeer) peer;
long did;
if (dialogPeer.peer.user_id != 0) {
if (dialogPeer.peer.user_id != 0) {
did = dialogPeer.peer.user_id;
} else if (dialogPeer.peer.chat_id != 0) {
did = -dialogPeer.peer.chat_id;
} else {
did = -dialogPeer.peer.channel_id;
}
} else {
did = 0;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogUnread(did, true);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2018-08-27 10:33:11 +02:00
if (dialog != null && !dialog.unread_mark) {
dialog.unread_mark = true;
if (dialog.unread_count == 0 && !isDialogMuted(did)) {
unreadUnmutedDialogs++;
2018-07-30 04:07:02 +02:00
}
}
}
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getUserConfig().unreadDialogsLoaded = true;
getUserConfig().saveConfig(false);
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
2018-08-27 10:33:11 +02:00
loadingUnreadDialogs = false;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
}));
2018-07-30 04:07:02 +02:00
}
2019-05-14 14:08:05 +02:00
public void reorderPinnedDialogs(int folderId, ArrayList<TLRPC.InputDialogPeer> order, long taskId) {
TLRPC.TL_messages_reorderPinnedDialogs req = new TLRPC.TL_messages_reorderPinnedDialogs();
req.folder_id = folderId;
req.force = true;
final long newTaskId;
if (taskId == 0) {
ArrayList<TLRPC.Dialog> dialogs = getDialogs(folderId);
if (dialogs.isEmpty()) {
return;
}
int size = 0;
for (int a = 0, N = dialogs.size(); a < N; a++) {
TLRPC.Dialog dialog = dialogs.get(a);
if (dialog instanceof TLRPC.TL_dialogFolder) {
continue;
}
if (!dialog.pinned) {
break;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogPinned(dialog.id, dialog.pinnedNum);
2019-05-14 14:08:05 +02:00
if ((int) dialog.id != 0) {
TLRPC.InputPeer inputPeer = getInputPeer((int) dialogs.get(a).id);
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = inputPeer;
req.order.add(inputDialogPeer);
size += inputDialogPeer.getObjectSize();
}
}
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(4 + 4 + 4 + size);
data.writeInt32(16);
data.writeInt32(folderId);
data.writeInt32(req.order.size());
for (int a = 0, N = req.order.size(); a < N; a++) {
req.order.get(a).serializeToStream(data);
}
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2019-05-14 14:08:05 +02:00
} else {
req.order = order;
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-05-14 14:08:05 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2019-05-14 14:08:05 +02:00
}
});
}
2017-03-31 01:58:05 +02:00
public boolean pinDialog(long did, boolean pin, TLRPC.InputPeer peer, long taskId) {
int lower_id = (int) did;
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2017-03-31 01:58:05 +02:00
if (dialog == null || dialog.pinned == pin) {
return dialog != null;
}
2019-05-14 14:08:05 +02:00
int folderId = dialog.folder_id;
ArrayList<TLRPC.Dialog> dialogs = getDialogs(folderId);
2017-03-31 01:58:05 +02:00
dialog.pinned = pin;
if (pin) {
int maxPinnedNum = 0;
for (int a = 0; a < dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog d = dialogs.get(a);
if (d instanceof TLRPC.TL_dialogFolder) {
continue;
}
2017-03-31 01:58:05 +02:00
if (!d.pinned) {
break;
}
maxPinnedNum = Math.max(d.pinnedNum, maxPinnedNum);
}
dialog.pinnedNum = maxPinnedNum + 1;
} else {
dialog.pinnedNum = 0;
}
sortDialogs(null);
2019-05-14 14:08:05 +02:00
if (!pin && dialogs.get(dialogs.size() - 1) == dialog && !dialogsEndReached.get(folderId)) {
2017-03-31 01:58:05 +02:00
dialogs.remove(dialogs.size() - 1);
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2017-03-31 01:58:05 +02:00
if (lower_id != 0) {
if (taskId != -1) {
TLRPC.TL_messages_toggleDialogPin req = new TLRPC.TL_messages_toggleDialogPin();
req.pinned = pin;
if (peer == null) {
peer = getInputPeer(lower_id);
}
if (peer instanceof TLRPC.TL_inputPeerEmpty) {
return false;
}
2018-07-30 04:07:02 +02:00
TLRPC.TL_inputDialogPeer inputDialogPeer = new TLRPC.TL_inputDialogPeer();
inputDialogPeer.peer = peer;
req.peer = inputDialogPeer;
2017-03-31 01:58:05 +02:00
final long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(16 + peer.getObjectSize());
2018-07-30 04:07:02 +02:00
data.writeInt32(4);
2017-03-31 01:58:05 +02:00
data.writeInt64(did);
data.writeBool(pin);
peer.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
2019-07-18 15:01:39 +02:00
newTaskId = getMessagesStorage().createPendingTask(data);
2017-03-31 01:58:05 +02:00
} else {
newTaskId = taskId;
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (newTaskId != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().removePendingTask(newTaskId);
2017-03-31 01:58:05 +02:00
}
});
}
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogPinned(did, dialog.pinnedNum);
2017-03-31 01:58:05 +02:00
return true;
}
2019-05-14 14:08:05 +02:00
public void loadPinnedDialogs(final int folderId, final long newDialogId, final ArrayList<Long> order) {
2019-07-18 15:01:39 +02:00
if (loadingPinnedDialogs.indexOfKey(folderId) >= 0 || getUserConfig().isPinnedDialogsLoaded(folderId)) {
2017-03-31 01:58:05 +02:00
return;
}
2019-05-14 14:08:05 +02:00
loadingPinnedDialogs.put(folderId, 1);
2017-03-31 01:58:05 +02:00
TLRPC.TL_messages_getPinnedDialogs req = new TLRPC.TL_messages_getPinnedDialogs();
2019-05-14 14:08:05 +02:00
req.folder_id = folderId;
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
final TLRPC.TL_messages_peerDialogs res = (TLRPC.TL_messages_peerDialogs) response;
2019-05-14 14:08:05 +02:00
ArrayList<TLRPC.Dialog> newPinnedDialogs = new ArrayList<>(res.dialogs);
fetchFolderInLoadedPinnedDialogs(res);
2018-08-27 10:33:11 +02:00
final TLRPC.TL_messages_dialogs toCache = new TLRPC.TL_messages_dialogs();
toCache.users.addAll(res.users);
toCache.chats.addAll(res.chats);
toCache.dialogs.addAll(res.dialogs);
toCache.messages.addAll(res.messages);
2017-03-31 01:58:05 +02:00
2018-08-27 10:33:11 +02:00
final LongSparseArray<MessageObject> new_dialogMessage = new LongSparseArray<>();
final SparseArray<TLRPC.User> usersDict = new SparseArray<>();
final SparseArray<TLRPC.Chat> chatsDict = new SparseArray<>();
2017-03-31 01:58:05 +02:00
2018-08-27 10:33:11 +02:00
for (int a = 0; a < res.users.size(); a++) {
TLRPC.User u = res.users.get(a);
usersDict.put(u.id, u);
}
for (int a = 0; a < res.chats.size(); a++) {
TLRPC.Chat c = res.chats.get(a);
chatsDict.put(c.id, c);
}
for (int a = 0; a < res.messages.size(); a++) {
TLRPC.Message message = res.messages.get(a);
if (message.to_id.channel_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.channel_id);
if (chat != null && chat.left) {
continue;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
} else if (message.to_id.chat_id != 0) {
TLRPC.Chat chat = chatsDict.get(message.to_id.chat_id);
if (chat != null && chat.migrated_to != null) {
continue;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
}
MessageObject messageObject = new MessageObject(currentAccount, message, usersDict, chatsDict, false);
new_dialogMessage.put(messageObject.getDialogId(), messageObject);
}
2019-05-14 14:08:05 +02:00
boolean firstIsFolder = !newPinnedDialogs.isEmpty() && newPinnedDialogs.get(0) instanceof TLRPC.TL_dialogFolder;
for (int a = 0, N = newPinnedDialogs.size(); a < N; a++) {
TLRPC.Dialog d = newPinnedDialogs.get(a);
d.pinned = true;
DialogObject.initDialog(d);
2018-08-27 10:33:11 +02:00
if (DialogObject.isChannel(d)) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.left) {
continue;
}
} else if ((int) d.id < 0) {
TLRPC.Chat chat = chatsDict.get(-(int) d.id);
if (chat != null && chat.migrated_to != null) {
continue;
}
}
if (d.last_message_date == 0) {
MessageObject mess = new_dialogMessage.get(d.id);
if (mess != null) {
d.last_message_date = mess.messageOwner.date;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
}
2017-03-31 01:58:05 +02:00
2018-08-27 10:33:11 +02:00
Integer value = dialogs_read_inbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_inbox_max.put(d.id, Math.max(value, d.read_inbox_max_id));
value = dialogs_read_outbox_max.get(d.id);
if (value == null) {
value = 0;
}
dialogs_read_outbox_max.put(d.id, Math.max(value, d.read_outbox_max_id));
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> {
2019-05-14 14:08:05 +02:00
loadingPinnedDialogs.delete(folderId);
applyDialogsNotificationsSettings(newPinnedDialogs);
2018-08-27 10:33:11 +02:00
boolean changed = false;
boolean added = false;
int maxPinnedNum = 0;
2019-05-14 14:08:05 +02:00
ArrayList<TLRPC.Dialog> dialogs = getDialogs(folderId);
int pinnedNum = firstIsFolder ? 1 : 0;
2018-08-27 10:33:11 +02:00
for (int a = 0; a < dialogs.size(); a++) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs.get(a);
if (dialog instanceof TLRPC.TL_dialogFolder) {
continue;
}
2018-08-27 10:33:11 +02:00
if ((int) dialog.id == 0) {
2019-05-14 14:08:05 +02:00
if (pinnedNum < newPinnedDialogs.size()) {
newPinnedDialogs.add(pinnedNum, dialog);
} else {
newPinnedDialogs.add(dialog);
}
pinnedNum++;
2018-08-27 10:33:11 +02:00
continue;
}
if (!dialog.pinned) {
break;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
maxPinnedNum = Math.max(dialog.pinnedNum, maxPinnedNum);
dialog.pinned = false;
dialog.pinnedNum = 0;
changed = true;
2019-05-14 14:08:05 +02:00
pinnedNum++;
2018-08-27 10:33:11 +02:00
}
2017-03-31 01:58:05 +02:00
2018-08-27 10:33:11 +02:00
ArrayList<Long> pinnedDialogs = new ArrayList<>();
2019-05-14 14:08:05 +02:00
if (!newPinnedDialogs.isEmpty()) {
2018-08-27 10:33:11 +02:00
putUsers(res.users, false);
putChats(res.chats, false);
2019-05-14 14:08:05 +02:00
for (int a = 0, N = newPinnedDialogs.size(); a < N; a++) {
TLRPC.Dialog dialog = newPinnedDialogs.get(a);
dialog.pinnedNum = (N - a) + maxPinnedNum;
2017-03-31 01:58:05 +02:00
2019-05-14 14:08:05 +02:00
pinnedDialogs.add(dialog.id);
TLRPC.Dialog d = dialogs_dict.get(dialog.id);
2018-08-27 10:33:11 +02:00
if (d != null) {
d.pinned = true;
d.pinnedNum = dialog.pinnedNum;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogPinned(dialog.id, dialog.pinnedNum);
2018-08-27 10:33:11 +02:00
} else {
added = true;
dialogs_dict.put(dialog.id, dialog);
MessageObject messageObject = new_dialogMessage.get(dialog.id);
dialogMessage.put(dialog.id, messageObject);
if (messageObject != null && messageObject.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(messageObject.getId(), messageObject);
if (messageObject.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(messageObject.messageOwner.random_id, messageObject);
2017-03-31 01:58:05 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
changed = true;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
}
if (changed) {
if (added) {
2019-05-14 14:08:05 +02:00
allDialogs.clear();
2018-08-27 10:33:11 +02:00
for (int a = 0, size = dialogs_dict.size(); a < size; a++) {
2019-05-14 14:08:05 +02:00
allDialogs.add(dialogs_dict.valueAt(a));
2018-08-27 10:33:11 +02:00
}
}
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().unpinAllDialogsExceptNew(pinnedDialogs, folderId);
getMessagesStorage().putDialogs(toCache, 1);
getUserConfig().setPinnedDialogsLoaded(folderId, true);
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
}));
2017-03-31 01:58:05 +02:00
}
});
}
2015-12-09 19:27:52 +01:00
public void generateJoinMessage(final int chat_id, boolean ignoreLeft) {
2015-09-24 22:52:02 +02:00
TLRPC.Chat chat = getChat(chat_id);
2018-07-30 04:07:02 +02:00
if (chat == null || !ChatObject.isChannel(chat_id, currentAccount) || (chat.left || chat.kicked) && !ignoreLeft) {
2015-09-24 22:52:02 +02:00
return;
}
TLRPC.TL_messageService message = new TLRPC.TL_messageService();
message.flags = TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
2019-07-18 15:01:39 +02:00
message.local_id = message.id = getUserConfig().getNewMessageId();
message.date = getConnectionsManager().getCurrentTime();
message.from_id = getUserConfig().getClientUserId();
2015-09-24 22:52:02 +02:00
message.to_id = new TLRPC.TL_peerChannel();
message.to_id.channel_id = chat_id;
message.dialog_id = -chat_id;
2016-03-16 13:26:32 +01:00
message.post = true;
2015-09-24 22:52:02 +02:00
message.action = new TLRPC.TL_messageActionChatAddUser();
2019-07-18 15:01:39 +02:00
message.action.users.add(getUserConfig().getClientUserId());
2016-03-16 13:26:32 +01:00
if (chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
2019-07-18 15:01:39 +02:00
getUserConfig().saveConfig(false);
2015-09-24 22:52:02 +02:00
final ArrayList<MessageObject> pushMessages = new ArrayList<>();
final ArrayList<TLRPC.Message> messagesArr = new ArrayList<>();
messagesArr.add(message);
2018-07-30 04:07:02 +02:00
MessageObject obj = new MessageObject(currentAccount, message, true);
2015-09-24 22:52:02 +02:00
pushMessages.add(obj);
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> getNotificationsController().processNewMessages(pushMessages, true, false, null)));
getMessagesStorage().putMessages(messagesArr, true, true, false, 0);
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
updateInterfaceWithMessages(-chat_id, pushMessages);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
});
}
protected void deleteMessagesByPush(long dialogId, ArrayList<Integer> ids, int channelId) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
AndroidUtilities.runOnUIThread(() -> {
getNotificationCenter().postNotificationName(NotificationCenter.messagesDeleted, ids, channelId);
if (channelId == 0) {
for (int b = 0, size2 = ids.size(); b < size2; b++) {
Integer id = ids.get(b);
MessageObject obj = dialogMessagesByIds.get(id);
if (obj != null) {
obj.deleted = true;
}
}
} else {
MessageObject obj = dialogMessage.get((long) -channelId);
if (obj != null) {
for (int b = 0, size2 = ids.size(); b < size2; b++) {
if (obj.getId() == ids.get(b)) {
obj.deleted = true;
break;
}
}
}
}
});
getMessagesStorage().deletePushMessages(dialogId, ids);
ArrayList<Long> dialogIds = getMessagesStorage().markMessagesAsDeleted(ids, false, channelId);
getMessagesStorage().updateDialogsWithDeletedMessages(ids, dialogIds, false, channelId);
2015-09-24 22:52:02 +02:00
});
}
public void checkChannelInviter(final int chat_id) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
final TLRPC.Chat chat = getChat(chat_id);
if (chat == null || !ChatObject.isChannel(chat_id, currentAccount) || chat.creator) {
return;
}
TLRPC.TL_channels_getParticipant req = new TLRPC.TL_channels_getParticipant();
req.channel = getInputChannel(chat_id);
req.user_id = new TLRPC.TL_inputUserSelf();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
final TLRPC.TL_channels_channelParticipant res = (TLRPC.TL_channels_channelParticipant) response;
2019-07-18 15:01:39 +02:00
if (res != null && res.participant instanceof TLRPC.TL_channelParticipantSelf && res.participant.inviter_id != getUserConfig().getClientUserId()) {
if (chat.megagroup && getMessagesStorage().isMigratedChat(chat.id)) {
2018-08-27 10:33:11 +02:00
return;
}
AndroidUtilities.runOnUIThread(() -> putUsers(res.users, false));
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, null, true, true);
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
TLRPC.TL_messageService message = new TLRPC.TL_messageService();
message.media_unread = true;
message.unread = true;
message.flags = TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
message.post = true;
if (chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
2019-07-18 15:01:39 +02:00
message.local_id = message.id = getUserConfig().getNewMessageId();
2018-08-27 10:33:11 +02:00
message.date = res.participant.date;
message.action = new TLRPC.TL_messageActionChatAddUser();
message.from_id = res.participant.inviter_id;
2019-07-18 15:01:39 +02:00
message.action.users.add(getUserConfig().getClientUserId());
2018-08-27 10:33:11 +02:00
message.to_id = new TLRPC.TL_peerChannel();
message.to_id.channel_id = chat_id;
message.dialog_id = -chat_id;
2019-07-18 15:01:39 +02:00
getUserConfig().saveConfig(false);
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
final ArrayList<MessageObject> pushMessages = new ArrayList<>();
final ArrayList<TLRPC.Message> messagesArr = new ArrayList<>();
2015-09-24 22:52:02 +02:00
2018-08-27 10:33:11 +02:00
ConcurrentHashMap<Integer, TLRPC.User> usersDict = new ConcurrentHashMap<>();
for (int a = 0; a < res.users.size(); a++) {
TLRPC.User user = res.users.get(a);
usersDict.put(user.id, user);
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
messagesArr.add(message);
MessageObject obj = new MessageObject(currentAccount, message, usersDict, true);
pushMessages.add(obj);
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> getNotificationsController().processNewMessages(pushMessages, true, false, null)));
getMessagesStorage().putMessages(messagesArr, true, true, false, 0);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
updateInterfaceWithMessages(-chat_id, pushMessages);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
});
}
});
});
2013-10-25 17:19:00 +02:00
}
private int getUpdateType(TLRPC.Update update) {
if (update instanceof TLRPC.TL_updateNewMessage || update instanceof TLRPC.TL_updateReadMessagesContents || update instanceof TLRPC.TL_updateReadHistoryInbox ||
2016-04-22 15:49:00 +02:00
update instanceof TLRPC.TL_updateReadHistoryOutbox || update instanceof TLRPC.TL_updateDeleteMessages || update instanceof TLRPC.TL_updateWebPage ||
2019-05-14 14:08:05 +02:00
update instanceof TLRPC.TL_updateEditMessage || update instanceof TLRPC.TL_updateFolderPeers) {
return 0;
} else if (update instanceof TLRPC.TL_updateNewEncryptedMessage) {
return 1;
2017-03-31 01:58:05 +02:00
} else if (update instanceof TLRPC.TL_updateNewChannelMessage || update instanceof TLRPC.TL_updateDeleteChannelMessages || update instanceof TLRPC.TL_updateEditChannelMessage ||
update instanceof TLRPC.TL_updateChannelWebPage) {
return 2;
2015-09-24 22:52:02 +02:00
} else {
return 3;
}
}
2018-07-30 04:07:02 +02:00
private static int getUpdatePts(TLRPC.Update update) {
if (update instanceof TLRPC.TL_updateDeleteMessages) {
return ((TLRPC.TL_updateDeleteMessages) update).pts;
} else if (update instanceof TLRPC.TL_updateNewChannelMessage) {
return ((TLRPC.TL_updateNewChannelMessage) update).pts;
} else if (update instanceof TLRPC.TL_updateReadHistoryOutbox) {
return ((TLRPC.TL_updateReadHistoryOutbox) update).pts;
} else if (update instanceof TLRPC.TL_updateNewMessage) {
return ((TLRPC.TL_updateNewMessage) update).pts;
} else if (update instanceof TLRPC.TL_updateEditMessage) {
return ((TLRPC.TL_updateEditMessage) update).pts;
} else if (update instanceof TLRPC.TL_updateWebPage) {
return ((TLRPC.TL_updateWebPage) update).pts;
} else if (update instanceof TLRPC.TL_updateReadHistoryInbox) {
return ((TLRPC.TL_updateReadHistoryInbox) update).pts;
} else if (update instanceof TLRPC.TL_updateChannelWebPage) {
return ((TLRPC.TL_updateChannelWebPage) update).pts;
} else if (update instanceof TLRPC.TL_updateDeleteChannelMessages) {
return ((TLRPC.TL_updateDeleteChannelMessages) update).pts;
} else if (update instanceof TLRPC.TL_updateEditChannelMessage) {
return ((TLRPC.TL_updateEditChannelMessage) update).pts;
} else if (update instanceof TLRPC.TL_updateReadMessagesContents) {
return ((TLRPC.TL_updateReadMessagesContents) update).pts;
} else if (update instanceof TLRPC.TL_updateChannelTooLong) {
return ((TLRPC.TL_updateChannelTooLong) update).pts;
2019-05-14 14:08:05 +02:00
} else if (update instanceof TLRPC.TL_updateFolderPeers) {
return ((TLRPC.TL_updateFolderPeers) update).pts;
2018-07-30 04:07:02 +02:00
} else {
return 0;
}
}
private static int getUpdatePtsCount(TLRPC.Update update) {
if (update instanceof TLRPC.TL_updateDeleteMessages) {
return ((TLRPC.TL_updateDeleteMessages) update).pts_count;
} else if (update instanceof TLRPC.TL_updateNewChannelMessage) {
return ((TLRPC.TL_updateNewChannelMessage) update).pts_count;
} else if (update instanceof TLRPC.TL_updateReadHistoryOutbox) {
return ((TLRPC.TL_updateReadHistoryOutbox) update).pts_count;
} else if (update instanceof TLRPC.TL_updateNewMessage) {
return ((TLRPC.TL_updateNewMessage) update).pts_count;
} else if (update instanceof TLRPC.TL_updateEditMessage) {
return ((TLRPC.TL_updateEditMessage) update).pts_count;
} else if (update instanceof TLRPC.TL_updateWebPage) {
return ((TLRPC.TL_updateWebPage) update).pts_count;
} else if (update instanceof TLRPC.TL_updateReadHistoryInbox) {
return ((TLRPC.TL_updateReadHistoryInbox) update).pts_count;
} else if (update instanceof TLRPC.TL_updateChannelWebPage) {
return ((TLRPC.TL_updateChannelWebPage) update).pts_count;
} else if (update instanceof TLRPC.TL_updateDeleteChannelMessages) {
return ((TLRPC.TL_updateDeleteChannelMessages) update).pts_count;
} else if (update instanceof TLRPC.TL_updateEditChannelMessage) {
return ((TLRPC.TL_updateEditChannelMessage) update).pts_count;
} else if (update instanceof TLRPC.TL_updateReadMessagesContents) {
return ((TLRPC.TL_updateReadMessagesContents) update).pts_count;
2019-05-14 14:08:05 +02:00
} else if (update instanceof TLRPC.TL_updateFolderPeers) {
return ((TLRPC.TL_updateFolderPeers) update).pts_count;
2018-07-30 04:07:02 +02:00
} else {
return 0;
}
}
private static int getUpdateQts(TLRPC.Update update) {
if (update instanceof TLRPC.TL_updateNewEncryptedMessage) {
return ((TLRPC.TL_updateNewEncryptedMessage) update).qts;
} else {
return 0;
}
}
private static int getUpdateChannelId(TLRPC.Update update) {
if (update instanceof TLRPC.TL_updateNewChannelMessage) {
return ((TLRPC.TL_updateNewChannelMessage) update).message.to_id.channel_id;
} else if (update instanceof TLRPC.TL_updateEditChannelMessage) {
return ((TLRPC.TL_updateEditChannelMessage) update).message.to_id.channel_id;
2018-07-30 04:07:02 +02:00
} else if (update instanceof TLRPC.TL_updateReadChannelOutbox) {
return ((TLRPC.TL_updateReadChannelOutbox) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannelMessageViews) {
return ((TLRPC.TL_updateChannelMessageViews) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannelTooLong) {
return ((TLRPC.TL_updateChannelTooLong) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannelPinnedMessage) {
return ((TLRPC.TL_updateChannelPinnedMessage) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannelReadMessagesContents) {
return ((TLRPC.TL_updateChannelReadMessagesContents) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannelAvailableMessages) {
return ((TLRPC.TL_updateChannelAvailableMessages) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannel) {
return ((TLRPC.TL_updateChannel) update).channel_id;
} else if (update instanceof TLRPC.TL_updateChannelWebPage) {
return ((TLRPC.TL_updateChannelWebPage) update).channel_id;
} else if (update instanceof TLRPC.TL_updateDeleteChannelMessages) {
return ((TLRPC.TL_updateDeleteChannelMessages) update).channel_id;
} else if (update instanceof TLRPC.TL_updateReadChannelInbox) {
return ((TLRPC.TL_updateReadChannelInbox) update).channel_id;
} else {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.e("trying to get unknown update channel_id for " + update);
}
return 0;
}
}
public void processUpdates(final TLRPC.Updates updates, boolean fromQueue) {
2015-09-24 22:52:02 +02:00
ArrayList<Integer> needGetChannelsDiff = null;
2013-10-25 17:19:00 +02:00
boolean needGetDiff = false;
boolean needReceivedQueue = false;
2014-11-21 01:14:44 +01:00
boolean updateStatus = false;
2013-10-25 17:19:00 +02:00
if (updates instanceof TLRPC.TL_updateShort) {
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Update> arr = new ArrayList<>();
2013-10-25 17:19:00 +02:00
arr.add(updates.update);
2019-07-18 15:01:39 +02:00
processUpdateArray(arr, null, null, false, updates.date);
} else if (updates instanceof TLRPC.TL_updateShortChatMessage || updates instanceof TLRPC.TL_updateShortMessage) {
final int user_id = updates instanceof TLRPC.TL_updateShortChatMessage ? updates.from_id : updates.user_id;
TLRPC.User user = getUser(user_id);
TLRPC.User user2 = null;
TLRPC.User user3 = null;
2015-09-24 22:52:02 +02:00
TLRPC.Chat channel = null;
2016-05-25 23:49:47 +02:00
if (user == null || user.min) {
2019-07-18 15:01:39 +02:00
user = getMessagesStorage().getUserSync(user_id);
2016-04-22 15:49:00 +02:00
if (user != null && user.min) {
user = null;
}
2015-04-09 20:00:14 +02:00
putUser(user, true);
}
boolean needFwdUser = false;
2016-03-06 02:49:31 +01:00
if (updates.fwd_from != null) {
if (updates.fwd_from.from_id != 0) {
user2 = getUser(updates.fwd_from.from_id);
if (user2 == null) {
2019-07-18 15:01:39 +02:00
user2 = getMessagesStorage().getUserSync(updates.fwd_from.from_id);
2016-03-06 02:49:31 +01:00
putUser(user2, true);
}
needFwdUser = true;
2015-04-09 20:00:14 +02:00
}
2016-03-06 02:49:31 +01:00
if (updates.fwd_from.channel_id != 0) {
channel = getChat(updates.fwd_from.channel_id);
if (channel == null) {
2019-07-18 15:01:39 +02:00
channel = getMessagesStorage().getChatSync(updates.fwd_from.channel_id);
2016-03-06 02:49:31 +01:00
putChat(channel, true);
}
needFwdUser = true;
2015-09-24 22:52:02 +02:00
}
}
boolean needBotUser = false;
if (updates.via_bot_id != 0) {
user3 = getUser(updates.via_bot_id);
if (user3 == null) {
2019-07-18 15:01:39 +02:00
user3 = getMessagesStorage().getUserSync(updates.via_bot_id);
putUser(user3, true);
}
needBotUser = true;
}
2015-05-21 23:27:27 +02:00
boolean missingData;
if (updates instanceof TLRPC.TL_updateShortMessage) {
missingData = user == null || needFwdUser && user2 == null && channel == null || needBotUser && user3 == null;
} else {
2015-04-09 20:00:14 +02:00
TLRPC.Chat chat = getChat(updates.chat_id);
if (chat == null) {
2019-07-18 15:01:39 +02:00
chat = getMessagesStorage().getChatSync(updates.chat_id);
2015-04-09 20:00:14 +02:00
putChat(chat, true);
}
missingData = chat == null || user == null || needFwdUser && user2 == null && channel == null || needBotUser && user3 == null;
}
2016-05-25 23:49:47 +02:00
if (!missingData && !updates.entities.isEmpty()) {
for (int a = 0; a < updates.entities.size(); a++) {
TLRPC.MessageEntity entity = updates.entities.get(a);
if (entity instanceof TLRPC.TL_messageEntityMentionName) {
int uid = ((TLRPC.TL_messageEntityMentionName) entity).user_id;
TLRPC.User entityUser = getUser(uid);
if (entityUser == null || entityUser.min) {
2019-07-18 15:01:39 +02:00
entityUser = getMessagesStorage().getUserSync(uid);
2016-05-25 23:49:47 +02:00
if (entityUser != null && entityUser.min) {
entityUser = null;
}
if (entityUser == null) {
missingData = true;
break;
}
putUser(user, true);
}
}
}
}
2019-07-18 15:01:39 +02:00
if (user != null && user.status != null && user.status.expires <= 0 && Math.abs(getConnectionsManager().getCurrentTime() - updates.date) < 30) {
onlinePrivacy.put(user.id, updates.date);
2014-11-21 01:14:44 +01:00
updateStatus = true;
}
if (missingData) {
needGetDiff = true;
} else {
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastPtsValue() + updates.pts_count == updates.pts) {
TLRPC.TL_message message = new TLRPC.TL_message();
message.id = updates.id;
2019-07-18 15:01:39 +02:00
int clientUserId = getUserConfig().getClientUserId();
if (updates instanceof TLRPC.TL_updateShortMessage) {
2015-11-26 22:04:02 +01:00
if (updates.out) {
2016-10-11 13:57:01 +02:00
message.from_id = clientUserId;
} else {
message.from_id = user_id;
}
message.to_id = new TLRPC.TL_peerUser();
message.to_id.user_id = user_id;
message.dialog_id = user_id;
} else {
message.from_id = user_id;
message.to_id = new TLRPC.TL_peerChat();
message.to_id.chat_id = updates.chat_id;
message.dialog_id = -updates.chat_id;
}
2016-03-06 02:49:31 +01:00
message.fwd_from = updates.fwd_from;
message.silent = updates.silent;
2015-11-26 22:04:02 +01:00
message.out = updates.out;
message.mentioned = updates.mentioned;
message.media_unread = updates.media_unread;
2015-09-02 00:14:21 +02:00
message.entities = updates.entities;
message.message = updates.message;
message.date = updates.date;
message.via_bot_id = updates.via_bot_id;
2015-09-24 22:52:02 +02:00
message.flags = updates.flags | TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
message.reply_to_msg_id = updates.reply_to_msg_id;
message.media = new TLRPC.TL_messageMediaEmpty();
ConcurrentHashMap<Long, Integer> read_max = message.out ? dialogs_read_outbox_max : dialogs_read_inbox_max;
Integer value = read_max.get(message.dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
read_max.put(message.dialog_id, value);
}
message.unread = value < message.id;
2016-10-11 13:57:01 +02:00
if (message.dialog_id == clientUserId) {
message.unread = false;
message.media_unread = false;
message.out = true;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastPtsValue(updates.pts);
2018-07-30 04:07:02 +02:00
final MessageObject obj = new MessageObject(currentAccount, message, createdDialogIds.contains(message.dialog_id));
2015-01-02 23:15:07 +01:00
final ArrayList<MessageObject> objArr = new ArrayList<>();
objArr.add(obj);
2015-01-02 23:15:07 +01:00
ArrayList<TLRPC.Message> arr = new ArrayList<>();
arr.add(message);
if (updates instanceof TLRPC.TL_updateShortMessage) {
2015-11-26 22:04:02 +01:00
final boolean printUpdate = !updates.out && updatePrintingUsersWithNewMessages(updates.user_id, objArr);
if (printUpdate) {
updatePrintingStrings();
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (printUpdate) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT);
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
updateInterfaceWithMessages(user_id, objArr);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
});
} else {
final boolean printUpdate = updatePrintingUsersWithNewMessages(-updates.chat_id, objArr);
if (printUpdate) {
updatePrintingStrings();
2013-10-25 17:19:00 +02:00
}
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (printUpdate) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT);
}
2018-08-27 10:33:11 +02:00
updateInterfaceWithMessages(-updates.chat_id, objArr);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
});
}
if (!obj.isOut()) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> getNotificationsController().processNewMessages(objArr, true, false, null)));
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(arr, false, true, false, 0);
} else if (getMessagesStorage().getLastPtsValue() != updates.pts) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-07-18 15:01:39 +02:00
FileLog.d("need get diff short message, pts: " + getMessagesStorage().getLastPtsValue() + " " + updates.pts + " count = " + updates.pts_count);
2018-07-30 04:07:02 +02:00
}
2015-11-26 22:04:02 +01:00
if (gettingDifference || updatesStartWaitTimePts == 0 || Math.abs(System.currentTimeMillis() - updatesStartWaitTimePts) <= 1500) {
if (updatesStartWaitTimePts == 0) {
updatesStartWaitTimePts = System.currentTimeMillis();
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("add to queue");
}
updatesQueuePts.add(updates);
} else {
needGetDiff = true;
}
}
2013-10-25 17:19:00 +02:00
}
} else if (updates instanceof TLRPC.TL_updatesCombined || updates instanceof TLRPC.TL_updates) {
2018-07-30 04:07:02 +02:00
SparseArray<TLRPC.Chat> minChannels = null;
2016-03-16 13:26:32 +01:00
for (int a = 0; a < updates.chats.size(); a++) {
TLRPC.Chat chat = updates.chats.get(a);
if (chat instanceof TLRPC.TL_channel) {
if (chat.min) {
TLRPC.Chat existChat = getChat(chat.id);
if (existChat == null || existChat.min) {
2019-07-18 15:01:39 +02:00
TLRPC.Chat cacheChat = getMessagesStorage().getChatSync(updates.chat_id);
2017-07-08 18:32:04 +02:00
putChat(cacheChat, true);
existChat = cacheChat;
}
if (existChat == null || existChat.min) {
if (minChannels == null) {
2018-07-30 04:07:02 +02:00
minChannels = new SparseArray<>();
}
minChannels.put(chat.id, chat);
2013-10-25 17:19:00 +02:00
}
2016-03-16 13:26:32 +01:00
}
}
}
if (minChannels != null) {
for (int a = 0; a < updates.updates.size(); a++) {
TLRPC.Update update = updates.updates.get(a);
if (update instanceof TLRPC.TL_updateNewChannelMessage) {
2017-12-08 18:35:59 +01:00
TLRPC.Message message = ((TLRPC.TL_updateNewChannelMessage) update).message;
int channelId = message.to_id.channel_id;
2018-07-30 04:07:02 +02:00
if (minChannels.indexOfKey(channelId) >= 0) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("need get diff because of min channel " + channelId);
}
needGetDiff = true;
2016-03-16 13:26:32 +01:00
break;
2014-07-30 09:49:39 +02:00
}
2017-12-08 18:35:59 +01:00
/*if (message.fwd_from != null && message.fwd_from.channel_id != 0) {
channelId = message.fwd_from.channel_id;
if (minChannels.containsKey(channelId)) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.e("need get diff because of min forward channel " + channelId);
}
2017-12-08 18:35:59 +01:00
needGetDiff = true;
break;
}
}*/
}
2016-03-16 13:26:32 +01:00
}
}
if (!needGetDiff) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(updates.users, updates.chats, true, true);
Collections.sort(updates.updates, updatesComparator);
2016-03-16 13:26:32 +01:00
for (int a = 0; a < updates.updates.size(); a++) {
TLRPC.Update update = updates.updates.get(a);
if (getUpdateType(update) == 0) {
TLRPC.TL_updates updatesNew = new TLRPC.TL_updates();
updatesNew.updates.add(update);
2018-07-30 04:07:02 +02:00
updatesNew.pts = getUpdatePts(update);
updatesNew.pts_count = getUpdatePtsCount(update);
2016-03-16 13:26:32 +01:00
for (int b = a + 1; b < updates.updates.size(); b++) {
TLRPC.Update update2 = updates.updates.get(b);
2018-07-30 04:07:02 +02:00
int pts2 = getUpdatePts(update2);
int count2 = getUpdatePtsCount(update2);
if (getUpdateType(update2) == 0 && updatesNew.pts + count2 == pts2) {
2016-03-16 13:26:32 +01:00
updatesNew.updates.add(update2);
2018-07-30 04:07:02 +02:00
updatesNew.pts = pts2;
updatesNew.pts_count += count2;
2016-03-16 13:26:32 +01:00
updates.updates.remove(b);
b--;
} else {
break;
}
}
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastPtsValue() + updatesNew.pts_count == updatesNew.pts) {
if (!processUpdateArray(updatesNew.updates, updates.users, updates.chats, false, updates.date)) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-07-18 15:01:39 +02:00
FileLog.d("need get diff inner TL_updates, pts: " + getMessagesStorage().getLastPtsValue() + " " + updates.seq);
2018-07-30 04:07:02 +02:00
}
2016-03-16 13:26:32 +01:00
needGetDiff = true;
} else {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastPtsValue(updatesNew.pts);
2018-07-30 04:07:02 +02:00
}
2019-07-18 15:01:39 +02:00
} else if (getMessagesStorage().getLastPtsValue() != updatesNew.pts) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-07-18 15:01:39 +02:00
FileLog.d(update + " need get diff, pts: " + getMessagesStorage().getLastPtsValue() + " " + updatesNew.pts + " count = " + updatesNew.pts_count);
2016-03-16 13:26:32 +01:00
}
if (gettingDifference || updatesStartWaitTimePts == 0 || updatesStartWaitTimePts != 0 && Math.abs(System.currentTimeMillis() - updatesStartWaitTimePts) <= 1500) {
if (updatesStartWaitTimePts == 0) {
updatesStartWaitTimePts = System.currentTimeMillis();
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("add to queue");
}
2016-03-16 13:26:32 +01:00
updatesQueuePts.add(updatesNew);
} else {
needGetDiff = true;
}
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
} else if (getUpdateType(update) == 1) {
TLRPC.TL_updates updatesNew = new TLRPC.TL_updates();
updatesNew.updates.add(update);
2018-07-30 04:07:02 +02:00
updatesNew.pts = getUpdateQts(update);
2016-03-16 13:26:32 +01:00
for (int b = a + 1; b < updates.updates.size(); b++) {
TLRPC.Update update2 = updates.updates.get(b);
2018-07-30 04:07:02 +02:00
int qts2 = getUpdateQts(update2);
if (getUpdateType(update2) == 1 && updatesNew.pts + 1 == qts2) {
2016-03-16 13:26:32 +01:00
updatesNew.updates.add(update2);
2018-07-30 04:07:02 +02:00
updatesNew.pts = qts2;
2016-03-16 13:26:32 +01:00
updates.updates.remove(b);
b--;
} else {
break;
}
}
2019-07-18 15:01:39 +02:00
if (getMessagesStorage().getLastQtsValue() == 0 || getMessagesStorage().getLastQtsValue() + updatesNew.updates.size() == updatesNew.pts) {
processUpdateArray(updatesNew.updates, updates.users, updates.chats, false, updates.date);
getMessagesStorage().setLastQtsValue(updatesNew.pts);
2016-03-16 13:26:32 +01:00
needReceivedQueue = true;
2019-07-18 15:01:39 +02:00
} else if (getMessagesStorage().getLastPtsValue() != updatesNew.pts) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2019-07-18 15:01:39 +02:00
FileLog.d(update + " need get diff, qts: " + getMessagesStorage().getLastQtsValue() + " " + updatesNew.pts);
2018-07-30 04:07:02 +02:00
}
2016-03-16 13:26:32 +01:00
if (gettingDifference || updatesStartWaitTimeQts == 0 || updatesStartWaitTimeQts != 0 && Math.abs(System.currentTimeMillis() - updatesStartWaitTimeQts) <= 1500) {
if (updatesStartWaitTimeQts == 0) {
updatesStartWaitTimeQts = System.currentTimeMillis();
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("add to queue");
}
2016-03-16 13:26:32 +01:00
updatesQueueQts.add(updatesNew);
} else {
needGetDiff = true;
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
}
} else if (getUpdateType(update) == 2) {
int channelId = getUpdateChannelId(update);
boolean skipUpdate = false;
2018-07-30 04:07:02 +02:00
int channelPts = channelsPts.get(channelId);
if (channelPts == 0) {
2019-07-18 15:01:39 +02:00
channelPts = getMessagesStorage().getChannelPtsSync(channelId);
2016-03-16 13:26:32 +01:00
if (channelPts == 0) {
for (int c = 0; c < updates.chats.size(); c++) {
TLRPC.Chat chat = updates.chats.get(c);
if (chat.id == channelId) {
loadUnknownChannel(chat, 0);
skipUpdate = true;
break;
}
}
} else {
channelsPts.put(channelId, channelPts);
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
}
TLRPC.TL_updates updatesNew = new TLRPC.TL_updates();
updatesNew.updates.add(update);
2018-07-30 04:07:02 +02:00
updatesNew.pts = getUpdatePts(update);
updatesNew.pts_count = getUpdatePtsCount(update);
2016-03-16 13:26:32 +01:00
for (int b = a + 1; b < updates.updates.size(); b++) {
TLRPC.Update update2 = updates.updates.get(b);
2018-07-30 04:07:02 +02:00
int pts2 = getUpdatePts(update2);
int count2 = getUpdatePtsCount(update2);
if (getUpdateType(update2) == 2 && channelId == getUpdateChannelId(update2) && updatesNew.pts + count2 == pts2) {
2016-03-16 13:26:32 +01:00
updatesNew.updates.add(update2);
2018-07-30 04:07:02 +02:00
updatesNew.pts = pts2;
updatesNew.pts_count += count2;
2016-03-16 13:26:32 +01:00
updates.updates.remove(b);
b--;
} else {
break;
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
}
if (!skipUpdate) {
if (channelPts + updatesNew.pts_count == updatesNew.pts) {
2019-07-18 15:01:39 +02:00
if (!processUpdateArray(updatesNew.updates, updates.users, updates.chats, false, updates.date)) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("need get channel diff inner TL_updates, channel_id = " + channelId);
}
if (needGetChannelsDiff == null) {
needGetChannelsDiff = new ArrayList<>();
} else if (!needGetChannelsDiff.contains(channelId)) {
needGetChannelsDiff.add(channelId);
}
} else {
channelsPts.put(channelId, updatesNew.pts);
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveChannelPts(channelId, updatesNew.pts);
2016-03-16 13:26:32 +01:00
}
} else if (channelPts != updatesNew.pts) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d(update + " need get channel diff, pts: " + channelPts + " " + updatesNew.pts + " count = " + updatesNew.pts_count + " channelId = " + channelId);
2016-03-16 13:26:32 +01:00
}
2018-07-30 04:07:02 +02:00
long updatesStartWaitTime = updatesStartWaitTimeChannels.get(channelId);
boolean gettingDifferenceChannel = gettingDifferenceChannels.get(channelId);
if (gettingDifferenceChannel || updatesStartWaitTime == 0 || Math.abs(System.currentTimeMillis() - updatesStartWaitTime) <= 1500) {
if (updatesStartWaitTime == 0) {
updatesStartWaitTimeChannels.put(channelId, System.currentTimeMillis());
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("add to queue");
}
ArrayList<TLRPC.Updates> arrayList = updatesQueueChannels.get(channelId);
if (arrayList == null) {
arrayList = new ArrayList<>();
updatesQueueChannels.put(channelId, arrayList);
}
arrayList.add(updatesNew);
} else {
if (needGetChannelsDiff == null) {
needGetChannelsDiff = new ArrayList<>();
} else if (!needGetChannelsDiff.contains(channelId)) {
needGetChannelsDiff.add(channelId);
}
2016-03-16 13:26:32 +01:00
}
2015-09-24 22:52:02 +02:00
}
} else {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("need load unknown channel = " + channelId);
}
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
} else {
break;
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
updates.updates.remove(a);
a--;
}
2016-03-16 13:26:32 +01:00
boolean processUpdate;
if (updates instanceof TLRPC.TL_updatesCombined) {
2019-07-18 15:01:39 +02:00
processUpdate = getMessagesStorage().getLastSeqValue() + 1 == updates.seq_start || getMessagesStorage().getLastSeqValue() == updates.seq_start;
} else {
2019-07-18 15:01:39 +02:00
processUpdate = getMessagesStorage().getLastSeqValue() + 1 == updates.seq || updates.seq == 0 || updates.seq == getMessagesStorage().getLastSeqValue();
}
2016-03-16 13:26:32 +01:00
if (processUpdate) {
2019-07-18 15:01:39 +02:00
processUpdateArray(updates.updates, updates.users, updates.chats, false, updates.date);
2016-03-16 13:26:32 +01:00
if (updates.seq != 0) {
2017-07-08 18:32:04 +02:00
if (updates.date != 0) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastDateValue(updates.date);
2017-07-08 18:32:04 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastSeqValue(updates.seq);
2013-10-25 17:19:00 +02:00
}
} else {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
if (updates instanceof TLRPC.TL_updatesCombined) {
2019-07-18 15:01:39 +02:00
FileLog.d("need get diff TL_updatesCombined, seq: " + getMessagesStorage().getLastSeqValue() + " " + updates.seq_start);
2018-07-30 04:07:02 +02:00
} else {
2019-07-18 15:01:39 +02:00
FileLog.d("need get diff TL_updates, seq: " + getMessagesStorage().getLastSeqValue() + " " + updates.seq);
2018-07-30 04:07:02 +02:00
}
2016-03-16 13:26:32 +01:00
}
if (gettingDifference || updatesStartWaitTimeSeq == 0 || Math.abs(System.currentTimeMillis() - updatesStartWaitTimeSeq) <= 1500) {
if (updatesStartWaitTimeSeq == 0) {
updatesStartWaitTimeSeq = System.currentTimeMillis();
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("add TL_updates/Combined to queue");
}
2016-03-16 13:26:32 +01:00
updatesQueueSeq.add(updates);
} else {
needGetDiff = true;
}
}
2013-10-25 17:19:00 +02:00
}
} else if (updates instanceof TLRPC.TL_updatesTooLong) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("need get diff TL_updatesTooLong");
}
2013-10-25 17:19:00 +02:00
needGetDiff = true;
} else if (updates instanceof UserActionUpdatesSeq) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastSeqValue(updates.seq);
} else if (updates instanceof UserActionUpdatesPts) {
2015-09-24 22:52:02 +02:00
if (updates.chat_id != 0) {
channelsPts.put(updates.chat_id, updates.pts);
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveChannelPts(updates.chat_id, updates.pts);
2015-09-24 22:52:02 +02:00
} else {
2019-07-18 15:01:39 +02:00
getMessagesStorage().setLastPtsValue(updates.pts);
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getSecretChatHelper().processPendingEncMessages();
if (!fromQueue) {
2018-07-30 04:07:02 +02:00
for (int a = 0; a < updatesQueueChannels.size(); a++) {
int key = updatesQueueChannels.keyAt(a);
2015-09-24 22:52:02 +02:00
if (needGetChannelsDiff != null && needGetChannelsDiff.contains(key)) {
getChannelDifference(key);
} else {
processChannelsUpdatesQueue(key, 0);
}
}
if (needGetDiff) {
getDifference();
} else {
for (int a = 0; a < 3; a++) {
2015-09-24 22:52:02 +02:00
processUpdatesQueue(a, 0);
}
}
2013-10-25 17:19:00 +02:00
}
if (needReceivedQueue) {
TLRPC.TL_messages_receivedQueue req = new TLRPC.TL_messages_receivedQueue();
2019-07-18 15:01:39 +02:00
req.max_qts = getMessagesStorage().getLastQtsValue();
getConnectionsManager().sendRequest(req, (response, error) -> {
2013-10-25 17:19:00 +02:00
});
2013-10-25 17:19:00 +02:00
}
2014-11-21 01:14:44 +01:00
if (updateStatus) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS));
2014-11-21 01:14:44 +01:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().saveDiffParams(getMessagesStorage().getLastSeqValue(), getMessagesStorage().getLastPtsValue(), getMessagesStorage().getLastDateValue(), getMessagesStorage().getLastQtsValue());
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
public boolean processUpdateArray(ArrayList<TLRPC.Update> updates, final ArrayList<TLRPC.User> usersArr, final ArrayList<TLRPC.Chat> chatsArr, boolean fromGetDifference, int date) {
2013-10-25 17:19:00 +02:00
if (updates.isEmpty()) {
2015-09-24 22:52:02 +02:00
if (usersArr != null || chatsArr != null) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(usersArr, false);
putChats(chatsArr, false);
2015-09-24 22:52:02 +02:00
});
}
2013-10-25 17:19:00 +02:00
return true;
}
long currentTime = System.currentTimeMillis();
boolean printChanged = false;
2018-07-30 04:07:02 +02:00
LongSparseArray<ArrayList<MessageObject>> messages = null;
LongSparseArray<TLRPC.WebPage> webPages = null;
ArrayList<MessageObject> pushMessages = null;
ArrayList<TLRPC.Message> messagesArr = null;
LongSparseArray<ArrayList<MessageObject>> editingMessages = null;
SparseArray<SparseIntArray> channelViews = null;
SparseLongArray markAsReadMessagesInbox = null;
SparseLongArray markAsReadMessagesOutbox = null;
ArrayList<Long> markAsReadMessages = null;
SparseIntArray markAsReadEncrypted = null;
SparseArray<ArrayList<Integer>> deletedMessages = null;
SparseIntArray clearHistoryMessages = null;
ArrayList<TLRPC.ChatParticipants> chatInfoToUpdate = null;
ArrayList<TLRPC.Update> updatesOnMainThread = null;
ArrayList<TLRPC.TL_updateEncryptedMessagesRead> tasks = null;
ArrayList<Integer> contactsIds = null;
2013-10-25 17:19:00 +02:00
boolean checkForUsers = true;
ConcurrentHashMap<Integer, TLRPC.User> usersDict;
ConcurrentHashMap<Integer, TLRPC.Chat> chatsDict;
if (usersArr != null) {
2015-01-02 23:15:07 +01:00
usersDict = new ConcurrentHashMap<>();
2018-07-30 04:07:02 +02:00
for (int a = 0, size = usersArr.size(); a < size; a++) {
2016-04-22 15:49:00 +02:00
TLRPC.User user = usersArr.get(a);
2013-10-25 17:19:00 +02:00
usersDict.put(user.id, user);
}
} else {
checkForUsers = false;
usersDict = users;
}
if (chatsArr != null) {
2015-01-02 23:15:07 +01:00
chatsDict = new ConcurrentHashMap<>();
2018-07-30 04:07:02 +02:00
for (int a = 0, size = chatsArr.size(); a < size; a++) {
2016-04-22 15:49:00 +02:00
TLRPC.Chat chat = chatsArr.get(a);
2013-10-25 17:19:00 +02:00
chatsDict.put(chat.id, chat);
}
} else {
checkForUsers = false;
chatsDict = chats;
}
if (fromGetDifference) {
checkForUsers = false;
}
2013-10-25 17:19:00 +02:00
if (usersArr != null || chatsArr != null) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
putUsers(usersArr, false);
putChats(chatsArr, false);
});
}
int interfaceUpdateMask = 0;
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
for (int c = 0, size3 = updates.size(); c < size3; c++) {
TLRPC.Update baseUpdate = updates.get(c);
if (BuildVars.LOGS_ENABLED) {
FileLog.d("process update " + baseUpdate);
}
if (baseUpdate instanceof TLRPC.TL_updateNewMessage || baseUpdate instanceof TLRPC.TL_updateNewChannelMessage) {
2015-09-24 22:52:02 +02:00
TLRPC.Message message;
2018-07-30 04:07:02 +02:00
if (baseUpdate instanceof TLRPC.TL_updateNewMessage) {
message = ((TLRPC.TL_updateNewMessage) baseUpdate).message;
2015-09-24 22:52:02 +02:00
} else {
2018-07-30 04:07:02 +02:00
message = ((TLRPC.TL_updateNewChannelMessage) baseUpdate).message;
if (BuildVars.LOGS_ENABLED) {
FileLog.d(baseUpdate + " channelId = " + message.to_id.channel_id);
}
2019-07-18 15:01:39 +02:00
if (!message.out && message.from_id == getUserConfig().getClientUserId()) {
2016-04-22 15:49:00 +02:00
message.out = true;
}
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
TLRPC.Chat chat = null;
2016-10-11 13:57:01 +02:00
int chat_id = 0;
int user_id = 0;
if (message.to_id.channel_id != 0) {
chat_id = message.to_id.channel_id;
} else if (message.to_id.chat_id != 0) {
chat_id = message.to_id.chat_id;
} else if (message.to_id.user_id != 0) {
user_id = message.to_id.user_id;
}
if (chat_id != 0) {
chat = chatsDict.get(chat_id);
if (chat == null) {
chat = getChat(chat_id);
2015-11-26 22:04:02 +01:00
}
2016-10-11 13:57:01 +02:00
if (chat == null) {
2019-07-18 15:01:39 +02:00
chat = getMessagesStorage().getChatSync(chat_id);
2016-10-11 13:57:01 +02:00
putChat(chat, true);
}
}
if (checkForUsers) {
2015-11-26 22:04:02 +01:00
if (chat_id != 0) {
if (chat == null) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("not found chat " + chat_id);
}
2015-10-29 18:10:07 +01:00
return false;
}
2015-09-24 22:52:02 +02:00
}
2016-04-22 15:49:00 +02:00
2016-05-25 23:49:47 +02:00
int count = 3 + message.entities.size();
for (int a = 0; a < count; a++) {
boolean allowMin = false;
2016-04-22 15:49:00 +02:00
if (a != 0) {
2016-05-25 23:49:47 +02:00
if (a == 1) {
user_id = message.from_id;
if (message.post) {
allowMin = true;
}
2016-05-25 23:49:47 +02:00
} else if (a == 2) {
user_id = message.fwd_from != null ? message.fwd_from.from_id : 0;
} else {
TLRPC.MessageEntity entity = message.entities.get(a - 3);
user_id = entity instanceof TLRPC.TL_messageEntityMentionName ? ((TLRPC.TL_messageEntityMentionName) entity).user_id : 0;
}
2015-09-24 22:52:02 +02:00
}
2016-04-22 15:49:00 +02:00
if (user_id > 0) {
TLRPC.User user = usersDict.get(user_id);
if (user == null || !allowMin && user.min) {
2016-04-22 15:49:00 +02:00
user = getUser(user_id);
}
if (user == null || !allowMin && user.min) {
2019-07-18 15:01:39 +02:00
user = getMessagesStorage().getUserSync(user_id);
if (user != null && !allowMin && user.min) {
2016-04-22 15:49:00 +02:00
user = null;
}
putUser(user, true);
}
if (user == null) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("not found user " + user_id);
}
2016-04-22 15:49:00 +02:00
return false;
}
2019-07-18 15:01:39 +02:00
if (a == 1 && user.status != null && user.status.expires <= 0 && Math.abs(getConnectionsManager().getCurrentTime() - message.date) < 30) {
onlinePrivacy.put(user_id, message.date);
2016-04-22 15:49:00 +02:00
interfaceUpdateMask |= UPDATE_MASK_STATUS;
}
2015-09-24 22:52:02 +02:00
}
2014-11-21 01:14:44 +01:00
}
2013-10-25 17:19:00 +02:00
}
2016-10-11 13:57:01 +02:00
if (chat != null && chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
2015-11-26 22:04:02 +01:00
if (message.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
TLRPC.User user = usersDict.get(message.action.user_id);
if (user != null && user.bot) {
message.reply_markup = new TLRPC.TL_replyKeyboardHide();
2017-07-08 18:32:04 +02:00
message.flags |= 64;
2019-07-18 15:01:39 +02:00
} else if (message.from_id == getUserConfig().getClientUserId() && message.action.user_id == getUserConfig().getClientUserId()) {
2015-11-26 22:04:02 +01:00
continue;
}
}
2018-07-30 04:07:02 +02:00
if (messagesArr == null) {
messagesArr = new ArrayList<>();
}
2015-09-24 22:52:02 +02:00
messagesArr.add(message);
ImageLoader.saveMessageThumbs(message);
2019-07-18 15:01:39 +02:00
int clientUserId = getUserConfig().getClientUserId();
2015-09-24 22:52:02 +02:00
if (message.to_id.chat_id != 0) {
message.dialog_id = -message.to_id.chat_id;
} else if (message.to_id.channel_id != 0) {
message.dialog_id = -message.to_id.channel_id;
2013-10-25 17:19:00 +02:00
} else {
2016-10-11 13:57:01 +02:00
if (message.to_id.user_id == clientUserId) {
2015-09-24 22:52:02 +02:00
message.to_id.user_id = message.from_id;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
message.dialog_id = message.to_id.user_id;
2013-10-25 17:19:00 +02:00
}
ConcurrentHashMap<Long, Integer> read_max = message.out ? dialogs_read_outbox_max : dialogs_read_inbox_max;
Integer value = read_max.get(message.dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
read_max.put(message.dialog_id, value);
}
message.unread = !(value >= message.id || chat != null && ChatObject.isNotInChat(chat) || message.action instanceof TLRPC.TL_messageActionChatMigrateTo || message.action instanceof TLRPC.TL_messageActionChannelCreate);
2016-10-11 13:57:01 +02:00
if (message.dialog_id == clientUserId) {
message.unread = false;
message.media_unread = false;
message.out = true;
}
2018-07-30 04:07:02 +02:00
MessageObject obj = new MessageObject(currentAccount, message, usersDict, chatsDict, createdDialogIds.contains(message.dialog_id));
2016-03-06 02:49:31 +01:00
if (obj.type == 11) {
interfaceUpdateMask |= UPDATE_MASK_CHAT_AVATAR;
} else if (obj.type == 10) {
interfaceUpdateMask |= UPDATE_MASK_CHAT_NAME;
}
2018-07-30 04:07:02 +02:00
if (messages == null) {
messages = new LongSparseArray<>();
}
2015-09-24 22:52:02 +02:00
ArrayList<MessageObject> arr = messages.get(message.dialog_id);
2013-10-25 17:19:00 +02:00
if (arr == null) {
2015-01-02 23:15:07 +01:00
arr = new ArrayList<>();
2015-09-24 22:52:02 +02:00
messages.put(message.dialog_id, arr);
2013-10-25 17:19:00 +02:00
}
arr.add(obj);
if (!obj.isOut() && obj.isUnread()) {
2018-07-30 04:07:02 +02:00
if (pushMessages == null) {
pushMessages = new ArrayList<>();
}
2014-07-10 02:15:58 +02:00
pushMessages.add(obj);
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateReadMessagesContents) {
TLRPC.TL_updateReadMessagesContents update = (TLRPC.TL_updateReadMessagesContents) baseUpdate;
if (markAsReadMessages == null) {
markAsReadMessages = new ArrayList<>();
}
for (int a = 0, size = update.messages.size(); a < size; a++) {
2015-09-24 22:52:02 +02:00
long id = update.messages.get(a);
markAsReadMessages.add(id);
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateChannelReadMessagesContents) {
TLRPC.TL_updateChannelReadMessagesContents update = (TLRPC.TL_updateChannelReadMessagesContents) baseUpdate;
if (markAsReadMessages == null) {
markAsReadMessages = new ArrayList<>();
}
for (int a = 0, size = update.messages.size(); a < size; a++) {
2017-12-08 18:35:59 +01:00
long id = update.messages.get(a);
id |= ((long) update.channel_id) << 32;
markAsReadMessages.add(id);
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateReadHistoryInbox) {
TLRPC.TL_updateReadHistoryInbox update = (TLRPC.TL_updateReadHistoryInbox) baseUpdate;
long dialog_id;
2018-07-30 04:07:02 +02:00
if (markAsReadMessagesInbox == null) {
markAsReadMessagesInbox = new SparseLongArray();
}
if (update.peer.chat_id != 0) {
markAsReadMessagesInbox.put(-update.peer.chat_id, (long) update.max_id);
dialog_id = -update.peer.chat_id;
} else {
2018-07-30 04:07:02 +02:00
markAsReadMessagesInbox.put(update.peer.user_id, (long) update.max_id);
dialog_id = update.peer.user_id;
}
Integer value = dialogs_read_inbox_max.get(dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-07-30 04:07:02 +02:00
}
dialogs_read_inbox_max.put(dialog_id, Math.max(value, update.max_id));
} else if (baseUpdate instanceof TLRPC.TL_updateReadHistoryOutbox) {
TLRPC.TL_updateReadHistoryOutbox update = (TLRPC.TL_updateReadHistoryOutbox) baseUpdate;
long dialog_id;
if (markAsReadMessagesOutbox == null) {
markAsReadMessagesOutbox = new SparseLongArray();
}
if (update.peer.chat_id != 0) {
markAsReadMessagesOutbox.put(-update.peer.chat_id, (long) update.max_id);
dialog_id = -update.peer.chat_id;
} else {
markAsReadMessagesOutbox.put(update.peer.user_id, (long) update.max_id);
dialog_id = update.peer.user_id;
}
2018-07-30 04:07:02 +02:00
Integer value = dialogs_read_outbox_max.get(dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(true, dialog_id);
2018-07-30 04:07:02 +02:00
}
dialogs_read_outbox_max.put(dialog_id, Math.max(value, update.max_id));
} else if (baseUpdate instanceof TLRPC.TL_updateDeleteMessages) {
TLRPC.TL_updateDeleteMessages update = (TLRPC.TL_updateDeleteMessages) baseUpdate;
if (deletedMessages == null) {
deletedMessages = new SparseArray<>();
}
2015-09-24 22:52:02 +02:00
ArrayList<Integer> arrayList = deletedMessages.get(0);
if (arrayList == null) {
arrayList = new ArrayList<>();
deletedMessages.put(0, arrayList);
}
arrayList.addAll(update.messages);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateUserTyping || baseUpdate instanceof TLRPC.TL_updateChatUserTyping) {
int user_id;
int chat_id;
TLRPC.SendMessageAction action;
if (baseUpdate instanceof TLRPC.TL_updateUserTyping) {
TLRPC.TL_updateUserTyping update = (TLRPC.TL_updateUserTyping) baseUpdate;
user_id = update.user_id;
action = update.action;
chat_id = 0;
} else {
TLRPC.TL_updateChatUserTyping update = (TLRPC.TL_updateChatUserTyping) baseUpdate;
chat_id = update.chat_id;
user_id = update.user_id;
action = update.action;
}
2019-07-18 15:01:39 +02:00
if (user_id != getUserConfig().getClientUserId()) {
2018-07-30 04:07:02 +02:00
long uid = -chat_id;
2013-10-25 17:19:00 +02:00
if (uid == 0) {
2018-07-30 04:07:02 +02:00
uid = user_id;
2013-10-25 17:19:00 +02:00
}
ArrayList<PrintingUser> arr = printingUsers.get(uid);
2018-07-30 04:07:02 +02:00
if (action instanceof TLRPC.TL_sendMessageCancelAction) {
if (arr != null) {
2018-07-30 04:07:02 +02:00
for (int a = 0, size = arr.size(); a < size; a++) {
PrintingUser pu = arr.get(a);
2018-07-30 04:07:02 +02:00
if (pu.userId == user_id) {
arr.remove(a);
printChanged = true;
break;
}
}
if (arr.isEmpty()) {
printingUsers.remove(uid);
}
}
} else {
if (arr == null) {
arr = new ArrayList<>();
printingUsers.put(uid, arr);
}
boolean exist = false;
for (PrintingUser u : arr) {
2018-07-30 04:07:02 +02:00
if (u.userId == user_id) {
exist = true;
u.lastTime = currentTime;
2018-07-30 04:07:02 +02:00
if (u.action.getClass() != action.getClass()) {
2015-06-29 19:12:11 +02:00
printChanged = true;
}
2018-07-30 04:07:02 +02:00
u.action = action;
break;
}
}
if (!exist) {
PrintingUser newUser = new PrintingUser();
2018-07-30 04:07:02 +02:00
newUser.userId = user_id;
newUser.lastTime = currentTime;
2018-07-30 04:07:02 +02:00
newUser.action = action;
arr.add(newUser);
printChanged = true;
2013-10-25 17:19:00 +02:00
}
}
2019-07-18 15:01:39 +02:00
if (Math.abs(getConnectionsManager().getCurrentTime() - date) < 30) {
onlinePrivacy.put(user_id, date);
}
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateChatParticipants) {
TLRPC.TL_updateChatParticipants update = (TLRPC.TL_updateChatParticipants) baseUpdate;
interfaceUpdateMask |= UPDATE_MASK_CHAT_MEMBERS;
2018-07-30 04:07:02 +02:00
if (chatInfoToUpdate == null) {
chatInfoToUpdate = new ArrayList<>();
}
2013-10-25 17:19:00 +02:00
chatInfoToUpdate.add(update.participants);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateUserStatus) {
interfaceUpdateMask |= UPDATE_MASK_STATUS;
2018-07-30 04:07:02 +02:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateUserName) {
interfaceUpdateMask |= UPDATE_MASK_NAME;
2018-07-30 04:07:02 +02:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateUserPhoto) {
TLRPC.TL_updateUserPhoto update = (TLRPC.TL_updateUserPhoto) baseUpdate;
interfaceUpdateMask |= UPDATE_MASK_AVATAR;
2019-07-18 15:01:39 +02:00
getMessagesStorage().clearUserPhotos(update.user_id);
2018-07-30 04:07:02 +02:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateUserPhone) {
interfaceUpdateMask |= UPDATE_MASK_PHONE;
2018-07-30 04:07:02 +02:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
2019-07-18 15:01:39 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updatePeerSettings) {
TLRPC.TL_updatePeerSettings update = (TLRPC.TL_updatePeerSettings) baseUpdate;
2018-07-30 04:07:02 +02:00
if (contactsIds == null) {
contactsIds = new ArrayList<>();
}
2019-07-18 15:01:39 +02:00
if (update.peer instanceof TLRPC.TL_peerUser) {
TLRPC.User user = usersDict.get(update.peer.user_id);
if (user != null) {
if (user.contact) {
int idx = contactsIds.indexOf(-update.peer.user_id);
if (idx != -1) {
contactsIds.remove(idx);
}
if (!contactsIds.contains(update.peer.user_id)) {
contactsIds.add(update.peer.user_id);
}
} else {
int idx = contactsIds.indexOf(update.peer.user_id);
if (idx != -1) {
contactsIds.remove(idx);
}
if (!contactsIds.contains(update.peer.user_id)) {
contactsIds.add(-update.peer.user_id);
}
}
}
}
2019-07-18 15:01:39 +02:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateNewEncryptedMessage) {
2019-07-18 15:01:39 +02:00
ArrayList<TLRPC.Message> decryptedMessages = getSecretChatHelper().decryptMessage(((TLRPC.TL_updateNewEncryptedMessage) baseUpdate).message);
2014-10-28 22:27:44 +01:00
if (decryptedMessages != null && !decryptedMessages.isEmpty()) {
2018-07-30 04:07:02 +02:00
int cid = ((TLRPC.TL_updateNewEncryptedMessage) baseUpdate).message.chat_id;
2014-10-28 22:27:44 +01:00
long uid = ((long) cid) << 32;
2018-07-30 04:07:02 +02:00
if (messages == null) {
messages = new LongSparseArray<>();
}
2013-10-25 17:19:00 +02:00
ArrayList<MessageObject> arr = messages.get(uid);
if (arr == null) {
2015-01-02 23:15:07 +01:00
arr = new ArrayList<>();
2013-10-25 17:19:00 +02:00
messages.put(uid, arr);
}
2018-07-30 04:07:02 +02:00
for (int a = 0, size = decryptedMessages.size(); a < size; a++) {
2016-03-06 02:49:31 +01:00
TLRPC.Message message = decryptedMessages.get(a);
2015-02-01 19:51:02 +01:00
ImageLoader.saveMessageThumbs(message);
2018-07-30 04:07:02 +02:00
if (messagesArr == null) {
messagesArr = new ArrayList<>();
}
2014-10-28 22:27:44 +01:00
messagesArr.add(message);
2018-07-30 04:07:02 +02:00
MessageObject obj = new MessageObject(currentAccount, message, usersDict, chatsDict, createdDialogIds.contains(uid));
2014-10-28 22:27:44 +01:00
arr.add(obj);
2018-07-30 04:07:02 +02:00
if (pushMessages == null) {
pushMessages = new ArrayList<>();
}
2014-10-28 22:27:44 +01:00
pushMessages.add(obj);
}
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateEncryptedChatTyping) {
TLRPC.TL_updateEncryptedChatTyping update = (TLRPC.TL_updateEncryptedChatTyping) baseUpdate;
2017-03-31 01:58:05 +02:00
TLRPC.EncryptedChat encryptedChat = getEncryptedChatDB(update.chat_id, true);
if (encryptedChat != null) {
long uid = ((long) update.chat_id) << 32;
ArrayList<PrintingUser> arr = printingUsers.get(uid);
if (arr == null) {
2015-01-02 23:15:07 +01:00
arr = new ArrayList<>();
printingUsers.put(uid, arr);
}
boolean exist = false;
2018-07-30 04:07:02 +02:00
for (int a = 0, size = arr.size(); a < size; a++) {
PrintingUser u = arr.get(a);
if (u.userId == encryptedChat.user_id) {
exist = true;
u.lastTime = currentTime;
u.action = new TLRPC.TL_sendMessageTypingAction();
break;
}
}
if (!exist) {
PrintingUser newUser = new PrintingUser();
2018-07-30 04:07:02 +02:00
newUser.userId = encryptedChat.user_id;
newUser.lastTime = currentTime;
newUser.action = new TLRPC.TL_sendMessageTypingAction();
arr.add(newUser);
printChanged = true;
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
if (Math.abs(getConnectionsManager().getCurrentTime() - date) < 30) {
onlinePrivacy.put(encryptedChat.user_id, date);
}
2018-07-30 04:07:02 +02:00
}
} else if (baseUpdate instanceof TLRPC.TL_updateEncryptedMessagesRead) {
TLRPC.TL_updateEncryptedMessagesRead update = (TLRPC.TL_updateEncryptedMessagesRead) baseUpdate;
if (markAsReadEncrypted == null) {
markAsReadEncrypted = new SparseIntArray();
2013-10-25 17:19:00 +02:00
}
2019-01-23 18:03:33 +01:00
markAsReadEncrypted.put(update.chat_id, update.max_date);
2018-07-30 04:07:02 +02:00
if (tasks == null) {
tasks = new ArrayList<>();
}
tasks.add((TLRPC.TL_updateEncryptedMessagesRead) baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateChatParticipantAdd) {
TLRPC.TL_updateChatParticipantAdd update = (TLRPC.TL_updateChatParticipantAdd) baseUpdate;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(update.chat_id, update.user_id, 0, update.inviter_id, update.version);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateChatParticipantDelete) {
TLRPC.TL_updateChatParticipantDelete update = (TLRPC.TL_updateChatParticipantDelete) baseUpdate;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(update.chat_id, update.user_id, 1, 0, update.version);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateDcOptions || baseUpdate instanceof TLRPC.TL_updateConfig) {
2019-07-18 15:01:39 +02:00
getConnectionsManager().updateDcSettings();
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateEncryption) {
2019-07-18 15:01:39 +02:00
getSecretChatHelper().processUpdateEncryption((TLRPC.TL_updateEncryption) baseUpdate, usersDict);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateUserBlocked) {
final TLRPC.TL_updateUserBlocked finalUpdate = (TLRPC.TL_updateUserBlocked) baseUpdate;
if (finalUpdate.blocked) {
2018-08-27 10:33:11 +02:00
SparseIntArray ids = new SparseIntArray();
ids.put(finalUpdate.user_id, 1);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putBlockedUsers(ids, false);
} else {
2019-07-18 15:01:39 +02:00
getMessagesStorage().deleteBlockedUser(finalUpdate.user_id);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
if (finalUpdate.blocked) {
if (blockedUsers.indexOfKey(finalUpdate.user_id) < 0) {
blockedUsers.put(finalUpdate.user_id, 1);
}
} else {
blockedUsers.delete(finalUpdate.user_id);
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.blockedUsersDidLoad);
2018-08-27 10:33:11 +02:00
}));
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateNotifySettings) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateServiceNotification) {
final TLRPC.TL_updateServiceNotification update = (TLRPC.TL_updateServiceNotification) baseUpdate;
if (update.popup && update.message != null && update.message.length() > 0) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.needShowAlert, 2, update.message, update.type));
2014-10-31 20:02:29 +01:00
}
2018-07-30 04:07:02 +02:00
if ((update.flags & 2) != 0) {
2017-03-31 01:58:05 +02:00
TLRPC.TL_message newMessage = new TLRPC.TL_message();
2019-07-18 15:01:39 +02:00
newMessage.local_id = newMessage.id = getUserConfig().getNewMessageId();
getUserConfig().saveConfig(false);
2017-03-31 01:58:05 +02:00
newMessage.unread = true;
newMessage.flags = TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
2018-07-30 04:07:02 +02:00
if (update.inbox_date != 0) {
newMessage.date = update.inbox_date;
2017-07-08 18:32:04 +02:00
} else {
newMessage.date = (int) (System.currentTimeMillis() / 1000);
}
2017-03-31 01:58:05 +02:00
newMessage.from_id = 777000;
newMessage.to_id = new TLRPC.TL_peerUser();
2019-07-18 15:01:39 +02:00
newMessage.to_id.user_id = getUserConfig().getClientUserId();
2017-03-31 01:58:05 +02:00
newMessage.dialog_id = 777000;
if (update.media != null) {
newMessage.media = update.media;
newMessage.flags |= TLRPC.MESSAGE_FLAG_HAS_MEDIA;
}
2018-07-30 04:07:02 +02:00
newMessage.message = update.message;
if (update.entities != null) {
newMessage.entities = update.entities;
2019-07-18 15:01:39 +02:00
newMessage.flags |= 128;
2017-03-31 01:58:05 +02:00
}
2018-07-30 04:07:02 +02:00
if (messagesArr == null) {
messagesArr = new ArrayList<>();
}
2017-03-31 01:58:05 +02:00
messagesArr.add(newMessage);
2018-07-30 04:07:02 +02:00
MessageObject obj = new MessageObject(currentAccount, newMessage, usersDict, chatsDict, createdDialogIds.contains(newMessage.dialog_id));
if (messages == null) {
messages = new LongSparseArray<>();
}
2017-03-31 01:58:05 +02:00
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
if (arr == null) {
arr = new ArrayList<>();
messages.put(newMessage.dialog_id, arr);
}
arr.add(obj);
2018-07-30 04:07:02 +02:00
if (pushMessages == null) {
pushMessages = new ArrayList<>();
}
2017-03-31 01:58:05 +02:00
pushMessages.add(obj);
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateDialogPinned) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updatePinnedDialogs) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
2019-05-14 14:08:05 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateFolderPeers) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
TLRPC.TL_updateFolderPeers update = (TLRPC.TL_updateFolderPeers) baseUpdate;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogsFolderId(update.folder_peers, null, 0, 0);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updatePrivacy) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateWebPage) {
TLRPC.TL_updateWebPage update = (TLRPC.TL_updateWebPage) baseUpdate;
if (webPages == null) {
webPages = new LongSparseArray<>();
}
2017-03-31 01:58:05 +02:00
webPages.put(update.webpage.id, update.webpage);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateChannelWebPage) {
TLRPC.TL_updateChannelWebPage update = (TLRPC.TL_updateChannelWebPage) baseUpdate;
if (webPages == null) {
webPages = new LongSparseArray<>();
}
2018-07-30 04:07:02 +02:00
webPages.put(update.webpage.id, update.webpage);
} else if (baseUpdate instanceof TLRPC.TL_updateChannelTooLong) {
TLRPC.TL_updateChannelTooLong update = (TLRPC.TL_updateChannelTooLong) baseUpdate;
if (BuildVars.LOGS_ENABLED) {
FileLog.d(baseUpdate + " channelId = " + update.channel_id);
}
int channelPts = channelsPts.get(update.channel_id);
if (channelPts == 0) {
2019-07-18 15:01:39 +02:00
channelPts = getMessagesStorage().getChannelPtsSync(update.channel_id);
if (channelPts == 0) {
TLRPC.Chat chat = chatsDict.get(update.channel_id);
if (chat == null || chat.min) {
chat = getChat(update.channel_id);
}
if (chat == null || chat.min) {
2019-07-18 15:01:39 +02:00
chat = getMessagesStorage().getChatSync(update.channel_id);
putChat(chat, true);
2016-03-16 13:26:32 +01:00
}
if (chat != null && !chat.min) {
loadUnknownChannel(chat, 0);
}
} else {
2016-03-16 13:26:32 +01:00
channelsPts.put(update.channel_id, channelPts);
}
}
if (channelPts != 0) {
if ((update.flags & 1) != 0) {
if (update.pts > channelPts) {
getChannelDifference(update.channel_id);
}
} else {
2016-03-16 13:26:32 +01:00
getChannelDifference(update.channel_id);
}
2015-09-24 22:52:02 +02:00
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateReadChannelInbox) {
TLRPC.TL_updateReadChannelInbox update = (TLRPC.TL_updateReadChannelInbox) baseUpdate;
2015-09-24 22:52:02 +02:00
long message_id = update.max_id;
message_id |= ((long) update.channel_id) << 32;
2015-11-26 22:04:02 +01:00
long dialog_id = -update.channel_id;
2018-07-30 04:07:02 +02:00
if (markAsReadMessagesInbox == null) {
markAsReadMessagesInbox = new SparseLongArray();
}
2018-07-30 04:07:02 +02:00
markAsReadMessagesInbox.put(-update.channel_id, message_id);
Integer value = dialogs_read_inbox_max.get(dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(false, dialog_id);
2018-07-30 04:07:02 +02:00
}
dialogs_read_inbox_max.put(dialog_id, Math.max(value, update.max_id));
} else if (baseUpdate instanceof TLRPC.TL_updateReadChannelOutbox) {
TLRPC.TL_updateReadChannelOutbox update = (TLRPC.TL_updateReadChannelOutbox) baseUpdate;
long message_id = update.max_id;
message_id |= ((long) update.channel_id) << 32;
long dialog_id = -update.channel_id;
if (markAsReadMessagesOutbox == null) {
markAsReadMessagesOutbox = new SparseLongArray();
}
markAsReadMessagesOutbox.put(-update.channel_id, message_id);
Integer value = dialogs_read_outbox_max.get(dialog_id);
2015-11-26 22:04:02 +01:00
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(true, dialog_id);
2015-11-26 22:04:02 +01:00
}
2018-07-30 04:07:02 +02:00
dialogs_read_outbox_max.put(dialog_id, Math.max(value, update.max_id));
} else if (baseUpdate instanceof TLRPC.TL_updateDeleteChannelMessages) {
TLRPC.TL_updateDeleteChannelMessages update = (TLRPC.TL_updateDeleteChannelMessages) baseUpdate;
if (BuildVars.LOGS_ENABLED) {
FileLog.d(baseUpdate + " channelId = " + update.channel_id);
}
if (deletedMessages == null) {
deletedMessages = new SparseArray<>();
}
2015-09-24 22:52:02 +02:00
ArrayList<Integer> arrayList = deletedMessages.get(update.channel_id);
if (arrayList == null) {
arrayList = new ArrayList<>();
deletedMessages.put(update.channel_id, arrayList);
}
arrayList.addAll(update.messages);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateChannel) {
if (BuildVars.LOGS_ENABLED) {
TLRPC.TL_updateChannel update = (TLRPC.TL_updateChannel) baseUpdate;
FileLog.d(baseUpdate + " channelId = " + update.channel_id);
}
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
2018-07-30 04:07:02 +02:00
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateChannelMessageViews) {
TLRPC.TL_updateChannelMessageViews update = (TLRPC.TL_updateChannelMessageViews) baseUpdate;
if (BuildVars.LOGS_ENABLED) {
FileLog.d(baseUpdate + " channelId = " + update.channel_id);
}
if (channelViews == null) {
channelViews = new SparseArray<>();
}
2015-09-24 22:52:02 +02:00
SparseIntArray array = channelViews.get(update.channel_id);
if (array == null) {
array = new SparseIntArray();
channelViews.put(update.channel_id, array);
}
2018-07-30 04:07:02 +02:00
array.put(update.id, update.views);
} else if (baseUpdate instanceof TLRPC.TL_updateChatParticipantAdmin) {
TLRPC.TL_updateChatParticipantAdmin update = (TLRPC.TL_updateChatParticipantAdmin) baseUpdate;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatInfo(update.chat_id, update.user_id, 2, update.is_admin ? 1 : 0, update.version);
2019-01-23 18:03:33 +01:00
} else if (baseUpdate instanceof TLRPC.TL_updateChatDefaultBannedRights) {
TLRPC.TL_updateChatDefaultBannedRights update = (TLRPC.TL_updateChatDefaultBannedRights) baseUpdate;
int chatId;
if (update.peer.channel_id != 0) {
chatId = update.peer.channel_id;
} else {
chatId = update.peer.chat_id;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatDefaultBannedRights(chatId, update.default_banned_rights, update.version);
2018-07-30 04:07:02 +02:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateStickerSets) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateStickerSetsOrder) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateNewStickerSet) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateDraftMessage) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateSavedGifs) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateEditChannelMessage || baseUpdate instanceof TLRPC.TL_updateEditMessage) {
2016-04-22 15:49:00 +02:00
TLRPC.Message message;
2019-07-18 15:01:39 +02:00
int clientUserId = getUserConfig().getClientUserId();
2018-07-30 04:07:02 +02:00
if (baseUpdate instanceof TLRPC.TL_updateEditChannelMessage) {
message = ((TLRPC.TL_updateEditChannelMessage) baseUpdate).message;
2016-05-25 23:49:47 +02:00
TLRPC.Chat chat = chatsDict.get(message.to_id.channel_id);
if (chat == null) {
chat = getChat(message.to_id.channel_id);
}
if (chat == null) {
2019-07-18 15:01:39 +02:00
chat = getMessagesStorage().getChatSync(message.to_id.channel_id);
2016-05-25 23:49:47 +02:00
putChat(chat, true);
}
if (chat != null && chat.megagroup) {
message.flags |= TLRPC.MESSAGE_FLAG_MEGAGROUP;
}
2016-04-22 15:49:00 +02:00
} else {
2018-07-30 04:07:02 +02:00
message = ((TLRPC.TL_updateEditMessage) baseUpdate).message;
2016-10-11 13:57:01 +02:00
if (message.dialog_id == clientUserId) {
message.unread = false;
message.media_unread = false;
message.out = true;
}
2016-04-22 15:49:00 +02:00
}
2019-07-18 15:01:39 +02:00
if (!message.out && message.from_id == getUserConfig().getClientUserId()) {
2016-10-11 13:57:01 +02:00
message.out = true;
2016-05-25 23:49:47 +02:00
}
if (!fromGetDifference) {
2018-07-30 04:07:02 +02:00
for (int a = 0, count = message.entities.size(); a < count; a++) {
TLRPC.MessageEntity entity = message.entities.get(a);
if (entity instanceof TLRPC.TL_messageEntityMentionName) {
int user_id = ((TLRPC.TL_messageEntityMentionName) entity).user_id;
TLRPC.User user = usersDict.get(user_id);
if (user == null || user.min) {
user = getUser(user_id);
}
if (user == null || user.min) {
2019-07-18 15:01:39 +02:00
user = getMessagesStorage().getUserSync(user_id);
if (user != null && user.min) {
user = null;
}
putUser(user, true);
}
if (user == null) {
return false;
2016-05-25 23:49:47 +02:00
}
}
2016-04-22 15:49:00 +02:00
}
2016-03-06 02:49:31 +01:00
}
if (message.to_id.chat_id != 0) {
message.dialog_id = -message.to_id.chat_id;
} else if (message.to_id.channel_id != 0) {
message.dialog_id = -message.to_id.channel_id;
} else {
2019-07-18 15:01:39 +02:00
if (message.to_id.user_id == getUserConfig().getClientUserId()) {
2016-03-06 02:49:31 +01:00
message.to_id.user_id = message.from_id;
}
message.dialog_id = message.to_id.user_id;
}
2016-05-25 23:49:47 +02:00
ConcurrentHashMap<Long, Integer> read_max = message.out ? dialogs_read_outbox_max : dialogs_read_inbox_max;
Integer value = read_max.get(message.dialog_id);
if (value == null) {
2019-07-18 15:01:39 +02:00
value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
read_max.put(message.dialog_id, value);
2016-05-25 23:49:47 +02:00
}
message.unread = value < message.id;
2016-10-11 13:57:01 +02:00
if (message.dialog_id == clientUserId) {
message.out = true;
message.unread = false;
message.media_unread = false;
}
2018-07-30 04:07:02 +02:00
if (message.out && message.message == null) {
message.message = "";
2016-10-11 13:57:01 +02:00
message.attachPath = "";
}
2016-05-25 23:49:47 +02:00
ImageLoader.saveMessageThumbs(message);
2018-07-30 04:07:02 +02:00
MessageObject obj = new MessageObject(currentAccount, message, usersDict, chatsDict, createdDialogIds.contains(message.dialog_id));
2016-03-06 02:49:31 +01:00
2018-07-30 04:07:02 +02:00
if (editingMessages == null) {
editingMessages = new LongSparseArray<>();
}
2016-03-06 02:49:31 +01:00
ArrayList<MessageObject> arr = editingMessages.get(message.dialog_id);
if (arr == null) {
arr = new ArrayList<>();
editingMessages.put(message.dialog_id, arr);
}
arr.add(obj);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateChannelPinnedMessage) {
TLRPC.TL_updateChannelPinnedMessage update = (TLRPC.TL_updateChannelPinnedMessage) baseUpdate;
if (BuildVars.LOGS_ENABLED) {
FileLog.d(baseUpdate + " channelId = " + update.channel_id);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatPinnedMessage(update.channel_id, update.id);
2019-01-23 18:03:33 +01:00
} else if (baseUpdate instanceof TLRPC.TL_updateChatPinnedMessage) {
TLRPC.TL_updateChatPinnedMessage update = (TLRPC.TL_updateChatPinnedMessage) baseUpdate;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatPinnedMessage(update.chat_id, update.id);
2019-01-23 18:03:33 +01:00
} else if (baseUpdate instanceof TLRPC.TL_updateUserPinnedMessage) {
TLRPC.TL_updateUserPinnedMessage update = (TLRPC.TL_updateUserPinnedMessage) baseUpdate;
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateUserPinnedMessage(update.user_id, update.id);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateReadFeaturedStickers) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updatePhoneCall) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateLangPack) {
TLRPC.TL_updateLangPack update = (TLRPC.TL_updateLangPack) baseUpdate;
2019-01-23 18:03:33 +01:00
AndroidUtilities.runOnUIThread(() -> LocaleController.getInstance().saveRemoteLocaleStringsForCurrentLocale(update.difference, currentAccount));
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateLangPackTooLong) {
2019-01-23 18:03:33 +01:00
TLRPC.TL_updateLangPackTooLong update = (TLRPC.TL_updateLangPackTooLong) baseUpdate;
LocaleController.getInstance().reloadCurrentRemoteLocale(currentAccount, update.lang_code);
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateFavedStickers) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateContactsReset) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updateChannelAvailableMessages) {
TLRPC.TL_updateChannelAvailableMessages update = (TLRPC.TL_updateChannelAvailableMessages) baseUpdate;
if (clearHistoryMessages == null) {
clearHistoryMessages = new SparseIntArray();
}
int currentValue = clearHistoryMessages.get(update.channel_id);
if (currentValue == 0 || currentValue < update.available_min_id) {
2017-12-08 18:35:59 +01:00
clearHistoryMessages.put(update.channel_id, update.available_min_id);
}
2018-07-30 04:07:02 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateDialogUnreadMark) {
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
2019-01-23 18:03:33 +01:00
} else if (baseUpdate instanceof TLRPC.TL_updateMessagePoll) {
TLRPC.TL_updateMessagePoll update = (TLRPC.TL_updateMessagePoll) baseUpdate;
2019-07-18 15:01:39 +02:00
long time = getSendMessagesHelper().getVoteSendTime(update.poll_id);
2019-01-23 18:03:33 +01:00
if (Math.abs(SystemClock.uptimeMillis() - time) < 600) {
continue;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateMessagePollResults(update.poll_id, update.poll, update.results);
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
} else if (baseUpdate instanceof TLRPC.TL_updatePeerLocated) {
2019-01-23 18:03:33 +01:00
if (updatesOnMainThread == null) {
updatesOnMainThread = new ArrayList<>();
}
updatesOnMainThread.add(baseUpdate);
2013-10-25 17:19:00 +02:00
}
}
2018-07-30 04:07:02 +02:00
if (messages != null) {
for (int a = 0, size = messages.size(); a < size; a++) {
long key = messages.keyAt(a);
ArrayList<MessageObject> value = messages.valueAt(a);
if (updatePrintingUsersWithNewMessages(key, value)) {
printChanged = true;
2013-10-25 17:19:00 +02:00
}
}
}
if (printChanged) {
2013-10-25 17:19:00 +02:00
updatePrintingStrings();
}
final int interfaceUpdateMaskFinal = interfaceUpdateMask;
final boolean printChangedArg = printChanged;
2018-07-30 04:07:02 +02:00
if (contactsIds != null) {
2019-07-18 15:01:39 +02:00
getContactsController().processContactsUpdates(contactsIds, usersDict);
}
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
if (pushMessages != null) {
final ArrayList<MessageObject> pushMessagesFinal = pushMessages;
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> getNotificationsController().processNewMessages(pushMessagesFinal, true, false, null)));
2015-09-24 22:52:02 +02:00
}
2014-07-30 09:49:39 +02:00
2018-07-30 04:07:02 +02:00
if (messagesArr != null) {
2019-07-18 15:01:39 +02:00
getStatsController().incrementReceivedItemsCount(ApplicationLoader.getCurrentNetworkType(), StatsController.TYPE_MESSAGES, messagesArr.size());
getMessagesStorage().putMessages(messagesArr, true, true, false, getDownloadController().getAutodownloadMask());
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
if (editingMessages != null) {
for (int b = 0, size = editingMessages.size(); b < size; b++) {
2016-03-06 02:49:31 +01:00
TLRPC.TL_messages_messages messagesRes = new TLRPC.TL_messages_messages();
2018-07-30 04:07:02 +02:00
ArrayList<MessageObject> messageObjects = editingMessages.valueAt(b);
for (int a = 0, size2 = messageObjects.size(); a < size2; a++) {
2016-03-06 02:49:31 +01:00
messagesRes.messages.add(messageObjects.get(a).messageOwner);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(messagesRes, editingMessages.keyAt(b), -2, 0, false);
2016-03-06 02:49:31 +01:00
}
}
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
if (channelViews != null) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putChannelViews(channelViews, true);
2015-09-24 22:52:02 +02:00
}
2018-07-30 04:07:02 +02:00
final LongSparseArray<ArrayList<MessageObject>> editingMessagesFinal = editingMessages;
final SparseArray<SparseIntArray> channelViewsFinal = channelViews;
final LongSparseArray<TLRPC.WebPage> webPagesFinal = webPages;
final LongSparseArray<ArrayList<MessageObject>> messagesFinal = messages;
final ArrayList<TLRPC.ChatParticipants> chatInfoToUpdateFinal = chatInfoToUpdate;
final ArrayList<Integer> contactsIdsFinal = contactsIds;
final ArrayList<TLRPC.Update> updatesOnMainThreadFinal = updatesOnMainThread;
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
int updateMask = interfaceUpdateMaskFinal;
2019-05-14 14:08:05 +02:00
boolean forceDialogsUpdate = false;
2018-08-27 10:33:11 +02:00
if (updatesOnMainThreadFinal != null) {
ArrayList<TLRPC.User> dbUsers = new ArrayList<>();
ArrayList<TLRPC.User> dbUsersStatus = new ArrayList<>();
SharedPreferences.Editor editor = null;
for (int a = 0, size = updatesOnMainThreadFinal.size(); a < size; a++) {
final TLRPC.Update baseUpdate = updatesOnMainThreadFinal.get(a);
if (baseUpdate instanceof TLRPC.TL_updatePrivacy) {
TLRPC.TL_updatePrivacy update = (TLRPC.TL_updatePrivacy) baseUpdate;
if (update.key instanceof TLRPC.TL_privacyKeyStatusTimestamp) {
2019-07-18 15:01:39 +02:00
getContactsController().setPrivacyRules(update.rules, 0);
2018-08-27 10:33:11 +02:00
} else if (update.key instanceof TLRPC.TL_privacyKeyChatInvite) {
2019-07-18 15:01:39 +02:00
getContactsController().setPrivacyRules(update.rules, 1);
2018-08-27 10:33:11 +02:00
} else if (update.key instanceof TLRPC.TL_privacyKeyPhoneCall) {
2019-07-18 15:01:39 +02:00
getContactsController().setPrivacyRules(update.rules, 2);
2019-01-23 18:03:33 +01:00
} else if (update.key instanceof TLRPC.TL_privacyKeyPhoneP2P) {
2019-07-18 15:01:39 +02:00
getContactsController().setPrivacyRules(update.rules, 3);
2018-08-27 10:33:11 +02:00
}
} else if (baseUpdate instanceof TLRPC.TL_updateUserStatus) {
TLRPC.TL_updateUserStatus update = (TLRPC.TL_updateUserStatus) baseUpdate;
final TLRPC.User currentUser = getUser(update.user_id);
if (update.status instanceof TLRPC.TL_userStatusRecently) {
update.status.expires = -100;
} else if (update.status instanceof TLRPC.TL_userStatusLastWeek) {
update.status.expires = -101;
} else if (update.status instanceof TLRPC.TL_userStatusLastMonth) {
update.status.expires = -102;
}
if (currentUser != null) {
currentUser.id = update.user_id;
currentUser.status = update.status;
}
2019-05-14 14:08:05 +02:00
final TLRPC.User toDbUser = new TLRPC.TL_user();
2018-08-27 10:33:11 +02:00
toDbUser.id = update.user_id;
toDbUser.status = update.status;
dbUsersStatus.add(toDbUser);
2019-07-18 15:01:39 +02:00
if (update.user_id == getUserConfig().getClientUserId()) {
getNotificationsController().setLastOnlineFromOtherDevice(update.status.expires);
2018-08-27 10:33:11 +02:00
}
} else if (baseUpdate instanceof TLRPC.TL_updateUserName) {
TLRPC.TL_updateUserName update = (TLRPC.TL_updateUserName) baseUpdate;
final TLRPC.User currentUser = getUser(update.user_id);
if (currentUser != null) {
if (!UserObject.isContact(currentUser)) {
currentUser.first_name = update.first_name;
currentUser.last_name = update.last_name;
}
if (!TextUtils.isEmpty(currentUser.username)) {
objectsByUsernames.remove(currentUser.username);
}
if (TextUtils.isEmpty(update.username)) {
objectsByUsernames.put(update.username, currentUser);
}
currentUser.username = update.username;
}
2019-05-14 14:08:05 +02:00
final TLRPC.User toDbUser = new TLRPC.TL_user();
2018-08-27 10:33:11 +02:00
toDbUser.id = update.user_id;
toDbUser.first_name = update.first_name;
toDbUser.last_name = update.last_name;
toDbUser.username = update.username;
dbUsers.add(toDbUser);
} else if (baseUpdate instanceof TLRPC.TL_updateDialogPinned) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_updateDialogPinned update = (TLRPC.TL_updateDialogPinned) baseUpdate;
2018-08-27 10:33:11 +02:00
long did;
2019-05-14 14:08:05 +02:00
if (update.peer instanceof TLRPC.TL_dialogPeer) {
TLRPC.TL_dialogPeer dialogPeer = (TLRPC.TL_dialogPeer) update.peer;
did = DialogObject.getPeerDialogId(dialogPeer.peer);
2018-08-27 10:33:11 +02:00
} else {
did = 0;
}
2019-05-14 14:08:05 +02:00
if (!pinDialog(did, update.pinned, null, -1)) {
2019-07-18 15:01:39 +02:00
getUserConfig().setPinnedDialogsLoaded(update.folder_id, false);
getUserConfig().saveConfig(false);
2019-05-14 14:08:05 +02:00
loadPinnedDialogs(update.folder_id, did, null);
2018-08-27 10:33:11 +02:00
}
} else if (baseUpdate instanceof TLRPC.TL_updatePinnedDialogs) {
TLRPC.TL_updatePinnedDialogs update = (TLRPC.TL_updatePinnedDialogs) baseUpdate;
2019-07-18 15:01:39 +02:00
getUserConfig().setPinnedDialogsLoaded(update.folder_id, false);
getUserConfig().saveConfig(false);
2018-08-27 10:33:11 +02:00
ArrayList<Long> order;
if ((update.flags & 1) != 0) {
order = new ArrayList<>();
2019-05-14 14:08:05 +02:00
ArrayList<TLRPC.DialogPeer> peers = update.order;
2018-08-27 10:33:11 +02:00
for (int b = 0, size2 = peers.size(); b < size2; b++) {
long did;
TLRPC.DialogPeer dialogPeer = peers.get(b);
if (dialogPeer instanceof TLRPC.TL_dialogPeer) {
TLRPC.Peer peer = ((TLRPC.TL_dialogPeer) dialogPeer).peer;
if (peer.user_id != 0) {
did = peer.user_id;
} else if (peer.chat_id != 0) {
did = -peer.chat_id;
2017-03-31 01:58:05 +02:00
} else {
2018-08-27 10:33:11 +02:00
did = -peer.channel_id;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
did = 0;
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
order.add(did);
2014-07-10 02:15:58 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
order = null;
}
2019-05-14 14:08:05 +02:00
loadPinnedDialogs(update.folder_id, 0, order);
} else if (baseUpdate instanceof TLRPC.TL_updateFolderPeers) {
TLRPC.TL_updateFolderPeers update = (TLRPC.TL_updateFolderPeers) baseUpdate;
for (int b = 0, size2 = update.folder_peers.size(); b < size2; b++) {
TLRPC.TL_folderPeer folderPeer = update.folder_peers.get(b);
long dialogId = DialogObject.getPeerDialogId(folderPeer.peer);
TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
if (dialog == null) {
continue;
}
//TODO folder
dialog.folder_id = folderPeer.folder_id;
ensureFolderDialogExists(folderPeer.folder_id, null);
}
forceDialogsUpdate = true;
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateUserPhoto) {
TLRPC.TL_updateUserPhoto update = (TLRPC.TL_updateUserPhoto) baseUpdate;
final TLRPC.User currentUser = getUser(update.user_id);
if (currentUser != null) {
currentUser.photo = update.photo;
}
2019-05-14 14:08:05 +02:00
final TLRPC.User toDbUser = new TLRPC.TL_user();
2018-08-27 10:33:11 +02:00
toDbUser.id = update.user_id;
toDbUser.photo = update.photo;
dbUsers.add(toDbUser);
} else if (baseUpdate instanceof TLRPC.TL_updateUserPhone) {
TLRPC.TL_updateUserPhone update = (TLRPC.TL_updateUserPhone) baseUpdate;
final TLRPC.User currentUser = getUser(update.user_id);
if (currentUser != null) {
currentUser.phone = update.phone;
2019-07-18 15:01:39 +02:00
Utilities.phoneBookQueue.postRunnable(() -> getContactsController().addContactToPhoneBook(currentUser, true));
if (UserObject.isUserSelf(currentUser)) {
getNotificationCenter().postNotificationName(NotificationCenter.mainUserInfoChanged);
}
2018-08-27 10:33:11 +02:00
}
2019-05-14 14:08:05 +02:00
final TLRPC.User toDbUser = new TLRPC.TL_user();
2018-08-27 10:33:11 +02:00
toDbUser.id = update.user_id;
toDbUser.phone = update.phone;
dbUsers.add(toDbUser);
} else if (baseUpdate instanceof TLRPC.TL_updateNotifySettings) {
TLRPC.TL_updateNotifySettings update = (TLRPC.TL_updateNotifySettings) baseUpdate;
if (update.notify_settings instanceof TLRPC.TL_peerNotifySettings) {
if (editor == null) {
editor = notificationsPreferences.edit();
}
2019-07-18 15:01:39 +02:00
int currentTime1 = getConnectionsManager().getCurrentTime();
2018-08-27 10:33:11 +02:00
if (update.peer instanceof TLRPC.TL_notifyPeer) {
2019-01-23 18:03:33 +01:00
TLRPC.TL_notifyPeer notifyPeer = (TLRPC.TL_notifyPeer) update.peer;
2018-08-27 10:33:11 +02:00
long dialog_id;
2019-01-23 18:03:33 +01:00
if (notifyPeer.peer.user_id != 0) {
dialog_id = notifyPeer.peer.user_id;
} else if (notifyPeer.peer.chat_id != 0) {
dialog_id = -notifyPeer.peer.chat_id;
2018-08-27 10:33:11 +02:00
} else {
2019-01-23 18:03:33 +01:00
dialog_id = -notifyPeer.peer.channel_id;
2015-02-01 19:51:02 +01:00
}
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(dialog_id);
2018-08-27 10:33:11 +02:00
if (dialog != null) {
dialog.notify_settings = update.notify_settings;
}
if ((update.notify_settings.flags & 2) != 0) {
editor.putBoolean("silent_" + dialog_id, update.notify_settings.silent);
} else {
editor.remove("silent_" + dialog_id);
}
if ((update.notify_settings.flags & 4) != 0) {
if (update.notify_settings.mute_until > currentTime1) {
int until = 0;
if (update.notify_settings.mute_until > currentTime1 + 60 * 60 * 24 * 365) {
editor.putInt("notify2_" + dialog_id, 2);
if (dialog != null) {
update.notify_settings.mute_until = Integer.MAX_VALUE;
2018-07-30 04:07:02 +02:00
}
} else {
2018-08-27 10:33:11 +02:00
until = update.notify_settings.mute_until;
editor.putInt("notify2_" + dialog_id, 3);
editor.putInt("notifyuntil_" + dialog_id, update.notify_settings.mute_until);
2018-07-30 04:07:02 +02:00
if (dialog != null) {
2018-08-27 10:33:11 +02:00
update.notify_settings.mute_until = until;
2018-07-30 04:07:02 +02:00
}
2015-02-01 19:51:02 +01:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, ((long) until << 32) | 1);
getNotificationsController().removeNotificationsForDialog(dialog_id);
2015-02-01 19:51:02 +01:00
} else {
if (dialog != null) {
2018-07-30 04:07:02 +02:00
update.notify_settings.mute_until = 0;
2015-02-01 19:51:02 +01:00
}
2018-08-27 10:33:11 +02:00
editor.putInt("notify2_" + dialog_id, 0);
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, 0);
2015-02-01 19:51:02 +01:00
}
2018-08-27 10:33:11 +02:00
} else {
if (dialog != null) {
update.notify_settings.mute_until = 0;
2015-02-01 19:51:02 +01:00
}
2018-08-27 10:33:11 +02:00
editor.remove("notify2_" + dialog_id);
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, 0);
2018-08-27 10:33:11 +02:00
}
} else if (update.peer instanceof TLRPC.TL_notifyChats) {
if ((update.notify_settings.flags & 1) != 0) {
editor.putBoolean("EnablePreviewGroup", update.notify_settings.show_previews);
}
if ((update.notify_settings.flags & 2) != 0) {
/*if (update.notify_settings.silent) {
editor.putString("GroupSoundPath", "NoSound");
} else {
editor.remove("GroupSoundPath");
}*/
}
if ((update.notify_settings.flags & 4) != 0) {
2019-01-23 18:03:33 +01:00
editor.putInt("EnableGroup2", update.notify_settings.mute_until);
2018-08-27 10:33:11 +02:00
}
} else if (update.peer instanceof TLRPC.TL_notifyUsers) {
if ((update.notify_settings.flags & 1) != 0) {
editor.putBoolean("EnablePreviewAll", update.notify_settings.show_previews);
}
if ((update.notify_settings.flags & 2) != 0) {
/*if (update.notify_settings.silent) {
editor.putString("GlobalSoundPath", "NoSound");
} else {
editor.remove("GlobalSoundPath");
}*/
}
if ((update.notify_settings.flags & 4) != 0) {
2019-01-23 18:03:33 +01:00
editor.putInt("EnableAll2", update.notify_settings.mute_until);
}
} else if (update.peer instanceof TLRPC.TL_notifyBroadcasts) {
if ((update.notify_settings.flags & 1) != 0) {
editor.putBoolean("EnablePreviewChannel", update.notify_settings.show_previews);
}
if ((update.notify_settings.flags & 2) != 0) {
/*if (update.notify_settings.silent) {
editor.putString("ChannelSoundPath", "NoSound");
} else {
editor.remove("ChannelSoundPath");
}*/
}
if ((update.notify_settings.flags & 4) != 0) {
editor.putInt("EnableChannel2", update.notify_settings.mute_until);
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
}
2018-08-27 10:33:11 +02:00
}
} else if (baseUpdate instanceof TLRPC.TL_updateChannel) {
final TLRPC.TL_updateChannel update = (TLRPC.TL_updateChannel) baseUpdate;
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(-(long) update.channel_id);
2018-08-27 10:33:11 +02:00
TLRPC.Chat chat = getChat(update.channel_id);
if (chat != null) {
if (dialog == null && chat instanceof TLRPC.TL_channel && !chat.left) {
Utilities.stageQueue.postRunnable(() -> getChannelDifference(update.channel_id, 1, 0, null));
} else if (chat.left && dialog != null && (proxyDialog == null || proxyDialog.id != dialog.id)) {
deleteDialog(dialog.id, 0);
}
}
2019-01-23 18:03:33 +01:00
updateMask |= UPDATE_MASK_CHAT;
2018-08-27 10:33:11 +02:00
loadFullChat(update.channel_id, 0, true);
2019-01-23 18:03:33 +01:00
} else if (baseUpdate instanceof TLRPC.TL_updateChatDefaultBannedRights) {
TLRPC.TL_updateChatDefaultBannedRights update = (TLRPC.TL_updateChatDefaultBannedRights) baseUpdate;
int chatId;
if (update.peer.channel_id != 0) {
chatId = update.peer.channel_id;
} else {
chatId = update.peer.chat_id;
}
TLRPC.Chat chat = getChat(chatId);
if (chat != null) {
chat.default_banned_rights = update.default_banned_rights;
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.channelRightsUpdated, chat));
2019-01-23 18:03:33 +01:00
}
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateStickerSets) {
TLRPC.TL_updateStickerSets update = (TLRPC.TL_updateStickerSets) baseUpdate;
2019-07-18 15:01:39 +02:00
getMediaDataController().loadStickers(MediaDataController.TYPE_IMAGE, false, true);
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateStickerSetsOrder) {
TLRPC.TL_updateStickerSetsOrder update = (TLRPC.TL_updateStickerSetsOrder) baseUpdate;
2019-07-18 15:01:39 +02:00
getMediaDataController().reorderStickers(update.masks ? MediaDataController.TYPE_MASK : MediaDataController.TYPE_IMAGE, ((TLRPC.TL_updateStickerSetsOrder) baseUpdate).order);
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateFavedStickers) {
2019-07-18 15:01:39 +02:00
getMediaDataController().loadRecents(MediaDataController.TYPE_FAVE, false, false, true);
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateContactsReset) {
2019-07-18 15:01:39 +02:00
getContactsController().forceImportContacts();
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateNewStickerSet) {
TLRPC.TL_updateNewStickerSet update = (TLRPC.TL_updateNewStickerSet) baseUpdate;
2019-07-18 15:01:39 +02:00
getMediaDataController().addNewStickerSet(update.stickerset);
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateSavedGifs) {
SharedPreferences.Editor editor2 = emojiPreferences.edit();
editor2.putLong("lastGifLoadTime", 0).commit();
} else if (baseUpdate instanceof TLRPC.TL_updateRecentStickers) {
SharedPreferences.Editor editor2 = emojiPreferences.edit();
editor2.putLong("lastStickersLoadTime", 0).commit();
} else if (baseUpdate instanceof TLRPC.TL_updateDraftMessage) {
TLRPC.TL_updateDraftMessage update = (TLRPC.TL_updateDraftMessage) baseUpdate;
2019-05-14 14:08:05 +02:00
forceDialogsUpdate = true;
2018-08-27 10:33:11 +02:00
long did;
TLRPC.Peer peer = ((TLRPC.TL_updateDraftMessage) baseUpdate).peer;
if (peer.user_id != 0) {
did = peer.user_id;
} else if (peer.channel_id != 0) {
did = -peer.channel_id;
} else {
did = -peer.chat_id;
}
2019-07-18 15:01:39 +02:00
getMediaDataController().saveDraft(did, update.draft, null, true);
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updateReadFeaturedStickers) {
2019-07-18 15:01:39 +02:00
getMediaDataController().markFaturedStickersAsRead(false);
2018-08-27 10:33:11 +02:00
} else if (baseUpdate instanceof TLRPC.TL_updatePhoneCall) {
TLRPC.TL_updatePhoneCall upd = (TLRPC.TL_updatePhoneCall) baseUpdate;
TLRPC.PhoneCall call = upd.phone_call;
VoIPService svc = VoIPService.getSharedInstance();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("Received call in update: " + call);
FileLog.d("call id " + call.id);
}
if (call instanceof TLRPC.TL_phoneCallRequested) {
2019-07-18 15:01:39 +02:00
if (call.date + callRingTimeout / 1000 < getConnectionsManager().getCurrentTime()) {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("ignoring too old call");
}
2018-08-27 10:33:11 +02:00
continue;
2015-09-24 22:52:02 +02:00
}
2019-03-03 21:40:48 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !NotificationManagerCompat.from(ApplicationLoader.applicationContext).areNotificationsEnabled()) {
if (BuildVars.LOGS_ENABLED)
FileLog.d("Ignoring incoming call because notifications are disabled in system");
continue;
}
2018-08-27 10:33:11 +02:00
TelephonyManager tm = (TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
2019-03-03 21:40:48 +01:00
if (svc != null || VoIPService.callIShouldHavePutIntoIntent != null || tm.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("Auto-declining call " + call.id + " because there's already active one");
}
TLRPC.TL_phone_discardCall req = new TLRPC.TL_phone_discardCall();
req.peer = new TLRPC.TL_inputPhoneCall();
req.peer.access_hash = call.access_hash;
req.peer.id = call.id;
req.reason = new TLRPC.TL_phoneCallDiscardReasonBusy();
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
TLRPC.Updates updates1 = (TLRPC.Updates) response;
processUpdates(updates1, false);
}
});
continue;
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2018-08-27 10:33:11 +02:00
FileLog.d("Starting service for call " + call.id);
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
VoIPService.callIShouldHavePutIntoIntent = call;
Intent intent = new Intent(ApplicationLoader.applicationContext, VoIPService.class);
intent.putExtra("is_outgoing", false);
2019-07-18 15:01:39 +02:00
intent.putExtra("user_id", call.participant_id == getUserConfig().getClientUserId() ? call.admin_id : call.participant_id);
2018-08-27 10:33:11 +02:00
intent.putExtra("account", currentAccount);
try {
2019-03-03 21:40:48 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2018-08-27 10:33:11 +02:00
ApplicationLoader.applicationContext.startForegroundService(intent);
2019-03-03 21:40:48 +01:00
} else {
2018-08-27 10:33:11 +02:00
ApplicationLoader.applicationContext.startService(intent);
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
} catch (Throwable e) {
FileLog.e(e);
}
} else {
if (svc != null && call != null) {
svc.onCallUpdated(call);
} else if (VoIPService.callIShouldHavePutIntoIntent != null) {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
2018-08-27 10:33:11 +02:00
FileLog.d("Updated the call while the service is starting");
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
if (call.id == VoIPService.callIShouldHavePutIntoIntent.id) {
VoIPService.callIShouldHavePutIntoIntent = call;
2017-03-31 01:58:05 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
} else if (baseUpdate instanceof TLRPC.TL_updateDialogUnreadMark) {
TLRPC.TL_updateDialogUnreadMark update = (TLRPC.TL_updateDialogUnreadMark) baseUpdate;
long did;
if (update.peer instanceof TLRPC.TL_dialogPeer) {
TLRPC.TL_dialogPeer dialogPeer = (TLRPC.TL_dialogPeer) update.peer;
if (dialogPeer.peer.user_id != 0) {
did = dialogPeer.peer.user_id;
} else if (dialogPeer.peer.chat_id != 0) {
did = -dialogPeer.peer.chat_id;
2018-07-30 04:07:02 +02:00
} else {
2018-08-27 10:33:11 +02:00
did = -dialogPeer.peer.channel_id;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
did = 0;
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogUnread(did, update.unread);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2018-08-27 10:33:11 +02:00
if (dialog != null && dialog.unread_mark != update.unread) {
dialog.unread_mark = update.unread;
if (dialog.unread_count == 0 && !isDialogMuted(did)) {
if (dialog.unread_mark) {
unreadUnmutedDialogs++;
} else {
unreadUnmutedDialogs--;
2018-07-30 04:07:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
}
2019-01-23 18:03:33 +01:00
} else if (baseUpdate instanceof TLRPC.TL_updateMessagePoll) {
TLRPC.TL_updateMessagePoll update = (TLRPC.TL_updateMessagePoll) baseUpdate;
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.didUpdatePollResults, update.poll_id, update.poll, update.results);
} else if (baseUpdate instanceof TLRPC.TL_updatePeerSettings) {
TLRPC.TL_updatePeerSettings update = (TLRPC.TL_updatePeerSettings) baseUpdate;
long dialogId;
if (update.peer instanceof TLRPC.TL_peerUser) {
dialogId = update.peer.user_id;
} else if (update.peer instanceof TLRPC.TL_peerChat) {
dialogId = -update.peer.chat_id;
} else {
dialogId = -update.peer.channel_id;
}
savePeerSettings(dialogId, update.settings, true);
} else if (baseUpdate instanceof TLRPC.TL_updatePeerLocated) {
getNotificationCenter().postNotificationName(NotificationCenter.newPeopleNearbyAvailable, baseUpdate);
2013-10-25 17:19:00 +02:00
}
2014-07-10 02:15:58 +02:00
}
2018-08-27 10:33:11 +02:00
if (editor != null) {
editor.commit();
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.notificationsSettingsUpdated);
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateUsers(dbUsersStatus, true, true, true);
getMessagesStorage().updateUsers(dbUsers, false, true, true);
2018-08-27 10:33:11 +02:00
}
2018-08-27 10:33:11 +02:00
if (webPagesFinal != null) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.didReceivedWebpagesInUpdates, webPagesFinal);
2018-08-27 10:33:11 +02:00
for (int b = 0, size = webPagesFinal.size(); b < size; b++) {
long key = webPagesFinal.keyAt(b);
ArrayList<MessageObject> arrayList = reloadingWebpagesPending.get(key);
reloadingWebpagesPending.remove(key);
if (arrayList != null) {
TLRPC.WebPage webpage = webPagesFinal.valueAt(b);
ArrayList<TLRPC.Message> arr = new ArrayList<>();
long dialog_id = 0;
if (webpage instanceof TLRPC.TL_webPage || webpage instanceof TLRPC.TL_webPageEmpty) {
for (int a = 0, size2 = arrayList.size(); a < size2; a++) {
arrayList.get(a).messageOwner.media.webpage = webpage;
if (a == 0) {
dialog_id = arrayList.get(a).getDialogId();
ImageLoader.saveMessageThumbs(arrayList.get(a).messageOwner);
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
arr.add(arrayList.get(a).messageOwner);
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
} else {
reloadingWebpagesPending.put(webpage.id, arrayList);
}
if (!arr.isEmpty()) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putMessages(arr, true, true, false, getDownloadController().getAutodownloadMask());
getNotificationCenter().postNotificationName(NotificationCenter.replaceMessagesObjects, dialog_id, arrayList);
2016-03-06 02:49:31 +01:00
}
}
2015-04-09 20:00:14 +02:00
}
2018-08-27 10:33:11 +02:00
}
2015-04-09 20:00:14 +02:00
2018-08-27 10:33:11 +02:00
boolean updateDialogs = false;
if (messagesFinal != null) {
for (int a = 0, size = messagesFinal.size(); a < size; a++) {
long key = messagesFinal.keyAt(a);
ArrayList<MessageObject> value = messagesFinal.valueAt(a);
updateInterfaceWithMessages(key, value);
}
updateDialogs = true;
2019-05-14 14:08:05 +02:00
} else if (forceDialogsUpdate) {
2018-08-27 10:33:11 +02:00
sortDialogs(null);
updateDialogs = true;
}
if (editingMessagesFinal != null) {
for (int b = 0, size = editingMessagesFinal.size(); b < size; b++) {
long dialog_id = editingMessagesFinal.keyAt(b);
ArrayList<MessageObject> arrayList = editingMessagesFinal.valueAt(b);
MessageObject oldObject = dialogMessage.get(dialog_id);
if (oldObject != null) {
for (int a = 0, size2 = arrayList.size(); a < size2; a++) {
MessageObject newMessage = arrayList.get(a);
if (oldObject.getId() == newMessage.getId()) {
dialogMessage.put(dialog_id, newMessage);
if (newMessage.messageOwner.to_id != null && newMessage.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(newMessage.getId(), newMessage);
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
updateDialogs = true;
break;
} else if (oldObject.getDialogId() == newMessage.getDialogId() && oldObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage && oldObject.replyMessageObject != null && oldObject.replyMessageObject.getId() == newMessage.getId()) {
oldObject.replyMessageObject = newMessage;
oldObject.generatePinMessageText(null, null);
updateDialogs = true;
break;
2016-03-06 02:49:31 +01:00
}
}
}
2019-07-18 15:01:39 +02:00
getMediaDataController().loadReplyMessagesForMessages(arrayList, dialog_id);
getNotificationCenter().postNotificationName(NotificationCenter.replaceMessagesObjects, dialog_id, arrayList);
2016-03-06 02:49:31 +01:00
}
2018-08-27 10:33:11 +02:00
}
if (updateDialogs) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
2018-08-27 10:33:11 +02:00
}
2016-03-06 02:49:31 +01:00
2018-08-27 10:33:11 +02:00
if (printChangedArg) {
updateMask |= UPDATE_MASK_USER_PRINT;
}
if (contactsIdsFinal != null) {
updateMask |= UPDATE_MASK_NAME;
updateMask |= UPDATE_MASK_USER_PHONE;
}
if (chatInfoToUpdateFinal != null) {
for (int a = 0, size = chatInfoToUpdateFinal.size(); a < size; a++) {
TLRPC.ChatParticipants info = chatInfoToUpdateFinal.get(a);
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateChatParticipants(info);
2014-07-10 02:15:58 +02:00
}
}
2018-08-27 10:33:11 +02:00
if (channelViewsFinal != null) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.didUpdatedMessagesViews, channelViewsFinal);
2018-08-27 10:33:11 +02:00
}
if (updateMask != 0) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, updateMask);
2018-08-27 10:33:11 +02:00
}
2014-07-10 02:15:58 +02:00
});
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
final SparseLongArray markAsReadMessagesInboxFinal = markAsReadMessagesInbox;
final SparseLongArray markAsReadMessagesOutboxFinal = markAsReadMessagesOutbox;
final ArrayList<Long> markAsReadMessagesFinal = markAsReadMessages;
final SparseIntArray markAsReadEncryptedFinal = markAsReadEncrypted;
final SparseArray<ArrayList<Integer>> deletedMessagesFinal = deletedMessages;
final SparseIntArray clearHistoryMessagesFinal = clearHistoryMessages;
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
int updateMask = 0;
if (markAsReadMessagesInboxFinal != null || markAsReadMessagesOutboxFinal != null) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.messagesRead, markAsReadMessagesInboxFinal, markAsReadMessagesOutboxFinal);
2018-08-27 10:33:11 +02:00
if (markAsReadMessagesInboxFinal != null) {
2019-07-18 15:01:39 +02:00
getNotificationsController().processReadMessages(markAsReadMessagesInboxFinal, 0, 0, 0, false);
2018-08-27 10:33:11 +02:00
SharedPreferences.Editor editor = notificationsPreferences.edit();
for (int b = 0, size = markAsReadMessagesInboxFinal.size(); b < size; b++) {
int key = markAsReadMessagesInboxFinal.keyAt(b);
int messageId = (int) markAsReadMessagesInboxFinal.valueAt(b);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get((long) key);
2018-08-27 10:33:11 +02:00
if (dialog != null && dialog.top_message > 0 && dialog.top_message <= messageId) {
MessageObject obj = dialogMessage.get(dialog.id);
if (obj != null && !obj.isOut()) {
obj.setIsRead();
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
2014-08-06 01:17:40 +02:00
}
}
2019-07-18 15:01:39 +02:00
if (key != getUserConfig().getClientUserId()) {
2018-08-27 10:33:11 +02:00
editor.remove("diditem" + key);
editor.remove("diditemo" + key);
}
}
editor.commit();
}
if (markAsReadMessagesOutboxFinal != null) {
for (int b = 0, size = markAsReadMessagesOutboxFinal.size(); b < size; b++) {
int key = markAsReadMessagesOutboxFinal.keyAt(b);
int messageId = (int) markAsReadMessagesOutboxFinal.valueAt(b);
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get((long) key);
2018-08-27 10:33:11 +02:00
if (dialog != null && dialog.top_message > 0 && dialog.top_message <= messageId) {
MessageObject obj = dialogMessage.get(dialog.id);
if (obj != null && obj.isOut()) {
obj.setIsRead();
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
2014-08-06 01:17:40 +02:00
}
}
2018-08-27 10:33:11 +02:00
}
}
}
if (markAsReadEncryptedFinal != null) {
for (int a = 0, size = markAsReadEncryptedFinal.size(); a < size; a++) {
int key = markAsReadEncryptedFinal.keyAt(a);
int value = markAsReadEncryptedFinal.valueAt(a);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.messagesReadEncrypted, key, value);
2018-08-27 10:33:11 +02:00
long dialog_id = (long) (key) << 32;
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(dialog_id);
2018-08-27 10:33:11 +02:00
if (dialog != null) {
MessageObject message = dialogMessage.get(dialog_id);
if (message != null && message.messageOwner.date <= value) {
message.setIsRead();
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
}
2018-08-27 10:33:11 +02:00
}
}
}
if (markAsReadMessagesFinal != null) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.messagesReadContent, markAsReadMessagesFinal);
2018-08-27 10:33:11 +02:00
}
if (deletedMessagesFinal != null) {
for (int a = 0, size = deletedMessagesFinal.size(); a < size; a++) {
int key = deletedMessagesFinal.keyAt(a);
ArrayList<Integer> arrayList = deletedMessagesFinal.valueAt(a);
if (arrayList == null) {
continue;
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.messagesDeleted, arrayList, key);
2018-08-27 10:33:11 +02:00
if (key == 0) {
for (int b = 0, size2 = arrayList.size(); b < size2; b++) {
Integer id = arrayList.get(b);
MessageObject obj = dialogMessagesByIds.get(id);
if (obj != null) {
obj.deleted = true;
2014-08-06 01:17:40 +02:00
}
2018-08-27 10:33:11 +02:00
}
} else {
MessageObject obj = dialogMessage.get((long) -key);
if (obj != null) {
for (int b = 0, size2 = arrayList.size(); b < size2; b++) {
if (obj.getId() == arrayList.get(b)) {
obj.deleted = true;
break;
2017-12-08 18:35:59 +01:00
}
}
}
2018-08-27 10:33:11 +02:00
}
}
2019-07-18 15:01:39 +02:00
getNotificationsController().removeDeletedMessagesFromNotifications(deletedMessagesFinal);
2018-08-27 10:33:11 +02:00
}
if (clearHistoryMessagesFinal != null) {
for (int a = 0, size = clearHistoryMessagesFinal.size(); a < size; a++) {
int key = clearHistoryMessagesFinal.keyAt(a);
int id = clearHistoryMessagesFinal.valueAt(a);
long did = (long) -key;
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.historyCleared, did, id);
2018-08-27 10:33:11 +02:00
MessageObject obj = dialogMessage.get(did);
if (obj != null) {
if (obj.getId() <= id) {
obj.deleted = true;
break;
}
2014-07-30 09:49:39 +02:00
}
2018-08-27 10:33:11 +02:00
}
2019-07-18 15:01:39 +02:00
getNotificationsController().removeDeletedHisoryFromNotifications(clearHistoryMessagesFinal);
2014-07-30 09:49:39 +02:00
}
2018-08-27 10:33:11 +02:00
if (updateMask != 0) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, updateMask);
2018-08-27 10:33:11 +02:00
}
}));
2018-07-30 04:07:02 +02:00
if (webPages != null) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putWebPages(webPages);
2015-04-09 20:00:14 +02:00
}
2018-07-30 04:07:02 +02:00
if (markAsReadMessagesInbox != null || markAsReadMessagesOutbox != null || markAsReadEncrypted != null || markAsReadMessages != null) {
if (markAsReadMessagesInbox != null || markAsReadMessages != null) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().updateDialogsWithReadMessages(markAsReadMessagesInbox, markAsReadMessagesOutbox, markAsReadMessages, true);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().markMessagesAsRead(markAsReadMessagesInbox, markAsReadMessagesOutbox, markAsReadEncrypted, true);
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
if (markAsReadMessages != null) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().markMessagesContentAsRead(markAsReadMessages, getConnectionsManager().getCurrentTime());
}
2018-07-30 04:07:02 +02:00
if (deletedMessages != null) {
for (int a = 0, size = deletedMessages.size(); a < size; a++) {
2017-03-31 01:58:05 +02:00
final int key = deletedMessages.keyAt(a);
2018-07-30 04:07:02 +02:00
final ArrayList<Integer> arrayList = deletedMessages.valueAt(a);
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
ArrayList<Long> dialogIds = getMessagesStorage().markMessagesAsDeleted(arrayList, false, key);
getMessagesStorage().updateDialogsWithDeletedMessages(arrayList, dialogIds, false, key);
2017-03-31 01:58:05 +02:00
});
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
if (clearHistoryMessages != null) {
for (int a = 0, size = clearHistoryMessages.size(); a < size; a++) {
2017-12-08 18:35:59 +01:00
final int key = clearHistoryMessages.keyAt(a);
2018-07-30 04:07:02 +02:00
final int id = clearHistoryMessages.valueAt(a);
2019-07-18 15:01:39 +02:00
getMessagesStorage().getStorageQueue().postRunnable(() -> {
ArrayList<Long> dialogIds = getMessagesStorage().markMessagesAsDeleted(key, id, false);
getMessagesStorage().updateDialogsWithDeletedMessages(new ArrayList<>(), dialogIds, false, key);
2017-12-08 18:35:59 +01:00
});
}
}
2018-07-30 04:07:02 +02:00
if (tasks != null) {
for (int a = 0, size = tasks.size(); a < size; a++) {
2016-03-06 02:49:31 +01:00
TLRPC.TL_updateEncryptedMessagesRead update = tasks.get(a);
2019-07-18 15:01:39 +02:00
getMessagesStorage().createTaskForSecretChat(update.chat_id, update.max_date, update.date, 1, null);
2013-10-25 17:19:00 +02:00
}
}
return true;
}
public boolean isDialogMuted(long dialog_id) {
2018-07-30 04:07:02 +02:00
int mute_type = notificationsPreferences.getInt("notify2_" + dialog_id, -1);
if (mute_type == -1) {
2019-07-18 15:01:39 +02:00
return !getNotificationsController().isGlobalNotificationsEnabled(dialog_id);
2018-07-30 04:07:02 +02:00
}
if (mute_type == 2) {
return true;
} else if (mute_type == 3) {
2018-07-30 04:07:02 +02:00
int mute_until = notificationsPreferences.getInt("notifyuntil_" + dialog_id, 0);
2019-07-18 15:01:39 +02:00
if (mute_until >= getConnectionsManager().getCurrentTime()) {
return true;
}
}
return false;
}
2013-10-25 17:19:00 +02:00
private boolean updatePrintingUsersWithNewMessages(long uid, ArrayList<MessageObject> messages) {
if (uid > 0) {
ArrayList<PrintingUser> arr = printingUsers.get(uid);
if (arr != null) {
printingUsers.remove(uid);
return true;
}
} else if (uid < 0) {
2015-01-02 23:15:07 +01:00
ArrayList<Integer> messagesUsers = new ArrayList<>();
2013-10-25 17:19:00 +02:00
for (MessageObject message : messages) {
if (!messagesUsers.contains(message.messageOwner.from_id)) {
messagesUsers.add(message.messageOwner.from_id);
}
}
ArrayList<PrintingUser> arr = printingUsers.get(uid);
boolean changed = false;
if (arr != null) {
for (int a = 0; a < arr.size(); a++) {
PrintingUser user = arr.get(a);
if (messagesUsers.contains(user.userId)) {
arr.remove(a);
a--;
if (arr.isEmpty()) {
printingUsers.remove(uid);
}
changed = true;
}
}
}
if (changed) {
return true;
}
}
return false;
}
protected void updateInterfaceWithMessages(long uid, ArrayList<MessageObject> messages) {
updateInterfaceWithMessages(uid, messages, false);
}
protected void updateInterfaceWithMessages(final long uid, final ArrayList<MessageObject> messages, boolean isBroadcast) {
2015-05-21 23:27:27 +02:00
if (messages == null || messages.isEmpty()) {
return;
}
2013-10-25 17:19:00 +02:00
2015-05-21 23:27:27 +02:00
boolean isEncryptedChat = ((int) uid) == 0;
MessageObject lastMessage = null;
2015-09-24 22:52:02 +02:00
int channelId = 0;
2016-05-25 23:49:47 +02:00
boolean updateRating = false;
2019-06-04 12:14:50 +02:00
boolean hasNotOutMessage = false;
2015-09-24 22:52:02 +02:00
for (int a = 0; a < messages.size(); a++) {
MessageObject message = messages.get(a);
if (lastMessage == null || (!isEncryptedChat && message.getId() > lastMessage.getId() || (isEncryptedChat || message.getId() < 0 && lastMessage.getId() < 0) && message.getId() < lastMessage.getId()) || message.messageOwner.date > lastMessage.messageOwner.date) {
lastMessage = message;
2015-09-24 22:52:02 +02:00
if (message.messageOwner.to_id.channel_id != 0) {
channelId = message.messageOwner.to_id.channel_id;
}
2013-10-25 17:19:00 +02:00
}
2019-06-04 12:14:50 +02:00
if (!hasNotOutMessage && !message.isOut()) {
hasNotOutMessage = true;
}
2016-10-11 13:57:01 +02:00
if (message.isOut() && !message.isSending() && !message.isForwarded()) {
if (message.isNewGif()) {
2019-07-18 15:01:39 +02:00
getMediaDataController().addRecentGif(message.messageOwner.media.document, message.messageOwner.date);
} else if (message.isSticker() || message.isAnimatedSticker()) {
getMediaDataController().addRecentSticker(MediaDataController.TYPE_IMAGE, message, message.messageOwner.media.document, message.messageOwner.date, false);
2016-10-11 13:57:01 +02:00
}
}
2016-05-25 23:49:47 +02:00
if (message.isOut() && message.isSent()) {
updateRating = true;
}
2013-10-25 17:19:00 +02:00
}
2019-07-18 15:01:39 +02:00
getMediaDataController().loadReplyMessagesForMessages(messages, uid);
getNotificationCenter().postNotificationName(NotificationCenter.didReceiveNewMessages, uid, messages);
2015-05-21 23:27:27 +02:00
if (lastMessage == null) {
return;
}
2019-05-14 14:08:05 +02:00
TLRPC.TL_dialog dialog = (TLRPC.TL_dialog) dialogs_dict.get(uid);
2015-11-26 22:04:02 +01:00
if (lastMessage.messageOwner.action instanceof TLRPC.TL_messageActionChatMigrateTo) {
if (dialog != null) {
2019-05-14 14:08:05 +02:00
allDialogs.remove(dialog);
2015-11-26 22:04:02 +01:00
dialogsServerOnly.remove(dialog);
2019-03-03 21:40:48 +01:00
dialogsCanAddUsers.remove(dialog);
2019-02-08 03:30:32 +01:00
dialogsChannelsOnly.remove(dialog);
2019-03-03 21:40:48 +01:00
dialogsGroupsOnly.remove(dialog);
2019-02-08 03:30:32 +01:00
dialogsUsersOnly.remove(dialog);
2019-01-23 18:03:33 +01:00
dialogsForward.remove(dialog);
2015-11-26 22:04:02 +01:00
dialogs_dict.remove(dialog.id);
dialogs_read_inbox_max.remove(dialog.id);
dialogs_read_outbox_max.remove(dialog.id);
2019-05-14 14:08:05 +02:00
int offset = nextDialogsCacheOffset.get(dialog.folder_id, 0);
if (offset > 0) {
nextDialogsCacheOffset.put(dialog.folder_id, offset - 1);
}
2015-11-26 22:04:02 +01:00
dialogMessage.remove(dialog.id);
2019-05-14 14:08:05 +02:00
ArrayList<TLRPC.Dialog> dialogs = dialogsByFolder.get(dialog.folder_id);
if (dialogs != null) {
dialogs.remove(dialog);
}
2018-07-30 04:07:02 +02:00
MessageObject object = dialogMessagesByIds.get(dialog.top_message);
dialogMessagesByIds.remove(dialog.top_message);
2016-03-06 02:49:31 +01:00
if (object != null && object.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(object.messageOwner.random_id);
}
2015-11-26 22:04:02 +01:00
dialog.top_message = 0;
2019-07-18 15:01:39 +02:00
getNotificationsController().removeNotificationsForDialog(dialog.id);
getNotificationCenter().postNotificationName(NotificationCenter.needReloadRecentDialogsSearch);
2015-11-26 22:04:02 +01:00
}
return;
}
2015-05-21 23:27:27 +02:00
boolean changed = false;
2013-10-25 17:19:00 +02:00
if (dialog == null) {
if (!isBroadcast) {
2015-09-24 22:52:02 +02:00
TLRPC.Chat chat = getChat(channelId);
2019-07-18 15:01:39 +02:00
if (channelId != 0 && chat == null || chat != null && ChatObject.isNotInChat(chat)) {
2015-09-24 22:52:02 +02:00
return;
}
2019-06-04 12:14:50 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("not found dialog with id " + uid + " dictCount = " + dialogs_dict.size() + " allCount = " + allDialogs.size());
}
dialog = new TLRPC.TL_dialog();
dialog.id = uid;
dialog.unread_count = 0;
dialog.top_message = lastMessage.getId();
dialog.last_message_date = lastMessage.messageOwner.date;
dialog.flags = ChatObject.isChannel(chat) ? 1 : 0;
dialogs_dict.put(uid, dialog);
2019-05-14 14:08:05 +02:00
allDialogs.add(dialog);
2015-09-24 22:52:02 +02:00
dialogMessage.put(uid, lastMessage);
if (lastMessage.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(lastMessage.getId(), lastMessage);
2016-03-06 02:49:31 +01:00
if (lastMessage.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(lastMessage.messageOwner.random_id, lastMessage);
}
2015-09-24 22:52:02 +02:00
}
changed = true;
2019-05-14 14:08:05 +02:00
//int offset = nextDialogsCacheOffset.get(dialog.folder_id, 0) + 1;
//nextDialogsCacheOffset.put(dialog.folder_id, offset);
TLRPC.Dialog dialogFinal = dialog;
2019-07-18 15:01:39 +02:00
getMessagesStorage().getDialogFolderId(uid, param -> {
2019-05-14 14:08:05 +02:00
if (param != -1) {
if (param != 0) {
dialogFinal.folder_id = param;
sortDialogs(null);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2019-05-14 14:08:05 +02:00
}
} else {
int lowerId = (int) uid;
if (lowerId != 0) {
loadUnknownDialog(getInputPeer(lowerId), 0);
}
}
});
}
2013-10-25 17:19:00 +02:00
} else {
2019-06-04 12:14:50 +02:00
if (hasNotOutMessage && dialog.folder_id == 1 && !isDialogMuted(dialog.id)) {
2019-05-14 14:08:05 +02:00
dialog.folder_id = 0;
dialog.pinned = false;
dialog.pinnedNum = 0;
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogsFolderId(null, null, dialog.id, 0);
2019-05-14 14:08:05 +02:00
changed = true;
}
2015-10-29 18:10:07 +01:00
if ((dialog.top_message > 0 && lastMessage.getId() > 0 && lastMessage.getId() > dialog.top_message) ||
(dialog.top_message < 0 && lastMessage.getId() < 0 && lastMessage.getId() < dialog.top_message) ||
2018-07-30 04:07:02 +02:00
dialogMessage.indexOfKey(uid) < 0 || dialog.top_message < 0 || dialog.last_message_date <= lastMessage.messageOwner.date) {
MessageObject object = dialogMessagesByIds.get(dialog.top_message);
dialogMessagesByIds.remove(dialog.top_message);
2016-03-06 02:49:31 +01:00
if (object != null && object.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.remove(object.messageOwner.random_id);
}
dialog.top_message = lastMessage.getId();
if (!isBroadcast) {
dialog.last_message_date = lastMessage.messageOwner.date;
changed = true;
}
2015-09-24 22:52:02 +02:00
dialogMessage.put(uid, lastMessage);
if (lastMessage.messageOwner.to_id.channel_id == 0) {
dialogMessagesByIds.put(lastMessage.getId(), lastMessage);
2016-03-06 02:49:31 +01:00
if (lastMessage.messageOwner.random_id != 0) {
dialogMessagesByRandomIds.put(lastMessage.messageOwner.random_id, lastMessage);
}
2015-09-24 22:52:02 +02:00
}
}
2013-10-25 17:19:00 +02:00
}
if (changed) {
sortDialogs(null);
2013-10-25 17:19:00 +02:00
}
2016-05-25 23:49:47 +02:00
if (updateRating) {
2019-07-18 15:01:39 +02:00
getMediaDataController().increasePeerRaiting(uid);
2016-05-25 23:49:47 +02:00
}
2013-10-25 17:19:00 +02:00
}
2015-02-27 20:57:58 +01:00
2019-01-23 18:03:33 +01:00
public void addDialogAction(long did, boolean clean) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2019-01-23 18:03:33 +01:00
if (dialog == null) {
return;
}
if (clean) {
clearingHistoryDialogs.put(did, dialog);
} else {
deletingDialogs.put(did, dialog);
2019-05-14 14:08:05 +02:00
allDialogs.remove(dialog);
2019-01-23 18:03:33 +01:00
sortDialogs(null);
}
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2019-01-23 18:03:33 +01:00
}
public void removeDialogAction(long did, boolean clean, boolean apply) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = dialogs_dict.get(did);
2019-01-23 18:03:33 +01:00
if (dialog == null) {
return;
}
if (clean) {
clearingHistoryDialogs.remove(did);
} else {
deletingDialogs.remove(did);
if (!apply) {
2019-05-14 14:08:05 +02:00
allDialogs.add(dialog);
2019-01-23 18:03:33 +01:00
sortDialogs(null);
}
}
if (!apply) {
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload, true);
2019-01-23 18:03:33 +01:00
}
}
public boolean isClearingDialog(long did) {
return clearingHistoryDialogs.get(did) != null;
}
2018-07-30 04:07:02 +02:00
public void sortDialogs(SparseArray<TLRPC.Chat> chatsDict) {
dialogsServerOnly.clear();
2019-03-03 21:40:48 +01:00
dialogsCanAddUsers.clear();
2019-02-08 03:30:32 +01:00
dialogsChannelsOnly.clear();
2019-03-03 21:40:48 +01:00
dialogsGroupsOnly.clear();
2019-02-08 03:30:32 +01:00
dialogsUsersOnly.clear();
2017-12-08 18:35:59 +01:00
dialogsForward.clear();
2019-05-14 14:08:05 +02:00
for (int a = 0; a < dialogsByFolder.size(); a++) {
2019-07-18 15:01:39 +02:00
ArrayList<TLRPC.Dialog> arrayList = dialogsByFolder.valueAt(a);
2019-05-14 14:08:05 +02:00
if (arrayList != null) {
arrayList.clear();
}
}
2018-07-30 04:07:02 +02:00
unreadUnmutedDialogs = 0;
2017-12-08 18:35:59 +01:00
boolean selfAdded = false;
2019-07-18 15:01:39 +02:00
int selfId = getUserConfig().getClientUserId();
2019-05-14 14:08:05 +02:00
Collections.sort(allDialogs, dialogComparator);
2018-07-30 04:07:02 +02:00
isLeftProxyChannel = true;
if (proxyDialog != null && proxyDialog.id < 0) {
TLRPC.Chat chat = getChat(-(int) proxyDialog.id);
if (chat != null && !chat.left) {
isLeftProxyChannel = false;
}
}
2019-07-18 15:01:39 +02:00
boolean countMessages = getNotificationsController().showBadgeMessages;
2019-05-14 14:08:05 +02:00
for (int a = 0, N = allDialogs.size(); a < N; a++) {
TLRPC.Dialog d = allDialogs.get(a);
int high_id = (int) (d.id >> 32);
int lower_id = (int) d.id;
2019-05-14 14:08:05 +02:00
if (d instanceof TLRPC.TL_dialog) {
2019-06-04 12:14:50 +02:00
boolean canAddToForward = true;
2019-05-14 14:08:05 +02:00
if (lower_id != 0 && high_id != 1) {
dialogsServerOnly.add(d);
if (DialogObject.isChannel(d)) {
TLRPC.Chat chat = getChat(-lower_id);
if (chat != null && (chat.megagroup && (chat.admin_rights != null && (chat.admin_rights.post_messages || chat.admin_rights.add_admins)) || chat.creator)) {
dialogsCanAddUsers.add(d);
}
if (chat != null && chat.megagroup) {
dialogsGroupsOnly.add(d);
} else {
dialogsChannelsOnly.add(d);
2019-06-04 12:14:50 +02:00
canAddToForward = ChatObject.hasAdminRights(chat) && ChatObject.canPost(chat);
2019-05-14 14:08:05 +02:00
}
} else if (lower_id < 0) {
if (chatsDict != null) {
TLRPC.Chat chat = chatsDict.get(-lower_id);
if (chat != null && chat.migrated_to != null) {
allDialogs.remove(a);
a--;
N--;
continue;
}
}
2019-03-03 21:40:48 +01:00
dialogsCanAddUsers.add(d);
dialogsGroupsOnly.add(d);
2019-07-18 15:01:39 +02:00
} else if (lower_id > 0 && lower_id != selfId) {
2019-05-14 14:08:05 +02:00
dialogsUsersOnly.add(d);
}
}
2019-07-18 15:01:39 +02:00
if (canAddToForward && d.folder_id == 0) {
2019-06-04 12:14:50 +02:00
if (lower_id == selfId) {
dialogsForward.add(0, d);
selfAdded = true;
} else {
dialogsForward.add(d);
}
}
}
2018-07-30 04:07:02 +02:00
if ((d.unread_count != 0 || d.unread_mark) && !isDialogMuted(d.id)) {
unreadUnmutedDialogs++;
}
2019-05-14 14:08:05 +02:00
if (proxyDialog != null && d.id == proxyDialog.id && isLeftProxyChannel) {
allDialogs.remove(a);
a--;
N--;
continue;
}
addDialogToItsFolder(-1, d, countMessages);
2018-07-30 04:07:02 +02:00
}
if (proxyDialog != null && isLeftProxyChannel) {
2019-05-14 14:08:05 +02:00
allDialogs.add(0, proxyDialog);
addDialogToItsFolder(-2, proxyDialog, countMessages);
}
2017-12-08 18:35:59 +01:00
if (!selfAdded) {
2019-07-18 15:01:39 +02:00
TLRPC.User user = getUserConfig().getCurrentUser();
2017-12-08 18:35:59 +01:00
if (user != null) {
2019-05-14 14:08:05 +02:00
TLRPC.Dialog dialog = new TLRPC.TL_dialog();
2017-12-08 18:35:59 +01:00
dialog.id = user.id;
dialog.notify_settings = new TLRPC.TL_peerNotifySettings();
dialog.peer = new TLRPC.TL_peerUser();
dialog.peer.user_id = user.id;
dialogsForward.add(0, dialog);
}
}
2019-05-14 14:08:05 +02:00
for (int a = 0; a < dialogsByFolder.size(); a++) {
int folderId = dialogsByFolder.keyAt(a);
ArrayList<TLRPC.Dialog> dialogs = dialogsByFolder.valueAt(a);
if (dialogs.isEmpty()) {
dialogsByFolder.remove(folderId);
}
}
}
private void addDialogToItsFolder(int index, TLRPC.Dialog dialog, boolean countMessages) {
int folderId;
if (dialog instanceof TLRPC.TL_dialogFolder) {
folderId = 0;
dialog.unread_count = 0;
dialog.unread_mentions_count = 0;
} else {
folderId = dialog.folder_id;
}
ArrayList<TLRPC.Dialog> dialogs = dialogsByFolder.get(folderId);
if (dialogs == null) {
dialogs = new ArrayList<>();
dialogsByFolder.put(folderId, dialogs);
}
if (folderId != 0 && dialog.unread_count != 0) {
TLRPC.Dialog folder = dialogs_dict.get(DialogObject.makeFolderDialogId(folderId));
if (folder != null) {
if (countMessages) {
if (isDialogMuted(dialog.id)) {
folder.unread_count += dialog.unread_count;
} else {
folder.unread_mentions_count += dialog.unread_count;
}
} else {
if (isDialogMuted(dialog.id)) {
folder.unread_count++;
} else {
folder.unread_mentions_count++;
}
}
}
}
if (index == -1) {
dialogs.add(dialog);
} else if (index == -2) {
if (dialogs.isEmpty() || !(dialogs.get(0) instanceof TLRPC.TL_dialogFolder)) {
dialogs.add(0, dialog);
} else {
dialogs.add(1, dialog);
}
} else {
dialogs.add(index, dialog);
}
}
2016-03-16 13:26:32 +01:00
private static String getRestrictionReason(String reason) {
if (reason == null || reason.length() == 0) {
return null;
}
int index = reason.indexOf(": ");
if (index > 0) {
String type = reason.substring(0, index);
if (type.contains("-all") || type.contains("-android")) {
return reason.substring(index + 2);
}
}
return null;
}
private static void showCantOpenAlert(BaseFragment fragment, String reason) {
2017-03-31 01:58:05 +02:00
if (fragment == null || fragment.getParentActivity() == null) {
return;
}
2016-03-16 13:26:32 +01:00
AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getParentActivity());
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
builder.setMessage(reason);
fragment.showDialog(builder.create());
}
2018-07-30 04:07:02 +02:00
public boolean checkCanOpenChat(Bundle bundle, BaseFragment fragment) {
2017-12-08 18:35:59 +01:00
return checkCanOpenChat(bundle, fragment, null);
}
2018-07-30 04:07:02 +02:00
public boolean checkCanOpenChat(final Bundle bundle, final BaseFragment fragment, MessageObject originalMessage) {
2016-03-16 13:26:32 +01:00
if (bundle == null || fragment == null) {
return true;
}
TLRPC.User user = null;
TLRPC.Chat chat = null;
int user_id = bundle.getInt("user_id", 0);
int chat_id = bundle.getInt("chat_id", 0);
2017-12-08 18:35:59 +01:00
int messageId = bundle.getInt("message_id", 0);
2016-03-16 13:26:32 +01:00
if (user_id != 0) {
2018-07-30 04:07:02 +02:00
user = getUser(user_id);
2016-03-16 13:26:32 +01:00
} else if (chat_id != 0) {
2018-07-30 04:07:02 +02:00
chat = getChat(chat_id);
2016-03-16 13:26:32 +01:00
}
if (user == null && chat == null) {
return true;
}
String reason = null;
if (chat != null) {
reason = getRestrictionReason(chat.restriction_reason);
} else if (user != null) {
reason = getRestrictionReason(user.restriction_reason);
}
if (reason != null) {
showCantOpenAlert(fragment, reason);
return false;
}
2017-12-08 18:35:59 +01:00
if (messageId != 0 && originalMessage != null && chat != null && chat.access_hash == 0) {
int did = (int) originalMessage.getDialogId();
if (did != 0) {
2019-01-23 18:03:33 +01:00
final AlertDialog progressDialog = new AlertDialog(fragment.getParentActivity(), 3);
2017-12-08 18:35:59 +01:00
TLObject req;
if (did < 0) {
2018-07-30 04:07:02 +02:00
chat = getChat(-did);
2017-12-08 18:35:59 +01:00
}
if (did > 0 || !ChatObject.isChannel(chat)) {
TLRPC.TL_messages_getMessages request = new TLRPC.TL_messages_getMessages();
request.id.add(originalMessage.getId());
req = request;
} else {
2018-07-30 04:07:02 +02:00
chat = getChat(-did);
2017-12-08 18:35:59 +01:00
TLRPC.TL_channels_getMessages request = new TLRPC.TL_channels_getMessages();
request.channel = getInputChannel(chat);
request.id.add(originalMessage.getId());
req = request;
}
2019-07-18 15:01:39 +02:00
final int reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
2018-08-27 10:33:11 +02:00
if (response != null) {
AndroidUtilities.runOnUIThread(() -> {
try {
progressDialog.dismiss();
} catch (Exception e) {
FileLog.e(e);
}
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
putUsers(res.users, false);
putChats(res.chats, false);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, res.chats, true, true);
2018-08-27 10:33:11 +02:00
fragment.presentFragment(new ChatActivity(bundle), true);
});
2017-12-08 18:35:59 +01:00
}
});
2019-01-23 18:03:33 +01:00
progressDialog.setOnCancelListener(dialog -> {
2019-07-18 15:01:39 +02:00
getConnectionsManager().cancelRequest(reqId, true);
2018-08-27 10:33:11 +02:00
if (fragment != null) {
fragment.setVisibleDialog(null);
2017-12-08 18:35:59 +01:00
}
});
fragment.setVisibleDialog(progressDialog);
progressDialog.show();
return false;
}
}
2016-03-16 13:26:32 +01:00
return true;
}
2016-10-11 13:57:01 +02:00
public static void openChatOrProfileWith(TLRPC.User user, TLRPC.Chat chat, BaseFragment fragment, int type, boolean closeLast) {
2016-03-16 13:26:32 +01:00
if (user == null && chat == null || fragment == null) {
2015-05-21 23:27:27 +02:00
return;
}
2016-03-16 13:26:32 +01:00
String reason = null;
if (chat != null) {
reason = getRestrictionReason(chat.restriction_reason);
} else if (user != null) {
reason = getRestrictionReason(user.restriction_reason);
2016-04-22 15:49:00 +02:00
if (user.bot) {
type = 1;
closeLast = true;
}
2016-03-16 13:26:32 +01:00
}
if (reason != null) {
showCantOpenAlert(fragment, reason);
} else {
2015-05-21 23:27:27 +02:00
Bundle args = new Bundle();
2016-03-16 13:26:32 +01:00
if (chat != null) {
args.putInt("chat_id", chat.id);
} else {
args.putInt("user_id", user.id);
}
2015-05-21 23:27:27 +02:00
if (type == 0) {
fragment.presentFragment(new ProfileActivity(args));
2017-07-08 18:32:04 +02:00
} else if (type == 2) {
fragment.presentFragment(new ChatActivity(args), true, true);
2015-05-21 23:27:27 +02:00
} else {
2016-04-22 15:49:00 +02:00
fragment.presentFragment(new ChatActivity(args), closeLast);
2015-05-21 23:27:27 +02:00
}
2016-03-16 13:26:32 +01:00
}
}
2018-07-30 04:07:02 +02:00
public void openByUserName(String username, final BaseFragment fragment, final int type) {
2016-03-16 13:26:32 +01:00
if (username == null || fragment == null) {
return;
}
2018-07-30 04:07:02 +02:00
TLObject object = getUserOrChat(username);
2017-07-08 18:32:04 +02:00
TLRPC.User user = null;
TLRPC.Chat chat = null;
if (object instanceof TLRPC.User) {
user = (TLRPC.User) object;
if (user.min) {
user = null;
}
} else if (object instanceof TLRPC.Chat) {
chat = (TLRPC.Chat) object;
if (chat.min) {
chat = null;
}
}
2016-03-16 13:26:32 +01:00
if (user != null) {
2016-10-11 13:57:01 +02:00
openChatOrProfileWith(user, null, fragment, type, false);
2017-07-08 18:32:04 +02:00
} else if (chat != null) {
openChatOrProfileWith(null, chat, fragment, 1, false);
2015-05-21 23:27:27 +02:00
} else {
if (fragment.getParentActivity() == null) {
return;
}
2019-05-14 14:08:05 +02:00
final AlertDialog[] progressDialog = new AlertDialog[]{new AlertDialog(fragment.getParentActivity(), 3)};
2015-02-27 20:57:58 +01:00
2015-05-21 23:27:27 +02:00
TLRPC.TL_contacts_resolveUsername req = new TLRPC.TL_contacts_resolveUsername();
req.username = username;
2019-07-18 15:01:39 +02:00
final int reqId = getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
2018-08-27 10:33:11 +02:00
try {
progressDialog[0].dismiss();
} catch (Exception ignored) {
2017-12-08 18:35:59 +01:00
2015-05-21 23:27:27 +02:00
}
2018-08-27 10:33:11 +02:00
progressDialog[0] = null;
fragment.setVisibleDialog(null);
if (error == null) {
TLRPC.TL_contacts_resolvedPeer res = (TLRPC.TL_contacts_resolvedPeer) response;
putUsers(res.users, false);
putChats(res.chats, false);
2019-07-18 15:01:39 +02:00
getMessagesStorage().putUsersAndChats(res.users, res.chats, false, true);
2018-08-27 10:33:11 +02:00
if (!res.chats.isEmpty()) {
openChatOrProfileWith(null, res.chats.get(0), fragment, 1, false);
} else if (!res.users.isEmpty()) {
openChatOrProfileWith(res.users.get(0), null, fragment, type, false);
2015-05-21 23:27:27 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
if (fragment != null && fragment.getParentActivity() != null) {
try {
Toast.makeText(fragment.getParentActivity(), LocaleController.getString("NoUsernameFound", R.string.NoUsernameFound), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
FileLog.e(e);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
}
}
}));
AndroidUtilities.runOnUIThread(() -> {
if (progressDialog[0] == null) {
return;
2015-05-21 23:27:27 +02:00
}
2019-07-18 15:01:39 +02:00
progressDialog[0].setOnCancelListener(dialog -> getConnectionsManager().cancelRequest(reqId, true));
2018-08-27 10:33:11 +02:00
fragment.showDialog(progressDialog[0]);
2017-12-08 18:35:59 +01:00
}, 500);
2015-05-21 23:27:27 +02:00
}
}
2013-10-25 17:19:00 +02:00
}