BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / teams.rs

teams.rs

teams.rs 5.3 KiB Download Raw
1use chrono::{DateTime, Utc};
2use rusqlite::{OptionalExtension, Row, params};
3
4use crate::error::Result;
5use crate::models::{ChannelRecord, TeamRecord};
6use crate::store::{Store, json_ids};
7use crate::time::{optional_from_millis, optional_to_millis};
8
9const CHANNEL_COLUMNS: &str = "id, team_id, name, membership_type, last_message_at, unread";
10
11impl 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: &[(String, bool, Option<DateTime<Utc>>)],
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 WHERE id = ?1",
63 )?;
64 for (channel_id, unread, last_message_at) in states {
65 update.execute(params![
66 channel_id,
67 unread,
68 optional_to_millis(*last_message_at)
69 ])?;
70 }
71 }
72 Ok(transaction.commit()?)
73 }
74
75 pub fn mark_channel_read(&self, channel_id: &str) -> Result<()> {
76 self.lock()?
77 .execute("UPDATE channels SET unread = 0 WHERE id = ?1", [channel_id])?;
78 Ok(())
79 }
80
81 pub fn mark_all_channels_read(&self) -> Result<()> {
82 let mut connection = self.lock()?;
83 let transaction = connection.transaction()?;
84 transaction.execute("UPDATE channels SET unread = 0 WHERE unread != 0", [])?;
85 transaction.execute("DELETE FROM channel_mentions", [])?;
86 Ok(transaction.commit()?)
87 }
88
89 pub fn remove_teams_except(&self, kept_team_ids: &[String]) -> Result<usize> {
90 let mut connection = self.lock()?;
91 let transaction = connection.transaction()?;
92 let kept = json_ids(kept_team_ids);
93 let removed = transaction.execute(
94 "DELETE FROM teams WHERE id NOT IN (SELECT value FROM json_each(?1))",
95 [kept],
96 )?;
97 transaction.commit()?;
98 Ok(removed)
99 }
100
101 pub fn remove_channels_except(
102 &self,
103 team_id: &str,
104 kept_channel_ids: &[String],
105 ) -> Result<usize> {
106 let connection = self.lock()?;
107 Ok(connection.execute(
108 "DELETE FROM channels WHERE team_id = ?1 AND id NOT IN (SELECT value FROM json_each(?2))",
109 params![team_id, json_ids(kept_channel_ids)],
110 )?)
111 }
112
113 pub fn team(&self, team_id: &str) -> Result<Option<TeamRecord>> {
114 let connection = self.lock()?;
115 Ok(connection
116 .query_row(
117 "SELECT id, name FROM teams WHERE id = ?1",
118 [team_id],
119 |row| {
120 Ok(TeamRecord {
121 id: row.get(0)?,
122 name: row.get(1)?,
123 })
124 },
125 )
126 .optional()?)
127 }
128
129 pub fn channel(&self, channel_id: &str) -> Result<Option<ChannelRecord>> {
130 let connection = self.lock()?;
131 let mut statement = connection.prepare_cached(&format!(
132 "SELECT {CHANNEL_COLUMNS} FROM channels WHERE id = ?1"
133 ))?;
134 let mut channels = statement
135 .query_map([channel_id], channel_from_row)?
136 .collect::<rusqlite::Result<Vec<_>>>()?;
137 Ok(channels.pop())
138 }
139}
140
141pub(crate) fn channel_from_row(row: &Row<'_>) -> rusqlite::Result<ChannelRecord> {
142 Ok(ChannelRecord {
143 id: row.get(0)?,
144 team_id: row.get(1)?,
145 name: row.get(2)?,
146 membership_type: row.get(3)?,
147 last_message_at: optional_from_millis(row.get(4)?),
148 unread: row.get(5)?,
149 })
150}