Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppDevice.kt
WhatsAppDevice.kt151 lines · 4.3 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import androidx.room3.Dao |
| 4 | import androidx.room3.Entity |
| 5 | import androidx.room3.Insert |
| 6 | import androidx.room3.OnConflictStrategy |
| 7 | import androidx.room3.PrimaryKey |
| 8 | import androidx.room3.Query |
| 9 | |
| 10 | /** |
| 11 | * Device information for WhatsApp Web companion. |
| 12 | * Stores per-device metadata for multi-device support. |
| 13 | */ |
| 14 | @Entity(tableName = "whatsapp_devices") |
| 15 | data class WhatsAppDevice( |
| 16 | @PrimaryKey |
| 17 | val deviceId: String, |
| 18 | val phoneNumber: String, |
| 19 | val pushName: String, |
| 20 | val platform: String, |
| 21 | val lastSeen: Long, |
| 22 | val jid: String = "", // Full JID including device suffix |
| 23 | // From Go UserLoginMetadata |
| 24 | val timezone: String = "", |
| 25 | val loggedInAt: Long = 0L, |
| 26 | val phoneLastSeen: Long = 0L, |
| 27 | val phoneLastPinged: Long = 0L, |
| 28 | val businessName: String = "", |
| 29 | ) |
| 30 | |
| 31 | /** |
| 32 | * Signal Protocol session data for E2E encryption. |
| 33 | * Each peer device gets its own session record. |
| 34 | * Based on whatsmeow's signal session store. |
| 35 | */ |
| 36 | @Entity(tableName = "whatsapp_sessions") |
| 37 | data class WhatsAppSession( |
| 38 | @PrimaryKey |
| 39 | val jid: String, // e.g., "1234567890:[email protected]" (includes device ID) |
| 40 | val sessionData: ByteArray, // Serialized Signal session record |
| 41 | val timestamp: Long, |
| 42 | val isPreKey: Boolean = false, // Whether this session was established via pre-key |
| 43 | ) { |
| 44 | override fun equals(other: Any?): Boolean { |
| 45 | if (this === other) return true |
| 46 | if (javaClass != other?.javaClass) return false |
| 47 | other as WhatsAppSession |
| 48 | if (jid != other.jid) return false |
| 49 | if (!sessionData.contentEquals(other.sessionData)) return false |
| 50 | return timestamp == other.timestamp |
| 51 | } |
| 52 | |
| 53 | override fun hashCode(): Int { |
| 54 | var result = jid.hashCode() |
| 55 | result = 31 * result + sessionData.contentHashCode() |
| 56 | result = 31 * result + timestamp.hashCode() |
| 57 | return result |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** Queries for linked WhatsApp devices. */ |
| 62 | @Dao |
| 63 | interface WhatsAppDeviceDao { |
| 64 | @Query("SELECT * FROM whatsapp_devices WHERE deviceId = :deviceId") |
| 65 | suspend fun getDevice(deviceId: String): WhatsAppDevice? |
| 66 | |
| 67 | @Insert(onConflict = OnConflictStrategy.REPLACE) |
| 68 | suspend fun insertDevice(device: WhatsAppDevice) |
| 69 | |
| 70 | @Query("DELETE FROM whatsapp_devices WHERE deviceId = :deviceId") |
| 71 | suspend fun deleteDevice(deviceId: String) |
| 72 | } |
| 73 | |
| 74 | /** Queries for stored WhatsApp protocol sessions. */ |
| 75 | @Dao |
| 76 | interface WhatsAppSessionDao { |
| 77 | @Query("SELECT * FROM whatsapp_sessions WHERE jid = :jid") |
| 78 | suspend fun getSession(jid: String): WhatsAppSession? |
| 79 | |
| 80 | @Query("SELECT EXISTS(SELECT 1 FROM whatsapp_sessions WHERE jid = :jid)") |
| 81 | suspend fun containsSession(jid: String): Boolean |
| 82 | |
| 83 | @Query("SELECT * FROM whatsapp_sessions") |
| 84 | suspend fun getAllSessions(): List<WhatsAppSession> |
| 85 | |
| 86 | @Insert(onConflict = OnConflictStrategy.REPLACE) |
| 87 | suspend fun insertSession(session: WhatsAppSession) |
| 88 | |
| 89 | @Query("DELETE FROM whatsapp_sessions WHERE jid = :jid") |
| 90 | suspend fun deleteSession(jid: String) |
| 91 | |
| 92 | @Query("DELETE FROM whatsapp_sessions") |
| 93 | suspend fun clearAllSessions() |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Message metadata matching Go's waid.MessageMetadata. |
| 98 | * Stored as JSON alongside messages. |
| 99 | */ |
| 100 | data class MessageMetadata( |
| 101 | val senderDeviceId: Int = 0, |
| 102 | val error: MessageErrorType = MessageErrorType.NO_ERROR, |
| 103 | val broadcastListJid: String? = null, |
| 104 | val isMatrixPoll: Boolean = false, |
| 105 | val edits: List<String> = emptyList(), |
| 106 | ) |
| 107 | |
| 108 | /** Why a WhatsApp message could not be processed (decryption, media, …). */ |
| 109 | enum class MessageErrorType { |
| 110 | NO_ERROR, |
| 111 | DECRYPTION_FAILED, |
| 112 | MEDIA_NOT_FOUND, |
| 113 | UNDECRYPTABLE, |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Portal metadata matching Go's waid.PortalMetadata. |
| 118 | */ |
| 119 | data class PortalMetadata( |
| 120 | val disappearingTimerSetAt: Long = 0L, |
| 121 | val topicId: String = "", |
| 122 | val lastSync: Long = 0L, |
| 123 | val communityAnnouncementGroup: Boolean = false, |
| 124 | val addressingMode: String = "", |
| 125 | ) |
| 126 | |
| 127 | /** |
| 128 | * Ghost metadata matching Go's waid.GhostMetadata. |
| 129 | */ |
| 130 | data class GhostMetadata( |
| 131 | val lastSync: Long = 0L, |
| 132 | ) |
| 133 | |
| 134 | /** |
| 135 | * Reaction metadata matching Go's waid.ReactionMetadata. |
| 136 | */ |
| 137 | data class ReactionMetadata( |
| 138 | val senderDeviceId: Int = 0, |
| 139 | ) |
| 140 | |
| 141 | /** |
| 142 | * Group invite metadata matching Go's waid.GroupInviteMeta. |
| 143 | */ |
| 144 | data class GroupInviteMeta( |
| 145 | val jid: String, |
| 146 | val code: String, |
| 147 | val expiration: Long, |
| 148 | val inviter: String, |
| 149 | val groupName: String, |
| 150 | val isParentGroup: Boolean = false, |
| 151 | ) |