2017-11-10 19:26:25 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* Lesser General Public License as published by the Free Software Foundation, either version 3 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky 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 Lesser
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If
|
|
|
|
* not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2018-02-04 08:45:14 +11:00
|
|
|
import android.util.Log;
|
2017-11-02 07:02:44 +11:00
|
|
|
|
|
|
|
import com.evernote.android.job.Job;
|
|
|
|
import com.evernote.android.job.JobCreator;
|
2018-02-04 08:45:14 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
2018-03-10 08:02:32 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2017-11-02 07:02:44 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi;
|
2018-02-13 08:03:08 +11:00
|
|
|
import com.keylesspalace.tusky.util.NotificationHelper;
|
2017-11-02 07:02:44 +11:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-02-04 08:45:14 +11:00
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.util.ArrayList;
|
2018-03-02 05:05:47 +11:00
|
|
|
import java.util.Collections;
|
2017-11-02 07:02:44 +11:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 31/10/17.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public final class NotificationPullJobCreator implements JobCreator {
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private static final String TAG = "NotificationPJC";
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
static final String NOTIFICATIONS_JOB_TAG = "notifications_job_tag";
|
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
private final MastodonApi api;
|
|
|
|
private final Context context;
|
|
|
|
private final AccountManager accountManager;
|
2017-11-02 07:02:44 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
@Inject NotificationPullJobCreator(MastodonApi api, Context context,
|
|
|
|
AccountManager accountManager) {
|
|
|
|
this.api = api;
|
2017-11-02 07:02:44 +11:00
|
|
|
this.context = context;
|
2018-03-28 04:47:00 +11:00
|
|
|
this.accountManager = accountManager;
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Job create(@NonNull String tag) {
|
|
|
|
if (tag.equals(NOTIFICATIONS_JOB_TAG)) {
|
2018-03-28 04:47:00 +11:00
|
|
|
return new NotificationPullJob(context, accountManager, api);
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final static class NotificationPullJob extends Job {
|
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
private final Context context;
|
|
|
|
private final AccountManager accountManager;
|
|
|
|
private final MastodonApi mastodonApi;
|
2017-11-02 07:02:44 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
NotificationPullJob(Context context, AccountManager accountManager,
|
|
|
|
MastodonApi mastodonApi) {
|
2017-11-02 07:02:44 +11:00
|
|
|
this.context = context;
|
2018-03-28 04:47:00 +11:00
|
|
|
this.accountManager = accountManager;
|
|
|
|
this.mastodonApi = mastodonApi;
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
2018-02-13 08:03:08 +11:00
|
|
|
protected Result onRunJob(@NonNull Params params) {
|
2018-03-10 08:02:32 +11:00
|
|
|
List<AccountEntity> accountList = new ArrayList<>(accountManager.getAllAccountsOrderedByActive());
|
|
|
|
for (AccountEntity account : accountList) {
|
|
|
|
if (account.getNotificationsEnabled()) {
|
2018-02-04 08:45:14 +11:00
|
|
|
try {
|
2018-03-10 08:02:32 +11:00
|
|
|
Log.d(TAG, "getting Notifications for " + account.getFullName());
|
2018-02-04 08:45:14 +11:00
|
|
|
Response<List<Notification>> notifications =
|
2018-03-28 04:47:00 +11:00
|
|
|
mastodonApi.notificationsWithAuth(
|
|
|
|
String.format("Bearer %s", account.getAccessToken()),
|
|
|
|
account.getDomain()
|
|
|
|
)
|
|
|
|
.execute();
|
2018-02-04 08:45:14 +11:00
|
|
|
if (notifications.isSuccessful()) {
|
|
|
|
onNotificationsReceived(account, notifications.body());
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "error receiving notificationsEnabled");
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "error receiving notificationsEnabled", e);
|
|
|
|
}
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
return Result.SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private void onNotificationsReceived(AccountEntity account, List<Notification> notificationList) {
|
2018-03-02 05:05:47 +11:00
|
|
|
Collections.reverse(notificationList);
|
2018-02-04 08:45:14 +11:00
|
|
|
BigInteger newId = new BigInteger(account.getLastNotificationId());
|
|
|
|
BigInteger newestId = BigInteger.ZERO;
|
2018-06-25 22:08:36 +10:00
|
|
|
boolean isFirstOfBatch = true;
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
for (Notification notification : notificationList) {
|
2018-03-03 23:24:03 +11:00
|
|
|
BigInteger currentId = new BigInteger(notification.getId());
|
2018-03-10 08:02:32 +11:00
|
|
|
if (isBiggerThan(currentId, newestId)) {
|
2018-02-04 08:45:14 +11:00
|
|
|
newestId = currentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBiggerThan(currentId, newId)) {
|
2018-06-25 22:08:36 +10:00
|
|
|
NotificationHelper.make(context, notification, account, isFirstOfBatch);
|
|
|
|
isFirstOfBatch = false;
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
account.setLastNotificationId(newestId.toString());
|
2018-03-28 04:47:00 +11:00
|
|
|
accountManager.saveAccount(account);
|
2018-02-04 08:45:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isBiggerThan(BigInteger newId, BigInteger lastShownNotificationId) {
|
2018-05-09 03:15:10 +10:00
|
|
|
return lastShownNotificationId.compareTo(newId) < 0;
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|