2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.util
|
|
|
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.os.Handler
|
2018-06-28 11:15:31 +02:00
|
|
|
import android.os.SystemClock
|
2019-02-15 02:51:22 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2018-01-16 07:48:17 +01:00
|
|
|
import android.text.*
|
2018-11-06 02:29:33 +01:00
|
|
|
import android.text.style.ForegroundColorSpan
|
2018-01-04 19:52:25 +01:00
|
|
|
import android.view.View
|
2019-09-14 22:09:52 +02:00
|
|
|
import jp.juggler.emoji.EmojiMap
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.App1
|
|
|
|
import jp.juggler.subwaytooter.Pref
|
|
|
|
import jp.juggler.subwaytooter.R
|
2021-02-14 18:09:23 +01:00
|
|
|
import jp.juggler.subwaytooter.Styler
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.subwaytooter.api.*
|
2018-01-10 16:47:35 +01:00
|
|
|
import jp.juggler.subwaytooter.api.entity.*
|
2019-09-12 19:16:07 +02:00
|
|
|
import jp.juggler.subwaytooter.dialog.ActionsDialog
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.dialog.DlgConfirm
|
|
|
|
import jp.juggler.subwaytooter.dialog.EmojiPicker
|
|
|
|
import jp.juggler.subwaytooter.span.MyClickableSpan
|
|
|
|
import jp.juggler.subwaytooter.span.NetworkEmojiSpan
|
|
|
|
import jp.juggler.subwaytooter.table.AcctColor
|
|
|
|
import jp.juggler.subwaytooter.table.AcctSet
|
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
|
|
|
import jp.juggler.subwaytooter.table.TagSet
|
|
|
|
import jp.juggler.subwaytooter.view.MyEditText
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.*
|
2018-01-04 19:52:25 +01:00
|
|
|
import okhttp3.Request
|
2019-08-24 05:35:22 +02:00
|
|
|
import okhttp3.RequestBody.Companion.toRequestBody
|
2018-06-28 11:43:46 +02:00
|
|
|
import java.lang.ref.WeakReference
|
2018-12-01 00:02:18 +01:00
|
|
|
import java.util.*
|
2019-07-20 08:32:43 +02:00
|
|
|
import kotlin.math.min
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class PostHelper(
|
2021-01-03 06:11:04 +01:00
|
|
|
private val activity: AppCompatActivity,
|
|
|
|
private val pref: SharedPreferences,
|
|
|
|
private val handler: Handler
|
2018-01-16 07:48:17 +01:00
|
|
|
) {
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
private val log = LogCategory("PostHelper")
|
|
|
|
|
|
|
|
private val reCharsNotEmoji = "[^0-9A-Za-z_-]".asciiPattern()
|
|
|
|
private val reAscii = """[\x00-\x7f]""".asciiPattern()
|
|
|
|
private val reNotAscii = """[^\x00-\x7f]""".asciiPattern()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PostCompleteCallback {
|
|
|
|
|
|
|
|
fun onPostComplete(target_account: SavedAccount, status: TootStatus)
|
|
|
|
fun onScheduledPostComplete(target_account: SavedAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 投稿機能
|
|
|
|
|
|
|
|
var content: String? = null
|
|
|
|
var spoiler_text: String? = null
|
|
|
|
var visibility: TootVisibility = TootVisibility.Public
|
|
|
|
var bNSFW = false
|
|
|
|
var in_reply_to_id: EntityId? = null
|
|
|
|
var attachment_list: ArrayList<PostAttachment>? = null
|
|
|
|
var enquete_items: ArrayList<String>? = null
|
|
|
|
var poll_type: TootPollsType? = null
|
|
|
|
var poll_expire_seconds = 0
|
|
|
|
var poll_hide_totals = false
|
|
|
|
var poll_multiple_choice = false
|
|
|
|
|
|
|
|
var emojiMapCustom: HashMap<String, CustomEmoji>? = null
|
|
|
|
var redraft_status_id: EntityId? = null
|
|
|
|
var useQuoteToot = false
|
|
|
|
var scheduledAt = 0L
|
|
|
|
var scheduledId: EntityId? = null
|
|
|
|
|
|
|
|
private var last_post_tapped: Long = 0L
|
|
|
|
|
|
|
|
private var last_post_task: WeakReference<TootTaskRunner>? = null
|
|
|
|
|
|
|
|
fun post(account: SavedAccount, callback: PostCompleteCallback) = post(
|
2019-09-30 23:27:21 +02:00
|
|
|
account,
|
|
|
|
callback,
|
|
|
|
bConfirmTag = false,
|
|
|
|
bConfirmAccount = false,
|
|
|
|
bConfirmRedraft = false,
|
|
|
|
bConfirmTagCharacter = false
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
fun post(
|
|
|
|
account: SavedAccount,
|
|
|
|
callback: PostCompleteCallback,
|
|
|
|
bConfirmTag: Boolean,
|
|
|
|
bConfirmAccount: Boolean,
|
|
|
|
bConfirmRedraft: Boolean,
|
|
|
|
bConfirmTagCharacter: Boolean
|
2018-01-21 17:47:13 +01:00
|
|
|
) {
|
2021-01-03 06:11:04 +01:00
|
|
|
val content = this.content ?: ""
|
|
|
|
val spoiler_text = this.spoiler_text
|
|
|
|
val bNSFW = this.bNSFW
|
|
|
|
val in_reply_to_id = this.in_reply_to_id
|
|
|
|
val attachment_list = this.attachment_list
|
|
|
|
val enquete_items = this.enquete_items
|
|
|
|
val poll_type = this.poll_type
|
|
|
|
val poll_expire_seconds = this.poll_expire_seconds
|
|
|
|
val poll_hide_totals = this.poll_hide_totals
|
|
|
|
val poll_multiple_choice = this.poll_multiple_choice
|
|
|
|
|
|
|
|
val visibility = this.visibility
|
|
|
|
val scheduledAt = this.scheduledAt
|
|
|
|
|
|
|
|
val hasAttachment = attachment_list?.isNotEmpty() ?: false
|
|
|
|
|
|
|
|
if (!hasAttachment && content.isEmpty()) {
|
|
|
|
activity.showToast(true, R.string.post_error_contents_empty)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// nullはCWチェックなしを示す
|
|
|
|
// nullじゃなくてカラならエラー
|
|
|
|
if (spoiler_text != null && spoiler_text.isEmpty()) {
|
|
|
|
activity.showToast(true, R.string.post_error_contents_warning_empty)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enquete_items?.isNotEmpty() == true) {
|
|
|
|
|
|
|
|
val choice_max_chars = when {
|
|
|
|
account.isMisskey -> 15
|
|
|
|
poll_type == TootPollsType.FriendsNico -> 15
|
|
|
|
else -> 25 // TootPollsType.Mastodon
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n in 0 until enquete_items.size) {
|
|
|
|
val item = enquete_items[n]
|
|
|
|
|
|
|
|
if (item.isEmpty()) {
|
|
|
|
if (n < 2) {
|
|
|
|
activity.showToast(true, R.string.enquete_item_is_empty, n + 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val code_count = item.codePointCount(0, item.length)
|
|
|
|
if (code_count > choice_max_chars) {
|
|
|
|
val over = code_count - choice_max_chars
|
|
|
|
activity.showToast(true, R.string.enquete_item_too_long, n + 1, over)
|
|
|
|
return
|
|
|
|
} else if (n > 0) {
|
|
|
|
for (i in 0 until n) {
|
|
|
|
if (item == enquete_items[i]) {
|
|
|
|
activity.showToast(true, R.string.enquete_item_duplicate, n + 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bConfirmAccount) {
|
|
|
|
DlgConfirm.open(
|
2018-01-21 17:47:13 +01:00
|
|
|
activity,
|
2020-02-01 14:26:57 +01:00
|
|
|
activity.getString(R.string.confirm_post_from, AcctColor.getNickname(account)),
|
2018-01-21 17:47:13 +01:00
|
|
|
object : DlgConfirm.Callback {
|
2021-01-03 06:11:04 +01:00
|
|
|
override var isConfirmEnabled: Boolean
|
2018-01-21 17:47:13 +01:00
|
|
|
get() = account.confirm_post
|
|
|
|
set(bv) {
|
|
|
|
account.confirm_post = bv
|
|
|
|
account.saveSetting()
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2018-01-21 17:47:13 +01:00
|
|
|
override fun onOK() {
|
2019-09-30 23:27:21 +02:00
|
|
|
post(
|
|
|
|
account, callback,
|
|
|
|
bConfirmTag = bConfirmTag,
|
|
|
|
bConfirmAccount = true,
|
|
|
|
bConfirmRedraft = bConfirmRedraft,
|
|
|
|
bConfirmTagCharacter = bConfirmTagCharacter
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-01-21 17:47:13 +01:00
|
|
|
})
|
2021-01-03 06:11:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bConfirmTagCharacter && Pref.bpWarnHashtagAsciiAndNonAscii(App1.pref)) {
|
|
|
|
val tags = TootTag.findHashtags(content, account.isMisskey)
|
|
|
|
val badTags = tags
|
|
|
|
?.filter {
|
|
|
|
val hasAscii = reAscii.matcher(it).find()
|
|
|
|
val hasNotAscii = reNotAscii.matcher(it).find()
|
|
|
|
hasAscii && hasNotAscii
|
|
|
|
}
|
|
|
|
?.map { "#$it" }
|
|
|
|
if (badTags?.isNotEmpty() == true) {
|
|
|
|
|
|
|
|
AlertDialog.Builder(activity)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setMessage(
|
2019-09-30 23:27:21 +02:00
|
|
|
activity.getString(
|
|
|
|
R.string.hashtag_contains_ascii_and_not_ascii,
|
|
|
|
badTags.joinToString(", ")
|
|
|
|
)
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
|
|
|
post(
|
2019-09-30 23:27:21 +02:00
|
|
|
account, callback,
|
|
|
|
bConfirmTag = bConfirmTag,
|
|
|
|
bConfirmAccount = bConfirmAccount,
|
|
|
|
bConfirmRedraft = bConfirmRedraft,
|
|
|
|
bConfirmTagCharacter = true
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
}
|
|
|
|
.show()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bConfirmTag) {
|
|
|
|
val isMisskey = account.isMisskey
|
|
|
|
if (!visibility.isTagAllowed(isMisskey)) {
|
|
|
|
val tags = TootTag.findHashtags(content, isMisskey)
|
|
|
|
if (tags != null) {
|
|
|
|
|
|
|
|
log.d("findHashtags ${tags.joinToString(",")}")
|
|
|
|
|
|
|
|
AlertDialog.Builder(activity)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setMessage(R.string.hashtag_and_visibility_not_match)
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
|
|
|
post(
|
2019-09-30 23:27:21 +02:00
|
|
|
account, callback,
|
2019-07-20 08:47:03 +02:00
|
|
|
bConfirmTag = true,
|
|
|
|
bConfirmAccount = bConfirmAccount,
|
|
|
|
bConfirmRedraft = bConfirmRedraft,
|
2019-09-30 23:27:21 +02:00
|
|
|
bConfirmTagCharacter = bConfirmTagCharacter
|
2019-07-20 08:47:03 +02:00
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
}
|
|
|
|
.show()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bConfirmRedraft && redraft_status_id != null) {
|
|
|
|
AlertDialog.Builder(activity)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setMessage(R.string.delete_base_status_before_toot)
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
|
|
|
post(
|
2019-09-30 23:27:21 +02:00
|
|
|
account, callback,
|
|
|
|
bConfirmTag = bConfirmTag,
|
|
|
|
bConfirmAccount = bConfirmAccount,
|
|
|
|
bConfirmRedraft = true,
|
|
|
|
bConfirmTagCharacter = bConfirmTagCharacter
|
2018-06-23 04:43:18 +02:00
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
}
|
|
|
|
.show()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!bConfirmRedraft && scheduledId != null) {
|
|
|
|
AlertDialog.Builder(activity)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setMessage(R.string.delete_scheduled_status_before_update)
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
|
|
|
post(
|
2019-09-30 23:27:21 +02:00
|
|
|
account, callback,
|
|
|
|
bConfirmTag = bConfirmTag,
|
|
|
|
bConfirmAccount = bConfirmAccount,
|
|
|
|
bConfirmRedraft = true,
|
|
|
|
bConfirmTagCharacter = bConfirmTagCharacter
|
2019-01-06 15:55:25 +01:00
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
}
|
|
|
|
.show()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 確認を終えたらボタン連打判定
|
|
|
|
|
|
|
|
if (last_post_task?.get()?.isActive == true) {
|
|
|
|
activity.showToast(false, R.string.post_button_tapped_repeatly)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val now = SystemClock.elapsedRealtime()
|
|
|
|
val delta = now - last_post_tapped
|
|
|
|
last_post_tapped = now
|
|
|
|
if (delta < 1000L) {
|
|
|
|
activity.showToast(false, R.string.post_button_tapped_repeatly)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 全ての確認を終えたらバックグラウンドでの処理を開始する
|
|
|
|
last_post_task = WeakReference(
|
2020-12-11 00:25:55 +01:00
|
|
|
TootTaskRunner(activity,
|
|
|
|
progressSetupCallback = { progressDialog ->
|
|
|
|
progressDialog.setCanceledOnTouchOutside(false)
|
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
).run(account, object : TootTask {
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
var status: TootStatus? = null
|
|
|
|
|
|
|
|
var credential_tmp: TootAccount? = null
|
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
var scheduledStatusSucceeded = false
|
2020-12-08 21:24:42 +01:00
|
|
|
|
|
|
|
suspend fun getCredential(
|
2021-01-03 06:11:04 +01:00
|
|
|
client: TootApiClient,
|
|
|
|
parser: TootParser
|
|
|
|
): TootApiResult? {
|
2020-09-29 19:44:56 +02:00
|
|
|
val result = client.request("/api/v1/accounts/verify_credentials")
|
|
|
|
credential_tmp = parser.account(result?.jsonObject)
|
|
|
|
return result
|
2019-09-30 23:27:21 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
override suspend fun background(client: TootApiClient): TootApiResult? {
|
2020-09-29 19:44:56 +02:00
|
|
|
val parser = TootParser(activity, account)
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
var result: TootApiResult?
|
|
|
|
|
2021-02-14 18:09:23 +01:00
|
|
|
|
|
|
|
val (instance, ri) = TootInstance.get(client)
|
|
|
|
instance ?: return ri
|
|
|
|
|
|
|
|
var visibility_checked: TootVisibility? = visibility
|
|
|
|
|
|
|
|
if (visibility == TootVisibility.WebSetting) {
|
|
|
|
visibility_checked =
|
|
|
|
if (account.isMisskey || instance.versionGE(TootInstance.VERSION_1_6)) {
|
|
|
|
null
|
|
|
|
} else {
|
|
|
|
val r2 = getCredential(client, parser)
|
|
|
|
val credential_tmp = this.credential_tmp ?: return r2
|
|
|
|
val privacy = credential_tmp.source?.privacy
|
|
|
|
?: return TootApiResult(activity.getString(R.string.cant_get_web_setting_visibility))
|
|
|
|
TootVisibility.parseMastodon(privacy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pair in arrayOf(
|
|
|
|
Pair(TootVisibility.Mutual, InstanceCapability.VisibilityMutual),
|
|
|
|
Pair(TootVisibility.Limited, InstanceCapability.VisibilityLimited),
|
|
|
|
)) {
|
|
|
|
val (checkVisibility, checkCapability) = pair
|
|
|
|
if (visibility == checkVisibility &&
|
|
|
|
!instance.hasCapability(checkCapability)
|
|
|
|
) {
|
|
|
|
val strVisibility = Styler.getVisibilityString(activity, account.isMisskey, checkVisibility)
|
|
|
|
return TootApiResult(activity.getString(R.string.server_has_no_support_of_visibility, strVisibility))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
// 元の投稿を削除する
|
2021-01-03 06:11:04 +01:00
|
|
|
if (redraft_status_id != null) {
|
|
|
|
result = if (account.isMisskey) {
|
2020-09-29 19:44:56 +02:00
|
|
|
client.request(
|
|
|
|
"/api/notes/delete",
|
2020-12-11 00:25:55 +01:00
|
|
|
account.putMisskeyApiToken(JsonObject()).apply {
|
|
|
|
put("noteId", redraft_status_id)
|
|
|
|
}.toPostRequestBuilder()
|
2020-09-29 19:44:56 +02:00
|
|
|
)
|
2018-11-06 02:29:33 +01:00
|
|
|
} else {
|
2020-09-29 19:44:56 +02:00
|
|
|
client.request(
|
|
|
|
"/api/v1/statuses/$redraft_status_id",
|
|
|
|
Request.Builder().delete()
|
|
|
|
)
|
2018-11-06 02:29:33 +01:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
log.d("delete redraft. result=$result")
|
|
|
|
Thread.sleep(2000L)
|
2020-12-11 00:25:55 +01:00
|
|
|
|
2021-01-03 06:11:04 +01:00
|
|
|
} else if (scheduledId != null) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val r1 = client.request(
|
|
|
|
"/api/v1/scheduled_statuses/$scheduledId",
|
|
|
|
Request.Builder().delete()
|
2018-06-23 04:43:18 +02:00
|
|
|
)
|
2020-09-29 19:44:56 +02:00
|
|
|
log.d("delete old scheduled status. result=$r1")
|
|
|
|
Thread.sleep(2000L)
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2021-02-14 18:09:23 +01:00
|
|
|
if (instance.instanceType == InstanceType.Pixelfed) {
|
2021-01-03 06:11:04 +01:00
|
|
|
if (in_reply_to_id != null && attachment_list?.isNotEmpty() == true) {
|
2020-09-29 19:44:56 +02:00
|
|
|
return TootApiResult(activity.getString(R.string.pixelfed_does_not_allow_reply_with_media))
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
if (in_reply_to_id == null && attachment_list?.isNotEmpty() != true) {
|
2020-09-29 19:44:56 +02:00
|
|
|
return TootApiResult(activity.getString(R.string.pixelfed_does_not_allow_post_without_media))
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
val json = JsonObject()
|
|
|
|
try {
|
2021-01-03 06:11:04 +01:00
|
|
|
if (account.isMisskey) {
|
2020-09-29 19:44:56 +02:00
|
|
|
account.putMisskeyApiToken(json)
|
|
|
|
json["text"] = EmojiDecoder.decodeShortCode(
|
|
|
|
content,
|
|
|
|
emojiMapCustom = emojiMapCustom
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (visibility_checked != null) {
|
|
|
|
|
2021-02-14 18:09:23 +01:00
|
|
|
if (visibility_checked == TootVisibility.DirectSpecified ||
|
|
|
|
visibility_checked == TootVisibility.DirectPrivate
|
|
|
|
) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val userIds = JsonArray()
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
val m = TootAccount.reMisskeyMentionPost.matcher(content)
|
2021-01-03 06:11:04 +01:00
|
|
|
while (m.find()) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val username = m.groupEx(1)
|
|
|
|
val host = m.groupEx(2) // may null
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
result = client.request(
|
|
|
|
"/api/users/show",
|
|
|
|
account.putMisskeyApiToken().apply {
|
2021-01-03 06:11:04 +01:00
|
|
|
if (username?.isNotEmpty() == true)
|
2020-09-29 19:44:56 +02:00
|
|
|
put("username", username)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (host?.isNotEmpty() == true)
|
2020-09-29 19:44:56 +02:00
|
|
|
put("host", host)
|
|
|
|
}.toPostRequestBuilder()
|
|
|
|
)
|
|
|
|
val id = result?.jsonObject?.string("id")
|
2021-01-03 06:11:04 +01:00
|
|
|
if (id?.isNotEmpty() == true) {
|
2020-09-29 19:44:56 +02:00
|
|
|
userIds.add(id)
|
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
json["visibility"] = when {
|
|
|
|
userIds.isNotEmpty() -> {
|
|
|
|
json["visibleUserIds"] = userIds
|
|
|
|
"specified"
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
account.misskeyVersion >= 11 -> "specified"
|
|
|
|
else -> "private"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val localVis = visibility_checked.strMisskey.replace(
|
|
|
|
"^local-".toRegex(),
|
|
|
|
""
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (localVis != visibility_checked.strMisskey) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["localOnly"] = true
|
|
|
|
json["visibility"] = localVis
|
|
|
|
} else {
|
|
|
|
json["visibility"] = visibility_checked.strMisskey
|
2018-11-06 02:29:33 +01:00
|
|
|
}
|
2020-01-07 09:03:32 +01:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (spoiler_text?.isNotEmpty() == true) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["cw"] = EmojiDecoder.decodeShortCode(
|
|
|
|
spoiler_text,
|
|
|
|
emojiMapCustom = emojiMapCustom
|
2018-11-30 06:22:17 +01:00
|
|
|
)
|
2020-09-29 19:44:56 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (in_reply_to_id != null) {
|
|
|
|
if (useQuoteToot) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["renoteId"] = in_reply_to_id.toString()
|
2018-11-30 06:22:17 +01:00
|
|
|
} else {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["replyId"] = in_reply_to_id.toString()
|
2018-11-16 00:43:10 +01:00
|
|
|
}
|
2018-08-20 19:37:42 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
json["viaMobile"] = true
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (attachment_list != null) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val array = JsonArray()
|
2021-01-03 06:11:04 +01:00
|
|
|
for (pa in attachment_list) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val a = pa.attachment ?: continue
|
|
|
|
// Misskeyは画像の再利用に問題がないので redraftとバージョンのチェックは行わない
|
|
|
|
array.add(a.id.toString())
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
// Misskeyの場合、NSFWするにはアップロード済みの画像を drive/files/update で更新する
|
2021-01-03 06:11:04 +01:00
|
|
|
if (bNSFW) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val r = client.request(
|
|
|
|
"/api/drive/files/update",
|
|
|
|
account.putMisskeyApiToken().apply {
|
|
|
|
put("fileId", a.id.toString())
|
|
|
|
put("isSensitive", true)
|
|
|
|
}
|
|
|
|
.toPostRequestBuilder()
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (r == null || r.error != null) return r
|
2020-09-29 19:44:56 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
if (array.isNotEmpty()) json["mediaIds"] = array
|
2020-09-29 19:44:56 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (enquete_items?.isNotEmpty() == true) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val choices = JsonArray().apply {
|
2021-01-03 06:11:04 +01:00
|
|
|
for (item in enquete_items) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val text = EmojiDecoder.decodeShortCode(
|
|
|
|
item,
|
|
|
|
emojiMapCustom = emojiMapCustom
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (text.isEmpty()) continue
|
2020-09-29 19:44:56 +02:00
|
|
|
add(text)
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
if (choices.isNotEmpty()) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["poll"] = jsonObject {
|
|
|
|
put("choices", choices)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (scheduledAt != 0L) {
|
2020-09-29 19:44:56 +02:00
|
|
|
return TootApiResult("misskey has no scheduled status API")
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
} else {
|
|
|
|
json["status"] = EmojiDecoder.decodeShortCode(
|
|
|
|
content,
|
2020-01-07 09:03:32 +01:00
|
|
|
emojiMapCustom = emojiMapCustom
|
2018-06-23 04:43:18 +02:00
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (visibility_checked != null) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["visibility"] = visibility_checked.strMastodon
|
2018-11-12 18:19:57 +01:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
json["sensitive"] = bNSFW
|
|
|
|
json["spoiler_text"] = EmojiDecoder.decodeShortCode(
|
|
|
|
spoiler_text ?: "",
|
|
|
|
emojiMapCustom = emojiMapCustom
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (in_reply_to_id != null) {
|
|
|
|
if (useQuoteToot) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["quote_id"] = in_reply_to_id.toString()
|
|
|
|
} else {
|
|
|
|
json["in_reply_to_id"] = in_reply_to_id.toString()
|
2018-08-21 02:26:49 +02:00
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (attachment_list != null) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["media_ids"] = jsonArray {
|
2021-01-03 06:11:04 +01:00
|
|
|
for (pa in attachment_list) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val a = pa.attachment ?: continue
|
2021-01-03 06:11:04 +01:00
|
|
|
if (a.redraft && !instance.versionGE(TootInstance.VERSION_2_4_1)) continue
|
2020-09-29 19:44:56 +02:00
|
|
|
add(a.id.toString())
|
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (enquete_items?.isNotEmpty() == true) {
|
|
|
|
if (poll_type == TootPollsType.Mastodon) {
|
2020-09-29 19:44:56 +02:00
|
|
|
json["poll"] = jsonObject {
|
|
|
|
put("multiple", poll_multiple_choice)
|
|
|
|
put("hide_totals", poll_hide_totals)
|
|
|
|
put("expires_in", poll_expire_seconds)
|
|
|
|
put("options",
|
|
|
|
enquete_items.map {
|
|
|
|
EmojiDecoder.decodeShortCode(
|
|
|
|
it,
|
|
|
|
emojiMapCustom = emojiMapCustom
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.toJsonArray()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
json["isEnquete"] = true
|
|
|
|
json["enquete_items"] = enquete_items.map {
|
|
|
|
EmojiDecoder.decodeShortCode(
|
|
|
|
it,
|
|
|
|
emojiMapCustom = emojiMapCustom
|
|
|
|
)
|
|
|
|
}.toJsonArray()
|
2020-01-07 09:03:32 +01:00
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (scheduledAt != 0L) {
|
|
|
|
if (!instance.versionGE(TootInstance.VERSION_2_7_0_rc1)) {
|
2020-09-29 19:44:56 +02:00
|
|
|
return TootApiResult(activity.getString(R.string.scheduled_status_requires_mastodon_2_7_0))
|
2020-01-07 09:03:32 +01:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
// UTCの日時を渡す
|
|
|
|
val c = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"))
|
|
|
|
c.timeInMillis = scheduledAt
|
|
|
|
val sv = String.format(
|
|
|
|
"%d-%02d-%02d %02d:%02d:%02d",
|
|
|
|
c.get(Calendar.YEAR),
|
|
|
|
c.get(Calendar.MONTH) + 1,
|
|
|
|
c.get(Calendar.DAY_OF_MONTH),
|
|
|
|
c.get(Calendar.HOUR_OF_DAY),
|
|
|
|
c.get(Calendar.MINUTE),
|
|
|
|
c.get(Calendar.SECOND)
|
|
|
|
)
|
|
|
|
json["scheduled_at"] = sv
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
} catch (ex: JsonException) {
|
2020-09-29 19:44:56 +02:00
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "status encoding failed.")
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
val body_string = json.toString()
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
val request_builder = body_string.toRequestBody(MEDIA_TYPE_JSON).toPost()
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (!Pref.bpDontDuplicationCheck(pref)) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val digest = (body_string + account.acct.ascii).digestSHA256Hex()
|
|
|
|
request_builder.header("Idempotency-Key", digest)
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
result = if (account.isMisskey) {
|
2020-09-29 19:44:56 +02:00
|
|
|
// log.d("misskey json %s", body_string)
|
|
|
|
client.request("/api/notes/create", request_builder)
|
|
|
|
} else {
|
|
|
|
client.request("/api/v1/statuses", request_builder)
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
val jsonObject = result?.jsonObject
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
if (scheduledAt != 0L && jsonObject != null) {
|
2020-09-29 19:44:56 +02:00
|
|
|
// {"id":"3","scheduled_at":"2019-01-06T07:08:00.000Z","media_attachments":[]}
|
|
|
|
scheduledStatusSucceeded = true
|
|
|
|
return result
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
val status = parser.status(
|
2021-01-03 06:11:04 +01:00
|
|
|
if (account.isMisskey) {
|
2020-09-29 19:44:56 +02:00
|
|
|
result?.jsonObject?.jsonObject("createdNote") ?: result?.jsonObject
|
|
|
|
} else {
|
|
|
|
result?.jsonObject
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
)
|
|
|
|
this.status = status
|
2021-01-03 06:11:04 +01:00
|
|
|
if (status != null) {
|
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
// タグを覚えておく
|
|
|
|
val s = status.decoded_content
|
|
|
|
val span_list = s.getSpans(0, s.length, MyClickableSpan::class.java)
|
2021-01-03 06:11:04 +01:00
|
|
|
if (span_list != null) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val tag_list = ArrayList<String?>(span_list.size)
|
2021-01-03 06:11:04 +01:00
|
|
|
for (span in span_list) {
|
2020-09-29 19:44:56 +02:00
|
|
|
val start = s.getSpanStart(span)
|
|
|
|
val end = s.getSpanEnd(span)
|
|
|
|
val text = s.subSequence(start, end).toString()
|
2021-01-03 06:11:04 +01:00
|
|
|
if (text.startsWith("#")) {
|
2020-09-29 19:44:56 +02:00
|
|
|
tag_list.add(text.substring(1))
|
2019-03-05 06:55:47 +01:00
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
val count = tag_list.size
|
2021-01-03 06:11:04 +01:00
|
|
|
if (count > 0) {
|
2020-09-29 19:44:56 +02:00
|
|
|
TagSet.saveList(System.currentTimeMillis(), tag_list, 0, count)
|
2019-01-06 09:20:37 +01:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2019-01-06 09:20:37 +01:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2018-06-23 04:43:18 +02:00
|
|
|
}
|
2019-01-06 09:20:37 +01:00
|
|
|
return result
|
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
override suspend fun handleResult(result: TootApiResult?) {
|
2020-09-29 19:44:56 +02:00
|
|
|
result ?: return
|
|
|
|
val status = this.status
|
|
|
|
when {
|
|
|
|
status != null -> {
|
|
|
|
// 連投してIdempotency が同じだった場合もエラーにはならず、ここを通る
|
|
|
|
callback.onPostComplete(account, status)
|
|
|
|
return
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
scheduledStatusSucceeded -> {
|
|
|
|
callback.onScheduledPostComplete(account)
|
|
|
|
return
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
else -> activity.showToast(true, result.error)
|
2019-01-06 09:20:37 +01:00
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
})
|
2021-01-03 06:11:04 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 入力補完機能
|
|
|
|
|
|
|
|
private val picker_caption_emoji: String by lazy {
|
|
|
|
activity.getString(R.string.open_picker_emoji)
|
|
|
|
}
|
|
|
|
// private val picker_caption_tag : String by lazy {
|
|
|
|
// activity.getString(R.string.open_picker_tag)
|
|
|
|
// }
|
|
|
|
// private val picker_caption_mention : String by lazy {
|
|
|
|
// activity.getString(R.string.open_picker_mention)
|
|
|
|
// }
|
|
|
|
|
|
|
|
private var callback2: Callback2? = null
|
|
|
|
private var et: MyEditText? = null
|
|
|
|
private var popup: PopupAutoCompleteAcct? = null
|
|
|
|
private var formRoot: View? = null
|
|
|
|
private var bMainScreen: Boolean = false
|
|
|
|
|
|
|
|
private var accessInfo: SavedAccount? = null
|
|
|
|
|
|
|
|
private val onEmojiListLoad: (list: ArrayList<CustomEmoji>) -> Unit =
|
|
|
|
{
|
|
|
|
val popup = this@PostHelper.popup
|
|
|
|
if (popup?.isShowing == true) proc_text_changed.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
private val proc_text_changed = object : Runnable {
|
|
|
|
|
|
|
|
override fun run() {
|
|
|
|
val et = this@PostHelper.et
|
|
|
|
if (et == null // EditTextを特定できない
|
|
|
|
|| et.selectionStart != et.selectionEnd // 範囲選択中
|
|
|
|
|| callback2?.canOpenPopup() != true // 何らかの理由でポップアップが許可されていない
|
|
|
|
) {
|
|
|
|
closeAcctPopup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
checkMention(et, et.text.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun checkMention(et: MyEditText, src: String) {
|
|
|
|
|
|
|
|
fun matchUserNameOrAsciiDomain(cp: Int): Boolean {
|
|
|
|
if (cp >= 0x7f) return false
|
|
|
|
val c = cp.toChar()
|
|
|
|
|
|
|
|
return '0' <= c && c <= '9' ||
|
|
|
|
'A' <= c && c <= 'Z' ||
|
|
|
|
'a' <= c && c <= 'z' ||
|
|
|
|
c == '_' || c == '-' || c == '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Letter | Mark | Decimal_Number | Connector_Punctuation
|
|
|
|
fun matchIdnWord(cp: Int) = when (Character.getType(cp).toByte()) {
|
|
|
|
// Letter
|
|
|
|
// LCはエイリアスなので文字から得られることはないはず
|
2020-02-02 07:25:08 +01:00
|
|
|
Character.UPPERCASE_LETTER,
|
|
|
|
Character.LOWERCASE_LETTER,
|
|
|
|
Character.TITLECASE_LETTER,
|
|
|
|
Character.MODIFIER_LETTER,
|
|
|
|
Character.OTHER_LETTER -> true
|
2021-01-03 06:11:04 +01:00
|
|
|
// Mark
|
2020-02-02 07:25:08 +01:00
|
|
|
Character.NON_SPACING_MARK,
|
|
|
|
Character.COMBINING_SPACING_MARK,
|
|
|
|
Character.ENCLOSING_MARK -> true
|
2021-01-03 06:11:04 +01:00
|
|
|
// Decimal_Number
|
2020-02-02 07:25:08 +01:00
|
|
|
Character.DECIMAL_DIGIT_NUMBER -> true
|
2021-01-03 06:11:04 +01:00
|
|
|
// Connector_Punctuation
|
2020-02-02 07:25:08 +01:00
|
|
|
Character.CONNECTOR_PUNCTUATION -> true
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
|
|
|
|
var count_atMark = 0
|
|
|
|
val end = et.selectionEnd
|
|
|
|
var start: Int = -1
|
|
|
|
var i = end
|
|
|
|
while (i > 0) {
|
|
|
|
val cp = src.codePointBefore(i)
|
|
|
|
i -= Character.charCount(cp)
|
|
|
|
|
|
|
|
if (cp == '@'.toInt()) {
|
|
|
|
start = i
|
|
|
|
if (++count_atMark >= 2) break else continue
|
|
|
|
} else if (count_atMark == 1) {
|
|
|
|
// @username@host の username部分はUnicodeを含まない
|
|
|
|
if (matchUserNameOrAsciiDomain(cp)) continue else break
|
|
|
|
} else {
|
|
|
|
// @username@host のhost 部分か、 @username のusername部分
|
|
|
|
// ここはUnicodeを含むかもしれない
|
|
|
|
if (matchUserNameOrAsciiDomain(cp) || matchIdnWord(cp)) continue else break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start == -1) {
|
|
|
|
checkTag(et, src)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 最低でも2文字ないと補完しない
|
|
|
|
if (end - start < 2) {
|
|
|
|
closeAcctPopup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val limit = 100
|
|
|
|
val s = src.substring(start, end)
|
|
|
|
val acct_list = AcctSet.searchPrefix(s, limit)
|
|
|
|
log.d("search for %s, result=%d", s, acct_list.size)
|
|
|
|
if (acct_list.isEmpty()) {
|
|
|
|
closeAcctPopup()
|
|
|
|
} else {
|
|
|
|
openPopup()?.setList(et, start, end, acct_list, null, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun checkTag(et: MyEditText, src: String) {
|
|
|
|
|
|
|
|
val end = et.selectionEnd
|
|
|
|
|
|
|
|
val last_sharp = src.lastIndexOf('#', end - 1)
|
|
|
|
|
|
|
|
if (last_sharp == -1 || end - last_sharp < 2) {
|
|
|
|
checkEmoji(et, src)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val part = src.substring(last_sharp + 1, end)
|
|
|
|
if (!TootTag.isValid(part, accessInfo?.isMisskey == true)) {
|
|
|
|
checkEmoji(et, src)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val limit = 100
|
|
|
|
val s = src.substring(last_sharp + 1, end)
|
|
|
|
val tag_list = TagSet.searchPrefix(s, limit)
|
|
|
|
log.d("search for %s, result=%d", s, tag_list.size)
|
|
|
|
if (tag_list.isEmpty()) {
|
|
|
|
closeAcctPopup()
|
|
|
|
} else {
|
|
|
|
openPopup()?.setList(et, last_sharp, end, tag_list, null, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun checkEmoji(et: MyEditText, src: String) {
|
|
|
|
|
|
|
|
val end = et.selectionEnd
|
|
|
|
val last_colon = src.lastIndexOf(':', end - 1)
|
|
|
|
if (last_colon == -1 || end - last_colon < 1) {
|
|
|
|
closeAcctPopup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EmojiDecoder.canStartShortCode(src, last_colon)) {
|
|
|
|
// : の手前は始端か改行か空白でなければならない
|
|
|
|
log.d("checkEmoji: invalid character before shortcode.")
|
|
|
|
closeAcctPopup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val part = src.substring(last_colon + 1, end)
|
|
|
|
|
|
|
|
if (part.isEmpty()) {
|
|
|
|
// :を入力した直後は候補は0で、「閉じる」と「絵文字を選ぶ」だけが表示されたポップアップを出す
|
|
|
|
openPopup()?.setList(
|
2018-01-04 19:52:25 +01:00
|
|
|
et, last_colon, end, null, picker_caption_emoji, open_picker_emoji
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reCharsNotEmoji.matcher(part).find()) {
|
|
|
|
// 範囲内に絵文字に使えない文字がある
|
|
|
|
closeAcctPopup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val code_list = ArrayList<CharSequence>()
|
|
|
|
val limit = 100
|
|
|
|
|
|
|
|
// カスタム絵文字の候補を部分一致検索
|
|
|
|
code_list.addAll(customEmojiCodeList(accessInfo, limit, part))
|
|
|
|
|
|
|
|
// 通常の絵文字を部分一致で検索
|
|
|
|
val remain = limit - code_list.size
|
|
|
|
if (remain > 0) {
|
|
|
|
val s =
|
|
|
|
src.substring(last_colon + 1, end).toLowerCase(Locale.JAPAN).replace('-', '_')
|
|
|
|
val matches = EmojiDecoder.searchShortCode(activity, s, remain)
|
|
|
|
log.d("checkEmoji: search for %s, result=%d", s, matches.size)
|
|
|
|
code_list.addAll(matches)
|
|
|
|
}
|
|
|
|
|
|
|
|
openPopup()?.setList(
|
2018-11-30 06:22:17 +01:00
|
|
|
et,
|
|
|
|
last_colon,
|
|
|
|
end,
|
|
|
|
code_list,
|
|
|
|
picker_caption_emoji,
|
|
|
|
open_picker_emoji
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// カスタム絵文字の候補を作る
|
|
|
|
private fun customEmojiCodeList(
|
|
|
|
accessInfo: SavedAccount?,
|
|
|
|
@Suppress("SameParameterValue") limit: Int,
|
|
|
|
needle: String
|
2020-02-05 09:18:28 +01:00
|
|
|
) = ArrayList<CharSequence>().also { dst ->
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
accessInfo ?: return@also
|
|
|
|
|
|
|
|
val custom_list =
|
|
|
|
App1.custom_emoji_lister.getListWithAliases(accessInfo, onEmojiListLoad)
|
|
|
|
?: return@also
|
|
|
|
|
|
|
|
|
|
|
|
for (item in custom_list) {
|
|
|
|
if (dst.size >= limit) break
|
|
|
|
if (!item.shortcode.contains(needle)) continue
|
|
|
|
|
|
|
|
val sb = SpannableStringBuilder()
|
|
|
|
sb.append(' ')
|
|
|
|
sb.setSpan(
|
2020-05-16 16:59:18 +02:00
|
|
|
NetworkEmojiSpan(item.url),
|
|
|
|
0,
|
|
|
|
sb.length,
|
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
sb.append(' ')
|
|
|
|
if (item.alias != null) {
|
|
|
|
val start = sb.length
|
|
|
|
sb.append(":")
|
|
|
|
sb.append(item.alias)
|
|
|
|
sb.append(": → ")
|
|
|
|
sb.setSpan(
|
2021-01-04 02:11:45 +01:00
|
|
|
ForegroundColorSpan(activity.attrColor(R.attr.colorTimeSmall)),
|
2020-05-16 16:59:18 +02:00
|
|
|
start,
|
|
|
|
sb.length,
|
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
|
|
)
|
2021-01-03 06:11:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sb.append(':')
|
|
|
|
sb.append(item.shortcode)
|
|
|
|
sb.append(':')
|
|
|
|
|
|
|
|
dst.add(sb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun openPopup(): PopupAutoCompleteAcct? {
|
|
|
|
var popup = this@PostHelper.popup
|
|
|
|
if (popup?.isShowing == true) return popup
|
|
|
|
val et = this@PostHelper.et ?: return null
|
|
|
|
val formRoot = this@PostHelper.formRoot ?: return null
|
|
|
|
popup = PopupAutoCompleteAcct(activity, et, formRoot, bMainScreen)
|
|
|
|
this@PostHelper.popup = popup
|
|
|
|
return popup
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Callback2 {
|
|
|
|
|
|
|
|
fun onTextUpdate()
|
|
|
|
|
|
|
|
fun canOpenPopup(): Boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setInstance(accessInfo: SavedAccount?) {
|
|
|
|
this.accessInfo = accessInfo
|
|
|
|
|
|
|
|
if (accessInfo != null) {
|
|
|
|
App1.custom_emoji_lister.getList(accessInfo, onEmojiListLoad)
|
|
|
|
}
|
|
|
|
|
|
|
|
val popup = this.popup
|
|
|
|
if (popup?.isShowing == true) {
|
|
|
|
proc_text_changed.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun closeAcctPopup() {
|
|
|
|
popup?.dismiss()
|
|
|
|
popup = null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onScrollChanged() {
|
|
|
|
if (popup?.isShowing == true) {
|
|
|
|
popup?.updatePosition()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onDestroy() {
|
|
|
|
handler.removeCallbacks(proc_text_changed)
|
|
|
|
closeAcctPopup()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun attachEditText(
|
|
|
|
_formRoot: View,
|
|
|
|
et: MyEditText,
|
|
|
|
bMainScreen: Boolean,
|
|
|
|
_callback2: Callback2
|
2018-01-21 17:47:13 +01:00
|
|
|
) {
|
2021-01-03 06:11:04 +01:00
|
|
|
this.formRoot = _formRoot
|
|
|
|
this.et = et
|
|
|
|
this.callback2 = _callback2
|
|
|
|
this.bMainScreen = bMainScreen
|
|
|
|
|
|
|
|
et.addTextChangedListener(object : TextWatcher {
|
2018-01-21 17:47:13 +01:00
|
|
|
override fun beforeTextChanged(
|
2021-01-03 06:11:04 +01:00
|
|
|
s: CharSequence,
|
|
|
|
start: Int,
|
|
|
|
count: Int,
|
|
|
|
after: Int
|
2018-01-21 17:47:13 +01:00
|
|
|
) {
|
2021-01-03 06:11:04 +01:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
2018-01-04 19:52:25 +01:00
|
|
|
handler.removeCallbacks(proc_text_changed)
|
2021-01-03 06:11:04 +01:00
|
|
|
handler.postDelayed(proc_text_changed, if (popup?.isShowing == true) 100L else 500L)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
override fun afterTextChanged(s: Editable) {
|
2018-01-10 16:47:35 +01:00
|
|
|
callback2?.onTextUpdate()
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
})
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
et.setOnSelectionChangeListener(object : MyEditText.OnSelectionChangeListener {
|
|
|
|
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
|
|
|
|
if (selStart != selEnd) {
|
2018-01-04 19:52:25 +01:00
|
|
|
// 範囲選択されてるならポップアップは閉じる
|
|
|
|
log.d("onSelectionChanged: range selected")
|
|
|
|
closeAcctPopup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
// 全然動いてなさそう…
|
|
|
|
// et.setCustomSelectionActionModeCallback( action_mode_callback );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun SpannableStringBuilder.appendEmoji(
|
|
|
|
name: String,
|
|
|
|
instance: String?,
|
|
|
|
bInstanceHasCustomEmoji: Boolean
|
|
|
|
): SpannableStringBuilder {
|
|
|
|
|
2021-02-20 12:22:22 +01:00
|
|
|
val item = EmojiMap.shortNameToEmojiInfo[name]
|
2021-01-03 06:11:04 +01:00
|
|
|
val separator = EmojiDecoder.customEmojiSeparator(pref)
|
|
|
|
if (item == null || instance != null) {
|
|
|
|
// カスタム絵文字は常にshortcode表現
|
|
|
|
if (!EmojiDecoder.canStartShortCode(this, this.length)) append(separator)
|
|
|
|
this.append(SpannableString(":$name:"))
|
|
|
|
// セパレータにZWSPを使う設定なら、補完した次の位置にもZWSPを追加する。連続して入力補完できるようになる。
|
|
|
|
if (separator != ' ') append(separator)
|
|
|
|
} else if (!bInstanceHasCustomEmoji) {
|
|
|
|
// 古いタンスだとshortcodeを使う。見た目は絵文字に変える。
|
|
|
|
if (!EmojiDecoder.canStartShortCode(this, this.length)) append(separator)
|
|
|
|
this.append(DecodeOptions(activity).decodeEmoji(":$name:"))
|
|
|
|
// セパレータにZWSPを使う設定なら、補完した次の位置にもZWSPを追加する。連続して入力補完できるようになる。
|
|
|
|
if (separator != ' ') append(separator)
|
|
|
|
} else {
|
|
|
|
// 十分に新しいタンスなら絵文字のunicodeを使う。見た目は絵文字に変える。
|
|
|
|
this.append(DecodeOptions(activity).decodeEmoji(item.unified))
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private val open_picker_emoji: Runnable = Runnable {
|
|
|
|
EmojiPicker(activity, accessInfo,
|
|
|
|
closeOnSelected = Pref.bpEmojiPickerCloseOnSelected(pref)
|
|
|
|
) { name, instance, bInstanceHasCustomEmoji, _, _ ->
|
|
|
|
val et = this.et ?: return@EmojiPicker
|
|
|
|
|
|
|
|
val src = et.text ?: ""
|
|
|
|
val src_length = src.length
|
|
|
|
val end = min(src_length, et.selectionEnd)
|
|
|
|
val start = src.lastIndexOf(':', end - 1)
|
|
|
|
if (start == -1 || end - start < 1) return@EmojiPicker
|
|
|
|
|
|
|
|
val sb = SpannableStringBuilder()
|
|
|
|
.append(src.subSequence(0, start))
|
|
|
|
.appendEmoji(name, instance, bInstanceHasCustomEmoji)
|
|
|
|
|
|
|
|
val newSelection = sb.length
|
|
|
|
if (end < src_length) sb.append(src.subSequence(end, src_length))
|
|
|
|
|
|
|
|
et.text = sb
|
|
|
|
et.setSelection(newSelection)
|
|
|
|
|
|
|
|
proc_text_changed.run()
|
|
|
|
|
|
|
|
// キーボードを再度表示する
|
|
|
|
App1.getAppState(
|
2020-09-29 19:44:56 +02:00
|
|
|
activity,
|
|
|
|
"PostHelper/EmojiPicker/cb"
|
|
|
|
).handler.post { et.showKeyboard() }
|
2021-01-03 06:11:04 +01:00
|
|
|
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun openEmojiPickerFromMore() {
|
|
|
|
EmojiPicker(activity, accessInfo,
|
|
|
|
closeOnSelected = Pref.bpEmojiPickerCloseOnSelected(pref)
|
|
|
|
) { name, instance, bInstanceHasCustomEmoji, _, _ ->
|
|
|
|
val et = this.et ?: return@EmojiPicker
|
|
|
|
|
|
|
|
val src = et.text ?: ""
|
|
|
|
val src_length = src.length
|
|
|
|
val start = min(src_length, et.selectionStart)
|
|
|
|
val end = min(src_length, et.selectionEnd)
|
|
|
|
|
|
|
|
val sb = SpannableStringBuilder()
|
|
|
|
.append(src.subSequence(0, start))
|
|
|
|
.appendEmoji(name, instance, bInstanceHasCustomEmoji)
|
|
|
|
|
|
|
|
val newSelection = sb.length
|
|
|
|
if (end < src_length) sb.append(src.subSequence(end, src_length))
|
|
|
|
|
|
|
|
et.text = sb
|
|
|
|
et.setSelection(newSelection)
|
|
|
|
|
|
|
|
proc_text_changed.run()
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun SpannableStringBuilder.appendHashTag(tagWithoutSharp: String): SpannableStringBuilder {
|
|
|
|
val separator = ' '
|
|
|
|
if (!EmojiDecoder.canStartHashtag(this, this.length)) append(separator)
|
|
|
|
this.append('#').append(tagWithoutSharp)
|
|
|
|
append(separator)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
fun openFeaturedTagList(list: List<TootTag>?) {
|
|
|
|
val ad = ActionsDialog()
|
|
|
|
list?.forEach { tag ->
|
|
|
|
ad.addAction("#${tag.name}") {
|
|
|
|
val et = this.et ?: return@addAction
|
|
|
|
|
|
|
|
val src = et.text ?: ""
|
|
|
|
val src_length = src.length
|
|
|
|
val start = min(src_length, et.selectionStart)
|
|
|
|
val end = min(src_length, et.selectionEnd)
|
|
|
|
|
|
|
|
val sb = SpannableStringBuilder()
|
|
|
|
.append(src.subSequence(0, start))
|
|
|
|
.appendHashTag(tag.name)
|
|
|
|
val newSelection = sb.length
|
|
|
|
if (end < src_length) sb.append(src.subSequence(end, src_length))
|
|
|
|
|
|
|
|
et.text = sb
|
|
|
|
et.setSelection(newSelection)
|
|
|
|
|
|
|
|
proc_text_changed.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ad.addAction(activity.getString(R.string.input_sharp_itself)) {
|
|
|
|
val et = this.et ?: return@addAction
|
|
|
|
|
|
|
|
val src = et.text ?: ""
|
|
|
|
val src_length = src.length
|
|
|
|
val start = min(src_length, et.selectionStart)
|
|
|
|
val end = min(src_length, et.selectionEnd)
|
|
|
|
|
|
|
|
val sb = SpannableStringBuilder()
|
|
|
|
sb.append(src.subSequence(0, start))
|
|
|
|
if (!EmojiDecoder.canStartHashtag(sb, sb.length)) sb.append(' ')
|
|
|
|
sb.append('#')
|
|
|
|
|
|
|
|
val newSelection = sb.length
|
|
|
|
if (end < src_length) sb.append(src.subSequence(end, src_length))
|
|
|
|
et.text = sb
|
|
|
|
et.setSelection(newSelection)
|
|
|
|
|
|
|
|
proc_text_changed.run()
|
|
|
|
|
|
|
|
}
|
|
|
|
ad.show(activity, activity.getString(R.string.featured_hashtags))
|
|
|
|
}
|
|
|
|
|
|
|
|
// final ActionMode.Callback action_mode_callback = new ActionMode.Callback() {
|
|
|
|
// @Override public boolean onCreateActionMode( ActionMode actionMode, Menu menu ){
|
|
|
|
// actionMode.getMenuInflater().inflate(R.menu.toot_long_tap, menu);
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// @Override public void onDestroyActionMode( ActionMode actionMode ){
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// @Override public boolean onPrepareActionMode( ActionMode actionMode, Menu menu ){
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public boolean onActionItemClicked( ActionMode actionMode, MenuItem item ){
|
|
|
|
// if (item.getItemId() == R.id.action_pick_emoji) {
|
|
|
|
// actionMode.finish();
|
|
|
|
// EmojiPicker.open( activity, instance, new EmojiPicker.Callback() {
|
|
|
|
// @Override public void onPickedEmoji( String name ){
|
|
|
|
// int end = et.getSelectionEnd();
|
|
|
|
// String src = et.getText().toString();
|
|
|
|
// CharSequence svInsert = ":" + name + ":";
|
|
|
|
// src = src.substring( 0, end ) + svInsert + " " + ( end >= src.length() ? "" : src.substring( end ) );
|
|
|
|
// et.setText( src );
|
|
|
|
// et.setSelection( end + svInsert.length() + 1 );
|
|
|
|
//
|
|
|
|
// proc_text_changed.run();
|
|
|
|
// }
|
|
|
|
// } );
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|