Skip to content

Commit

Permalink
Merge pull request #8115 from wordpress-mobile/fix/facebook-publicize…
Browse files Browse the repository at this point in the history
…-aug-1

Facebook Publicize disable selection of profiles
  • Loading branch information
mzorz authored Jul 31, 2018
2 parents e3ccec6 + b620788 commit f811294
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.wordpress.android.datasets.NotificationsTable;
import org.wordpress.android.datasets.PeopleTable;
import org.wordpress.android.datasets.PublicizeTable;
import org.wordpress.android.datasets.SiteSettingsTable;
import org.wordpress.android.datasets.SuggestionTable;
import org.wordpress.android.models.SiteSettingsModel;
Expand All @@ -21,7 +22,7 @@
import java.io.OutputStream;

public class WordPressDB {
private static final int DATABASE_VERSION = 65;
private static final int DATABASE_VERSION = 66;


// Warning if you rename DATABASE_NAME, that could break previous App backups (see: xml/backup_scheme.xml)
Expand Down Expand Up @@ -170,6 +171,9 @@ public WordPressDB(Context ctx) {
case 64:
// add site icon
mDb.execSQL(SiteSettingsModel.ADD_SITE_ICON);
case 65:
// add external users only to publicize services table
PublicizeTable.resetServicesTable(mDb);
}
mDb.setVersion(DATABASE_VERSION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void createTables(SQLiteDatabase db) {
+ " connect_url TEXT NOT NULL,"
+ " is_jetpack_supported INTEGER DEFAULT 0,"
+ " is_multi_user_id_supported INTEGER DEFAULT 0,"
+ " is_external_users_only INTEGER DEFAULT 0,"
+ " PRIMARY KEY (id))");

db.execSQL("CREATE TABLE IF NOT EXISTS " + CONNECTIONS_TABLE + " ("
Expand Down Expand Up @@ -55,13 +56,8 @@ private static SQLiteDatabase getWritableDb() {
return WordPress.wpDB.getDatabase();
}

/*
* for testing purposes - clears then recreates tables
*/
public static void reset() {
getWritableDb().execSQL("DROP TABLE IF EXISTS " + SERVICES_TABLE);
getWritableDb().execSQL("DROP TABLE IF EXISTS " + CONNECTIONS_TABLE);
createTables(getWritableDb());
public static void resetServicesTable(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + SERVICES_TABLE);
}

public static PublicizeService getService(String serviceId) {
Expand Down Expand Up @@ -111,8 +107,9 @@ public static void setServiceList(final PublicizeServiceList serviceList) {
+ " icon_url," // 5
+ " connect_url," // 6
+ " is_jetpack_supported," // 7
+ " is_multi_user_id_supported)" // 8
+ " VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)");
+ " is_multi_user_id_supported," // 8
+ " is_external_users_only)" // 9
+ " VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)");
for (PublicizeService service : serviceList) {
stmt.bindString(1, service.getId());
stmt.bindString(2, service.getLabel());
Expand All @@ -122,6 +119,7 @@ public static void setServiceList(final PublicizeServiceList serviceList) {
stmt.bindString(6, service.getConnectUrl());
stmt.bindLong(7, SqlUtils.boolToSql(service.isJetpackSupported()));
stmt.bindLong(8, SqlUtils.boolToSql(service.isMultiExternalUserIdSupported()));
stmt.bindLong(9, SqlUtils.boolToSql(service.isExternalUsersOnly()));
stmt.executeInsert();
}

Expand All @@ -132,6 +130,11 @@ public static void setServiceList(final PublicizeServiceList serviceList) {
}
}

private static boolean getBooleanFromCursor(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
return columnIndex != -1 && cursor.getInt(columnIndex) != 0;
}

private static PublicizeService getServiceFromCursor(Cursor c) {
PublicizeService service = new PublicizeService();

Expand All @@ -141,12 +144,23 @@ private static PublicizeService getServiceFromCursor(Cursor c) {
service.setGenericon(c.getString(c.getColumnIndex("genericon")));
service.setIconUrl(c.getString(c.getColumnIndex("icon_url")));
service.setConnectUrl(c.getString(c.getColumnIndex("connect_url")));
service.setIsJetpackSupported(SqlUtils.sqlToBool(c.getColumnIndex("is_jetpack_supported")));
service.setIsMultiExternalUserIdSupported(SqlUtils.sqlToBool(c.getColumnIndex("is_multi_user_id_supported")));
service.setIsJetpackSupported(getBooleanFromCursor(c, "is_jetpack_supported"));
service.setIsMultiExternalUserIdSupported(getBooleanFromCursor(c, "is_multi_user_id_supported"));
service.setIsExternalUsersOnly(getBooleanFromCursor(c, "is_external_users_only"));

return service;
}

public static boolean onlyExternalConnections(String serviceId) {
if (serviceId == null && serviceId.isEmpty()) {
return false;
}

String sql = "SELECT is_external_users_only FROM " + SERVICES_TABLE + " WHERE id=?";
String[] args = {serviceId};
return SqlUtils.boolForQuery(getReadableDb(), sql, args);
}

public static String getConnectUrlForService(String serviceId) {
if (TextUtils.isEmpty(serviceId)) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public String toString() {
public String toString() {
return "broken";
}
},
MUST_DISCONNECT {
public String toString() {
return "must-disconnect";
}
}
}

Expand Down Expand Up @@ -126,6 +131,8 @@ public void setStatus(String status) {
public ConnectStatus getStatusEnum() {
if (getStatus().equalsIgnoreCase(ConnectStatus.BROKEN.toString())) {
return ConnectStatus.BROKEN;
} else if (getStatus().equalsIgnoreCase(ConnectStatus.MUST_DISCONNECT.toString())) {
return ConnectStatus.MUST_DISCONNECT;
} else {
return ConnectStatus.OK;
}
Expand Down Expand Up @@ -231,6 +238,16 @@ public static PublicizeConnection fromJson(JSONObject json) {
return connection;
}

public static void updateConnectionfromExternalJson(PublicizeConnection connection, JSONObject json) {
if (connection == null) {
return;
}

connection.mExternalId = json.optString("external_ID");
connection.mExternalName = json.optString("external_name");
connection.mExternalProfilePictureUrl = json.optString("external_profile_picture");
}

private static long[] getSitesArrayFromJson(JSONArray jsonArray) throws JSONException {
long[] sitesArray = new long[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PublicizeService {
private String mGenericon;
private String mIconUrl;
private String mConnectUrl;
private boolean mIsExternalUsersOnly;

private boolean mIsJetpackSupported;
private boolean mIsMultiExternalUserIdSupported;
Expand Down Expand Up @@ -77,6 +78,14 @@ public void setIsMultiExternalUserIdSupported(boolean supported) {
mIsMultiExternalUserIdSupported = supported;
}

public boolean isExternalUsersOnly() {
return mIsExternalUsersOnly;
}

public void setIsExternalUsersOnly(boolean isExternalUsersOnly) {
mIsExternalUsersOnly = isExternalUsersOnly;
}

public boolean isSameAs(PublicizeService other) {
return other != null
&& other.getId().equals(this.getId())
Expand All @@ -85,6 +94,7 @@ public boolean isSameAs(PublicizeService other) {
&& other.getGenericon().equals(this.getGenericon())
&& other.getIconUrl().equals(this.getIconUrl())
&& other.getConnectUrl().equals(this.getConnectUrl())
&& other.isExternalUsersOnly() == this.isExternalUsersOnly()
&& other.isJetpackSupported() == this.isJetpackSupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ public boolean isSameAs(PublicizeServiceList otherList) {
/*
* passed JSON is the response from /meta/external-services?type=publicize
"services": {
"facebook": {
"ID": "facebook",
"label": "Facebook",
"type": "publicize",
"description": "Publish your posts to your Facebook timeline or page.",
"genericon": {
"class": "facebook-alt",
"unicode": "\\f203"
},
"icon": "http://i.wordpress.com/wp-content/admin-plugins/publicize/assets/publicize-fb-2x.png",
"connect_URL": "https://public-api.wordpress.com/connect/?action=request
&kr_nonce=b2c86a0cdb&nonce=94557d1529&for=connect&service=facebook&kr_blog_nonce=5e399375f1
&magic=keyring&blog=52451191",
"multiple_external_user_ID_support": true,
"jetpack_support": true,
"jetpack_module_required": "publicize"
"facebook":{
"ID":"facebook",
"label":"Facebook",
"type":"publicize",
"description":"Publish your posts to your Facebook timeline or page.",
"genericon":{
"class":"facebook-alt",
"unicode":"\\f203"
},
"icon":"http:\/\/i.wordpress.com\/wp-content\/admin-plugins\/publicize\/assets\/publicize-fb-2x.png",
"connect_URL":"https:\/\/public-api.wordpress.com\/connect\/?action=request&kr_nonce=a1e2ad2b80
&nonce=c4b69a25c1&for=connect&service=facebook&kr_blog_nonce=0ae2027be9&magic=keyring&blog=90298630",
"multiple_external_user_ID_support":true,
"external_users_only":true,
"jetpack_support":true,
"jetpack_module_required":"publicize"
},
...
*/
Expand Down Expand Up @@ -84,6 +84,7 @@ public static PublicizeServiceList fromJson(JSONObject json) {

service.setIsJetpackSupported(jsonService.optBoolean("jetpack_support"));
service.setIsMultiExternalUserIdSupported(jsonService.optBoolean("multiple_external_user_ID_support"));
service.setIsExternalUsersOnly(jsonService.optBoolean("external_users_only"));

JSONObject jsonGenericon = jsonService.optJSONObject("genericon");
if (jsonGenericon != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.json.JSONObject;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.datasets.PublicizeTable;
import org.wordpress.android.fluxc.model.SiteModel;
import org.wordpress.android.models.PublicizeConnection;
import org.wordpress.android.models.PublicizeService;
import org.wordpress.android.util.ToastUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -104,8 +106,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
int keychainId = mNotConnectedAccounts.get(mSelectedIndex).connectionId;
String service = mNotConnectedAccounts.get(mSelectedIndex).getService();
String externalUserId = mNotConnectedAccounts.get(mSelectedIndex).getExternalId();
EventBus.getDefault().post(new PublicizeEvents.ActionAccountChosen(mSite.getSiteId(), keychainId,
service));
service, externalUserId));
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
Expand Down Expand Up @@ -145,12 +148,27 @@ private void addConnectionsToLists(String jsonString) {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("connections");
for (int i = 0; i < jsonArray.length(); i++) {
PublicizeConnection connection = PublicizeConnection.fromJson(jsonArray.getJSONObject(i));
JSONObject currentConnectionJson = jsonArray.getJSONObject(i);
PublicizeConnection connection = PublicizeConnection.fromJson(currentConnectionJson);
if (connection.getService().equals(mServiceId)) {
if (connection.isInSite(mSite.getSiteId())) {
mConnectedAccounts.add(connection);
} else {
mNotConnectedAccounts.add(connection);
PublicizeService service = PublicizeTable.getService(mServiceId);
if (service != null && !service.isExternalUsersOnly()) {
if (connection.isInSite(mSite.getSiteId())) {
mConnectedAccounts.add(connection);
} else {
mNotConnectedAccounts.add(connection);
}
}

JSONArray externalJsonArray = currentConnectionJson.getJSONArray("additional_external_users");
for (int j = 0; j < externalJsonArray.length(); j++) {
JSONObject currentExternalConnectionJson = externalJsonArray.getJSONObject(j);
PublicizeConnection.updateConnectionfromExternalJson(connection, currentExternalConnectionJson);
if (connection.isInSite(mSite.getSiteId())) {
mConnectedAccounts.add(connection);
} else {
mNotConnectedAccounts.add(connection);
}
}
}
}
Expand All @@ -160,6 +178,10 @@ private void addConnectionsToLists(String jsonString) {
}

private void configureConnectionName() {
if (mNotConnectedAccounts.isEmpty()) {
return;
}

PublicizeConnection connection = mNotConnectedAccounts.get(0);
if (connection != null) {
mConnectionName = connection.getLabel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onBindViewHolder(final ViewHolder holder, int position) {
final PublicizeConnection connection = mConnectionItems.get(position);
holder.mProfileImageView
.setImageUrl(connection.getExternalProfilePictureUrl(), WPNetworkImageView.ImageType.PHOTO);
holder.mNameTextView.setText(connection.getExternalDisplayName());
holder.mNameTextView.setText(getName(connection));
holder.mRadioButton.setChecked(position == mSelectedPosition);

if (!mAreAccountsConnected) {
Expand Down Expand Up @@ -82,4 +82,14 @@ public ViewHolder(View view) {
public interface OnPublicizeAccountChooserListener {
void onAccountSelected(int selectedIndex);
}

private String getName(PublicizeConnection connection) {
String name = connection.getExternalName();

if (name.isEmpty()) {
name = connection.getExternalDisplayName();
}

return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void onResponse(JSONObject jsonObject) {
.post(new PublicizeEvents.ActionRequestChooseAccount(siteId, serviceId, jsonObject));
} else {
long keyringConnectionId = parseServiceKeyringId(serviceId, currentUserId, jsonObject);
connectStepTwo(siteId, keyringConnectionId, serviceId);
connectStepTwo(siteId, keyringConnectionId, serviceId, "");
}
}
};
Expand All @@ -113,7 +113,8 @@ public void onErrorResponse(VolleyError volleyError) {
* step two in creating a publicize connection: now that we have the keyring connection id,
* create the actual connection
*/
public static void connectStepTwo(final long siteId, long keyringConnectionId, final String serviceId) {
public static void connectStepTwo(final long siteId, long keyringConnectionId,
final String serviceId, final String externalUserId) {
RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
Expand All @@ -133,27 +134,40 @@ public void onErrorResponse(VolleyError volleyError) {

Map<String, String> params = new HashMap<>();
params.put("keyring_connection_ID", Long.toString(keyringConnectionId));
if (!externalUserId.isEmpty()) {
params.put("external_user_ID", externalUserId);
}
String path = String.format(Locale.ROOT, "/sites/%d/publicize-connections/new", siteId);
WordPress.getRestClientUtilsV1_1().post(path, params, null, listener, errorListener);
}

private static boolean shouldShowChooserDialog(long siteId, String serviceId, JSONObject jsonObject) {
JSONArray jsonConnectionList = jsonObject.optJSONArray("connections");

if (jsonConnectionList == null || jsonConnectionList.length() <= 1) {
return false;
}

int totalAccounts = 0;
int totalExternalAccounts = 0;
try {
for (int i = 0; i < jsonConnectionList.length(); i++) {
JSONObject connectionObject = jsonConnectionList.getJSONObject(i);
PublicizeConnection publicizeConnection = PublicizeConnection.fromJson(connectionObject);
if (publicizeConnection.getService().equals(serviceId) && !publicizeConnection.isInSite(siteId)) {
totalAccounts++;
JSONArray externalJsonArray = connectionObject.getJSONArray("additional_external_users");
for (int j = 0; j < externalJsonArray.length(); j++) {
totalExternalAccounts++;
}
}
}

return totalAccounts > 0;
if (PublicizeTable.onlyExternalConnections(serviceId)) {
return totalExternalAccounts > 0;
} else {
return totalAccounts > 0 || totalExternalAccounts > 0;
}
} catch (JSONException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class PublicizeConstants {
public static final String ARG_CONNECTION_ARRAY_JSON = "connection_array_json";

public static final String GOOGLE_PLUS_ID = "google_plus";
public static final String FACEBOOK_ID = "facebook";

public enum ConnectAction {
CONNECT,
Expand Down
Loading

0 comments on commit f811294

Please sign in to comment.