|
1 | 1 | package org.mushare.pluto; |
2 | 2 |
|
| 3 | +import com.github.kevinsawicki.http.HttpRequest; |
| 4 | +import net.sf.json.JSONArray; |
3 | 5 | import net.sf.json.JSONObject; |
4 | 6 | import org.mushare.pluto.exception.PlutoErrorCode; |
5 | 7 | import org.mushare.pluto.exception.PlutoException; |
6 | 8 |
|
7 | 9 | import java.io.UnsupportedEncodingException; |
8 | 10 | import java.security.Signature; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
9 | 13 | import java.util.Base64; |
10 | 14 | import java.util.List; |
| 15 | +import java.util.stream.Collectors; |
| 16 | +import java.util.stream.IntStream; |
| 17 | +import java.util.stream.Stream; |
11 | 18 |
|
12 | 19 | public class Pluto { |
13 | 20 |
|
@@ -71,9 +78,31 @@ public static PlutoUser auth(String token) throws PlutoException { |
71 | 78 | return new PlutoUser(payload); |
72 | 79 | } |
73 | 80 |
|
74 | | - public static List<PlutoUserInfo> fetUserInfos(long[] userIds) { |
75 | | - |
76 | | - return null; |
| 81 | + public static List<PlutoUserInfo> fetUserInfos(List<Long> userIds) { |
| 82 | + if (userIds == null || userIds.size() == 0) { |
| 83 | + return new ArrayList<>(); |
| 84 | + } |
| 85 | + String ids = userIds.stream() |
| 86 | + .map(userId -> userId + "-") |
| 87 | + .reduce("", (s1, s2) -> { |
| 88 | + return s1 + s2; |
| 89 | + }); |
| 90 | + ids = ids.substring(0, ids.length() - 1); |
| 91 | + String response = HttpRequest.get(shared.server + "api/user/info/" + ids).body(); |
| 92 | + JSONArray body = JSONObject.fromObject(response).getJSONArray("body"); |
| 93 | + return IntStream.range(0, body.size()) |
| 94 | + .mapToObj(index -> { |
| 95 | + JSONObject object = body.getJSONObject(index); |
| 96 | + PlutoUserInfo info = new PlutoUserInfo(); |
| 97 | + info.setUserId(object.getLong("id")); |
| 98 | + if (object.containsKey("err_code") && object.getInt("err_code") == 403) { |
| 99 | + return info; |
| 100 | + } |
| 101 | + info.setAvatar(object.getString("avatar")); |
| 102 | + info.setName(object.getString("name")); |
| 103 | + return info; |
| 104 | + }) |
| 105 | + .collect(Collectors.toList()); |
77 | 106 | } |
78 | 107 |
|
79 | 108 | } |
0 commit comments