Consume login client from data client library (#2894)

Fix actions for review client

Use data client library for notifications

With delete helper migrated to data client

With wikidata edits

With notifications and modifications migrated to data client

With upload migrated to retrofit

Delete unused code

Reuse thank interface from the library
This commit is contained in:
Vivek Maskara 2019-07-07 14:27:45 +05:30
parent 623c2f5467
commit d427a77c2a
35 changed files with 883 additions and 1668 deletions

View file

@ -1,5 +1,9 @@
package fr.free.nrw.commons.notification;
import org.wikipedia.util.DateUtil;
import fr.free.nrw.commons.utils.CommonsDateUtil;
/**
* Created by root on 18.12.2017.
*/
@ -8,33 +12,44 @@ public class Notification {
public NotificationType notificationType;
public String notificationText;
public String date;
public String description;
public String link;
public String iconUrl;
public String dateWithYear;
public String notificationId;
public Notification(NotificationType notificationType, String notificationText, String date, String description, String link, String iconUrl, String dateWithYear, String notificationId) {
public Notification(NotificationType notificationType,
String notificationText,
String date,
String link,
String iconUrl,
String notificationId) {
this.notificationType = notificationType;
this.notificationText = notificationText;
this.date = date;
this.description = description;
this.link = link;
this.iconUrl = iconUrl;
this.dateWithYear = dateWithYear;
this.notificationId=notificationId;
}
public static Notification from(org.wikipedia.notifications.Notification wikiNotification) {
org.wikipedia.notifications.Notification.Contents contents = wikiNotification.getContents();
String notificationLink = contents == null || contents.getLinks() == null
|| contents.getLinks().getPrimary() == null ? "" : contents.getLinks().getPrimary().getUrl();
return new Notification(NotificationType.UNKNOWN,
contents == null ? "" : contents.getCompactHeader(),
DateUtil.getMonthOnlyDateString(wikiNotification.getTimestamp()),
notificationLink,
"",
String.valueOf(wikiNotification.id()));
}
@Override
public String toString() {
return "Notification" +
"notificationType='" + notificationType + '\'' +
", notificationText='" + notificationText + '\'' +
", date='" + date + '\'' +
", description='" + description + '\'' +
", link='" + link + '\'' +
", iconUrl='" + iconUrl + '\'' +
", dateWithYear=" + dateWithYear +
", notificationId='" + notificationId + '\'' +
'}';
}

View file

@ -5,11 +5,6 @@ import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.snackbar.Snackbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -19,10 +14,17 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import com.pedrogomez.renderers.RVRendererAdapter;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.inject.Inject;
@ -34,7 +36,9 @@ import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.utils.NetworkUtils;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
@ -78,25 +82,25 @@ public class NotificationActivity extends NavigationBaseActivity {
@SuppressLint("CheckResult")
public void removeNotification(Notification notification) {
compositeDisposable.add(Observable.fromCallable(() -> controller.markAsRead(notification))
Disposable disposable = Observable.defer((Callable<ObservableSource<Boolean>>)
() -> controller.markAsRead(notification))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
if (result){
if (result) {
notificationList.remove(notification);
setAdapter(notificationList);
adapter.notifyDataSetChanged();
Snackbar snackbar = Snackbar
.make(relativeLayout,"Notification marked as read", Snackbar.LENGTH_LONG);
.make(relativeLayout, "Notification marked as read", Snackbar.LENGTH_LONG);
snackbar.show();
if (notificationList.size()==0){
if (notificationList.size() == 0) {
setEmptyView();
relativeLayout.setVisibility(View.GONE);
no_notification.setVisibility(View.VISIBLE);
}
}
else {
} else {
adapter.notifyDataSetChanged();
setAdapter(notificationList);
Toast.makeText(NotificationActivity.this, "There was some error!", Toast.LENGTH_SHORT).show();
@ -107,7 +111,8 @@ public class NotificationActivity extends NavigationBaseActivity {
throwable.printStackTrace();
ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications);
progressBar.setVisibility(View.GONE);
}));
});
compositeDisposable.add(disposable);
}
@ -140,11 +145,8 @@ public class NotificationActivity extends NavigationBaseActivity {
private void addNotifications(boolean archived) {
Timber.d("Add notifications");
if (mNotificationWorkerFragment == null) {
compositeDisposable.add(Observable.fromCallable(() -> {
progressBar.setVisibility(View.VISIBLE);
return controller.getNotifications(archived);
})
progressBar.setVisibility(View.VISIBLE);
compositeDisposable.add(controller.getNotifications(archived)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(notificationList -> {

View file

@ -0,0 +1,45 @@
package fr.free.nrw.commons.notification;
import org.wikipedia.csrf.CsrfTokenClient;
import org.wikipedia.dataclient.Service;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import io.reactivex.Observable;
import io.reactivex.Single;
import static fr.free.nrw.commons.di.NetworkingModule.NAMED_COMMONS_CSRF;
@Singleton
public class NotificationClient {
private final Service service;
private final CsrfTokenClient csrfTokenClient;
@Inject
public NotificationClient(@Named("commons-service") Service service, @Named(NAMED_COMMONS_CSRF) CsrfTokenClient csrfTokenClient) {
this.service = service;
this.csrfTokenClient = csrfTokenClient;
}
public Single<List<Notification>> getNotifications(boolean archived) {
return service.getAllNotifications("wikidatawiki|commonswiki|enwiki", archived ? "read" : "!read", null)
.map(mwQueryResponse -> mwQueryResponse.query().notifications().list())
.flatMap(Observable::fromIterable)
.map(notification -> Notification.from(notification))
.toList();
}
public Observable<Boolean> markNotificationAsRead(String notificationId) {
try {
return service.markRead(csrfTokenClient.getTokenBlocking(), notificationId, "")
.map(mwQueryResponse -> mwQueryResponse.success());
} catch (Throwable throwable) {
return Observable.just(false);
}
}
}

View file

@ -1,14 +1,12 @@
package fr.free.nrw.commons.notification;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import io.reactivex.Observable;
import io.reactivex.Single;
/**
* Created by root on 19.12.2017.
@ -16,27 +14,19 @@ import fr.free.nrw.commons.mwapi.MediaWikiApi;
@Singleton
public class NotificationController {
private MediaWikiApi mediaWikiApi;
private SessionManager sessionManager;
private NotificationClient notificationClient;
@Inject
public NotificationController(MediaWikiApi mediaWikiApi, SessionManager sessionManager) {
this.mediaWikiApi = mediaWikiApi;
this.sessionManager = sessionManager;
public NotificationController(NotificationClient notificationClient) {
this.notificationClient = notificationClient;
}
public List<Notification> getNotifications(boolean archived) throws IOException {
if (mediaWikiApi.validateLogin()) {
return mediaWikiApi.getNotifications(archived);
} else {
Boolean authTokenValidated = sessionManager.revalidateAuthToken();
if (authTokenValidated != null && authTokenValidated) {
return mediaWikiApi.getNotifications(archived);
}
}
return new ArrayList<>();
public Single<List<Notification>> getNotifications(boolean archived) {
return notificationClient.getNotifications(archived);
}
public boolean markAsRead(Notification notification) throws IOException{
return mediaWikiApi.markNotificationAsRead(notification);
Observable<Boolean> markAsRead(Notification notification) {
return notificationClient.markNotificationAsRead(notification.notificationId);
}
}

View file

@ -1,293 +0,0 @@
package fr.free.nrw.commons.notification;
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.ArrayList;
import java.util.List;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R;
import static fr.free.nrw.commons.notification.NotificationType.UNKNOWN;
public class NotificationUtils {
private static final String COMMONS_WIKI = "commonswiki";
private static final String WIKIDATA_WIKI = "wikidatawiki";
private static final String WIKIPEDIA_WIKI = "enwiki";
/**
* Returns true if the wiki attribute corresponds to commonswiki
* @param document
* @return boolean representing whether the wiki attribute corresponds to commonswiki
*/
public static boolean isCommonsNotification(Node document) {
if (document == null || !document.hasAttributes()) {
return false;
}
Element element = (Element) document;
return COMMONS_WIKI.equals(element.getAttribute("wiki"));
}
/**
* Returns true if the wiki attribute corresponds to wikidatawiki
* @param document
* @return boolean representing whether the wiki attribute corresponds to wikidatawiki
*/
public static boolean isWikidataNotification(Node document) {
if (document == null || !document.hasAttributes()) {
return false;
}
Element element = (Element) document;
return WIKIDATA_WIKI.equals(element.getAttribute("wiki"));
}
/**
* Returns true if the wiki attribute corresponds to enwiki
* @param document
* @return
*/
public static boolean isWikipediaNotification(Node document) {
if (document == null || !document.hasAttributes()) {
return false;
}
Element element = (Element) document;
return WIKIPEDIA_WIKI.equals(element.getAttribute("wiki"));
}
/**
* Returns document notification type
* @param document
* @return the document's NotificationType
*/
public static NotificationType getNotificationType(Node document) {
Element element = (Element) document;
String type = element.getAttribute("type");
return NotificationType.handledValueOf(type);
}
public static String getNotificationId(Node document) {
Element element = (Element) document;
return element.getAttribute("id");
}
public static List<Notification> getNotificationsFromBundle(Context context, Node document) {
Element bundledNotifications = getBundledNotifications(document);
NodeList childNodes = bundledNotifications.getChildNodes();
List<Notification> notifications = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (isUsefulNotification(node)) {
notifications.add(getNotificationFromApiResult(context, node));
}
}
return notifications;
}
@NonNull
public static List<Notification> getNotificationsFromList(Context context, NodeList childNodes) {
List<Notification> notifications = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (isUsefulNotification(node)) {
if (isBundledNotification(node)) {
notifications.addAll(getNotificationsFromBundle(context, node));
} else {
notifications.add(getNotificationFromApiResult(context, node));
}
}
}
return notifications;
}
/**
* Currently the app is interested in showing notifications just from the following three wikis: commons, wikidata, wikipedia
* This function returns true only if the notification belongs to any of the above wikis and is of a known notification type
* @param node
* @return whether a notification is from one of Commons, Wikidata or Wikipedia
*/
private static boolean isUsefulNotification(Node node) {
return (isCommonsNotification(node)
|| isWikidataNotification(node)
|| isWikipediaNotification(node))
&& !getNotificationType(node).equals(UNKNOWN);
}
public static boolean isBundledNotification(Node document) {
Element bundleElement = getBundledNotifications(document);
if (bundleElement == null) {
return false;
}
return bundleElement.getChildNodes().getLength() > 0;
}
private static Element getBundledNotifications(Node document) {
return (Element) getNode(document, "bundledNotifications");
}
public static Notification getNotificationFromApiResult(Context context, Node document) {
NotificationType type = getNotificationType(document);
String notificationText = "";
String link = getPrimaryLink(document);
String description = getNotificationDescription(document);
String iconUrl = getNotificationIconUrl(document);
switch (type) {
case THANK_YOU_EDIT:
notificationText = getThankYouEditDescription(document);
break;
case EDIT_USER_TALK:
notificationText = getNotificationText(document);
break;
case MENTION:
notificationText = getMentionMessage(context, document);
description = getMentionDescription(document);
break;
case WELCOME:
notificationText = getWelcomeMessage(context, document);
break;
}
return new Notification(type, notificationText, getTimestamp(document), description, link, iconUrl, getTimestampWithYear(document),
getNotificationId(document));
}
private static String getNotificationText(Node document) {
String notificationBody = getNotificationBody(document);
if (notificationBody == null || notificationBody.trim().equals("")) {
return getNotificationHeader(document);
}
return notificationBody;
}
private static String getNotificationHeader(Node document) {
Node body = getNode(getModel(document), "header");
if (body != null) {
String textContent = body.getTextContent();
return textContent.replace("<strong>", "").replace("</strong>", "");
} else {
return "";
}
}
private static String getNotificationBody(Node document) {
Node body = getNode(getModel(document), "body");
if (body != null) {
String textContent = body.getTextContent();
return textContent.replace("<strong>", "").replace("</strong>", "");
} else {
return "";
}
}
private static String getMentionDescription(Node document) {
Node body = getNode(getModel(document), "body");
return body != null ? body.getTextContent() : "";
}
/**
* Gets the header node returned in the XML document to form the description for thank you edits
* @param document
* @return
*/
private static String getThankYouEditDescription(Node document) {
Node body = getNode(getModel(document), "header");
return body != null ? body.getTextContent() : "";
}
private static String getNotificationIconUrl(Node document) {
String format = "%s%s";
Node iconUrl = getNode(getModel(document), "iconUrl");
if (iconUrl == null) {
return null;
} else {
String url = iconUrl.getTextContent();
return String.format(format, BuildConfig.COMMONS_URL, url);
}
}
public static String getMentionMessage(Context context, Node document) {
String format = context.getString(R.string.notifications_mention);
return String.format(format, getAgent(document), getNotificationDescription(document));
}
@SuppressLint("StringFormatMatches")
public static String getUserTalkMessage(Context context, Node document) {
String format = context.getString(R.string.notifications_talk_page_message);
return String.format(format, getAgent(document));
}
@SuppressLint("StringFormatInvalid")
public static String getWelcomeMessage(Context context, Node document) {
String welcomeMessageFormat = context.getString(R.string.notifications_welcome);
return String.format(welcomeMessageFormat, getAgent(document));
}
private static String getPrimaryLink(Node document) {
Node links = getNode(getModel(document), "links");
Element primaryLink = (Element) getNode(links, "primary");
if (primaryLink != null) {
return primaryLink.getAttribute("url");
}
return "";
}
private static Node getModel(Node document) {
return getNode(document, "_.2A.");
}
private static String getAgent(Node document) {
Element agentElement = (Element) getNode(document, "agent");
if (agentElement != null) {
return agentElement.getAttribute("name");
}
return "";
}
private static String getTimestamp(Node document) {
Element timestampElement = (Element) getNode(document, "timestamp");
if (timestampElement != null) {
return timestampElement.getAttribute("date");
}
return "";
}
private static String getTimestampWithYear(Node document) {
Element timestampElement = (Element) getNode(document, "timestamp");
if (timestampElement != null) {
return timestampElement.getAttribute("utcunix");
}
return "";
}
private static String getNotificationDescription(Node document) {
Element titleElement = (Element) getNode(document, "title");
if (titleElement != null) {
return titleElement.getAttribute("text");
}
return "";
}
@Nullable
public static Node getNode(Node node, String nodeName) {
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node nodeItem = childNodes.item(i);
Element item = (Element) nodeItem;
if (item.getTagName().equals(nodeName)) {
return nodeItem;
}
}
return null;
}
}