1
1
package fr .free .nrw .commons .mwapi ;
2
2
3
+ import android .content .Context ;
3
4
import android .os .Build ;
4
5
import android .support .annotation .NonNull ;
5
6
import android .support .annotation .Nullable ;
21
22
import org .apache .http .util .EntityUtils ;
22
23
import org .mediawiki .api .ApiResult ;
23
24
import org .mediawiki .api .MWApi ;
25
+ import org .w3c .dom .Node ;
26
+ import org .w3c .dom .NodeList ;
24
27
25
28
import java .io .IOException ;
26
29
import java .io .InputStream ;
36
39
37
40
import fr .free .nrw .commons .BuildConfig ;
38
41
import fr .free .nrw .commons .PageTitle ;
42
+ import fr .free .nrw .commons .notification .Notification ;
39
43
import in .yuvi .http .fluent .Http ;
40
44
import io .reactivex .Observable ;
41
45
import io .reactivex .Single ;
42
46
import timber .log .Timber ;
43
47
48
+ import static fr .free .nrw .commons .notification .NotificationType .UNKNOWN ;
49
+ import static fr .free .nrw .commons .notification .NotificationUtils .getNotificationFromApiResult ;
50
+ import static fr .free .nrw .commons .notification .NotificationUtils .getNotificationType ;
51
+ import static fr .free .nrw .commons .notification .NotificationUtils .isCommonsNotification ;
52
+
44
53
/**
45
54
* @author Addshore
46
55
*/
@@ -50,8 +59,10 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
50
59
private static final String THUMB_SIZE = "640" ;
51
60
private AbstractHttpClient httpClient ;
52
61
private MWApi api ;
62
+ private Context context ;
53
63
54
- public ApacheHttpClientMediaWikiApi (String apiURL ) {
64
+ public ApacheHttpClientMediaWikiApi (Context context , String apiURL ) {
65
+ this .context = context ;
55
66
BasicHttpParams params = new BasicHttpParams ();
56
67
SchemeRegistry schemeRegistry = new SchemeRegistry ();
57
68
schemeRegistry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
@@ -353,6 +364,42 @@ public String revisionsByFilename(String filename) throws IOException {
353
364
.getString ("/api/query/pages/page/revisions/rev" );
354
365
}
355
366
367
+ @ Override
368
+ @ NonNull
369
+ public List <Notification > getNotifications () {
370
+ ApiResult notificationNode = null ;
371
+ try {
372
+ notificationNode = api .action ("query" )
373
+ .param ("notprop" , "list" )
374
+ .param ("format" , "xml" )
375
+ .param ("meta" , "notifications" )
376
+ .param ("notfilter" , "!read" )
377
+ .get ()
378
+ .getNode ("/api/query/notifications/list" );
379
+ } catch (IOException e ) {
380
+ Timber .e ("Failed to obtain searchCategories" , e );
381
+ }
382
+
383
+ if (notificationNode == null ) {
384
+ return new ArrayList <>();
385
+ }
386
+
387
+ List <Notification > notifications = new ArrayList <>();
388
+
389
+ NodeList childNodes = notificationNode .getDocument ().getChildNodes ();
390
+
391
+ for (int i = 0 ; i < childNodes .getLength (); i ++) {
392
+ Node node = childNodes .item (i );
393
+ if (isCommonsNotification (node )
394
+ && !getNotificationType (node ).equals (UNKNOWN )) {
395
+ notifications .add (getNotificationFromApiResult (context , node ));
396
+ }
397
+ }
398
+
399
+ return notifications ;
400
+ }
401
+
402
+
356
403
@ Override
357
404
public boolean existingFile (String fileSha1 ) throws IOException {
358
405
return api .action ("query" )
0 commit comments