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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! MCP handlers for Reaction-related operations
use crate::core::types::Fid;
use crate::services::mcp::base::WaypointMcpService;
use prost::Message as ProstMessage;
// Common types are used in the handler implementations
impl<DB, HC> WaypointMcpService<DB, HC>
where
DB: crate::core::data_context::Database + Clone + Send + Sync + 'static,
HC: crate::core::data_context::HubClient + Clone + Send + Sync + 'static,
{
/// Get a specific reaction
pub async fn do_get_reaction(
&self,
fid: Fid,
reaction_type: u8,
target_cast_fid: Option<Fid>,
target_cast_hash: Option<&[u8]>,
target_url: Option<&str>,
) -> String {
tracing::info!("MCP: Fetching reaction with FID: {} and type: {}", fid, reaction_type);
// Use the data context to fetch the reaction
match self
.data_context
.get_reaction(fid, reaction_type, target_cast_fid, target_cast_hash, target_url)
.await
{
Ok(Some(message)) => {
// Try to decode the message payload as MessageData
if let Ok(data) = ProstMessage::decode(&*message.payload) {
let msg_data: crate::proto::MessageData = data;
// Process the reaction
if let Some(reaction_obj) =
super::utils::process_reaction_message(&message, &msg_data)
{
// Convert to JSON string
return serde_json::to_string_pretty(&reaction_obj).unwrap_or_else(|_| {
format!("Error formatting reaction for FID {}", fid)
});
}
}
format!("Reaction found but could not be processed for FID {}", fid)
},
Ok(None) => format!("No reaction found for FID {} with the specified parameters", fid),
Err(e) => format!("Error fetching reaction: {}", e),
}
}
/// Get reactions by FID
pub async fn do_get_reactions_by_fid(
&self,
fid: Fid,
reaction_type: Option<u8>,
limit: usize,
) -> String {
tracing::info!("MCP: Fetching reactions for FID: {}", fid);
// Use the data context to fetch reactions
match self.data_context.get_reactions_by_fid(fid, reaction_type, limit).await {
Ok(messages) => super::utils::format_reactions_response(messages, Some(fid)),
Err(e) => format!("Error fetching reactions: {}", e),
}
}
/// Get reactions by target (cast or URL)
pub async fn do_get_reactions_by_target(
&self,
target_cast_fid: Option<Fid>,
target_cast_hash: Option<&[u8]>,
target_url: Option<&str>,
reaction_type: Option<u8>,
limit: usize,
) -> String {
tracing::info!("MCP: Fetching reactions by target");
// Use the data context to fetch reactions
match self
.data_context
.get_reactions_by_target(
target_cast_fid,
target_cast_hash,
target_url,
reaction_type,
limit,
)
.await
{
Ok(messages) => {
// Format response based on target type
let result = if messages.is_empty() {
if let Some(target_url) = target_url {
serde_json::json!({
"target_url": target_url,
"count": 0,
"reactions": []
})
} else if let (Some(tfid), Some(thash)) = (target_cast_fid, target_cast_hash) {
serde_json::json!({
"target_cast": {
"fid": tfid.value(),
"hash": hex::encode(thash)
},
"count": 0,
"reactions": []
})
} else {
serde_json::json!({
"count": 0,
"reactions": []
})
}
} else {
// Process messages into reaction objects
let reactions: Vec<serde_json::Value> = messages
.iter()
.filter_map(|message| {
if let Ok(data) = ProstMessage::decode(&*message.payload) {
let msg_data: crate::proto::MessageData = data;
if let Some(reaction_obj) =
super::utils::process_reaction_message(message, &msg_data)
{
return Some(serde_json::Value::Object(reaction_obj));
}
}
None
})
.collect();
if let Some(target_url) = target_url {
serde_json::json!({
"target_url": target_url,
"count": reactions.len(),
"reactions": reactions
})
} else if let (Some(tfid), Some(thash)) = (target_cast_fid, target_cast_hash) {
serde_json::json!({
"target_cast": {
"fid": tfid.value(),
"hash": hex::encode(thash)
},
"count": reactions.len(),
"reactions": reactions
})
} else {
serde_json::json!({
"count": reactions.len(),
"reactions": reactions
})
}
};
// Convert to JSON string
serde_json::to_string_pretty(&result)
.unwrap_or_else(|_| "Error formatting reactions".to_string())
},
Err(e) => format!("Error fetching reactions by target: {}", e),
}
}
/// Get all reactions by FID with timestamp filtering
pub async fn do_get_all_reactions_by_fid(
&self,
fid: Fid,
limit: usize,
start_time: Option<u64>,
end_time: Option<u64>,
) -> String {
tracing::info!("MCP: Fetching all reactions for FID: {} with time filtering", fid);
// Use the data context to fetch reactions with time filtering
match self.data_context.get_all_reactions_by_fid(fid, limit, start_time, end_time).await {
Ok(messages) => {
// Format the basic response
let base_response = super::utils::format_reactions_response(messages, Some(fid));
// If there are no reactions, return a time-specific message
if base_response.starts_with("No reactions found") {
let time_range = match (start_time, end_time) {
(Some(start), Some(end)) => {
format!(" between timestamps {} and {}", start, end)
},
(Some(start), None) => format!(" after timestamp {}", start),
(None, Some(end)) => format!(" before timestamp {}", end),
(None, None) => "".to_string(),
};
return format!("No reactions found for FID {}{}", fid, time_range);
}
base_response
},
Err(e) => format!("Error fetching reactions with time filtering: {}", e),
}
}
}