Skip to content

Commit 1411690

Browse files
refactor(js-api): remove noteId param for get/setNoteTags
Now consistent with other methods: the currently selected note is used * Stop special-casing `ankiGetNoteTags` as no params are needed * Adjust Object.keys usage accordingly * Move jsonObjectOf to JsonUtils.kt Issue 17716 Co-authored-by: David Allison <62114487+david-allison@users.noreply.github.com>
1 parent 18d14c2 commit 1411690

4 files changed

Lines changed: 12 additions & 30 deletions

File tree

AnkiDroid/src/main/assets/scripts/js-api.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Object.keys(jsApiList).forEach(method => {
129129
return;
130130
}
131131
if (method === "ankiSetNoteTags") {
132-
AnkiDroidJS.prototype[method] = async function (noteId, tags) {
132+
AnkiDroidJS.prototype[method] = async function (tags) {
133133
let hasSpaces = false;
134134
for (let i = 0; i < tags.length; i++) {
135135
tags[i] = tags[i].trim();
@@ -142,15 +142,7 @@ Object.keys(jsApiList).forEach(method => {
142142
console.warn("Spaces in tags have been converted to underscores");
143143
}
144144
const endpoint = jsApiList[method];
145-
const data = JSON.stringify({ noteId, tags });
146-
return await this.handleRequest(endpoint, data);
147-
};
148-
return;
149-
}
150-
if (method === "ankiGetNoteTags") {
151-
AnkiDroidJS.prototype[method] = async function (noteId) {
152-
const endpoint = jsApiList[method];
153-
const data = JSON.stringify({ noteId });
145+
const data = JSON.stringify({ tags });
154146
return await this.handleRequest(endpoint, data);
155147
};
156148
return;

AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidJsAPI.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ open class AnkiDroidJsAPI(
380380

381381
"setNoteTags" -> {
382382
val jsonObject = JSONObject(apiParams)
383-
val noteId = jsonObject.getLong("noteId")
383+
val noteId = currentCard.nid
384384
val tags = jsonObject.getJSONArray("tags")
385385
withCol {
386386
fun Note.setTagsFromList(tagList: List<String>) {
@@ -403,8 +403,7 @@ open class AnkiDroidJsAPI(
403403
}
404404

405405
"getNoteTags" -> {
406-
val jsonObject = JSONObject(apiParams)
407-
val noteId = jsonObject.getLong("noteId")
406+
val noteId = currentCard.nid
408407
val noteTags =
409408
withCol {
410409
getNote(noteId).tags

AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {
428428

429429
// test get tags for note
430430
val expectedTags = n.tags
431-
val response = getDataFromRequest("getNoteTags", jsapi, jsonObjectOf("noteId" to n.id))
431+
val response = getDataFromRequest("getNoteTags", jsapi)
432432
val jsonResponse = JSONObject(response)
433433
val actualTags = JSONArray(jsonResponse.getString("value"))
434434

@@ -471,21 +471,5 @@ class AnkiDroidJsAPITest : RobolectricTest() {
471471
jsAPI
472472
.handleJsApiRequest(methodName, jsApiContract(apiData), false)
473473
.decodeToString()
474-
475-
suspend fun getDataFromRequest(
476-
methodName: String,
477-
jsAPI: AnkiDroidJsAPI,
478-
apiData: JSONObject,
479-
): String =
480-
jsAPI
481-
.handleJsApiRequest(methodName, jsApiContract(apiData.toString()), false)
482-
.decodeToString()
483474
}
484475
}
485-
486-
private fun jsonObjectOf(vararg pairs: Pair<String, Any>): JSONObject =
487-
JSONObject().apply {
488-
for ((key, value) in pairs) {
489-
put(key, value)
490-
}
491-
}

AnkiDroid/src/test/java/com/ichi2/testutils/JsonUtils.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,10 @@ class IsJsonHolderEqual(
7171
}
7272

7373
fun isJsonHolderEqual(expectedValue: String) = IsJsonHolderEqual(JSONObject(expectedValue))
74+
75+
private fun jsonObjectOf(vararg pairs: Pair<String, Any>): JSONObject =
76+
JSONObject().apply {
77+
for ((key, value) in pairs) {
78+
put(key, value)
79+
}
80+
}

0 commit comments

Comments
 (0)