Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for delete favorite and user withheld #67

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package com.zaubersoftware.gnip4j.api.impl.formats;

import java.io.IOException;

import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.compliance.ComplianceActivity;
import com.zaubersoftware.gnip4j.api.support.logging.LoggerFactory;
import com.zaubersoftware.gnip4j.api.support.logging.spi.Logger;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.io.UncheckedIOException;

/**
* Translates JSON input from the Compliance v2 stream into instances of Activity.
Expand All @@ -41,11 +40,9 @@ public class ComplianceActivityUnmarshaller implements Unmarshaller<Activity> {
public final Activity unmarshall(final String s) {
try {
return mapper.readValue(s, ComplianceActivity.class).toActivity();
} catch (final JsonMappingException e) {
} catch (final IOException | UncheckedIOException e) {
logger.warn("Failed to parse compliance activity: " + s, e);
return null;
} catch (final IOException e) {
throw new IllegalArgumentException("parsing compliance activity", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) 2011-2016 Zauber S.A. <http://flowics.com/>
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,36 +15,37 @@
*/
package com.zaubersoftware.gnip4j.api.model.compliance;

import java.io.Serializable;

import com.zaubersoftware.gnip4j.api.model.Activity;
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;

import com.zaubersoftware.gnip4j.api.model.Activity;
import java.io.Serializable;

/**
* Activity from the Compliance v2 stream that can be converted to an Activity
*
* @author Dennis Lloyd Jr
* @since Oct 14, 2016
*/
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value=DeleteStatusActivity.class, name=ComplianceActivity.Verb.DELETE),
@JsonSubTypes.Type(value=DeleteUserActivity.class, name=ComplianceActivity.Verb.USER_DELETE),
@JsonSubTypes.Type(value=UndeleteUserActivity.class, name=ComplianceActivity.Verb.USER_UNDELETE),
@JsonSubTypes.Type(value=ScrubGeoActivity.class, name=ComplianceActivity.Verb.SCRUB_GEO),
@JsonSubTypes.Type(value=UserProtectActivity.class, name=ComplianceActivity.Verb.USER_PROTECT),
@JsonSubTypes.Type(value=UserUnprotectActivity.class, name=ComplianceActivity.Verb.USER_UNPROTECT),
@JsonSubTypes.Type(value=UserSuspendActivity.class, name=ComplianceActivity.Verb.USER_SUSPEND),
@JsonSubTypes.Type(value=UserUnsuspendActivity.class, name=ComplianceActivity.Verb.USER_UNSUSPEND),
@JsonSubTypes.Type(value=StatusWithheldActivity.class, name=ComplianceActivity.Verb.STATUS_WITHHELD)
@JsonSubTypes.Type(value = DeleteActivity.class, name = ComplianceActivity.Verb.DELETE),
@JsonSubTypes.Type(value = DeleteUserActivity.class, name = ComplianceActivity.Verb.USER_DELETE),
@JsonSubTypes.Type(value = UndeleteUserActivity.class, name = ComplianceActivity.Verb.USER_UNDELETE),
@JsonSubTypes.Type(value = ScrubGeoActivity.class, name = ComplianceActivity.Verb.SCRUB_GEO),
@JsonSubTypes.Type(value = UserProtectActivity.class, name = ComplianceActivity.Verb.USER_PROTECT),
@JsonSubTypes.Type(value = UserUnprotectActivity.class, name = ComplianceActivity.Verb.USER_UNPROTECT),
@JsonSubTypes.Type(value = UserSuspendActivity.class, name = ComplianceActivity.Verb.USER_SUSPEND),
@JsonSubTypes.Type(value = UserUnsuspendActivity.class, name = ComplianceActivity.Verb.USER_UNSUSPEND),
@JsonSubTypes.Type(value = StatusWithheldActivity.class, name = ComplianceActivity.Verb.STATUS_WITHHELD),
@JsonSubTypes.Type(value = UserWithheldActivity.class, name = ComplianceActivity.Verb.USER_WITHHELD)
})
public interface ComplianceActivity extends Serializable {
Activity toActivity();

interface Verb {
String DELETE = "delete";
String FAVORITE_DELETE = "favorite_delete";
String USER_DELETE = "user_delete";
String USER_UNDELETE = "user_undelete";
String SCRUB_GEO = "scrub_geo";
Expand All @@ -53,5 +54,6 @@ interface Verb {
String USER_SUSPEND = "user_suspend";
String USER_UNSUSPEND = "user_unsuspend";
String STATUS_WITHHELD = "status_withheld";
String USER_WITHHELD = "user_withheld";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.zaubersoftware.gnip4j.api.model.compliance;

import java.io.UncheckedIOException;
import java.util.Date;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonTypeName;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.Actor;
import org.codehaus.jackson.map.JsonMappingException;

/**
* Represents deleting a status from the Compliance v2 stream
Expand All @@ -30,9 +32,11 @@
* @since Oct 14, 2016
*/
@JsonTypeName(ComplianceActivity.Verb.DELETE)
class DeleteStatusActivity implements ComplianceActivity {
class DeleteActivity implements ComplianceActivity {
@JsonProperty("status")
private Status status;
@JsonProperty("favorite")
private Favorite favorite;
@JsonProperty("timestamp_ms")
private String timestamp;

Expand All @@ -43,13 +47,31 @@ public static class Status {
private String userId;
}

public static class Favorite {
@JsonProperty("id")
private String id;
@JsonProperty("user_id")
private String userId;
}

@Override
public Activity toActivity() {
final Activity result = new Activity();
result.setVerb(ComplianceActivity.Verb.DELETE);
result.setId(status.id);
result.setActor(new Actor());
result.getActor().setId(status.userId);

if(status != null) {
result.setVerb(ComplianceActivity.Verb.DELETE);
result.setId(status.id);
result.setActor(new Actor());
result.getActor().setId(status.userId);
} else if (favorite != null) {
result.setVerb(ComplianceActivity.Verb.FAVORITE_DELETE);
result.setId(favorite.id);
result.setActor(new Actor());
result.getActor().setId(favorite.userId);
} else {
throw new UncheckedIOException(new JsonMappingException("Expected delete of 'status' or 'favorite'"));
}

result.setUpdated(new Date(Long.parseLong(timestamp)));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package com.zaubersoftware.gnip4j.api.impl.formats;

import static org.junit.Assert.*;
import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.compliance.ComplianceActivity;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

import com.zaubersoftware.gnip4j.api.model.Activity;
import com.zaubersoftware.gnip4j.api.model.compliance.ComplianceActivity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* @author Dennis Lloyd Jr
Expand All @@ -48,6 +48,23 @@ public void deleteStatus() {
assertEquals(new Date(1432228155593L), result.getUpdated());
}

@Test
public void deleteFavorite() {
final String s = "{\"delete\":{\"favorite\":{\"id\":601430178305220600,\"id_str\":\"601430178305220600\",\"user_id\":3198576760,\"user_id_str\":\"3198576760\"},\"timestamp_ms\":\"1432228155593\"}}";
final Activity result = unmarshaller.unmarshall(s);
assertEquals(ComplianceActivity.Verb.FAVORITE_DELETE, result.getVerb());
assertEquals("601430178305220600", result.getId());
assertEquals("3198576760", result.getActor().getId());
assertEquals(new Date(1432228155593L), result.getUpdated());
}

@Test
public void deleteUnknown() { // What happens when we receive data for a delete that we don't expect
final String s = "{\"delete\":{\"unknown\":{\"id\":601430178305220600,\"id_str\":\"601430178305220600\",\"user_id\":3198576760,\"user_id_str\":\"3198576760\"},\"timestamp_ms\":\"1432228155593\"}}";
final Activity result = unmarshaller.unmarshall(s);
assertNull(result);
}

@Test
public void deleteUser() {
final String s = "{\"user_delete\":{\"id\":771136850,\"timestamp_ms\":\"1432228153548\"}}";
Expand Down Expand Up @@ -123,6 +140,16 @@ public void statusWithheld() {
assertEquals(new Date(1476731813446L), result.getUpdated());
}

@Test
public void userWithheld() {
final String s = "{\"user_withheld\":{\"id\":1375036644,\"timestamp_ms\":\"1481626100690\",\"withheld_in_countries\":[\"TR\"]}}";
final Activity result = unmarshaller.unmarshall(s);
assertEquals(ComplianceActivity.Verb.USER_WITHHELD, result.getVerb());
assertEquals("1375036644", result.getActor().getId());
assertEquals(Arrays.asList("TR"), result.getWithheldInCountries());
assertEquals(new Date(1481626100690L), result.getUpdated());
}

@Test
public void unrecognizedMesssageType() {
final String s = "{\"unknown\":{\"id\":3293130873,\"timestamp_ms\":\"1432228193828\"}}";
Expand Down