Skip to content

Commit

Permalink
#189 Sending multiple attachments for ActivityPub
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Nov 7, 2019
1 parent 609569d commit 69a5986
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
Expand Up @@ -39,7 +39,6 @@
import io.vavr.control.Try;

import static org.andstatus.app.net.social.activitypub.ConnectionActivityPub.CONTENT_PROPERTY;
import static org.andstatus.app.net.social.activitypub.ConnectionActivityPub.FULL_IMAGE_OBJECT;
import static org.andstatus.app.net.social.activitypub.ConnectionActivityPub.NAME_PROPERTY;
import static org.andstatus.app.net.social.activitypub.ConnectionActivityPub.SENSITIVE_PROPERTY;
import static org.andstatus.app.net.social.activitypub.ConnectionActivityPub.SUMMARY_PROPERTY;
Expand Down Expand Up @@ -125,42 +124,7 @@ private Try<HttpReadResult> sendInternal(ActivityType activityTypeIn) throws Con
private JSONObject buildActivityToSend(ActivityType activityType) throws JSONException, ConnectionException {
JSONObject activity = newActivityOfThisAccount(activityType);
JSONObject obj = buildObject(activity);
if (attachments.nonEmpty()) {
if (attachments.size() > 1) {
MyLog.w(this, "Sending only the first attachment: " + attachments); // TODO
}
Attachment attachment = attachments.list.get(0);
ApObjectType objectType = ApObjectType.fromJson(obj);
if (isExisting()
&& (!ApObjectType.IMAGE.equals(objectType) || !ApObjectType.VIDEO.equals(objectType))
) {
throw ConnectionException.hardConnectionException(
"Cannot update '" + objectType + "' to " + ApObjectType.IMAGE, null);
}
JSONObject mediaObject = uploadMedia(attachment);
ApObjectType mediaObjectType = ApObjectType.fromJson(mediaObject);
if (isExisting() && mediaObjectType.equals(objectType)) {
if (objectType == ApObjectType.VIDEO) {
JSONObject video = mediaObject.optJSONObject(ConnectionActivityPub.VIDEO_OBJECT);
if (video != null) {
// Replace the video in the existing object
obj.put(ConnectionActivityPub.VIDEO_OBJECT, video);
}
} else {
JSONObject image = mediaObject.optJSONObject(ConnectionActivityPub.IMAGE_OBJECT);
if (image != null) {
// Replace an image in the existing object
obj.put(ConnectionActivityPub.IMAGE_OBJECT, image);
JSONObject fullImage = mediaObject.optJSONObject(FULL_IMAGE_OBJECT);
if (fullImage != null) {
obj.put(FULL_IMAGE_OBJECT, fullImage);
}
}
}
} else {
obj = mediaObject;
}
}
addAttachments(obj);
if (StringUtils.nonEmpty(note.getName())) {
obj.put(NAME_PROPERTY, note.getName());
}
Expand All @@ -180,6 +144,17 @@ private JSONObject buildActivityToSend(ActivityType activityType) throws JSONExc
return activity;
}

private void addAttachments(JSONObject obj) throws ConnectionException, JSONException {
if (attachments.isEmpty()) return;

JSONArray jsoAttachments = new JSONArray();
for (Attachment attachment: attachments.list) {
JSONObject mediaObject = uploadMedia(attachment);
jsoAttachments.put(mediaObject);
}
obj.put("attachment", jsoAttachments);
}

private boolean contentNotPosted(ActivityType activityType, JSONObject jsActivity) {
JSONObject objPosted = jsActivity.optJSONObject("object");
return ActivityType.CREATE.equals(activityType) && objPosted != null &&
Expand Down
Expand Up @@ -53,8 +53,8 @@ public boolean isTypeOf(JSONObject jso) {
private String id;
private ApObjectType compatibleType = this;

ApObjectType(String fieldName, ApObjectType compatibleType) {
this.id = fieldName;
ApObjectType(String id, ApObjectType compatibleType) {
this.id = id;
if (compatibleType != null) {
this.compatibleType = compatibleType;
}
Expand Down

0 comments on commit 69a5986

Please sign in to comment.