Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
10 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import java.util.List;
  17. import androidx.lifecycle.LiveData;
  18. import androidx.paging.DataSource;
  19. import androidx.room.Dao;
  20. import androidx.room.Insert;
  21. import androidx.room.Query;
  22. import androidx.room.Update;
  23. @Dao
  24. public interface DaoMessage {
  25. // About 'dummy': "When the min() or max() aggregate functions are used in an aggregate query,
  26. // all bare columns in the result set take values from the input row which also contains the minimum or maximum."
  27. // https://www.sqlite.org/lang_select.html
  28. @Query("SELECT message.*" +
  29. ", account.name AS accountName, account.color AS accountColor" +
  30. ", folder.name as folderName, folder.type as folderType" +
  31. ", COUNT(message.id) as count" +
  32. ", SUM(CASE WHEN message.ui_seen" +
  33. " OR folder.type = '" + EntityFolder.OUTBOX + "'" +
  34. " OR folder.type = '" + EntityFolder.DRAFTS + "' THEN 0 ELSE 1 END) as unseen" +
  35. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  36. ", MAX(CASE WHEN folder.unified THEN message.id ELSE 0 END) as dummy" +
  37. " FROM message" +
  38. " JOIN account ON account.id = message.account" +
  39. " JOIN folder ON folder.id = message.folder" +
  40. " WHERE account.`synchronize`" +
  41. " AND (NOT message.ui_hide OR :debug)" +
  42. " GROUP BY account.id, CASE WHEN message.thread IS NULL THEN message.id ELSE message.thread END" +
  43. " HAVING SUM(unified) > 0" +
  44. " ORDER BY CASE" +
  45. " WHEN 'unread' = :sort THEN NOT message.seen" +
  46. " WHEN 'starred' = :sort THEN message.flagged" +
  47. " ELSE 0" +
  48. " END DESC, message.received DESC, message.sent DESC")
  49. DataSource.Factory<Integer, TupleMessageEx> pagedUnifiedInbox(String sort, boolean debug);
  50. @Query("SELECT message.*" +
  51. ", account.name AS accountName, account.color AS accountColor" +
  52. ", folder.name as folderName, folder.type as folderType" +
  53. ", COUNT(message.id) as count" +
  54. ", SUM(CASE WHEN message.ui_seen" +
  55. " OR (folder.id <> :folder AND folder.type = '" + EntityFolder.OUTBOX + "')" +
  56. " OR (folder.id <> :folder AND folder.type = '" + EntityFolder.DRAFTS + "') THEN 0 ELSE 1 END) as unseen" +
  57. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  58. ", MAX(CASE WHEN folder.id = :folder THEN message.id ELSE 0 END) as dummy" +
  59. " FROM message" +
  60. " JOIN account ON account.id = message.account" +
  61. " JOIN folder ON folder.id = message.folder" +
  62. " JOIN folder f ON f.id = :folder" +
  63. " WHERE (message.account = f.account OR folder.type = '" + EntityFolder.OUTBOX + "')" +
  64. " AND (NOT message.ui_hide OR :debug)" +
  65. " AND (NOT :found OR ui_found = :found)" +
  66. " GROUP BY CASE WHEN message.thread IS NULL THEN message.id ELSE message.thread END" +
  67. " HAVING SUM(CASE WHEN folder.id = :folder THEN 1 ELSE 0 END) > 0" +
  68. " ORDER BY CASE" +
  69. " WHEN 'unread' = :sort THEN NOT message.seen" +
  70. " WHEN 'starred' = :sort THEN message.flagged" +
  71. " ELSE 0" +
  72. " END DESC, message.received DESC, message.sent DESC")
  73. DataSource.Factory<Integer, TupleMessageEx> pagedFolder(long folder, String sort, boolean found, boolean debug);
  74. @Query("SELECT message.*" +
  75. ", account.name AS accountName, account.color AS accountColor" +
  76. ", folder.name as folderName, folder.type as folderType" +
  77. ", 1 AS count" +
  78. ", CASE WHEN message.ui_seen THEN 0 ELSE 1 END as unseen" +
  79. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  80. " FROM message" +
  81. " JOIN account ON account.id = message.account" +
  82. " JOIN folder ON folder.id = message.folder" +
  83. " WHERE (NOT message.ui_hide OR :debug)" +
  84. " AND message.account = (SELECT m1.account FROM message m1 WHERE m1.id = :msgid)" +
  85. " AND message.thread = (SELECT m2.thread FROM message m2 WHERE m2.id = :msgid)" +
  86. " ORDER BY CASE" +
  87. " WHEN 'unread' = :sort THEN NOT message.seen" +
  88. " WHEN 'starred' = :sort THEN message.flagged" +
  89. " ELSE 0" +
  90. " END DESC, message.received DESC, message.sent DESC")
  91. DataSource.Factory<Integer, TupleMessageEx> pagedThread(long msgid, String sort, boolean debug);
  92. @Query("SELECT *" +
  93. " FROM message" +
  94. " WHERE id = :id")
  95. EntityMessage getMessage(long id);
  96. @Query("SELECT COUNT(id)" +
  97. " FROM message" +
  98. " WHERE id = :id")
  99. int countMessage(long id);
  100. @Query("SELECT *" +
  101. " FROM message" +
  102. " WHERE folder = :folder" +
  103. " AND uid = :uid")
  104. EntityMessage getMessageByUid(long folder, long uid);
  105. @Query("SELECT message.* FROM message" +
  106. " JOIN folder ON folder.id = message.folder" +
  107. " WHERE message.account = :account" +
  108. " AND (message.msgid = :msgid" +
  109. " OR message.msgid = :reference)")
  110. List<EntityMessage> getMessageByMsgId(long account, String msgid, String reference);
  111. @Query("SELECT message.* FROM message" +
  112. " JOIN folder ON folder.id = message.folder" +
  113. " WHERE message.account = :account" +
  114. " AND message.thread = :thread" +
  115. " AND folder.type <> '" + EntityFolder.OUTBOX + "'" +
  116. " AND folder.type <> '" + EntityFolder.DRAFTS + "'")
  117. List<EntityMessage> getMessageByThread(long account, String thread);
  118. @Query("SELECT id FROM message" +
  119. " WHERE folder = :folder" +
  120. " ORDER BY message.received DESC, message.sent DESC")
  121. List<Long> getMessageIDs(long folder);
  122. @Query("SELECT message.*" +
  123. ", account.name AS accountName, account.color AS accountColor" +
  124. ", folder.name as folderName, folder.type as folderType" +
  125. ", (SELECT COUNT(m1.id) FROM message m1 WHERE m1.account = message.account AND m1.thread = message.thread AND NOT m1.ui_hide) AS count" +
  126. ", (SELECT COUNT(m2.id) FROM message m2 WHERE m2.account = message.account AND m2.thread = message.thread AND NOT m2.ui_hide AND NOT m2.ui_seen) AS unseen" +
  127. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  128. " FROM message" +
  129. " LEFT JOIN account ON account.id = message.account" +
  130. " JOIN folder ON folder.id = message.folder" +
  131. " WHERE message.id = :id")
  132. LiveData<TupleMessageEx> liveMessage(long id);
  133. @Query("SELECT message.* FROM message" +
  134. " JOIN account ON account.id = message.account" +
  135. " JOIN folder ON folder.id = message.folder" +
  136. " WHERE account.`synchronize`" +
  137. " AND folder.unified" +
  138. " AND NOT message.ui_seen AND NOT message.ui_hide" +
  139. " AND (account.seen_until IS NULL OR message.stored > account.seen_until)" +
  140. " ORDER BY message.received")
  141. LiveData<List<EntityMessage>> liveUnseenUnified();
  142. @Query("SELECT uid FROM message" +
  143. " WHERE folder = :folder" +
  144. " AND received >= :received" +
  145. " AND NOT uid IS NULL" +
  146. " AND NOT ui_found" /* keep found messages */)
  147. List<Long> getUids(long folder, long received);
  148. @Insert
  149. long insertMessage(EntityMessage message);
  150. @Update
  151. void updateMessage(EntityMessage message);
  152. @Query("UPDATE message SET uid = :uid WHERE id = :id")
  153. int setMessageUid(long id, Long uid);
  154. @Query("UPDATE message SET seen = :seen WHERE id = :id")
  155. int setMessageSeen(long id, boolean seen);
  156. @Query("UPDATE message SET ui_seen = :ui_seen WHERE id = :id")
  157. int setMessageUiSeen(long id, boolean ui_seen);
  158. @Query("UPDATE message SET flagged = :flagged WHERE id = :id")
  159. int setMessageFlagged(long id, boolean flagged);
  160. @Query("UPDATE message SET ui_flagged = :ui_flagged WHERE id = :id")
  161. int setMessageUiFlagged(long id, boolean ui_flagged);
  162. @Query("UPDATE message SET ui_hide = :ui_hide WHERE id = :id")
  163. int setMessageUiHide(long id, boolean ui_hide);
  164. @Query("UPDATE message SET error = :error WHERE id = :id")
  165. int setMessageError(long id, String error);
  166. @Query("UPDATE message SET ui_found = :found WHERE id = :id")
  167. int setMessageFound(long id, boolean found);
  168. @Query("UPDATE message SET ui_found = 0 WHERE folder = :folder")
  169. int resetFound(long folder);
  170. @Query("UPDATE message SET content = :content WHERE id = :id")
  171. int setMessageContent(long id, boolean content);
  172. @Query("UPDATE message SET headers = :headers WHERE id = :id")
  173. int setMessageHeaders(long id, String headers);
  174. @Query("UPDATE message SET avatar = :avatar WHERE id = :id")
  175. int setMessageAvatar(long id, String avatar);
  176. @Query("UPDATE message SET avatar = NULL")
  177. void clearMessageAvatars();
  178. @Query("DELETE FROM message WHERE id = :id")
  179. int deleteMessage(long id);
  180. @Query("DELETE FROM message WHERE folder = :folder AND uid = :uid")
  181. int deleteMessage(long folder, long uid);
  182. @Query("DELETE FROM message WHERE folder = :folder AND NOT uid IS NULL")
  183. int deleteLocalMessages(long folder);
  184. @Query("DELETE FROM message WHERE folder = :folder AND seen")
  185. int deleteSeenMessages(long folder);
  186. @Query("DELETE FROM message WHERE folder = :folder AND received < :received AND NOT uid IS NULL")
  187. int deleteMessagesBefore(long folder, long received);
  188. @Query("DELETE FROM message WHERE ui_found")
  189. int deleteFoundMessages();
  190. }