removed some AbsTask implementations

This commit is contained in:
Mariotaku Lee 2017-11-08 23:18:01 +08:00
parent 9fd3e0ee9a
commit 04a4205651
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
14 changed files with 333 additions and 309 deletions

View File

@ -0,0 +1,33 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.text
import junit.framework.Assert
import org.junit.Test
class TwitterValidatorTest {
private val validator = TwitterValidator()
@Test
fun getTweetLength() {
Assert.assertEquals(280, validator.getTweetLength("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi placerat sodales libero id imperdiet. Aliquam malesuada volutpat nisl, quis elementum est tristique eget. In rhoncus lacus non sollicitudin tincidunt. Curabitur a turpis fringilla, varius nisl at, aliquet dolor metus."))
Assert.assertEquals(280, validator.getTweetLength("一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十"))
}
}

View File

@ -37,6 +37,7 @@ import org.apache.commons.cli.*
import org.json.JSONArray
import org.json.JSONObject
import org.mariotaku.ktextension.subArray
import org.mariotaku.twidere.exception.AccountNotFoundException
import org.mariotaku.twidere.exception.NoAccountException
import org.mariotaku.twidere.extension.model.updateDetails
import org.mariotaku.twidere.model.AccountDetails
@ -308,7 +309,8 @@ class AccountsDumperPlugin(val context: Context) : DumperPlugin {
private fun AccountManager.docContext(forKey: String): DocumentContext {
val accountKey = UserKey.valueOf(forKey)
val details = AccountUtils.getAccountDetails(this, accountKey, true) ?: throw NoAccountException()
val details = AccountUtils.getAccountDetails(this, accountKey, true)
?: throw AccountNotFoundException()
val configuration = Configuration.builder()
.jsonProvider(JsonOrgJsonProvider())
.mappingProvider(AsIsMappingProvider())

View File

@ -64,6 +64,7 @@ import org.mariotaku.twidere.util.refresh.JobSchedulerAutoRefreshController
import org.mariotaku.twidere.util.refresh.LegacyAutoRefreshController
import org.mariotaku.twidere.util.schedule.StatusScheduleProvider
import org.mariotaku.twidere.util.sync.*
import org.mariotaku.twidere.util.text.TwitterValidator
import java.io.File
import javax.inject.Singleton
@ -196,7 +197,7 @@ class ApplicationModule(private val application: Application) {
@Provides
@Singleton
fun validator(): Validator {
return Validator()
return TwitterValidator()
}
@Provides

View File

@ -65,6 +65,7 @@ import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.ParcelableUserUtils
import org.mariotaku.twidere.task.*
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.text.TwitterValidator
import org.mariotaku.twidere.view.iface.IExtendedView.OnSizeChangedListener
class UserProfileEditorFragment : BaseFragment(), OnSizeChangedListener,
@ -88,7 +89,7 @@ class UserProfileEditorFragment : BaseFragment(), OnSizeChangedListener,
return
}
val lengthChecker = TwitterValidatorMETLengthChecker(Validator())
val lengthChecker = TwitterValidatorMETLengthChecker(TwitterValidator())
editDescription.setLengthChecker(lengthChecker)

View File

@ -89,11 +89,10 @@ import org.mariotaku.twidere.model.*
import org.mariotaku.twidere.model.ParcelableMessageConversation.ConversationType
import org.mariotaku.twidere.model.ParcelableMessageConversation.ExtrasType
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.promise.MessageConversationPromises
import org.mariotaku.twidere.promise.MessagePromises
import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations
import org.mariotaku.twidere.task.status.UpdateStatusTask
import org.mariotaku.twidere.task.twitter.message.AddParticipantsTask
import org.mariotaku.twidere.task.twitter.message.ClearMessagesTask
import org.mariotaku.twidere.task.twitter.message.SetConversationNotificationDisabledTask
import org.mariotaku.twidere.util.IntentUtils
import org.mariotaku.twidere.view.holder.SimpleUserViewHolder
@ -297,7 +296,7 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
private fun performDestroyConversation() {
val weakThis by weak(this)
showProgressDialog("leave_conversation_progress") and MessageConversationPromises.destroyConversation(context, accountKey, conversationId).successUi {
showProgressDialog("leave_conversation_progress") and MessagePromises.destroyConversation(context, accountKey, conversationId).successUi {
val f = weakThis ?: return@successUi
f.dismissProgressDialog("leave_conversation_progress")
f.activity?.setResult(RESULT_CLOSE)
@ -306,18 +305,15 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
}
private fun performClearMessages() {
ProgressDialogFragment.show(childFragmentManager, "clear_messages_progress")
val weakThis = WeakReference(this)
val task = ClearMessagesTask(context, accountKey, conversationId)
task.callback = callback@ { succeed ->
val f = weakThis.get() ?: return@callback
val weakThis by weak(this)
showProgressDialog("clear_messages_progress") and MessagePromises.clearMessages(context, accountKey, conversationId).successUi { succeed ->
val f = weakThis ?: return@successUi
f.dismissDialogThen("clear_messages_progress") {
if (succeed) {
activity?.finish()
}
}
}
TaskStarter.execute(task)
}
private fun performAddParticipant(user: ParcelableUser) {

View File

@ -81,10 +81,9 @@ import org.mariotaku.twidere.model.ParcelableMessageConversation.ConversationTyp
import org.mariotaku.twidere.model.event.GetMessagesTaskEvent
import org.mariotaku.twidere.model.event.SendMessageTaskEvent
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.promise.MessagePromises
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.service.LengthyOperationsService
import org.mariotaku.twidere.util.obtainMedia
import org.mariotaku.twidere.task.twitter.message.DestroyMessageTask
import org.mariotaku.twidere.task.twitter.message.GetMessagesTask
import org.mariotaku.twidere.task.twitter.message.MarkMessageReadTask
import org.mariotaku.twidere.util.*
@ -351,9 +350,9 @@ class MessagesConversationFragment : AbsContentListRecyclerViewFragment<Messages
ClipboardUtils.setText(context, message.text_unescaped)
}
R.id.delete -> {
val task = DestroyMessageTask(context, message.account_key,
// TODO show progress
MessagePromises.destroyMessage(context, message.account_key,
message.conversation_id, message.id)
TaskStarter.execute(task)
}
}
return true

View File

@ -1,91 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.promise
import android.accounts.AccountManager
import android.content.Context
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.task
import org.mariotaku.ktextension.weak
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.extension.model.isOfficial
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.ParcelableMessageConversation
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.task.twitter.message.ClearMessagesTask
import org.mariotaku.twidere.util.DataStoreUtils
object MessageConversationPromises {
fun destroyConversation(context: Context, accountKey: UserKey, conversationId: String): Promise<Boolean, Exception> {
val weakContext by weak(context)
return task {
val taskContext = weakContext ?: throw InterruptedException()
val account = AccountUtils.getAccountDetails(AccountManager.get(taskContext), accountKey, true) ?:
throw MicroBlogException("No account")
val conversation = DataStoreUtils.findMessageConversation(taskContext, accountKey, conversationId)
var deleteMessages = true
var deleteConversation = true
// Only perform real deletion when it's not temp conversation (stored locally)
if (conversation != null) when {
conversation.conversation_extras_type != ParcelableMessageConversation.ExtrasType.TWITTER_OFFICIAL -> {
deleteMessages = false
deleteConversation = ClearMessagesTask.clearMessages(taskContext, account, conversationId)
}
!conversation.is_temp -> if (!requestDestroyConversation(taskContext, account, conversationId)) {
return@task false
}
}
if (deleteMessages) {
val deleteMessageWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val deleteMessageWhereArgs = arrayOf(accountKey.toString(), conversationId)
taskContext.contentResolver.delete(Messages.CONTENT_URI, deleteMessageWhere, deleteMessageWhereArgs)
}
if (deleteConversation) {
val deleteConversationWhere = Expression.and(Expression.equalsArgs(Messages.Conversations.ACCOUNT_KEY),
Expression.equalsArgs(Messages.Conversations.CONVERSATION_ID)).sql
val deleteConversationWhereArgs = arrayOf(accountKey.toString(), conversationId)
taskContext.contentResolver.delete(Messages.Conversations.CONTENT_URI, deleteConversationWhere, deleteConversationWhereArgs)
}
return@task true
}
}
private fun requestDestroyConversation(context: Context, account: AccountDetails, conversationId: String): Boolean {
when (account.type) {
AccountType.TWITTER -> {
if (account.isOfficial(context)) {
val twitter = account.newMicroBlogInstance(context, MicroBlog::class.java)
return twitter.deleteDmConversation(conversationId).isSuccessful
}
}
}
return false
}
}

View File

@ -0,0 +1,189 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.promise
import android.accounts.AccountManager
import android.content.Context
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.task
import org.mariotaku.ktextension.weak
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.exception.AccountNotFoundException
import org.mariotaku.twidere.extension.model.isOfficial
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.extension.queryReference
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.ParcelableMessageConversation
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.util.DataStoreUtils
import org.mariotaku.twidere.util.content.ContentResolverUtils
import org.mariotaku.twidere.util.updateItems
object MessagePromises {
fun destroyConversation(context: Context, accountKey: UserKey, conversationId: String): Promise<Boolean, Exception> {
val weakContext by weak(context)
return task {
val taskContext = weakContext ?: throw InterruptedException()
val account = AccountUtils.getAccountDetails(AccountManager.get(taskContext), accountKey, true) ?:
throw MicroBlogException("No account")
val conversation = DataStoreUtils.findMessageConversation(taskContext, accountKey, conversationId)
var deleteMessages = true
var deleteConversation = true
// Only perform real deletion when it's not temp conversation (stored locally)
if (conversation != null) when {
conversation.conversation_extras_type != ParcelableMessageConversation.ExtrasType.TWITTER_OFFICIAL -> {
deleteMessages = false
deleteConversation = clearMessagesSync(taskContext, account, conversationId)
}
!conversation.is_temp -> if (!requestDestroyConversation(taskContext, account, conversationId)) {
return@task false
}
}
if (deleteMessages) {
val deleteMessageWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val deleteMessageWhereArgs = arrayOf(accountKey.toString(), conversationId)
taskContext.contentResolver.delete(Messages.CONTENT_URI, deleteMessageWhere, deleteMessageWhereArgs)
}
if (deleteConversation) {
val deleteConversationWhere = Expression.and(Expression.equalsArgs(Messages.Conversations.ACCOUNT_KEY),
Expression.equalsArgs(Messages.Conversations.CONVERSATION_ID)).sql
val deleteConversationWhereArgs = arrayOf(accountKey.toString(), conversationId)
taskContext.contentResolver.delete(Messages.Conversations.CONTENT_URI, deleteConversationWhere, deleteConversationWhereArgs)
}
return@task true
}
}
fun destroyMessage(context: Context, accountKey: UserKey, conversationId: String?,
messageId: String): Promise<Boolean, Exception> {
val weakContext by weak(context)
return task {
val taskContext = weakContext ?: throw InterruptedException()
val account = AccountUtils.getAccountDetails(AccountManager.get(taskContext), accountKey,
true) ?: throw AccountNotFoundException()
if (!requestDestroyMessage(taskContext, account, messageId)) {
return@task false
}
val deleteWhere: String
val deleteWhereArgs: Array<String>
if (conversationId != null) {
deleteWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID),
Expression.equalsArgs(Messages.MESSAGE_ID)).sql
deleteWhereArgs = arrayOf(accountKey.toString(), conversationId, messageId)
} else {
deleteWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.MESSAGE_ID)).sql
deleteWhereArgs = arrayOf(accountKey.toString(), messageId)
}
taskContext.contentResolver.delete(Messages.CONTENT_URI, deleteWhere, deleteWhereArgs)
return@task true
}
}
fun clearMessages(context: Context, accountKey: UserKey, conversationId: String): Promise<Boolean, Exception> {
val weakContext by weak(context)
return task {
val taskContext = weakContext ?: throw InterruptedException()
val account = AccountUtils.getAccountDetails(AccountManager.get(taskContext), accountKey,
true) ?: throw AccountNotFoundException()
clearMessagesSync(taskContext, account, conversationId)
}
}
private fun clearMessagesSync(context: Context, account: AccountDetails, conversationId: String): Boolean {
val messagesWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val messagesWhereArgs = arrayOf(account.key.toString(), conversationId)
val projection = arrayOf(Messages.MESSAGE_ID)
val messageIds = mutableListOf<String>()
var allSuccess = true
context.contentResolver.queryReference(Messages.CONTENT_URI, projection, messagesWhere,
messagesWhereArgs, null)?.use { (cur) ->
cur.moveToFirst()
while (!cur.isAfterLast) {
val messageId = cur.getString(0)
try {
if (requestDestroyMessage(context, account, messageId)) {
messageIds.add(messageId)
}
} catch (e: MicroBlogException) {
allSuccess = false
// Ignore
}
cur.moveToNext()
}
}
ContentResolverUtils.bulkDelete(context.contentResolver, Messages.CONTENT_URI,
Messages.MESSAGE_ID, false, messageIds, messagesWhere, messagesWhereArgs)
val conversationWhere = Expression.and(Expression.equalsArgs(Messages.Conversations.ACCOUNT_KEY),
Expression.equalsArgs(Messages.Conversations.CONVERSATION_ID)).sql
val conversationWhereArgs = arrayOf(account.key.toString(), conversationId)
context.contentResolver.updateItems(Messages.Conversations.CONTENT_URI,
Messages.Conversations.COLUMNS, conversationWhere, conversationWhereArgs,
cls = ParcelableMessageConversation::class.java) { item ->
item.message_extras = null
item.message_type = null
item.message_timestamp = -1L
item.text_unescaped = null
item.media = null
return@updateItems item
}
return allSuccess
}
private fun requestDestroyConversation(context: Context, account: AccountDetails, conversationId: String): Boolean {
when (account.type) {
AccountType.TWITTER -> {
if (account.isOfficial(context)) {
val twitter = account.newMicroBlogInstance(context, MicroBlog::class.java)
return twitter.deleteDmConversation(conversationId).isSuccessful
}
}
}
return false
}
private fun requestDestroyMessage(context: Context, account: AccountDetails,
messageId: String): Boolean {
when (account.type) {
AccountType.TWITTER -> {
val twitter = account.newMicroBlogInstance(context, cls = MicroBlog::class.java)
if (account.isOfficial(context)) {
return twitter.destroyDm(messageId).isSuccessful
}
twitter.destroyDirectMessage(messageId)
return true
}
}
return false
}
}

View File

@ -33,7 +33,6 @@ import android.support.annotation.WorkerThread
import android.support.media.ExifInterface
import android.text.TextUtils
import android.webkit.MimeTypeMap
import com.twitter.Validator
import net.ypresto.androidtranscoder.MediaTranscoder
import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets
import org.mariotaku.ktextension.*
@ -74,6 +73,7 @@ import org.mariotaku.twidere.task.BaseAbstractTask
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.io.ContentLengthInputStream
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
import org.mariotaku.twidere.util.text.TwitterValidator
import java.io.Closeable
import java.io.File
import java.io.FileNotFoundException
@ -241,7 +241,7 @@ class UpdateStatusTask(
update: ParcelableStatusUpdate,
pending: PendingStatusUpdate) {
if (shortener == null) return
val validator = Validator()
val validator = TwitterValidator()
stateCallback.onShorteningStatus()
val sharedShortened = HashMap<UserKey, StatusShortenResult>()
for (i in 0 until pending.length) {

View File

@ -24,7 +24,7 @@ import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.all
import nl.komponents.kovenant.task
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.exception.NoAccountException
import org.mariotaku.twidere.exception.AccountNotFoundException
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.provider.TwidereDataStore.SavedSearches
import org.mariotaku.twidere.task.PromiseTask
@ -40,7 +40,7 @@ class GetSavedSearchesTask(
return@map task {
val cr = context.contentResolver
val twitter = MicroBlogAPIFactory.getInstance(context, accountKey) ?:
throw NoAccountException()
throw AccountNotFoundException()
val searches = twitter.savedSearches
val values = ContentValuesCreator.createSavedSearches(searches,
accountKey)

View File

@ -1,107 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.task.twitter.message
import android.accounts.AccountManager
import android.content.Context
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.extension.queryReference
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.ParcelableMessageConversation
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.task.ExceptionHandlingAbstractTask
import org.mariotaku.twidere.util.content.ContentResolverUtils
import org.mariotaku.twidere.util.updateItems
/**
* Created by mariotaku on 2017/2/16.
*/
class ClearMessagesTask(
context: Context,
val accountKey: UserKey,
val conversationId: String
) : ExceptionHandlingAbstractTask<Unit?, Boolean, MicroBlogException, ((Boolean) -> Unit)?>(context) {
override val exceptionClass = MicroBlogException::class.java
override fun onExecute(params: Unit?): Boolean {
val account = AccountUtils.getAccountDetails(AccountManager.get(context), accountKey, true) ?:
throw MicroBlogException("No account")
clearMessages(context, account, conversationId)
return true
}
override fun afterExecute(callback: ((Boolean) -> Unit)?, result: Boolean?, exception: MicroBlogException?) {
callback?.invoke(result ?: false)
}
companion object {
fun clearMessages(context: Context, account: AccountDetails, conversationId: String): Boolean {
val microBlog = account.newMicroBlogInstance(context, cls = MicroBlog::class.java)
val messagesWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val messagesWhereArgs = arrayOf(account.key.toString(), conversationId)
val projection = arrayOf(Messages.MESSAGE_ID)
val messageIds = mutableListOf<String>()
var allSuccess = true
context.contentResolver.queryReference(Messages.CONTENT_URI, projection, messagesWhere,
messagesWhereArgs, null)?.use { (cur) ->
cur.moveToFirst()
while (!cur.isAfterLast) {
val messageId = cur.getString(0)
try {
if (DestroyMessageTask.performDestroyMessage(context, microBlog, account,
messageId)) {
messageIds.add(messageId)
}
} catch (e: MicroBlogException) {
allSuccess = false
// Ignore
}
cur.moveToNext()
}
}
ContentResolverUtils.bulkDelete(context.contentResolver, Messages.CONTENT_URI,
Messages.MESSAGE_ID, false, messageIds, messagesWhere, messagesWhereArgs)
val conversationWhere = Expression.and(Expression.equalsArgs(Messages.Conversations.ACCOUNT_KEY),
Expression.equalsArgs(Messages.Conversations.CONVERSATION_ID)).sql
val conversationWhereArgs = arrayOf(account.key.toString(), conversationId)
context.contentResolver.updateItems(Messages.Conversations.CONTENT_URI,
Messages.Conversations.COLUMNS, conversationWhere, conversationWhereArgs,
cls = ParcelableMessageConversation::class.java) { item ->
item.message_extras = null
item.message_type = null
item.message_timestamp = -1L
item.text_unescaped = null
item.media = null
return@updateItems item
}
return allSuccess
}
}
}

View File

@ -1,88 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.task.twitter.message
import android.accounts.AccountManager
import android.content.Context
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.extension.model.isOfficial
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.task.ExceptionHandlingAbstractTask
/**
* Created by mariotaku on 2017/2/16.
*/
class DestroyMessageTask(
context: Context,
val accountKey: UserKey,
val conversationId: String?,
val messageId: String
) : ExceptionHandlingAbstractTask<Unit?, Boolean, MicroBlogException, Unit?>(context) {
override val exceptionClass = MicroBlogException::class.java
override fun onExecute(params: Unit?): Boolean {
val account = AccountUtils.getAccountDetails(AccountManager.get(context), accountKey, true) ?:
throw MicroBlogException("No account")
val microBlog = account.newMicroBlogInstance(context, cls = MicroBlog::class.java)
if (!performDestroyMessage(context, microBlog, account, messageId)) {
return false
}
val deleteWhere: String
val deleteWhereArgs: Array<String>
if (conversationId != null) {
deleteWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID),
Expression.equalsArgs(Messages.MESSAGE_ID)).sql
deleteWhereArgs = arrayOf(accountKey.toString(), conversationId, messageId)
} else {
deleteWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.MESSAGE_ID)).sql
deleteWhereArgs = arrayOf(accountKey.toString(), messageId)
}
context.contentResolver.delete(Messages.CONTENT_URI, deleteWhere, deleteWhereArgs)
return true
}
companion object {
internal fun performDestroyMessage(context: Context, microBlog: MicroBlog,
account: AccountDetails, messageId: String): Boolean {
when (account.type) {
AccountType.TWITTER -> {
if (account.isOfficial(context)) {
return microBlog.destroyDm(messageId).isSuccessful
}
}
}
microBlog.destroyDirectMessage(messageId)
return true
}
}
}

View File

@ -0,0 +1,89 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.text
import com.twitter.Extractor
import com.twitter.Validator
import java.text.Normalizer
class TwitterValidator: Validator() {
val maxWeightedTweetLength: Int = 280
val defaultWeight: Int = 200
var ranges: Array<WeightRange> = arrayOf(
WeightRange(0, 4351, 100),
WeightRange(8192, 8205, 100),
WeightRange(8208, 8223, 100),
WeightRange(8242, 8247, 100)
)
private val extractor = Extractor()
override fun getTweetLength(text: String): Int {
val normalized = Normalizer.normalize(text, Normalizer.Form.NFC)
var weightedLength = 0
val inputLength = normalized.length
var charOffset = 0
while (charOffset < inputLength) {
val codePoint = Character.codePointAt(normalized, charOffset)
weightedLength += weightForCodePoint(codePoint)
charOffset += Character.charCount(codePoint)
}
var length = weightedLength / 100
for (urlEntity in extractor.extractURLsWithIndices(normalized)) {
length += urlEntity.start - urlEntity.end
length += if (urlEntity.value.toLowerCase().startsWith("https://")) shortUrlLengthHttps else shortUrlLength
}
return length
}
override fun isValidTweet(text: String?): Boolean {
if (text == null || text.isEmpty()) {
return false
}
for (c in text.toCharArray()) {
if (c == '\uFFFE' || c == '\uFEFF' || // BOM
c == '\uFFFF' || // Special
c in '\u202A'..'\u202E') { // Direction change
return false
}
}
return getTweetLength(text) <= maxWeightedTweetLength
}
private fun weightForCodePoint(codePoint: Int): Int {
val range = ranges.find { codePoint in it } ?: return defaultWeight
return range.weight
}
data class WeightRange(val start: Int, val end: Int, val weight: Int) {
operator fun contains(codePoint: Int): Boolean {
return codePoint in start..end
}
}
}

View File

@ -38,7 +38,7 @@ import org.mariotaku.ktextension.weak
import org.mariotaku.microblog.library.twitter.TwitterCaps
import org.mariotaku.microblog.library.twitter.model.CardDataMap
import org.mariotaku.twidere.R
import org.mariotaku.twidere.exception.NoAccountException
import org.mariotaku.twidere.exception.AccountNotFoundException
import org.mariotaku.twidere.extension.model.*
import org.mariotaku.twidere.model.ParcelableCardEntity
import org.mariotaku.twidere.model.ParcelableStatus
@ -92,7 +92,7 @@ class CardPollViewController : ContainerView.ViewController() {
task {
val vc = weakThis ?: throw IllegalStateException()
val details = AccountUtils.getAccountDetails(AccountManager.get(vc.context),
card.account_key, true) ?: throw NoAccountException()
card.account_key, true) ?: throw AccountNotFoundException()
val caps = details.newMicroBlogInstance(vc.context, cls = TwitterCaps::class.java)
val params = CardDataMap()
params.putString("card_uri", card.url)
@ -188,7 +188,7 @@ class CardPollViewController : ContainerView.ViewController() {
task {
val vc = weakThis ?: throw InterruptedException()
val details = AccountUtils.getAccountDetails(AccountManager.get(vc.context),
card.account_key, true) ?: throw NoAccountException()
card.account_key, true) ?: throw AccountNotFoundException()
val caps = details.newMicroBlogInstance(vc.context, cls = TwitterCaps::class.java)
val cardEntity = caps.sendPassThrough(cardData).card
return@task cardEntity.toParcelable(card.account_key, details.type)