Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions app/src/main/java/org/piwigo/accounts/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class UserManager {
@VisibleForTesting static final String KEY_IS_GUEST = "is_guest";
@VisibleForTesting static final String KEY_SITE_URL = "url";
@VisibleForTesting static final String KEY_USERNAME = "username";
@VisibleForTesting static final String KEY_STATUS = "status";
@VisibleForTesting static final String KEY_IS_FAKED_BY_COMMUNITY = "is_faked_by_community";
@VisibleForTesting static final String KEY_COOKIE = "cookie";
@VisibleForTesting static final String KEY_TOKEN = "token";

Expand Down Expand Up @@ -103,12 +105,12 @@ public boolean userExists(String siteUrl, String username) {
return getAccountForUser(siteUrl, username) != null;
}

public Account createUser(String siteUrl, String username, String password, String cookie, String token) {
public Account createUser(String siteUrl, String username, String password, String status, String cookie, String token) {
Account result;
if (TextUtils.isEmpty(username) && TextUtils.isEmpty(password)) {
result = createGuestUser(siteUrl);
} else {
result = createNormalUser(siteUrl, username, password, cookie, token);
result = createNormalUser(siteUrl, username, status, password, cookie, token);
}
accountManager.setUserData(result, KEY_TOKEN, token);

Expand Down Expand Up @@ -161,6 +163,24 @@ public String getToken(Account account) {
return accountManager.getUserData(account, KEY_TOKEN);
}

public void setStatus(Account account, String status) {
accountManager.setUserData(account, KEY_STATUS, status);
}

public String getStatus(Account account)
{
return accountManager.getUserData(account, KEY_STATUS);
}

public void setFakedByCommunity(Account account, String fakedByCommunity) {
accountManager.setUserData(account, KEY_IS_FAKED_BY_COMMUNITY, fakedByCommunity);
}

public boolean isFakedByCommunity(Account account)
{
return Boolean.getBoolean(accountManager.getUserData(account, KEY_IS_FAKED_BY_COMMUNITY));
}

public boolean isGuest(Account account) {
return GUEST_ACCOUNT_NAME.equals(getUsername(account));
}
Expand All @@ -178,13 +198,14 @@ private String getAccountName(String siteUrl, String username) {
return resources.getString(R.string.account_name, username, sitename.toLowerCase(Locale.ROOT));
}

private Account createNormalUser(String siteUrl, String username, String password, String cookie, String token) {
private Account createNormalUser(String siteUrl, String username, String password, String status, String cookie, String token) {
String accountName = getAccountName(siteUrl, username);
Account account = new Account(accountName, resources.getString(R.string.account_type));
Bundle userdata = new Bundle();
userdata.putString(KEY_IS_GUEST, Boolean.toString(false));
userdata.putString(KEY_SITE_URL, siteUrl);
userdata.putString(KEY_USERNAME, username);
userdata.putString(KEY_STATUS, status);
userdata.putString(KEY_COOKIE, cookie);
userdata.putString(KEY_TOKEN, token);
accountManager.addAccountExplicitly(account, password, userdata);
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/org/piwigo/io/RestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import org.piwigo.io.model.AddCategoryResponse;
import org.piwigo.io.model.CategoryListResponse;
import org.piwigo.io.model.CommunityStatusResponse;
import org.piwigo.io.model.GetImageInfoResponse;
import org.piwigo.io.model.ImageListResponse;
import org.piwigo.io.model.ImageUploadResponse;
import org.piwigo.io.model.MethodListResponse;
import org.piwigo.io.model.StatusResponse;
import org.piwigo.io.model.SuccessResponse;

Expand Down Expand Up @@ -64,7 +66,8 @@ public interface RestService {

@GET("ws.php?method=pwg.categories.getList") Observable<CategoryListResponse> getCategories(
@Query("cat_id") Integer categoryId,
@Query("thumbnail_size") String thumbnailSize
@Query("thumbnail_size") String thumbnailSize,
@Query("faked_by_community") boolean fakedByCommunity
);

@GET("ws.php?method=pwg.images.getInfo") Observable<GetImageInfoResponse> getImageInfo(
Expand All @@ -84,4 +87,10 @@ Call<ImageUploadResponse> uploadImage(
@Part MultipartBody.Part filePart
);

@GET("ws.php?method=reflection.getMethodList") Observable<MethodListResponse> getMethodList();

@GET("ws.php?method=community.session.getStatus") Observable<CommunityStatusResponse> getCommunitySessionStatus();

@GET("ws.php?method=community.session.getStatus") Observable<CommunityStatusResponse> getCommunitySessionStatus(@Header("Cookie") String pwgIdCookie);

}
19 changes: 19 additions & 0 deletions app/src/main/java/org/piwigo/io/model/CommunityStatusResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.piwigo.io.model;

import com.google.gson.annotations.SerializedName;

public class CommunityStatusResponse {

@SerializedName("stat") public String stat;

@SerializedName("result") public Result result;

public class Result {

@SerializedName("real_user_status") public String status;

@SerializedName("upload_categories_getList_method") public String uploadCategoriesGetListMethod;

}

}
2 changes: 2 additions & 0 deletions app/src/main/java/org/piwigo/io/model/LoginResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public class LoginResponse {

public StatusResponse statusResponse;

public CommunityStatusResponse communityStatusResponse;

}
20 changes: 20 additions & 0 deletions app/src/main/java/org/piwigo/io/model/MethodListResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.piwigo.io.model;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class MethodListResponse
{

@SerializedName("stat") public String stat;

@SerializedName("result") public Result result;

public class Result {

@SerializedName("methods") public List<String> methods = new ArrayList<>();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public class CategoriesRepository extends BaseRepository {
super(restServiceFactory, ioScheduler, uiScheduler, userManager);
}

public Observable<List<Category>> getCategories(Account account, @Nullable Integer categoryId, String thumbnailSize) {
public Observable<List<Category>> getCategories(Account account, @Nullable Integer categoryId, String thumbnailSize, boolean fakedByCommunity) {
RestService restService = restServiceFactory.createForAccount(account);
/* TODO: make thumbnail Size configurable, also check for ImageRepository, whether it can reduce the amount of REST/JSON traffic */
return restService.getCategories(categoryId, thumbnailSize)
return restService.getCategories(categoryId, thumbnailSize, fakedByCommunity)
// .flatMap(response -> Observable.from(response.result.categories))
.compose(applySchedulers())
.flatMap(response -> {
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/org/piwigo/io/repository/MethodsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.piwigo.io.repository;

import android.accounts.Account;

import org.piwigo.accounts.UserManager;
import org.piwigo.io.RestService;
import org.piwigo.io.RestServiceFactory;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import rx.Observable;
import rx.Scheduler;

public class MethodsRepository extends BaseRepository {
@Inject
MethodsRepository(RestServiceFactory restServiceFactory, @Named("IoScheduler") Scheduler ioScheduler, @Named("UiScheduler") Scheduler uiScheduler, UserManager userManager) {
super(restServiceFactory, ioScheduler, uiScheduler, userManager);
}

public Observable<List<String>> getMethodList(Account account) {
RestService restService = restServiceFactory.createForAccount(account);

return restService
.getMethodList()
.compose(applySchedulers())
.map(methodListResponse -> {
if (methodListResponse.result != null) {
return methodListResponse.result.methods;
} else {
return null;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public Observable<LoginResponse> login(String url, String username, String passw
loginResponse.statusResponse = statusResponse;
return loginResponse;
})
.flatMap(communityResponse -> restService.getCommunitySessionStatus("pwg_id=" + loginResponse.pwgId).compose(applySchedulers()))
.map(communityStatusResponse -> {
loginResponse.communityStatusResponse = communityStatusResponse;
return loginResponse;
})
;
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/piwigo/ui/login/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void loginSuccess(LoginResponse response) {
Snackbar.make(binding.getRoot(), R.string.login_account_error, Snackbar.LENGTH_LONG)
.show();
} else {
Account account = userManager.createUser(response.url, response.statusResponse.result.username, response.password, response.pwgId, response.statusResponse.result.pwgToken);
Account account = userManager.createUser(response.url, response.statusResponse.result.username, response.password, response.communityStatusResponse.result.status, response.pwgId, response.statusResponse.result.pwgToken);
userManager.setActiveAccount(account);
setResultIntent(account);
finish();
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/org/piwigo/ui/main/AlbumsViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,18 @@ private void forcedLoadAlbums(){
photosSubscription = null;
}
if (account != null) {
albumsSubscription = categoriesRepository.getCategories(account, category,
preferences.getString(PreferencesRepository.KEY_PREF_DOWNLOAD_SIZE))
albumsSubscription = categoriesRepository.getCategories(account,
category,
preferences.getString(PreferencesRepository.KEY_PREF_DOWNLOAD_SIZE),
userManager.isFakedByCommunity(account))
.subscribe(new CategoriesSubscriber());
photosSubscription = imageRepository.getImages(account, category)
.subscribe(new ImagesSubscriber());
}
}

void loadAlbums(Integer categoryId) {
if(category == null || category != category) {
if(category == null) {
category = categoryId;
forcedLoadAlbums();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/piwigo/ui/main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.piwigo.io.model.ImageUploadItem;
import org.piwigo.io.model.LoginResponse;
import org.piwigo.io.model.SuccessResponse;
import org.piwigo.io.repository.MethodsRepository;
import org.piwigo.io.repository.UserRepository;
import org.piwigo.ui.about.AboutActivity;
import org.piwigo.ui.about.PrivacyPolicyActivity;
Expand Down
39 changes: 38 additions & 1 deletion app/src/main/java/org/piwigo/ui/main/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@

import org.piwigo.R;
import org.piwigo.accounts.UserManager;
import org.piwigo.io.model.MethodListResponse;
import org.piwigo.io.model.SuccessResponse;
import org.piwigo.io.repository.MethodsRepository;
import org.piwigo.io.repository.UserRepository;

import java.util.List;

public class MainViewModel extends ViewModel {
// TODO: cleanup here...
public static int STAT_OFFLINE = 0;
Expand Down Expand Up @@ -69,12 +73,14 @@ LiveData<Throwable> getLogoutError() {
// TODO: finish loginstatus
public ObservableInt loginStatus = new ObservableInt(STAT_OFFLINE);
public ObservableField<String> piwigoVersion = new ObservableField<>("");
public ObservableBoolean communityEnabled = new ObservableBoolean(false);

private MutableLiveData<Integer> selectedNavigationItemId = new MutableLiveData<>();
private MethodsRepository mMethodsRepository;
private UserRepository mUserRepository;
private UserManager userManager;

MainViewModel(UserManager userManager, UserRepository userRepository) {
MainViewModel(UserManager userManager, UserRepository userRepository, MethodsRepository methodsRepository) {
Account account = userManager.getActiveAccount().getValue();
this.userManager = userManager;
if (account != null) {
Expand All @@ -83,6 +89,7 @@ LiveData<Throwable> getLogoutError() {
displayFab.set(!userManager.isGuest(account));
}
mUserRepository = userRepository;
mMethodsRepository = methodsRepository;

navigationItemId.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {

Expand All @@ -92,6 +99,11 @@ public void onPropertyChanged(Observable sender, int propertyId) {
drawerState.set(false);
}
});

if (userManager.getActiveAccount().getValue() != null) {
mMethodsRepository.getMethodList(userManager.getActiveAccount().getValue())
.subscribe(new MainViewModel.MethodsSubscriber());
}
}

LiveData<Integer> getSelectedNavigationItemId() {
Expand All @@ -109,6 +121,31 @@ public void onLogoutClick() {
}
}

private class MethodsSubscriber extends Subscriber<List<String>> {

@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
Log.e(TAG, e.getMessage());
}

@Override
public void onNext(List<String> methods) {
if (methods.contains("community.session.getStatus")) {
//Here we put "false" as Piwigo expects this value to be false when Community is enabled..
Log.e("MVM", "Community method found.");
userManager.setFakedByCommunity(userManager.getActiveAccount().getValue(), "false");
} else {
Log.e("MVM", "Community method not found.");
userManager.setFakedByCommunity(userManager.getActiveAccount().getValue(), "true");
}
}
}


private class LogoutSubscriber extends Subscriber<SuccessResponse> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.lifecycle.ViewModelProvider;

import org.piwigo.accounts.UserManager;
import org.piwigo.io.repository.MethodsRepository;
import org.piwigo.io.repository.UserRepository;

import javax.inject.Inject;
Expand All @@ -32,16 +33,18 @@ public class MainViewModelFactory implements ViewModelProvider.Factory {

private final UserManager userManager;
private final UserRepository userRepository;
private final MethodsRepository methodsRepository;

@Inject public MainViewModelFactory(UserManager userManager, UserRepository userRepository) {
@Inject public MainViewModelFactory(UserManager userManager, UserRepository userRepository, MethodsRepository methodsRepository) {
this.userManager = userManager;
this.userRepository = userRepository;
this.methodsRepository = methodsRepository;
}

@Override public <T extends ViewModel> T create(Class<T> viewModelClass) {
if (viewModelClass.isAssignableFrom(MainViewModel.class)) {
//noinspection unchecked
return (T) new MainViewModel(userManager, userRepository);
return (T) new MainViewModel(userManager, userRepository, methodsRepository);
}
throw new IllegalStateException("Unable to create " + viewModelClass.getName());
}
Expand Down