forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

shared.rs167 lines, 6.4 KiBHistoryRawDownload
1//! Items shared with the viewer (`GET /shared`) and the stream that refreshes it (`GET /live/shared`).
2//!
3//! The page lists the top-level grants only: an item that lies under a folder shared with the viewer is
4//! reached through that folder, not listed twice. Trashed items and items under a trashed folder are left
5//! out by the store. Folders open in the drive (`/drive/{id}`), files in the viewer (`/view/{id}`), and the
6//! download link appears only with a download grant. The owner's name is shown; no owner's folder is.
7
8use in_core::store::shares::SharedItem;
9use topcoat::Result;
10use topcoat::context::{Cx, app_context};
11use topcoat::router::error::not_found;
12use topcoat::router::response::Response;
13use topcoat::router::{page, route};
14use topcoat::view::{View, component, view};
15
16use crate::auth::require_member;
17use crate::i18n::Lang;
18use crate::i18n::drive::{DK, d};
19use crate::i18n::share::{SK, s as share_s};
20use crate::live::{Topic, stream_changes};
21use crate::pages::drive::viewer_lang;
22use crate::state::AppState;
23use crate::views::drive::size_text;
24use crate::views::{ViewerInfo, empty_state, shell};
25
26/// The fingerprint of the shared page: every row's grant, name, level, size and owner. Equal text means the
27/// page would look the same, so the stream stays quiet.
28fn fingerprint(items: &[SharedItem]) -> String {
29 let mut text = String::new();
30 for item in items {
31 text.push_str(&format!(
32 "{}|{}|{}|{}|{}|{}\n",
33 item.share.id,
34 item.name,
35 item.share.can_download,
36 item.size,
37 item.owner_name,
38 item.share.created_at
39 ));
40 }
41 text
42}
43
44/// The shared page's content: a table of the items, folders first, each group by name.
45#[component]
46pub async fn shared_body(lang: Lang, items: &[SharedItem]) -> Result<impl View> {
47 let mut sorted: Vec<&SharedItem> = items.iter().collect();
48 sorted.sort_by(|a, b| {
49 let a_folder = a.share.kind == in_core::store::trash::ItemKind::Folder;
50 let b_folder = b.share.kind == in_core::store::trash::ItemKind::Folder;
51 b_folder
52 .cmp(&a_folder)
53 .then_with(|| a.name.cmp(&b.name))
54 .then_with(|| a.share.id.cmp(&b.share.id))
55 });
56 Ok(view! {
57 <section class="drive" data-live="/live/shared">
58 <h1>(share_s(lang, SK::SharedTitle))</h1>
59 if sorted.is_empty() {
60 empty_state(title: share_s(lang, SK::SharedEmpty), <p>(share_s(lang, SK::SharedEmptyHint))</p>)
61 } else {
62 <div class="table-wrap">
63 <table class="table">
64 <thead>
65 <tr>
66 <th scope="col">(d(lang, DK::Name))</th>
67 <th scope="col">(share_s(lang, SK::SharePerson))</th>
68 <th scope="col">(share_s(lang, SK::ShareLevel))</th>
69 <th scope="col">(d(lang, DK::Size))</th>
70 <th scope="col">(d(lang, DK::Actions))</th>
71 </tr>
72 </thead>
73 <tbody>
74 for item in sorted {
75 <tr>
76 <td>
77 if item.share.kind == in_core::store::trash::ItemKind::Folder {
78 <a href=(format!("/drive/{}", item.share.target_id))>(item.name.as_str())</a>
79 } else {
80 <a href=(format!("/view/{}", item.share.target_id))>(item.name.as_str())</a>
81 }
82 </td>
83 <td class="muted">(item.owner_name.as_str())</td>
84 <td>
85 if item.share.can_download {
86 (share_s(lang, SK::AccessDownload))
87 } else {
88 (share_s(lang, SK::AccessView))
89 }
90 </td>
91 <td>
92 if item.share.kind == in_core::store::trash::ItemKind::File {
93 (size_text(item.size))
94 } else {
95 "\u{2014}"
96 }
97 </td>
98 <td>
99 if item.share.kind == in_core::store::trash::ItemKind::File && item.share.can_download {
100 <a class="button ghost" href=(format!("/file/{}?dl=1", item.share.target_id))>(d(lang, DK::Download))</a>
101 }
102 </td>
103 </tr>
104 }
105 </tbody>
106 </table>
107 </div>
108 }
109 </section>
110 })
111}
112
113/// `GET /shared`: the items shared with the signed-in person.
114#[page("/shared")]
115async fn shared_page(cx: &Cx) -> Result<impl View> {
116 let state = app_context::<AppState>(cx);
117 let viewer = require_member(cx).await?;
118 let lang = viewer_lang(cx, &viewer);
119 let items = state.store.shared_with(&viewer.member.sub).await?;
120 let info = ViewerInfo::of(&viewer, state.family.list());
121 let title = share_s(lang, SK::SharedTitle);
122 Ok(view! {
123 shell(title: title, lang: lang, viewer: Some(&info),
124 shared_body(lang: lang, items: &items)
125 )
126 })
127}
128
129/// `GET /live/shared`: a data-less `changed` event whenever the viewer's shared items change. It ends when
130/// the session is gone, and when a share the page showed when the stream opened is revoked (or its item is
131/// trashed): the viewer no longer has that grant, so the page must not stay open on it.
132#[route(GET "/live/shared")]
133async fn shared_live(cx: &Cx) -> Result<Response> {
134 let state = app_context::<AppState>(cx);
135 let viewer = require_member(cx).await?;
136 let me = viewer.member.sub.clone();
137 let token_hash = viewer.session.token_hash.clone();
138 let store = state.store.clone();
139 // The grants the page shows now. A grant that later leaves the list has been revoked.
140 let shown: Vec<String> = store
141 .shared_with(&me)
142 .await?
143 .into_iter()
144 .map(|item| item.share.id)
145 .collect();
146 let topics = vec![Topic::Shared(me.clone())];
147 stream_changes(cx, &state.live, &topics, move || {
148 let store = store.clone();
149 let me = me.clone();
150 let token_hash = token_hash.clone();
151 let shown = shown.clone();
152 async move {
153 if store.session_by_token_hash(&token_hash).await?.is_none() {
154 return Err(not_found().into());
155 }
156 let items = store.shared_with(&me).await?;
157 let revoked = shown
158 .iter()
159 .any(|id| !items.iter().any(|item| &item.share.id == id));
160 if revoked {
161 return Err(not_found().into());
162 }
163 Ok(fingerprint(&items))
164 }
165 })
166 .await
167}