unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Component, inject, Input, OnChanges, SimpleChanges } from '@angular/core'
2import { EnvironmentService } from 'src/app/services/environment.service'
3import { MediaService } from 'src/app/services/media.service'
4import { MatCardModule } from '@angular/material/card'
5import { CommonModule } from '@angular/common'
6@Component({
7 selector: 'app-link-preview',
8 imports: [CommonModule, MatCardModule],
9 templateUrl: './link-preview.component.html',
10 styleUrl: './link-preview.component.scss'
11})
12export class LinkPreviewComponent implements OnChanges {
13 private mediaService = inject(MediaService)
14
15 @Input() link: string = ''
16
17 loading = true
18 url = ''
19 hostname = ''
20 title = ''
21 description = ''
22 img = ''
23 favicon: string | undefined = ''
24 forceTenorGif = false
25 forceYoutube = false
26
27 ngOnChanges(changes: SimpleChanges): void {
28 this.forceTenorGif = false
29 this.forceYoutube = false
30 if (this.link) {
31 if (this.url.startsWith('https://media.tenor.com/')) {
32 this.loading = false
33 this.forceTenorGif = true
34 return
35 }
36 this.loading = true
37 const linkToGet = this.link.startsWith(EnvironmentService.environment.externalCacheurl)
38 this.url = linkToGet
39 ? (new URL(this.link, EnvironmentService.environment.frontUrl).searchParams.get('media') as string)
40 : this.link
41 this.hostname = new URL(this.url).hostname
42 this.mediaService.getLinkPreview(this.url).then((data) => {
43 this.favicon = EnvironmentService.environment.externalCacheurl +
44 encodeURIComponent(data.favicons.at(0))
45 this.loading = false
46 if (data.images && data.images.length) {
47 this.img = EnvironmentService.environment.externalCacheurl + encodeURIComponent(data.images[0])
48 }
49 if (!this.img && data.favicons && data.favicons.length) {
50 this.img =
51 this.favicon
52 }
53 let sitenamePrefix = ''
54 if (data.siteName) {
55 sitenamePrefix = data.siteName + ' - '
56 }
57 if (data.title) {
58 this.title = sitenamePrefix + data.title
59 }
60 if (data.description) {
61 this.description = data.description
62 }
63 })
64 }
65 }
66}