Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
automigrate to any available server from our list
Browse files Browse the repository at this point in the history
  • Loading branch information
iamironrabbit committed Jun 21, 2018
1 parent 0cbd840 commit 835c96e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 100 deletions.
20 changes: 4 additions & 16 deletions app/src/main/java/org/awesomeapp/messenger/ImApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.awesomeapp.messenger.crypto.otr.OtrAndroidKeyManagerImpl;
import org.awesomeapp.messenger.model.ImConnection;
import org.awesomeapp.messenger.model.ImErrorInfo;
import org.awesomeapp.messenger.model.Server;
import org.awesomeapp.messenger.provider.Imps;
import org.awesomeapp.messenger.provider.ImpsProvider;
import org.awesomeapp.messenger.service.Broadcaster;
Expand Down Expand Up @@ -1121,7 +1122,7 @@ public void notifyAccountUpgrade ()

}

public boolean doUpgrade (Activity activity, String newDomain, MigrateAccountTask.MigrateAccountListener listener)
public boolean doUpgrade (Activity activity, MigrateAccountTask.MigrateAccountListener listener)
{

boolean result = false;
Expand All @@ -1147,23 +1148,10 @@ public boolean doUpgrade (Activity activity, String newDomain, MigrateAccountTas

long providerId = cursorProviders.getLong(0);
long accountId = cursorProviders.getLong(1);
String username = cursorProviders.getString(2);
String nickname = cursorProviders.getString(3);
boolean keepSignedIn = cursorProviders.getInt(4) != 0;

Cursor pCursor = getContentResolver().query(Imps.ProviderSettings.CONTENT_URI, new String[]{Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE}, Imps.ProviderSettings.PROVIDER + "=?", new String[]{Long.toString(providerId)}, null);

Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
pCursor, getContentResolver(), providerId, false /* don't keep updated */, null /* no handler */);

username = username + '@' + settings.getDomain();

if (settings.getDomain().equalsIgnoreCase("dukgo.com") && keepSignedIn)
{
new MigrateAccountTask(activity, this, providerId, accountId, listener).execute(newDomain);
}

settings.close();
if (keepSignedIn)
new MigrateAccountTask(activity, this, providerId, accountId, listener).execute(Server.getServers(this));

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
Expand All @@ -20,6 +21,7 @@
import org.awesomeapp.messenger.model.Contact;
import org.awesomeapp.messenger.model.ImConnection;
import org.awesomeapp.messenger.model.ImErrorInfo;
import org.awesomeapp.messenger.model.Server;
import org.awesomeapp.messenger.provider.Imps;
import org.awesomeapp.messenger.service.IChatSession;
import org.awesomeapp.messenger.service.IChatSessionManager;
Expand Down Expand Up @@ -49,10 +51,11 @@
* Created by n8fr8 on 5/1/17.
*/

public class MigrateAccountTask extends AsyncTask<String, Void, OnboardingAccount> {
public class MigrateAccountTask extends AsyncTask<Server, Void, OnboardingAccount> {

Activity mContext;
IImConnection mConn;
String mDomain;
long mAccountId;
long mProviderId;
ImApp mApp;
Expand All @@ -76,11 +79,23 @@ public MigrateAccountTask(Activity context, ImApp app, long providerId, long acc

mConn = app.getConnection(providerId, accountId);

Cursor cursor = context.getContentResolver().query(Imps.ProviderSettings.CONTENT_URI, new String[]{Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE}, Imps.ProviderSettings.PROVIDER + "=?", new String[]{Long.toString(mProviderId)}, null);

if (cursor == null)
return;

Imps.ProviderSettings.QueryMap providerSettings = new Imps.ProviderSettings.QueryMap(
cursor, context.getContentResolver(), mProviderId, false, null);
mDomain = providerSettings.getDomain();
providerSettings.close();
if (!cursor.isClosed())
cursor.close();

mContacts = new ArrayList<>();
}

@Override
protected OnboardingAccount doInBackground(String... newDomains) {
protected OnboardingAccount doInBackground(Server... newServers) {

//get existing account username
String nickname = Imps.Account.getNickname(mContext.getContentResolver(), mAccountId);
Expand All @@ -92,120 +107,114 @@ protected OnboardingAccount doInBackground(String... newDomains) {
String fingerprint = keyMan.getFingerprint(keyPair.getPublic());

//find or use provided new server/domain
String domain = newDomains[0];
for (Server newServer : newServers) {

//register account on new domain with same password
mNewAccount = registerNewAccount(nickname, username, password, domain, null);
if (mDomain.equals(newServer.domain))
continue; //don't migrate to the same server... to to =

if (mNewAccount == null) {

username = username + '.' + fingerprint.substring(fingerprint.length()-8,fingerprint.length()).toLowerCase();
mNewAccount = registerNewAccount(nickname, username, password, domain, null);
//register account on new domain with same password
mNewAccount = registerNewAccount(nickname, username, password, newServer.domain, newServer.server);

if (mNewAccount == null)
return null;
}
continue; //try the next server

String newJabberId = mNewAccount.username + '@' + mNewAccount.domain;
keyMan.storeKeyPair(newJabberId,keyPair);
String newJabberId = mNewAccount.username + '@' + mNewAccount.domain;
keyMan.storeKeyPair(newJabberId, keyPair);

//send migration message to existing contacts and/or sessions
try {
//send migration message to existing contacts and/or sessions
try {

boolean loggedInToOldAccount = mConn.getState() == ImConnection.LOGGED_IN;
boolean loggedInToOldAccount = mConn.getState() == ImConnection.LOGGED_IN;

//login and set new default account
SignInHelper signInHelper = new SignInHelper(mContext, mHandler);
signInHelper.activateAccount(mNewAccount.providerId, mNewAccount.accountId);
signInHelper.signIn(mNewAccount.password, mNewAccount.providerId, mNewAccount.accountId, true);
//login and set new default account
SignInHelper signInHelper = new SignInHelper(mContext, mHandler);
signInHelper.activateAccount(mNewAccount.providerId, mNewAccount.accountId);
signInHelper.signIn(mNewAccount.password, mNewAccount.providerId, mNewAccount.accountId, true);

mNewConn = mApp.getConnection(mNewAccount.providerId, mNewAccount.accountId);
mNewConn = mApp.getConnection(mNewAccount.providerId, mNewAccount.accountId);

while (mNewConn.getState() != ImConnection.LOGGED_IN) {
try {
Thread.sleep(500);
} catch (Exception e) {
while (mNewConn.getState() != ImConnection.LOGGED_IN) {
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}

String inviteLink = OnboardingManager.generateInviteLink(mContext, newJabberId, fingerprint, nickname,true);
String inviteLink = OnboardingManager.generateInviteLink(mContext, newJabberId, fingerprint, nickname, true);

String migrateMessage = mContext.getString(R.string.migrate_message) + ' ' + inviteLink;
IChatSessionManager sessionMgr = mConn.getChatSessionManager();
IContactListManager clManager = mConn.getContactListManager();
List<IContactList> listOfLists = clManager.getContactLists();
String migrateMessage = mContext.getString(R.string.migrate_message) + ' ' + inviteLink;
IChatSessionManager sessionMgr = mConn.getChatSessionManager();
IContactListManager clManager = mConn.getContactListManager();
List<IContactList> listOfLists = clManager.getContactLists();

if (loggedInToOldAccount) {
if (loggedInToOldAccount) {

for (IContactList contactList : listOfLists) {
String[] contacts = contactList.getContacts();
for (IContactList contactList : listOfLists) {
String[] contacts = contactList.getContacts();

for (String contact : contacts) {
mContacts.add(contact);
for (String contact : contacts) {
mContacts.add(contact);

IChatSession session = sessionMgr.getChatSession(contact);
IChatSession session = sessionMgr.getChatSession(contact);

if (session == null) {
session = sessionMgr.createChatSession(contact, true);
}
if (session == null) {
session = sessionMgr.createChatSession(contact, true);
}

if (!session.isEncrypted()) {
//try to kick off some encryption here
session.getDefaultOtrChatSession().startChatEncryption();
try {
Thread.sleep(500);
} //just wait a half second here?
catch (Exception e) {
if (!session.isEncrypted()) {
//try to kick off some encryption here
session.getDefaultOtrChatSession().startChatEncryption();
try {
Thread.sleep(500);
} //just wait a half second here?
catch (Exception e) {
}
}
}

session.sendMessage(migrateMessage, false);
session.sendMessage(migrateMessage, false);


//archive existing contact
clManager.archiveContact(contact, session.isGroupChatSession() ? Imps.Contacts.TYPE_NORMAL : Imps.Contacts.TYPE_GROUP, true);
}

//archive existing contact
clManager.archiveContact(contact,session.isGroupChatSession() ? Imps.Contacts.TYPE_NORMAL : Imps.Contacts.TYPE_GROUP, true);
}
} else {
String[] offlineAddresses = clManager.getOfflineAddresses();

for (String address : offlineAddresses) {
mContacts.add(address);
clManager.archiveContact(address, Imps.Contacts.TYPE_NORMAL, true);
}
}
}
else
{
String[] offlineAddresses = clManager.getOfflineAddresses();

for (String address : offlineAddresses) {
mContacts.add(address);
clManager.archiveContact(address, Imps.Contacts.TYPE_NORMAL, true);
for (String contact : mContacts) {
addToContactList(mNewConn, contact, keyMan.getRemoteFingerprint(contact), null);
}
}

for (String contact : mContacts) {
addToContactList(mNewConn, contact, keyMan.getRemoteFingerprint(contact), null);
}

if (loggedInToOldAccount) {
//archive existing conversations and contacts
List<IChatSession> listSession = mConn.getChatSessionManager().getActiveChatSessions();
for (IChatSession session : listSession) {
session.leave();
if (loggedInToOldAccount) {
//archive existing conversations and contacts
List<IChatSession> listSession = mConn.getChatSessionManager().getActiveChatSessions();
for (IChatSession session : listSession) {
session.leave();
}
mConn.broadcastMigrationIdentity(newJabberId);
}
mConn.broadcastMigrationIdentity(newJabberId);
}

migrateAvatars(username, newJabberId);
mApp.setDefaultAccount(mNewAccount.providerId, mNewAccount.accountId);
migrateAvatars(username, newJabberId);
mApp.setDefaultAccount(mNewAccount.providerId, mNewAccount.accountId);

//logout of existing account
setKeepSignedIn(mAccountId, false);
//logout of existing account
setKeepSignedIn(mAccountId, false);

if (loggedInToOldAccount)
mConn.logout();
if (loggedInToOldAccount)
mConn.logout();

return mNewAccount;
return mNewAccount;

}
catch (Exception e)
{
Log.e(ImApp.LOG_TAG,"error with migration",e);
} catch (Exception e) {
Log.e(ImApp.LOG_TAG, "error with migration", e);
}
}

//failed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import im.zom.messenger.R;

import org.awesomeapp.messenger.ImApp;
import org.awesomeapp.messenger.model.Server;
import org.awesomeapp.messenger.provider.Imps;
import org.awesomeapp.messenger.service.ImServiceConstants;
import org.awesomeapp.messenger.tasks.MigrateAccountTask;
Expand Down Expand Up @@ -121,7 +122,7 @@ private void migrateAccountConfirmed ()

mIsMigrating = true;

String domain = "home.zom.im";
Server[] servers = Server.getServers(this);
final ProgressDialog progress = new ProgressDialog(this);
progress.setIndeterminate(true);
progress.setTitle(R.string.upgrade_progress_action);
Expand All @@ -144,7 +145,7 @@ public void migrateFailed(long providerId, long accountId) {

}
});
maTask.execute(domain);
maTask.execute(servers);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ private void checkUpgrade ()

}


private MigrateAccountTask.MigrateAccountListener mMigrateTaskListener;

private synchronized void doUpgrade () {
Expand Down Expand Up @@ -691,7 +692,7 @@ public void onClick(View view) {
mUpgradeImage.setImageResource(R.drawable.olo_thinking);


((ImApp) getActivity().getApplication()).doUpgrade(getActivity(), "home.zom.im", mMigrateTaskListener);
((ImApp) getActivity().getApplication()).doUpgrade(getActivity(), mMigrateTaskListener);

}
}
Expand Down

0 comments on commit 835c96e

Please sign in to comment.