2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite ;
2018-01-04 15:58:39 +01:00
/ * Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-01-04 15:58:39 +01:00
*
* 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 .
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even
2018-01-04 15:58:39 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General
* Public License for more details .
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab ; if not ,
2018-01-04 15:58:39 +01:00
* see < http : //www.gnu.org/licenses>. */
import android.content.ContentValues ;
import android.content.Context ;
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
2019-09-06 17:55:14 +02:00
2018-01-04 15:58:39 +01:00
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Account ;
import app.fedilab.android.helper.Helper ;
2018-01-04 15:58:39 +01:00
/ * *
* Created by Thomas on 04 / 01 / 2018 .
* Manage temp mute in DB
* /
public class TempMuteDAO {
public Context context ;
2019-11-15 16:32:25 +01:00
private SQLiteDatabase db ;
2018-01-04 15:58:39 +01:00
public TempMuteDAO ( Context context , SQLiteDatabase db ) {
//Creation of the DB with tables
this . context = context ;
this . db = db ;
}
//------- INSERTIONS -------
/ * *
* Insert a keyword in database
2019-09-06 17:55:14 +02:00
*
* @param account Account
2018-01-04 15:58:39 +01:00
* @param targeted_id String
2019-09-06 17:55:14 +02:00
* @param date Date
2018-01-04 15:58:39 +01:00
* /
2018-01-04 18:43:33 +01:00
public void insert ( Account account , String targeted_id , Date date ) {
2018-01-04 15:58:39 +01:00
ContentValues values = new ContentValues ( ) ;
values . put ( Sqlite . COL_TARGETED_USER_ID , targeted_id ) ;
2018-01-04 18:43:33 +01:00
values . put ( Sqlite . COL_ACCT , account . getAcct ( ) ) ;
values . put ( Sqlite . COL_INSTANCE , account . getInstance ( ) ) ;
2018-04-28 16:54:06 +02:00
values . put ( Sqlite . COL_DATE_CREATION , Helper . dateToString ( new Date ( ) ) ) ;
values . put ( Sqlite . COL_DATE_END , Helper . dateToString ( date ) ) ;
2018-01-04 15:58:39 +01:00
//Inserts temp mute
2019-09-06 17:55:14 +02:00
try {
2018-01-04 15:58:39 +01:00
db . insert ( Sqlite . TABLE_TEMP_MUTE , null , values ) ;
2019-09-06 17:55:14 +02:00
} catch ( Exception ignored ) {
}
2018-01-04 15:58:39 +01:00
}
2018-01-04 16:16:29 +01:00
//------- REMOVE -------
/ * * *
* Remove mute by its id for the authenticated user
* @return int
* /
2019-09-06 17:55:14 +02:00
public int remove ( Account account , String targeted_id ) {
return db . delete ( Sqlite . TABLE_TEMP_MUTE , Sqlite . COL_TARGETED_USER_ID + " = \" " + targeted_id + " \" AND " + Sqlite . COL_ACCT + " = \" " + account . getAcct ( ) + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + account . getInstance ( ) + " \" " , null ) ;
2018-01-04 16:16:29 +01:00
}
2018-01-04 15:58:39 +01:00
/ * * *
* Remove mute by its id
* /
2019-09-06 17:55:14 +02:00
public void removeOld ( ) {
db . delete ( Sqlite . TABLE_TEMP_MUTE , Sqlite . COL_DATE_END + " < \" " + Helper . dateToString ( new Date ( ) ) + " \" " , null ) ;
2018-01-04 15:58:39 +01:00
}
//------- GETTERS -------
/ * *
2018-01-04 16:16:29 +01:00
* Returns all id of timed mute in db
2019-09-06 17:55:14 +02:00
*
2018-01-04 16:16:29 +01:00
* @return time muted List < String >
2018-01-04 15:58:39 +01:00
* /
2019-09-06 17:55:14 +02:00
public List < String > getAllTimeMuted ( Account account ) {
2018-01-04 15:58:39 +01:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_TEMP_MUTE , null , Sqlite . COL_DATE_END + " >= \" " + Helper . dateToString ( new Date ( ) ) + " \" AND " + Sqlite . COL_ACCT + " = \" " + account . getAcct ( ) + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + account . getInstance ( ) + " \" " , null , null , null , null , null ) ;
2018-01-04 16:16:29 +01:00
return cursorToTimeMute ( c ) ;
2018-01-04 15:58:39 +01:00
} catch ( Exception e ) {
return null ;
}
}
/ * *
2018-01-04 17:31:53 +01:00
* Returns true if user is temp muted by the authenticated user
2019-09-06 17:55:14 +02:00
*
2018-01-04 15:58:39 +01:00
* @return keywords List < String >
* /
2019-09-06 17:55:14 +02:00
public boolean isTempMuted ( Account account , String targeted_id ) {
2018-01-04 15:58:39 +01:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_TEMP_MUTE , null , Sqlite . COL_TARGETED_USER_ID + " = \" " + targeted_id + " \" AND " + Sqlite . COL_DATE_END + " >= \" " + Helper . dateToString ( new Date ( ) ) + " \" AND " + Sqlite . COL_ACCT + " = \" " + account . getAcct ( ) + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + account . getInstance ( ) + " \" " , null , null , null , null , null ) ;
2018-01-04 16:16:29 +01:00
return cursorToTimeMute ( c ) ! = null ;
2018-01-04 15:58:39 +01:00
} catch ( Exception e ) {
2018-01-04 16:16:29 +01:00
return false ;
2018-01-04 15:58:39 +01:00
}
}
2018-01-04 16:16:29 +01:00
2018-01-04 15:58:39 +01:00
/ * *
2018-01-04 18:43:33 +01:00
* Returns true if user is temp muted by the authenticated user
2019-09-06 17:55:14 +02:00
*
2018-01-04 15:58:39 +01:00
* @return keywords List < String >
* /
2019-09-06 17:55:14 +02:00
public String getMuteDateByID ( Account account , String targeted_id ) {
2018-01-04 15:58:39 +01:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_TEMP_MUTE , null , Sqlite . COL_TARGETED_USER_ID + " = \" " + targeted_id + " \" AND " + Sqlite . COL_DATE_END + " >= \" " + Helper . dateToString ( new Date ( ) ) + " \" AND " + Sqlite . COL_ACCT + " = \" " + account . getAcct ( ) + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + account . getInstance ( ) + " \" " , null , null , null , Sqlite . COL_DATE_END + " DESC " , " 1 " ) ;
2018-01-04 18:43:33 +01:00
return cursorToDate ( c ) ;
2018-01-04 15:58:39 +01:00
} catch ( Exception e ) {
2018-01-04 18:43:33 +01:00
e . printStackTrace ( ) ;
return null ;
2018-01-04 15:58:39 +01:00
}
}
/ * * *
2018-01-04 16:16:29 +01:00
* Method to hydrate time mute id from database
2018-01-04 15:58:39 +01:00
* @param c Cursor
* @return List < String >
* /
2019-09-06 17:55:14 +02:00
private List < String > cursorToTimeMute ( Cursor c ) {
2018-01-04 15:58:39 +01:00
//No element found
2019-08-16 17:35:18 +02:00
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
2018-01-04 15:58:39 +01:00
return null ;
2019-08-16 17:35:18 +02:00
}
2018-01-04 16:16:29 +01:00
List < String > timeMutes = new ArrayList < > ( ) ;
2019-09-06 17:55:14 +02:00
while ( c . moveToNext ( ) ) {
2018-01-04 16:16:29 +01:00
timeMutes . add ( c . getString ( c . getColumnIndex ( Sqlite . COL_TARGETED_USER_ID ) ) ) ;
2018-01-04 15:58:39 +01:00
}
//Close the cursor
c . close ( ) ;
2018-01-04 16:16:29 +01:00
//Time mute id list is returned
return timeMutes ;
2018-01-04 15:58:39 +01:00
}
2018-01-04 18:43:33 +01:00
/ * * *
* Method to get date for the timed muted account by its id
* @param c Cursor
* @return String
* /
2019-09-06 17:55:14 +02:00
private String cursorToDate ( Cursor c ) {
2018-01-04 18:43:33 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
2018-01-04 18:43:33 +01:00
return null ;
2019-08-17 14:34:33 +02:00
}
2018-01-04 18:43:33 +01:00
c . moveToNext ( ) ;
String date = c . getString ( c . getColumnIndex ( Sqlite . COL_DATE_END ) ) ;
//Close the cursor
c . close ( ) ;
//Time mute date is returned
return date ;
}
2018-01-04 15:58:39 +01:00
}