Skip to content

Commit e24f3ea

Browse files
committed
Implement new notifications api
1 parent d738c6e commit e24f3ea

9 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.proxer.library.api;
2+
3+
import com.squareup.moshi.FromJson;
4+
import com.squareup.moshi.Json;
5+
import me.proxer.library.entitiy.notifications.Notification;
6+
import me.proxer.library.enums.NotificationType;
7+
import me.proxer.library.util.ProxerUrls;
8+
import okhttp3.HttpUrl;
9+
10+
import java.util.Date;
11+
12+
/**
13+
* @author Ruben Gees
14+
*/
15+
class NotificationAdapter {
16+
17+
@FromJson
18+
Notification fromJson(IntermediateNotification json) {
19+
final String base = ProxerUrls.webBase().toString();
20+
final HttpUrl properContentLink = HttpUrl.parse(base.substring(0, base.length() - 1) + json.contentLink);
21+
22+
return new Notification(json.id, json.type, json.contentId, properContentLink, json.text,
23+
json.date, json.additionalDescription);
24+
}
25+
26+
static class IntermediateNotification {
27+
28+
@Json(name = "id")
29+
String id;
30+
31+
@Json(name = "type")
32+
private NotificationType type;
33+
34+
@Json(name = "tid")
35+
private String contentId;
36+
37+
@Json(name = "link")
38+
private String contentLink;
39+
40+
@Json(name = "linktext")
41+
private String text;
42+
43+
@Json(name = "time")
44+
private Date date;
45+
46+
@Json(name = "description")
47+
private String additionalDescription;
48+
}
49+
}

library/src/main/java/me/proxer/library/api/ProxerApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ private void initMoshi() {
225225
.add(new BooleanAdapter())
226226
.add(new DelimitedEnumSetAdapterFactory())
227227
.add(new HttpUrlAdapter())
228+
.add(new NotificationAdapter())
228229
.add(new ConferenceAdapter())
229230
.add(new ConferenceInfoAdapter())
230231
.add(new EpisodeInfoAdapter())

library/src/main/java/me/proxer/library/api/notifications/InternalApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.proxer.library.api.ProxerCall;
44
import me.proxer.library.entitiy.notifications.NewsArticle;
5+
import me.proxer.library.entitiy.notifications.Notification;
56
import retrofit2.http.GET;
67
import retrofit2.http.Query;
78

@@ -14,4 +15,8 @@ interface InternalApi {
1415

1516
@GET("notifications/news")
1617
ProxerCall<List<NewsArticle>> news(@Query("p") Integer page, @Query("limit") Integer limit);
18+
19+
@GET("notifications/notifications")
20+
ProxerCall<List<Notification>> notifications(@Query("p") Integer page, @Query("limit") Integer limit,
21+
@Query("set_read") Boolean markAsRead);
1722
}

library/src/main/java/me/proxer/library/api/notifications/NotificationsApi.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ public NotificationsApi(@NotNull final Retrofit retrofit) {
2626
public NewsEndpoint news() {
2727
return new NewsEndpoint(internalApi);
2828
}
29+
30+
/**
31+
* Returns the respective endpoint.
32+
*/
33+
@NotNull
34+
public NotificationsEndpoint notifications() {
35+
return new NotificationsEndpoint(internalApi);
36+
}
2937
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package me.proxer.library.api.notifications;
2+
3+
import lombok.Setter;
4+
import lombok.experimental.Accessors;
5+
import me.proxer.library.api.LimitEndpoint;
6+
import me.proxer.library.api.PagingEndpoint;
7+
import me.proxer.library.api.ProxerCall;
8+
import me.proxer.library.entitiy.notifications.Notification;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.List;
13+
14+
/**
15+
* Endpoint for retrieving the notifications of an user.
16+
*
17+
* @author Ruben Gees
18+
*/
19+
@Accessors(fluent = true)
20+
public final class NotificationsEndpoint implements PagingEndpoint, LimitEndpoint {
21+
22+
private final InternalApi internalApi;
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
@Nullable
28+
@Setter(onMethod = @__({@Override, @NotNull}))
29+
private Integer page;
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
@Nullable
35+
@Setter(onMethod = @__({@Override, @NotNull}))
36+
private Integer limit;
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
@Nullable
42+
@Setter(onMethod = @__({@NotNull}))
43+
private Boolean markAsRead;
44+
45+
NotificationsEndpoint(@NotNull final InternalApi internalApi) {
46+
this.internalApi = internalApi;
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
@Override
53+
@NotNull
54+
public ProxerCall<List<Notification>> build() {
55+
return internalApi.notifications(page, limit, markAsRead);
56+
}
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package me.proxer.library.entitiy.notifications;
2+
3+
import com.squareup.moshi.Json;
4+
import lombok.Getter;
5+
import lombok.Value;
6+
import me.proxer.library.entitiy.ProxerDateItem;
7+
import me.proxer.library.entitiy.ProxerIdItem;
8+
import me.proxer.library.enums.NotificationType;
9+
import okhttp3.HttpUrl;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
12+
13+
import java.util.Date;
14+
15+
/**
16+
* @author Ruben Gees
17+
*/
18+
@Value
19+
public class Notification implements ProxerIdItem, ProxerDateItem {
20+
21+
/**
22+
* Returns the id.
23+
*/
24+
@Getter(onMethod = @__({@Override, @NotNull}))
25+
@Json(name = "id")
26+
private String id;
27+
28+
/**
29+
* Returns the type of the notification.
30+
*/
31+
@Getter(onMethod = @__({@NotNull}))
32+
@Json(name = "type")
33+
private NotificationType type;
34+
35+
/**
36+
* Returns the id of the content. This depends on the {@link #type}.
37+
*/
38+
@Getter(onMethod = @__({@NotNull}))
39+
@Json(name = "tid")
40+
private String contentId;
41+
42+
/**
43+
* Returns the link to the content.
44+
*/
45+
@Getter(onMethod = @__({@NotNull}))
46+
@Json(name = "link")
47+
private HttpUrl contentLink;
48+
49+
/**
50+
* Returns the description of the content. This is directly usable for presentation.
51+
*/
52+
@Getter(onMethod = @__({@NotNull}))
53+
@Json(name = "linktext")
54+
private String text;
55+
56+
/**
57+
* Returns the time.
58+
*/
59+
@Getter(onMethod = @__({@Override, @NotNull}))
60+
@Json(name = "time")
61+
private Date date;
62+
63+
/**
64+
* Returns additional info, depending on the {@link #type}.
65+
*/
66+
@Getter(onMethod = @__({@Nullable}))
67+
@Json(name = "description")
68+
private String additionalDescription;
69+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.proxer.library.enums;
2+
3+
import com.squareup.moshi.Json;
4+
5+
/**
6+
* Enum holding the available notification types.
7+
*
8+
* @author Ruben Gees
9+
*/
10+
public enum NotificationType {
11+
@Json(name = "user_boardmessage")BOARD_MESSAGE,
12+
@Json(name = "user_boardreply")BOARD_REPLY,
13+
@Json(name = "user_friendaccept")FRIEND_ACCEPT,
14+
@Json(name = "subs_projectstate")SUBS_PROJECT_STATE,
15+
@Json(name = "media_reminder")REMINDER,
16+
@Json(name = "ticket")TICKET,
17+
@Json(name = "ticket_comment")TICKET_COMMENT,
18+
@Json(name = "ticket_mention")TICKET_MENTION,
19+
@Json(name = "forum_post")FORUM_POST,
20+
@Json(name = "forum_topic")FORUM_TOPIC,
21+
@Json(name = "gallery2_album")GALLERY_ALBUM,
22+
@Json(name = "apps_release")APPS_RELEASE,
23+
@Json(name = "apps_state")APPS_STATE,
24+
@Json(name = "podcast")PODCAST
25+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package me.proxer.library.api.notifications;
2+
3+
import me.proxer.library.ProxerTest;
4+
import me.proxer.library.api.ProxerException;
5+
import me.proxer.library.entitiy.notifications.Notification;
6+
import me.proxer.library.enums.NotificationType;
7+
import okhttp3.HttpUrl;
8+
import okhttp3.mockwebserver.MockResponse;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.util.Date;
13+
import java.util.List;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
/**
18+
* @author Ruben Gees
19+
*/
20+
public class NotificationEndpointTest extends ProxerTest {
21+
22+
@Test
23+
public void testDefault() throws ProxerException, IOException {
24+
server.enqueue(new MockResponse().setBody(fromResource("notification.json")));
25+
26+
final List<Notification> result = api.notifications()
27+
.notifications()
28+
.build()
29+
.execute();
30+
31+
assertThat(result).first().isEqualTo(buildTestNotification());
32+
}
33+
34+
@Test
35+
public void testPath() throws ProxerException, IOException, InterruptedException {
36+
server.enqueue(new MockResponse().setBody(fromResource("notification.json")));
37+
38+
api.notifications().notifications()
39+
.page(0)
40+
.limit(10)
41+
.markAsRead(true)
42+
.build()
43+
.execute();
44+
45+
assertThat(server.takeRequest().getPath())
46+
.isEqualTo("/api/v1/notifications/notifications?p=0&limit=10&set_read=true");
47+
}
48+
49+
private Notification buildTestNotification() {
50+
return new Notification("10185686", NotificationType.REMINDER, "49815105",
51+
HttpUrl.parse("https://proxer.me/chapter/2373/26/en#top"), "Lesezeichen: <u>Test123 Manga #26</u> ist online!",
52+
new Date(1494452692L * 1000L), "");
53+
}
54+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"error": 0,
3+
"message": "Abfrage erfolgreich",
4+
"data": [
5+
{
6+
"id": "10185686",
7+
"tid": "49815105",
8+
"type": "media_reminder",
9+
"link": "/chapter/2373/26/en#top",
10+
"linktext": "Lesezeichen: <u>Test123 Manga #26</u> ist online!",
11+
"time": "1494452692",
12+
"description": ""
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)