Skip to content

Commit

Permalink
Support Get Recent Messages and Lock Section
Browse files Browse the repository at this point in the history
  • Loading branch information
kenichi-ando committed Oct 23, 2020
1 parent 02b650e commit 2e2b689
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 67 deletions.
27 changes: 0 additions & 27 deletions .classpath

This file was deleted.

14 changes: 0 additions & 14 deletions .project

This file was deleted.

61 changes: 61 additions & 0 deletions src/main/java/kenichia/quipapi/QuipDiff.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package kenichia.quipapi;

import java.util.stream.Stream;

import com.google.gson.JsonObject;

public class QuipDiff extends QuipJsonObject {

// ============================================
// Enum
// ============================================

public enum DiffClass {
TRACK_CHANGES("track_changes"),
DELETE_ONLY("delete_only"),
INSERT_ONLY("insert_only"),
INSERT_COMPLETELY("insert_completely");

private final String _value;

private DiffClass(String value) {
_value = value;
}

private static DiffClass find(String value) {
return Stream.of(values()).filter(e -> e._value.equals(value)).findFirst().orElse(null);
}
}

// ============================================
// Constructor
// ============================================

protected QuipDiff(JsonObject json) {
super(json);
}

// ============================================
// Properties
// ============================================

public String getRtml() {
return _getString("rtml");
}

public String getList() {
return _getString("list");
}

public DiffClass getDiffClass() {
return DiffClass.find(_getString("diff_class"));
}

public String getSectionId() {
return _getString("section_id");
}

public String getStyle() {
return _getString("style");
}
}
31 changes: 31 additions & 0 deletions src/main/java/kenichia/quipapi/QuipDiffGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kenichia.quipapi;

import java.util.stream.StreamSupport;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class QuipDiffGroup extends QuipJsonObject {

// ============================================
// Constructor
// ============================================

protected QuipDiffGroup(JsonObject json) {
super(json);
}

// ============================================
// Properties
// ============================================

public QuipDiff[] getDiffs() {
JsonArray arr = _getJsonArray("diffs");
if (arr == null)
return null;
QuipDiff[] diffs = StreamSupport.stream(arr.spliterator(), false)
.map(obj -> new QuipDiff(obj.getAsJsonObject()))
.toArray(QuipDiff[]::new);
return diffs;
}
}
2 changes: 1 addition & 1 deletion src/main/java/kenichia/quipapi/QuipFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private Color(String value) {
}

private static Color find(String value) {
return Stream.of(values()).filter(e -> e._value.equals(value)).findFirst().orElse(MANILA);
return Stream.of(values()).filter(e -> e._value.equals(value)).findFirst().orElse(null);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/kenichia/quipapi/QuipJsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,25 @@ protected String _getString(String key) {

protected boolean _getBoolean(String key) {
JsonElement element = _jsonObject.get(key);
return (element == null) ? null : element.getAsBoolean();
return (element == null) ? false : element.getAsBoolean();
}

protected boolean _getBoolean(String keyToJsonObject, String keyToBoolean) {
JsonObject object = _getJsonObject(keyToJsonObject);
if (object == null)
return false;
JsonElement element = object.get(keyToBoolean);
return (element == null) ? false : element.getAsBoolean();
}

protected int _getInt(String key) {
JsonElement element = _jsonObject.get(key);
return (element == null) ? null : element.getAsInt();
return (element == null) ? 0 : element.getAsInt();
}

protected double _getDouble(String key) {
JsonElement element = _jsonObject.get(key);
return (element == null) ? null : element.getAsDouble();
return (element == null) ? 0.0 : element.getAsDouble();
}

protected JsonObject _getJsonObject(String key) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/kenichia/quipapi/QuipMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.google.gson.JsonObject;

public class QuipMessage extends QuipJsonObject {

// ============================================
// Constructor
// ============================================
Expand Down Expand Up @@ -68,4 +68,14 @@ public String[] getFiles() {
.map(e -> e.getAsJsonObject().get("name").getAsString())
.toArray(String[]::new);
}

public QuipDiffGroup[] getDiffGroups() {
JsonArray arr = _getJsonArray("diff_groups");
if (arr == null)
return null;
QuipDiffGroup[] diffGroups = StreamSupport.stream(arr.spliterator(), false)
.map(obj -> new QuipDiffGroup(obj.getAsJsonObject()))
.toArray(QuipDiffGroup[]::new);
return diffGroups;
}
}
45 changes: 40 additions & 5 deletions src/main/java/kenichia/quipapi/QuipThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private Type(String value) {
}

private static Type find(String value) {
return Stream.of(values()).filter(e -> e._value.equals(value)).findFirst().orElse(DOCUMENT);
return Stream.of(values()).filter(e -> e._value.equals(value)).findFirst().orElse(null);
}
}

Expand Down Expand Up @@ -90,6 +90,17 @@ private Mode(String value) {
}
};

public enum MessageType {
MESSAGE("message"),
EDIT("edit");

private final String _value;

private MessageType(String value) {
_value = value;
}
};

// ============================================
// Constructor
// ============================================
Expand Down Expand Up @@ -129,11 +140,15 @@ public String getSharing() {
public Type getType() {
return Type.find(_getString("thread", "type"));
}

public String getAuthorId() {
return _getString("thread", "author_id");
}

public boolean isDeleted() {
return _getBoolean("thread", "is_deleted");
}

public String[] getSharedFolderIds() {
return _getStringArray("shared_folder_ids");
}
Expand Down Expand Up @@ -197,7 +212,7 @@ public boolean reload() throws Exception {
// ============================================
// Create / Update / Delete
// ============================================

public static QuipThread createDocument(String title, String content, String[] memberIds, Format format, Type type) throws Exception {
Form form = Form.form();
if (title != null)
Expand Down Expand Up @@ -283,13 +298,25 @@ public void delete() throws Exception {
.add("thread_id", getId()));
}

// ============================================
// Lock
// ============================================

public void lockEdits(Boolean isEditsDisabled) throws Exception {
_postToJsonObject("https://platform.quip.com/1/threads/lock-edits",
Form.form()
.add("thread_id", getId())
.add("edits_disabled", String.valueOf(isEditsDisabled)));
}

public void lockSectionEdits(String sectionId, Boolean isEditsDisabled) throws Exception {
_postToJsonObject("https://platform.quip.com/1/threads/lock-section-edits",
Form.form()
.add("thread_id", getId())
.add("section_id", sectionId)
.add("edits_disabled", String.valueOf(isEditsDisabled)));
}

// ============================================
// Import / Export
// ============================================
Expand Down Expand Up @@ -321,8 +348,16 @@ public byte[] exportAsPdf() throws Exception {
// Messages
// ============================================

public QuipMessage[] getRecentMessages() throws Exception {
JsonArray arr = _getToJsonArray("https://platform.quip.com/1/messages/" + getId());
public QuipMessage[] getRecentMessages(Integer count, Instant maxCreatedUsec, MessageType messageType) throws Exception {
List<NameValuePair> params = new ArrayList<>();
if (count != null)
params.add(new BasicNameValuePair("count", String.valueOf(count)));
if (maxCreatedUsec != null)
params.add(new BasicNameValuePair("max_created_usec", String.valueOf(maxCreatedUsec)));
if (messageType != null)
params.add(new BasicNameValuePair("message_type", messageType._value));
JsonArray arr = _getToJsonArray(
new URIBuilder("https://platform.quip.com/1/messages/" + getId()).addParameters(params).build());
return StreamSupport.stream(arr.spliterator(), false)
.map(obj -> new QuipMessage(obj.getAsJsonObject()))
.toArray(QuipMessage[]::new);
Expand Down
Loading

0 comments on commit 2e2b689

Please sign in to comment.