Skip to content

Commit

Permalink
Fix search - implement new cabbage API
Browse files Browse the repository at this point in the history
  • Loading branch information
solkin committed Oct 8, 2023
1 parent 9a8c448 commit 1c76490
Showing 4 changed files with 77 additions and 224 deletions.
10 changes: 0 additions & 10 deletions app/src/main/java/com/tomclaw/mandarin/core/RequestHelper.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@
import com.tomclaw.mandarin.im.icq.BuddyAvatarRequest;
import com.tomclaw.mandarin.im.icq.BuddyIgnoreStateRequest;
import com.tomclaw.mandarin.im.icq.BuddyInfoRequest;
import com.tomclaw.mandarin.im.icq.BuddyPresenceRequest;
import com.tomclaw.mandarin.im.icq.BuddyRemoveRequest;
import com.tomclaw.mandarin.im.icq.BuddyRenameRequest;
import com.tomclaw.mandarin.im.icq.BuddySearchRequest;
@@ -32,8 +31,6 @@
import com.tomclaw.mandarin.util.GsonSingleton;
import com.tomclaw.mandarin.util.QueryBuilder;

import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: solkin
@@ -203,13 +200,6 @@ public static void requestSearch(ContentResolver contentResolver, String appSess
insertRequest(contentResolver, Request.REQUEST_TYPE_SHORT, false, accountDbId, appSession, buddySearchRequest);
}

public static void requestBuddyPresence(ContentResolver contentResolver, String appSession, int accountDbId,
int total, int skipped, List<String> buddyIds,
IcqSearchOptionsBuilder searchOptions) {
BuddyPresenceRequest request = new BuddyPresenceRequest(total, skipped, buddyIds, searchOptions);
insertRequest(contentResolver, Request.REQUEST_TYPE_SHORT, false, accountDbId, appSession, request);
}

public static void requestSearchAvatar(ContentResolver contentResolver, int accountDbId, String buddyId,
String appSession, String url) {
SearchAvatarRequest searchAvatarRequest = new SearchAvatarRequest(buddyId, url);

This file was deleted.

130 changes: 76 additions & 54 deletions app/src/main/java/com/tomclaw/mandarin/im/icq/BuddySearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package com.tomclaw.mandarin.im.icq;

import static com.tomclaw.mandarin.im.icq.WimConstants.CODE;
import static com.tomclaw.mandarin.im.icq.WimConstants.RESULTS_OBJECT;
import static com.tomclaw.mandarin.im.icq.WimConstants.STATUS_OBJECT;
import static com.tomclaw.mandarin.im.icq.WimConstants.WEB_RAPI_BASE;
import static com.tomclaw.mandarin.util.StringUtil.generateRandomWord;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.tomclaw.mandarin.core.CoreService;
import com.tomclaw.mandarin.core.RequestHelper;
import com.tomclaw.mandarin.im.ShortBuddyInfo;
import com.tomclaw.mandarin.util.HttpParamsBuilder;
import com.tomclaw.mandarin.util.HttpUtil;
import com.tomclaw.mandarin.util.JsonParamsBuilder;
import com.tomclaw.mandarin.util.ParamsBuilder;
import com.tomclaw.mandarin.util.StringUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import static com.tomclaw.mandarin.im.icq.WimConstants.RESPONSE_OBJECT;
import static com.tomclaw.mandarin.im.icq.WimConstants.STATUS_CODE;
import static com.tomclaw.mandarin.im.icq.WimConstants.WEB_API_BASE;

/**
* Created by Igor on 26.06.2014.
*/
@@ -36,18 +41,17 @@ public class BuddySearchRequest extends WimRequest {
public static final String NO_SEARCH_RESULT_CASE = "no_search_result_case";

private IcqSearchOptionsBuilder searchOptions;
private int nToGet;
private int nToSkip;
private String locale;

public BuddySearchRequest() {
}

public BuddySearchRequest(IcqSearchOptionsBuilder searchOptions, int nToGet, int nToSkip, String locale) {
this.searchOptions = searchOptions;
this.nToGet = nToGet;
this.nToSkip = nToSkip;
this.locale = locale;
}

@Override
protected String getHttpRequestType() {
return HttpUtil.POST;
}

@Override
@@ -57,63 +61,81 @@ protected JSONObject parseResponse(String responseString) throws JSONException {

@Override
protected int parseJson(JSONObject response) throws JSONException {
Intent intent;
// Start to JSON parsing.
JSONObject responseObject = response.optJSONObject(RESPONSE_OBJECT);
int statusCode = 0;
if (responseObject != null) {
statusCode = responseObject.optInt(STATUS_CODE);
} else {
RequestHelper.requestBuddyPresence(getAccountRoot().getContentResolver(),
CoreService.getAppSession(), getAccountRoot().getAccountDbId(),
0, 0, Collections.<String>emptyList(), searchOptions);
return REQUEST_DELETE;
}
JSONObject results = response.getJSONObject(RESULTS_OBJECT);
JSONObject statusObject = response.getJSONObject(STATUS_OBJECT);
int statusCode = statusObject.getInt(CODE);
// Check for server reply.
if (statusCode == WIM_OK) {
JSONObject data = responseObject.getJSONObject("data");
JSONObject results = data.getJSONObject("results");
int total = results.getInt("nTotal");
int skipped = results.getInt("nSkipped");
int profiles = results.getInt("nProfiles");
List<String> userIds = new ArrayList<>();
if (profiles > 0) {
JSONArray infoArray = results.getJSONArray("infoArray");
for (int i = 0; i < infoArray.length(); i++) {
JSONObject buddyInfo = infoArray.getJSONObject(i);
JSONObject profile = buddyInfo.getJSONObject("profile");
// Obtain buddy id from profile.
String buddyId = profile.getString("aimId");
userIds.add(buddyId);
if (statusCode == CABBAGE_OK) {
Map<String, ShortBuddyInfo> shortInfoMap = new HashMap<>();
JSONArray persons = results.getJSONArray("persons");
boolean finish = results.optBoolean("finish", false);
if (persons.length() > 0) {
IcqAccountRoot accountRoot = getAccountRoot();
for (int i = 0; i < persons.length(); i++) {
JSONObject person = persons.getJSONObject(i);
String buddyId = person.getString("sn");
if (buddyId.equals(accountRoot.getUserId())) {
continue;
}
String friendly = person.optString("friendly");
String firstName = person.optString("firstName");
String lastName = person.optString("lastName");
String buddyIcon = HttpUtil.getAvatarUrl(buddyId);
String avatarHash = null;
// Check avatar fields be able to modify.
if (!TextUtils.isEmpty(buddyIcon)) {
avatarHash = HttpUtil.getUrlHash(buddyIcon);
Context context = accountRoot.getContext();
RequestHelper.requestLargeAvatar(
context.getContentResolver(), getAccountRoot().getAccountDbId(),
buddyId, CoreService.getAppSession(), buddyIcon);
}
ShortBuddyInfo buddyInfo = new ShortBuddyInfo(buddyId);
buddyInfo.setBuddyNick(friendly);
buddyInfo.setFirstName(firstName);
buddyInfo.setLastName(lastName);
buddyInfo.setAvatarHash(avatarHash);
shortInfoMap.put(buddyId, buddyInfo);
}
}
// Delegate request to get buddies avatars.
RequestHelper.requestBuddyPresence(getAccountRoot().getContentResolver(),
CoreService.getAppSession(), getAccountRoot().getAccountDbId(),
total, skipped, userIds, searchOptions);
intent = BuddySearchRequest.getSearchResultIntent(getAccountRoot().getAccountDbId(),
searchOptions, 0, 0, shortInfoMap);
} else {
// We must send intent in any case,
// because our request is going to be deleted.
getService().sendBroadcast(getNoResultIntent(
getAccountRoot().getAccountDbId(), searchOptions));
intent = BuddySearchRequest.getNoResultIntent(getAccountRoot().getAccountDbId(), searchOptions);
}
// We must send intent in any case,
// because our request is going to be deleted.
getService().sendBroadcast(intent);
return REQUEST_DELETE;
}

@Override
protected String getUrl() {
return WEB_API_BASE.concat("memberDir/search");
return WEB_RAPI_BASE.concat("v92/rapi/search");
}

@Override
protected HttpParamsBuilder getParams() {
return new HttpParamsBuilder()
.appendParam("aimsid", getAccountRoot().getAimSid())
.appendParam("f", "json")
.appendParam("infoLevel", "min")
.appendParam("nToSkip", String.valueOf(nToSkip))
.appendParam("nToGet", String.valueOf(nToGet))
.appendParam("locale", locale)
.appendParam("match", searchOptions.toString());
public Map<String, String> getRequestProperties() {
return Collections.singletonMap("Content-Type", "application/json;charset=UTF-8");
}

@Override
protected ParamsBuilder getParams() {
JsonParamsBuilder builder = new JsonParamsBuilder();
try {
builder.put("reqId", generateRandomWord());
builder.put("aimsid", getAccountRoot().getAimSid());
JSONObject params = new JSONObject();
params.put("keyword", searchOptions.getKeyword());
builder.put("params", params);
} catch (JSONException e) {
e.printStackTrace();
}
return builder;
}

private static Intent getBaseIntent(int accountDbId, IcqSearchOptionsBuilder searchOptions) {
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.1.1'
classpath 'com.android.tools.build:gradle:8.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

0 comments on commit 1c76490

Please sign in to comment.