Skip to content

Commit

Permalink
v2.getLikingUsers, v2.getLikedTweets, v2.likeTweet, v2.unlikeTweet
Browse files Browse the repository at this point in the history
  • Loading branch information
takke committed Oct 17, 2022
1 parent 147d5c6 commit 19d3807
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {
// getLikingUsers example
//--------------------------------------------------
val statusId = 1435645603065778176L
twitter.getLikingUsers(
twitter.v2.getLikingUsers(
statusId,
expansions = "pinned_tweet_id",
tweetFields = V2DefaultFields.tweetFields,
Expand All @@ -35,7 +35,7 @@ fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {

println("minimum query")
println("=============")
twitter.getLikingUsers(
twitter.v2.getLikingUsers(
statusId
).let {
println(it)
Expand Down
135 changes: 0 additions & 135 deletions twitter4j-v2-support/src/main/kotlin/twitter4j/LikesEx.kt

This file was deleted.

57 changes: 57 additions & 0 deletions twitter4j-v2-support/src/main/kotlin/twitter4j/TwitterV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,61 @@ interface TwitterV2 {
userId: Long,
tweetId: Long
): BooleanResponse

/**
* Allows you to get information about a Tweet’s liking users.
*
* @throws TwitterException when Twitter service or network is unavailable
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users"
*/
@Throws(TwitterException::class)
fun getLikingUsers(
tweetId: Long,
expansions: String? = null,
tweetFields: String? = null,
userFields: String? = null,
): UsersResponse

/**
* Allows you to get information about a user’s liked Tweets.
*
* @throws TwitterException when Twitter service or network is unavailable
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-users-id-liked_tweets"
*/
@Throws(TwitterException::class)
fun getLikedTweets(
userId: Long,
expansions: String? = null,
maxResults: Int? = null,
paginationToken: PaginationToken? = null,
mediaFields: String? = null,
placeFields: String? = null,
pollFields: String? = null,
tweetFields: String? = null,
userFields: String? = null,
): TweetsResponse

/**
* Causes the user ID identified in the path parameter to Like the target Tweet.
*
* @throws TwitterException when Twitter service or network is unavailable
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/post-users-id-likes"
*/
@Throws(TwitterException::class)
fun likeTweet(
userId: Long,
tweetId: Long
): BooleanResponse

/**
* Allows a user or authenticated user ID to unlike a Tweet.
*
* @throws TwitterException when Twitter service or network is unavailable
* @see "https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/delete-users-id-likes-tweet_id"
*/
@Throws(TwitterException::class)
fun unlikeTweet(
userId: Long,
tweetId: Long
): BooleanResponse
}
79 changes: 79 additions & 0 deletions twitter4j-v2-support/src/main/kotlin/twitter4j/TwitterV2Impl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,85 @@ class TwitterV2Impl(private val twitter: Twitter) : TwitterV2 {
)
}

@Throws(TwitterException::class)
override fun getLikingUsers(
tweetId: Long,
expansions: String?,
tweetFields: String?,
userFields: String?,
): UsersResponse {

val params = ArrayList<HttpParameter>()

V2Util.addHttpParamIfNotNull(params, "expansions", expansions)
V2Util.addHttpParamIfNotNull(params, "tweet.fields", tweetFields)
V2Util.addHttpParamIfNotNull(params, "user.fields", userFields)

return V2ResponseFactory().createUsersResponse(
get(conf.v2Configuration.baseURL + "tweets/" + tweetId + "/liking_users", params.toTypedArray()),
conf
)
}

@Throws(TwitterException::class)
override fun getLikedTweets(
userId: Long,
expansions: String?,
maxResults: Int?,
paginationToken: PaginationToken?,
mediaFields: String?,
placeFields: String?,
pollFields: String?,
tweetFields: String?,
userFields: String?,
): TweetsResponse {

val params = ArrayList<HttpParameter>()

V2Util.addHttpParamIfNotNull(params, "expansions", expansions)
V2Util.addHttpParamIfNotNull(params, "max_results", maxResults)
V2Util.addHttpParamIfNotNull(params, "pagination_token", paginationToken)
V2Util.addHttpParamIfNotNull(params, "media.fields", mediaFields)
V2Util.addHttpParamIfNotNull(params, "place.fields", placeFields)
V2Util.addHttpParamIfNotNull(params, "poll.fields", pollFields)
V2Util.addHttpParamIfNotNull(params, "tweet.fields", tweetFields)
V2Util.addHttpParamIfNotNull(params, "user.fields", userFields)

return V2ResponseFactory().createTweetsResponse(
get(conf.v2Configuration.baseURL + "users/" + userId + "/liked_tweets", params.toTypedArray()),
conf
)
}

@Throws(TwitterException::class)
override fun likeTweet(
userId: Long,
tweetId: Long
): BooleanResponse {

val json = JSONObject()
json.put("tweet_id", tweetId.toString())

return V2ResponseFactory().createBooleanResponse(
post(conf.v2Configuration.baseURL + "users/" + userId + "/likes", arrayOf(HttpParameter(json))),
conf,
"liked"
)
}

@Throws(TwitterException::class)
override fun unlikeTweet(
userId: Long,
tweetId: Long
): BooleanResponse {

return V2ResponseFactory().createBooleanResponse(
delete(conf.v2Configuration.baseURL + "users/" + userId + "/likes/" + tweetId),
conf,
"liked"
)
}

//--------------------------------------------------
// get/post/delete
//--------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions twitter4j-v2-support/src/test/kotlin/twitter4j/LikesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LikesTest {

// https://twitter.com/TwitterDev/status/1430984356139470849
val tweetId = 1430984356139470849L
val res = twitter.getLikingUsers(tweetId)
val res = twitter.v2.getLikingUsers(tweetId)
println(res)

val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
Expand All @@ -28,7 +28,7 @@ class LikesTest {
@Test
fun getLikedTweets_simple() {

val res = twitter.getLikedTweets(myId)
val res = twitter.v2.getLikedTweets(myId)

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

// like
twitter.likeTweet(myId, tweetId).let { res ->
twitter.v2.likeTweet(myId, tweetId).let { res ->
println(res)
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
println(json.toString(3))
Expand All @@ -56,7 +56,7 @@ class LikesTest {
Thread.sleep(2000)

// unlike
twitter.unlikeTweet(myId, tweetId).let { res ->
twitter.v2.unlikeTweet(myId, tweetId).let { res ->
println(res)
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
println(json.toString(3))
Expand Down

0 comments on commit 19d3807

Please sign in to comment.