Replace Nextcloud News /user depreciated api call by /ocs/v1.php/cloud/users/{userId}
This commit is contained in:
parent
1cba1b6e17
commit
f2607c307c
@ -11,7 +11,7 @@ public abstract class Credentials {
|
||||
|
||||
private final String authorization;
|
||||
|
||||
private final String url;
|
||||
private String url;
|
||||
|
||||
public Credentials(String authorization, String url) {
|
||||
this.authorization = authorization;
|
||||
@ -26,6 +26,10 @@ public abstract class Credentials {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public static Credentials toCredentials(Account account) {
|
||||
String endPoint = getEndPoint(account.getAccountType());
|
||||
|
||||
|
@ -7,9 +7,9 @@ import androidx.annotation.Nullable;
|
||||
|
||||
import com.readrops.api.services.SyncResult;
|
||||
import com.readrops.api.services.SyncType;
|
||||
import com.readrops.api.services.nextcloudnews.json.NextNewsUser;
|
||||
import com.readrops.api.utils.exceptions.ConflictException;
|
||||
import com.readrops.api.services.nextcloudnews.adapters.NextNewsUserAdapter;
|
||||
import com.readrops.api.utils.ApiUtils;
|
||||
import com.readrops.api.utils.exceptions.ConflictException;
|
||||
import com.readrops.api.utils.exceptions.UnknownFormatException;
|
||||
import com.readrops.db.entities.Feed;
|
||||
import com.readrops.db.entities.Folder;
|
||||
@ -23,6 +23,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class NextNewsDataSource {
|
||||
@ -38,13 +39,17 @@ public class NextNewsDataSource {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public NextNewsUser login() throws IOException {
|
||||
Response<NextNewsUser> response = api.getUser().execute();
|
||||
public String login(String user) throws IOException {
|
||||
Response<ResponseBody> response = api.getUser(user).execute();
|
||||
|
||||
if (!response.isSuccessful())
|
||||
if (!response.isSuccessful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.body();
|
||||
String displayName = new NextNewsUserAdapter().fromXml(response.body().byteStream());
|
||||
response.body().close();
|
||||
|
||||
return displayName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@ -3,7 +3,6 @@ package com.readrops.api.services.nextcloudnews;
|
||||
import com.readrops.db.entities.Feed;
|
||||
import com.readrops.db.entities.Folder;
|
||||
import com.readrops.db.entities.Item;
|
||||
import com.readrops.api.services.nextcloudnews.json.NextNewsUser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -13,6 +12,7 @@ import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.DELETE;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Headers;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.PUT;
|
||||
import retrofit2.http.Path;
|
||||
@ -22,8 +22,9 @@ public interface NextNewsService {
|
||||
|
||||
String END_POINT = "/index.php/apps/news/api/v1-2/";
|
||||
|
||||
@GET("user")
|
||||
Call<NextNewsUser> getUser();
|
||||
@GET("/ocs/v1.php/cloud/users/{userId}")
|
||||
@Headers("OCS-APIRequest: true")
|
||||
Call<ResponseBody> getUser(@Path("userId") String userId);
|
||||
|
||||
@GET("folders")
|
||||
Call<List<Folder>> getFolders();
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.readrops.api.services.nextcloudnews.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.core.net.toUri
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import com.readrops.api.utils.extensions.nextNonEmptyString
|
||||
import com.readrops.api.utils.extensions.nextNullableInt
|
||||
@ -9,6 +10,7 @@ import com.readrops.db.entities.Feed
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.JsonReader
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.net.URI
|
||||
|
||||
class NextNewsFeedsAdapter {
|
||||
|
||||
@ -47,7 +49,7 @@ class NextNewsFeedsAdapter {
|
||||
when (reader.selectName(NAMES)) {
|
||||
0 -> remoteId = reader.nextNonEmptyString()
|
||||
1 -> url = reader.nextNonEmptyString()
|
||||
2 -> name = reader.nextNonEmptyString()
|
||||
2 -> name = reader.nextNullableString()
|
||||
3 -> iconUrl = reader.nextNullableString()
|
||||
4 -> {
|
||||
val nextInt = reader.nextNullableInt()
|
||||
@ -59,6 +61,8 @@ class NextNewsFeedsAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
if (feed.name == null) feed.name = URI.create(feed.url).host
|
||||
|
||||
feeds += feed
|
||||
reader.endObject()
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.readrops.api.services.nextcloudnews.adapters
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.allChildrenAutoIgnore
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import com.readrops.api.localfeed.XmlAdapter
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import com.readrops.api.utils.extensions.nonNullText
|
||||
import java.io.InputStream
|
||||
|
||||
class NextNewsUserAdapter : XmlAdapter<String> {
|
||||
|
||||
override fun fromXml(inputStream: InputStream): String {
|
||||
val konsumer = inputStream.konsumeXml()
|
||||
var displayName: String? = null
|
||||
|
||||
return try {
|
||||
konsumer.child("ocs") {
|
||||
allChildrenAutoIgnore("data") {
|
||||
allChildrenAutoIgnore("displayname") {
|
||||
displayName = nonNullText()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
konsumer.close()
|
||||
displayName!!
|
||||
} catch (e: Exception) {
|
||||
throw ParseException(e.message)
|
||||
}
|
||||
}
|
||||
}
|
@ -34,6 +34,7 @@ class NextNewsFeedsAdapterTest {
|
||||
assertNull(feed2.remoteFolderId)
|
||||
|
||||
val feed3 = feeds[2]
|
||||
assertEquals(feed3.name, "krebsonsecurity.com")
|
||||
assertEquals(feed3.remoteFolderId, "5")
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.readrops.api.services.nextcloudnews.adapters
|
||||
|
||||
import com.readrops.api.TestUtils
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class NextNewsUserAdapterTest {
|
||||
|
||||
private val adapter = NextNewsUserAdapter()
|
||||
|
||||
@Test
|
||||
fun validXmlTest() {
|
||||
val stream = TestUtils.loadResource("services/nextcloudnews/user.xml")
|
||||
|
||||
assertEquals(adapter.fromXml(stream), "Shinokuni")
|
||||
}
|
||||
}
|
@ -33,7 +33,7 @@
|
||||
{
|
||||
"id": 3,
|
||||
"url": "https://krebsonsecurity.com/feed/",
|
||||
"title": "Krebs on Security",
|
||||
"title": "",
|
||||
"faviconLink": "https://krebsonsecurity.com/favicon.ico",
|
||||
"added": 1490999780,
|
||||
"folderId": 5,
|
||||
|
39
api/src/test/resources/services/nextcloudnews/user.xml
Normal file
39
api/src/test/resources/services/nextcloudnews/user.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<ocs>
|
||||
<meta>
|
||||
<status>ok</status>
|
||||
<statuscode>100</statuscode>
|
||||
<message>OK</message>
|
||||
<totalitems></totalitems>
|
||||
<itemsperpage></itemsperpage>
|
||||
</meta>
|
||||
<data>
|
||||
<enabled>1</enabled>
|
||||
<storageLocation>/opt/nextcloud/data/Shinokuni</storageLocation>
|
||||
<id>Shinokuni</id>
|
||||
<lastLogin>1605289415000</lastLogin>
|
||||
<backend>Database</backend>
|
||||
<subadmin/>
|
||||
<quota>
|
||||
<free>6215059179</free>
|
||||
<used>15259777301</used>
|
||||
<total>21474836480</total>
|
||||
<relative>71.06</relative>
|
||||
<quota>21474836480</quota>
|
||||
</quota>
|
||||
<email>email@email.org</email>
|
||||
<displayname>Shinokuni</displayname>
|
||||
<phone>phone</phone>
|
||||
<address></address>
|
||||
<website>https://url.com</website>
|
||||
<twitter></twitter>
|
||||
<groups>
|
||||
<element>admin</element>
|
||||
</groups>
|
||||
<language>fr</language>
|
||||
<locale>fr_FR</locale>
|
||||
<backendCapabilities>
|
||||
<setDisplayName>1</setDisplayName>
|
||||
<setPassword>1</setPassword>
|
||||
</backendCapabilities>
|
||||
</data>
|
||||
</ocs>
|
@ -11,8 +11,9 @@ import androidx.annotation.Nullable;
|
||||
import com.readrops.api.services.SyncResult;
|
||||
import com.readrops.api.services.SyncType;
|
||||
import com.readrops.api.services.nextcloudnews.NextNewsDataSource;
|
||||
import com.readrops.api.services.nextcloudnews.NextNewsService;
|
||||
import com.readrops.api.services.nextcloudnews.NextNewsSyncData;
|
||||
import com.readrops.api.services.nextcloudnews.json.NextNewsUser;
|
||||
import com.readrops.api.utils.AuthInterceptor;
|
||||
import com.readrops.api.utils.exceptions.UnknownFormatException;
|
||||
import com.readrops.app.addfeed.FeedInsertionResult;
|
||||
import com.readrops.app.addfeed.ParsingResult;
|
||||
@ -24,6 +25,7 @@ import com.readrops.db.entities.Item;
|
||||
import com.readrops.db.entities.account.Account;
|
||||
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.koin.java.KoinJavaComponent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@ -49,16 +51,20 @@ public class NextNewsRepository extends ARepository {
|
||||
@Override
|
||||
public Completable login(Account account, boolean insert) {
|
||||
setCredentials(account);
|
||||
return Single.<NextNewsUser>create(emitter -> {
|
||||
NextNewsUser user = dataSource.login();
|
||||
return Single.<String>create(emitter -> {
|
||||
// workaround to have the Nextcloud API call working, as I don't know how to do otherwise
|
||||
AuthInterceptor authInterceptor = KoinJavaComponent.get(AuthInterceptor.class);
|
||||
authInterceptor.getCredentials().setUrl(authInterceptor.getCredentials().getUrl().replace(NextNewsService.END_POINT, ""));
|
||||
|
||||
if (user != null) {
|
||||
emitter.onSuccess(user);
|
||||
String displayName = dataSource.login(account.getLogin());
|
||||
|
||||
if (displayName != null) {
|
||||
emitter.onSuccess(displayName);
|
||||
} else {
|
||||
emitter.onError(new Exception("Login failed. Please check your credentials and your Nextcloud News setup."));
|
||||
}
|
||||
}).flatMapCompletable(user -> {
|
||||
account.setDisplayedName(user.getDisplayName());
|
||||
}).flatMapCompletable(displayName -> {
|
||||
account.setDisplayedName(displayName);
|
||||
account.setCurrentAccount(true);
|
||||
|
||||
if (insert) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user