-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathactionbar.rs
128 lines (114 loc) · 3.51 KB
/
actionbar.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{
column::Columns,
route::{Route, Router},
timeline::{TimelineCache, TimelineKind},
};
use enostr::{NoteId, RelayPool};
use nostrdb::{Ndb, NoteKey, Transaction};
use notedeck::{NoteCache, UnknownIds};
use tracing::error;
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum NoteAction {
Reply(NoteId),
Quote(NoteId),
OpenTimeline(TimelineKind),
}
pub struct NewNotes {
pub id: TimelineKind,
pub notes: Vec<NoteKey>,
}
pub enum TimelineOpenResult {
NewNotes(NewNotes),
}
impl NoteAction {
#[allow(clippy::too_many_arguments)]
pub fn execute(
&self,
ndb: &Ndb,
router: &mut Router<Route>,
timeline_cache: &mut TimelineCache,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
) -> Option<TimelineOpenResult> {
match self {
NoteAction::Reply(note_id) => {
router.route_to(Route::reply(*note_id));
None
}
NoteAction::OpenTimeline(kind) => {
router.route_to(Route::Timeline(kind.to_owned()));
timeline_cache.open(ndb, note_cache, txn, pool, kind)
}
NoteAction::Quote(note_id) => {
router.route_to(Route::quote(*note_id));
None
}
}
}
/// Execute the NoteAction and process the TimelineOpenResult
#[allow(clippy::too_many_arguments)]
pub fn execute_and_process_result(
&self,
ndb: &Ndb,
columns: &mut Columns,
col: usize,
timeline_cache: &mut TimelineCache,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
unknown_ids: &mut UnknownIds,
) {
let router = columns.column_mut(col).router_mut();
if let Some(br) = self.execute(ndb, router, timeline_cache, note_cache, pool, txn) {
br.process(ndb, note_cache, txn, timeline_cache, unknown_ids);
}
}
}
impl TimelineOpenResult {
pub fn new_notes(notes: Vec<NoteKey>, id: TimelineKind) -> Self {
Self::NewNotes(NewNotes::new(notes, id))
}
pub fn process(
&self,
ndb: &Ndb,
note_cache: &mut NoteCache,
txn: &Transaction,
storage: &mut TimelineCache,
unknown_ids: &mut UnknownIds,
) {
match self {
// update the thread for next render if we have new notes
TimelineOpenResult::NewNotes(new_notes) => {
new_notes.process(storage, ndb, txn, unknown_ids, note_cache);
}
}
}
}
impl NewNotes {
pub fn new(notes: Vec<NoteKey>, id: TimelineKind) -> Self {
NewNotes { notes, id }
}
/// Simple helper for processing a NewThreadNotes result. It simply
/// inserts/merges the notes into the corresponding timeline cache
pub fn process(
&self,
timeline_cache: &mut TimelineCache,
ndb: &Ndb,
txn: &Transaction,
unknown_ids: &mut UnknownIds,
note_cache: &mut NoteCache,
) {
let reversed = matches!(&self.id, TimelineKind::Thread(_));
let timeline = if let Some(profile) = timeline_cache.timelines.get_mut(&self.id) {
profile
} else {
error!("NewNotes: could not get timeline for key {}", self.id);
return;
};
if let Err(err) = timeline.insert(&self.notes, ndb, txn, unknown_ids, note_cache, reversed)
{
error!("error inserting notes into profile timeline: {err}")
}
}
}