2013-10-25 17:19:00 +02:00
/ *
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v . 3 . 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 ) .
*
2016-01-11 18:19:48 +01:00
* Copyright Nikolai Kudashov , 2013 - 2016 .
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
2014-02-04 19:36:55 +01:00
import android.app.Activity ;
2015-02-27 20:57:58 +01:00
import android.app.AlertDialog ;
2015-05-21 23:27:27 +02:00
import android.app.ProgressDialog ;
2015-11-26 22:04:02 +01:00
import android.content.Context ;
2015-05-21 23:27:27 +02:00
import android.content.DialogInterface ;
2013-10-25 17:19:00 +02:00
import android.content.SharedPreferences ;
import android.content.pm.PackageInfo ;
import android.os.Build ;
2015-05-21 23:27:27 +02:00
import android.os.Bundle ;
2015-02-27 20:57:58 +01:00
import android.util.Base64 ;
2014-10-10 19:16:39 +02:00
import android.util.SparseArray ;
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 ;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.query.BotQuery ;
2016-03-16 13:26:32 +01:00
import org.telegram.messenger.query.MessagesQuery ;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.query.StickersQuery ;
import org.telegram.tgnet.ConnectionsManager ;
import org.telegram.tgnet.RequestDelegate ;
import org.telegram.tgnet.SerializedData ;
import org.telegram.tgnet.TLObject ;
import org.telegram.tgnet.TLRPC ;
2015-02-27 20:57:58 +01:00
import org.telegram.ui.ActionBar.BaseFragment ;
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 ;
2015-05-21 23:27:27 +02:00
import org.telegram.ui.ProfileActivity ;
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 ;
import java.util.concurrent.Semaphore ;
public class MessagesController implements NotificationCenter . NotificationCenterDelegate {
2014-12-01 18:56:31 +01:00
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 ) ;
2015-03-19 00:09:45 +01:00
private ConcurrentHashMap < String , TLRPC . User > usersByUsernames = 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 < > ( ) ;
2015-05-03 13:48:36 +02:00
private HashMap < Integer , TLRPC . ExportedChatInvite > exportedChats = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
public ArrayList < TLRPC . Dialog > dialogs = new ArrayList < > ( ) ;
public ArrayList < TLRPC . Dialog > dialogsServerOnly = new ArrayList < > ( ) ;
public ArrayList < TLRPC . Dialog > dialogsGroupsOnly = new ArrayList < > ( ) ;
public int nextDialogsCacheOffset ;
2015-11-26 22:04:02 +01:00
public ConcurrentHashMap < Long , Integer > dialogs_read_inbox_max = new ConcurrentHashMap < > ( 100 , 1 . 0f , 2 ) ;
2015-09-24 22:52:02 +02:00
public ConcurrentHashMap < Long , TLRPC . Dialog > dialogs_dict = new ConcurrentHashMap < > ( 100 , 1 . 0f , 2 ) ;
public HashMap < Long , MessageObject > dialogMessage = new HashMap < > ( ) ;
2016-03-06 02:49:31 +01:00
public HashMap < Long , MessageObject > dialogMessagesByRandomIds = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
public HashMap < Integer , MessageObject > dialogMessagesByIds = new HashMap < > ( ) ;
2015-01-02 23:15:07 +01:00
public ConcurrentHashMap < Long , ArrayList < PrintingUser > > printingUsers = new ConcurrentHashMap < > ( 20 , 1 . 0f , 2 ) ;
public HashMap < Long , CharSequence > printingStrings = new HashMap < > ( ) ;
2015-05-03 13:48:36 +02:00
public HashMap < Long , Integer > printingStringsTypes = new HashMap < > ( ) ;
public HashMap < Integer , HashMap < Long , Boolean > > sendingTypings = new HashMap < > ( ) ;
2015-01-02 23:15:07 +01:00
public ConcurrentHashMap < Integer , Integer > onlinePrivacy = new ConcurrentHashMap < > ( 20 , 1 . 0f , 2 ) ;
2014-05-20 22:22:44 +02:00
private int lastPrintingStringCount = 0 ;
2013-10-25 17:19:00 +02:00
2016-03-16 13:26:32 +01:00
private HashMap < Long , Boolean > loadingPeerSettings = new HashMap < > ( ) ;
private ArrayList < Long > createdDialogIds = 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 ( ) ;
2014-08-22 16:24:33 +02:00
public boolean loadingBlockedUsers = false ;
2015-01-02 23:15:07 +01:00
public ArrayList < Integer > blockedUsers = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
2015-09-24 22:52:02 +02:00
private SparseArray < ArrayList < Integer > > channelViewsToSend = new SparseArray < > ( ) ;
private SparseArray < ArrayList < Integer > > channelViewsToReload = new SparseArray < > ( ) ;
private long lastViewsCheckTime ;
private HashMap < Integer , ArrayList < TLRPC . Updates > > updatesQueueChannels = new HashMap < > ( ) ;
private HashMap < Integer , Long > updatesStartWaitTimeChannels = new HashMap < > ( ) ;
private HashMap < Integer , Integer > channelsPts = new HashMap < > ( ) ;
private HashMap < Integer , Boolean > gettingDifferenceChannels = new HashMap < > ( ) ;
2015-03-19 00:09:45 +01:00
private ArrayList < TLRPC . Updates > updatesQueueSeq = new ArrayList < > ( ) ;
private ArrayList < TLRPC . Updates > updatesQueuePts = new ArrayList < > ( ) ;
private ArrayList < TLRPC . Updates > updatesQueueQts = new ArrayList < > ( ) ;
private long updatesStartWaitTimeSeq = 0 ;
private long updatesStartWaitTimePts = 0 ;
private long updatesStartWaitTimeQts = 0 ;
2016-03-16 13:26:32 +01:00
private HashMap < Integer , String > fullUsersAbout = new HashMap < > ( ) ;
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 < > ( ) ;
2013-10-25 17:19:00 +02:00
2016-03-06 02:49:31 +01:00
private HashMap < String , ArrayList < MessageObject > > reloadingWebpages = new HashMap < > ( ) ;
private HashMap < Long , ArrayList < MessageObject > > reloadingWebpagesPending = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
private HashMap < Long , ArrayList < Integer > > reloadingMessages = new HashMap < > ( ) ;
2015-01-09 13:50:15 +01:00
2013-10-25 17:19:00 +02:00
private boolean gettingNewDeleteTask = false ;
private int currentDeletingTaskTime = 0 ;
private ArrayList < Integer > currentDeletingTaskMids = null ;
2014-10-14 10:13:16 +02:00
private Runnable currentDeleteTaskRunnable = null ;
2013-10-25 17:19:00 +02:00
public boolean loadingDialogs = false ;
2015-11-26 22:04:02 +01:00
private boolean migratingDialogs = false ;
2013-10-25 17:19:00 +02:00
public boolean dialogsEndReached = false ;
public boolean gettingDifference = false ;
public boolean updatingState = false ;
public boolean firstGettingTask = false ;
public boolean registeringForPush = false ;
2014-07-03 00:39:05 +02:00
2016-03-06 02:49:31 +01:00
public int secretWebpagePreview = 2 ;
2013-10-25 17:19:00 +02:00
private long lastStatusUpdateTime = 0 ;
2015-09-24 22:52:02 +02:00
private int statusRequest = 0 ;
2014-02-28 23:28:25 +01:00
private int statusSettingState = 0 ;
private boolean offlineSent = false ;
2013-10-25 17:19:00 +02:00
private String uploadingAvatar = null ;
2014-07-03 00:39:05 +02:00
2014-02-04 19:36:55 +01:00
public boolean enableJoined = true ;
2014-07-03 00:39:05 +02:00
public int fontSize = AndroidUtilities . dp ( 16 ) ;
2014-08-06 01:17:40 +02:00
public int maxGroupCount = 200 ;
public int maxBroadcastCount = 100 ;
2016-03-16 13:26:32 +01:00
public int maxMegagroupCount = 5000 ;
2015-11-26 22:04:02 +01:00
public int minGroupConvertSize = 200 ;
2016-03-06 02:49:31 +01:00
public int maxEditTime = 172800 ;
2015-02-27 20:57:58 +01:00
public int groupBigSize ;
private ArrayList < TLRPC . TL_disabledFeature > disabledFeatures = new ArrayList < > ( ) ;
2014-02-04 19:36:55 +01:00
2015-03-19 00:09:45 +01:00
private class UserActionUpdatesSeq extends TLRPC . Updates {
}
private class UserActionUpdatesPts extends TLRPC . Updates {
2014-02-04 19:36:55 +01:00
}
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 ;
2014-02-04 19:36:55 +01:00
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 ;
2014-03-22 23:31:55 +01:00
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 ;
2014-12-01 18:56:31 +01:00
public static final int UPDATE_MASK_PHONE = 1024 ;
2015-02-26 02:32:51 +01:00
public static final int UPDATE_MASK_NEW_MESSAGE = 2048 ;
public static final int UPDATE_MASK_SEND_STATE = 4096 ;
2015-09-24 22:52:02 +02:00
public static final int UPDATE_MASK_CHANNEL = 8192 ;
2015-11-26 22:04:02 +01:00
public static final int UPDATE_MASK_CHAT_ADMINS = 16384 ;
2014-12-01 18:56:31 +01:00
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
public static class PrintingUser {
public long lastTime ;
public int userId ;
2015-05-03 13:48:36 +02:00
public TLRPC . SendMessageAction action ;
2013-10-25 17:19:00 +02:00
}
2014-03-22 23:31:55 +01:00
private static volatile MessagesController Instance = null ;
2015-05-21 23:27:27 +02:00
2014-03-22 23:31:55 +01:00
public static MessagesController getInstance ( ) {
MessagesController localInstance = Instance ;
if ( localInstance = = null ) {
synchronized ( MessagesController . class ) {
localInstance = Instance ;
if ( localInstance = = null ) {
Instance = localInstance = new MessagesController ( ) ;
}
}
}
return localInstance ;
}
2013-10-25 17:19:00 +02:00
public MessagesController ( ) {
2014-08-22 16:24:33 +02:00
ImageLoader . getInstance ( ) ;
MessagesStorage . getInstance ( ) ;
NotificationCenter . getInstance ( ) . addObserver ( this , NotificationCenter . FileDidUpload ) ;
NotificationCenter . getInstance ( ) . addObserver ( this , NotificationCenter . FileDidFailUpload ) ;
NotificationCenter . getInstance ( ) . addObserver ( this , NotificationCenter . FileDidLoaded ) ;
NotificationCenter . getInstance ( ) . addObserver ( this , NotificationCenter . FileDidFailedLoad ) ;
NotificationCenter . getInstance ( ) . addObserver ( this , NotificationCenter . messageReceivedByServer ) ;
2013-10-25 17:19:00 +02:00
addSupportUser ( ) ;
2014-02-04 19:36:55 +01:00
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
enableJoined = preferences . getBoolean ( " EnableContactJoined " , true ) ;
2014-08-22 16:24:33 +02:00
2014-02-28 23:28:25 +01:00
preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " mainconfig " , Activity . MODE_PRIVATE ) ;
2016-03-06 02:49:31 +01:00
secretWebpagePreview = preferences . getInt ( " secretWebpage2 " , 2 ) ;
2014-08-06 01:17:40 +02:00
maxGroupCount = preferences . getInt ( " maxGroupCount " , 200 ) ;
2015-11-26 22:04:02 +01:00
maxMegagroupCount = preferences . getInt ( " maxMegagroupCount " , 1000 ) ;
2016-03-06 02:49:31 +01:00
maxEditTime = preferences . getInt ( " maxEditTime " , 3600 ) ;
2015-02-27 20:57:58 +01:00
groupBigSize = preferences . getInt ( " groupBigSize " , 10 ) ;
2014-09-24 04:17:27 +02:00
fontSize = preferences . getInt ( " fons_size " , AndroidUtilities . isTablet ( ) ? 18 : 16 ) ;
2015-02-27 20:57:58 +01:00
String disabledFeaturesString = preferences . getString ( " disabledFeatures " , null ) ;
if ( disabledFeaturesString ! = null & & disabledFeaturesString . length ( ) ! = 0 ) {
try {
byte [ ] bytes = Base64 . decode ( disabledFeaturesString , Base64 . DEFAULT ) ;
if ( bytes ! = null ) {
SerializedData data = new SerializedData ( bytes ) ;
2015-05-03 13:48:36 +02:00
int count = data . readInt32 ( false ) ;
2015-02-27 20:57:58 +01:00
for ( int a = 0 ; a < count ; a + + ) {
2015-05-03 13:48:36 +02:00
TLRPC . TL_disabledFeature feature = TLRPC . TL_disabledFeature . TLdeserialize ( data , data . readInt32 ( false ) , false ) ;
2015-02-27 20:57:58 +01:00
if ( feature ! = null & & feature . feature ! = null & & feature . description ! = null ) {
disabledFeatures . add ( feature ) ;
}
}
}
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
}
2013-10-25 17:19:00 +02:00
}
2014-08-06 01:17:40 +02:00
public void updateConfig ( final TLRPC . TL_config config ) {
2016-03-16 13:26:32 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-06 01:17:40 +02:00
@Override
public void run ( ) {
2015-11-26 22:04:02 +01:00
//maxBroadcastCount = config.broadcast_size_max;
maxMegagroupCount = config . megagroup_size_max ;
2014-08-06 01:17:40 +02:00
maxGroupCount = config . chat_size_max ;
2015-02-27 20:57:58 +01:00
groupBigSize = config . chat_big_size ;
disabledFeatures = config . disabled_features ;
2016-03-06 02:49:31 +01:00
maxEditTime = config . edit_time_limit ;
2015-02-27 20:57:58 +01:00
2014-08-06 01:17:40 +02:00
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " mainconfig " , Activity . MODE_PRIVATE ) ;
SharedPreferences . Editor editor = preferences . edit ( ) ;
editor . putInt ( " maxGroupCount " , maxGroupCount ) ;
2015-11-26 22:04:02 +01:00
//editor.putInt("maxBroadcastCount", maxBroadcastCount);
editor . putInt ( " maxMegagroupCount " , maxMegagroupCount ) ;
2015-02-27 20:57:58 +01:00
editor . putInt ( " groupBigSize " , groupBigSize ) ;
2016-03-06 02:49:31 +01:00
editor . putInt ( " maxEditTime " , maxEditTime ) ;
2015-02-27 20:57:58 +01:00
try {
SerializedData data = new SerializedData ( ) ;
data . writeInt32 ( disabledFeatures . size ( ) ) ;
for ( TLRPC . TL_disabledFeature disabledFeature : disabledFeatures ) {
disabledFeature . serializeToStream ( data ) ;
}
String string = Base64 . encodeToString ( data . toByteArray ( ) , Base64 . DEFAULT ) ;
2015-05-21 23:27:27 +02:00
if ( string . length ( ) ! = 0 ) {
2015-02-27 20:57:58 +01:00
editor . putString ( " disabledFeatures " , string ) ;
}
} catch ( Exception e ) {
editor . remove ( " disabledFeatures " ) ;
FileLog . e ( " tmessages " , e ) ;
}
2014-08-06 01:17:40 +02:00
editor . commit ( ) ;
}
} ) ;
}
2015-02-27 20:57:58 +01:00
public static boolean isFeatureEnabled ( String feature , BaseFragment fragment ) {
if ( feature = = null | | feature . length ( ) = = 0 | | getInstance ( ) . disabledFeatures . isEmpty ( ) | | fragment = = null ) {
return true ;
}
for ( TLRPC . TL_disabledFeature disabledFeature : getInstance ( ) . disabledFeatures ) {
if ( disabledFeature . feature . equals ( feature ) ) {
if ( fragment . getParentActivity ( ) ! = null ) {
AlertDialog . Builder builder = new AlertDialog . Builder ( fragment . getParentActivity ( ) ) ;
builder . setTitle ( " Oops! " ) ;
2015-03-19 00:09:45 +01:00
builder . setPositiveButton ( LocaleController . getString ( " OK " , R . string . OK ) , null ) ;
2015-02-27 20:57:58 +01:00
builder . setMessage ( disabledFeature . description ) ;
2015-05-21 23:27:27 +02:00
fragment . showDialog ( builder . create ( ) ) ;
2015-02-27 20:57:58 +01:00
}
return false ;
}
}
return true ;
}
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 ( ) ;
2014-08-22 16:24:33 +02:00
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 ;
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
}
2014-02-04 19:36:55 +01:00
public static TLRPC . InputUser getInputUser ( TLRPC . User user ) {
if ( user = = null ) {
2015-09-24 22:52:02 +02:00
return new TLRPC . TL_inputUserEmpty ( ) ;
2014-02-04 19:36:55 +01:00
}
2015-05-21 23:27:27 +02:00
TLRPC . InputUser inputUser ;
2014-06-13 12:42:21 +02:00
if ( user . id = = UserConfig . getClientUserId ( ) ) {
2014-02-04 19:36:55 +01:00
inputUser = new TLRPC . TL_inputUserSelf ( ) ;
} else {
2015-09-02 00:14:21 +02:00
inputUser = new TLRPC . TL_inputUser ( ) ;
2014-02-04 19:36:55 +01:00
inputUser . user_id = user . id ;
2015-09-02 00:14:21 +02:00
inputUser . access_hash = user . access_hash ;
2014-02-04 19:36:55 +01:00
}
return inputUser ;
}
2015-09-24 22:52:02 +02:00
public static TLRPC . InputUser getInputUser ( int user_id ) {
2016-03-16 13:26:32 +01:00
TLRPC . User user = getInstance ( ) . 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 ( ) ;
}
}
public static TLRPC . InputChannel getInputChannel ( int chatId ) {
return getInputChannel ( getInstance ( ) . getChat ( chatId ) ) ;
}
public static TLRPC . InputPeer getInputPeer ( int id ) {
TLRPC . InputPeer inputPeer ;
if ( id < 0 ) {
TLRPC . Chat chat = getInstance ( ) . getChat ( - id ) ;
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 {
TLRPC . User user = getInstance ( ) . getUser ( id ) ;
inputPeer = new TLRPC . TL_inputPeerUser ( ) ;
inputPeer . user_id = id ;
if ( user ! = null ) {
inputPeer . access_hash = user . access_hash ;
}
}
return inputPeer ;
}
public static TLRPC . Peer getPeer ( int id ) {
TLRPC . Peer inputPeer ;
if ( id < 0 ) {
TLRPC . Chat chat = getInstance ( ) . getChat ( - id ) ;
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 {
TLRPC . User user = getInstance ( ) . getUser ( id ) ;
inputPeer = new TLRPC . TL_peerUser ( ) ;
inputPeer . user_id = id ;
}
return inputPeer ;
}
2013-10-25 17:19:00 +02:00
@Override
public void didReceivedNotification ( int id , Object . . . args ) {
2014-08-22 16:24:33 +02:00
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 ] ;
2014-07-03 00:39:05 +02:00
if ( uploadingAvatar ! = null & & uploadingAvatar . equals ( location ) ) {
TLRPC . TL_photos_uploadProfilePhoto req = new TLRPC . TL_photos_uploadProfilePhoto ( ) ;
req . caption = " " ;
req . crop = new TLRPC . TL_inputPhotoCropAuto ( ) ;
req . file = file ;
req . geo_point = new TLRPC . TL_inputGeoPointEmpty ( ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-07-03 00:39:05 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2014-08-22 16:24:33 +02:00
TLRPC . User user = getUser ( UserConfig . getClientUserId ( ) ) ;
2014-07-03 00:39:05 +02:00
if ( user = = null ) {
user = UserConfig . getCurrentUser ( ) ;
2014-08-22 16:24:33 +02:00
putUser ( user , true ) ;
2014-07-03 00:39:05 +02:00
} else {
UserConfig . setCurrentUser ( user ) ;
}
if ( user = = null ) {
return ;
}
TLRPC . TL_photos_photo photo = ( TLRPC . TL_photos_photo ) response ;
ArrayList < TLRPC . PhotoSize > sizes = photo . photo . sizes ;
2014-10-01 21:55:24 +02:00
TLRPC . PhotoSize smallSize = FileLoader . getClosestPhotoSizeWithSize ( sizes , 100 ) ;
TLRPC . PhotoSize bigSize = FileLoader . getClosestPhotoSizeWithSize ( sizes , 1000 ) ;
2014-07-03 00:39:05 +02:00
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 ;
}
MessagesStorage . getInstance ( ) . clearUserPhotos ( user . id ) ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . User > users = new ArrayList < > ( ) ;
2014-07-03 00:39:05 +02:00
users . add ( user ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( users , null , false , true ) ;
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-07-03 00:39:05 +02:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_AVATAR ) ;
2014-07-03 00:39:05 +02:00
UserConfig . saveConfig ( true ) ;
}
} ) ;
}
}
} ) ;
}
2014-08-22 16:24:33 +02:00
} else if ( id = = NotificationCenter . FileDidFailUpload ) {
2014-07-03 00:39:05 +02:00
final String location = ( String ) args [ 0 ] ;
if ( uploadingAvatar ! = null & & uploadingAvatar . equals ( location ) ) {
uploadingAvatar = null ;
}
2014-08-22 16:24:33 +02:00
} 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 ) ;
if ( obj ! = null & & obj . getId ( ) = = msgId ) {
2013-10-25 17:19:00 +02:00
obj . messageOwner . id = newMsgId ;
2014-08-22 16:24:33 +02:00
obj . messageOwner . send_state = MessageObject . MESSAGE_SEND_STATE_SENT ;
2015-09-24 22:52:02 +02:00
TLRPC . Dialog dialog = dialogs_dict . get ( did ) ;
2013-10-25 17:19:00 +02:00
if ( dialog ! = null ) {
if ( dialog . top_message = = msgId ) {
dialog . top_message = newMsgId ;
}
}
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2014-07-27 00:28:33 +02:00
}
2015-10-29 18:10:07 +01:00
obj = dialogMessagesByIds . remove ( msgId ) ;
if ( obj ! = null ) {
dialogMessagesByIds . put ( newMsgId , obj ) ;
}
2013-10-25 17:19:00 +02:00
}
}
public void cleanUp ( ) {
2014-03-22 23:31:55 +01:00
ContactsController . getInstance ( ) . cleanup ( ) ;
MediaController . getInstance ( ) . cleanup ( ) ;
2014-07-10 02:15:58 +02:00
NotificationsController . getInstance ( ) . cleanup ( ) ;
2014-08-22 16:24:33 +02:00
SendMessagesHelper . getInstance ( ) . cleanUp ( ) ;
2014-12-01 18:56:31 +01:00
SecretChatHelper . getInstance ( ) . cleanUp ( ) ;
2015-06-29 19:12:11 +02:00
StickersQuery . cleanup ( ) ;
2014-02-04 19:36:55 +01:00
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 ( ) ;
2015-05-03 13:48:36 +02:00
exportedChats . clear ( ) ;
2016-03-16 13:26:32 +01:00
fullUsersAbout . clear ( ) ;
2013-10-25 17:19:00 +02:00
dialogs . clear ( ) ;
2015-10-29 18:10:07 +01:00
joiningToChannels . clear ( ) ;
2015-09-24 22:52:02 +02:00
channelViewsToSend . clear ( ) ;
channelViewsToReload . clear ( ) ;
2013-10-25 17:19:00 +02:00
dialogsServerOnly . clear ( ) ;
2015-06-29 19:12:11 +02:00
dialogsGroupsOnly . clear ( ) ;
2016-03-06 02:49:31 +01:00
dialogMessagesByIds . clear ( ) ;
dialogMessagesByRandomIds . clear ( ) ;
2013-10-25 17:19:00 +02:00
users . clear ( ) ;
2015-03-19 00:09:45 +01:00
usersByUsernames . clear ( ) ;
2013-10-25 17:19:00 +02:00
chats . clear ( ) ;
dialogMessage . clear ( ) ;
printingUsers . clear ( ) ;
printingStrings . clear ( ) ;
2015-05-03 13:48:36 +02:00
printingStringsTypes . clear ( ) ;
2014-11-21 01:14:44 +01:00
onlinePrivacy . clear ( ) ;
2016-03-16 13:26:32 +01:00
loadingPeerSettings . clear ( ) ;
2014-05-20 22:22:44 +02:00
lastPrintingStringCount = 0 ;
2015-09-24 22:52:02 +02:00
nextDialogsCacheOffset = 0 ;
2016-01-11 18:19:48 +01:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
updatesQueueSeq . clear ( ) ;
updatesQueuePts . clear ( ) ;
updatesQueueQts . clear ( ) ;
updatesStartWaitTimeSeq = 0 ;
updatesStartWaitTimePts = 0 ;
updatesStartWaitTimeQts = 0 ;
2016-03-16 13:26:32 +01:00
createdDialogIds . clear ( ) ;
2016-01-11 18:19:48 +01:00
gettingDifference = false ;
}
} ) ;
2014-08-22 16:24:33 +02:00
blockedUsers . clear ( ) ;
2014-08-23 01:22:33 +02:00
sendingTypings . clear ( ) ;
2014-10-17 20:29:13 +02:00
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
currentDeletingTaskTime = 0 ;
currentDeletingTaskMids = null ;
gettingNewDeleteTask = false ;
loadingDialogs = false ;
dialogsEndReached = false ;
2014-08-22 16:24:33 +02:00
loadingBlockedUsers = false ;
2013-10-25 17:19:00 +02:00
firstGettingTask = false ;
updatingState = false ;
lastStatusUpdateTime = 0 ;
2014-02-28 23:28:25 +01:00
offlineSent = false ;
2013-10-25 17:19:00 +02:00
registeringForPush = false ;
uploadingAvatar = null ;
2014-02-28 23:28:25 +01:00
statusRequest = 0 ;
statusSettingState = 0 ;
2014-10-14 10:13:16 +02:00
2015-09-24 22:52:02 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
2015-10-29 18:10:07 +01:00
ConnectionsManager . getInstance ( ) . setIsUpdating ( false ) ;
2015-09-24 22:52:02 +02:00
updatesQueueChannels . clear ( ) ;
updatesStartWaitTimeChannels . clear ( ) ;
gettingDifferenceChannels . clear ( ) ;
channelsPts . clear ( ) ;
shortPollChannels . clear ( ) ;
needShortPollChannels . clear ( ) ;
}
} ) ;
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 ( ) ;
}
2014-08-22 16:24:33 +02:00
public TLRPC . User getUser ( Integer id ) {
return users . get ( id ) ;
}
2015-03-19 00:09:45 +01:00
public TLRPC . User getUser ( String username ) {
2015-05-21 23:27:27 +02:00
if ( username = = null | | username . length ( ) = = 0 ) {
return null ;
}
return usersByUsernames . get ( username . toLowerCase ( ) ) ;
2015-03-19 00:09:45 +01:00
}
public ConcurrentHashMap < Integer , TLRPC . User > getUsers ( ) {
return users ;
}
2014-08-22 16:24:33 +02:00
public TLRPC . Chat getChat ( Integer id ) {
return chats . get ( id ) ;
}
public TLRPC . EncryptedChat getEncryptedChat ( Integer id ) {
return encryptedChats . get ( id ) ;
}
public TLRPC . EncryptedChat getEncryptedChatDB ( int chat_id ) {
TLRPC . EncryptedChat chat = encryptedChats . get ( chat_id ) ;
if ( chat = = null ) {
Semaphore semaphore = new Semaphore ( 0 ) ;
2015-01-02 23:15:07 +01:00
ArrayList < TLObject > result = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
MessagesStorage . getInstance ( ) . getEncryptedChat ( chat_id , semaphore , result ) ;
try {
semaphore . acquire ( ) ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , 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 ) ;
2014-08-22 16:24:33 +02:00
putEncryptedChat ( chat , false ) ;
putUser ( user , true ) ;
}
}
return chat ;
}
2016-03-06 02:49:31 +01:00
public void setLastCreatedDialogId ( final long dialog_id , final boolean set ) {
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
if ( set ) {
2016-03-16 13:26:32 +01:00
createdDialogIds . add ( dialog_id ) ;
} else {
createdDialogIds . remove ( dialog_id ) ;
2016-03-06 02:49:31 +01:00
}
}
} ) ;
}
2015-05-03 13:48:36 +02:00
public TLRPC . ExportedChatInvite getExportedInvite ( int chat_id ) {
return exportedChats . get ( chat_id ) ;
}
2014-08-22 16:24:33 +02:00
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 ;
2014-08-22 16:24:33 +02:00
TLRPC . User oldUser = users . get ( user . id ) ;
2015-03-19 00:09:45 +01:00
if ( oldUser ! = null & & oldUser . username ! = null & & oldUser . username . length ( ) > 0 ) {
usersByUsernames . remove ( oldUser . username ) ;
}
if ( user . username ! = null & & user . username . length ( ) > 0 ) {
2015-05-21 23:27:27 +02:00
usersByUsernames . put ( user . username . toLowerCase ( ) , user ) ;
2015-03-19 00:09:45 +01:00
}
2016-03-06 02:49:31 +01:00
if ( user . min ) {
if ( oldUser ! = null ) {
if ( ! fromCache ) {
if ( user . first_name ! = null ) {
oldUser . first_name = user . first_name ;
oldUser . flags | = 2 ;
} else {
oldUser . first_name = null ;
oldUser . flags = oldUser . flags & ~ 2 ;
}
if ( user . last_name ! = null ) {
oldUser . last_name = user . last_name ;
oldUser . flags | = 4 ;
} else {
oldUser . last_name = null ;
oldUser . flags = oldUser . flags & ~ 4 ;
}
2016-03-16 13:26:32 +01:00
if ( user . username ! = null ) {
oldUser . username = user . username ;
oldUser . flags | = 8 ;
} else {
oldUser . username = null ;
oldUser . flags = oldUser . flags & ~ 8 ;
}
2016-03-06 02:49:31 +01:00
if ( user . photo ! = null ) {
oldUser . photo = user . photo ;
oldUser . flags | = 32 ;
} else {
oldUser . photo = null ;
oldUser . flags = oldUser . flags & ~ 32 ;
}
}
} else {
users . put ( user . id , user ) ;
2014-08-22 16:24:33 +02:00
}
2016-03-06 02:49:31 +01:00
} else {
if ( ! fromCache ) {
users . put ( user . id , user ) ;
if ( user . id = = UserConfig . getClientUserId ( ) ) {
UserConfig . setCurrentUser ( user ) ;
UserConfig . saveConfig ( true ) ;
}
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 ;
if ( oldUser . first_name ! = null ) {
user . first_name = oldUser . first_name ;
user . flags | = 2 ;
} else {
user . first_name = null ;
user . flags = user . flags & ~ 2 ;
}
if ( oldUser . last_name ! = null ) {
user . last_name = oldUser . last_name ;
user . flags | = 4 ;
} else {
user . last_name = null ;
user . flags = user . flags & ~ 4 ;
}
if ( oldUser . username ! = null ) {
user . username = oldUser . username ;
user . flags | = 8 ;
} else {
user . username = null ;
user . flags = user . flags & ~ 8 ;
}
if ( oldUser . photo ! = null ) {
user . photo = oldUser . photo ;
user . flags | = 32 ;
} else {
user . photo = null ;
user . flags = user . flags & ~ 32 ;
}
users . put ( user . id , user ) ;
2014-08-22 16:24:33 +02:00
}
}
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 ) ;
2014-08-22 16:24:33 +02:00
if ( putUser ( user , fromCache ) ) {
updateStatus = true ;
}
}
if ( updateStatus ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_STATUS ) ;
}
} ) ;
}
}
public void putChat ( TLRPC . Chat chat , boolean fromCache ) {
if ( chat = = null ) {
return ;
}
2015-09-02 00:14:21 +02:00
TLRPC . Chat oldChat = chats . get ( chat . id ) ;
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 ;
oldChat . democracy = chat . democracy ;
if ( chat . username ! = null ) {
oldChat . username = chat . username ;
oldChat . flags | = 64 ;
} else {
oldChat . username = null ;
oldChat . flags = oldChat . flags & ~ 64 ;
}
}
} else {
chats . put ( chat . id , chat ) ;
}
} else {
if ( ! fromCache ) {
if ( oldChat ! = null & & chat . version ! = oldChat . version ) {
loadedFullChats . remove ( ( Integer ) chat . id ) ;
}
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 ;
chat . democracy = oldChat . democracy ;
if ( oldChat . username ! = null ) {
chat . username = oldChat . username ;
chat . flags | = 64 ;
} else {
chat . username = null ;
chat . flags = chat . flags & ~ 64 ;
}
chats . put ( chat . id , chat ) ;
2015-09-02 00:14:21 +02:00
}
2014-08-22 16:24:33 +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 ) ;
2014-08-22 16:24:33 +02:00
putChat ( chat , fromCache ) ;
}
}
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 ) ;
2014-08-22 16:24:33 +02:00
putEncryptedChat ( encryptedChat , fromCache ) ;
}
}
2016-03-16 13:26:32 +01:00
public String getUserAbout ( int uid ) {
return fullUsersAbout . get ( uid ) ;
}
2014-10-17 20:29:13 +02:00
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 ( ) ;
}
2015-05-03 13:48:36 +02:00
public void loadFullChat ( final int chat_id , final int classGuid , boolean force ) {
if ( loadingFullChats . contains ( chat_id ) | | ! force & & loadedFullChats . contains ( chat_id ) ) {
2014-11-19 16:17:24 +01:00
return ;
}
loadingFullChats . add ( chat_id ) ;
2015-09-24 22:52:02 +02:00
TLObject request ;
2015-11-26 22:04:02 +01:00
final TLRPC . Chat chat = getChat ( chat_id ) ;
2015-09-24 22:52:02 +02:00
if ( ChatObject . isChannel ( chat_id ) ) {
TLRPC . TL_channels_getFullChannel req = new TLRPC . TL_channels_getFullChannel ( ) ;
req . channel = getInputChannel ( chat_id ) ;
request = req ;
} else {
TLRPC . TL_messages_getFullChat req = new TLRPC . TL_messages_getFullChat ( ) ;
req . chat_id = chat_id ;
request = req ;
}
int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2014-11-19 16:17:24 +01:00
@Override
2015-09-24 22:52:02 +02:00
public void run ( TLObject response , final TLRPC . TL_error error ) {
2014-11-19 16:17:24 +01:00
if ( error = = null ) {
final TLRPC . TL_messages_chatFull res = ( TLRPC . TL_messages_chatFull ) response ;
2015-11-26 22:04:02 +01:00
if ( chat ! = null & & chat . megagroup ) {
res . full_chat . unread_important_count = Math . max ( res . full_chat . unread_important_count , res . full_chat . unread_count ) ;
}
2014-11-19 16:17:24 +01:00
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , res . chats , true , true ) ;
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . updateChatInfo ( res . full_chat , false ) ;
2014-11-19 16:17:24 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2015-11-26 22:04:02 +01:00
applyDialogNotificationsSettings ( - chat_id , res . full_chat . notify_settings ) ;
2015-06-29 19:12:11 +02:00
for ( int a = 0 ; a < res . full_chat . bot_info . size ( ) ; a + + ) {
TLRPC . BotInfo botInfo = res . full_chat . bot_info . get ( a ) ;
BotQuery . putBotInfo ( botInfo ) ;
}
2015-05-03 13:48:36 +02:00
exportedChats . put ( chat_id , res . full_chat . exported_invite ) ;
loadingFullChats . remove ( ( Integer ) chat_id ) ;
2014-11-19 16:17:24 +01:00
loadedFullChats . add ( chat_id ) ;
2015-09-24 22:52:02 +02:00
if ( ! res . chats . isEmpty ( ) ) {
TLRPC . Chat chat = res . chats . get ( 0 ) ;
chat . address = res . full_chat . about ;
}
2014-11-19 16:17:24 +01:00
putUsers ( res . users , false ) ;
putChats ( res . chats , false ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoDidLoaded , res . full_chat , classGuid , false , null ) ;
2014-11-19 16:17:24 +01:00
}
} ) ;
} else {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2016-03-16 13:26:32 +01:00
checkChannelError ( error . text , chat_id ) ;
2014-11-19 16:17:24 +01:00
loadingFullChats . remove ( ( Integer ) chat_id ) ;
}
} ) ;
}
}
} ) ;
if ( classGuid ! = 0 ) {
ConnectionsManager . getInstance ( ) . bindRequestToGuid ( reqId , classGuid ) ;
}
}
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 ) ) {
2014-08-22 16:24:33 +02:00
return ;
}
2014-10-17 20:29:13 +02:00
loadingFullUsers . add ( user . id ) ;
2014-08-22 16:24:33 +02:00
TLRPC . TL_users_getFullUser req = new TLRPC . TL_users_getFullUser ( ) ;
req . id = getInputUser ( user ) ;
2015-09-24 22:52:02 +02:00
int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( final TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
2015-06-29 19:12:11 +02:00
TLRPC . TL_userFull userFull = ( TLRPC . TL_userFull ) response ;
2015-11-26 22:04:02 +01:00
applyDialogNotificationsSettings ( user . id , userFull . notify_settings ) ;
2015-06-29 19:12:11 +02:00
if ( userFull . bot_info instanceof TLRPC . TL_botInfo ) {
BotQuery . putBotInfo ( userFull . bot_info ) ;
}
2016-03-16 13:26:32 +01:00
if ( userFull . about ! = null & & userFull . about . length ( ) > 0 ) {
fullUsersAbout . put ( user . id , userFull . about ) ;
} else {
fullUsersAbout . remove ( user . id ) ;
}
2015-05-03 13:48:36 +02:00
loadingFullUsers . remove ( ( Integer ) user . id ) ;
2014-10-17 20:29:13 +02:00
loadedFullUsers . add ( user . id ) ;
String names = user . first_name + user . last_name + user . username ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . User > users = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
users . add ( userFull . user ) ;
putUsers ( users , false ) ;
2014-10-17 20:29:13 +02:00
MessagesStorage . getInstance ( ) . putUsersAndChats ( users , null , false , true ) ;
2015-11-26 22:04:02 +01:00
if ( names ! = null & & ! names . equals ( userFull . user . first_name + userFull . user . last_name + userFull . user . username ) ) {
2014-10-17 20:29:13 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_NAME ) ;
}
2015-06-29 19:12:11 +02:00
if ( userFull . bot_info instanceof TLRPC . TL_botInfo ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . botInfoDidLoaded , userFull . bot_info , classGuid ) ;
}
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . userInfoDidLoaded , user . id ) ;
2014-10-17 20:29:13 +02:00
}
} ) ;
} else {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-10-17 20:29:13 +02:00
@Override
public void run ( ) {
2015-05-21 23:27:27 +02:00
loadingFullUsers . remove ( ( Integer ) user . id ) ;
2014-08-22 16:24:33 +02:00
}
} ) ;
}
}
} ) ;
ConnectionsManager . getInstance ( ) . 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 < > ( ) ;
2015-11-26 22:04:02 +01:00
final TLRPC . Chat chat = ChatObject . getChatByDialog ( dialog_id ) ;
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 ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2015-01-09 13:50:15 +01:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . messages_Messages messagesRes = ( TLRPC . messages_Messages ) response ;
2015-11-26 22:04:02 +01:00
final HashMap < Integer , TLRPC . User > usersLocal = new HashMap < > ( ) ;
for ( int a = 0 ; a < messagesRes . users . size ( ) ; a + + ) {
TLRPC . User u = messagesRes . users . get ( a ) ;
usersLocal . put ( u . id , u ) ;
}
final HashMap < Integer , TLRPC . Chat > chatsLocal = new HashMap < > ( ) ;
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
final ArrayList < MessageObject > objects = new ArrayList < > ( ) ;
2015-11-26 22:04:02 +01:00
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
}
2015-11-26 22:04:02 +01:00
message . dialog_id = dialog_id ;
objects . add ( new MessageObject ( message , usersLocal , chatsLocal , true ) ) ;
2015-01-09 13:50:15 +01:00
}
2015-11-26 22:04:02 +01:00
ImageLoader . saveMessagesThumbs ( messagesRes . messages ) ;
MessagesStorage . getInstance ( ) . putMessages ( messagesRes , dialog_id , - 1 , 0 , 0 , false ) ;
2015-01-09 13:50:15 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
ArrayList < Integer > arrayList = reloadingMessages . get ( dialog_id ) ;
if ( arrayList ! = null ) {
arrayList . removeAll ( result ) ;
if ( arrayList . isEmpty ( ) ) {
reloadingMessages . remove ( dialog_id ) ;
}
}
2016-01-11 18:19:48 +01:00
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 ) {
obj = dialogMessagesByIds . remove ( obj . getId ( ) ) ;
if ( obj ! = null ) {
dialogMessagesByIds . put ( obj . getId ( ) , obj ) ;
}
}
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
break ;
}
}
}
2015-01-09 13:50:15 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . replaceMessagesObjects , dialog_id , objects ) ;
}
} ) ;
}
}
} ) ;
}
2016-03-16 13:26:32 +01:00
public void hideReportSpam ( final long dialogId , TLRPC . User currentUser , TLRPC . Chat currentChat ) {
if ( currentUser = = null & & currentChat = = null ) {
return ;
}
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
SharedPreferences . Editor editor = preferences . edit ( ) ;
editor . putInt ( " spam3_ " + dialogId , 1 ) ;
editor . commit ( ) ;
TLRPC . TL_messages_hideReportSpam req = new TLRPC . TL_messages_hideReportSpam ( ) ;
if ( currentUser ! = null ) {
req . peer = MessagesController . getInputPeer ( currentUser . id ) ;
} else if ( currentChat ! = null ) {
req . peer = MessagesController . getInputPeer ( - currentChat . id ) ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
}
} ) ;
}
public void reportSpam ( final long dialogId , TLRPC . User currentUser , TLRPC . Chat currentChat ) {
if ( currentUser = = null & & currentChat = = null ) {
return ;
}
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
SharedPreferences . Editor editor = preferences . edit ( ) ;
editor . putInt ( " spam3_ " + dialogId , 1 ) ;
editor . commit ( ) ;
TLRPC . TL_messages_reportSpam req = new TLRPC . TL_messages_reportSpam ( ) ;
if ( currentChat ! = null ) {
req . peer = MessagesController . getInputPeer ( - currentChat . id ) ;
} else if ( currentUser ! = null ) {
req . peer = MessagesController . getInputPeer ( currentUser . id ) ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
}
} , ConnectionsManager . RequestFlagFailOnServerErrors ) ;
}
public void loadPeerSettings ( final long dialogId , TLRPC . User currentUser , TLRPC . Chat currentChat ) {
if ( loadingPeerSettings . containsKey ( dialogId ) | | currentUser = = null & & currentChat = = null ) {
return ;
}
loadingPeerSettings . put ( dialogId , true ) ;
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
if ( preferences . getInt ( " spam3_ " + dialogId , 0 ) = = 1 ) {
return ;
}
boolean hidden = preferences . getBoolean ( " spam_ " + dialogId , false ) ;
if ( hidden ) {
TLRPC . TL_messages_hideReportSpam req = new TLRPC . TL_messages_hideReportSpam ( ) ;
if ( currentUser ! = null ) {
req . peer = MessagesController . getInputPeer ( currentUser . id ) ;
} else if ( currentChat ! = null ) {
req . peer = MessagesController . getInputPeer ( - currentChat . id ) ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
loadingPeerSettings . remove ( dialogId ) ;
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
SharedPreferences . Editor editor = preferences . edit ( ) ;
editor . remove ( " spam_ " + dialogId ) ;
editor . putInt ( " spam3_ " + dialogId , 1 ) ;
editor . commit ( ) ;
}
} ) ;
}
} ) ;
return ;
}
TLRPC . TL_messages_getPeerSettings req = new TLRPC . TL_messages_getPeerSettings ( ) ;
if ( currentUser ! = null ) {
req . peer = MessagesController . getInputPeer ( currentUser . id ) ;
} else if ( currentChat ! = null ) {
req . peer = MessagesController . getInputPeer ( - currentChat . id ) ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( final TLObject response , TLRPC . TL_error error ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
loadingPeerSettings . remove ( dialogId ) ;
if ( response ! = null ) {
TLRPC . TL_peerSettings res = ( TLRPC . TL_peerSettings ) response ;
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
SharedPreferences . Editor editor = preferences . edit ( ) ;
if ( ! res . report_spam ) {
editor . putInt ( " spam3_ " + dialogId , 1 ) ;
} else {
editor . putInt ( " spam3_ " + dialogId , 2 ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . peerSettingsDidLoaded , dialogId ) ;
}
editor . commit ( ) ;
}
}
} ) ;
}
} ) ;
}
2015-09-24 22:52:02 +02:00
protected void processNewChannelDifferenceParams ( int pts , int pts_count , int channelId ) {
FileLog . e ( " tmessages " , " processNewChannelDifferenceParams pts = " + pts + " pts_count = " + pts_count + " channeldId = " + channelId ) ;
TLRPC . Dialog dialog = dialogs_dict . get ( ( long ) - channelId ) ;
if ( dialog instanceof TLRPC . TL_dialogChannel ) {
Integer channelPts = channelsPts . get ( channelId ) ;
if ( channelPts = = null ) {
channelPts = MessagesStorage . getInstance ( ) . getChannelPtsSync ( channelId ) ;
if ( channelPts = = 0 ) {
channelPts = 1 ;
}
channelsPts . put ( channelId , channelPts ) ;
}
if ( channelPts + pts_count = = pts ) {
FileLog . e ( " tmessages " , " APPLY CHANNEL PTS " ) ;
channelsPts . put ( channelId , pts ) ;
MessagesStorage . getInstance ( ) . saveChannelPts ( channelId , pts ) ;
} else if ( channelPts ! = pts ) {
Long updatesStartWaitTime = updatesStartWaitTimeChannels . get ( channelId ) ;
Boolean gettingDifferenceChannel = gettingDifferenceChannels . get ( channelId ) ;
if ( gettingDifferenceChannel = = null ) {
gettingDifferenceChannel = false ;
}
2015-11-26 22:04:02 +01:00
if ( gettingDifferenceChannel | | updatesStartWaitTime = = null | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTime ) < = 1500 ) {
2015-09-24 22:52:02 +02:00
FileLog . e ( " tmessages " , " ADD CHANNEL UPDATE TO QUEUE pts = " + pts + " pts_count = " + pts_count ) ;
if ( updatesStartWaitTime = = null ) {
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 ) ;
}
arrayList . add ( updates ) ;
} else {
getChannelDifference ( channelId ) ;
}
}
}
}
2015-03-19 00:09:45 +01:00
protected void processNewDifferenceParams ( int seq , int pts , int date , int pts_count ) {
2015-03-20 00:03:19 +01:00
FileLog . e ( " tmessages " , " processNewDifferenceParams seq = " + seq + " pts = " + pts + " date = " + date + " pts_count = " + pts_count ) ;
2015-03-19 00:09:45 +01:00
if ( pts ! = - 1 ) {
if ( MessagesStorage . lastPtsValue + pts_count = = pts ) {
2015-03-20 00:03:19 +01:00
FileLog . e ( " tmessages " , " APPLY PTS " ) ;
2014-08-22 16:24:33 +02:00
MessagesStorage . lastPtsValue = pts ;
2015-03-19 00:09:45 +01:00
MessagesStorage . getInstance ( ) . saveDiffParams ( MessagesStorage . lastSeqValue , MessagesStorage . lastPtsValue , MessagesStorage . lastDateValue , MessagesStorage . lastQtsValue ) ;
2015-03-20 00:03:19 +01:00
} else if ( MessagesStorage . lastPtsValue ! = pts ) {
2015-11-26 22:04:02 +01:00
if ( gettingDifference | | updatesStartWaitTimePts = = 0 | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTimePts ) < = 1500 ) {
2015-03-20 00:03:19 +01:00
FileLog . e ( " tmessages " , " ADD UPDATE TO QUEUE pts = " + pts + " pts_count = " + pts_count ) ;
2015-03-19 00:09:45 +01:00
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 ;
2015-03-19 00:09:45 +01:00
updatesQueuePts . add ( updates ) ;
} else {
getDifference ( ) ;
}
2014-08-22 16:24:33 +02:00
}
2015-03-19 00:09:45 +01:00
}
if ( seq ! = - 1 ) {
if ( MessagesStorage . lastSeqValue + 1 = = seq ) {
2015-03-20 00:03:19 +01:00
FileLog . e ( " tmessages " , " APPLY SEQ " ) ;
2015-03-19 00:09:45 +01:00
MessagesStorage . lastSeqValue = seq ;
if ( date ! = - 1 ) {
MessagesStorage . lastDateValue = date ;
}
MessagesStorage . getInstance ( ) . saveDiffParams ( MessagesStorage . lastSeqValue , MessagesStorage . lastPtsValue , MessagesStorage . lastDateValue , MessagesStorage . lastQtsValue ) ;
} else if ( MessagesStorage . lastSeqValue ! = seq ) {
2015-11-26 22:04:02 +01:00
if ( gettingDifference | | updatesStartWaitTimeSeq = = 0 | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTimeSeq ) < = 1500 ) {
2015-03-20 00:03:19 +01:00
FileLog . e ( " tmessages " , " ADD UPDATE TO QUEUE seq = " + seq ) ;
2015-03-19 00:09:45 +01:00
if ( updatesStartWaitTimeSeq = = 0 ) {
updatesStartWaitTimeSeq = System . currentTimeMillis ( ) ;
}
UserActionUpdatesSeq updates = new UserActionUpdatesSeq ( ) ;
updates . seq = seq ;
updatesQueueSeq . add ( updates ) ;
} else {
getDifference ( ) ;
2014-08-22 16:24:33 +02:00
}
}
}
}
2014-10-10 19:16:39 +02:00
public void didAddedNewTask ( final int minDate , final SparseArray < ArrayList < Integer > > mids ) {
2013-10-25 17:19:00 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
2014-10-08 22:22:22 +02:00
if ( currentDeletingTaskMids = = null & & ! gettingNewDeleteTask | | currentDeletingTaskTime ! = 0 & & minDate < currentDeletingTaskTime ) {
2013-10-25 17:19:00 +02:00
getNewDeleteTask ( null ) ;
}
}
} ) ;
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-10-10 19:16:39 +02:00
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . didCreatedNewDeleteTask , mids ) ;
}
} ) ;
2013-10-25 17:19:00 +02:00
}
2014-10-08 22:22:22 +02:00
public void getNewDeleteTask ( final ArrayList < Integer > oldTask ) {
2013-10-25 17:19:00 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
gettingNewDeleteTask = true ;
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . getNewTask ( oldTask ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
2014-10-14 10:13:16 +02:00
private boolean checkDeletingTask ( boolean runnable ) {
2014-03-22 23:31:55 +01:00
int currentServerTime = ConnectionsManager . getInstance ( ) . 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 ;
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
deleteMessages ( currentDeletingTaskMids , null , null , 0 ) ;
2013-10-25 17:19:00 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
2014-10-08 22:22:22 +02:00
getNewDeleteTask ( currentDeletingTaskMids ) ;
2013-10-25 17:19:00 +02:00
currentDeletingTaskTime = 0 ;
2014-10-08 22:22:22 +02:00
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
}
2014-10-08 22:22:22 +02:00
public void processLoadedDeleteTask ( final int taskTime , final ArrayList < Integer > messages ) {
2013-10-25 17:19:00 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
gettingNewDeleteTask = false ;
2014-10-08 22:22:22 +02:00
if ( messages ! = null ) {
2013-10-25 17:19:00 +02:00
currentDeletingTaskTime = taskTime ;
currentDeletingTaskMids = messages ;
2014-10-14 10:13:16 +02:00
if ( currentDeleteTaskRunnable ! = null ) {
Utilities . stageQueue . cancelRunnable ( currentDeleteTaskRunnable ) ;
currentDeleteTaskRunnable = null ;
}
if ( ! checkDeletingTask ( false ) ) {
currentDeleteTaskRunnable = new Runnable ( ) {
@Override
public void run ( ) {
checkDeletingTask ( true ) ;
}
} ;
int currentServerTime = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
2015-05-21 23:27:27 +02:00
Utilities . stageQueue . postRunnable ( currentDeleteTaskRunnable , ( long ) Math . abs ( currentServerTime - currentDeletingTaskTime ) * 1000 ) ;
2014-10-14 10:13:16 +02:00
}
2013-10-25 17:19:00 +02:00
} else {
currentDeletingTaskTime = 0 ;
currentDeletingTaskMids = null ;
}
}
} ) ;
}
2013-12-20 20:25:49 +01:00
public void loadUserPhotos ( final int uid , final int offset , final int count , final long max_id , final boolean fromCache , final int classGuid ) {
if ( fromCache ) {
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . getUserPhotos ( uid , offset , count , max_id , classGuid ) ;
2013-12-20 20:25:49 +01:00
} else {
2014-08-22 16:24:33 +02:00
TLRPC . User user = getUser ( uid ) ;
2013-12-20 20:25:49 +01:00
if ( user = = null ) {
return ;
}
TLRPC . TL_photos_getUserPhotos req = new TLRPC . TL_photos_getUserPhotos ( ) ;
req . limit = count ;
req . offset = offset ;
2015-05-21 23:27:27 +02:00
req . max_id = ( int ) max_id ;
2014-02-04 19:36:55 +01:00
req . user_id = getInputUser ( user ) ;
2015-09-24 22:52:02 +02:00
int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-12-20 20:25:49 +01:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2014-03-22 23:31:55 +01:00
TLRPC . photos_Photos res = ( TLRPC . photos_Photos ) response ;
2015-05-21 23:27:27 +02:00
processLoadedUserPhotos ( res , uid , offset , count , max_id , false , classGuid ) ;
2013-12-20 20:25:49 +01:00
}
}
2014-07-03 00:39:05 +02:00
} ) ;
2014-03-22 23:31:55 +01:00
ConnectionsManager . getInstance ( ) . bindRequestToGuid ( reqId , classGuid ) ;
2013-12-20 20:25:49 +01:00
}
}
2014-08-22 16:24:33 +02:00
public void blockUser ( int user_id ) {
final TLRPC . User user = getUser ( user_id ) ;
2016-03-16 13:26:32 +01:00
if ( user = = null | | blockedUsers . contains ( user_id ) ) {
2014-08-22 16:24:33 +02:00
return ;
}
blockedUsers . add ( user_id ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . blockedUsersDidLoaded ) ;
TLRPC . TL_contacts_block req = new TLRPC . TL_contacts_block ( ) ;
2016-03-16 13:26:32 +01:00
req . id = getInputUser ( user ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2015-01-02 23:15:07 +01:00
ArrayList < Integer > ids = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
ids . add ( user . id ) ;
MessagesStorage . getInstance ( ) . putBlockedUsers ( ids , false ) ;
}
}
} ) ;
}
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 ) ;
2014-08-22 16:24:33 +02:00
if ( user = = null ) {
return ;
}
2015-03-19 00:09:45 +01:00
blockedUsers . remove ( ( Integer ) user . id ) ;
2016-03-16 13:26:32 +01:00
req . id = getInputUser ( user ) ;
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . blockedUsersDidLoaded ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
MessagesStorage . getInstance ( ) . deleteBlockedUser ( user . id ) ;
}
} ) ;
}
public void getBlockedUsers ( boolean cache ) {
if ( ! UserConfig . isClientActivated ( ) | | loadingBlockedUsers ) {
return ;
}
loadingBlockedUsers = true ;
if ( cache ) {
MessagesStorage . getInstance ( ) . getBlockedUsers ( ) ;
} else {
TLRPC . TL_contacts_getBlocked req = new TLRPC . TL_contacts_getBlocked ( ) ;
req . offset = 0 ;
req . limit = 200 ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2015-01-02 23:15:07 +01:00
ArrayList < Integer > blocked = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
ArrayList < TLRPC . User > users = null ;
if ( error = = null ) {
2015-05-21 23:27:27 +02:00
final TLRPC . contacts_Blocked res = ( TLRPC . contacts_Blocked ) response ;
2014-08-22 16:24:33 +02:00
for ( TLRPC . TL_contactBlocked contactBlocked : res . blocked ) {
blocked . add ( contactBlocked . user_id ) ;
}
users = res . users ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , null , true , true ) ;
MessagesStorage . getInstance ( ) . putBlockedUsers ( blocked , true ) ;
}
processLoadedBlockedUsers ( blocked , users , false ) ;
}
} ) ;
}
}
public void processLoadedBlockedUsers ( final ArrayList < Integer > ids , final ArrayList < TLRPC . User > users , final boolean cache ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
if ( users ! = null ) {
2016-03-16 13:26:32 +01:00
putUsers ( users , cache ) ;
2014-08-22 16:24:33 +02:00
}
loadingBlockedUsers = false ;
if ( ids . isEmpty ( ) & & cache & & ! UserConfig . blockedUsersLoaded ) {
getBlockedUsers ( false ) ;
return ;
} else if ( ! cache ) {
UserConfig . blockedUsersLoaded = true ;
UserConfig . saveConfig ( false ) ;
}
blockedUsers = ids ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . blockedUsersDidLoaded ) ;
}
} ) ;
}
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 ( ) ;
req . crop = new TLRPC . TL_inputPhotoCropAuto ( ) ;
UserConfig . getCurrentUser ( ) . photo = new TLRPC . TL_userProfilePhotoEmpty ( ) ;
2016-03-16 13:26:32 +01:00
TLRPC . User user = getUser ( UserConfig . getClientUserId ( ) ) ;
2014-08-22 16:24:33 +02:00
if ( user = = null ) {
user = UserConfig . getCurrentUser ( ) ;
}
if ( user = = null ) {
return ;
}
2015-05-21 23:27:27 +02:00
user . photo = UserConfig . getCurrentUser ( ) . photo ;
2014-11-17 03:44:57 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . mainUserInfoChanged ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_ALL ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2016-03-16 13:26:32 +01:00
TLRPC . User user = getUser ( UserConfig . getClientUserId ( ) ) ;
2014-08-22 16:24:33 +02:00
if ( user = = null ) {
user = UserConfig . getCurrentUser ( ) ;
2016-03-16 13:26:32 +01:00
putUser ( user , false ) ;
2014-08-22 16:24:33 +02:00
} else {
UserConfig . setCurrentUser ( user ) ;
}
if ( user = = null ) {
return ;
}
MessagesStorage . getInstance ( ) . clearUserPhotos ( user . id ) ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . User > users = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
users . add ( user ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( users , null , false , true ) ;
2015-03-19 00:09:45 +01:00
user . photo = ( TLRPC . UserProfilePhoto ) response ;
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
2015-02-26 02:32:51 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . mainUserInfoChanged ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_ALL ) ;
2014-08-22 16:24:33 +02:00
UserConfig . saveConfig ( true ) ;
}
} ) ;
}
}
} ) ;
} else {
TLRPC . TL_photos_deletePhotos req = new TLRPC . TL_photos_deletePhotos ( ) ;
req . id . add ( photo ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
}
} ) ;
}
}
2013-12-20 20:25:49 +01:00
public void processLoadedUserPhotos ( final TLRPC . photos_Photos res , final int uid , final int offset , final int count , final long max_id , final boolean fromCache , final int classGuid ) {
if ( ! fromCache ) {
2014-07-14 23:27:26 +02:00
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , null , true , true ) ;
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . putUserPhotos ( uid , res ) ;
2013-12-20 20:25:49 +01:00
} else if ( res = = null | | res . photos . isEmpty ( ) ) {
loadUserPhotos ( uid , offset , count , max_id , false , classGuid ) ;
return ;
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-12-20 20:25:49 +01:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
putUsers ( res . users , fromCache ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . userPhotosLoaded , uid , offset , count , fromCache , classGuid , res . photos ) ;
2013-12-20 20:25:49 +01:00
}
} ) ;
}
2013-10-25 17:19:00 +02:00
public void uploadAndApplyUserAvatar ( TLRPC . PhotoSize bigPhoto ) {
if ( bigPhoto ! = null ) {
2014-09-25 05:54:35 +02:00
uploadingAvatar = FileLoader . getInstance ( ) . getDirectory ( FileLoader . MEDIA_DIR_CACHE ) + " / " + bigPhoto . location . volume_id + " _ " + bigPhoto . location . local_id + " .jpg " ;
2014-09-01 17:43:03 +02:00
FileLoader . getInstance ( ) . uploadFile ( uploadingAvatar , false , true ) ;
2013-10-25 17:19:00 +02:00
}
}
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 ;
}
}
}
}
2015-09-24 22:52:02 +02:00
public void deleteMessages ( ArrayList < Integer > messages , ArrayList < Long > randoms , TLRPC . EncryptedChat encryptedChat , final int channelId ) {
2015-11-26 22:04:02 +01:00
if ( messages = = null | | messages . isEmpty ( ) ) {
2014-09-14 01:40:36 +02:00
return ;
}
2015-09-24 22:52:02 +02:00
if ( channelId = = 0 ) {
2016-03-06 02:49:31 +01:00
for ( int a = 0 ; a < messages . size ( ) ; a + + ) {
Integer id = messages . get ( a ) ;
2015-09-24 22:52:02 +02:00
MessageObject obj = dialogMessagesByIds . get ( id ) ;
if ( obj ! = null ) {
obj . deleted = true ;
}
}
} else {
2016-03-16 13:26:32 +01:00
markChannelDialogMessageAsDeleted ( messages , channelId ) ;
2013-10-25 17:19:00 +02:00
}
2015-01-02 23:15:07 +01:00
ArrayList < Integer > toSend = new ArrayList < > ( ) ;
2016-03-06 02:49:31 +01:00
for ( int a = 0 ; a < messages . size ( ) ; a + + ) {
Integer mid = messages . get ( a ) ;
2013-10-25 17:19:00 +02:00
if ( mid > 0 ) {
toSend . add ( mid ) ;
}
}
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . markMessagesAsDeleted ( messages , true , channelId ) ;
MessagesStorage . getInstance ( ) . updateDialogsWithDeletedMessages ( messages , true , channelId ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messagesDeleted , messages , channelId ) ;
if ( channelId ! = 0 ) {
TLRPC . TL_channels_deleteMessages req = new TLRPC . TL_channels_deleteMessages ( ) ;
req . id = toSend ;
req . channel = getInputChannel ( channelId ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . TL_messages_affectedMessages res = ( TLRPC . TL_messages_affectedMessages ) response ;
processNewChannelDifferenceParams ( res . pts , res . pts_count , channelId ) ;
}
2015-03-19 00:09:45 +01:00
}
2015-09-24 22:52:02 +02:00
} ) ;
} else {
if ( randoms ! = null & & encryptedChat ! = null & & ! randoms . isEmpty ( ) ) {
SecretChatHelper . getInstance ( ) . sendMessagesDeleteMessage ( encryptedChat , randoms , null ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
TLRPC . TL_messages_deleteMessages req = new TLRPC . TL_messages_deleteMessages ( ) ;
req . id = toSend ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . TL_messages_affectedMessages res = ( TLRPC . TL_messages_affectedMessages ) response ;
processNewDifferenceParams ( - 1 , res . pts , - 1 , res . pts_count ) ;
}
}
} ) ;
}
2013-10-25 17:19:00 +02:00
}
2016-03-16 13:26:32 +01:00
public void pinChannelMessage ( TLRPC . Chat chat , int id , boolean notify ) {
TLRPC . TL_channels_updatePinnedMessage req = new TLRPC . TL_channels_updatePinnedMessage ( ) ;
req . channel = getInputChannel ( chat ) ;
req . id = id ;
req . silent = ! notify ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . Updates updates = ( TLRPC . Updates ) response ;
processUpdates ( updates , false ) ;
}
}
} ) ;
}
2015-12-09 19:27:52 +01:00
public void deleteDialog ( final long did , final int onlyHistory ) {
2015-11-26 22:04:02 +01:00
deleteDialog ( did , true , onlyHistory , 0 ) ;
}
2016-03-16 13:26:32 +01:00
public void deleteUserChannelHistory ( final TLRPC . Chat chat , final TLRPC . User user , int offset ) {
if ( offset = = 0 ) {
MessagesStorage . getInstance ( ) . deleteUserChannelHistory ( chat . id , user . id ) ;
}
TLRPC . TL_channels_deleteUserHistory req = new TLRPC . TL_channels_deleteUserHistory ( ) ;
req . channel = getInputChannel ( chat ) ;
req . user_id = getInputUser ( user ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . TL_messages_affectedHistory res = ( TLRPC . TL_messages_affectedHistory ) response ;
if ( res . offset > 0 ) {
deleteUserChannelHistory ( chat , user , res . offset ) ;
}
processNewChannelDifferenceParams ( res . pts , res . pts_count , chat . id ) ;
}
}
} ) ;
}
2015-12-09 19:27:52 +01:00
private void deleteDialog ( final long did , final boolean first , final int onlyHistory , final int max_id ) {
2015-05-21 23:27:27 +02:00
int lower_part = ( int ) did ;
int high_id = ( int ) ( did > > 32 ) ;
2015-11-26 22:04:02 +01:00
int max_id_delete = max_id ;
2014-11-20 15:45:33 +01:00
2015-12-09 19:27:52 +01:00
if ( onlyHistory = = 2 ) {
MessagesStorage . getInstance ( ) . deleteDialog ( did , onlyHistory ) ;
return ;
}
2015-11-26 22:04:02 +01:00
if ( first ) {
2015-09-24 22:52:02 +02:00
TLRPC . Dialog dialog = dialogs_dict . get ( did ) ;
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 ) ;
}
2015-12-09 19:27:52 +01:00
if ( onlyHistory = = 0 ) {
2014-11-21 01:14:44 +01:00
dialogs . remove ( dialog ) ;
2015-09-24 22:52:02 +02:00
if ( dialogsServerOnly . remove ( dialog ) ) {
2015-11-26 22:04:02 +01:00
if ( dialog instanceof TLRPC . TL_dialogChannel ) {
2015-09-24 22:52:02 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
channelsPts . remove ( - ( int ) did ) ;
2015-12-09 19:27:52 +01:00
shortPollChannels . delete ( - ( int ) did ) ;
needShortPollChannels . delete ( - ( int ) did ) ;
2015-09-24 22:52:02 +02:00
}
} ) ;
}
}
2015-06-29 19:12:11 +02:00
dialogsGroupsOnly . remove ( dialog ) ;
2014-11-21 01:14:44 +01:00
dialogs_dict . remove ( did ) ;
2015-11-26 22:04:02 +01:00
dialogs_read_inbox_max . remove ( did ) ;
2015-09-24 22:52:02 +02:00
nextDialogsCacheOffset - - ;
2014-11-21 01:14:44 +01:00
} else {
dialog . unread_count = 0 ;
}
2015-09-24 22:52:02 +02:00
dialogMessage . remove ( dialog . id ) ;
2016-03-06 02:49:31 +01:00
MessageObject object = dialogMessagesByIds . remove ( dialog . top_message ) ;
if ( object ! = null & & object . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . remove ( object . messageOwner . random_id ) ;
}
2015-02-26 02:32:51 +01:00
dialog . top_message = 0 ;
2014-07-27 12:53:00 +02:00
}
2015-02-26 02:32:51 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2015-09-24 22:52:02 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . removeAllMessagesFromDialog , did , false ) ;
2014-11-20 15:45:33 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2015-11-26 22:04:02 +01:00
NotificationsController . getInstance ( ) . removeNotificationsForDialog ( did ) ;
2014-11-20 15:45:33 +01:00
}
} ) ;
}
} ) ;
2015-02-26 02:32:51 +01:00
MessagesStorage . getInstance ( ) . deleteDialog ( did , onlyHistory ) ;
2014-11-20 15:45:33 +01:00
}
2014-07-27 12:53:00 +02:00
2014-11-20 15:45:33 +01:00
if ( high_id = = 1 ) {
return ;
}
if ( lower_part ! = 0 ) {
2015-09-24 22:52:02 +02:00
TLRPC . InputPeer peer = getInputPeer ( lower_part ) ;
if ( peer = = null | | peer instanceof TLRPC . TL_inputPeerChannel ) {
return ;
}
2014-11-20 15:45:33 +01:00
TLRPC . TL_messages_deleteHistory req = new TLRPC . TL_messages_deleteHistory ( ) ;
2015-09-24 22:52:02 +02:00
req . peer = peer ;
2015-11-26 22:04:02 +01:00
req . max_id = max_id_delete ;
final int max_id_delete_final = max_id_delete ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-11-20 15:45:33 +01:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . TL_messages_affectedHistory res = ( TLRPC . TL_messages_affectedHistory ) response ;
if ( res . offset > 0 ) {
2015-11-26 22:04:02 +01:00
deleteDialog ( did , false , onlyHistory , max_id_delete_final ) ;
2013-10-25 17:19:00 +02:00
}
2015-03-19 00:09:45 +01:00
processNewDifferenceParams ( - 1 , res . pts , - 1 , res . pts_count ) ;
2013-10-25 17:19:00 +02:00
}
2014-03-22 23:31:55 +01:00
}
2015-11-26 22:04:02 +01:00
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
2014-11-20 15:45:33 +01:00
} else {
2015-12-09 19:27:52 +01:00
if ( onlyHistory = = 1 ) {
2014-12-01 18:56:31 +01:00
SecretChatHelper . getInstance ( ) . sendClearHistoryMessage ( getEncryptedChat ( high_id ) , null ) ;
2014-11-20 15:45:33 +01:00
} else {
2014-12-01 18:56:31 +01:00
SecretChatHelper . getInstance ( ) . declineSecretChat ( high_id ) ;
2013-10-25 17:19:00 +02:00
}
}
}
2016-03-16 13:26:32 +01:00
public MediaController . SearchImage saveGif ( TLRPC . Document document ) {
MediaController . SearchImage searchImage = new MediaController . SearchImage ( ) ;
searchImage . type = 2 ;
searchImage . document = document ;
searchImage . date = ( int ) ( System . currentTimeMillis ( ) / 1000 ) ;
searchImage . id = " " + searchImage . document . id ;
ArrayList < MediaController . SearchImage > arrayList = new ArrayList < > ( ) ;
arrayList . add ( searchImage ) ;
MessagesStorage . getInstance ( ) . putWebRecent ( arrayList ) ;
TLRPC . TL_messages_saveGif req = new TLRPC . TL_messages_saveGif ( ) ;
req . id = new TLRPC . TL_inputDocument ( ) ;
req . id . id = searchImage . document . id ;
req . id . access_hash = searchImage . document . access_hash ;
req . unsave = false ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
}
} ) ;
return searchImage ;
}
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 ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( final TLObject response , final TLRPC . TL_error error ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( error = = null ) {
TLRPC . TL_channels_channelParticipants res = ( TLRPC . TL_channels_channelParticipants ) response ;
putUsers ( res . users , false ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , null , true , true ) ;
MessagesStorage . getInstance ( ) . updateChannelUsers ( chat_id , res . participants ) ;
loadedFullParticipants . add ( chat_id ) ;
}
loadingFullParticipants . remove ( chat_id ) ;
}
} ) ;
}
} ) ;
}
2015-09-24 22:52:02 +02:00
public void loadChatInfo ( final int chat_id , Semaphore semaphore , boolean force ) {
2015-11-26 22:04:02 +01:00
MessagesStorage . getInstance ( ) . loadChatInfo ( chat_id , semaphore , 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 ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
putUsers ( usersArr , fromCache ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoDidLoaded , info , 0 , byChannelUsers , pinnedMessageObject ) ;
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 ) ;
2013-10-25 17:19:00 +02:00
2014-06-13 12:42:21 +02:00
if ( UserConfig . isClientActivated ( ) ) {
2014-07-27 00:28:33 +02:00
if ( ConnectionsManager . getInstance ( ) . getPauseTime ( ) = = 0 & & ApplicationLoader . isScreenOn & & ! ApplicationLoader . mainInterfacePaused ) {
2015-11-26 22:04:02 +01:00
if ( statusSettingState ! = 1 & & ( lastStatusUpdateTime = = 0 | | Math . abs ( System . currentTimeMillis ( ) - lastStatusUpdateTime ) > = 55000 | | offlineSent ) ) {
2014-02-28 23:28:25 +01:00
statusSettingState = 1 ;
if ( statusRequest ! = 0 ) {
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . cancelRequest ( statusRequest , true ) ;
2014-02-28 23:28:25 +01:00
}
2013-10-25 17:19:00 +02:00
TLRPC . TL_account_updateStatus req = new TLRPC . TL_account_updateStatus ( ) ;
req . offline = false ;
2015-09-24 22:52:02 +02:00
statusRequest = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2014-02-28 23:28:25 +01:00
if ( error = = null ) {
lastStatusUpdateTime = System . currentTimeMillis ( ) ;
offlineSent = false ;
statusSettingState = 0 ;
} else {
if ( lastStatusUpdateTime ! = 0 ) {
lastStatusUpdateTime + = 5000 ;
}
}
statusRequest = 0 ;
2013-10-25 17:19:00 +02:00
}
2014-07-03 00:39:05 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
2015-11-26 22:04:02 +01:00
} else if ( statusSettingState ! = 2 & & ! offlineSent & & Math . abs ( System . currentTimeMillis ( ) - ConnectionsManager . getInstance ( ) . getPauseTime ( ) ) > = 2000 ) {
2014-02-28 23:28:25 +01:00
statusSettingState = 2 ;
if ( statusRequest ! = 0 ) {
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . cancelRequest ( statusRequest , true ) ;
2014-02-28 23:28:25 +01:00
}
2013-10-25 17:19:00 +02:00
TLRPC . TL_account_updateStatus req = new TLRPC . TL_account_updateStatus ( ) ;
req . offline = true ;
2015-09-24 22:52:02 +02:00
statusRequest = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2014-02-28 23:28:25 +01:00
if ( error = = null ) {
offlineSent = true ;
} else {
if ( lastStatusUpdateTime ! = 0 ) {
lastStatusUpdateTime + = 5000 ;
}
}
statusRequest = 0 ;
2013-10-25 17:19:00 +02:00
}
2014-07-03 00:39:05 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
2014-02-04 19:36:55 +01:00
2015-09-24 22:52:02 +02:00
if ( ! updatesQueueChannels . isEmpty ( ) ) {
ArrayList < Integer > keys = new ArrayList < > ( updatesQueueChannels . keySet ( ) ) ;
for ( int a = 0 ; a < keys . size ( ) ; a + + ) {
int key = keys . get ( a ) ;
Long updatesStartWaitTime = updatesStartWaitTimeChannels . get ( key ) ;
if ( updatesStartWaitTime ! = null & & updatesStartWaitTime + 1500 < currentTime ) {
FileLog . e ( " tmessages " , " QUEUE CHANNEL " + key + " UPDATES WAIT TIMEOUT - CHECK QUEUE " ) ;
processChannelsUpdatesQueue ( key , 0 ) ;
}
}
}
2015-03-19 00:09:45 +01:00
for ( int a = 0 ; a < 3 ; a + + ) {
if ( getUpdatesStartTime ( a ) ! = 0 & & getUpdatesStartTime ( a ) + 1500 < currentTime ) {
FileLog . e ( " tmessages " , a + " QUEUE UPDATES WAIT TIMEOUT - CHECK QUEUE " ) ;
processUpdatesQueue ( a , 0 ) ;
}
2014-02-04 19:36:55 +01:00
}
2013-10-25 17:19:00 +02:00
}
2015-11-26 22:04:02 +01:00
if ( ( channelViewsToSend . size ( ) ! = 0 | | channelViewsToReload . size ( ) ! = 0 ) & & Math . abs ( System . currentTimeMillis ( ) - lastViewsCheckTime ) > = 5000 ) {
2015-09-24 22:52:02 +02:00
lastViewsCheckTime = System . currentTimeMillis ( ) ;
for ( int b = 0 ; b < 2 ; b + + ) {
SparseArray < ArrayList < Integer > > array = b = = 0 ? channelViewsToSend : channelViewsToReload ;
if ( array . size ( ) = = 0 ) {
continue ;
}
for ( int a = 0 ; a < array . size ( ) ; a + + ) {
final int key = array . keyAt ( a ) ;
final TLRPC . TL_messages_getMessagesViews req = new TLRPC . TL_messages_getMessagesViews ( ) ;
req . peer = getInputPeer ( key ) ;
req . id = array . get ( key ) ;
req . increment = a = = 0 ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
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 a = 0 ; a < req . id . size ( ) ; a + + ) {
if ( a > = vector . objects . size ( ) ) {
break ;
}
array . put ( req . id . get ( a ) , ( Integer ) vector . objects . get ( a ) ) ;
}
MessagesStorage . getInstance ( ) . putChannelViews ( channelViews , req . peer instanceof TLRPC . TL_inputPeerChannel ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . didUpdatedMessagesViews , channelViews ) ;
}
} ) ;
}
}
} ) ;
}
array . clear ( ) ;
}
}
2014-11-21 01:14:44 +01:00
if ( ! onlinePrivacy . isEmpty ( ) ) {
ArrayList < Integer > toRemove = null ;
int currentServerTime = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
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 ) ;
}
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_STATUS ) ;
}
} ) ;
}
}
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 ) ;
int timeout = shortPollChannels . get ( key ) ;
if ( timeout < System . currentTimeMillis ( ) / 1000 ) {
shortPollChannels . delete ( key ) ;
2015-11-26 22:04:02 +01:00
if ( needShortPollChannels . indexOfKey ( key ) > = 0 ) {
2015-09-24 22:52:02 +02:00
getChannelDifference ( key ) ;
}
}
}
}
2014-05-20 22:22:44 +02:00
if ( ! printingUsers . isEmpty ( ) | | lastPrintingStringCount ! = printingUsers . size ( ) ) {
2014-02-04 19:36:55 +01:00
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 + + ) {
Long key = keys . get ( b ) ;
ArrayList < PrintingUser > arr = printingUsers . get ( key ) ;
for ( int a = 0 ; a < arr . size ( ) ; a + + ) {
PrintingUser user = arr . get ( a ) ;
if ( user . lastTime + 5900 < currentTime ) {
2014-02-04 19:36:55 +01:00
updated = true ;
2013-10-25 17:19:00 +02:00
arr . remove ( user ) ;
a - - ;
}
}
if ( arr . isEmpty ( ) ) {
printingUsers . remove ( key ) ;
keys . remove ( b ) ;
b - - ;
}
}
updatePrintingStrings ( ) ;
2014-02-04 19:36:55 +01:00
if ( updated ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_USER_PRINT ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
}
}
2015-05-03 13:48:36 +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 ( ) {
2015-01-02 23:15:07 +01:00
final HashMap < Long , CharSequence > newPrintingStrings = new HashMap < > ( ) ;
2015-05-03 13:48:36 +02:00
final HashMap < Long , Integer > newPrintingStringsTypes = new HashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
2015-01-02 23:15:07 +01:00
ArrayList < Long > keys = new ArrayList < > ( printingUsers . keySet ( ) ) ;
2015-05-03 13:48:36 +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 ) {
return ;
}
if ( pu . action instanceof TLRPC . TL_sendMessageUploadAudioAction | | 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 ) ;
} 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 ) ;
} else {
if ( lower_id < 0 ) {
newPrintingStrings . put ( key , String . format ( " %s %s " , getUserNameForTyping ( user ) , LocaleController . getString ( " IsTyping " , R . string . IsTyping ) ) ) ;
} 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 ;
String label = " " ;
for ( PrintingUser pu : arr ) {
2014-08-22 16:24:33 +02:00
TLRPC . User user = getUser ( pu . userId ) ;
2013-10-25 17:19:00 +02:00
if ( user ! = null ) {
if ( label . length ( ) ! = 0 ) {
label + = " , " ;
}
2015-05-03 13:48:36 +02:00
label + = 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 ) {
newPrintingStrings . put ( key , String . format ( " %s %s " , label , LocaleController . getString ( " IsTyping " , R . string . IsTyping ) ) ) ;
2013-10-25 17:19:00 +02:00
} else {
2015-10-29 18:10:07 +01:00
if ( arr . size ( ) > 2 ) {
newPrintingStrings . put ( key , String . format ( " %s %s " , label , LocaleController . formatPluralString ( " AndMoreTyping " , arr . size ( ) - 2 ) ) ) ;
} else {
newPrintingStrings . put ( key , String . format ( " %s %s " , label , LocaleController . getString ( " AreTyping " , R . string . AreTyping ) ) ) ;
}
2013-10-25 17:19:00 +02:00
}
2015-05-03 13:48:36 +02:00
newPrintingStringsTypes . put ( key , 0 ) ;
2013-10-25 17:19:00 +02:00
}
}
}
2014-05-20 22:22:44 +02:00
lastPrintingStringCount = newPrintingStrings . size ( ) ;
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
printingStrings = newPrintingStrings ;
2015-05-03 13:48:36 +02:00
printingStringsTypes = newPrintingStringsTypes ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
2015-05-03 13:48:36 +02:00
public void cancelTyping ( int action , long dialog_id ) {
HashMap < Long , Boolean > typings = sendingTypings . get ( action ) ;
if ( typings ! = null ) {
typings . remove ( dialog_id ) ;
}
2014-08-23 01:22:33 +02:00
}
2015-05-03 13:48:36 +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 ;
}
2015-05-03 13:48:36 +02:00
HashMap < Long , Boolean > typings = sendingTypings . get ( action ) ;
if ( typings ! = null & & typings . get ( dialog_id ) ! = null ) {
2014-08-23 01:22:33 +02:00
return ;
}
2015-05-03 13:48:36 +02:00
if ( typings = = null ) {
typings = new HashMap < > ( ) ;
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 ) {
2014-07-27 12:53:00 +02:00
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
}
2015-05-03 13:48:36 +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 ( ) ;
}
typings . put ( dialog_id , true ) ;
2015-09-24 22:52:02 +02:00
int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-23 01:22:33 +02:00
@Override
public void run ( ) {
2015-05-03 13:48:36 +02:00
HashMap < Long , Boolean > typings = sendingTypings . get ( action ) ;
if ( typings ! = null ) {
typings . remove ( dialog_id ) ;
}
2014-08-23 01:22:33 +02:00
}
} ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
} , ConnectionsManager . RequestFlagFailOnServerErrors ) ;
2015-05-03 13:48:36 +02:00
if ( classGuid ! = 0 ) {
ConnectionsManager . getInstance ( ) . bindRequestToGuid ( reqId , classGuid ) ;
}
2013-10-25 17:19:00 +02:00
} else {
2015-05-03 13:48:36 +02:00
if ( action ! = 0 ) {
return ;
}
2014-08-22 16:24:33 +02:00
TLRPC . EncryptedChat chat = getEncryptedChat ( high_id ) ;
2014-07-27 12:53:00 +02:00
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 ;
2015-05-03 13:48:36 +02:00
typings . put ( dialog_id , true ) ;
2015-09-24 22:52:02 +02:00
int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-07-27 12:53:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2015-05-03 13:48:36 +02:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
HashMap < Long , Boolean > typings = sendingTypings . get ( action ) ;
if ( typings ! = null ) {
typings . remove ( dialog_id ) ;
}
}
} ) ;
2014-07-27 12:53:00 +02:00
}
2015-09-24 22:52:02 +02:00
} , ConnectionsManager . RequestFlagFailOnServerErrors ) ;
2015-05-03 13:48:36 +02:00
if ( classGuid ! = 0 ) {
ConnectionsManager . getInstance ( ) . bindRequestToGuid ( reqId , classGuid ) ;
}
2013-10-25 17:19:00 +02:00
}
}
}
2015-11-26 22:04:02 +01:00
public void loadMessages ( final long dialog_id , final int count , final int max_id , boolean fromCache , int midDate , final int classGuid , final int load_type , final int last_message_id , final int important , final int loadIndex ) {
2015-12-09 19:27:52 +01:00
loadMessages ( dialog_id , count , max_id , fromCache , midDate , classGuid , load_type , last_message_id , important , loadIndex , 0 , 0 , 0 , false ) ;
}
public void loadMessages ( final long dialog_id , final int count , final int max_id , boolean fromCache , int midDate , final int classGuid , final int load_type , final int last_message_id , final int important , final int loadIndex , final int first_unread , final int unread_count , final int last_date , final boolean 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 ) {
2015-11-26 22:04:02 +01:00
MessagesStorage . getInstance ( ) . getMessages ( dialog_id , count , max_id , midDate , classGuid , load_type , important , loadIndex ) ;
2013-10-25 17:19:00 +02:00
} else {
2015-09-24 22:52:02 +02:00
TLObject request ;
if ( important = = 2 ) {
TLRPC . TL_channels_getImportantHistory req = new TLRPC . TL_channels_getImportantHistory ( ) ;
req . channel = getInputChannel ( - lower_part ) ;
if ( load_type = = 3 ) {
req . add_offset = - count / 2 ;
} else if ( load_type = = 1 ) {
req . add_offset = - count - 1 ;
2015-12-09 19:27:52 +01:00
} else if ( load_type = = 2 & & max_id ! = 0 ) {
req . add_offset = - count + 6 ;
2015-09-24 22:52:02 +02:00
} else {
if ( max_id ! = 0 ) {
req . add_offset = - 1 ;
req . limit + = 1 ;
}
2015-05-21 23:27:27 +02:00
}
2015-09-24 22:52:02 +02:00
req . limit + = count ;
req . offset_id = max_id ;
request = req ;
2014-11-18 06:01:04 +01:00
} else {
2015-09-24 22:52:02 +02:00
TLRPC . TL_messages_getHistory req = new TLRPC . TL_messages_getHistory ( ) ;
req . peer = getInputPeer ( lower_part ) ;
if ( load_type = = 3 ) {
req . add_offset = - count / 2 ;
} else if ( load_type = = 1 ) {
req . add_offset = - count - 1 ;
2015-12-09 19:27:52 +01:00
} else if ( load_type = = 2 & & max_id ! = 0 ) {
req . add_offset = - count + 6 ;
2015-09-24 22:52:02 +02:00
} else {
req . add_offset = 0 ;
}
req . limit = count ;
req . offset_id = max_id ;
request = req ;
2014-11-18 06:01:04 +01:00
}
2015-09-24 22:52:02 +02:00
int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2014-03-22 23:31:55 +01:00
final TLRPC . messages_Messages res = ( TLRPC . messages_Messages ) response ;
2015-09-24 22:52:02 +02:00
if ( res . messages . size ( ) > count ) {
res . messages . remove ( 0 ) ;
}
2015-12-09 19:27:52 +01:00
processLoadedMessages ( res , dialog_id , count , max_id , false , classGuid , first_unread , last_message_id , unread_count , last_date , load_type , important , false , loadIndex , queryFromServer ) ;
2013-10-25 17:19:00 +02:00
}
}
2014-07-03 00:39:05 +02:00
} ) ;
2014-03-22 23:31:55 +01:00
ConnectionsManager . getInstance ( ) . 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 ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( final TLObject response , final TLRPC . TL_error error ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
ArrayList < MessageObject > arrayList = reloadingWebpages . remove ( url ) ;
if ( arrayList = = null ) {
return ;
}
TLRPC . TL_messages_messages messagesRes = new TLRPC . TL_messages_messages ( ) ;
if ( ! ( response instanceof TLRPC . TL_messageMediaWebPage ) ) {
for ( int a = 0 ; a < arrayList . size ( ) ; a + + ) {
arrayList . get ( a ) . messageOwner . media . webpage = new TLRPC . TL_webPageEmpty ( ) ;
messagesRes . messages . add ( arrayList . 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 < arrayList . size ( ) ; a + + ) {
arrayList . get ( a ) . messageOwner . media . webpage = media . webpage ;
if ( a = = 0 ) {
ImageLoader . saveMessageThumbs ( arrayList . get ( a ) . messageOwner ) ;
}
messagesRes . messages . add ( arrayList . get ( a ) . messageOwner ) ;
}
} else {
reloadingWebpagesPending . put ( media . webpage . id , arrayList ) ;
}
}
if ( ! messagesRes . messages . isEmpty ( ) ) {
MessagesStorage . getInstance ( ) . putMessages ( messagesRes , dialog_id , - 2 , 0 , 0 , false ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . replaceMessagesObjects , dialog_id , arrayList ) ;
}
}
} ) ;
}
} ) ;
}
}
2014-11-19 02:23:46 +01:00
public void processLoadedMessages ( final TLRPC . messages_Messages messagesRes , final long dialog_id , final int count , final int max_id , final boolean isCache , final int classGuid ,
2015-12-09 19:27:52 +01:00
final int first_unread , final int last_message_id , final int unread_count , final int last_date , final int load_type , final int important , final boolean isEnd , final int loadIndex , final boolean queryFromServer ) {
2013-10-25 17:19:00 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
boolean createDialog = false ;
2015-11-26 22:04:02 +01:00
boolean isMegagroup = false ;
2015-09-24 22:52:02 +02:00
if ( messagesRes instanceof TLRPC . TL_messages_channelMessages ) {
int channelId = - ( int ) dialog_id ;
Integer channelPts = channelsPts . get ( channelId ) ;
if ( channelPts = = null ) {
channelPts = MessagesStorage . getInstance ( ) . getChannelPtsSync ( channelId ) ;
if ( channelPts = = 0 ) {
channelsPts . put ( channelId , messagesRes . pts ) ;
createDialog = true ;
2016-03-16 13:26:32 +01:00
if ( needShortPollChannels . indexOfKey ( channelId ) > = 0 & & shortPollChannels . indexOfKey ( channelId ) < 0 ) {
getChannelDifference ( channelId , 2 ) ;
} else {
getChannelDifference ( channelId ) ;
}
2015-09-24 22:52:02 +02:00
}
}
2015-11-26 22:04:02 +01: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 ;
}
}
2015-09-24 22:52:02 +02:00
}
2015-03-19 00:09:45 +01:00
int lower_id = ( int ) dialog_id ;
int high_id = ( int ) ( dialog_id > > 32 ) ;
2015-02-01 19:51:02 +01:00
if ( ! isCache ) {
ImageLoader . saveMessagesThumbs ( messagesRes . messages ) ;
}
2015-09-24 22:52:02 +02:00
if ( high_id ! = 1 & & lower_id ! = 0 & & isCache & & messagesRes . messages . size ( ) = = 0 ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2015-12-09 19:27:52 +01:00
loadMessages ( dialog_id , count , load_type = = 2 & & queryFromServer ? first_unread : max_id , false , 0 , classGuid , load_type , last_message_id , important , loadIndex , first_unread , unread_count , last_date , queryFromServer ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
return ;
}
2015-06-29 19:12:11 +02:00
final HashMap < Integer , TLRPC . User > usersDict = new HashMap < > ( ) ;
2015-11-26 22:04:02 +01:00
final HashMap < Integer , TLRPC . Chat > chatsDict = new HashMap < > ( ) ;
for ( int a = 0 ; a < messagesRes . users . size ( ) ; a + + ) {
TLRPC . User u = messagesRes . users . get ( a ) ;
2015-06-29 19:12:11 +02:00
usersDict . put ( u . id , u ) ;
}
2015-11-26 22:04:02 +01:00
for ( int a = 0 ; a < messagesRes . chats . size ( ) ; a + + ) {
TLRPC . Chat c = messagesRes . chats . get ( a ) ;
chatsDict . put ( c . id , c ) ;
}
2015-09-24 22:52:02 +02:00
int size = messagesRes . messages . size ( ) ;
if ( ! isCache ) {
for ( int a = 0 ; a < size ; a + + ) {
2015-06-29 19:12:11 +02:00
TLRPC . Message message = messagesRes . messages . get ( a ) ;
2016-03-06 02:49:31 +01:00
if ( ! isCache & & message . post & & ! message . out ) {
2015-11-26 22:04:02 +01:00
message . media_unread = true ;
}
if ( isMegagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
2015-09-24 22:52:02 +02:00
}
2015-06-29 19:12:11 +02:00
if ( message . action instanceof TLRPC . TL_messageActionChatDeleteUser ) {
TLRPC . User user = usersDict . get ( message . action . user_id ) ;
2015-11-26 22:04:02 +01:00
if ( user ! = null & & user . bot ) {
2015-06-29 19:12:11 +02:00
message . reply_markup = new TLRPC . TL_replyKeyboardHide ( ) ;
}
2015-11-26 22:04:02 +01:00
} else if ( message . action instanceof TLRPC . TL_messageActionChatMigrateTo | | message . action instanceof TLRPC . TL_messageActionChannelCreate ) {
message . unread = false ;
message . media_unread = false ;
2015-06-29 19:12:11 +02:00
}
}
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . putMessages ( messagesRes , dialog_id , load_type , max_id , important , createDialog ) ;
2013-10-25 17:19:00 +02:00
}
2015-01-02 23:15:07 +01:00
final ArrayList < MessageObject > objects = new ArrayList < > ( ) ;
2015-09-24 22:52:02 +02:00
final ArrayList < Integer > messagesToReload = new ArrayList < > ( ) ;
2016-03-06 02:49:31 +01:00
final HashMap < String , ArrayList < MessageObject > > webpagesToReload = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
TLRPC . InputChannel inputChannel = null ;
for ( int a = 0 ; a < size ; a + + ) {
TLRPC . Message message = messagesRes . messages . get ( a ) ;
2013-10-25 17:19:00 +02:00
message . dialog_id = dialog_id ;
2016-03-06 02:49:31 +01:00
MessageObject messageObject = new MessageObject ( message , usersDict , chatsDict , true ) ;
objects . add ( messageObject ) ;
2015-04-09 20:00:14 +02:00
if ( isCache ) {
if ( message . media instanceof TLRPC . TL_messageMediaUnsupported ) {
2016-01-11 18:19:48 +01:00
if ( message . media . bytes ! = null & & ( message . media . bytes . length = = 0 | | message . media . bytes . length = = 1 & & message . media . bytes [ 0 ] < TLRPC . LAYER ) ) {
2015-04-09 20:00:14 +02:00
messagesToReload . add ( message . id ) ;
}
} else if ( message . media instanceof TLRPC . TL_messageMediaWebPage ) {
if ( message . media . webpage instanceof TLRPC . TL_webPagePending & & message . media . webpage . date < = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) {
messagesToReload . add ( message . id ) ;
2016-03-06 02:49:31 +01:00
} 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 ) ;
}
arrayList . add ( messageObject ) ;
2015-01-09 13:50:15 +01:00
}
}
}
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-10-31 20:02:29 +01:00
@Override
public void run ( ) {
putUsers ( messagesRes . users , isCache ) ;
putChats ( messagesRes . chats , isCache ) ;
2015-12-09 19:27:52 +01:00
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 ;
}
}
}
if ( first_unread_final = = Integer . MAX_VALUE ) {
first_unread_final = first_unread ;
}
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messagesDidLoaded , dialog_id , count , objects , isCache , first_unread_final , last_message_id , unread_count , last_date , load_type , messagesRes . collapsed , isEnd , classGuid , loadIndex ) ;
2016-03-06 02:49:31 +01:00
if ( ! messagesToReload . isEmpty ( ) ) {
2015-09-24 22:52:02 +02:00
reloadMessages ( messagesToReload , dialog_id ) ;
}
2016-03-06 02:49:31 +01:00
if ( ! webpagesToReload . isEmpty ( ) ) {
reloadWebPages ( dialog_id , webpagesToReload ) ;
}
2014-10-31 20:02:29 +01:00
}
} ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
2015-11-26 22:04:02 +01:00
public void loadDialogs ( final int offset , final int count , boolean fromCache ) {
2013-10-25 17:19:00 +02:00
if ( loadingDialogs ) {
return ;
}
loadingDialogs = true ;
2014-11-20 15:45:33 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2015-11-26 22:04:02 +01:00
FileLog . e ( " tmessages " , " load cacheOffset = " + offset + " count = " + count + " cache = " + fromCache ) ;
2013-10-25 17:19:00 +02:00
if ( fromCache ) {
2015-11-26 22:04:02 +01:00
MessagesStorage . getInstance ( ) . getDialogs ( offset = = 0 ? 0 : nextDialogsCacheOffset , count ) ;
2013-10-25 17:19:00 +02:00
} else {
TLRPC . TL_messages_getDialogs req = new TLRPC . TL_messages_getDialogs ( ) ;
req . limit = count ;
2015-11-26 22:04:02 +01:00
boolean found = false ;
for ( int a = dialogs . size ( ) - 1 ; a > = 0 ; a - - ) {
TLRPC . Dialog dialog = dialogs . get ( a ) ;
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 ) {
2016-03-06 02:49:31 +01:00
req . offset_date = Math . max ( dialog . last_message_date_i , message . messageOwner . date ) ;
2015-11-26 22:04:02 +01:00
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 ;
}
2016-03-16 13:26:32 +01:00
req . offset_peer = getInputPeer ( id ) ;
2015-11-26 22:04:02 +01:00
found = true ;
break ;
}
}
}
if ( ! found ) {
req . offset_peer = new TLRPC . TL_inputPeerEmpty ( ) ;
}
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2014-03-22 23:31:55 +01:00
final TLRPC . messages_Dialogs dialogsRes = ( TLRPC . messages_Dialogs ) response ;
2015-11-26 22:04:02 +01:00
processLoadedDialogs ( dialogsRes , null , 0 , count , false , false , false ) ;
2013-10-25 17:19:00 +02:00
}
}
2014-07-03 00:39:05 +02: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 ( ) ;
req . limit = 100 ;
req . offset_id = offset ;
req . offset_date = offsetDate ;
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 ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
final TLRPC . messages_Dialogs dialogsRes = ( TLRPC . messages_Dialogs ) response ;
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
try {
int offsetId ;
if ( dialogsRes . dialogs . size ( ) = = 100 ) {
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 ;
}
}
offsetId = lastMessage . id ;
UserConfig . migrateOffsetDate = lastMessage . date ;
if ( lastMessage . to_id . channel_id ! = 0 ) {
UserConfig . migrateOffsetChannelId = lastMessage . to_id . channel_id ;
UserConfig . migrateOffsetChatId = 0 ;
UserConfig . migrateOffsetUserId = 0 ;
for ( int a = 0 ; a < dialogsRes . chats . size ( ) ; a + + ) {
TLRPC . Chat chat = dialogsRes . chats . get ( a ) ;
if ( chat . id = = UserConfig . migrateOffsetChannelId ) {
UserConfig . migrateOffsetAccess = chat . access_hash ;
break ;
}
}
} else if ( lastMessage . to_id . chat_id ! = 0 ) {
UserConfig . migrateOffsetChatId = lastMessage . to_id . chat_id ;
UserConfig . migrateOffsetChannelId = 0 ;
UserConfig . migrateOffsetUserId = 0 ;
for ( int a = 0 ; a < dialogsRes . chats . size ( ) ; a + + ) {
TLRPC . Chat chat = dialogsRes . chats . get ( a ) ;
if ( chat . id = = UserConfig . migrateOffsetChatId ) {
UserConfig . migrateOffsetAccess = chat . access_hash ;
break ;
}
}
} else if ( lastMessage . to_id . user_id ! = 0 ) {
UserConfig . migrateOffsetUserId = lastMessage . to_id . user_id ;
UserConfig . migrateOffsetChatId = 0 ;
UserConfig . migrateOffsetChannelId = 0 ;
for ( int a = 0 ; a < dialogsRes . users . size ( ) ; a + + ) {
TLRPC . User user = dialogsRes . users . get ( a ) ;
if ( user . id = = UserConfig . migrateOffsetUserId ) {
UserConfig . migrateOffsetAccess = user . access_hash ;
break ;
}
}
}
} else {
offsetId = - 1 ;
}
StringBuilder dids = new StringBuilder ( dialogsRes . dialogs . size ( ) * 12 ) ;
HashMap < Long , TLRPC . Dialog > dialogHashMap = new HashMap < > ( ) ;
for ( int a = 0 ; a < dialogsRes . dialogs . size ( ) ; a + + ) {
TLRPC . Dialog dialog = dialogsRes . dialogs . get ( a ) ;
if ( dialog . peer . channel_id ! = 0 ) {
dialog . id = - dialog . peer . channel_id ;
} else if ( dialog . peer . chat_id ! = 0 ) {
dialog . id = - dialog . peer . chat_id ;
} else {
dialog . id = dialog . peer . user_id ;
}
if ( dids . length ( ) > 0 ) {
dids . append ( " , " ) ;
}
dids . append ( dialog . id ) ;
dialogHashMap . put ( dialog . id , dialog ) ;
}
SQLiteCursor cursor = MessagesStorage . getInstance ( ) . getDatabase ( ) . queryFinalized ( String . format ( Locale . US , " SELECT did FROM dialogs WHERE did IN (%s) " , dids . toString ( ) ) ) ;
while ( cursor . next ( ) ) {
long did = cursor . longValue ( 0 ) ;
TLRPC . Dialog dialog = dialogHashMap . remove ( did ) ;
if ( dialog ! = null ) {
dialogsRes . dialogs . remove ( dialog ) ;
for ( int a = 0 ; a < dialogsRes . messages . size ( ) ; a + + ) {
TLRPC . Message message = dialogsRes . messages . get ( a ) ;
if ( MessageObject . getDialogId ( message ) ! = did ) {
continue ;
}
dialogsRes . messages . remove ( a ) ;
a - - ;
if ( message . id = = dialog . top_message ) {
dialog . top_message = 0 ;
}
if ( message . id = = dialog . top_not_important_message ) {
dialog . top_not_important_message = 0 ;
}
if ( dialog . top_message = = 0 & & dialog . top_not_important_message = = 0 ) {
break ;
}
}
}
}
cursor . dispose ( ) ;
cursor = MessagesStorage . getInstance ( ) . getDatabase ( ) . queryFinalized ( " SELECT min(date) FROM dialogs WHERE date != 0 AND did >> 32 IN (0, -1) " ) ;
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 ) {
offsetId = - 1 ;
dialogsRes . messages . remove ( a ) ;
a - - ;
TLRPC . Dialog dialog = dialogHashMap . remove ( MessageObject . getDialogId ( message ) ) ;
if ( dialog ! = null ) {
dialogsRes . dialogs . remove ( dialog ) ;
}
}
}
}
cursor . dispose ( ) ;
2016-03-16 13:26:32 +01:00
processLoadedDialogs ( dialogsRes , null , offsetId , 0 , false , false , true ) ;
2015-11-26 22:04:02 +01:00
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
migratingDialogs = false ;
}
} ) ;
}
}
} ) ;
} else {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
migratingDialogs = false ;
}
} ) ;
}
}
} ) ;
}
public void processLoadedDialogs ( final TLRPC . messages_Dialogs dialogsRes , final ArrayList < TLRPC . EncryptedChat > encChats , final int offset , final int count , final boolean isCache , final boolean resetEnd , final boolean migrate ) {
2013-10-25 17:19:00 +02:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
2015-10-29 18:10:07 +01:00
FileLog . e ( " tmessages " , " loaded from " + isCache + " count " + dialogsRes . dialogs . size ( ) ) ;
2015-09-24 22:52:02 +02:00
if ( isCache & & dialogsRes . dialogs . size ( ) = = 0 ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
putUsers ( dialogsRes . users , true ) ;
loadingDialogs = false ;
if ( resetEnd ) {
dialogsEndReached = false ;
}
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2015-11-26 22:04:02 +01:00
loadDialogs ( 0 , count , false ) ;
2015-09-24 22:52:02 +02:00
}
} ) ;
return ;
}
2015-11-26 22:04:02 +01:00
2015-09-24 22:52:02 +02:00
final HashMap < Long , TLRPC . Dialog > new_dialogs_dict = new HashMap < > ( ) ;
final HashMap < Long , MessageObject > new_dialogMessage = new HashMap < > ( ) ;
2016-03-06 02:49:31 +01:00
final HashMap < Integer , Integer > notImportantDates = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
final HashMap < Integer , TLRPC . User > usersDict = new HashMap < > ( ) ;
final HashMap < Integer , TLRPC . Chat > chatsDict = new HashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < dialogsRes . users . size ( ) ; a + + ) {
TLRPC . User u = dialogsRes . users . get ( a ) ;
usersDict . put ( u . id , u ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < dialogsRes . chats . size ( ) ; a + + ) {
TLRPC . Chat c = dialogsRes . chats . get ( a ) ;
chatsDict . put ( c . id , c ) ;
}
if ( isCache ) {
2015-11-26 22:04:02 +01:00
nextDialogsCacheOffset = offset + count ;
2015-09-24 22:52:02 +02:00
}
for ( int a = 0 ; a < dialogsRes . messages . size ( ) ; a + + ) {
TLRPC . Message message = dialogsRes . messages . get ( a ) ;
if ( message . to_id . channel_id ! = 0 ) {
2016-03-06 02:49:31 +01:00
if ( ! MessageObject . isImportant ( message ) ) {
notImportantDates . put ( - message . to_id . channel_id , message . date ) ;
}
2015-09-24 22:52:02 +02:00
TLRPC . Chat chat = chatsDict . get ( message . to_id . channel_id ) ;
2016-01-11 18:19:48 +01:00
if ( chat ! = null & & chat . left /* && !chat.megagroup*/ ) {
2015-11-26 22:04:02 +01:00
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 ) {
2015-09-24 22:52:02 +02:00
continue ;
2013-10-25 17:19:00 +02:00
}
}
2016-03-06 02:49:31 +01:00
if ( ! isCache & & message . post & & ! message . out ) {
2015-11-26 22:04:02 +01:00
message . media_unread = true ;
2015-09-24 22:52:02 +02:00
}
2015-11-26 22:04:02 +01:00
MessageObject messageObject = new MessageObject ( message , usersDict , chatsDict , false ) ;
2015-09-24 22:52:02 +02:00
MessageObject currentMessage = new_dialogMessage . get ( messageObject . getDialogId ( ) ) ;
2015-11-26 22:04:02 +01:00
if ( currentMessage = = null | | messageObject . isMegagroup ( ) | | messageObject . isImportant ( ) ) {
2015-09-24 22:52:02 +02:00
new_dialogMessage . put ( messageObject . getDialogId ( ) , messageObject ) ;
}
}
for ( int a = 0 ; a < dialogsRes . dialogs . size ( ) ; a + + ) {
TLRPC . Dialog d = dialogsRes . dialogs . get ( a ) ;
if ( d . id = = 0 & & d . peer ! = null ) {
if ( d . peer . user_id ! = 0 ) {
2013-10-25 17:19:00 +02:00
d . id = d . peer . user_id ;
2015-09-24 22:52:02 +02:00
} else if ( d . peer . chat_id ! = 0 ) {
2013-10-25 17:19:00 +02:00
d . id = - d . peer . chat_id ;
2015-09-24 22:52:02 +02:00
} else if ( d . peer . channel_id ! = 0 ) {
d . id = - d . peer . channel_id ;
}
}
if ( d . id = = 0 ) {
continue ;
}
2016-03-06 02:49:31 +01:00
if ( d . last_message_date = = 0 ) {
MessageObject mess = new_dialogMessage . get ( d . id ) ;
if ( mess ! = null ) {
d . last_message_date = mess . messageOwner . date ;
}
}
if ( d . last_message_date_i = = 0 & & d . top_not_important_message ! = 0 ) {
Integer date = notImportantDates . get ( ( int ) d . id ) ;
if ( date ! = null ) {
d . last_message_date_i = date ;
}
}
2015-09-24 22:52:02 +02:00
if ( d instanceof TLRPC . TL_dialogChannel ) {
TLRPC . Chat chat = chatsDict . get ( - ( int ) d . id ) ;
2015-11-26 22:04:02 +01:00
if ( chat ! = null & & chat . megagroup ) {
d . top_message = Math . max ( d . top_message , d . top_not_important_message ) ;
d . unread_count = Math . max ( d . unread_count , d . unread_not_important_count ) ;
}
2016-01-11 18:19:48 +01:00
if ( chat ! = null & & chat . left /* && !chat.megagroup*/ ) {
2015-09-24 22:52:02 +02:00
continue ;
}
channelsPts . put ( - ( int ) d . id , d . pts ) ;
2015-11-26 22:04:02 +01:00
} else if ( ( int ) d . id < 0 ) {
TLRPC . Chat chat = chatsDict . get ( - ( int ) d . id ) ;
if ( chat ! = null & & chat . migrated_to ! = null ) {
continue ;
}
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
new_dialogs_dict . put ( d . id , d ) ;
2015-11-26 22:04:02 +01: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 ) ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
if ( ! isCache ) {
ImageLoader . saveMessagesThumbs ( dialogsRes . messages ) ;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
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 ) ;
2015-11-26 22:04:02 +01:00
if ( user ! = null & & user . bot ) {
2015-09-24 22:52:02 +02:00
message . reply_markup = new TLRPC . TL_replyKeyboardHide ( ) ;
}
2015-11-26 22:04:02 +01:00
} else if ( message . action instanceof TLRPC . TL_messageActionChatMigrateTo | | message . action instanceof TLRPC . TL_messageActionChannelCreate ) {
message . unread = false ;
message . media_unread = false ;
2015-09-24 22:52:02 +02:00
}
}
MessagesStorage . getInstance ( ) . putDialogs ( dialogsRes ) ;
}
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( ! isCache ) {
applyDialogsNotificationsSettings ( dialogsRes . dialogs ) ;
}
putUsers ( dialogsRes . users , isCache ) ;
putChats ( dialogsRes . chats , isCache ) ;
if ( encChats ! = null ) {
2015-11-26 22:04:02 +01:00
for ( int a = 0 ; a < encChats . size ( ) ; a + + ) {
TLRPC . EncryptedChat encryptedChat = encChats . get ( a ) ;
2015-09-24 22:52:02 +02:00
if ( encryptedChat instanceof TLRPC . TL_encryptedChat & & AndroidUtilities . getMyLayerVersion ( encryptedChat . layer ) < SecretChatHelper . CURRENT_SECRET_CHAT_LAYER ) {
SecretChatHelper . getInstance ( ) . sendNotifyLayerMessage ( encryptedChat , null ) ;
}
putEncryptedChat ( encryptedChat , true ) ;
}
}
2015-11-26 22:04:02 +01:00
if ( ! migrate ) {
loadingDialogs = false ;
}
2015-10-29 18:10:07 +01:00
boolean added = false ;
2015-09-24 22:52:02 +02:00
2015-11-26 22:04:02 +01:00
int lastDialogDate = migrate & & ! dialogs . isEmpty ( ) ? dialogs . get ( dialogs . size ( ) - 1 ) . last_message_date : 0 ;
2015-09-24 22:52:02 +02:00
for ( HashMap . Entry < Long , TLRPC . Dialog > pair : new_dialogs_dict . entrySet ( ) ) {
Long key = pair . getKey ( ) ;
TLRPC . Dialog value = pair . getValue ( ) ;
2015-11-26 22:04:02 +01:00
if ( migrate & & lastDialogDate ! = 0 & & value . last_message_date < lastDialogDate ) {
continue ;
}
2015-09-24 22:52:02 +02:00
TLRPC . Dialog currentDialog = dialogs_dict . get ( key ) ;
if ( currentDialog = = null ) {
2015-10-29 18:10:07 +01:00
added = true ;
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 ) ;
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
}
} else {
2016-03-16 13:26:32 +01:00
if ( ! isCache ) {
currentDialog . notify_settings = value . notify_settings ;
}
2015-09-24 22:52:02 +02:00
MessageObject oldMsg = dialogMessage . get ( key ) ;
if ( oldMsg ! = null & & oldMsg . deleted | | oldMsg = = null | | currentDialog . top_message > 0 ) {
if ( 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 ! = null & & messageObject . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . put ( messageObject . messageOwner . random_id , messageObject ) ;
}
}
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
}
}
} else {
2015-09-24 22:52:02 +02:00
MessageObject newMsg = new_dialogMessage . get ( value . id ) ;
2013-10-25 17:19:00 +02:00
if ( oldMsg . deleted | | newMsg = = null | | newMsg . messageOwner . date > oldMsg . messageOwner . date ) {
dialogs_dict . put ( key , value ) ;
2015-09-24 22:52:02 +02:00
dialogMessage . put ( key , newMsg ) ;
if ( newMsg ! = null & & newMsg . messageOwner . to_id . channel_id = = 0 ) {
dialogMessagesByIds . put ( newMsg . getId ( ) , newMsg ) ;
2016-03-06 02:49:31 +01:00
if ( newMsg ! = null & & newMsg . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . put ( newMsg . messageOwner . random_id , newMsg ) ;
}
}
dialogMessagesByIds . remove ( oldMsg . getId ( ) ) ;
if ( oldMsg . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . remove ( oldMsg . messageOwner . random_id ) ;
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
}
}
}
dialogs . clear ( ) ;
dialogsServerOnly . clear ( ) ;
2015-06-29 19:12:11 +02:00
dialogsGroupsOnly . clear ( ) ;
2013-10-25 17:19:00 +02:00
dialogs . addAll ( dialogs_dict . values ( ) ) ;
2015-09-24 22:52:02 +02:00
Collections . sort ( dialogs , new Comparator < TLRPC . Dialog > ( ) {
2013-10-25 17:19:00 +02:00
@Override
2015-09-24 22:52:02 +02:00
public int compare ( TLRPC . Dialog tl_dialog , TLRPC . Dialog tl_dialog2 ) {
2013-10-25 17:19:00 +02:00
if ( tl_dialog . last_message_date = = tl_dialog2 . last_message_date ) {
return 0 ;
} else if ( tl_dialog . last_message_date < tl_dialog2 . last_message_date ) {
return 1 ;
} else {
return - 1 ;
}
}
} ) ;
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < dialogs . size ( ) ; a + + ) {
TLRPC . Dialog d = dialogs . get ( a ) ;
2015-03-19 00:09:45 +01:00
int high_id = ( int ) ( d . id > > 32 ) ;
if ( ( int ) d . id ! = 0 & & high_id ! = 1 ) {
2015-09-24 22:52:02 +02:00
if ( d instanceof TLRPC . TL_dialog ) {
if ( d . id < 0 ) {
2015-11-26 22:04:02 +01:00
if ( migrate ) {
TLRPC . Chat chat = chatsDict . get ( - ( int ) d . id ) ;
if ( chat ! = null & & chat . migrated_to ! = null ) {
dialogs . remove ( a ) ;
a - - ;
continue ;
}
}
2015-09-24 22:52:02 +02:00
dialogsGroupsOnly . add ( d ) ;
}
} else if ( d instanceof TLRPC . TL_dialogChannel ) {
2015-11-26 22:04:02 +01:00
int lower_id = ( int ) d . id ;
TLRPC . Chat chat = getChat ( - lower_id ) ;
if ( chat ! = null & & ( chat . megagroup & & chat . editor | | chat . creator ) ) {
dialogsGroupsOnly . add ( d ) ;
}
2015-06-29 19:12:11 +02:00
}
2015-11-26 22:04:02 +01:00
dialogsServerOnly . add ( d ) ;
2013-10-25 17:19:00 +02:00
}
}
2015-09-24 22:52:02 +02:00
2015-11-26 22:04:02 +01:00
if ( ! migrate ) {
2015-10-29 18:10:07 +01:00
dialogsEndReached = ( dialogsRes . dialogs . size ( ) = = 0 | | dialogsRes . dialogs . size ( ) ! = count ) & & ! isCache ;
}
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2015-10-29 18:10:07 +01:00
2015-11-26 22:04:02 +01:00
if ( migrate ) {
UserConfig . migrateOffsetId = offset ;
UserConfig . saveConfig ( false ) ;
migratingDialogs = false ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . needReloadRecentDialogsSearch ) ;
} else {
generateUpdateMessage ( ) ;
if ( ! added & & isCache ) {
loadDialogs ( 0 , count , false ) ;
}
2015-10-29 18:10:07 +01:00
}
2015-11-26 22:04:02 +01:00
migrateDialogs ( UserConfig . migrateOffsetId , UserConfig . migrateOffsetDate , UserConfig . migrateOffsetUserId , UserConfig . migrateOffsetChatId , UserConfig . migrateOffsetChannelId , UserConfig . migrateOffsetAccess ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
2015-03-19 00:09:45 +01:00
}
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 ) {
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
int currentValue = preferences . getInt ( " notify2_ " + dialog_id , 0 ) ;
int currentValue2 = preferences . getInt ( " notifyuntil_ " + dialog_id , 0 ) ;
2016-03-06 02:49:31 +01:00
SharedPreferences . Editor editor = preferences . edit ( ) ;
boolean updated = false ;
2015-11-26 22:04:02 +01:00
TLRPC . Dialog dialog = dialogs_dict . get ( dialog_id ) ;
if ( dialog ! = null ) {
dialog . notify_settings = notify_settings ;
}
2016-03-06 02:49:31 +01:00
editor . putBoolean ( " silent_ " + dialog_id , notify_settings . silent ) ;
2015-11-26 22:04:02 +01:00
if ( notify_settings . mute_until > ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) {
int until = 0 ;
if ( notify_settings . mute_until > ConnectionsManager . getInstance ( ) . getCurrentTime ( ) + 60 * 60 * 24 * 365 ) {
if ( currentValue ! = 2 ) {
2016-03-06 02:49:31 +01:00
updated = true ;
2015-11-26 22:04:02 +01:00
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 ) {
2016-03-06 02:49:31 +01:00
updated = true ;
2015-11-26 22:04:02 +01:00
until = notify_settings . mute_until ;
editor . putInt ( " notify2_ " + dialog_id , 3 ) ;
editor . putInt ( " notifyuntil_ " + dialog_id , notify_settings . mute_until ) ;
if ( dialog ! = null ) {
dialog . notify_settings . mute_until = until ;
}
}
}
MessagesStorage . getInstance ( ) . setDialogFlags ( dialog_id , ( ( long ) until < < 32 ) | 1 ) ;
NotificationsController . getInstance ( ) . removeNotificationsForDialog ( dialog_id ) ;
} else {
if ( currentValue ! = 0 ) {
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 ) ;
}
MessagesStorage . getInstance ( ) . setDialogFlags ( dialog_id , 0 ) ;
}
2016-03-06 02:49:31 +01:00
editor . commit ( ) ;
if ( updated ) {
2015-11-26 22:04:02 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . notificationsSettingsUpdated ) ;
}
}
2015-09-24 22:52:02 +02:00
private void applyDialogsNotificationsSettings ( ArrayList < TLRPC . Dialog > dialogs ) {
SharedPreferences . Editor editor = null ;
2015-11-26 22:04:02 +01:00
for ( int a = 0 ; a < dialogs . size ( ) ; a + + ) {
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 ) {
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
editor = preferences . edit ( ) ;
}
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 ;
}
2016-03-06 02:49:31 +01:00
editor . putBoolean ( " silent_ " + dialog_id , dialog . notify_settings . silent ) ;
2015-09-24 22:52:02 +02:00
if ( dialog . notify_settings . mute_until ! = 0 ) {
if ( dialog . notify_settings . mute_until > ConnectionsManager . getInstance ( ) . getCurrentTime ( ) + 60 * 60 * 24 * 365 ) {
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-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 ( ) ;
}
}
public void processDialogsUpdateRead ( final HashMap < Long , Integer > dialogsToUpdate ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
for ( HashMap . Entry < Long , Integer > entry : dialogsToUpdate . entrySet ( ) ) {
TLRPC . Dialog currentDialog = dialogs_dict . get ( entry . getKey ( ) ) ;
if ( currentDialog ! = null ) {
currentDialog . unread_count = entry . getValue ( ) ;
}
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_READ_DIALOG_MESSAGE ) ;
NotificationsController . getInstance ( ) . processDialogsUpdateRead ( dialogsToUpdate ) ;
}
} ) ;
}
public void processDialogsUpdate ( final TLRPC . messages_Dialogs dialogsRes , ArrayList < TLRPC . EncryptedChat > encChats ) {
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
final HashMap < Long , TLRPC . Dialog > new_dialogs_dict = new HashMap < > ( ) ;
final HashMap < Long , MessageObject > new_dialogMessage = new HashMap < > ( ) ;
2015-06-29 19:12:11 +02:00
final HashMap < Integer , TLRPC . User > usersDict = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
final HashMap < Integer , TLRPC . Chat > chatsDict = new HashMap < > ( ) ;
final HashMap < Long , Integer > dialogsToUpdate = new HashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < dialogsRes . users . size ( ) ; a + + ) {
TLRPC . User u = dialogsRes . users . get ( a ) ;
2015-06-29 19:12:11 +02:00
usersDict . put ( u . id , u ) ;
}
2015-09-24 22:52:02 +02:00
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
}
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < dialogsRes . messages . size ( ) ; a + + ) {
TLRPC . Message message = dialogsRes . messages . get ( a ) ;
if ( message . to_id . channel_id ! = 0 ) {
TLRPC . Chat chat = chatsDict . get ( message . to_id . channel_id ) ;
2016-01-11 18:19:48 +01:00
if ( chat ! = null & & chat . left /* && !chat.megagroup*/ ) {
2015-11-26 22:04:02 +01:00
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 ) {
2015-09-24 22:52:02 +02:00
continue ;
2013-10-25 17:19:00 +02:00
}
}
2015-11-26 22:04:02 +01:00
MessageObject messageObject = new MessageObject ( message , usersDict , chatsDict , false ) ;
2015-09-24 22:52:02 +02:00
new_dialogMessage . put ( messageObject . getDialogId ( ) , messageObject ) ;
}
for ( int a = 0 ; a < dialogsRes . dialogs . size ( ) ; a + + ) {
TLRPC . Dialog d = dialogsRes . dialogs . get ( a ) ;
2013-10-25 17:19:00 +02:00
if ( d . id = = 0 ) {
2015-09-24 22:52:02 +02:00
if ( d . peer . user_id ! = 0 ) {
2013-10-25 17:19:00 +02:00
d . id = d . peer . user_id ;
2015-09-24 22:52:02 +02:00
} else if ( d . peer . chat_id ! = 0 ) {
2013-10-25 17:19:00 +02:00
d . id = - d . peer . chat_id ;
2015-09-24 22:52:02 +02:00
} else if ( d . peer . channel_id ! = 0 ) {
d . id = - d . peer . channel_id ;
}
}
if ( d instanceof TLRPC . TL_dialogChannel ) {
TLRPC . Chat chat = chatsDict . get ( - ( int ) d . id ) ;
2016-01-11 18:19:48 +01:00
if ( chat ! = null & & chat . left /* && !chat.megagroup*/ ) {
2015-11-26 22:04:02 +01:00
continue ;
}
} else if ( ( int ) d . id < 0 ) {
TLRPC . Chat chat = chatsDict . get ( - ( int ) d . id ) ;
if ( chat ! = null & & chat . migrated_to ! = null ) {
2015-09-24 22:52:02 +02:00
continue ;
}
}
if ( d . last_message_date = = 0 ) {
MessageObject mess = new_dialogMessage . get ( d . id ) ;
if ( mess ! = null ) {
d . last_message_date = mess . messageOwner . date ;
2013-10-25 17:19:00 +02:00
}
}
new_dialogs_dict . put ( d . id , d ) ;
2015-09-24 22:52:02 +02:00
dialogsToUpdate . put ( d . id , d . unread_count ) ;
2015-11-26 22:04:02 +01: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 ) ) ;
2013-10-25 17:19:00 +02:00
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
putUsers ( dialogsRes . users , true ) ;
putChats ( dialogsRes . chats , true ) ;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
for ( HashMap . Entry < Long , TLRPC . Dialog > pair : new_dialogs_dict . entrySet ( ) ) {
Long key = pair . getKey ( ) ;
TLRPC . Dialog value = pair . getValue ( ) ;
TLRPC . Dialog currentDialog = dialogs_dict . get ( key ) ;
2013-10-25 17:19:00 +02:00
if ( currentDialog = = null ) {
2015-09-24 22:52:02 +02:00
nextDialogsCacheOffset + + ;
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
}
2013-10-25 17:19:00 +02:00
} else {
2015-09-24 22:52:02 +02:00
currentDialog . unread_count = value . unread_count ;
MessageObject oldMsg = dialogMessage . get ( key ) ;
2013-10-25 17:19:00 +02:00
if ( oldMsg = = null | | currentDialog . top_message > 0 ) {
if ( oldMsg ! = null & & oldMsg . deleted | | value . top_message > currentDialog . top_message ) {
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 ) ;
}
}
if ( oldMsg ! = null ) {
dialogMessagesByIds . remove ( oldMsg . getId ( ) ) ;
if ( oldMsg . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . remove ( oldMsg . messageOwner . random_id ) ;
}
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
} else {
2015-09-24 22:52:02 +02:00
MessageObject newMsg = new_dialogMessage . get ( value . id ) ;
2013-10-25 17:19:00 +02:00
if ( oldMsg . deleted | | newMsg = = null | | newMsg . messageOwner . date > oldMsg . messageOwner . date ) {
dialogs_dict . put ( key , value ) ;
2015-09-24 22:52:02 +02:00
dialogMessage . put ( key , newMsg ) ;
if ( newMsg ! = null & & newMsg . messageOwner . to_id . channel_id = = 0 ) {
dialogMessagesByIds . put ( newMsg . getId ( ) , newMsg ) ;
2016-03-06 02:49:31 +01:00
if ( newMsg . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . put ( newMsg . messageOwner . random_id , newMsg ) ;
}
}
dialogMessagesByIds . remove ( oldMsg . getId ( ) ) ;
if ( oldMsg . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . remove ( oldMsg . messageOwner . random_id ) ;
2015-09-24 22:52:02 +02:00
}
2013-10-25 17:19:00 +02:00
}
}
}
}
dialogs . clear ( ) ;
dialogsServerOnly . clear ( ) ;
2015-06-29 19:12:11 +02:00
dialogsGroupsOnly . clear ( ) ;
2013-10-25 17:19:00 +02:00
dialogs . addAll ( dialogs_dict . values ( ) ) ;
2015-09-24 22:52:02 +02:00
Collections . sort ( dialogs , new Comparator < TLRPC . Dialog > ( ) {
2013-10-25 17:19:00 +02:00
@Override
2015-09-24 22:52:02 +02:00
public int compare ( TLRPC . Dialog dialog , TLRPC . Dialog dialog2 ) {
if ( dialog . last_message_date = = dialog2 . last_message_date ) {
2013-10-25 17:19:00 +02:00
return 0 ;
2015-09-24 22:52:02 +02:00
} else if ( dialog . last_message_date < dialog2 . last_message_date ) {
2013-10-25 17:19:00 +02:00
return 1 ;
} else {
return - 1 ;
}
}
} ) ;
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < dialogs . size ( ) ; a + + ) {
TLRPC . Dialog d = dialogs . get ( a ) ;
2015-03-19 00:09:45 +01:00
int high_id = ( int ) ( d . id > > 32 ) ;
if ( ( int ) d . id ! = 0 & & high_id ! = 1 ) {
2013-10-25 17:19:00 +02:00
dialogsServerOnly . add ( d ) ;
2015-09-24 22:52:02 +02:00
if ( d instanceof TLRPC . TL_dialog ) {
if ( d . id < 0 ) {
dialogsGroupsOnly . add ( d ) ;
}
} else if ( d instanceof TLRPC . TL_dialogChannel ) {
2015-11-26 22:04:02 +01:00
int lower_id = ( int ) d . id ;
TLRPC . Chat chat = getChat ( - lower_id ) ;
if ( chat ! = null & & ( chat . megagroup & & chat . editor | | chat . creator ) ) {
dialogsGroupsOnly . add ( d ) ;
}
2015-06-29 19:12:11 +02:00
}
2013-10-25 17:19:00 +02:00
}
}
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2015-09-24 22:52:02 +02:00
NotificationsController . getInstance ( ) . processDialogsUpdateRead ( dialogsToUpdate ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
} ) ;
}
2015-09-24 22:52:02 +02:00
public void addToViewsQueue ( final TLRPC . Message message , final boolean reload ) {
ArrayList < Long > arrayList = new ArrayList < > ( ) ;
long messageId = message . id ;
if ( message . to_id . channel_id ! = 0 ) {
messageId | = ( ( long ) message . to_id . channel_id ) < < 32 ;
}
arrayList . add ( messageId ) ;
MessagesStorage . getInstance ( ) . markMessagesContentAsRead ( arrayList ) ;
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
SparseArray < ArrayList < Integer > > array = channelViewsToSend ; //reload ? channelViewsToReload : channelViewsToSend;
int peer ;
if ( message . to_id . channel_id ! = 0 ) {
peer = - message . to_id . channel_id ;
} else if ( message . to_id . chat_id ! = 0 ) {
peer = - message . to_id . chat_id ;
} else {
peer = message . to_id . user_id ;
}
ArrayList < Integer > ids = array . get ( peer ) ;
if ( ids = = null ) {
ids = new ArrayList < > ( ) ;
array . put ( peer , ids ) ;
}
if ( ! ids . contains ( message . id ) ) {
ids . add ( message . id ) ;
}
}
} ) ;
}
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
}
arrayList . add ( messageId ) ;
MessagesStorage . getInstance ( ) . markMessagesContentAsRead ( arrayList ) ;
NotificationCenter . getInstance ( ) . 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 ) ;
} else {
TLRPC . TL_messages_readMessageContents req = new TLRPC . TL_messages_readMessageContents ( ) ;
req . id . add ( messageObject . getId ( ) ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
TLRPC . TL_messages_affectedMessages res = ( TLRPC . TL_messages_affectedMessages ) response ;
processNewDifferenceParams ( - 1 , res . pts , - 1 , res . pts_count ) ;
}
2015-05-03 13:48:36 +02:00
}
2016-03-06 02:49:31 +01:00
} ) ;
}
2015-05-03 13:48:36 +02: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 ) {
2014-10-09 17:55:05 +02:00
return ;
}
2015-05-21 23:27:27 +02:00
int lower_part = ( int ) dialog_id ;
int high_id = ( int ) ( dialog_id > > 32 ) ;
2014-10-09 17:55:05 +02:00
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 < > ( ) ;
2014-10-09 17:55:05 +02:00
random_ids . add ( random_id ) ;
2014-12-01 18:56:31 +01:00
SecretChatHelper . getInstance ( ) . sendMessagesReadMessage ( chat , random_ids , null ) ;
2016-03-06 02:49:31 +01:00
if ( ttl > 0 ) {
int time = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
MessagesStorage . getInstance ( ) . createTaskForSecretChat ( chat . id , time , time , 0 , random_ids ) ;
}
2014-10-09 17:55:05 +02:00
}
2015-11-26 22:04:02 +01:00
public void markDialogAsRead ( final long dialog_id , final int max_id , final int max_positive_id , final int max_date , final boolean was , final boolean popup ) {
2015-05-21 23:27:27 +02:00
int lower_part = ( int ) dialog_id ;
int high_id = ( int ) ( dialog_id > > 32 ) ;
2014-07-27 12:53:00 +02:00
2013-10-25 17:19:00 +02:00
if ( lower_part ! = 0 ) {
2015-11-26 22:04:02 +01:00
if ( max_positive_id = = 0 | | high_id = = 1 ) {
2013-10-25 17:19:00 +02:00
return ;
}
2015-09-24 22:52:02 +02:00
TLRPC . InputPeer inputPeer = getInputPeer ( lower_part ) ;
TLObject req ;
long messageId = max_positive_id ;
if ( inputPeer instanceof TLRPC . TL_inputPeerChannel ) {
TLRPC . TL_channels_readHistory request = new TLRPC . TL_channels_readHistory ( ) ;
request . channel = getInputChannel ( - lower_part ) ;
request . max_id = max_positive_id ;
req = request ;
messageId | = ( ( long ) - lower_part ) < < 32 ;
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 ;
request . max_id = max_positive_id ;
req = request ;
2013-10-25 17:19:00 +02:00
}
2015-11-26 22:04:02 +01:00
Integer value = dialogs_read_inbox_max . get ( dialog_id ) ;
if ( value = = null ) {
value = 0 ;
}
dialogs_read_inbox_max . put ( dialog_id , Math . max ( value , max_positive_id ) ) ;
2015-09-24 22:52:02 +02:00
2015-11-26 22:04:02 +01:00
MessagesStorage . getInstance ( ) . processPendingRead ( dialog_id , messageId , max_date , false ) ;
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
TLRPC . Dialog dialog = dialogs_dict . get ( dialog_id ) ;
if ( dialog ! = null ) {
dialog . unread_count = 0 ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_READ_DIALOG_MESSAGE ) ;
2014-08-22 16:24:33 +02:00
}
2015-11-26 22:04:02 +01:00
if ( ! popup ) {
NotificationsController . getInstance ( ) . processReadMessages ( null , dialog_id , 0 , max_positive_id , false ) ;
HashMap < Long , Integer > dialogsToUpdate = new HashMap < > ( ) ;
dialogsToUpdate . put ( dialog_id , 0 ) ;
NotificationsController . getInstance ( ) . processDialogsUpdateRead ( dialogsToUpdate ) ;
} else {
NotificationsController . getInstance ( ) . processReadMessages ( null , dialog_id , 0 , max_positive_id , true ) ;
HashMap < Long , Integer > dialogsToUpdate = new HashMap < > ( ) ;
dialogsToUpdate . put ( dialog_id , - 1 ) ;
NotificationsController . getInstance ( ) . processDialogsUpdateRead ( dialogsToUpdate ) ;
}
}
} ) ;
}
} ) ;
2015-09-24 22:52:02 +02:00
if ( max_positive_id ! = Integer . MAX_VALUE ) {
final long messageIdFinal = messageId ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . processPendingRead ( dialog_id , messageIdFinal , max_date , true ) ;
2015-11-26 22:04:02 +01:00
if ( response instanceof TLRPC . TL_messages_affectedMessages ) {
TLRPC . TL_messages_affectedMessages res = ( TLRPC . TL_messages_affectedMessages ) response ;
2015-09-24 22:52:02 +02:00
processNewDifferenceParams ( - 1 , res . pts , - 1 , res . pts_count ) ;
2013-10-25 17:19:00 +02:00
}
}
}
2014-08-22 16:24:33 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
} else {
2014-08-22 16:24:33 +02:00
if ( max_date = = 0 ) {
return ;
2013-10-25 17:19:00 +02:00
}
2014-08-22 16:24:33 +02:00
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 = max_date ;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
//MessagesStorage.getInstance().processPendingRead(dialog_id, max_id, max_date, true);
2013-10-25 17:19:00 +02:00
}
2014-08-22 16:24:33 +02:00
} ) ;
}
MessagesStorage . getInstance ( ) . processPendingRead ( dialog_id , max_id , max_date , false ) ;
2014-10-31 20:02:29 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
NotificationsController . getInstance ( ) . processReadMessages ( null , dialog_id , max_date , 0 , popup ) ;
2015-09-24 22:52:02 +02:00
TLRPC . Dialog dialog = dialogs_dict . get ( dialog_id ) ;
2014-08-22 16:24:33 +02:00
if ( dialog ! = null ) {
dialog . unread_count = 0 ;
2014-11-11 23:16:17 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_READ_DIALOG_MESSAGE ) ;
2014-07-27 00:28:33 +02:00
}
2015-01-02 23:15:07 +01:00
HashMap < Long , Integer > dialogsToUpdate = new HashMap < > ( ) ;
2014-08-22 16:24:33 +02:00
dialogsToUpdate . put ( dialog_id , 0 ) ;
2014-08-27 00:26:25 +02:00
NotificationsController . getInstance ( ) . processDialogsUpdateRead ( dialogsToUpdate ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
2014-08-22 16:24:33 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
2014-08-22 16:24:33 +02:00
if ( chat . ttl > 0 & & was ) {
int serverTime = Math . max ( ConnectionsManager . getInstance ( ) . getCurrentTime ( ) , max_date ) ;
2014-10-09 17:55:05 +02:00
MessagesStorage . getInstance ( ) . createTaskForSecretChat ( chat . id , serverTime , serverTime , 0 , null ) ;
2014-08-22 16:24:33 +02:00
}
2014-02-28 23:28:25 +01:00
}
}
2016-03-06 02:49:31 +01:00
public int createChat ( String title , ArrayList < Integer > selectedContacts , final String about , int type , final BaseFragment fragment ) {
2015-09-24 22:52:02 +02:00
if ( type = = ChatObject . CHAT_TYPE_BROADCAST ) {
2014-07-27 00:28:33 +02:00
TLRPC . TL_chat chat = new TLRPC . TL_chat ( ) ;
chat . id = UserConfig . 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 ) ;
2014-07-27 00:28:33 +02:00
chat . version = 1 ;
UserConfig . lastBroadcastId - - ;
2014-08-22 16:24:33 +02:00
putChat ( chat , false ) ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . Chat > chatsArrays = new ArrayList < > ( ) ;
2014-07-27 00:28:33 +02:00
chatsArrays . add ( chat ) ;
MessagesStorage . getInstance ( ) . 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 ( ) ;
chatFull . notify_settings = new TLRPC . TL_peerNotifySettingsEmpty ( ) ;
chatFull . exported_invite = new TLRPC . TL_chatInviteEmpty ( ) ;
chatFull . participants = new TLRPC . TL_chatParticipants ( ) ;
chatFull . participants . chat_id = chat . id ;
chatFull . participants . admin_id = UserConfig . getClientUserId ( ) ;
chatFull . participants . version = 1 ;
2016-03-06 02:49:31 +01:00
for ( int a = 0 ; a < selectedContacts . size ( ) ; a + + ) {
2014-07-27 00:28:33 +02:00
TLRPC . TL_chatParticipant participant = new TLRPC . TL_chatParticipant ( ) ;
2016-03-06 02:49:31 +01:00
participant . user_id = selectedContacts . get ( a ) ;
2014-07-27 00:28:33 +02:00
participant . inviter_id = UserConfig . 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
}
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . updateChatInfo ( chatFull , false ) ;
2014-07-27 00:28:33 +02:00
TLRPC . TL_messageService newMsg = new TLRPC . TL_messageService ( ) ;
newMsg . action = new TLRPC . TL_messageActionCreatedBroadcastList ( ) ;
newMsg . local_id = newMsg . id = UserConfig . getNewMessageId ( ) ;
newMsg . from_id = UserConfig . getClientUserId ( ) ;
2014-07-27 12:53:00 +02:00
newMsg . dialog_id = AndroidUtilities . makeBroadcastId ( chat . id ) ;
2014-07-27 00:28:33 +02:00
newMsg . to_id = new TLRPC . TL_peerChat ( ) ;
newMsg . to_id . chat_id = chat . id ;
newMsg . date = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
newMsg . random_id = 0 ;
2015-09-24 22:52:02 +02:00
newMsg . flags | = TLRPC . MESSAGE_FLAG_HAS_FROM_ID ;
2014-07-27 00:28:33 +02:00
UserConfig . saveConfig ( false ) ;
2015-02-01 19:51:02 +01:00
MessageObject newMsgObj = new MessageObject ( newMsg , users , true ) ;
2014-08-22 16:24:33 +02:00
newMsgObj . messageOwner . send_state = MessageObject . MESSAGE_SEND_STATE_SENT ;
2014-07-27 00:28:33 +02:00
2015-01-02 23:15:07 +01:00
ArrayList < MessageObject > objArr = new ArrayList < > ( ) ;
2014-07-27 00:28:33 +02:00
objArr . add ( newMsgObj ) ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . Message > arr = new ArrayList < > ( ) ;
2014-07-27 00:28:33 +02:00
arr . add ( newMsg ) ;
2014-08-22 16:24:33 +02:00
MessagesStorage . getInstance ( ) . putMessages ( arr , false , true , false , 0 ) ;
2014-07-27 00:28:33 +02:00
updateInterfaceWithMessages ( newMsg . dialog_id , objArr ) ;
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2015-02-26 02:32:51 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidCreated , chat . id ) ;
2014-07-27 00:28:33 +02:00
return 0 ;
2015-09-24 22:52:02 +02:00
} else if ( type = = ChatObject . CHAT_TYPE_CHAT ) {
2014-07-27 00:28:33 +02:00
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 ) ) ;
2014-07-27 00:28:33 +02:00
if ( user = = null ) {
continue ;
}
req . users . add ( getInputUser ( user ) ) ;
}
2015-09-24 22:52:02 +02:00
return ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-07-27 00:28:33 +02:00
@Override
2016-03-06 02:49:31 +01:00
public void run ( TLObject response , final TLRPC . TL_error error ) {
2014-07-27 00:28:33 +02:00
if ( error ! = null ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-07-27 00:28:33 +02:00
@Override
public void run ( ) {
2016-03-16 13:26:32 +01:00
if ( error . text . startsWith ( " FLOOD_WAIT " ) ) {
AlertsCreator . showFloodWaitAlert ( error . text , fragment ) ;
} else {
AlertsCreator . showAddUserAlert ( error . text , fragment , false ) ;
}
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidFailCreate ) ;
2014-07-27 00:28:33 +02:00
}
} ) ;
return ;
}
2015-04-09 20:00:14 +02:00
final TLRPC . Updates updates = ( TLRPC . Updates ) response ;
processUpdates ( updates , false ) ;
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2015-04-09 20:00:14 +02:00
putUsers ( updates . users , false ) ;
putChats ( updates . chats , false ) ;
if ( updates . chats ! = null & & ! updates . chats . isEmpty ( ) ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidCreated , updates . chats . get ( 0 ) . id ) ;
} else {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidFailCreate ) ;
2014-07-27 00:28:33 +02:00
}
2013-10-25 17:19:00 +02:00
}
} ) ;
2014-02-04 19:36:55 +01: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 ) {
2015-09-24 22:52:02 +02:00
TLRPC . TL_channels_createChannel req = new TLRPC . TL_channels_createChannel ( ) ;
req . title = title ;
req . about = 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
}
return ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
2016-03-16 13:26:32 +01:00
public void run ( TLObject response , final TLRPC . TL_error error ) {
2015-09-24 22:52:02 +02:00
if ( error ! = null ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2016-03-16 13:26:32 +01:00
if ( error . text . startsWith ( " FLOOD_WAIT " ) ) {
AlertsCreator . showFloodWaitAlert ( error . text , fragment ) ;
}
2015-09-24 22:52:02 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidFailCreate ) ;
}
} ) ;
return ;
}
final TLRPC . Updates updates = ( TLRPC . Updates ) response ;
processUpdates ( updates , false ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
putUsers ( updates . users , false ) ;
putChats ( updates . chats , false ) ;
if ( updates . chats ! = null & & ! updates . chats . isEmpty ( ) ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidCreated , updates . chats . get ( 0 ) . id ) ;
} else {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatDidFailCreate ) ;
}
}
} ) ;
}
2016-03-16 13:26:32 +01:00
} , ConnectionsManager . RequestFlagFailOnServerErrors ) ;
2015-09-24 22:52:02 +02:00
}
return 0 ;
}
2015-11-26 22:04:02 +01:00
public void convertToMegaGroup ( final Context context , int chat_id ) {
TLRPC . TL_messages_migrateChat req = new TLRPC . TL_messages_migrateChat ( ) ;
req . chat_id = chat_id ;
final ProgressDialog progressDialog = new ProgressDialog ( context ) ;
progressDialog . setMessage ( LocaleController . getString ( " Loading " , R . string . Loading ) ) ;
progressDialog . setCanceledOnTouchOutside ( false ) ;
progressDialog . setCancelable ( false ) ;
final int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( ! ( ( Activity ) context ) . isFinishing ( ) ) {
try {
progressDialog . dismiss ( ) ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
}
}
} ) ;
TLRPC . Updates updates = ( TLRPC . Updates ) response ;
processUpdates ( ( TLRPC . Updates ) response , false ) ;
} else {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( ! ( ( Activity ) context ) . isFinishing ( ) ) {
try {
progressDialog . dismiss ( ) ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
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 ) ;
}
}
} ) ;
}
}
} ) ;
progressDialog . setButton ( DialogInterface . BUTTON_NEGATIVE , LocaleController . getString ( " Cancel " , R . string . Cancel ) , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
ConnectionsManager . getInstance ( ) . cancelRequest ( reqId , true ) ;
try {
dialog . dismiss ( ) ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
}
} ) ;
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 ;
}
TLRPC . TL_channels_inviteToChannel req = new TLRPC . TL_channels_inviteToChannel ( ) ;
req . channel = getInputChannel ( chat_id ) ;
req . users = users ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , final TLRPC . TL_error error ) {
if ( error ! = null ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2015-11-26 22:04:02 +01:00
if ( fragment ! = null ) {
AlertsCreator . showAddUserAlert ( error . text , fragment , true ) ;
} else if ( error . text . equals ( " PEER_FLOOD " ) ) {
2015-12-09 19:27:52 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . needShowAlert , 1 ) ;
2015-09-24 22:52:02 +02:00
}
}
} ) ;
return ;
}
processUpdates ( ( TLRPC . Updates ) response , false ) ;
}
} ) ;
}
2016-03-06 02:49:31 +01:00
public void toogleChannelInvites ( int chat_id , boolean enabled ) {
TLRPC . TL_channels_toggleInvites req = new TLRPC . TL_channels_toggleInvites ( ) ;
req . channel = getInputChannel ( chat_id ) ;
req . enabled = enabled ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( response ! = null ) {
processUpdates ( ( TLRPC . Updates ) response , false ) ;
}
}
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
}
2015-09-24 22:52:02 +02:00
public void toogleChannelComments ( int chat_id , boolean enabled ) {
TLRPC . TL_channels_toggleComments req = new TLRPC . TL_channels_toggleComments ( ) ;
req . channel = getInputChannel ( chat_id ) ;
req . enabled = enabled ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( response ! = null ) {
processUpdates ( ( TLRPC . Updates ) response , false ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_CHANNEL ) ;
}
} ) ;
}
}
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
}
2016-03-06 02:49:31 +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 ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( response ! = null ) {
processUpdates ( ( TLRPC . Updates ) response , false ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_CHANNEL ) ;
}
} ) ;
}
}
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
}
2015-09-24 22:52:02 +02:00
public void updateChannelAbout ( int chat_id , final String about , final TLRPC . ChatFull info ) {
if ( info = = null ) {
return ;
2014-07-27 00:28:33 +02:00
}
2015-09-24 22:52:02 +02:00
TLRPC . TL_channels_editAbout req = new TLRPC . TL_channels_editAbout ( ) ;
req . channel = getInputChannel ( chat_id ) ;
req . about = about ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( response instanceof TLRPC . TL_boolTrue ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
info . about = about ;
MessagesStorage . getInstance ( ) . updateChatInfo ( info , false ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoDidLoaded , info , 0 , false , null ) ;
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 ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( response instanceof TLRPC . TL_boolTrue ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
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 ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( null , arrayList , true , true ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_CHANNEL ) ;
}
} ) ;
}
}
} , 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 ) ;
2015-11-26 22:04:02 +01:00
req . peer = getInputPeer ( user . id ) ;
2015-06-29 19:12:11 +02:00
req . start_param = botHash ;
req . random_id = Utilities . random . nextLong ( ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2015-06-29 19:12:11 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error ! = null ) {
return ;
}
processUpdates ( ( TLRPC . Updates ) response , false ) ;
}
} ) ;
}
2015-11-26 22:04:02 +01:00
public void toggleAdminMode ( final int chat_id , boolean enabled ) {
TLRPC . TL_messages_toggleChatAdmins req = new TLRPC . TL_messages_toggleChatAdmins ( ) ;
req . chat_id = chat_id ;
req . enabled = enabled ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
processUpdates ( ( TLRPC . Updates ) response , false ) ;
loadFullChat ( chat_id , 0 , true ) ;
}
}
} ) ;
}
public void toggleUserAdmin ( final int chat_id , int user_id , boolean admin ) {
TLRPC . TL_messages_editChatAdmin req = new TLRPC . TL_messages_editChatAdmin ( ) ;
req . chat_id = chat_id ;
req . user_id = getInputUser ( user_id ) ;
req . is_admin = admin ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
}
} ) ;
}
2015-09-24 22:52:02 +02:00
public void addUserToChat ( final int chat_id , final TLRPC . User user , final TLRPC . ChatFull info , int count_fwd , String botHash , final BaseFragment fragment ) {
2014-02-04 19:36:55 +01:00
if ( user = = null ) {
return ;
}
2014-07-27 00:28:33 +02:00
if ( chat_id > 0 ) {
2015-06-29 19:12:11 +02:00
TLObject request ;
2015-09-24 22:52:02 +02:00
final boolean isChannel = ChatObject . isChannel ( chat_id ) ;
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 {
2015-11-26 22:04:02 +01:00
if ( user . bot & & ! isMegagroup ) {
TLRPC . TL_channels_editAdmin req = new TLRPC . TL_channels_editAdmin ( ) ;
req . channel = getInputChannel ( chat_id ) ;
2016-03-16 13:26:32 +01:00
req . user_id = getInputUser ( user ) ;
2015-11-26 22:04:02 +01:00
req . role = new TLRPC . TL_channelRoleEditor ( ) ;
request = req ;
} else {
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 ;
}
2014-02-04 19:36:55 +01:00
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2014-07-27 00:28:33 +02:00
@Override
2015-09-24 22:52:02 +02:00
public void run ( TLObject response , final TLRPC . TL_error error ) {
2015-10-29 18:10:07 +01:00
if ( isChannel & & inputUser instanceof TLRPC . TL_inputUserSelf ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
joiningToChannels . remove ( ( Integer ) chat_id ) ;
}
} ) ;
}
2014-07-27 00:28:33 +02:00
if ( error ! = null ) {
2015-09-24 22:52:02 +02:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( fragment ! = null ) {
2015-11-26 22:04:02 +01:00
AlertsCreator . showAddUserAlert ( error . text , fragment , isChannel & & ! isMegagroup ) ;
2015-09-24 22:52:02 +02:00
} else {
if ( error . text . equals ( " PEER_FLOOD " ) ) {
2015-12-09 19:27:52 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . needShowAlert , 1 ) ;
2015-09-24 22:52:02 +02:00
}
}
}
} ) ;
2014-07-27 00:28:33 +02:00
return ;
}
2016-03-16 13:26:32 +01:00
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 ;
}
}
}
processUpdates ( updates , false ) ;
2015-09-24 22:52:02 +02:00
if ( isChannel ) {
2016-03-16 13:26:32 +01:00
if ( ! hasJoinMessage & & inputUser instanceof TLRPC . TL_inputUserSelf ) {
2015-12-09 19:27:52 +01:00
generateJoinMessage ( chat_id , true ) ;
2015-09-24 22:52:02 +02:00
}
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
loadFullChat ( chat_id , 0 , true ) ;
}
} , 1000 ) ;
}
if ( isChannel & & inputUser instanceof TLRPC . TL_inputUserSelf ) {
MessagesStorage . getInstance ( ) . updateDialogsWithDeletedMessages ( new ArrayList < Integer > ( ) , true , chat_id ) ;
}
2014-02-04 19:36:55 +01:00
}
2014-07-27 00:28:33 +02:00
} ) ;
} 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 ) {
2014-07-27 00:28:33 +02:00
return ;
}
}
2014-08-22 16:24:33 +02:00
TLRPC . Chat chat = getChat ( chat_id ) ;
2014-07-27 00:28:33 +02:00
chat . participants_count + + ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . Chat > chatArrayList = new ArrayList < > ( ) ;
2014-07-27 00:28:33 +02:00
chatArrayList . add ( chat ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( null , chatArrayList , true , true ) ;
TLRPC . TL_chatParticipant newPart = new TLRPC . TL_chatParticipant ( ) ;
newPart . user_id = user . id ;
newPart . inviter_id = UserConfig . getClientUserId ( ) ;
newPart . date = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
2015-09-24 22:52:02 +02:00
info . participants . participants . add ( 0 , newPart ) ;
MessagesStorage . getInstance ( ) . updateChatInfo ( info , true ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoDidLoaded , info , 0 , false , null ) ;
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_CHAT_MEMBERS ) ;
2013-10-25 17:19:00 +02:00
}
2014-07-27 00:28:33 +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 ) {
2014-02-04 19:36:55 +01:00
if ( user = = null ) {
return ;
}
2014-07-27 00:28:33 +02:00
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 ) {
2015-11-26 22:04:02 +01:00
if ( chat . creator ) {
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 {
TLRPC . TL_channels_kickFromChannel req = new TLRPC . TL_channels_kickFromChannel ( ) ;
req . channel = getInputChannel ( chat ) ;
req . user_id = inputUser ;
req . kicked = true ;
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 ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2014-07-27 00:28:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2015-09-24 22:52:02 +02:00
if ( user . id = = UserConfig . getClientUserId ( ) ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2016-03-16 13:26:32 +01:00
deleteDialog ( - chat_id , 0 ) ;
2015-09-24 22:52:02 +02:00
}
} ) ;
}
2014-07-27 00:28:33 +02:00
if ( error ! = null ) {
return ;
}
2015-04-09 20:00:14 +02:00
final TLRPC . Updates updates = ( TLRPC . Updates ) response ;
processUpdates ( updates , false ) ;
2015-09-24 22:52:02 +02:00
if ( isChannel & & ! ( inputUser instanceof TLRPC . TL_inputUserSelf ) ) {
2015-04-09 20:00:14 +02:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
loadFullChat ( chat_id , 0 , true ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
} , 1000 ) ;
2015-03-19 00:09:45 +01:00
}
2014-02-04 19:36:55 +01:00
}
2015-11-26 22:04:02 +01:00
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
2014-07-27 00:28:33 +02:00
} else {
2015-09-24 22:52:02 +02:00
if ( info instanceof TLRPC . TL_chatFull ) {
2014-08-22 16:24:33 +02:00
TLRPC . Chat chat = getChat ( chat_id ) ;
2014-07-27 00:28:33 +02:00
chat . participants_count - - ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . Chat > chatArrayList = new ArrayList < > ( ) ;
2014-07-27 00:28:33 +02:00
chatArrayList . add ( chat ) ;
MessagesStorage . getInstance ( ) . 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 ;
2014-02-04 19:36:55 +01:00
}
}
2015-05-21 23:27:27 +02:00
if ( changed ) {
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . updateChatInfo ( info , true ) ;
2016-03-16 13:26:32 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoDidLoaded , info , 0 , false , null ) ;
2015-05-21 23:27:27 +02:00
}
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_CHAT_MEMBERS ) ;
2013-10-25 17:19:00 +02:00
}
2014-07-27 00:28:33 +02:00
}
2013-10-25 17:19:00 +02:00
}
public void changeChatTitle ( int chat_id , String title ) {
2014-07-27 00:28:33 +02:00
if ( chat_id > 0 ) {
2015-09-24 22:52:02 +02:00
TLObject request ;
if ( ChatObject . isChannel ( chat_id ) ) {
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 ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2014-07-27 00:28:33 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error ! = null ) {
return ;
}
2015-04-09 20:00:14 +02:00
processUpdates ( ( TLRPC . Updates ) response , false ) ;
2014-02-04 19:36:55 +01:00
}
2015-09-24 22:52:02 +02:00
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
2014-07-27 00:28:33 +02:00
} else {
2014-08-22 16:24:33 +02:00
TLRPC . Chat chat = getChat ( chat_id ) ;
2014-07-27 00:28:33 +02:00
chat . title = title ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . Chat > chatArrayList = new ArrayList < > ( ) ;
2014-07-27 00:28:33 +02:00
chatArrayList . add ( chat ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( null , chatArrayList , true , true ) ;
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_CHAT_NAME ) ;
2014-07-27 00:28:33 +02:00
}
2013-10-25 17:19:00 +02:00
}
2013-12-20 20:25:49 +01:00
public void changeChatAvatar ( int chat_id , TLRPC . InputFile uploadedAvatar ) {
2015-09-24 22:52:02 +02:00
TLObject request ;
if ( ChatObject . isChannel ( chat_id ) ) {
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 ;
req . photo . crop = new TLRPC . TL_inputPhotoCropAuto ( ) ;
} 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 ;
req . photo . crop = new TLRPC . TL_inputPhotoCropAuto ( ) ;
} else {
req . photo = new TLRPC . TL_inputChatPhotoEmpty ( ) ;
}
request = req ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( request , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error ! = null ) {
return ;
}
2015-04-09 20:00:14 +02:00
processUpdates ( ( TLRPC . Updates ) response , false ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
} , ConnectionsManager . RequestFlagInvokeAfter ) ;
2013-10-25 17:19:00 +02:00
}
public void unregistedPush ( ) {
2014-02-28 23:28:25 +01:00
if ( UserConfig . registeredForPush & & UserConfig . pushString . length ( ) = = 0 ) {
TLRPC . TL_account_unregisterDevice req = new TLRPC . TL_account_unregisterDevice ( ) ;
req . token = UserConfig . pushString ;
req . token_type = 2 ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2014-02-28 23:28:25 +01:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2014-02-04 19:36:55 +01:00
2014-02-28 23:28:25 +01:00
}
2014-07-03 00:39:05 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
2014-06-13 12:42:21 +02:00
}
2014-02-28 23:28:25 +01:00
2015-05-21 23:27:27 +02:00
public void performLogout ( boolean byUser ) {
2016-01-11 18:19:48 +01:00
SharedPreferences . Editor editor = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) . edit ( ) ;
2015-05-21 23:27:27 +02:00
editor . clear ( ) . commit ( ) ;
2016-01-11 18:19:48 +01:00
editor = ApplicationLoader . applicationContext . getSharedPreferences ( " emoji " , Activity . MODE_PRIVATE ) . edit ( ) ;
editor . putLong ( " lastGifLoadTime " , 0 ) . commit ( ) ;
editor = ApplicationLoader . applicationContext . getSharedPreferences ( " mainconfig " , Activity . MODE_PRIVATE ) . edit ( ) ;
editor . remove ( " gifhint " ) . commit ( ) ;
2015-05-21 23:27:27 +02:00
if ( byUser ) {
unregistedPush ( ) ;
TLRPC . TL_auth_logOut req = new TLRPC . TL_auth_logOut ( ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2015-05-21 23:27:27 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
ConnectionsManager . getInstance ( ) . cleanUp ( ) ;
}
} ) ;
} else {
ConnectionsManager . getInstance ( ) . cleanUp ( ) ;
}
UserConfig . clearConfig ( ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . appDidLogout ) ;
MessagesStorage . getInstance ( ) . cleanUp ( false ) ;
cleanUp ( ) ;
ContactsController . getInstance ( ) . deleteAllAppAccounts ( ) ;
2013-10-25 17:19:00 +02:00
}
2015-05-03 13:48:36 +02:00
public void generateUpdateMessage ( ) {
2016-03-06 02:49:31 +01:00
if ( UserConfig . lastUpdateVersion = = null | | UserConfig . lastUpdateVersion . equals ( BuildVars . BUILD_VERSION_STRING ) ) {
return ;
}
TLRPC . TL_help_getAppChangelog req = new TLRPC . TL_help_getAppChangelog ( ) ;
req . app_version = BuildVars . BUILD_VERSION_STRING ;
try {
req . lang_code = LocaleController . getLocaleString ( LocaleController . getInstance ( ) . getSystemDefaultLocale ( ) ) ;
req . device_model = Build . MANUFACTURER + Build . MODEL ;
req . system_version = " SDK " + Build . VERSION . SDK_INT ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
if ( req . lang_code = = null | | req . lang_code . trim ( ) . length ( ) = = 0 ) {
req . lang_code = " en " ;
}
if ( req . device_model = = null | | req . device_model . trim ( ) . length ( ) = = 0 ) {
req . device_model = " Android unknown " ;
}
if ( req . system_version = = null | | req . system_version . trim ( ) . length ( ) = = 0 ) {
req . system_version = " SDK Unknown " ;
}
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2015-05-03 13:48:36 +02:00
@Override
2016-03-06 02:49:31 +01:00
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
UserConfig . lastUpdateVersion = BuildVars . BUILD_VERSION_STRING ;
UserConfig . saveConfig ( false ) ;
}
if ( response instanceof TLRPC . TL_help_appChangelog ) {
2015-05-03 13:48:36 +02:00
TLRPC . TL_updateServiceNotification update = new TLRPC . TL_updateServiceNotification ( ) ;
2016-03-06 02:49:31 +01:00
update . message = ( ( TLRPC . TL_help_appChangelog ) response ) . text ;
2015-05-03 13:48:36 +02:00
update . media = new TLRPC . TL_messageMediaEmpty ( ) ;
update . type = " update " ;
update . popup = false ;
ArrayList < TLRPC . Update > updates = new ArrayList < > ( ) ;
updates . add ( update ) ;
processUpdateArray ( updates , null , null ) ;
}
}
} ) ;
}
2013-10-25 17:19:00 +02:00
public void registerForPush ( final String regid ) {
2014-06-13 12:42:21 +02:00
if ( regid = = null | | regid . length ( ) = = 0 | | registeringForPush | | UserConfig . getClientUserId ( ) = = 0 ) {
2013-10-25 17:19:00 +02:00
return ;
}
if ( UserConfig . registeredForPush & & regid . equals ( UserConfig . pushString ) ) {
return ;
}
registeringForPush = true ;
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 ;
req . app_sandbox = false ;
2013-10-25 17:19:00 +02:00
try {
2015-05-03 13:48:36 +02:00
req . lang_code = LocaleController . getLocaleString ( LocaleController . getInstance ( ) . getSystemDefaultLocale ( ) ) ;
2015-05-21 23:27:27 +02:00
if ( req . lang_code . length ( ) = = 0 ) {
2015-05-03 13:48:36 +02:00
req . lang_code = " en " ;
}
2013-10-25 17:19:00 +02:00
req . device_model = Build . MANUFACTURER + Build . MODEL ;
req . system_version = " SDK " + Build . VERSION . SDK_INT ;
2013-11-04 13:31:01 +01:00
PackageInfo pInfo = ApplicationLoader . applicationContext . getPackageManager ( ) . getPackageInfo ( ApplicationLoader . applicationContext . getPackageName ( ) , 0 ) ;
2014-06-22 11:36:52 +02:00
req . app_version = pInfo . versionName + " ( " + pInfo . versionCode + " ) " ;
2013-10-25 17:19:00 +02:00
} catch ( Exception e ) {
2013-12-20 20:25:49 +01:00
FileLog . e ( " tmessages " , e ) ;
req . lang_code = " en " ;
req . device_model = " Android unknown " ;
req . system_version = " SDK " + Build . VERSION . SDK_INT ;
req . app_version = " App version unknown " ;
2013-10-25 17:19:00 +02:00
}
2013-12-26 17:46:13 +01:00
2015-12-09 19:27:52 +01:00
if ( req . lang_code = = null | | req . lang_code . trim ( ) . length ( ) = = 0 ) {
2013-12-26 17:46:13 +01:00
req . lang_code = " en " ;
}
2015-12-09 19:27:52 +01:00
if ( req . device_model = = null | | req . device_model . trim ( ) . length ( ) = = 0 ) {
2013-12-26 17:46:13 +01:00
req . device_model = " Android unknown " ;
}
2015-12-09 19:27:52 +01:00
if ( req . app_version = = null | | req . app_version . trim ( ) . length ( ) = = 0 ) {
2013-12-26 17:46:13 +01:00
req . app_version = " App version unknown " ;
}
2015-12-09 19:27:52 +01:00
if ( req . system_version = = null | | req . system_version . trim ( ) . length ( ) = = 0 ) {
2013-12-26 17:46:13 +01:00
req . system_version = " SDK Unknown " ;
}
2013-10-25 17:19:00 +02:00
if ( req . app_version ! = null ) {
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
2016-01-11 18:19:48 +01:00
if ( response instanceof TLRPC . TL_boolTrue ) {
2013-12-20 20:25:49 +01:00
FileLog . e ( " tmessages " , " registered for push " ) ;
2013-10-25 17:19:00 +02:00
UserConfig . registeredForPush = true ;
UserConfig . pushString = regid ;
2013-11-04 13:31:01 +01:00
UserConfig . saveConfig ( false ) ;
2013-10-25 17:19:00 +02:00
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
registeringForPush = false ;
}
} ) ;
}
2014-07-03 00:39:05 +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 ( ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
updatingState = false ;
if ( error = = null ) {
2014-03-22 23:31:55 +01:00
TLRPC . TL_updates_state res = ( TLRPC . TL_updates_state ) response ;
2013-11-04 13:31:01 +01:00
MessagesStorage . lastDateValue = res . date ;
MessagesStorage . lastPtsValue = res . pts ;
MessagesStorage . lastSeqValue = res . seq ;
MessagesStorage . lastQtsValue = res . qts ;
2015-03-19 00:09:45 +01:00
for ( int a = 0 ; a < 3 ; a + + ) {
processUpdatesQueue ( a , 2 ) ;
}
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . saveDiffParams ( MessagesStorage . lastSeqValue , MessagesStorage . lastPtsValue , MessagesStorage . lastDateValue , MessagesStorage . lastQtsValue ) ;
2013-10-25 17:19:00 +02:00
} else {
2014-02-04 19:36:55 +01:00
if ( error . code ! = 401 ) {
loadCurrentState ( ) ;
}
2013-10-25 17:19:00 +02:00
}
}
2014-07-03 00:39:05 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
2014-02-04 19:36:55 +01:00
private int getUpdateSeq ( TLRPC . Updates updates ) {
if ( updates instanceof TLRPC . TL_updatesCombined ) {
return updates . seq_start ;
} else {
return updates . seq ;
}
}
2015-03-19 00:09:45 +01:00
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 ) ;
if ( MessagesStorage . lastSeqValue + 1 = = seq | | MessagesStorage . lastSeqValue = = seq ) {
return 0 ;
} else if ( MessagesStorage . lastSeqValue < seq ) {
return 1 ;
} else {
return 2 ;
}
} else if ( type = = 1 ) {
if ( updates . pts < = MessagesStorage . lastPtsValue ) {
return 2 ;
2015-05-21 23:27:27 +02:00
} else if ( MessagesStorage . lastPtsValue + updates . pts_count = = updates . pts ) {
2015-03-19 00:09:45 +01:00
return 0 ;
} else {
return 1 ;
}
} else if ( type = = 2 ) {
2015-05-21 23:27:27 +02:00
if ( updates . pts < = MessagesStorage . lastQtsValue ) {
2015-03-19 00:09:45 +01:00
return 2 ;
2015-05-21 23:27:27 +02:00
} else if ( MessagesStorage . lastQtsValue + updates . updates . size ( ) = = updates . pts ) {
2015-03-19 00:09:45 +01:00
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 ;
}
Integer channelPts = channelsPts . get ( channelId ) ;
if ( updatesQueue . isEmpty ( ) | | channelPts = = null ) {
updatesQueueChannels . remove ( channelId ) ;
2015-10-29 18:10:07 +01:00
return ;
2015-09-24 22:52:02 +02:00
}
Collections . sort ( updatesQueue , new Comparator < TLRPC . Updates > ( ) {
@Override
public int compare ( TLRPC . Updates updates , TLRPC . Updates updates2 ) {
return AndroidUtilities . compare ( updates . pts , updates2 . pts ) ;
}
} ) ;
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 ) {
Long updatesStartWaitTime = updatesStartWaitTimeChannels . get ( channelId ) ;
2015-11-26 22:04:02 +01:00
if ( updatesStartWaitTime ! = null & & ( anyProceed | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTime ) < = 1500 ) ) {
2015-09-24 22:52:02 +02:00
FileLog . e ( " tmessages " , " HOLE IN CHANNEL " + channelId + " UPDATES QUEUE - will wait more time " ) ;
if ( anyProceed ) {
updatesStartWaitTimeChannels . put ( channelId , System . currentTimeMillis ( ) ) ;
}
return ;
} else {
FileLog . e ( " tmessages " , " HOLE IN CHANNEL " + channelId + " UPDATES QUEUE - getChannelDifference " ) ;
updatesStartWaitTimeChannels . remove ( channelId ) ;
updatesQueueChannels . remove ( channelId ) ;
getChannelDifference ( channelId ) ;
return ;
}
} else {
updatesQueue . remove ( a ) ;
a - - ;
}
}
updatesQueueChannels . remove ( channelId ) ;
updatesStartWaitTimeChannels . remove ( channelId ) ;
FileLog . e ( " tmessages " , " UPDATES CHANNEL " + channelId + " QUEUE PROCEED - OK " ) ;
}
private void processUpdatesQueue ( int type , int state ) {
2015-03-19 00:09:45 +01:00
ArrayList < TLRPC . Updates > updatesQueue = null ;
if ( type = = 0 ) {
updatesQueue = updatesQueueSeq ;
2014-02-04 19:36:55 +01:00
Collections . sort ( updatesQueue , new Comparator < TLRPC . Updates > ( ) {
@Override
public int compare ( TLRPC . Updates updates , TLRPC . Updates updates2 ) {
2015-05-03 13:48:36 +02:00
return AndroidUtilities . compare ( getUpdateSeq ( updates ) , getUpdateSeq ( updates2 ) ) ;
2014-02-04 19:36:55 +01:00
}
} ) ;
2015-03-19 00:09:45 +01:00
} else if ( type = = 1 ) {
updatesQueue = updatesQueuePts ;
Collections . sort ( updatesQueue , new Comparator < TLRPC . Updates > ( ) {
@Override
public int compare ( TLRPC . Updates updates , TLRPC . Updates updates2 ) {
2015-05-03 13:48:36 +02:00
return AndroidUtilities . compare ( updates . pts , updates2 . pts ) ;
2015-03-19 00:09:45 +01:00
}
} ) ;
} else if ( type = = 2 ) {
updatesQueue = updatesQueueQts ;
Collections . sort ( updatesQueue , new Comparator < TLRPC . Updates > ( ) {
@Override
public int compare ( TLRPC . Updates updates , TLRPC . Updates updates2 ) {
2015-05-21 23:27:27 +02:00
return AndroidUtilities . compare ( updates . pts , updates2 . pts ) ;
2015-03-19 00:09:45 +01:00
}
} ) ;
}
2015-05-21 23:27:27 +02:00
if ( updatesQueue ! = null & & ! updatesQueue . isEmpty ( ) ) {
2014-02-04 19:36:55 +01:00
boolean anyProceed = false ;
2014-07-23 01:27:00 +02:00
if ( state = = 2 ) {
TLRPC . Updates updates = updatesQueue . get ( 0 ) ;
2015-03-19 00:09:45 +01:00
if ( type = = 0 ) {
MessagesStorage . lastSeqValue = getUpdateSeq ( updates ) ;
} else if ( type = = 1 ) {
MessagesStorage . lastPtsValue = updates . pts ;
2015-05-21 23:27:27 +02:00
} else {
MessagesStorage . lastQtsValue = updates . pts ;
2015-03-19 00:09:45 +01:00
}
2014-07-23 01:27:00 +02:00
}
2014-02-04 19:36:55 +01:00
for ( int a = 0 ; a < updatesQueue . size ( ) ; a + + ) {
TLRPC . Updates updates = updatesQueue . get ( a ) ;
2015-03-19 00:09:45 +01:00
int updateState = isValidUpdate ( updates , type ) ;
if ( updateState = = 0 ) {
2014-02-04 19:36:55 +01:00
processUpdates ( updates , true ) ;
anyProceed = true ;
updatesQueue . remove ( a ) ;
a - - ;
2015-03-19 00:09:45 +01:00
} else if ( updateState = = 1 ) {
2015-11-26 22:04:02 +01:00
if ( getUpdatesStartTime ( type ) ! = 0 & & ( anyProceed | | Math . abs ( System . currentTimeMillis ( ) - getUpdatesStartTime ( type ) ) < = 1500 ) ) {
2014-02-04 19:36:55 +01:00
FileLog . e ( " tmessages " , " HOLE IN UPDATES QUEUE - will wait more time " ) ;
if ( anyProceed ) {
2015-03-19 00:09:45 +01:00
setUpdatesStartTime ( type , System . currentTimeMillis ( ) ) ;
2014-02-04 19:36:55 +01:00
}
2015-09-24 22:52:02 +02:00
return ;
} else {
FileLog . e ( " tmessages " , " HOLE IN UPDATES QUEUE - getDifference " ) ;
setUpdatesStartTime ( type , 0 ) ;
updatesQueue . clear ( ) ;
getDifference ( ) ;
return ;
}
} else {
updatesQueue . remove ( a ) ;
a - - ;
}
}
updatesQueue . clear ( ) ;
FileLog . e ( " tmessages " , " UPDATES QUEUE PROCEED - OK " ) ;
}
setUpdatesStartTime ( type , 0 ) ;
}
public void startShortPoll ( final int channelId , final boolean stop ) {
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
if ( stop ) {
needShortPollChannels . delete ( channelId ) ;
} else {
needShortPollChannels . put ( channelId , 0 ) ;
if ( shortPollChannels . indexOfKey ( channelId ) < 0 ) {
2015-12-09 19:27:52 +01:00
getChannelDifference ( channelId , 2 ) ;
2015-09-24 22:52:02 +02:00
}
}
}
} ) ;
}
public void getChannelDifference ( final int channelId ) {
2015-12-09 19:27:52 +01:00
getChannelDifference ( channelId , 0 ) ;
2015-09-24 22:52:02 +02:00
}
2015-12-09 19:27:52 +01:00
public void getChannelDifference ( final int channelId , final int newDialogType ) {
2015-09-24 22:52:02 +02:00
Boolean gettingDifferenceChannel = gettingDifferenceChannels . get ( channelId ) ;
if ( gettingDifferenceChannel = = null ) {
gettingDifferenceChannel = false ;
}
2015-12-09 19:27:52 +01:00
if ( gettingDifferenceChannel ) {
return ;
}
int limit = 100 ;
2015-09-24 22:52:02 +02:00
Integer channelPts ;
2015-12-09 19:27:52 +01:00
if ( newDialogType = = 1 ) {
channelPts = channelsPts . get ( channelId ) ;
if ( channelPts ! = null ) {
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 ) ;
if ( channelPts = = null ) {
channelPts = MessagesStorage . getInstance ( ) . getChannelPtsSync ( channelId ) ;
if ( channelPts ! = 0 ) {
channelsPts . put ( channelId , channelPts ) ;
}
2015-12-09 19:27:52 +01:00
if ( channelPts = = 0 & & newDialogType = = 2 ) {
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 ;
}
}
gettingDifferenceChannels . put ( channelId , true ) ;
TLRPC . TL_updates_getChannelDifference req = new TLRPC . TL_updates_getChannelDifference ( ) ;
req . channel = getInputChannel ( channelId ) ;
req . filter = new TLRPC . TL_channelMessagesFilterEmpty ( ) ;
req . pts = channelPts ;
2015-12-09 19:27:52 +01:00
req . limit = limit ;
2015-09-24 22:52:02 +02:00
FileLog . e ( " tmessages " , " start getChannelDifference with pts = " + channelPts + " channelId = " + channelId ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
2016-03-16 13:26:32 +01:00
public void run ( TLObject response , final TLRPC . TL_error error ) {
2015-09-24 22:52:02 +02:00
if ( error = = null ) {
final TLRPC . updates_ChannelDifference res = ( TLRPC . updates_ChannelDifference ) response ;
final HashMap < Integer , TLRPC . User > usersDict = new HashMap < > ( ) ;
2015-11-26 22:04:02 +01:00
for ( int a = 0 ; a < res . users . size ( ) ; a + + ) {
TLRPC . User user = res . users . get ( a ) ;
2015-09-24 22:52:02 +02:00
usersDict . put ( user . id , user ) ;
}
2015-11-26 22:04:02 +01:00
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 ;
}
}
final TLRPC . Chat channelFinal = channel ;
2015-09-24 22:52:02 +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 - - ;
}
}
}
2016-03-16 13:26:32 +01:00
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , res . chats , true , true ) ;
2015-09-24 22:52:02 +02:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
putUsers ( res . users , false ) ;
putChats ( res . chats , false ) ;
}
} ) ;
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
if ( ! msgUpdates . isEmpty ( ) ) {
final HashMap < Integer , long [ ] > corrected = new HashMap < > ( ) ;
for ( TLRPC . TL_updateMessageID update : msgUpdates ) {
long [ ] ids = MessagesStorage . getInstance ( ) . updateMessageStateAndId ( update . random_id , null , update . id , 0 , false , channelId ) ;
if ( ids ! = null ) {
corrected . put ( update . id , ids ) ;
}
}
if ( ! corrected . isEmpty ( ) ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
for ( HashMap . Entry < Integer , long [ ] > entry : corrected . entrySet ( ) ) {
Integer newId = entry . getKey ( ) ;
long [ ] ids = entry . getValue ( ) ;
Integer oldId = ( int ) ids [ 1 ] ;
SendMessagesHelper . getInstance ( ) . processSentMessage ( oldId ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messageReceivedByServer , oldId , newId , null , ids [ 0 ] ) ;
}
}
} ) ;
}
}
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
if ( res instanceof TLRPC . TL_updates_channelDifference | | res instanceof TLRPC . TL_updates_channelDifferenceEmpty ) {
if ( ! res . new_messages . isEmpty ( ) ) {
final HashMap < Long , ArrayList < MessageObject > > messages = new HashMap < > ( ) ;
ImageLoader . saveMessagesThumbs ( res . new_messages ) ;
final ArrayList < MessageObject > pushMessages = new ArrayList < > ( ) ;
for ( int a = 0 ; a < res . new_messages . size ( ) ; a + + ) {
TLRPC . Message message = res . new_messages . get ( a ) ;
2015-11-26 22:04:02 +01:00
if ( ! message . out ) {
message . unread = true ;
2016-03-06 02:49:31 +01:00
if ( message . post | | channelFinal ! = null & & channelFinal . megagroup ) {
2015-11-26 22:04:02 +01:00
message . media_unread = true ;
2015-09-24 22:52:02 +02:00
}
}
2016-03-06 02:49:31 +01:00
if ( message . action instanceof TLRPC . TL_messageActionChannelCreate ) {
message . unread = false ;
message . media_unread = false ;
}
2015-11-26 22:04:02 +01:00
if ( channelFinal ! = null & & channelFinal . megagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
}
long dialog_id = - channelId ;
Integer value = dialogs_read_inbox_max . get ( dialog_id ) ;
if ( value = = null ) {
value = MessagesStorage . getInstance ( ) . getChannelReadInboxMax ( channelId ) ;
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( message , usersDict , createdDialogIds . contains ( dialog_id ) ) ;
2015-11-26 22:04:02 +01:00
if ( channelFinal ! = null & & channelFinal . left | | value > = obj . getId ( ) ) {
2015-10-29 18:10:07 +01:00
obj . setIsRead ( ) ;
obj . setContentIsRead ( ) ;
}
2015-09-24 22:52:02 +02:00
2015-10-29 18:10:07 +01:00
if ( ! obj . isOut ( ) & & obj . isContentUnread ( ) ) {
2015-09-24 22:52:02 +02:00
pushMessages . add ( obj ) ;
}
long uid = - channelId ;
ArrayList < MessageObject > arr = messages . get ( uid ) ;
if ( arr = = null ) {
arr = new ArrayList < > ( ) ;
messages . put ( uid , arr ) ;
}
arr . add ( obj ) ;
}
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
for ( HashMap . Entry < Long , ArrayList < MessageObject > > pair : messages . entrySet ( ) ) {
Long key = pair . getKey ( ) ;
ArrayList < MessageObject > value = pair . getValue ( ) ;
updateInterfaceWithMessages ( key , value ) ;
}
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
}
} ) ;
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
if ( ! pushMessages . isEmpty ( ) ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationsController . getInstance ( ) . processNewMessages ( pushMessages , true ) ;
}
} ) ;
}
2016-03-16 13:26:32 +01:00
MessagesStorage . getInstance ( ) . putMessages ( res . new_messages , true , false , false , MediaController . getInstance ( ) . getAutodownloadMask ( ) ) ;
2015-09-24 22:52:02 +02:00
}
} ) ;
}
if ( ! res . other_updates . isEmpty ( ) ) {
processUpdateArray ( res . other_updates , res . users , res . chats ) ;
}
processChannelsUpdatesQueue ( channelId , 1 ) ;
MessagesStorage . getInstance ( ) . saveChannelPts ( channelId , res . pts ) ;
} else if ( res instanceof TLRPC . TL_updates_channelDifferenceTooLong ) {
for ( int a = 0 ; a < res . messages . size ( ) ; a + + ) {
2015-11-26 22:04:02 +01:00
TLRPC . Message message = res . messages . get ( a ) ;
message . dialog_id = - channelId ;
if ( ! message . out ) {
message . unread = true ;
2016-03-06 02:49:31 +01:00
if ( message . post | | channelFinal ! = null & & channelFinal . megagroup ) {
2015-11-26 22:04:02 +01:00
message . media_unread = true ;
}
}
2016-03-06 02:49:31 +01:00
if ( message . action instanceof TLRPC . TL_messageActionChannelCreate ) {
message . unread = false ;
message . media_unread = false ;
}
2015-11-26 22:04:02 +01:00
if ( channelFinal ! = null & & channelFinal . megagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
}
if ( channelFinal ! = null & & channelFinal . left | | res . read_inbox_max_id > = message . id ) {
message . unread = false ;
message . media_unread = false ;
}
if ( channelFinal ! = null & & channelFinal . megagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
}
2015-09-24 22:52:02 +02:00
}
2016-03-06 02:49:31 +01:00
if ( channelFinal ! = null & & channelFinal . megagroup ) {
2015-11-26 22:04:02 +01:00
res . unread_important_count = Math . max ( res . unread_count , res . unread_important_count ) ;
res . top_important_message = Math . max ( res . top_important_message , res . top_message ) ;
}
2015-12-09 19:27:52 +01:00
MessagesStorage . getInstance ( ) . overwriteChannel ( channelId , ( TLRPC . TL_updates_channelDifferenceTooLong ) res , newDialogType ) ;
2015-09-24 22:52:02 +02:00
}
gettingDifferenceChannels . remove ( channelId ) ;
channelsPts . put ( channelId , res . pts ) ;
if ( ( res . flags & 2 ) ! = 0 ) {
shortPollChannels . put ( channelId , ( int ) ( System . currentTimeMillis ( ) / 1000 ) + res . timeout ) ;
}
2015-11-26 22:04:02 +01:00
if ( ! res . isFinal ) {
2015-09-24 22:52:02 +02:00
getChannelDifference ( channelId ) ;
}
FileLog . e ( " tmessages " , " received channel difference with pts = " + res . pts + " channelId = " + channelId ) ;
FileLog . e ( " tmessages " , " messages = " + res . new_messages . size ( ) + " users = " + res . users . size ( ) + " chats = " + res . chats . size ( ) + " other updates = " + res . other_updates . size ( ) ) ;
}
} ) ;
}
} ) ;
2014-02-04 19:36:55 +01:00
} else {
2016-03-16 13:26:32 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
checkChannelError ( error . text , channelId ) ;
}
} ) ;
2015-09-24 22:52:02 +02:00
gettingDifferenceChannels . remove ( channelId ) ;
2014-02-04 19:36:55 +01:00
}
}
2015-09-24 22:52:02 +02:00
} ) ;
2014-02-04 19:36:55 +01:00
}
2016-03-16 13:26:32 +01:00
private void checkChannelError ( String text , int channelId ) {
switch ( text ) {
case " CHANNEL_PRIVATE " :
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoCantLoad , channelId , 0 ) ;
break ;
case " CHANNEL_PUBLIC_GROUP_NA " :
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoCantLoad , channelId , 1 ) ;
break ;
case " USER_BANNED_IN_CHANNEL " :
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . chatInfoCantLoad , channelId , 2 ) ;
break ;
}
}
2013-10-25 17:19:00 +02:00
public void getDifference ( ) {
2015-09-24 22:52:02 +02:00
getDifference ( MessagesStorage . lastPtsValue , MessagesStorage . lastDateValue , MessagesStorage . lastQtsValue , false ) ;
}
public void getDifference ( int pts , int date , int qts , boolean slice ) {
2013-10-25 17:19:00 +02:00
registerForPush ( UserConfig . pushString ) ;
2015-03-26 18:34:47 +01:00
if ( MessagesStorage . lastPtsValue = = 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 ;
}
if ( ! firstGettingTask ) {
getNewDeleteTask ( null ) ;
firstGettingTask = true ;
}
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 ;
2015-04-09 20:00:14 +02:00
if ( req . date = = 0 ) {
req . date = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
}
2014-02-04 19:36:55 +01:00
FileLog . e ( " tmessages " , " start getDifference with date = " + MessagesStorage . lastDateValue + " pts = " + MessagesStorage . lastPtsValue + " seq = " + MessagesStorage . lastSeqValue ) ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . setIsUpdating ( true ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
if ( error = = null ) {
2014-03-22 23:31:55 +01:00
final TLRPC . updates_Difference res = ( TLRPC . updates_Difference ) response ;
2015-09-24 22:52:02 +02:00
if ( res instanceof TLRPC . TL_updates_differenceSlice ) {
getDifference ( res . intermediate_state . pts , res . intermediate_state . date , res . intermediate_state . qts , true ) ;
}
2013-10-25 17:19:00 +02:00
2015-01-02 23:15:07 +01:00
final HashMap < Integer , TLRPC . User > usersDict = new HashMap < > ( ) ;
2015-11-26 22:04:02 +01:00
final HashMap < Integer , TLRPC . Chat > chatsDict = new HashMap < > ( ) ;
for ( int a = 0 ; a < res . users . size ( ) ; a + + ) {
TLRPC . User user = res . users . get ( a ) ;
2013-10-25 17:19:00 +02:00
usersDict . put ( user . id , user ) ;
}
2015-11-26 22:04:02 +01:00
for ( int a = 0 ; a < res . chats . size ( ) ; a + + ) {
TLRPC . Chat chat = res . chats . get ( a ) ;
chatsDict . put ( chat . id , chat ) ;
}
2013-10-25 17:19:00 +02:00
2015-01-02 23:15:07 +01:00
final ArrayList < TLRPC . TL_updateMessageID > msgUpdates = new ArrayList < > ( ) ;
2013-10-25 17:19:00 +02:00
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 ) {
2014-03-22 23:31:55 +01:00
msgUpdates . add ( ( TLRPC . TL_updateMessageID ) upd ) ;
2013-10-25 17:19:00 +02:00
res . other_updates . remove ( a ) ;
a - - ;
}
}
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-02-11 15:32:09 +01:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
putUsers ( res . users , false ) ;
putChats ( res . chats , false ) ;
2014-02-11 15:32:09 +01:00
}
} ) ;
2014-10-31 20:02:29 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
if ( ! msgUpdates . isEmpty ( ) ) {
2015-09-24 22:52:02 +02:00
final HashMap < Integer , long [ ] > corrected = new HashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
for ( TLRPC . TL_updateMessageID update : msgUpdates ) {
2015-09-24 22:52:02 +02:00
long [ ] ids = MessagesStorage . getInstance ( ) . updateMessageStateAndId ( update . random_id , null , update . id , 0 , false , 0 ) ;
if ( ids ! = null ) {
corrected . put ( update . id , ids ) ;
2013-10-25 17:19:00 +02:00
}
}
if ( ! corrected . isEmpty ( ) ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
2015-09-24 22:52:02 +02:00
for ( HashMap . Entry < Integer , long [ ] > entry : corrected . entrySet ( ) ) {
Integer newId = entry . getKey ( ) ;
long [ ] ids = entry . getValue ( ) ;
Integer oldId = ( int ) ids [ 1 ] ;
2014-09-28 15:37:26 +02:00
SendMessagesHelper . getInstance ( ) . processSentMessage ( oldId ) ;
2015-09-24 22:52:02 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messageReceivedByServer , oldId , newId , null , ids [ 0 ] ) ;
2013-10-25 17:19:00 +02:00
}
}
} ) ;
}
}
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
if ( ! res . new_messages . isEmpty ( ) | | ! res . new_encrypted_messages . isEmpty ( ) ) {
2015-01-02 23:15:07 +01:00
final HashMap < Long , ArrayList < MessageObject > > messages = new HashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
for ( TLRPC . EncryptedMessage encryptedMessage : res . new_encrypted_messages ) {
2014-12-01 18:56:31 +01:00
ArrayList < TLRPC . Message > decryptedMessages = SecretChatHelper . getInstance ( ) . decryptMessage ( encryptedMessage ) ;
2014-10-28 22:27:44 +01:00
if ( decryptedMessages ! = null & & ! decryptedMessages . isEmpty ( ) ) {
for ( TLRPC . Message message : decryptedMessages ) {
res . new_messages . add ( message ) ;
}
2013-10-25 17:19:00 +02:00
}
}
2015-02-01 19:51:02 +01:00
ImageLoader . saveMessagesThumbs ( res . new_messages ) ;
2015-01-02 23:15:07 +01:00
final ArrayList < MessageObject > pushMessages = new ArrayList < > ( ) ;
2015-06-29 19:12:11 +02:00
for ( int a = 0 ; a < res . new_messages . size ( ) ; a + + ) {
TLRPC . Message message = res . new_messages . get ( a ) ;
if ( message . action instanceof TLRPC . TL_messageActionChatDeleteUser ) {
TLRPC . User user = usersDict . get ( message . action . user_id ) ;
2015-11-26 22:04:02 +01:00
if ( user ! = null & & user . bot ) {
2015-06-29 19:12:11 +02:00
message . reply_markup = new TLRPC . TL_replyKeyboardHide ( ) ;
}
2015-11-26 22:04:02 +01:00
} else if ( message . action instanceof TLRPC . TL_messageActionChatMigrateTo | | message . action instanceof TLRPC . TL_messageActionChannelCreate ) {
message . unread = false ;
message . media_unread = false ;
2015-06-29 19:12:11 +02:00
}
2013-10-25 17:19:00 +02:00
long uid ;
if ( message . dialog_id ! = 0 ) {
uid = message . dialog_id ;
} else {
if ( message . to_id . chat_id ! = 0 ) {
uid = - message . to_id . chat_id ;
} else {
2014-06-13 12:42:21 +02:00
if ( message . to_id . user_id = = UserConfig . getClientUserId ( ) ) {
2013-10-25 17:19:00 +02:00
message . to_id . user_id = message . from_id ;
}
uid = message . to_id . user_id ;
}
}
2016-03-06 02:49:31 +01:00
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( message , usersDict , chatsDict , createdDialogIds . contains ( uid ) ) ;
2016-03-06 02:49:31 +01:00
if ( ! obj . isOut ( ) & & obj . isUnread ( ) ) {
pushMessages . add ( obj ) ;
}
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 ) ;
}
arr . add ( obj ) ;
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( ) {
for ( HashMap . Entry < Long , ArrayList < MessageObject > > pair : messages . entrySet ( ) ) {
Long key = pair . getKey ( ) ;
ArrayList < MessageObject > value = pair . getValue ( ) ;
updateInterfaceWithMessages ( key , value ) ;
}
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
2014-10-31 20:02:29 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
2014-02-04 19:36:55 +01:00
@Override
public void run ( ) {
2014-07-30 09:49:39 +02:00
if ( ! pushMessages . isEmpty ( ) ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-07-30 09:49:39 +02:00
@Override
public void run ( ) {
NotificationsController . getInstance ( ) . processNewMessages ( pushMessages , ! ( res instanceof TLRPC . TL_updates_differenceSlice ) ) ;
}
} ) ;
}
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . startTransaction ( false ) ;
2014-08-22 16:24:33 +02:00
MessagesStorage . getInstance ( ) . putMessages ( res . new_messages , false , false , false , MediaController . getInstance ( ) . getAutodownloadMask ( ) ) ;
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , res . chats , false , false ) ;
MessagesStorage . getInstance ( ) . commitTransaction ( false ) ;
2014-02-04 19:36:55 +01:00
}
} ) ;
2015-01-02 23:15:07 +01:00
SecretChatHelper . getInstance ( ) . processPendingEncMessages ( ) ;
2013-10-25 17:19:00 +02:00
}
2015-05-21 23:27:27 +02:00
if ( ! res . other_updates . isEmpty ( ) ) {
2013-12-20 20:25:49 +01:00
processUpdateArray ( res . other_updates , res . users , res . chats ) ;
2013-10-25 17:19:00 +02:00
}
if ( res instanceof TLRPC . TL_updates_difference ) {
2015-09-24 22:52:02 +02:00
gettingDifference = false ;
2013-11-04 13:31:01 +01:00
MessagesStorage . lastSeqValue = res . state . seq ;
MessagesStorage . lastDateValue = res . state . date ;
MessagesStorage . lastPtsValue = res . state . pts ;
MessagesStorage . lastQtsValue = res . state . qts ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . setIsUpdating ( false ) ;
2015-03-19 00:09:45 +01:00
for ( int a = 0 ; a < 3 ; a + + ) {
2015-09-24 22:52:02 +02:00
processUpdatesQueue ( a , 1 ) ;
2015-03-19 00:09:45 +01:00
}
2013-10-25 17:19:00 +02:00
} else if ( res instanceof TLRPC . TL_updates_differenceSlice ) {
2013-11-04 13:31:01 +01:00
MessagesStorage . lastDateValue = res . intermediate_state . date ;
MessagesStorage . lastPtsValue = res . intermediate_state . pts ;
MessagesStorage . lastQtsValue = res . intermediate_state . qts ;
2013-10-25 17:19:00 +02:00
} else if ( res instanceof TLRPC . TL_updates_differenceEmpty ) {
2015-09-24 22:52:02 +02:00
gettingDifference = false ;
2013-11-04 13:31:01 +01:00
MessagesStorage . lastSeqValue = res . seq ;
MessagesStorage . lastDateValue = res . date ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . setIsUpdating ( false ) ;
2015-03-19 00:09:45 +01:00
for ( int a = 0 ; a < 3 ; a + + ) {
2015-09-24 22:52:02 +02:00
processUpdatesQueue ( a , 1 ) ;
2015-03-19 00:09:45 +01:00
}
2013-10-25 17:19:00 +02:00
}
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . saveDiffParams ( MessagesStorage . lastSeqValue , MessagesStorage . lastPtsValue , MessagesStorage . lastDateValue , MessagesStorage . lastQtsValue ) ;
2014-02-04 19:36:55 +01:00
FileLog . e ( " tmessages " , " received difference with date = " + MessagesStorage . lastDateValue + " pts = " + MessagesStorage . lastPtsValue + " seq = " + MessagesStorage . lastSeqValue ) ;
FileLog . e ( " tmessages " , " messages = " + res . new_messages . size ( ) + " users = " + res . users . size ( ) + " chats = " + res . chats . size ( ) + " other updates = " + res . other_updates . size ( ) ) ;
2013-10-25 17:19:00 +02:00
}
} ) ;
}
} ) ;
} else {
2014-02-04 19:36:55 +01:00
gettingDifference = false ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . setIsUpdating ( false ) ;
}
}
} ) ;
}
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 ) ;
2016-03-16 13:26:32 +01:00
if ( chat = = null | | ! ChatObject . isChannel ( chat_id ) | | ( 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 ;
message . local_id = message . id = UserConfig . getNewMessageId ( ) ;
message . date = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
2016-03-16 13:26:32 +01:00
message . from_id = UserConfig . 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 ( ) ;
2015-11-26 22:04:02 +01:00
message . action . users . add ( UserConfig . getClientUserId ( ) ) ;
2016-03-16 13:26:32 +01:00
if ( chat . megagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
}
2015-09-24 22:52:02 +02:00
UserConfig . saveConfig ( false ) ;
final ArrayList < MessageObject > pushMessages = new ArrayList < > ( ) ;
final ArrayList < TLRPC . Message > messagesArr = new ArrayList < > ( ) ;
messagesArr . add ( message ) ;
MessageObject obj = new MessageObject ( message , null , true ) ;
pushMessages . add ( obj ) ;
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationsController . getInstance ( ) . processNewMessages ( pushMessages , true ) ;
}
} ) ;
}
} ) ;
MessagesStorage . getInstance ( ) . putMessages ( messagesArr , true , true , false , MediaController . getInstance ( ) . getAutodownloadMask ( ) ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
updateInterfaceWithMessages ( - chat_id , pushMessages ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
}
} ) ;
}
2015-11-26 22:04:02 +01:00
public void convertGroup ( ) {
}
2015-09-24 22:52:02 +02:00
public void checkChannelInviter ( final int chat_id ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2015-11-26 22:04:02 +01:00
final TLRPC . Chat chat = getChat ( chat_id ) ;
if ( chat = = null | | ! ChatObject . isChannel ( chat_id ) | | chat . creator ) {
2015-09-24 22:52:02 +02:00
return ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
TLRPC . TL_channels_getParticipant req = new TLRPC . TL_channels_getParticipant ( ) ;
req . channel = getInputChannel ( chat_id ) ;
req . user_id = new TLRPC . TL_inputUserSelf ( ) ;
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
final TLRPC . TL_channels_channelParticipant res = ( TLRPC . TL_channels_channelParticipant ) response ;
if ( res ! = null & & res . participant instanceof TLRPC . TL_channelParticipantSelf & & res . participant . inviter_id ! = UserConfig . getClientUserId ( ) ) {
2015-11-26 22:04:02 +01:00
if ( chat . megagroup & & MessagesStorage . getInstance ( ) . isMigratedChat ( chat . id ) ) {
return ;
}
2015-09-24 22:52:02 +02:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
putUsers ( res . users , false ) ;
}
} ) ;
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , null , true , true ) ;
TLRPC . TL_messageService message = new TLRPC . TL_messageService ( ) ;
2015-11-26 22:04:02 +01:00
message . media_unread = true ;
message . unread = true ;
message . flags = TLRPC . MESSAGE_FLAG_HAS_FROM_ID ;
2016-03-16 13:26:32 +01:00
message . post = true ;
2015-11-26 22:04:02 +01:00
if ( chat . megagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
}
2015-09-24 22:52:02 +02:00
message . local_id = message . id = UserConfig . getNewMessageId ( ) ;
2015-11-26 22:04:02 +01:00
message . date = res . participant . date ;
message . action = new TLRPC . TL_messageActionChatAddUser ( ) ;
2016-03-16 13:26:32 +01:00
message . from_id = res . participant . inviter_id ;
message . action . users . add ( UserConfig . 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 ;
UserConfig . saveConfig ( false ) ;
final ArrayList < MessageObject > pushMessages = new ArrayList < > ( ) ;
final ArrayList < TLRPC . Message > messagesArr = new ArrayList < > ( ) ;
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 ) ;
}
messagesArr . add ( message ) ;
MessageObject obj = new MessageObject ( message , usersDict , true ) ;
pushMessages . add ( obj ) ;
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationsController . getInstance ( ) . processNewMessages ( pushMessages , true ) ;
}
} ) ;
}
} ) ;
MessagesStorage . getInstance ( ) . putMessages ( messagesArr , true , true , false , MediaController . getInstance ( ) . getAutodownloadMask ( ) ) ;
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
updateInterfaceWithMessages ( - chat_id , pushMessages ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
}
} ) ;
}
}
} ) ;
2013-10-25 17:19:00 +02:00
}
2014-07-03 00:39:05 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
2015-05-03 13:48:36 +02:00
private int getUpdateType ( TLRPC . Update update ) {
if ( update instanceof TLRPC . TL_updateNewMessage | | update instanceof TLRPC . TL_updateReadMessagesContents | | update instanceof TLRPC . TL_updateReadHistoryInbox | |
2015-09-24 22:52:02 +02:00
update instanceof TLRPC . TL_updateReadHistoryOutbox | | update instanceof TLRPC . TL_updateDeleteMessages | | update instanceof TLRPC . TL_updateWebPage ) {
2015-05-03 13:48:36 +02:00
return 0 ;
} else if ( update instanceof TLRPC . TL_updateNewEncryptedMessage ) {
return 1 ;
2016-03-06 02:49:31 +01:00
} else if ( update instanceof TLRPC . TL_updateNewChannelMessage | | update instanceof TLRPC . TL_updateDeleteChannelMessages | | update instanceof TLRPC . TL_updateEditChannelMessage ) {
2015-05-03 13:48:36 +02:00
return 2 ;
2015-09-24 22:52:02 +02:00
} else {
return 3 ;
2015-05-03 13:48:36 +02:00
}
}
2014-02-04 19:36:55 +01:00
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 ) ;
processUpdateArray ( arr , null , null ) ;
2015-03-19 00:09:45 +01:00
} else if ( updates instanceof TLRPC . TL_updateShortChatMessage | | updates instanceof TLRPC . TL_updateShortMessage ) {
2015-05-03 13:48:36 +02:00
final int user_id = updates instanceof TLRPC . TL_updateShortChatMessage ? updates . from_id : updates . user_id ;
TLRPC . User user = getUser ( user_id ) ;
2015-03-19 00:09:45 +01:00
TLRPC . User user2 = null ;
2016-01-11 18:19:48 +01:00
TLRPC . User user3 = null ;
2015-09-24 22:52:02 +02:00
TLRPC . Chat channel = null ;
2015-03-19 00:09:45 +01:00
2015-04-09 20:00:14 +02:00
if ( user = = null ) {
2015-05-03 13:48:36 +02:00
user = MessagesStorage . getInstance ( ) . getUserSync ( user_id ) ;
2015-04-09 20:00:14 +02:00
putUser ( user , true ) ;
}
2015-03-19 00:09:45 +01:00
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 ) {
user2 = MessagesStorage . getInstance ( ) . getUserSync ( updates . fwd_from . from_id ) ;
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 ) {
channel = MessagesStorage . getInstance ( ) . getChatSync ( updates . fwd_from . channel_id ) ;
putChat ( channel , true ) ;
}
needFwdUser = true ;
2015-09-24 22:52:02 +02:00
}
2015-03-19 00:09:45 +01:00
}
2016-01-11 18:19:48 +01:00
boolean needBotUser = false ;
if ( updates . via_bot_id ! = 0 ) {
user3 = getUser ( updates . via_bot_id ) ;
if ( user3 = = null ) {
user3 = MessagesStorage . getInstance ( ) . getUserSync ( updates . via_bot_id ) ;
putUser ( user3 , true ) ;
}
needBotUser = true ;
}
2015-05-21 23:27:27 +02:00
boolean missingData ;
2015-03-19 00:09:45 +01:00
if ( updates instanceof TLRPC . TL_updateShortMessage ) {
2016-01-11 18:19:48 +01:00
missingData = user = = null | | needFwdUser & & user2 = = null & & channel = = null | | needBotUser & & user3 = = null ;
2015-03-19 00:09:45 +01:00
} else {
2015-04-09 20:00:14 +02:00
TLRPC . Chat chat = getChat ( updates . chat_id ) ;
if ( chat = = null ) {
chat = MessagesStorage . getInstance ( ) . getChatSync ( updates . chat_id ) ;
putChat ( chat , true ) ;
}
2016-01-11 18:19:48 +01:00
missingData = chat = = null | | user = = null | | needFwdUser & & user2 = = null & & channel = = null | | needBotUser & & user3 = = null ;
2015-03-19 00:09:45 +01:00
}
2014-11-21 01:14:44 +01:00
if ( user ! = null & & user . status ! = null & & user . status . expires < = 0 ) {
onlinePrivacy . put ( user . id , ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) ;
updateStatus = true ;
}
2015-03-19 00:09:45 +01:00
2014-04-03 23:18:54 +02:00
if ( missingData ) {
needGetDiff = true ;
} else {
2015-03-19 00:09:45 +01:00
if ( MessagesStorage . lastPtsValue + updates . pts_count = = updates . pts ) {
2014-04-03 23:18:54 +02:00
TLRPC . TL_message message = new TLRPC . TL_message ( ) ;
message . id = updates . id ;
2015-03-19 00:09:45 +01:00
if ( updates instanceof TLRPC . TL_updateShortMessage ) {
2015-11-26 22:04:02 +01:00
if ( updates . out ) {
2015-03-19 00:09:45 +01:00
message . from_id = UserConfig . getClientUserId ( ) ;
} else {
2015-05-03 13:48:36 +02:00
message . from_id = user_id ;
2015-03-19 00:09:45 +01:00
}
message . to_id = new TLRPC . TL_peerUser ( ) ;
2015-05-03 13:48:36 +02:00
message . to_id . user_id = user_id ;
message . dialog_id = user_id ;
2015-03-19 00:09:45 +01:00
} else {
2015-05-03 13:48:36 +02:00
message . from_id = user_id ;
2015-03-19 00:09:45 +01:00
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 . unread = updates . unread ;
message . mentioned = updates . mentioned ;
message . media_unread = updates . media_unread ;
2015-09-02 00:14:21 +02:00
message . entities = updates . entities ;
2014-04-03 23:18:54 +02:00
message . message = updates . message ;
message . date = updates . date ;
2016-01-11 18:19:48 +01:00
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 ;
2015-03-19 00:09:45 +01:00
message . reply_to_msg_id = updates . reply_to_msg_id ;
2014-04-03 23:18:54 +02:00
message . media = new TLRPC . TL_messageMediaEmpty ( ) ;
MessagesStorage . lastPtsValue = updates . pts ;
2016-03-16 13:26:32 +01:00
final MessageObject obj = new MessageObject ( message , null , createdDialogIds . contains ( message . dialog_id ) ) ;
2015-01-02 23:15:07 +01:00
final ArrayList < MessageObject > objArr = new ArrayList < > ( ) ;
2014-04-03 23:18:54 +02:00
objArr . add ( obj ) ;
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . Message > arr = new ArrayList < > ( ) ;
2014-04-03 23:18:54 +02:00
arr . add ( message ) ;
2015-03-19 00:09:45 +01:00
if ( updates instanceof TLRPC . TL_updateShortMessage ) {
2015-11-26 22:04:02 +01:00
final boolean printUpdate = ! updates . out & & updatePrintingUsersWithNewMessages ( updates . user_id , objArr ) ;
2015-03-19 00:09:45 +01:00
if ( printUpdate ) {
updatePrintingStrings ( ) ;
}
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( printUpdate ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_USER_PRINT ) ;
}
2015-05-03 13:48:36 +02:00
updateInterfaceWithMessages ( user_id , objArr ) ;
2015-03-19 00:09:45 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2013-10-25 17:19:00 +02:00
}
2015-03-19 00:09:45 +01:00
} ) ;
} else {
final boolean printUpdate = updatePrintingUsersWithNewMessages ( - updates . chat_id , objArr ) ;
if ( printUpdate ) {
updatePrintingStrings ( ) ;
2013-10-25 17:19:00 +02:00
}
2015-03-19 00:09:45 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( printUpdate ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_USER_PRINT ) ;
}
updateInterfaceWithMessages ( - updates . chat_id , objArr ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
}
} ) ;
}
2014-10-31 20:02:29 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
2014-07-30 09:49:39 +02:00
@Override
public void run ( ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-07-30 09:49:39 +02:00
@Override
public void run ( ) {
2015-03-26 18:34:47 +01:00
if ( ! obj . isOut ( ) ) {
2014-07-30 09:49:39 +02:00
NotificationsController . getInstance ( ) . processNewMessages ( objArr , true ) ;
}
}
} ) ;
}
} ) ;
2014-08-22 16:24:33 +02:00
MessagesStorage . getInstance ( ) . putMessages ( arr , false , true , false , 0 ) ;
2015-03-19 00:09:45 +01:00
} else if ( MessagesStorage . lastPtsValue ! = updates . pts ) {
FileLog . e ( " tmessages " , " need get diff short message, pts: " + MessagesStorage . lastPtsValue + " " + updates . pts + " count = " + updates . pts_count ) ;
2015-11-26 22:04:02 +01:00
if ( gettingDifference | | updatesStartWaitTimePts = = 0 | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTimePts ) < = 1500 ) {
2015-03-19 00:09:45 +01:00
if ( updatesStartWaitTimePts = = 0 ) {
updatesStartWaitTimePts = System . currentTimeMillis ( ) ;
2014-04-03 23:18:54 +02:00
}
2015-05-03 13:48:36 +02:00
FileLog . e ( " tmessages " , " add to queue " ) ;
2015-03-19 00:09:45 +01:00
updatesQueuePts . add ( updates ) ;
2014-04-03 23:18:54 +02:00
} else {
needGetDiff = true ;
2014-02-04 19:36:55 +01:00
}
}
2013-10-25 17:19:00 +02:00
}
2015-03-19 00:09:45 +01:00
} else if ( updates instanceof TLRPC . TL_updatesCombined | | updates instanceof TLRPC . TL_updates ) {
2016-03-16 13:26:32 +01:00
HashMap < Integer , TLRPC . Chat > minChannels = null ;
for ( int a = 0 ; a < updates . chats . size ( ) ; a + + ) {
TLRPC . Chat chat = updates . chats . get ( a ) ;
if ( chat instanceof TLRPC . TL_channel & & chat . min ) {
TLRPC . Chat existChat = getChat ( chat . id ) ;
if ( existChat = = null | | existChat . min ) {
TLRPC . Chat cacheChat = MessagesStorage . getInstance ( ) . getChatSync ( updates . chat_id ) ;
if ( existChat = = null ) {
putChat ( cacheChat , true ) ;
2015-05-03 13:48:36 +02:00
}
2016-03-16 13:26:32 +01:00
existChat = cacheChat ;
2015-05-03 13:48:36 +02:00
}
2016-03-16 13:26:32 +01:00
if ( existChat = = null | | existChat . min ) {
if ( minChannels = = null ) {
minChannels = new HashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
}
2016-03-16 13:26:32 +01:00
minChannels . put ( chat . id , chat ) ;
}
}
}
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 ) {
int channelId = ( ( TLRPC . TL_updateNewChannelMessage ) update ) . message . to_id . channel_id ;
if ( minChannels . containsKey ( channelId ) ) {
FileLog . e ( " tmessages " , " need get diff because of min channel " + channelId ) ;
2015-03-19 00:09:45 +01:00
needGetDiff = true ;
2016-03-16 13:26:32 +01:00
break ;
2014-07-30 09:49:39 +02:00
}
2015-03-19 00:09:45 +01:00
}
2016-03-16 13:26:32 +01:00
}
}
if ( ! needGetDiff ) {
MessagesStorage . getInstance ( ) . putUsersAndChats ( updates . users , updates . chats , true , true ) ;
Collections . sort ( updates . updates , new Comparator < TLRPC . Update > ( ) {
@Override
public int compare ( TLRPC . Update lhs , TLRPC . Update rhs ) {
int ltype = getUpdateType ( lhs ) ;
int rtype = getUpdateType ( rhs ) ;
if ( ltype ! = rtype ) {
return AndroidUtilities . compare ( ltype , rtype ) ;
} else if ( ltype = = 0 | | ltype = = 2 ) {
return AndroidUtilities . compare ( lhs . pts , rhs . pts ) ;
} else if ( ltype = = 1 ) {
return AndroidUtilities . compare ( lhs . qts , rhs . qts ) ;
2015-05-03 13:48:36 +02:00
}
2016-03-16 13:26:32 +01:00
return 0 ;
2015-05-03 13:48:36 +02:00
}
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 ) ;
updatesNew . pts = update . pts ;
updatesNew . pts_count = update . pts_count ;
for ( int b = a + 1 ; b < updates . updates . size ( ) ; b + + ) {
TLRPC . Update update2 = updates . updates . get ( b ) ;
if ( getUpdateType ( update2 ) = = 0 & & updatesNew . pts + update2 . pts_count = = update2 . pts ) {
updatesNew . updates . add ( update2 ) ;
updatesNew . pts = update2 . pts ;
updatesNew . pts_count + = update2 . pts_count ;
updates . updates . remove ( b ) ;
b - - ;
} else {
break ;
2015-03-19 00:09:45 +01:00
}
2014-04-03 23:18:54 +02:00
}
2016-03-16 13:26:32 +01:00
if ( MessagesStorage . lastPtsValue + updatesNew . pts_count = = updatesNew . pts ) {
if ( ! processUpdateArray ( updatesNew . updates , updates . users , updates . chats ) ) {
FileLog . e ( " tmessages " , " need get diff inner TL_updates, seq: " + MessagesStorage . lastSeqValue + " " + updates . seq ) ;
needGetDiff = true ;
} else {
MessagesStorage . lastPtsValue = updatesNew . pts ;
}
} else if ( MessagesStorage . lastPtsValue ! = updatesNew . pts ) {
FileLog . e ( " tmessages " , update + " need get diff, pts: " + MessagesStorage . lastPtsValue + " " + updatesNew . pts + " count = " + updatesNew . pts_count ) ;
if ( gettingDifference | | updatesStartWaitTimePts = = 0 | | updatesStartWaitTimePts ! = 0 & & Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTimePts ) < = 1500 ) {
if ( updatesStartWaitTimePts = = 0 ) {
updatesStartWaitTimePts = System . currentTimeMillis ( ) ;
}
FileLog . e ( " tmessages " , " add to queue " ) ;
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 ) ;
updatesNew . pts = update . qts ;
for ( int b = a + 1 ; b < updates . updates . size ( ) ; b + + ) {
TLRPC . Update update2 = updates . updates . get ( b ) ;
if ( getUpdateType ( update2 ) = = 1 & & updatesNew . pts + 1 = = update2 . qts ) {
updatesNew . updates . add ( update2 ) ;
updatesNew . pts = update2 . qts ;
updates . updates . remove ( b ) ;
b - - ;
} else {
break ;
}
}
if ( MessagesStorage . lastQtsValue = = 0 | | MessagesStorage . lastQtsValue + updatesNew . updates . size ( ) = = updatesNew . pts ) {
processUpdateArray ( updatesNew . updates , updates . users , updates . chats ) ;
MessagesStorage . lastQtsValue = updatesNew . pts ;
needReceivedQueue = true ;
} else if ( MessagesStorage . lastPtsValue ! = updatesNew . pts ) {
FileLog . e ( " tmessages " , update + " need get diff, qts: " + MessagesStorage . lastQtsValue + " " + updatesNew . pts ) ;
if ( gettingDifference | | updatesStartWaitTimeQts = = 0 | | updatesStartWaitTimeQts ! = 0 & & Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTimeQts ) < = 1500 ) {
if ( updatesStartWaitTimeQts = = 0 ) {
updatesStartWaitTimeQts = System . currentTimeMillis ( ) ;
}
FileLog . e ( " tmessages " , " add to queue " ) ;
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 ;
if ( update instanceof TLRPC . TL_updateNewChannelMessage ) {
channelId = ( ( TLRPC . TL_updateNewChannelMessage ) update ) . message . to_id . channel_id ;
} else if ( update instanceof TLRPC . TL_updateEditChannelMessage ) {
channelId = ( ( TLRPC . TL_updateEditChannelMessage ) update ) . message . to_id . channel_id ;
2015-09-24 22:52:02 +02:00
} else {
2016-03-16 13:26:32 +01:00
channelId = update . channel_id ;
}
Integer channelPts = channelsPts . get ( channelId ) ;
if ( channelPts = = null ) {
channelPts = MessagesStorage . getInstance ( ) . getChannelPtsSync ( channelId ) ;
if ( channelPts = = 0 ) {
channelPts = update . pts - update . pts_count ;
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
channelsPts . put ( channelId , channelPts ) ;
}
TLRPC . TL_updates updatesNew = new TLRPC . TL_updates ( ) ;
updatesNew . updates . add ( update ) ;
updatesNew . pts = update . pts ;
updatesNew . pts_count = update . pts_count ;
for ( int b = a + 1 ; b < updates . updates . size ( ) ; b + + ) {
TLRPC . Update update2 = updates . updates . get ( b ) ;
if ( getUpdateType ( update2 ) = = 2 & & updatesNew . pts + update2 . pts_count = = update2 . pts ) {
updatesNew . updates . add ( update2 ) ;
updatesNew . pts = update2 . pts ;
updatesNew . pts_count + = update2 . pts_count ;
updates . updates . remove ( b ) ;
b - - ;
} else {
break ;
2015-09-24 22:52:02 +02:00
}
2016-03-16 13:26:32 +01:00
}
if ( channelPts + updatesNew . pts_count = = updatesNew . pts ) {
if ( ! processUpdateArray ( updatesNew . updates , updates . users , updates . chats ) ) {
FileLog . e ( " tmessages " , " 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 ) ;
MessagesStorage . getInstance ( ) . saveChannelPts ( channelId , updatesNew . pts ) ;
}
} else if ( channelPts ! = updatesNew . pts ) {
FileLog . e ( " tmessages " , update + " need get channel diff, pts: " + channelPts + " " + updatesNew . pts + " count = " + updatesNew . pts_count + " channelId = " + channelId ) ;
Long updatesStartWaitTime = updatesStartWaitTimeChannels . get ( channelId ) ;
Boolean gettingDifferenceChannel = gettingDifferenceChannels . get ( channelId ) ;
if ( gettingDifferenceChannel = = null ) {
gettingDifferenceChannel = false ;
}
if ( gettingDifferenceChannel | | updatesStartWaitTime = = null | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTime ) < = 1500 ) {
if ( updatesStartWaitTime = = null ) {
updatesStartWaitTimeChannels . put ( channelId , System . currentTimeMillis ( ) ) ;
}
FileLog . e ( " tmessages " , " 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 ) ;
}
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 - - ;
2014-02-04 19:36:55 +01:00
}
2015-03-19 00:09:45 +01:00
2016-03-16 13:26:32 +01:00
boolean processUpdate ;
2015-03-19 00:09:45 +01:00
if ( updates instanceof TLRPC . TL_updatesCombined ) {
2016-03-16 13:26:32 +01:00
processUpdate = MessagesStorage . lastSeqValue + 1 = = updates . seq_start | | MessagesStorage . lastSeqValue = = updates . seq_start ;
2014-02-04 19:36:55 +01:00
} else {
2016-03-16 13:26:32 +01:00
processUpdate = MessagesStorage . lastSeqValue + 1 = = updates . seq | | updates . seq = = 0 | | updates . seq = = MessagesStorage . lastSeqValue ;
2014-02-04 19:36:55 +01:00
}
2016-03-16 13:26:32 +01:00
if ( processUpdate ) {
processUpdateArray ( updates . updates , updates . users , updates . chats ) ;
if ( updates . date ! = 0 ) {
MessagesStorage . lastDateValue = updates . date ;
}
if ( updates . seq ! = 0 ) {
MessagesStorage . lastSeqValue = updates . seq ;
2013-10-25 17:19:00 +02:00
}
2014-02-04 19:36:55 +01:00
} else {
2016-03-16 13:26:32 +01:00
if ( updates instanceof TLRPC . TL_updatesCombined ) {
FileLog . e ( " tmessages " , " need get diff TL_updatesCombined, seq: " + MessagesStorage . lastSeqValue + " " + updates . seq_start ) ;
} else {
FileLog . e ( " tmessages " , " need get diff TL_updates, seq: " + MessagesStorage . lastSeqValue + " " + updates . seq ) ;
}
if ( gettingDifference | | updatesStartWaitTimeSeq = = 0 | | Math . abs ( System . currentTimeMillis ( ) - updatesStartWaitTimeSeq ) < = 1500 ) {
if ( updatesStartWaitTimeSeq = = 0 ) {
updatesStartWaitTimeSeq = System . currentTimeMillis ( ) ;
}
FileLog . e ( " tmessages " , " add TL_updates/Combined to queue " ) ;
updatesQueueSeq . add ( updates ) ;
} else {
needGetDiff = true ;
}
2014-02-04 19:36:55 +01:00
}
2013-10-25 17:19:00 +02:00
}
} else if ( updates instanceof TLRPC . TL_updatesTooLong ) {
2013-12-20 20:25:49 +01:00
FileLog . e ( " tmessages " , " need get diff TL_updatesTooLong " ) ;
2013-10-25 17:19:00 +02:00
needGetDiff = true ;
2015-03-19 00:09:45 +01:00
} else if ( updates instanceof UserActionUpdatesSeq ) {
2014-02-04 19:36:55 +01:00
MessagesStorage . lastSeqValue = updates . seq ;
2015-03-19 00:09:45 +01:00
} 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 ) ;
MessagesStorage . getInstance ( ) . saveChannelPts ( updates . chat_id , updates . pts ) ;
} else {
MessagesStorage . lastPtsValue = updates . pts ;
}
2013-10-25 17:19:00 +02:00
}
2015-01-02 23:15:07 +01:00
SecretChatHelper . getInstance ( ) . processPendingEncMessages ( ) ;
2015-03-19 00:09:45 +01:00
if ( ! fromQueue ) {
2015-09-24 22:52:02 +02:00
ArrayList < Integer > keys = new ArrayList < > ( updatesQueueChannels . keySet ( ) ) ;
for ( int a = 0 ; a < keys . size ( ) ; a + + ) {
Integer key = keys . get ( a ) ;
if ( needGetChannelsDiff ! = null & & needGetChannelsDiff . contains ( key ) ) {
getChannelDifference ( key ) ;
} else {
processChannelsUpdatesQueue ( key , 0 ) ;
}
}
2015-03-19 00:09:45 +01:00
if ( needGetDiff ) {
getDifference ( ) ;
} else {
for ( int a = 0 ; a < 3 ; a + + ) {
2015-09-24 22:52:02 +02:00
processUpdatesQueue ( a , 0 ) ;
2015-03-19 00:09:45 +01:00
}
}
2013-10-25 17:19:00 +02:00
}
if ( needReceivedQueue ) {
TLRPC . TL_messages_receivedQueue req = new TLRPC . TL_messages_receivedQueue ( ) ;
2013-11-04 13:31:01 +01:00
req . max_qts = MessagesStorage . lastQtsValue ;
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2013-10-25 17:19:00 +02:00
@Override
public void run ( TLObject response , TLRPC . TL_error error ) {
}
2014-07-03 00:39:05 +02:00
} ) ;
2013-10-25 17:19:00 +02:00
}
2014-11-21 01:14:44 +01:00
if ( updateStatus ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , UPDATE_MASK_STATUS ) ;
}
} ) ;
}
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . saveDiffParams ( MessagesStorage . lastSeqValue , MessagesStorage . lastPtsValue , MessagesStorage . lastDateValue , MessagesStorage . lastQtsValue ) ;
2013-10-25 17:19:00 +02:00
}
public boolean processUpdateArray ( ArrayList < TLRPC . Update > updates , final ArrayList < TLRPC . User > usersArr , final ArrayList < TLRPC . Chat > chatsArr ) {
if ( updates . isEmpty ( ) ) {
2015-09-24 22:52:02 +02:00
if ( usersArr ! = null | | chatsArr ! = null ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
putUsers ( usersArr , false ) ;
putChats ( chatsArr , false ) ;
}
} ) ;
}
2013-10-25 17:19:00 +02:00
return true ;
}
long currentTime = System . currentTimeMillis ( ) ;
2015-01-02 23:15:07 +01:00
final HashMap < Long , ArrayList < MessageObject > > messages = new HashMap < > ( ) ;
2015-04-09 20:00:14 +02:00
final HashMap < Long , TLRPC . WebPage > webPages = new HashMap < > ( ) ;
2015-01-02 23:15:07 +01:00
final ArrayList < MessageObject > pushMessages = new ArrayList < > ( ) ;
final ArrayList < TLRPC . Message > messagesArr = new ArrayList < > ( ) ;
2016-03-06 02:49:31 +01:00
final HashMap < Long , ArrayList < MessageObject > > editingMessages = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
final SparseArray < SparseIntArray > channelViews = new SparseArray < > ( ) ;
final SparseArray < ArrayList < TLRPC . TL_messageGroup > > channelsGroups = new SparseArray < > ( ) ;
final SparseArray < Long > markAsReadMessagesInbox = new SparseArray < > ( ) ;
final SparseIntArray markAsReadMessagesOutbox = new SparseIntArray ( ) ;
final ArrayList < Long > markAsReadMessages = new ArrayList < > ( ) ;
2015-01-02 23:15:07 +01:00
final HashMap < Integer , Integer > markAsReadEncrypted = new HashMap < > ( ) ;
2015-09-24 22:52:02 +02:00
final SparseArray < ArrayList < Integer > > deletedMessages = new SparseArray < > ( ) ;
2014-05-20 22:22:44 +02:00
boolean printChanged = false ;
2015-01-02 23:15:07 +01:00
final ArrayList < TLRPC . ChatParticipants > chatInfoToUpdate = new ArrayList < > ( ) ;
final ArrayList < TLRPC . Update > updatesOnMainThread = new ArrayList < > ( ) ;
final ArrayList < TLRPC . TL_updateEncryptedMessagesRead > tasks = new ArrayList < > ( ) ;
final ArrayList < Integer > contactsIds = new ArrayList < > ( ) ;
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 < > ( ) ;
2013-10-25 17:19:00 +02:00
for ( TLRPC . User user : usersArr ) {
usersDict . put ( user . id , user ) ;
}
} else {
checkForUsers = false ;
usersDict = users ;
}
if ( chatsArr ! = null ) {
2015-01-02 23:15:07 +01:00
chatsDict = new ConcurrentHashMap < > ( ) ;
2013-10-25 17:19:00 +02:00
for ( TLRPC . Chat chat : chatsArr ) {
chatsDict . put ( chat . id , chat ) ;
}
} else {
checkForUsers = false ;
chatsDict = chats ;
}
2014-02-11 15:32:09 +01:00
if ( usersArr ! = null | | chatsArr ! = null ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-02-11 15:32:09 +01:00
@Override
public void run ( ) {
2014-08-22 16:24:33 +02:00
putUsers ( usersArr , false ) ;
putChats ( chatsArr , false ) ;
2014-02-11 15:32:09 +01:00
}
} ) ;
}
2014-02-04 19:36:55 +01:00
int interfaceUpdateMask = 0 ;
2013-10-25 17:19:00 +02:00
2015-11-26 22:04:02 +01:00
for ( int c = 0 ; c < updates . size ( ) ; c + + ) {
TLRPC . Update update = updates . get ( c ) ;
2015-09-24 22:52:02 +02:00
if ( update instanceof TLRPC . TL_updateNewMessage | | update instanceof TLRPC . TL_updateNewChannelMessage ) {
TLRPC . Message message ;
if ( update instanceof TLRPC . TL_updateNewMessage ) {
message = ( ( TLRPC . TL_updateNewMessage ) update ) . message ;
} else {
message = ( ( TLRPC . TL_updateNewChannelMessage ) update ) . message ;
}
2016-03-16 13:26:32 +01:00
TLRPC . Chat chat = null ;
2013-10-25 17:19:00 +02:00
if ( checkForUsers ) {
2015-11-26 22:04:02 +01:00
int chat_id = 0 ;
2015-10-29 18:10:07 +01:00
if ( message . to_id . channel_id ! = 0 ) {
2015-11-26 22:04:02 +01:00
chat_id = message . to_id . channel_id ;
} else if ( message . to_id . chat_id ! = 0 ) {
chat_id = message . to_id . chat_id ;
}
if ( chat_id ! = 0 ) {
2016-03-16 13:26:32 +01:00
chat = chatsDict . get ( chat_id ) ;
2015-11-26 22:04:02 +01:00
if ( chat = = null ) {
chat = getChat ( chat_id ) ;
2015-10-29 18:10:07 +01:00
}
2016-03-16 13:26:32 +01:00
if ( chat = = null ) {
chat = MessagesStorage . getInstance ( ) . getChatSync ( chat_id ) ;
putChat ( chat , true ) ;
}
2015-11-26 22:04:02 +01:00
if ( chat = = null ) {
2015-10-29 18:10:07 +01:00
return false ;
}
2015-11-26 22:04:02 +01:00
if ( chat . megagroup ) {
message . flags | = TLRPC . MESSAGE_FLAG_MEGAGROUP ;
}
2015-09-24 22:52:02 +02:00
}
if ( message . from_id > 0 ) {
TLRPC . User user = getUser ( message . from_id ) ;
if ( usersDict . get ( message . from_id ) = = null & & user = = null ) {
return false ;
}
if ( user ! = null & & user . status ! = null & & user . status . expires < = 0 ) {
onlinePrivacy . put ( message . from_id , ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) ;
interfaceUpdateMask | = UPDATE_MASK_STATUS ;
}
2014-11-21 01:14:44 +01:00
}
2013-10-25 17:19:00 +02:00
}
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 ( ) ;
2016-03-06 02:49:31 +01:00
} else if ( message . from_id = = UserConfig . getClientUserId ( ) & & message . action . user_id = = UserConfig . getClientUserId ( ) ) {
2015-11-26 22:04:02 +01:00
continue ;
}
} else if ( message . action instanceof TLRPC . TL_messageActionChatMigrateTo | | message . action instanceof TLRPC . TL_messageActionChannelCreate ) {
message . unread = false ;
message . media_unread = false ;
}
if ( update instanceof TLRPC . TL_updateNewChannelMessage ) {
if ( message . to_id . channel_id ! = 0 & & ! message . out ) {
message . unread = true ;
2016-03-06 02:49:31 +01:00
if ( message . post | | ( message . flags & TLRPC . MESSAGE_FLAG_MEGAGROUP ) ! = 0 ) {
2015-11-26 22:04:02 +01:00
message . media_unread = true ;
}
}
long dialog_id = - update . channel_id ;
Integer value = dialogs_read_inbox_max . get ( dialog_id ) ;
if ( value = = null ) {
value = MessagesStorage . getInstance ( ) . getChannelReadInboxMax ( update . channel_id ) ;
}
2016-03-16 13:26:32 +01:00
if ( value > = message . id | | ChatObject . isNotInChat ( chat ) ) {
2015-11-26 22:04:02 +01:00
message . unread = false ;
message . media_unread = false ;
}
}
2015-09-24 22:52:02 +02:00
messagesArr . add ( message ) ;
ImageLoader . saveMessageThumbs ( message ) ;
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 {
2015-09-24 22:52:02 +02:00
if ( message . to_id . user_id = = UserConfig . getClientUserId ( ) ) {
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
}
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( 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 ;
}
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 ) ;
2015-11-26 22:04:02 +01:00
if ( ! obj . isOut ( ) & & ( obj . isUnread ( ) & & message . to_id . channel_id = = 0 | | obj . isContentUnread ( ) ) ) {
2014-07-10 02:15:58 +02:00
pushMessages . add ( obj ) ;
2013-10-25 17:19:00 +02:00
}
2015-05-03 13:48:36 +02:00
} else if ( update instanceof TLRPC . TL_updateReadMessagesContents ) {
2015-09-24 22:52:02 +02:00
for ( int a = 0 ; a < update . messages . size ( ) ; a + + ) {
long id = update . messages . get ( a ) ;
markAsReadMessages . add ( id ) ;
}
2015-03-19 00:09:45 +01:00
} else if ( update instanceof TLRPC . TL_updateReadHistoryInbox ) {
TLRPC . Peer peer = ( ( TLRPC . TL_updateReadHistoryInbox ) update ) . peer ;
if ( peer . chat_id ! = 0 ) {
2015-09-24 22:52:02 +02:00
markAsReadMessagesInbox . put ( - peer . chat_id , ( long ) update . max_id ) ;
2015-03-19 00:09:45 +01:00
} else {
2015-09-24 22:52:02 +02:00
markAsReadMessagesInbox . put ( peer . user_id , ( long ) update . max_id ) ;
2015-03-19 00:09:45 +01:00
}
} else if ( update instanceof TLRPC . TL_updateReadHistoryOutbox ) {
TLRPC . Peer peer = ( ( TLRPC . TL_updateReadHistoryOutbox ) update ) . peer ;
if ( peer . chat_id ! = 0 ) {
markAsReadMessagesOutbox . put ( - peer . chat_id , update . max_id ) ;
} else {
markAsReadMessagesOutbox . put ( peer . user_id , update . max_id ) ;
}
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateDeleteMessages ) {
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 ) ;
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateUserTyping | | update instanceof TLRPC . TL_updateChatUserTyping ) {
2015-05-03 13:48:36 +02:00
if ( update . user_id ! = UserConfig . getClientUserId ( ) ) {
2013-10-25 17:19:00 +02:00
long uid = - update . chat_id ;
if ( uid = = 0 ) {
uid = update . user_id ;
}
ArrayList < PrintingUser > arr = printingUsers . get ( uid ) ;
2015-05-03 13:48:36 +02:00
if ( update . action instanceof TLRPC . TL_sendMessageCancelAction ) {
if ( arr ! = null ) {
for ( int a = 0 ; a < arr . size ( ) ; a + + ) {
PrintingUser pu = arr . get ( a ) ;
if ( pu . userId = = update . 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 ) {
if ( u . userId = = update . user_id ) {
exist = true ;
u . lastTime = currentTime ;
2015-06-29 19:12:11 +02:00
if ( u . action . getClass ( ) ! = update . action . getClass ( ) ) {
printChanged = true ;
}
2015-05-03 13:48:36 +02:00
u . action = update . action ;
break ;
}
}
if ( ! exist ) {
PrintingUser newUser = new PrintingUser ( ) ;
newUser . userId = update . user_id ;
newUser . lastTime = currentTime ;
newUser . action = update . action ;
arr . add ( newUser ) ;
printChanged = true ;
2013-10-25 17:19:00 +02:00
}
}
2014-11-21 01:14:44 +01:00
onlinePrivacy . put ( update . user_id , ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) ;
2013-10-25 17:19:00 +02:00
}
} else if ( update instanceof TLRPC . TL_updateChatParticipants ) {
2014-02-04 19:36:55 +01:00
interfaceUpdateMask | = UPDATE_MASK_CHAT_MEMBERS ;
2013-10-25 17:19:00 +02:00
chatInfoToUpdate . add ( update . participants ) ;
} else if ( update instanceof TLRPC . TL_updateUserStatus ) {
2014-02-04 19:36:55 +01:00
interfaceUpdateMask | = UPDATE_MASK_STATUS ;
2013-10-25 17:19:00 +02:00
updatesOnMainThread . add ( update ) ;
} else if ( update instanceof TLRPC . TL_updateUserName ) {
2014-02-04 19:36:55 +01:00
interfaceUpdateMask | = UPDATE_MASK_NAME ;
2013-10-25 17:19:00 +02:00
updatesOnMainThread . add ( update ) ;
} else if ( update instanceof TLRPC . TL_updateUserPhoto ) {
2014-02-04 19:36:55 +01:00
interfaceUpdateMask | = UPDATE_MASK_AVATAR ;
2014-03-22 23:31:55 +01:00
MessagesStorage . getInstance ( ) . clearUserPhotos ( update . user_id ) ;
2013-10-25 17:19:00 +02:00
updatesOnMainThread . add ( update ) ;
2014-12-01 18:56:31 +01:00
} else if ( update instanceof TLRPC . TL_updateUserPhone ) {
interfaceUpdateMask | = UPDATE_MASK_PHONE ;
updatesOnMainThread . add ( update ) ;
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateContactRegistered ) {
2015-10-29 18:10:07 +01:00
if ( enableJoined & & usersDict . containsKey ( update . user_id ) & & ! MessagesStorage . getInstance ( ) . isDialogHasMessages ( update . user_id ) ) {
2013-12-20 20:25:49 +01:00
TLRPC . TL_messageService newMessage = new TLRPC . TL_messageService ( ) ;
newMessage . action = new TLRPC . TL_messageActionUserJoined ( ) ;
newMessage . local_id = newMessage . id = UserConfig . getNewMessageId ( ) ;
UserConfig . saveConfig ( false ) ;
2015-11-26 22:04:02 +01:00
newMessage . unread = false ;
newMessage . flags = TLRPC . MESSAGE_FLAG_HAS_FROM_ID ;
2013-12-20 20:25:49 +01:00
newMessage . date = update . date ;
newMessage . from_id = update . user_id ;
newMessage . to_id = new TLRPC . TL_peerUser ( ) ;
2014-06-13 12:42:21 +02:00
newMessage . to_id . user_id = UserConfig . getClientUserId ( ) ;
2013-12-20 20:25:49 +01:00
newMessage . dialog_id = update . user_id ;
messagesArr . add ( newMessage ) ;
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( newMessage , usersDict , chatsDict , createdDialogIds . contains ( newMessage . dialog_id ) ) ;
2013-12-20 20:25:49 +01:00
ArrayList < MessageObject > arr = messages . get ( newMessage . dialog_id ) ;
if ( arr = = null ) {
2015-01-02 23:15:07 +01:00
arr = new ArrayList < > ( ) ;
2013-12-20 20:25:49 +01:00
messages . put ( newMessage . dialog_id , arr ) ;
}
arr . add ( obj ) ;
}
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateContactLink ) {
2015-03-19 00:09:45 +01:00
if ( update . my_link instanceof TLRPC . TL_contactLinkContact ) {
2014-02-04 19:36:55 +01:00
int idx = contactsIds . indexOf ( - update . user_id ) ;
if ( idx ! = - 1 ) {
contactsIds . remove ( idx ) ;
}
if ( ! contactsIds . contains ( update . user_id ) ) {
contactsIds . add ( update . user_id ) ;
}
} else {
int idx = contactsIds . indexOf ( update . user_id ) ;
if ( idx ! = - 1 ) {
contactsIds . remove ( idx ) ;
}
if ( ! contactsIds . contains ( update . user_id ) ) {
contactsIds . add ( - update . user_id ) ;
}
}
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateNewAuthorization ) {
2015-04-09 20:00:14 +02:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . newSessionReceived ) ;
}
} ) ;
2013-12-20 20:25:49 +01:00
TLRPC . TL_messageService newMessage = new TLRPC . TL_messageService ( ) ;
newMessage . action = new TLRPC . TL_messageActionLoginUnknownLocation ( ) ;
newMessage . action . title = update . device ;
newMessage . action . address = update . location ;
newMessage . local_id = newMessage . id = UserConfig . getNewMessageId ( ) ;
UserConfig . saveConfig ( false ) ;
2015-11-26 22:04:02 +01:00
newMessage . unread = true ;
newMessage . flags = TLRPC . MESSAGE_FLAG_HAS_FROM_ID ;
2013-12-20 20:25:49 +01:00
newMessage . date = update . date ;
2014-08-29 23:06:04 +02:00
newMessage . from_id = 777000 ;
2013-12-20 20:25:49 +01:00
newMessage . to_id = new TLRPC . TL_peerUser ( ) ;
2014-06-13 12:42:21 +02:00
newMessage . to_id . user_id = UserConfig . getClientUserId ( ) ;
2014-08-29 23:06:04 +02:00
newMessage . dialog_id = 777000 ;
2013-12-20 20:25:49 +01:00
messagesArr . add ( newMessage ) ;
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( newMessage , usersDict , chatsDict , createdDialogIds . contains ( newMessage . dialog_id ) ) ;
2013-12-20 20:25:49 +01:00
ArrayList < MessageObject > arr = messages . get ( newMessage . dialog_id ) ;
if ( arr = = null ) {
2015-01-02 23:15:07 +01:00
arr = new ArrayList < > ( ) ;
2013-12-20 20:25:49 +01:00
messages . put ( newMessage . dialog_id , arr ) ;
}
arr . add ( obj ) ;
2014-07-10 02:15:58 +02:00
pushMessages . add ( obj ) ;
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateNewGeoChatMessage ) {
2014-02-04 19:36:55 +01:00
//DEPRECATED
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateNewEncryptedMessage ) {
2015-03-19 00:09:45 +01:00
ArrayList < TLRPC . Message > decryptedMessages = SecretChatHelper . getInstance ( ) . decryptMessage ( ( ( TLRPC . TL_updateNewEncryptedMessage ) update ) . message ) ;
2014-10-28 22:27:44 +01:00
if ( decryptedMessages ! = null & & ! decryptedMessages . isEmpty ( ) ) {
2015-05-21 23:27:27 +02:00
int cid = ( ( TLRPC . TL_updateNewEncryptedMessage ) update ) . message . chat_id ;
2014-10-28 22:27:44 +01:00
long uid = ( ( long ) cid ) < < 32 ;
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 ) ;
}
2016-03-06 02:49:31 +01:00
for ( int a = 0 ; a < decryptedMessages . size ( ) ; a + + ) {
TLRPC . Message message = decryptedMessages . get ( a ) ;
2015-02-01 19:51:02 +01:00
ImageLoader . saveMessageThumbs ( message ) ;
2014-10-28 22:27:44 +01:00
messagesArr . add ( message ) ;
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( message , usersDict , chatsDict , createdDialogIds . contains ( uid ) ) ;
2014-10-28 22:27:44 +01:00
arr . add ( obj ) ;
pushMessages . add ( obj ) ;
}
2013-10-25 17:19:00 +02:00
}
} else if ( update instanceof TLRPC . TL_updateEncryptedChatTyping ) {
2014-08-22 16:24:33 +02:00
TLRPC . EncryptedChat encryptedChat = getEncryptedChatDB ( update . chat_id ) ;
2014-06-11 01:05:54 +02:00
if ( encryptedChat ! = null ) {
update . user_id = encryptedChat . user_id ;
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 < > ( ) ;
2014-06-11 01:05:54 +02:00
printingUsers . put ( uid , arr ) ;
}
boolean exist = false ;
for ( PrintingUser u : arr ) {
if ( u . userId = = update . user_id ) {
exist = true ;
u . lastTime = currentTime ;
2015-05-03 13:48:36 +02:00
u . action = new TLRPC . TL_sendMessageTypingAction ( ) ;
2014-06-11 01:05:54 +02:00
break ;
}
}
if ( ! exist ) {
PrintingUser newUser = new PrintingUser ( ) ;
newUser . userId = update . user_id ;
newUser . lastTime = currentTime ;
2015-05-03 13:48:36 +02:00
newUser . action = new TLRPC . TL_sendMessageTypingAction ( ) ;
2014-06-11 01:05:54 +02:00
arr . add ( newUser ) ;
printChanged = true ;
2013-10-25 17:19:00 +02:00
}
2014-11-21 01:14:44 +01:00
onlinePrivacy . put ( update . user_id , ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) ;
2013-10-25 17:19:00 +02:00
}
} else if ( update instanceof TLRPC . TL_updateEncryptedMessagesRead ) {
markAsReadEncrypted . put ( update . chat_id , Math . max ( update . max_date , update . date ) ) ;
2015-03-19 00:09:45 +01:00
tasks . add ( ( TLRPC . TL_updateEncryptedMessagesRead ) update ) ;
2014-02-04 19:36:55 +01:00
} else if ( update instanceof TLRPC . TL_updateChatParticipantAdd ) {
2015-11-26 22:04:02 +01:00
MessagesStorage . getInstance ( ) . updateChatInfo ( update . chat_id , update . user_id , 0 , update . inviter_id , update . version ) ;
2014-02-04 19:36:55 +01:00
} else if ( update instanceof TLRPC . TL_updateChatParticipantDelete ) {
2015-11-26 22:04:02 +01:00
MessagesStorage . getInstance ( ) . updateChatInfo ( update . chat_id , update . user_id , 1 , 0 , update . version ) ;
2014-02-04 19:36:55 +01:00
} else if ( update instanceof TLRPC . TL_updateDcOptions ) {
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . updateDcSettings ( ) ;
2013-10-25 17:19:00 +02:00
} else if ( update instanceof TLRPC . TL_updateEncryption ) {
2014-12-01 18:56:31 +01:00
SecretChatHelper . getInstance ( ) . processUpdateEncryption ( ( TLRPC . TL_updateEncryption ) update , usersDict ) ;
2014-07-03 00:39:05 +02:00
} else if ( update instanceof TLRPC . TL_updateUserBlocked ) {
2015-05-21 23:27:27 +02:00
final TLRPC . TL_updateUserBlocked finalUpdate = ( TLRPC . TL_updateUserBlocked ) update ;
2014-08-22 16:24:33 +02:00
if ( finalUpdate . blocked ) {
2015-01-02 23:15:07 +01:00
ArrayList < Integer > ids = new ArrayList < > ( ) ;
2014-08-22 16:24:33 +02:00
ids . add ( finalUpdate . user_id ) ;
MessagesStorage . getInstance ( ) . putBlockedUsers ( ids , false ) ;
} else {
MessagesStorage . getInstance ( ) . deleteBlockedUser ( finalUpdate . user_id ) ;
}
2014-10-31 20:02:29 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-08-22 16:24:33 +02:00
@Override
public void run ( ) {
if ( finalUpdate . blocked ) {
if ( ! blockedUsers . contains ( finalUpdate . user_id ) ) {
blockedUsers . add ( finalUpdate . user_id ) ;
}
} else {
2014-10-31 20:02:29 +01:00
blockedUsers . remove ( ( Integer ) finalUpdate . user_id ) ;
2014-08-22 16:24:33 +02:00
}
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . blockedUsersDidLoaded ) ;
}
} ) ;
}
} ) ;
2014-07-03 00:39:05 +02:00
} else if ( update instanceof TLRPC . TL_updateNotifySettings ) {
updatesOnMainThread . add ( update ) ;
2014-10-17 20:29:13 +02:00
} else if ( update instanceof TLRPC . TL_updateServiceNotification ) {
2015-12-09 19:27:52 +01:00
TLRPC . TL_updateServiceNotification notification = ( TLRPC . TL_updateServiceNotification ) update ;
if ( notification . popup & & notification . message ! = null & & notification . message . length ( ) > 0 ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . needShowAlert , 2 , notification . message ) ;
}
2014-10-31 20:02:29 +01:00
TLRPC . TL_message newMessage = new TLRPC . TL_message ( ) ;
newMessage . local_id = newMessage . id = UserConfig . getNewMessageId ( ) ;
UserConfig . saveConfig ( false ) ;
2015-11-26 22:04:02 +01:00
newMessage . unread = true ;
newMessage . flags = TLRPC . MESSAGE_FLAG_HAS_FROM_ID ;
2015-05-03 13:48:36 +02:00
newMessage . date = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
2014-10-31 20:02:29 +01:00
newMessage . from_id = 777000 ;
newMessage . to_id = new TLRPC . TL_peerUser ( ) ;
newMessage . to_id . user_id = UserConfig . getClientUserId ( ) ;
newMessage . dialog_id = 777000 ;
newMessage . media = update . media ;
2015-09-24 22:52:02 +02:00
newMessage . flags | = TLRPC . MESSAGE_FLAG_HAS_MEDIA ;
2015-12-09 19:27:52 +01:00
newMessage . message = notification . message ;
2014-10-31 20:02:29 +01:00
messagesArr . add ( newMessage ) ;
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( newMessage , usersDict , chatsDict , createdDialogIds . contains ( newMessage . dialog_id ) ) ;
2014-10-31 20:02:29 +01:00
ArrayList < MessageObject > arr = messages . get ( newMessage . dialog_id ) ;
if ( arr = = null ) {
2015-01-02 23:15:07 +01:00
arr = new ArrayList < > ( ) ;
2014-10-31 20:02:29 +01:00
messages . put ( newMessage . dialog_id , arr ) ;
}
arr . add ( obj ) ;
pushMessages . add ( obj ) ;
2014-11-17 03:44:57 +01:00
} else if ( update instanceof TLRPC . TL_updatePrivacy ) {
updatesOnMainThread . add ( update ) ;
2015-04-09 20:00:14 +02:00
} else if ( update instanceof TLRPC . TL_updateWebPage ) {
webPages . put ( update . webpage . id , update . webpage ) ;
2015-09-24 22:52:02 +02:00
} else if ( update instanceof TLRPC . TL_updateChannelTooLong ) {
2016-03-16 13:26:32 +01:00
if ( ( update . flags & 1 ) ! = 0 ) {
Integer channelPts = channelsPts . get ( update . channel_id ) ;
if ( channelPts = = null ) {
channelPts = MessagesStorage . getInstance ( ) . getChannelPtsSync ( update . channel_id ) ;
if ( channelPts = = 0 ) {
channelPts = 1 ;
}
channelsPts . put ( update . channel_id , channelPts ) ;
}
if ( update . pts > channelPts ) {
getChannelDifference ( update . channel_id ) ;
}
} else {
getChannelDifference ( update . channel_id ) ;
}
2015-09-24 22:52:02 +02:00
} else if ( update instanceof TLRPC . TL_updateChannelGroup ) {
ArrayList < TLRPC . TL_messageGroup > arrayList = channelsGroups . get ( update . channel_id ) ;
if ( arrayList = = null ) {
arrayList = new ArrayList < > ( ) ;
channelsGroups . put ( update . channel_id , arrayList ) ;
}
arrayList . add ( update . group ) ;
} else if ( update instanceof TLRPC . TL_updateReadChannelInbox ) {
long message_id = update . max_id ;
message_id | = ( ( long ) update . channel_id ) < < 32 ;
markAsReadMessagesInbox . put ( - update . channel_id , message_id ) ;
2015-11-26 22:04:02 +01:00
long dialog_id = - update . channel_id ;
Integer value = dialogs_read_inbox_max . get ( dialog_id ) ;
if ( value = = null ) {
value = MessagesStorage . getInstance ( ) . getChannelReadInboxMax ( update . channel_id ) ;
}
dialogs_read_inbox_max . put ( dialog_id , Math . max ( value , update . max_id ) ) ;
2015-09-24 22:52:02 +02:00
} else if ( update instanceof TLRPC . TL_updateDeleteChannelMessages ) {
ArrayList < Integer > arrayList = deletedMessages . get ( update . channel_id ) ;
if ( arrayList = = null ) {
arrayList = new ArrayList < > ( ) ;
deletedMessages . put ( update . channel_id , arrayList ) ;
}
arrayList . addAll ( update . messages ) ;
} else if ( update instanceof TLRPC . TL_updateChannel ) {
updatesOnMainThread . add ( update ) ;
} else if ( update instanceof TLRPC . TL_updateChannelMessageViews ) {
SparseIntArray array = channelViews . get ( update . channel_id ) ;
if ( array = = null ) {
array = new SparseIntArray ( ) ;
channelViews . put ( update . channel_id , array ) ;
}
array . put ( update . id , update . views ) ;
2015-11-26 22:04:02 +01:00
} else if ( update instanceof TLRPC . TL_updateChatParticipantAdmin ) {
MessagesStorage . getInstance ( ) . updateChatInfo ( update . chat_id , update . user_id , 2 , update . is_admin ? 1 : 0 , update . version ) ;
} else if ( update instanceof TLRPC . TL_updateChatAdmins ) {
updatesOnMainThread . add ( update ) ;
2015-12-09 19:27:52 +01:00
} else if ( update instanceof TLRPC . TL_updateStickerSets ) {
updatesOnMainThread . add ( update ) ;
} else if ( update instanceof TLRPC . TL_updateStickerSetsOrder ) {
updatesOnMainThread . add ( update ) ;
} else if ( update instanceof TLRPC . TL_updateNewStickerSet ) {
updatesOnMainThread . add ( update ) ;
2016-01-11 18:19:48 +01:00
} else if ( update instanceof TLRPC . TL_updateSavedGifs ) {
updatesOnMainThread . add ( update ) ;
2016-03-06 02:49:31 +01:00
} else if ( update instanceof TLRPC . TL_updateEditChannelMessage ) {
TLRPC . Message message = ( ( TLRPC . TL_updateEditChannelMessage ) update ) . message ;
if ( message . to_id . channel_id ! = 0 & & ! message . out ) {
message . unread = true ;
if ( message . post | | ( message . flags & TLRPC . MESSAGE_FLAG_MEGAGROUP ) ! = 0 ) {
message . media_unread = true ;
}
}
long dialog_id = - update . channel_id ;
Integer value = dialogs_read_inbox_max . get ( dialog_id ) ;
if ( value = = null ) {
value = MessagesStorage . getInstance ( ) . getChannelReadInboxMax ( update . channel_id ) ;
}
if ( value > = message . id ) {
message . unread = false ;
message . media_unread = false ;
}
ImageLoader . saveMessageThumbs ( message ) ;
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 {
if ( message . to_id . user_id = = UserConfig . getClientUserId ( ) ) {
message . to_id . user_id = message . from_id ;
}
message . dialog_id = message . to_id . user_id ;
}
2016-03-16 13:26:32 +01:00
MessageObject obj = new MessageObject ( message , usersDict , chatsDict , createdDialogIds . contains ( message . dialog_id ) ) ;
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 ) ;
2016-03-16 13:26:32 +01:00
} else if ( update instanceof TLRPC . TL_updateChannelPinnedMessage ) {
MessagesStorage . getInstance ( ) . updateChannelPinnedMessage ( update . channel_id , update . id ) ;
2013-10-25 17:19:00 +02:00
}
}
if ( ! messages . isEmpty ( ) ) {
for ( HashMap . Entry < Long , ArrayList < MessageObject > > pair : messages . entrySet ( ) ) {
Long key = pair . getKey ( ) ;
ArrayList < MessageObject > value = pair . getValue ( ) ;
2014-05-20 22:22:44 +02:00
if ( updatePrintingUsersWithNewMessages ( key , value ) ) {
printChanged = true ;
2013-10-25 17:19:00 +02:00
}
}
}
2014-05-20 22:22:44 +02:00
if ( printChanged ) {
2013-10-25 17:19:00 +02:00
updatePrintingStrings ( ) ;
}
2014-02-04 19:36:55 +01:00
final int interfaceUpdateMaskFinal = interfaceUpdateMask ;
2014-05-20 22:22:44 +02:00
final boolean printChangedArg = printChanged ;
2014-02-04 19:36:55 +01:00
if ( ! contactsIds . isEmpty ( ) ) {
2014-03-22 23:31:55 +01:00
ContactsController . getInstance ( ) . processContactsUpdates ( contactsIds , usersDict ) ;
2014-02-04 19:36:55 +01:00
}
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
if ( ! pushMessages . isEmpty ( ) ) {
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
2014-07-30 09:49:39 +02:00
NotificationsController . getInstance ( ) . processNewMessages ( pushMessages , true ) ;
}
2015-09-24 22:52:02 +02:00
} ) ;
}
} ) ;
}
2014-07-30 09:49:39 +02:00
2013-10-25 17:19:00 +02:00
if ( ! messagesArr . isEmpty ( ) ) {
2014-08-22 16:24:33 +02:00
MessagesStorage . getInstance ( ) . putMessages ( messagesArr , true , true , false , MediaController . getInstance ( ) . getAutodownloadMask ( ) ) ;
2013-10-25 17:19:00 +02:00
}
2016-03-06 02:49:31 +01:00
if ( ! editingMessages . isEmpty ( ) ) {
for ( HashMap . Entry < Long , ArrayList < MessageObject > > pair : editingMessages . entrySet ( ) ) {
TLRPC . TL_messages_messages messagesRes = new TLRPC . TL_messages_messages ( ) ;
ArrayList < MessageObject > messageObjects = pair . getValue ( ) ;
for ( int a = 0 ; a < messageObjects . size ( ) ; a + + ) {
messagesRes . messages . add ( messageObjects . get ( a ) . messageOwner ) ;
}
MessagesStorage . getInstance ( ) . putMessages ( messagesRes , pair . getKey ( ) , - 2 , 0 , 0 , false ) ;
}
}
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
if ( channelViews . size ( ) ! = 0 ) {
MessagesStorage . getInstance ( ) . putChannelViews ( channelViews , true ) ;
}
if ( channelsGroups . size ( ) ! = 0 ) {
2016-03-16 13:26:32 +01:00
//MessagesStorage.getInstance().applyNewChannelsGroups(channelsGroups);
2015-09-24 22:52:02 +02:00
}
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-07-10 02:15:58 +02:00
@Override
public void run ( ) {
int updateMask = interfaceUpdateMaskFinal ;
if ( ! updatesOnMainThread . isEmpty ( ) ) {
2015-01-02 23:15:07 +01:00
ArrayList < TLRPC . User > dbUsers = new ArrayList < > ( ) ;
ArrayList < TLRPC . User > dbUsersStatus = new ArrayList < > ( ) ;
2014-07-10 02:15:58 +02:00
SharedPreferences . Editor editor = null ;
2015-12-09 19:27:52 +01:00
for ( int a = 0 ; a < updatesOnMainThread . size ( ) ; a + + ) {
final TLRPC . Update update = updatesOnMainThread . get ( a ) ;
2014-12-01 18:56:31 +01:00
final TLRPC . User toDbUser = new TLRPC . User ( ) ;
2014-07-10 02:15:58 +02:00
toDbUser . id = update . user_id ;
2014-12-01 18:56:31 +01:00
final TLRPC . User currentUser = getUser ( update . user_id ) ;
2014-11-17 03:44:57 +01:00
if ( update instanceof TLRPC . TL_updatePrivacy ) {
if ( update . key instanceof TLRPC . TL_privacyKeyStatusTimestamp ) {
2016-03-06 02:49:31 +01:00
ContactsController . getInstance ( ) . setPrivacyRules ( update . rules , false ) ;
} else if ( update . key instanceof TLRPC . TL_privacyKeyChatInvite ) {
ContactsController . getInstance ( ) . setPrivacyRules ( update . rules , true ) ;
2014-11-17 03:44:57 +01:00
}
} else if ( update instanceof TLRPC . TL_updateUserStatus ) {
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 ;
}
2014-07-10 02:15:58 +02:00
if ( currentUser ! = null ) {
currentUser . id = update . user_id ;
currentUser . status = update . status ;
}
toDbUser . status = update . status ;
dbUsersStatus . add ( toDbUser ) ;
2015-02-01 19:51:02 +01:00
if ( update . user_id = = UserConfig . getClientUserId ( ) ) {
NotificationsController . getInstance ( ) . setLastOnlineFromOtherDevice ( update . status . expires ) ;
}
2014-07-10 02:15:58 +02:00
} else if ( update instanceof TLRPC . TL_updateUserName ) {
if ( currentUser ! = null ) {
2015-06-29 19:12:11 +02:00
if ( ! UserObject . isContact ( currentUser ) ) {
2015-05-03 13:48:36 +02:00
currentUser . first_name = update . first_name ;
currentUser . last_name = update . last_name ;
}
2015-03-19 00:09:45 +01:00
if ( currentUser . username ! = null & & currentUser . username . length ( ) > 0 ) {
usersByUsernames . remove ( currentUser . username ) ;
}
if ( update . username ! = null & & update . username . length ( ) > 0 ) {
usersByUsernames . put ( update . username , currentUser ) ;
}
2014-10-17 20:29:13 +02:00
currentUser . username = update . username ;
2014-07-10 02:15:58 +02:00
}
toDbUser . first_name = update . first_name ;
toDbUser . last_name = update . last_name ;
2014-10-17 20:29:13 +02:00
toDbUser . username = update . username ;
2014-07-10 02:15:58 +02:00
dbUsers . add ( toDbUser ) ;
} else if ( update instanceof TLRPC . TL_updateUserPhoto ) {
if ( currentUser ! = null ) {
currentUser . photo = update . photo ;
}
toDbUser . photo = update . photo ;
dbUsers . add ( toDbUser ) ;
2014-12-01 18:56:31 +01:00
} else if ( update instanceof TLRPC . TL_updateUserPhone ) {
if ( currentUser ! = null ) {
currentUser . phone = update . phone ;
2015-05-03 13:48:36 +02:00
Utilities . phoneBookQueue . postRunnable ( new Runnable ( ) {
2014-12-01 18:56:31 +01:00
@Override
public void run ( ) {
ContactsController . getInstance ( ) . addContactToPhoneBook ( currentUser , true ) ;
}
} ) ;
}
toDbUser . phone = update . phone ;
dbUsers . add ( toDbUser ) ;
2014-07-10 02:15:58 +02:00
} else if ( update instanceof TLRPC . TL_updateNotifySettings ) {
2015-05-21 23:27:27 +02:00
TLRPC . TL_updateNotifySettings updateNotifySettings = ( TLRPC . TL_updateNotifySettings ) update ;
if ( update . notify_settings instanceof TLRPC . TL_peerNotifySettings & & updateNotifySettings . peer instanceof TLRPC . TL_notifyPeer ) {
2014-07-10 02:15:58 +02:00
if ( editor = = null ) {
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
editor = preferences . edit ( ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
long dialog_id ;
if ( updateNotifySettings . peer . peer . user_id ! = 0 ) {
dialog_id = updateNotifySettings . peer . peer . user_id ;
} else if ( updateNotifySettings . peer . peer . chat_id ! = 0 ) {
2015-05-21 23:27:27 +02:00
dialog_id = - updateNotifySettings . peer . peer . chat_id ;
2015-09-24 22:52:02 +02:00
} else {
dialog_id = - updateNotifySettings . peer . peer . channel_id ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
TLRPC . Dialog dialog = dialogs_dict . get ( dialog_id ) ;
2015-02-01 19:51:02 +01:00
if ( dialog ! = null ) {
dialog . notify_settings = update . notify_settings ;
}
2016-03-06 02:49:31 +01:00
editor . putBoolean ( " silent_ " + dialog_id , update . notify_settings . silent ) ;
2015-02-01 19:51:02 +01:00
if ( update . notify_settings . mute_until > ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) {
int until = 0 ;
if ( update . notify_settings . mute_until > ConnectionsManager . getInstance ( ) . getCurrentTime ( ) + 60 * 60 * 24 * 365 ) {
editor . putInt ( " notify2_ " + dialog_id , 2 ) ;
if ( dialog ! = null ) {
dialog . notify_settings . mute_until = Integer . MAX_VALUE ;
}
} else {
until = update . notify_settings . mute_until ;
editor . putInt ( " notify2_ " + dialog_id , 3 ) ;
editor . putInt ( " notifyuntil_ " + dialog_id , update . notify_settings . mute_until ) ;
if ( dialog ! = null ) {
dialog . notify_settings . mute_until = until ;
}
}
2015-05-21 23:27:27 +02:00
MessagesStorage . getInstance ( ) . setDialogFlags ( dialog_id , ( ( long ) until < < 32 ) | 1 ) ;
2015-11-26 22:04:02 +01:00
NotificationsController . getInstance ( ) . removeNotificationsForDialog ( dialog_id ) ;
2014-07-10 02:15:58 +02:00
} else {
2015-02-01 19:51:02 +01:00
if ( dialog ! = null ) {
dialog . notify_settings . mute_until = 0 ;
}
2014-07-10 02:15:58 +02:00
editor . remove ( " notify2_ " + dialog_id ) ;
2015-02-01 19:51:02 +01:00
MessagesStorage . getInstance ( ) . setDialogFlags ( dialog_id , 0 ) ;
2013-10-25 17:19:00 +02:00
}
2015-09-24 22:52:02 +02:00
}
} else if ( update instanceof TLRPC . TL_updateChannel ) {
TLRPC . Dialog dialog = dialogs_dict . get ( - ( long ) update . channel_id ) ;
TLRPC . Chat chat = getChat ( update . channel_id ) ;
2015-11-26 22:04:02 +01:00
if ( dialog = = null & & chat instanceof TLRPC . TL_channel & & ! chat . left ) {
2015-12-09 19:27:52 +01:00
Utilities . stageQueue . postRunnable ( new Runnable ( ) {
@Override
public void run ( ) {
getChannelDifference ( update . channel_id , 1 ) ;
}
} ) ;
2015-11-26 22:04:02 +01:00
} else if ( chat . left & & dialog ! = null ) {
2015-12-09 19:27:52 +01:00
deleteDialog ( dialog . id , 0 ) ;
2015-09-24 22:52:02 +02:00
}
updateMask | = UPDATE_MASK_CHANNEL ;
loadFullChat ( update . channel_id , 0 , true ) ;
2015-11-26 22:04:02 +01:00
} else if ( update instanceof TLRPC . TL_updateChatAdmins ) {
updateMask | = UPDATE_MASK_CHAT_ADMINS ;
2015-12-09 19:27:52 +01:00
} else if ( update instanceof TLRPC . TL_updateStickerSets ) {
StickersQuery . loadStickers ( false , true ) ;
} else if ( update instanceof TLRPC . TL_updateStickerSetsOrder ) {
StickersQuery . reorderStickers ( update . order ) ;
} else if ( update instanceof TLRPC . TL_updateNewStickerSet ) {
StickersQuery . addNewStickerSet ( update . stickerset ) ;
2016-01-11 18:19:48 +01:00
} else if ( update instanceof TLRPC . TL_updateSavedGifs ) {
SharedPreferences . Editor editor2 = ApplicationLoader . applicationContext . getSharedPreferences ( " emoji " , Activity . MODE_PRIVATE ) . edit ( ) ;
editor2 . putLong ( " lastGifLoadTime " , 0 ) . commit ( ) ;
2013-10-25 17:19:00 +02:00
}
}
2014-07-10 02:15:58 +02:00
if ( editor ! = null ) {
editor . commit ( ) ;
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . notificationsSettingsUpdated ) ;
2013-10-25 17:19:00 +02:00
}
2014-07-10 02:15:58 +02:00
MessagesStorage . getInstance ( ) . updateUsers ( dbUsersStatus , true , true , true ) ;
MessagesStorage . getInstance ( ) . updateUsers ( dbUsers , false , true , true ) ;
}
2014-03-22 23:31:55 +01:00
2015-04-09 20:00:14 +02:00
if ( ! webPages . isEmpty ( ) ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . didReceivedWebpagesInUpdates , webPages ) ;
2016-03-06 02:49:31 +01:00
for ( HashMap . Entry < Long , TLRPC . WebPage > entry : webPages . entrySet ( ) ) {
ArrayList < MessageObject > arrayList = reloadingWebpagesPending . remove ( entry . getKey ( ) ) ;
if ( arrayList ! = null ) {
TLRPC . WebPage webpage = entry . getValue ( ) ;
ArrayList < TLRPC . Message > messagesArr = new ArrayList < > ( ) ;
long dialog_id = 0 ;
if ( webpage instanceof TLRPC . TL_webPage | | webpage instanceof TLRPC . TL_webPageEmpty ) {
for ( int a = 0 ; a < arrayList . size ( ) ; a + + ) {
arrayList . get ( a ) . messageOwner . media . webpage = webpage ;
if ( a = = 0 ) {
dialog_id = arrayList . get ( a ) . getDialogId ( ) ;
ImageLoader . saveMessageThumbs ( arrayList . get ( a ) . messageOwner ) ;
}
messagesArr . add ( arrayList . get ( a ) . messageOwner ) ;
}
} else {
reloadingWebpagesPending . put ( webpage . id , arrayList ) ;
}
if ( ! messagesArr . isEmpty ( ) ) {
MessagesStorage . getInstance ( ) . putMessages ( messagesArr , true , true , false , MediaController . getInstance ( ) . getAutodownloadMask ( ) ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . replaceMessagesObjects , dialog_id , arrayList ) ;
}
}
}
2015-04-09 20:00:14 +02:00
}
2016-03-06 02:49:31 +01:00
boolean updateDialogs = false ;
if ( ! editingMessages . isEmpty ( ) ) {
for ( HashMap . Entry < Long , ArrayList < MessageObject > > pair : editingMessages . entrySet ( ) ) {
Long dialog_id = pair . getKey ( ) ;
ArrayList < MessageObject > arrayList = pair . getValue ( ) ;
MessageObject oldObject = dialogMessage . get ( dialog_id ) ;
if ( oldObject ! = null ) {
for ( int a = 0 ; a < arrayList . size ( ) ; a + + ) {
MessageObject newMessage = arrayList . get ( a ) ;
if ( oldObject . getId ( ) = = newMessage . getId ( ) ) {
dialogMessage . put ( dialog_id , newMessage ) ;
updateDialogs = true ;
break ;
}
}
}
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . replaceMessagesObjects , dialog_id , arrayList ) ;
}
}
2014-07-10 02:15:58 +02:00
if ( ! messages . isEmpty ( ) ) {
for ( HashMap . Entry < Long , ArrayList < MessageObject > > entry : messages . entrySet ( ) ) {
Long key = entry . getKey ( ) ;
ArrayList < MessageObject > value = entry . getValue ( ) ;
updateInterfaceWithMessages ( key , value ) ;
2013-10-25 17:19:00 +02:00
}
2016-03-06 02:49:31 +01:00
updateDialogs = true ;
}
if ( updateDialogs ) {
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . dialogsNeedReload ) ;
2014-07-10 02:15:58 +02:00
}
2016-03-06 02:49:31 +01:00
2014-07-10 02:15:58 +02:00
if ( printChangedArg ) {
updateMask | = UPDATE_MASK_USER_PRINT ;
}
if ( ! contactsIds . isEmpty ( ) ) {
updateMask | = UPDATE_MASK_NAME ;
updateMask | = UPDATE_MASK_USER_PHONE ;
}
if ( ! chatInfoToUpdate . isEmpty ( ) ) {
2016-03-06 02:49:31 +01:00
for ( int a = 0 ; a < chatInfoToUpdate . size ( ) ; a + + ) {
TLRPC . ChatParticipants info = chatInfoToUpdate . get ( a ) ;
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . updateChatParticipants ( info ) ;
2013-10-25 17:19:00 +02:00
}
}
2015-09-24 22:52:02 +02:00
if ( channelViews . size ( ) ! = 0 ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . didUpdatedMessagesViews , channelViews ) ;
}
2014-07-10 02:15:58 +02:00
if ( updateMask ! = 0 ) {
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , updateMask ) ;
2014-07-10 02:15:58 +02:00
}
}
} ) ;
2013-10-25 17:19:00 +02:00
2014-10-31 20:02:29 +01:00
MessagesStorage . getInstance ( ) . getStorageQueue ( ) . postRunnable ( new Runnable ( ) {
2014-07-30 09:49:39 +02:00
@Override
public void run ( ) {
2014-11-06 22:34:47 +01:00
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
2014-07-30 09:49:39 +02:00
@Override
public void run ( ) {
2014-08-06 01:17:40 +02:00
int updateMask = 0 ;
2015-09-24 22:52:02 +02:00
if ( markAsReadMessagesInbox . size ( ) ! = 0 | | markAsReadMessagesOutbox . size ( ) ! = 0 ) {
2015-03-19 00:09:45 +01:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messagesRead , markAsReadMessagesInbox , markAsReadMessagesOutbox ) ;
NotificationsController . getInstance ( ) . processReadMessages ( markAsReadMessagesInbox , 0 , 0 , 0 , false ) ;
2015-09-24 22:52:02 +02:00
for ( int b = 0 ; b < markAsReadMessagesInbox . size ( ) ; b + + ) {
int key = markAsReadMessagesInbox . keyAt ( b ) ;
long messageId = markAsReadMessagesInbox . get ( key ) ;
TLRPC . Dialog dialog = dialogs_dict . get ( ( long ) key ) ;
if ( dialog ! = null & & dialog . top_message < = ( int ) messageId ) {
MessageObject obj = dialogMessage . get ( dialog . id ) ;
2015-03-19 00:09:45 +01:00
if ( obj ! = null ) {
obj . setIsRead ( ) ;
updateMask | = UPDATE_MASK_READ_DIALOG_MESSAGE ;
}
}
}
2015-09-24 22:52:02 +02:00
for ( int b = 0 ; b < markAsReadMessagesOutbox . size ( ) ; b + + ) {
int key = markAsReadMessagesOutbox . keyAt ( b ) ;
int messageId = markAsReadMessagesOutbox . get ( key ) ;
TLRPC . Dialog dialog = dialogs_dict . get ( ( long ) key ) ;
if ( dialog ! = null & & dialog . top_message < = messageId ) {
MessageObject obj = dialogMessage . get ( dialog . id ) ;
2015-03-19 00:09:45 +01:00
if ( obj ! = null ) {
obj . setIsRead ( ) ;
updateMask | = UPDATE_MASK_READ_DIALOG_MESSAGE ;
}
2014-08-06 01:17:40 +02:00
}
}
}
if ( ! markAsReadEncrypted . isEmpty ( ) ) {
for ( HashMap . Entry < Integer , Integer > entry : markAsReadEncrypted . entrySet ( ) ) {
2015-05-03 13:48:36 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messagesReadEncrypted , entry . getKey ( ) , entry . getValue ( ) ) ;
2014-10-31 20:02:29 +01:00
long dialog_id = ( long ) ( entry . getKey ( ) ) < < 32 ;
2015-09-24 22:52:02 +02:00
TLRPC . Dialog dialog = dialogs_dict . get ( dialog_id ) ;
2014-08-06 01:17:40 +02:00
if ( dialog ! = null ) {
2015-09-24 22:52:02 +02:00
MessageObject message = dialogMessage . get ( dialog_id ) ;
2014-08-06 01:17:40 +02:00
if ( message ! = null & & message . messageOwner . date < = entry . getValue ( ) ) {
2014-10-09 17:55:05 +02:00
message . setIsRead ( ) ;
2014-08-06 01:17:40 +02:00
updateMask | = UPDATE_MASK_READ_DIALOG_MESSAGE ;
}
}
}
}
2015-05-03 13:48:36 +02:00
if ( ! markAsReadMessages . isEmpty ( ) ) {
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messagesReadContent , markAsReadMessages ) ;
}
2015-09-24 22:52:02 +02:00
if ( deletedMessages . size ( ) ! = 0 ) {
for ( int a = 0 ; a < deletedMessages . size ( ) ; a + + ) {
int key = deletedMessages . keyAt ( a ) ;
ArrayList < Integer > arrayList = deletedMessages . get ( key ) ;
2015-11-26 22:04:02 +01:00
if ( arrayList = = null ) {
continue ;
}
2015-09-24 22:52:02 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . messagesDeleted , arrayList , key ) ;
if ( key = = 0 ) {
for ( int b = 0 ; b < arrayList . size ( ) ; b + + ) {
Integer id = arrayList . get ( b ) ;
MessageObject obj = dialogMessagesByIds . get ( id ) ;
if ( obj ! = null ) {
obj . deleted = true ;
}
}
} else {
MessageObject obj = dialogMessage . get ( ( long ) - key ) ;
if ( obj ! = null ) {
for ( int b = 0 ; b < arrayList . size ( ) ; b + + ) {
if ( obj . getId ( ) = = arrayList . get ( b ) ) {
obj . deleted = true ;
break ;
}
}
}
2014-08-06 01:17:40 +02:00
}
}
2015-11-26 22:04:02 +01:00
NotificationsController . getInstance ( ) . removeDeletedMessagesFromNotifications ( deletedMessages ) ;
2014-08-06 01:17:40 +02:00
}
if ( updateMask ! = 0 ) {
2014-08-22 16:24:33 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . updateInterfaces , updateMask ) ;
2014-03-22 23:31:55 +01:00
}
2014-07-30 09:49:39 +02:00
}
} ) ;
}
} ) ;
2014-03-22 23:31:55 +01:00
2015-04-09 20:00:14 +02:00
if ( ! webPages . isEmpty ( ) ) {
MessagesStorage . getInstance ( ) . putWebPages ( webPages ) ;
}
2015-09-24 22:52:02 +02:00
if ( markAsReadMessagesInbox . size ( ) ! = 0 | | markAsReadMessagesOutbox . size ( ) ! = 0 | | ! markAsReadEncrypted . isEmpty ( ) ) {
2015-12-09 19:27:52 +01:00
if ( markAsReadMessagesInbox . size ( ) ! = 0 ) {
2015-07-22 20:56:37 +02:00
MessagesStorage . getInstance ( ) . updateDialogsWithReadMessages ( markAsReadMessagesInbox , true ) ;
2014-02-28 23:28:25 +01:00
}
2015-03-19 00:09:45 +01:00
MessagesStorage . getInstance ( ) . markMessagesAsRead ( markAsReadMessagesInbox , markAsReadMessagesOutbox , markAsReadEncrypted , true ) ;
2013-10-25 17:19:00 +02:00
}
2015-05-03 13:48:36 +02:00
if ( ! markAsReadMessages . isEmpty ( ) ) {
MessagesStorage . getInstance ( ) . markMessagesContentAsRead ( markAsReadMessages ) ;
}
2015-09-24 22:52:02 +02:00
if ( deletedMessages . size ( ) ! = 0 ) {
for ( int a = 0 ; a < deletedMessages . size ( ) ; a + + ) {
int key = deletedMessages . keyAt ( a ) ;
ArrayList < Integer > arrayList = deletedMessages . get ( key ) ;
MessagesStorage . getInstance ( ) . markMessagesAsDeleted ( arrayList , true , key ) ;
MessagesStorage . getInstance ( ) . updateDialogsWithDeletedMessages ( arrayList , true , key ) ;
}
2013-10-25 17:19:00 +02:00
}
if ( ! tasks . isEmpty ( ) ) {
2016-03-06 02:49:31 +01:00
for ( int a = 0 ; a < tasks . size ( ) ; a + + ) {
TLRPC . TL_updateEncryptedMessagesRead update = tasks . get ( a ) ;
2014-10-09 17:55:05 +02:00
MessagesStorage . getInstance ( ) . createTaskForSecretChat ( update . chat_id , update . max_date , update . date , 1 , null ) ;
2013-10-25 17:19:00 +02:00
}
}
return true ;
}
2015-03-19 00:09:45 +01:00
private boolean isNotifySettingsMuted ( TLRPC . PeerNotifySettings settings ) {
return settings instanceof TLRPC . TL_peerNotifySettings & & settings . mute_until > ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ;
}
public boolean isDialogMuted ( long dialog_id ) {
SharedPreferences preferences = ApplicationLoader . applicationContext . getSharedPreferences ( " Notifications " , Activity . MODE_PRIVATE ) ;
int mute_type = preferences . getInt ( " notify2_ " + dialog_id , 0 ) ;
if ( mute_type = = 2 ) {
return true ;
} else if ( mute_type = = 3 ) {
int mute_until = preferences . getInt ( " notifyuntil_ " + dialog_id , 0 ) ;
if ( mute_until > = ConnectionsManager . getInstance ( ) . 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 ;
}
2014-08-22 16:24:33 +02:00
protected void updateInterfaceWithMessages ( long uid , ArrayList < MessageObject > messages ) {
2014-07-27 00:28:33 +02:00
updateInterfaceWithMessages ( uid , messages , false ) ;
}
2016-01-11 18:19:48 +01:00
protected static void addNewGifToRecent ( TLRPC . Document document , int date ) {
ArrayList < MediaController . SearchImage > arrayList = new ArrayList < > ( ) ;
MediaController . SearchImage searchImage = new MediaController . SearchImage ( ) ;
searchImage . type = 2 ;
searchImage . document = document ;
searchImage . date = date ;
searchImage . id = " " + searchImage . document . id ;
arrayList . add ( searchImage ) ;
MessagesStorage . getInstance ( ) . putWebRecent ( arrayList ) ;
}
2014-08-22 16:24:33 +02:00
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 ;
for ( int a = 0 ; a < messages . size ( ) ; a + + ) {
MessageObject message = messages . get ( a ) ;
2015-05-03 13:48:36 +02:00
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 ) {
2015-11-26 22:04:02 +01:00
if ( message . messageOwner . to_id . channel_id = = 0 | | message . isMegagroup ( ) | | message . isImportant ( ) ) {
2015-09-24 22:52:02 +02:00
lastMessage = message ;
}
if ( message . messageOwner . to_id . channel_id ! = 0 ) {
channelId = message . messageOwner . to_id . channel_id ;
}
2013-10-25 17:19:00 +02:00
}
2016-03-06 02:49:31 +01:00
if ( message . isOut ( ) & & message . isNewGif ( ) & & ! message . isSending ( ) & & ! message . isForwarded ( ) ) {
2016-01-11 18:19:48 +01:00
addNewGifToRecent ( message . messageOwner . media . document , message . messageOwner . date ) ;
}
2013-10-25 17:19:00 +02:00
}
2016-03-16 13:26:32 +01:00
MessagesQuery . loadReplyMessagesForMessages ( messages , uid ) ;
2015-05-21 23:27:27 +02:00
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . didReceivedNewMessages , uid , messages ) ;
if ( lastMessage = = null ) {
return ;
}
2015-09-24 22:52:02 +02:00
TLRPC . Dialog dialog = dialogs_dict . get ( uid ) ;
2015-11-26 22:04:02 +01:00
if ( lastMessage . messageOwner . action instanceof TLRPC . TL_messageActionChatMigrateTo ) {
if ( dialog ! = null ) {
dialogs . remove ( dialog ) ;
dialogsServerOnly . remove ( dialog ) ;
dialogsGroupsOnly . remove ( dialog ) ;
dialogs_dict . remove ( dialog . id ) ;
dialogs_read_inbox_max . remove ( dialog . id ) ;
nextDialogsCacheOffset - - ;
dialogMessage . remove ( dialog . id ) ;
2016-03-06 02:49:31 +01:00
MessageObject object = dialogMessagesByIds . remove ( dialog . top_message ) ;
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 ;
NotificationsController . getInstance ( ) . removeNotificationsForDialog ( dialog . id ) ;
NotificationCenter . getInstance ( ) . postNotificationName ( NotificationCenter . needReloadRecentDialogsSearch ) ;
}
return ;
}
2015-05-21 23:27:27 +02:00
2014-03-05 17:42:10 +01:00
boolean changed = false ;
2013-10-25 17:19:00 +02:00
if ( dialog = = null ) {
2014-07-27 00:28:33 +02:00
if ( ! isBroadcast ) {
2015-09-24 22:52:02 +02:00
TLRPC . Chat chat = getChat ( channelId ) ;
2016-01-11 18:19:48 +01:00
if ( channelId ! = 0 & & chat = = null | | chat ! = null & & chat . left ) {
2015-09-24 22:52:02 +02:00
return ;
}
if ( ! ChatObject . isChannel ( chat ) ) {
dialog = new TLRPC . TL_dialog ( ) ;
} else {
dialog = new TLRPC . TL_dialogChannel ( ) ;
}
2014-07-27 00:28:33 +02:00
dialog . id = uid ;
dialog . unread_count = 0 ;
2015-03-19 00:09:45 +01:00
dialog . top_message = lastMessage . getId ( ) ;
2014-07-27 00:28:33 +02:00
dialog . last_message_date = lastMessage . messageOwner . date ;
dialogs_dict . put ( uid , dialog ) ;
dialogs . 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
}
nextDialogsCacheOffset + + ;
2014-07-27 00:28:33 +02:00
changed = true ;
}
2013-10-25 17:19:00 +02:00
} else {
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 ) | |
! dialogMessage . containsKey ( uid ) | | dialog . top_message < 0 | | dialog . last_message_date < = lastMessage . messageOwner . date ) {
2016-03-06 02:49:31 +01:00
MessageObject object = dialogMessagesByIds . remove ( dialog . top_message ) ;
if ( object ! = null & & object . messageOwner . random_id ! = 0 ) {
dialogMessagesByRandomIds . remove ( object . messageOwner . random_id ) ;
}
2015-03-19 00:09:45 +01:00
dialog . top_message = lastMessage . getId ( ) ;
2014-07-27 00:28:33 +02:00
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
}
2014-03-04 20:29:32 +01:00
}
2013-10-25 17:19:00 +02:00
}
2014-03-05 17:42:10 +01:00
if ( changed ) {
dialogsServerOnly . clear ( ) ;
2015-06-29 19:12:11 +02:00
dialogsGroupsOnly . clear ( ) ;
2015-09-24 22:52:02 +02:00
Collections . sort ( dialogs , new Comparator < TLRPC . Dialog > ( ) {
2014-03-05 17:42:10 +01:00
@Override
2015-09-24 22:52:02 +02:00
public int compare ( TLRPC . Dialog tl_dialog , TLRPC . Dialog tl_dialog2 ) {
2014-03-05 17:42:10 +01:00
if ( tl_dialog . last_message_date = = tl_dialog2 . last_message_date ) {
return 0 ;
} else if ( tl_dialog . last_message_date < tl_dialog2 . last_message_date ) {
return 1 ;
} else {
return - 1 ;
}
}
} ) ;
2015-10-29 18:10:07 +01:00
for ( int a = 0 ; a < dialogs . size ( ) ; a + + ) {
TLRPC . Dialog d = dialogs . get ( a ) ;
2015-05-21 23:27:27 +02:00
int high_id = ( int ) ( d . id > > 32 ) ;
if ( ( int ) d . id ! = 0 & & high_id ! = 1 ) {
2014-03-05 17:42:10 +01:00
dialogsServerOnly . add ( d ) ;
2015-09-24 22:52:02 +02:00
if ( d instanceof TLRPC . TL_dialog ) {
if ( d . id < 0 ) {
dialogsGroupsOnly . add ( d ) ;
}
} else if ( d instanceof TLRPC . TL_dialogChannel ) {
2015-11-26 22:04:02 +01:00
int lower_id = ( int ) d . id ;
TLRPC . Chat chat = getChat ( - lower_id ) ;
if ( chat ! = null & & ( chat . megagroup & & chat . editor | | chat . creator ) ) {
dialogsGroupsOnly . add ( d ) ;
}
2015-06-29 19:12:11 +02:00
}
2013-10-25 17:19:00 +02:00
}
}
}
}
2015-02-27 20:57:58 +01:00
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 ) {
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 ( ) ) ;
}
public static boolean checkCanOpenChat ( Bundle bundle , BaseFragment fragment ) {
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 ) ;
if ( user_id ! = 0 ) {
user = MessagesController . getInstance ( ) . getUser ( user_id ) ;
} else if ( chat_id ! = 0 ) {
chat = MessagesController . getInstance ( ) . getChat ( chat_id ) ;
}
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 ;
}
return true ;
}
public static void openChatOrProfileWith ( TLRPC . User user , TLRPC . Chat chat , BaseFragment fragment , int type ) {
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 ) ;
}
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 ) ) ;
} else {
2016-03-16 13:26:32 +01:00
fragment . presentFragment ( new ChatActivity ( args ) /*, fragment instanceof ChatActivity*/ ) ;
2015-05-21 23:27:27 +02:00
}
2016-03-16 13:26:32 +01:00
}
}
public static void openByUserName ( String username , final BaseFragment fragment , final int type ) {
if ( username = = null | | fragment = = null ) {
return ;
}
TLRPC . User user = getInstance ( ) . getUser ( username ) ;
if ( user ! = null ) {
openChatOrProfileWith ( user , null , fragment , type ) ;
2015-05-21 23:27:27 +02:00
} else {
if ( fragment . getParentActivity ( ) = = null ) {
return ;
}
final ProgressDialog progressDialog = new ProgressDialog ( fragment . getParentActivity ( ) ) ;
progressDialog . setMessage ( LocaleController . getString ( " Loading " , R . string . Loading ) ) ;
progressDialog . setCanceledOnTouchOutside ( false ) ;
progressDialog . setCancelable ( false ) ;
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 ;
2015-09-24 22:52:02 +02:00
final int reqId = ConnectionsManager . getInstance ( ) . sendRequest ( req , new RequestDelegate ( ) {
2015-05-21 23:27:27 +02:00
@Override
public void run ( final TLObject response , final TLRPC . TL_error error ) {
AndroidUtilities . runOnUIThread ( new Runnable ( ) {
@Override
public void run ( ) {
try {
progressDialog . dismiss ( ) ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
2015-09-24 22:52:02 +02:00
fragment . setVisibleDialog ( null ) ;
2015-05-21 23:27:27 +02:00
if ( error = = null ) {
2015-09-24 22:52:02 +02:00
TLRPC . TL_contacts_resolvedPeer res = ( TLRPC . TL_contacts_resolvedPeer ) response ;
2016-03-16 13:26:32 +01:00
getInstance ( ) . putUsers ( res . users , false ) ;
getInstance ( ) . putChats ( res . chats , false ) ;
2015-09-24 22:52:02 +02:00
MessagesStorage . getInstance ( ) . putUsersAndChats ( res . users , res . chats , false , true ) ;
if ( ! res . chats . isEmpty ( ) ) {
2016-03-16 13:26:32 +01:00
openChatOrProfileWith ( null , res . chats . get ( 0 ) , fragment , 1 ) ;
} else if ( ! res . users . isEmpty ( ) ) {
openChatOrProfileWith ( res . users . get ( 0 ) , null , fragment , type ) ;
2015-05-21 23:27:27 +02:00
}
2015-06-29 19:12: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 ( " tmessages " , e ) ;
}
}
2015-05-21 23:27:27 +02:00
}
}
} ) ;
}
} ) ;
progressDialog . setButton ( DialogInterface . BUTTON_NEGATIVE , LocaleController . getString ( " Cancel " , R . string . Cancel ) , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
2015-09-24 22:52:02 +02:00
ConnectionsManager . getInstance ( ) . cancelRequest ( reqId , true ) ;
2015-05-21 23:27:27 +02:00
try {
dialog . dismiss ( ) ;
} catch ( Exception e ) {
FileLog . e ( " tmessages " , e ) ;
}
if ( fragment ! = null ) {
fragment . setVisibleDialog ( null ) ;
}
}
} ) ;
fragment . setVisibleDialog ( progressDialog ) ;
progressDialog . show ( ) ;
}
}
2013-10-25 17:19:00 +02:00
}