11package org .mushare .pluto ;
22
3+ import com .github .kevinsawicki .http .HttpRequest ;
4+ import net .sf .json .JSONArray ;
35import net .sf .json .JSONObject ;
46import org .mushare .pluto .exception .PlutoErrorCode ;
57import org .mushare .pluto .exception .PlutoException ;
68
79import java .io .UnsupportedEncodingException ;
810import java .security .Signature ;
11+ import java .util .ArrayList ;
912import java .util .Base64 ;
13+ import java .util .List ;
14+ import java .util .stream .Collectors ;
15+ import java .util .stream .IntStream ;
1016
1117public class Pluto {
1218
1319 private PublicKeyManager keyManager ;
20+ private String server ;
1421 private String appId ;
1522
1623 private Pluto () {
@@ -21,6 +28,7 @@ private Pluto() {
2128
2229 public static void setup (String server , String appId ) {
2330 shared .keyManager = new PublicKeyManager (server );
31+ shared .server = server ;
2432 shared .appId = appId ;
2533 }
2634
@@ -68,4 +76,31 @@ public static PlutoUser auth(String token) throws PlutoException {
6876 return new PlutoUser (payload );
6977 }
7078
79+ public static List <PlutoUserInfo > fetUserInfos (List <Long > userIds ) {
80+ if (userIds == null || userIds .size () == 0 ) {
81+ return new ArrayList <>();
82+ }
83+ String ids = userIds .stream ()
84+ .map (userId -> userId + "-" )
85+ .reduce ("" , (s1 , s2 ) -> {
86+ return s1 + s2 ;
87+ });
88+ ids = ids .substring (0 , ids .length () - 1 );
89+ String response = HttpRequest .get (shared .server + "api/user/info/" + ids ).body ();
90+ JSONArray body = JSONObject .fromObject (response ).getJSONArray ("body" );
91+ return IntStream .range (0 , body .size ())
92+ .mapToObj (index -> {
93+ JSONObject object = body .getJSONObject (index );
94+ PlutoUserInfo info = new PlutoUserInfo ();
95+ info .setUserId (object .getLong ("id" ));
96+ if (object .containsKey ("err_code" ) && object .getInt ("err_code" ) == 403 ) {
97+ return info ;
98+ }
99+ info .setAvatar (object .getString ("avatar" ));
100+ info .setName (object .getString ("name" ));
101+ return info ;
102+ })
103+ .collect (Collectors .toList ());
104+ }
105+
71106}
0 commit comments