unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at angular21 150 lines 4.6 kB view raw
1import { CommonModule } from '@angular/common' 2import { Component, OnChanges, input, inject } from '@angular/core' 3import { MatButtonModule } from '@angular/material/button' 4import { MatTooltipModule } from '@angular/material/tooltip' 5import { RouterModule } from '@angular/router' 6import { FontAwesomeModule } from '@fortawesome/angular-fontawesome' 7import { 8 faArrowUpRightFromSquare, 9 faChevronDown, 10 faClose, 11 faEnvelope, 12 faGlobe, 13 faHeart, 14 faHeartBroken, 15 faPen, 16 faQuoteLeft, 17 faRepeat, 18 faReply, 19 faServer, 20 faShareNodes, 21 faTrash, 22 faUnlock, 23 faUser, 24 faNewspaper, 25 faRobot 26} from '@fortawesome/free-solid-svg-icons' 27import { DateTime } from 'luxon' 28import { PostLinkModule } from 'src/app/directives/post-link/post-link.module' 29import { ProcessedPost } from '../../../interfaces/processed-post' 30import { LoginService } from '../../../services/login.service' 31import { MessageService } from '../../../services/message.service' 32import { PostsService } from '../../../services/posts.service' 33import { AvatarSmallComponent } from '../../avatar-small/avatar-small.component' 34import { PostActionsComponent } from '../../post-actions/post-actions.component' 35import { BlogLinkModule } from 'src/app/directives/blog-link/blog-link.module' 36import { TranslatePipe } from '@ngx-translate/core' 37import { SimpleDialogService } from 'src/app/services/simple-dialog.service' 38 39@Component({ 40 selector: 'app-post-header', 41 imports: [ 42 CommonModule, 43 RouterModule, 44 AvatarSmallComponent, 45 PostActionsComponent, 46 MatTooltipModule, 47 FontAwesomeModule, 48 MatButtonModule, 49 MatTooltipModule, 50 PostLinkModule, 51 BlogLinkModule, 52 TranslatePipe 53 ], 54 templateUrl: './post-header.component.html', 55 styleUrl: './post-header.component.scss' 56}) 57export class PostHeaderComponent implements OnChanges { 58 postService = inject(PostsService); 59 private messages = inject(MessageService); 60 private simpleDialog = inject(SimpleDialogService); 61 protected loginService = inject(LoginService); 62 63 fragment = input.required<ProcessedPost>() 64 readonly simplified = input<boolean>(true) 65 readonly disableLink = input<boolean>(false) 66 readonly headerText = input<string>('') 67 68 // table for the icons. ATTENTION, PRIVACY 10 IS SET ON CONSTRUCTOR 69 privacyOptions = [ 70 { level: 0, name: 'Public', icon: faGlobe }, 71 { level: 1, name: 'Followers only', icon: faUser }, 72 { level: 2, name: 'This instance only', icon: faServer }, 73 { level: 3, name: 'Unlisted', icon: faUnlock } 74 ] 75 76 // icons 77 shareIcon = faShareNodes 78 expandDownIcon = faChevronDown 79 solidHeartIcon = faHeart 80 clearHeartIcon = faHeartBroken 81 reblogIcon = faReply 82 quickReblogIcon = faRepeat 83 quoteIcon = faQuoteLeft 84 shareExternalIcon = faArrowUpRightFromSquare 85 deleteIcon = faTrash 86 closeIcon = faClose 87 worldIcon = faGlobe 88 unlockIcon = faUnlock 89 envelopeIcon = faEnvelope 90 serverIcon = faServer 91 userIcon = faUser 92 botIcon = faRobot 93 editedIcon = faPen 94 edited = false 95 96 timeAgo = '' 97 98 constructor() { 99 // its an array 100 this.privacyOptions[10] = { level: 10, name: 'Direct Message', icon: faEnvelope } 101 this.privacyOptions[20] = { level: 20, name: 'Link only', icon: faNewspaper } 102 } 103 ngOnChanges(): void { 104 const relative = DateTime.fromJSDate(this.fragment().createdAt).setLocale('en').toRelative() 105 this.timeAgo = relative ? relative : 'Error with date' 106 this.edited = this.fragment().updatedAt.getTime() - this.fragment().createdAt.getTime() > 6000 107 } 108 109 async followUser(post: ProcessedPost) { 110 const confirm = await this.simpleDialog.createConfirmDialog({ 111 title: 'dialog.post-header.followTitle', 112 titleSuffix: post.user.url 113 }) 114 115 if (!confirm) return 116 117 const response = await this.postService.followUser(post.userId) 118 if (response) { 119 this.messages.add({ 120 severity: 'success', 121 summary: 'messages.followMessageSuccess', 122 translate: true, 123 soundName: 'follow' 124 }) 125 } else { 126 this.messages.add({ 127 severity: 'error', 128 summary: 'messages.genericError', 129 translate: true 130 }) 131 } 132 } 133 134 async cancelFollowUser(post: ProcessedPost) { 135 const response = await this.postService.unfollowUser(post.userId) 136 if (response) { 137 this.messages.add({ 138 severity: 'success', 139 summary: 'messages.cancelFollowMessageSuccess', 140 translate: true 141 }) 142 } else { 143 this.messages.add({ 144 severity: 'error', 145 summary: 'messages.genericError', 146 translate: true 147 }) 148 } 149 } 150}