BlueGit

ict rusty-teams

Files

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

teams.rs

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