Skip to content

Commit 19d3807

Browse files
committed
v2.getLikingUsers, v2.getLikedTweets, v2.likeTweet, v2.unlikeTweet
1 parent 147d5c6 commit 19d3807

File tree

5 files changed

+142
-141
lines changed

5 files changed

+142
-141
lines changed

twitter4j-v2-support-kotlin-example/src/main/kotlin/twitter4j_v2_support_example/getLikingUsersExample.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {
2121
// getLikingUsers example
2222
//--------------------------------------------------
2323
val statusId = 1435645603065778176L
24-
twitter.getLikingUsers(
24+
twitter.v2.getLikingUsers(
2525
statusId,
2626
expansions = "pinned_tweet_id",
2727
tweetFields = V2DefaultFields.tweetFields,
@@ -35,7 +35,7 @@ fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {
3535

3636
println("minimum query")
3737
println("=============")
38-
twitter.getLikingUsers(
38+
twitter.v2.getLikingUsers(
3939
statusId
4040
).let {
4141
println(it)

twitter4j-v2-support/src/main/kotlin/twitter4j/LikesEx.kt

Lines changed: 0 additions & 135 deletions
This file was deleted.

twitter4j-v2-support/src/main/kotlin/twitter4j/TwitterV2.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,61 @@ interface TwitterV2 {
267267
userId: Long,
268268
tweetId: Long
269269
): BooleanResponse
270+
271+
/**
272+
* Allows you to get information about a Tweet’s liking users.
273+
*
274+
* @throws TwitterException when Twitter service or network is unavailable
275+
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users"
276+
*/
277+
@Throws(TwitterException::class)
278+
fun getLikingUsers(
279+
tweetId: Long,
280+
expansions: String? = null,
281+
tweetFields: String? = null,
282+
userFields: String? = null,
283+
): UsersResponse
284+
285+
/**
286+
* Allows you to get information about a user’s liked Tweets.
287+
*
288+
* @throws TwitterException when Twitter service or network is unavailable
289+
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-users-id-liked_tweets"
290+
*/
291+
@Throws(TwitterException::class)
292+
fun getLikedTweets(
293+
userId: Long,
294+
expansions: String? = null,
295+
maxResults: Int? = null,
296+
paginationToken: PaginationToken? = null,
297+
mediaFields: String? = null,
298+
placeFields: String? = null,
299+
pollFields: String? = null,
300+
tweetFields: String? = null,
301+
userFields: String? = null,
302+
): TweetsResponse
303+
304+
/**
305+
* Causes the user ID identified in the path parameter to Like the target Tweet.
306+
*
307+
* @throws TwitterException when Twitter service or network is unavailable
308+
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/post-users-id-likes"
309+
*/
310+
@Throws(TwitterException::class)
311+
fun likeTweet(
312+
userId: Long,
313+
tweetId: Long
314+
): BooleanResponse
315+
316+
/**
317+
* Allows a user or authenticated user ID to unlike a Tweet.
318+
*
319+
* @throws TwitterException when Twitter service or network is unavailable
320+
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/delete-users-id-likes-tweet_id"
321+
*/
322+
@Throws(TwitterException::class)
323+
fun unlikeTweet(
324+
userId: Long,
325+
tweetId: Long
326+
): BooleanResponse
270327
}

twitter4j-v2-support/src/main/kotlin/twitter4j/TwitterV2Impl.kt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,85 @@ class TwitterV2Impl(private val twitter: Twitter) : TwitterV2 {
611611
)
612612
}
613613

614+
@Throws(TwitterException::class)
615+
override fun getLikingUsers(
616+
tweetId: Long,
617+
expansions: String?,
618+
tweetFields: String?,
619+
userFields: String?,
620+
): UsersResponse {
621+
622+
val params = ArrayList<HttpParameter>()
623+
624+
V2Util.addHttpParamIfNotNull(params, "expansions", expansions)
625+
V2Util.addHttpParamIfNotNull(params, "tweet.fields", tweetFields)
626+
V2Util.addHttpParamIfNotNull(params, "user.fields", userFields)
627+
628+
return V2ResponseFactory().createUsersResponse(
629+
get(conf.v2Configuration.baseURL + "tweets/" + tweetId + "/liking_users", params.toTypedArray()),
630+
conf
631+
)
632+
}
633+
634+
@Throws(TwitterException::class)
635+
override fun getLikedTweets(
636+
userId: Long,
637+
expansions: String?,
638+
maxResults: Int?,
639+
paginationToken: PaginationToken?,
640+
mediaFields: String?,
641+
placeFields: String?,
642+
pollFields: String?,
643+
tweetFields: String?,
644+
userFields: String?,
645+
): TweetsResponse {
646+
647+
val params = ArrayList<HttpParameter>()
648+
649+
V2Util.addHttpParamIfNotNull(params, "expansions", expansions)
650+
V2Util.addHttpParamIfNotNull(params, "max_results", maxResults)
651+
V2Util.addHttpParamIfNotNull(params, "pagination_token", paginationToken)
652+
V2Util.addHttpParamIfNotNull(params, "media.fields", mediaFields)
653+
V2Util.addHttpParamIfNotNull(params, "place.fields", placeFields)
654+
V2Util.addHttpParamIfNotNull(params, "poll.fields", pollFields)
655+
V2Util.addHttpParamIfNotNull(params, "tweet.fields", tweetFields)
656+
V2Util.addHttpParamIfNotNull(params, "user.fields", userFields)
657+
658+
return V2ResponseFactory().createTweetsResponse(
659+
get(conf.v2Configuration.baseURL + "users/" + userId + "/liked_tweets", params.toTypedArray()),
660+
conf
661+
)
662+
}
663+
664+
@Throws(TwitterException::class)
665+
override fun likeTweet(
666+
userId: Long,
667+
tweetId: Long
668+
): BooleanResponse {
669+
670+
val json = JSONObject()
671+
json.put("tweet_id", tweetId.toString())
672+
673+
return V2ResponseFactory().createBooleanResponse(
674+
post(conf.v2Configuration.baseURL + "users/" + userId + "/likes", arrayOf(HttpParameter(json))),
675+
conf,
676+
"liked"
677+
)
678+
}
679+
680+
@Throws(TwitterException::class)
681+
override fun unlikeTweet(
682+
userId: Long,
683+
tweetId: Long
684+
): BooleanResponse {
685+
686+
return V2ResponseFactory().createBooleanResponse(
687+
delete(conf.v2Configuration.baseURL + "users/" + userId + "/likes/" + tweetId),
688+
conf,
689+
"liked"
690+
)
691+
}
692+
614693
//--------------------------------------------------
615694
// get/post/delete
616695
//--------------------------------------------------

twitter4j-v2-support/src/test/kotlin/twitter4j/LikesTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LikesTest {
1313

1414
// https://twitter.com/TwitterDev/status/1430984356139470849
1515
val tweetId = 1430984356139470849L
16-
val res = twitter.getLikingUsers(tweetId)
16+
val res = twitter.v2.getLikingUsers(tweetId)
1717
println(res)
1818

1919
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
@@ -28,7 +28,7 @@ class LikesTest {
2828
@Test
2929
fun getLikedTweets_simple() {
3030

31-
val res = twitter.getLikedTweets(myId)
31+
val res = twitter.v2.getLikedTweets(myId)
3232

3333
// no dump
3434
println("res.tweets.size: " + res.tweets.size)
@@ -44,7 +44,7 @@ class LikesTest {
4444
val tweetId = 1430984356139470849L
4545

4646
// like
47-
twitter.likeTweet(myId, tweetId).let { res ->
47+
twitter.v2.likeTweet(myId, tweetId).let { res ->
4848
println(res)
4949
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
5050
println(json.toString(3))
@@ -56,7 +56,7 @@ class LikesTest {
5656
Thread.sleep(2000)
5757

5858
// unlike
59-
twitter.unlikeTweet(myId, tweetId).let { res ->
59+
twitter.v2.unlikeTweet(myId, tweetId).let { res ->
6060
println(res)
6161
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
6262
println(json.toString(3))

0 commit comments

Comments
 (0)