11package fr .free .nrw .commons .notification ;
22
3+ import android .annotation .SuppressLint ;
34import android .content .Context ;
45
56import 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