Skip to content

Commit 9b4f1d0

Browse files
committed
v2.getMutingUsers, v2.muteUser, v2.unmuteUser
1 parent 91252e1 commit 9b4f1d0

File tree

4 files changed

+116
-105
lines changed

4 files changed

+116
-105
lines changed

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

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

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,53 @@ interface TwitterV2 {
514514
targetUserId: Long
515515
): BooleanResponse
516516

517+
/**
518+
* Returns a list of users who are muted by the specified user ID.
519+
*
520+
* @throws TwitterException when Twitter service or network is unavailable
521+
* @see "https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/get-users-muting"
522+
*/
523+
@Throws(TwitterException::class)
524+
fun getMutingUsers(
525+
/**
526+
* The user ID whose muted users you would like to retrieve. The user’s ID must correspond to the user ID of the
527+
* authenticating user, meaning that you must pass the Access Tokens associated with the user ID when authenticating
528+
* your request.
529+
*/
530+
userId: Long,
531+
expansions: String? = "pinned_tweet_id",
532+
/**
533+
* The maximum number of results to be returned per page. This can be a number between 1 and 1000. By default, each page will return 100 results.
534+
*/
535+
maxResults: Int? = null,
536+
paginationToken: PaginationToken? = null,
537+
tweetFields: String? = null,
538+
userFields: String? = null,
539+
): UsersResponse
540+
541+
/**
542+
* Allows an authenticated user ID to mute the target user.
543+
*
544+
* @throws TwitterException when Twitter service or network is unavailable
545+
* @see "https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/post-users-user_id-muting"
546+
*/
547+
@Throws(TwitterException::class)
548+
fun muteUser(
549+
sourceUserId: Long,
550+
targetUserId: Long
551+
): BooleanResponse
552+
553+
/**
554+
* Allows an authenticated user ID to unmute the target user.
555+
*
556+
* @throws TwitterException when Twitter service or network is unavailable
557+
* @see "https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/delete-users-user_id-muting"
558+
*/
559+
@Throws(TwitterException::class)
560+
fun unmuteUser(
561+
sourceUserId: Long,
562+
targetUserId: Long
563+
): BooleanResponse
564+
565+
517566
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,70 @@ class TwitterV2Impl(private val twitter: Twitter) : TwitterV2 {
949949
)
950950
}
951951

952+
@Throws(TwitterException::class)
953+
override fun getMutingUsers(
954+
/**
955+
* The user ID whose muted users you would like to retrieve. The user’s ID must correspond to the user ID of the
956+
* authenticating user, meaning that you must pass the Access Tokens associated with the user ID when authenticating
957+
* your request.
958+
*/
959+
userId: Long,
960+
expansions: String?,
961+
/**
962+
* The maximum number of results to be returned per page. This can be a number between 1 and 1000. By default, each page will return 100 results.
963+
*/
964+
/**
965+
* The maximum number of results to be returned per page. This can be a number between 1 and 1000. By default, each page will return 100 results.
966+
*/
967+
maxResults: Int?,
968+
paginationToken: PaginationToken?,
969+
tweetFields: String?,
970+
userFields: String?,
971+
): UsersResponse {
972+
973+
val params = ArrayList<HttpParameter>()
974+
975+
V2Util.addHttpParamIfNotNull(params, "expansions", expansions)
976+
V2Util.addHttpParamIfNotNull(params, "max_results", maxResults)
977+
V2Util.addHttpParamIfNotNull(params, "pagination_token", paginationToken)
978+
V2Util.addHttpParamIfNotNull(params, "tweet.fields", tweetFields)
979+
V2Util.addHttpParamIfNotNull(params, "user.fields", userFields)
980+
981+
return V2ResponseFactory().createUsersResponse(
982+
get(conf.v2Configuration.baseURL + "users/" + userId + "/muting", params.toTypedArray()),
983+
conf
984+
)
985+
}
986+
987+
@Throws(TwitterException::class)
988+
override fun muteUser(
989+
sourceUserId: Long,
990+
targetUserId: Long
991+
): BooleanResponse {
992+
993+
val json = JSONObject()
994+
json.put("target_user_id", targetUserId.toString())
995+
996+
return V2ResponseFactory().createBooleanResponse(
997+
post(conf.v2Configuration.baseURL + "users/" + sourceUserId + "/muting", json),
998+
conf,
999+
"muting"
1000+
)
1001+
}
1002+
1003+
@Throws(TwitterException::class)
1004+
override fun unmuteUser(
1005+
sourceUserId: Long,
1006+
targetUserId: Long
1007+
): BooleanResponse {
1008+
1009+
return V2ResponseFactory().createBooleanResponse(
1010+
delete(conf.v2Configuration.baseURL + "users/" + sourceUserId + "/muting/" + targetUserId),
1011+
conf,
1012+
"muting"
1013+
)
1014+
}
1015+
9521016
//--------------------------------------------------
9531017
// get/post/delete
9541018
//--------------------------------------------------

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MutesTest {
1515

1616
println("muteUser")
1717
println("========")
18-
twitter.muteUser(myId, targetUserId).let { res ->
18+
twitter.v2.muteUser(myId, targetUserId).let { res ->
1919
println(res)
2020
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
2121
println(json.toString(3))
@@ -27,7 +27,7 @@ class MutesTest {
2727
println("delaying...")
2828
Thread.sleep(2000)
2929

30-
val mutingUsers = twitter.getMutingUsers(myId, null)
30+
val mutingUsers = twitter.v2.getMutingUsers(myId, null)
3131
println("muting users: ${mutingUsers.users.size}, ${mutingUsers.meta?.resultCount}")
3232
assertThat(mutingUsers.users).isNotEmpty
3333

@@ -37,7 +37,7 @@ class MutesTest {
3737

3838
println("unmuteUser")
3939
println("============")
40-
twitter.unmuteUser(myId, targetUserId).let { res ->
40+
twitter.v2.unmuteUser(myId, targetUserId).let { res ->
4141
println(res)
4242
val json = JSONObject(TwitterObjectFactory.getRawJSON(res))
4343
println(json.toString(3))

0 commit comments

Comments
 (0)