Created
September 24, 2017 09:48
-
-
Save tateisu/b053500eaec33a38e3eb9a0733f5e673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static final String PAYLOAD = "payload"; | |
// ストリーミングAPIのペイロード部分をTootStatus,TootNotification,整数IDのどれかに解釈する | |
private Object parsePayload( @NonNull String event, @NonNull JSONObject parent, @NonNull String parent_text ){ | |
try{ | |
if( parent.isNull( PAYLOAD ) ){ | |
return null; | |
} | |
Object payload = parent.opt( PAYLOAD ); | |
if( payload instanceof JSONObject ){ | |
JSONObject src = (JSONObject)payload; | |
switch( event ){ | |
case "update": | |
// ここを通るケースはまだ確認できていない | |
return TootStatus.parse( context, access_info, src ); | |
case "notification": | |
// ここを通るケースはまだ確認できていない | |
return TootNotification.parse( context, access_info, src ); | |
default: | |
// ここを通るケースはまだ確認できていない | |
log.e( "unknown payload(1). message=%s", parent_text ); | |
return null; | |
} | |
} | |
if( payload instanceof Number ){ | |
// 2017/8/24 18:37 mastodon.juggler.jpでここを通った | |
return ( (Number) payload ).longValue(); | |
} | |
if( payload instanceof String ){ | |
String sv = (String) payload; | |
if( sv.charAt( 0 ) == '{' ){ | |
JSONObject src = new JSONObject( sv ); | |
switch( event ){ | |
case "update": | |
// 2017/8/24 18:37 mastodon.juggler.jpでここを通った | |
return TootStatus.parse( context, access_info, src ); | |
case "notification": | |
// 2017/8/24 18:37 mastodon.juggler.jpでここを通った | |
return TootNotification.parse( context, access_info, src ); | |
default: | |
// ここを通るケースはまだ確認できていない | |
log.e( "unknown payload(2). message=%s", parent_text ); | |
return null; | |
} | |
} | |
// 2017/8/24 18:37 mdx.ggtea.org でここを通った | |
Matcher m = reNumber.matcher( sv ); | |
if( m.find() ){ | |
return Long.parseLong( m.group( 1 ), 10 ); | |
} | |
} | |
// ここを通るケースはまだ確認できていない | |
log.e( "unknown payload(3). message=%s", parent_text ); | |
}catch( Throwable ex ){ | |
log.trace( ex ); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment