added Notification model & class, implemented Mastodon mention timeline

This commit is contained in:
nuclearfog 2022-11-27 18:59:14 +01:00
parent 507b9c51b2
commit 53dce34397
No known key found for this signature in database
GPG Key ID: 03488A185C476379
5 changed files with 272 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import org.nuclearfog.twidda.backend.update.UserListUpdate;
import org.nuclearfog.twidda.model.Account; import org.nuclearfog.twidda.model.Account;
import org.nuclearfog.twidda.model.Location; import org.nuclearfog.twidda.model.Location;
import org.nuclearfog.twidda.model.Metrics; import org.nuclearfog.twidda.model.Metrics;
import org.nuclearfog.twidda.model.Notification;
import org.nuclearfog.twidda.model.Relation; import org.nuclearfog.twidda.model.Relation;
import org.nuclearfog.twidda.model.Status; import org.nuclearfog.twidda.model.Status;
import org.nuclearfog.twidda.model.Trend; import org.nuclearfog.twidda.model.Trend;
@ -545,4 +546,13 @@ public interface Connection {
* @return media ID * @return media ID
*/ */
long uploadMedia(MediaUpdate mediaUpdate) throws ConnectionException; long uploadMedia(MediaUpdate mediaUpdate) throws ConnectionException;
/**
* get notification of the current user
*
* @param minId minimum ID
* @param maxId maximum ID
* @return notification list
*/
List<Notification> getNotifications(long minId, long maxId) throws ConnectionException;
} }

View File

@ -10,8 +10,10 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.nuclearfog.twidda.backend.api.Connection; import org.nuclearfog.twidda.backend.api.Connection;
import org.nuclearfog.twidda.backend.api.ConnectionException;
import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonAccount; import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonAccount;
import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonList; import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonList;
import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonNotification;
import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonRelation; import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonRelation;
import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonStatus; import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonStatus;
import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonTrend; import org.nuclearfog.twidda.backend.api.mastodon.impl.MastodonTrend;
@ -29,6 +31,7 @@ import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.model.Account; import org.nuclearfog.twidda.model.Account;
import org.nuclearfog.twidda.model.Location; import org.nuclearfog.twidda.model.Location;
import org.nuclearfog.twidda.model.Metrics; import org.nuclearfog.twidda.model.Metrics;
import org.nuclearfog.twidda.model.Notification;
import org.nuclearfog.twidda.model.Relation; import org.nuclearfog.twidda.model.Relation;
import org.nuclearfog.twidda.model.Status; import org.nuclearfog.twidda.model.Status;
import org.nuclearfog.twidda.model.Trend; import org.nuclearfog.twidda.model.Trend;
@ -86,6 +89,7 @@ public class Mastodon implements Connection {
private static final String ENDPOINT_INCOMIN_REQUESTS = "/api/v1/follow_requests"; private static final String ENDPOINT_INCOMIN_REQUESTS = "/api/v1/follow_requests";
private static final String ENDPOINT_LOOKUP_USER = "/api/v1/accounts/lookup"; private static final String ENDPOINT_LOOKUP_USER = "/api/v1/accounts/lookup";
private static final String ENDPOINT_USERLIST = "/api/v1/lists"; private static final String ENDPOINT_USERLIST = "/api/v1/lists";
private static final String ENDPOINT_NOTIFICATION = "/api/v1/notifications";
MediaType TYPE_TEXT = MediaType.parse("text/plain"); MediaType TYPE_TEXT = MediaType.parse("text/plain");
@ -384,7 +388,23 @@ public class Mastodon implements Connection {
@Override @Override
public List<Status> getMentionTimeline(long minId, long maxId) throws MastodonException { public List<Status> getMentionTimeline(long minId, long maxId) throws MastodonException {
throw new MastodonException("not implemented!"); // todo add implementation List<String> params = new ArrayList<>();
params.add("since_id=" + minId);
params.add("max_id=" + maxId);
params.add("limit=" + settings.getListSize());
params.add("types[]=mention");
try {
List<Notification> notifications = createNotifications(get(ENDPOINT_NOTIFICATION, params));
List<Status> mentions = new ArrayList<>(notifications.size());
for (Notification notification : notifications) {
if (notification.getType() == Notification.TYPE_MENTION) {
mentions.add(notification.getStatus());
}
}
return mentions;
} catch (IOException e) {
throw new MastodonException(e);
}
} }
@ -662,6 +682,20 @@ public class Mastodon implements Connection {
throw new MastodonException("not implemented!"); // todo add implementation throw new MastodonException("not implemented!"); // todo add implementation
} }
@Override
public List<Notification> getNotifications(long minId, long maxId) throws ConnectionException {
List<String> params = new ArrayList<>();
params.add("since_id=" + minId);
params.add("max_id=" + maxId);
params.add("limit=" + settings.getListSize());
try {
return createNotifications(get(ENDPOINT_NOTIFICATION, params));
} catch (IOException e) {
throw new MastodonException(e);
}
}
/** /**
* get information about the current user * get information about the current user
* *
@ -865,6 +899,28 @@ public class Mastodon implements Connection {
} }
} }
/**
* create notification from response
*
* @return notification
*/
private List<Notification> createNotifications(Response response) throws MastodonException {
try {
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
long currentId = settings.getLogin().getId();
JSONArray json = new JSONArray(body.string());
List<Notification> result = new ArrayList<>(json.length());
for (int i = 0; i < json.length(); i++)
result.add(new MastodonNotification(json.getJSONObject(i), currentId));
return result;
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
throw new MastodonException(e);
}
}
/** /**
* send post request without return * send post request without return
* *

View File

@ -0,0 +1,109 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.backend.utils.StringTools;
import org.nuclearfog.twidda.model.Notification;
import org.nuclearfog.twidda.model.Status;
import org.nuclearfog.twidda.model.User;
/**
* notification implementation for Mastodon
*
* @author nuclearfog
*/
public class MastodonNotification implements Notification {
private static final long serialVersionUID = 4113306729125959429L;
private long id;
private long createdAt;
private int type;
private User user;
private Status status;
public MastodonNotification(JSONObject json, long currentId) throws JSONException {
String idStr = json.getString("id");
String typeStr = json.getString("type");
JSONObject statusJson = json.optJSONObject("status");
JSONObject userJson = json.getJSONObject("account");
createdAt = StringTools.getTime2(json.getString("created_at"));
user = new MastodonUser(userJson);
switch (typeStr) {
case "mention":
type = TYPE_MENTION;
break;
case "status":
type = TYPE_STATUS;
break;
case "reblog":
type = TYPE_REPOST;
break;
case "follow":
type = TYPE_FOLLOW;
break;
case "follow_request":
type = TYPE_REQUEST;
break;
case "favourite":
type = TYPE_FAVORITE;
break;
case "poll":
type = TYPE_POLL;
break;
case "update":
type = TYPE_UPDATE;
break;
}
if (statusJson != null) {
status = new MastodonStatus(statusJson, currentId);
}
try {
id = Long.parseLong(idStr);
} catch (NumberFormatException e) {
throw new JSONException("bad ID:" + idStr);
}
}
@Override
public long getId() {
return id;
}
@Override
public int getType() {
return type;
}
@Override
public long createdAt() {
return createdAt;
}
@Override
public User getUser() {
return user;
}
@Nullable
@Override
public Status getStatus() {
return status;
}
}

View File

@ -36,6 +36,7 @@ import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.model.Account; import org.nuclearfog.twidda.model.Account;
import org.nuclearfog.twidda.model.Location; import org.nuclearfog.twidda.model.Location;
import org.nuclearfog.twidda.model.Metrics; import org.nuclearfog.twidda.model.Metrics;
import org.nuclearfog.twidda.model.Notification;
import org.nuclearfog.twidda.model.Relation; import org.nuclearfog.twidda.model.Relation;
import org.nuclearfog.twidda.model.Status; import org.nuclearfog.twidda.model.Status;
import org.nuclearfog.twidda.model.Trend; import org.nuclearfog.twidda.model.Trend;
@ -1141,6 +1142,12 @@ public class Twitter implements Connection {
return new ArrayList<>(result); return new ArrayList<>(result);
} }
@Override
public List<Notification> getNotifications(long minId, long maxId) throws ConnectionException {
throw new TwitterException("not supported!");
}
/** /**
* get tweets using an endpoint * get tweets using an endpoint
* *

View File

@ -0,0 +1,89 @@
package org.nuclearfog.twidda.model;
import androidx.annotation.Nullable;
import java.io.Serializable;
/**
* Interface for notifications
*
* @author nuclearfog
*/
public interface Notification extends Serializable {
/**
* mention
*/
int TYPE_MENTION = 0x87AA;
/**
* Someone (enabled notifications) for has posted a status
*/
int TYPE_STATUS = 0x394A;
/**
* A (replied) status has been edited
*/
int TYPE_UPDATE = 0x2FB7;
/**
* Someone reposted a status
*/
int TYPE_REPOST = 0xF2A8;
/**
* new follower
*/
int TYPE_FOLLOW = 0x9BF5;
/**
* Someone requested to follow
*/
int TYPE_REQUEST = 0xB80E;
/**
* Someone favourited a status
*/
int TYPE_FAVORITE = 0xAA5F;
/**
* a poll is finished
*/
int TYPE_POLL = 0x6EB7;
/**
* notification ID
*
* @return ID
*/
long getId();
/**
* type of the notification {@link #TYPE_FAVORITE,#TYPE_FOLLOW,#TYPE_MENTION,#TYPE_REPOST,#TYPE_REQUEST,#TYPE_STATUS,#TYPE_UPDATE}
*
* @return notification type
*/
int getType();
/**
* get notification time
*
* @return time
*/
long createdAt();
/**
* get user from the notification
*
* @return user
*/
User getUser();
/**
* get status when there was an interaction
*
* @return status
*/
@Nullable
Status getStatus();
}