| 1 | use chrono::{DateTime, Utc}; |
| 2 | use rusqlite::{OptionalExtension, Row, params}; |
| 3 | |
| 4 | use crate::error::Result; |
| 5 | use crate::models::{ChannelReadState, ChannelRecord, TeamRecord}; |
| 6 | use crate::store::{Store, json_ids}; |
| 7 | use crate::time::{optional_from_millis, optional_to_millis}; |
| 8 | |
| 9 | const CHANNEL_COLUMNS: &str = "id, team_id, name, membership_type, last_message_at, unread"; |
| 10 | |
| 11 | impl Store { |
| 12 | pub fn upsert_teams(&self, teams: &[TeamRecord]) -> Result<()> { |
| 13 | let mut connection = self.lock()?; |
| 14 | let transaction = connection.transaction()?; |
| 15 | { |
| 16 | let mut upsert = transaction.prepare_cached( |
| 17 | "INSERT INTO teams (id, name) VALUES (?1, ?2) ON CONFLICT (id) DO UPDATE SET name = excluded.name", |
| 18 | )?; |
| 19 | for team in teams { |
| 20 | upsert.execute(params![team.id, team.name])?; |
| 21 | } |
| 22 | } |
| 23 | Ok(transaction.commit()?) |
| 24 | } |
| 25 | |
| 26 | pub fn upsert_channels(&self, channels: &[ChannelRecord]) -> Result<()> { |
| 27 | let mut connection = self.lock()?; |
| 28 | let transaction = connection.transaction()?; |
| 29 | { |
| 30 | let mut upsert = transaction.prepare_cached( |
| 31 | "INSERT INTO channels (id, team_id, name, membership_type, last_message_at, unread) |
| 32 | VALUES (?1, ?2, ?3, ?4, ?5, ?6) |
| 33 | ON CONFLICT (id) DO UPDATE SET |
| 34 | team_id = excluded.team_id, name = excluded.name, membership_type = excluded.membership_type, |
| 35 | last_message_at = COALESCE(excluded.last_message_at, channels.last_message_at)", |
| 36 | )?; |
| 37 | for channel in channels { |
| 38 | upsert.execute(params![ |
| 39 | channel.id, |
| 40 | channel.team_id, |
| 41 | channel.name, |
| 42 | channel.membership_type, |
| 43 | optional_to_millis(channel.last_message_at), |
| 44 | channel.unread, |
| 45 | ])?; |
| 46 | } |
| 47 | } |
| 48 | Ok(transaction.commit()?) |
| 49 | } |
| 50 | |
| 51 | /// Read state comes from chatsvc only; channel metadata refreshes leave it alone. |
| 52 | pub fn apply_channel_read_states( |
| 53 | &self, |
| 54 | states: &[ChannelReadState], |
| 55 | ) -> Result<()> { |
| 56 | let mut connection = self.lock()?; |
| 57 | let transaction = connection.transaction()?; |
| 58 | { |
| 59 | let mut update = transaction.prepare_cached( |
| 60 | "UPDATE channels SET unread = ?2, |
| 61 | last_message_at = COALESCE(?3, last_message_at), |
| 62 | read_until = COALESCE(?4, read_until) |
| 63 | WHERE id = ?1", |
| 64 | )?; |
| 65 | for state in states { |
| 66 | update.execute(params![ |
| 67 | state.channel_id, |
| 68 | state.unread, |
| 69 | optional_to_millis(state.last_message_at), |
| 70 | optional_to_millis(state.read_until) |
| 71 | ])?; |
| 72 | } |
| 73 | } |
| 74 | Ok(transaction.commit()?) |
| 75 | } |
| 76 | |
| 77 | pub fn mark_channel_read(&self, channel_id: &str) -> Result<()> { |
| 78 | self.lock()? |
| 79 | .execute("UPDATE channels SET unread = 0 WHERE id = ?1", [channel_id])?; |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | pub fn mark_all_channels_read(&self) -> Result<()> { |
| 84 | let mut connection = self.lock()?; |
| 85 | let transaction = connection.transaction()?; |
| 86 | transaction.execute("UPDATE channels SET unread = 0 WHERE unread != 0", [])?; |
| 87 | transaction.execute("DELETE FROM channel_mentions", [])?; |
| 88 | Ok(transaction.commit()?) |
| 89 | } |
| 90 | |
| 91 | pub fn remove_teams_except(&self, kept_team_ids: &[String]) -> Result<usize> { |
| 92 | let mut connection = self.lock()?; |
| 93 | let transaction = connection.transaction()?; |
| 94 | let kept = json_ids(kept_team_ids); |
| 95 | let removed = transaction.execute( |
| 96 | "DELETE FROM teams WHERE id NOT IN (SELECT value FROM json_each(?1))", |
| 97 | [kept], |
| 98 | )?; |
| 99 | transaction.commit()?; |
| 100 | Ok(removed) |
| 101 | } |
| 102 | |
| 103 | pub fn remove_channels_except( |
| 104 | &self, |
| 105 | team_id: &str, |
| 106 | kept_channel_ids: &[String], |
| 107 | ) -> Result<usize> { |
| 108 | let connection = self.lock()?; |
| 109 | Ok(connection.execute( |
| 110 | "DELETE FROM channels WHERE team_id = ?1 AND id NOT IN (SELECT value FROM json_each(?2))", |
| 111 | params![team_id, json_ids(kept_channel_ids)], |
| 112 | )?) |
| 113 | } |
| 114 | |
| 115 | pub fn team(&self, team_id: &str) -> Result<Option<TeamRecord>> { |
| 116 | let connection = self.lock()?; |
| 117 | Ok(connection |
| 118 | .query_row( |
| 119 | "SELECT id, name FROM teams WHERE id = ?1", |
| 120 | [team_id], |
| 121 | |row| { |
| 122 | Ok(TeamRecord { |
| 123 | id: row.get(0)?, |
| 124 | name: row.get(1)?, |
| 125 | }) |
| 126 | }, |
| 127 | ) |
| 128 | .optional()?) |
| 129 | } |
| 130 | |
| 131 | pub fn channel_read_until(&self, channel_id: &str) -> Result<Option<DateTime<Utc>>> { |
| 132 | let connection = self.lock()?; |
| 133 | let read_until: Option<Option<i64>> = connection |
| 134 | .query_row( |
| 135 | "SELECT read_until FROM channels WHERE id = ?1", |
| 136 | [channel_id], |
| 137 | |row| row.get(0), |
| 138 | ) |
| 139 | .optional()?; |
| 140 | Ok(optional_from_millis(read_until.flatten())) |
| 141 | } |
| 142 | |
| 143 | pub fn channel(&self, channel_id: &str) -> Result<Option<ChannelRecord>> { |
| 144 | let connection = self.lock()?; |
| 145 | let mut statement = connection.prepare_cached(&format!( |
| 146 | "SELECT {CHANNEL_COLUMNS} FROM channels WHERE id = ?1" |
| 147 | ))?; |
| 148 | let mut channels = statement |
| 149 | .query_map([channel_id], channel_from_row)? |
| 150 | .collect::<rusqlite::Result<Vec<_>>>()?; |
| 151 | Ok(channels.pop()) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | pub(crate) fn channel_from_row(row: &Row<'_>) -> rusqlite::Result<ChannelRecord> { |
| 156 | Ok(ChannelRecord { |
| 157 | id: row.get(0)?, |
| 158 | team_id: row.get(1)?, |
| 159 | name: row.get(2)?, |
| 160 | membership_type: row.get(3)?, |
| 161 | last_message_at: optional_from_millis(row.get(4)?), |
| 162 | unread: row.get(5)?, |
| 163 | }) |
| 164 | } |