| 334 | } |
| 335 | |
| 336 | fn make_link( |
| 337 | &self, |
| 338 | mutation: &Mutation, |
| 339 | link_id: &ExternalId, |
| 340 | url: &RepoUrl, |
| 341 | ) -> Result<(RepoLink, Option<String>)> { |
| 342 | if mutation.exists(self.repo_id, link_id)? { |
| 343 | if let Some(link) = mutation.fetch_link(self.repo_id, link_id) { |
| 344 | let title = link.metadata.title().to_owned(); |
| 345 | return Ok((link, Some(title))); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | let title = if let Some(title) = &self.title { |
| 350 | title.clone() |
| 351 | } else { |
| 352 | match self.fetcher.fetch(url) { |
| 353 | Ok(response) => response.title().unwrap_or_else(|| "Missing title".into()), |
| 354 | Err(_) => "Failed to fetch title".into(), |
| 355 | } |
| 356 | }; |
| 357 | |
| 358 | let mut parent_topics = BTreeSet::new(); |
| 359 | if let Some(id) = &self.add_parent_topic_id { |
| 360 | parent_topics.insert(ParentTopic { id: id.to_owned() }); |
| 361 | } |
| 362 | |
| 363 | let link = RepoLink { |
| 364 | api_version: API_VERSION.into(), |
| 365 | parent_topics, |
| 366 | metadata: RepoLinkMetadata { |
| 367 | added: chrono::Utc::now(), |
| 368 | id: link_id.to_owned(), |
| 369 | details: Some(RepoLinkDetails { |
| 370 | title, |
| 371 | url: url.normalized.to_owned(), |
| 372 | }), |
| 373 | }, |
| 374 | }; |
| 375 | |
| 376 | Ok((link, None)) |
| 377 | } |
| 378 | } |