Skip to content

Commit 21a6b1f

Browse files
author
maskara
committed
Fixes in notifications for pending issues
1 parent fe1f0b5 commit 21a6b1f

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

app/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ dependencies {
102102
compile 'com.android.support.constraint:constraint-layout:1.0.2'
103103
}
104104

105+
repositories {
106+
mavenCentral()
107+
google()
108+
}
109+
110+
105111
android {
106112
compileSdkVersion project.compileSdkVersion
107113
buildToolsVersion project.buildToolsVersion
@@ -150,6 +156,7 @@ android {
150156
buildConfigField "String", "WIKIMEDIA_FORGE_API_HOST", "\"https://tools.wmflabs.org/\""
151157
buildConfigField "String", "IMAGE_URL_BASE", "\"https://upload.wikimedia.org/wikipedia/commons\""
152158
buildConfigField "String", "HOME_URL", "\"https://commons.wikimedia.org/wiki/\""
159+
buildConfigField "String", "COMMONS_URL", "\"https://commons.wikimedia.org\""
153160
buildConfigField "String", "MOBILE_HOME_URL", "\"https://commons.m.wikimedia.org/wiki/\""
154161
buildConfigField "String", "EVENTLOG_URL", "\"https://www.wikimedia.org/beacon/event\""
155162
buildConfigField "String", "EVENTLOG_WIKI", "\"commonswiki\""
@@ -165,6 +172,7 @@ android {
165172
buildConfigField "String", "WIKIMEDIA_FORGE_API_HOST", "\"https://tools.wmflabs.org/\""
166173
buildConfigField "String", "IMAGE_URL_BASE", "\"https://upload.beta.wmflabs.org/wikipedia/commons\""
167174
buildConfigField "String", "HOME_URL", "\"https://commons.wikimedia.beta.wmflabs.org/wiki/\""
175+
buildConfigField "String", "COMMONS_URL", "\"https://commons.wikimedia.beta.wmflabs.org\""
168176
buildConfigField "String", "MOBILE_HOME_URL", "\"https://commons.m.wikimedia.beta.wmflabs.org/wiki/\""
169177
buildConfigField "String", "EVENTLOG_URL", "\"https://commons.wikimedia.beta.wmflabs.org/beacon/event\""
170178
buildConfigField "String", "EVENTLOG_WIKI", "\"commonswiki\""

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ public List<Notification> getNotifications() {
434434
.param("notprop", "list")
435435
.param("format", "xml")
436436
.param("meta", "notifications")
437-
.param("notfilter", "!read")
437+
.param("notformat", "model")
438+
//.param("notfilter", "!read")
438439
.get()
439440
.getNode("/api/query/notifications/list");
440441
} catch (IOException e) {

app/src/main/java/fr/free/nrw/commons/notification/Notification.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ public class Notification {
1010
public String date;
1111
public String description;
1212
public String link;
13+
public String iconUrl;
1314

14-
public Notification(NotificationType notificationType, String notificationText, String date, String description, String link) {
15+
public Notification(NotificationType notificationType, String notificationText, String date, String description, String link, String iconUrl) {
1516
this.notificationType = notificationType;
1617
this.notificationText = notificationText;
1718
this.date = date;
1819
this.description = description;
1920
this.link = link;
21+
this.iconUrl = iconUrl;
2022
}
2123
}

app/src/main/java/fr/free/nrw/commons/notification/NotificationUtils.java

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons.notification;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45

56
import org.w3c.dom.Element;
@@ -33,40 +34,84 @@ public static Notification getNotificationFromApiResult(Context context, Node do
3334
NotificationType type = getNotificationType(document);
3435

3536
String notificationText = "";
36-
String link = getNotificationLink(document);
37+
String link = getPrimaryLink(document);
3738
String description = getNotificationDescription(document);
39+
String iconUrl = getNotificationIconUrl(document);
40+
3841
switch (type) {
3942
case THANK_YOU_EDIT:
4043
notificationText = context.getString(R.string.notifications_thank_you_edit);
4144
break;
4245
case EDIT_USER_TALK:
43-
notificationText = getUserTalkMessage(context, document);
46+
notificationText = getNotificationHeader(document);
4447
break;
4548
case MENTION:
4649
notificationText = getMentionMessage(context, document);
50+
description = getMentionDescription(document);
4751
break;
4852
case WELCOME:
4953
notificationText = getWelcomeMessage(context, document);
5054
break;
5155
}
52-
return new Notification(type, notificationText, getTimestamp(document), description, link);
56+
return new Notification(type, notificationText, getTimestamp(document), description, link, iconUrl);
57+
}
58+
59+
private static String getNotificationHeader(Node document) {
60+
Node body = getNode(getModel(document), "header");
61+
if (body != null) {
62+
String textContent = body.getTextContent();
63+
return textContent.replace("<strong>", "").replace("</strong>", "");
64+
} else {
65+
return "";
66+
}
67+
}
68+
69+
private static String getMentionDescription(Node document) {
70+
Node body = getNode(getModel(document), "body");
71+
return body != null ? body.getTextContent() : "";
72+
}
73+
74+
private static String getNotificationIconUrl(Node document) {
75+
String format = "%s%s";
76+
Node iconUrl = getNode(getModel(document), "iconUrl");
77+
if(iconUrl == null) {
78+
return null;
79+
} else {
80+
String url = iconUrl.getTextContent();
81+
return String.format(format, BuildConfig.COMMONS_URL, url);
82+
}
5383
}
5484

5585
public static String getMentionMessage(Context context, Node document) {
5686
String format = context.getString(R.string.notifications_mention);
5787
return String.format(format, getAgent(document), getNotificationDescription(document));
5888
}
5989

90+
@SuppressLint("StringFormatMatches")
6091
public static String getUserTalkMessage(Context context, Node document) {
6192
String format = context.getString(R.string.notifications_talk_page_message);
6293
return String.format(format, getAgent(document));
6394
}
6495

96+
@SuppressLint("StringFormatInvalid")
6597
public static String getWelcomeMessage(Context context, Node document) {
6698
String welcomeMessageFormat = context.getString(R.string.notifications_welcome);
6799
return String.format(welcomeMessageFormat, getAgent(document));
68100
}
69101

102+
private static String getPrimaryLink(Node document) {
103+
Node links = getNode(getModel(document), "links");
104+
Element primaryLink = (Element) getNode(links, "primary");
105+
if (primaryLink != null) {
106+
return primaryLink.getAttribute("url");
107+
}
108+
return "";
109+
}
110+
111+
private static Node getModel(Node document) {
112+
return getNode(document, "_.2A.");
113+
}
114+
70115
private static String getAgent(Node document) {
71116
Element agentElement = (Element) getNode(document, "agent");
72117
if (agentElement != null) {
@@ -83,16 +128,6 @@ private static String getTimestamp(Node document) {
83128
return "";
84129
}
85130

86-
private static String getNotificationLink(Node document) {
87-
String format = "%s%s";
88-
Element titleElement = (Element) getNode(document, "title");
89-
if (titleElement != null) {
90-
String fullName = titleElement.getAttribute("full");
91-
return String.format(format, BuildConfig.HOME_URL, fullName);
92-
}
93-
return "";
94-
}
95-
96131
private static String getNotificationDescription(Node document) {
97132
Element titleElement = (Element) getNode(document, "title");
98133
if (titleElement != null) {

0 commit comments

Comments
 (0)