From 45fff1b064b766617167712e2990db62685f7e8d Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sat, 13 Apr 2019 13:47:20 -0400 Subject: [PATCH] Candid implementation of #84 --- .../create-status/create-status.component.ts | 3 ++ .../stream/thread/thread.component.ts | 44 ++++++++++++++++--- src/app/services/notification.service.ts | 15 +++++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index 6390180d..cab9d3a2 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -238,6 +238,9 @@ export class CreateStatusComponent implements OnInit, OnDestroy { return this.sendStatus(acc, this.status, visibility, this.title, status, mediaAttachments); }) .then((res: Status) => { + if (this.statusReplyingToWrapper) { + this.notificationService.newStatusPosted(this.statusReplyingToWrapper.status.id, new StatusWrapper(res, acc)); + } this.title = ''; this.status = ''; this.onClose.emit(); diff --git a/src/app/components/stream/thread/thread.component.ts b/src/app/components/stream/thread/thread.component.ts index 0e7a794f..b09bedc4 100644 --- a/src/app/components/stream/thread/thread.component.ts +++ b/src/app/components/stream/thread/thread.component.ts @@ -1,10 +1,11 @@ -import { Component, OnInit, Input, Output, EventEmitter, ViewChildren, QueryList } from '@angular/core'; +import { Component, OnInit, OnDestroy, Input, Output, EventEmitter, ViewChildren, QueryList } from '@angular/core'; import { HttpErrorResponse } from '@angular/common/http'; +import { Subscription } from 'rxjs'; import { MastodonService } from '../../../services/mastodon.service'; import { ToolsService, OpenThreadEvent } from '../../../services/tools.service'; import { Results, Context, Status } from '../../../services/models/mastodon.interfaces'; -import { NotificationService } from '../../../services/notification.service'; +import { NotificationService, NewReplyData } from '../../../services/notification.service'; import { AccountInfo } from '../../../states/accounts.state'; import { StatusWrapper } from '../../../models/common.model'; import { StatusComponent } from '../status/status.component'; @@ -14,10 +15,10 @@ import { StatusComponent } from '../status/status.component'; templateUrl: '../stream-statuses/stream-statuses.component.html', styleUrls: ['../stream-statuses/stream-statuses.component.scss'] }) -export class ThreadComponent implements OnInit { +export class ThreadComponent implements OnInit, OnDestroy { statuses: StatusWrapper[] = []; displayError: string; - isLoading = true; + isLoading = true; isThread = true; hasContentWarnings = false; @@ -37,12 +38,43 @@ export class ThreadComponent implements OnInit { @ViewChildren(StatusComponent) statusChildren: QueryList; + private newPostSub: Subscription; + constructor( private readonly notificationService: NotificationService, private readonly toolsService: ToolsService, private readonly mastodonService: MastodonService) { } ngOnInit() { + this.newPostSub = this.notificationService.newRespondPostedStream.subscribe((replyData: NewReplyData) => { + if(replyData){ + const repondingStatus = this.statuses.find(x => x.status.id === replyData.uiStatusId); + const responseStatus = replyData.response; + if(repondingStatus && this.statuses[0]){ + this.statuses.push(responseStatus); + + // const uiProvider = this.statuses[0].provider; + // if(uiProvider.id === responseStatus.provider.id){ + + // } else { + // this.toolsService.getStatusUsableByAccount(uiProvider, responseStatus) + // .then((status: Status) => { + // this.statuses.push(new StatusWrapper(status, uiProvider)); + // }) + // .catch((err) => { + // this.notificationService.notifyHttpError(err); + // }); + // } + // this.getThread(this.statuses[0].provider, this.lastThreadEvent); + } + } + }); + } + + ngOnDestroy(): void { + if (this.newPostSub) { + this.newPostSub.unsubscribe(); + } } private getThread(openThreadEvent: OpenThreadEvent) { @@ -93,7 +125,7 @@ export class ThreadComponent implements OnInit { this.hasContentWarnings = this.statuses.filter(x => x.status.sensitive || x.status.spoiler_text).length > 1; }); - + }) .catch((err: HttpErrorResponse) => { this.notificationService.notifyHttpError(err); @@ -126,7 +158,7 @@ export class ThreadComponent implements OnInit { this.browseThreadEvent.next(openThreadEvent); } - removeCw(){ + removeCw() { const statuses = this.statusChildren.toArray(); statuses.forEach(x => { x.removeContentWarning(); diff --git a/src/app/services/notification.service.ts b/src/app/services/notification.service.ts index 3d85645d..d44ad91a 100644 --- a/src/app/services/notification.service.ts +++ b/src/app/services/notification.service.ts @@ -1,10 +1,13 @@ import { Injectable } from '@angular/core'; import { Subject } from 'rxjs'; import { HttpErrorResponse } from '@angular/common/http'; +import { StatusWrapper } from '../models/common.model'; +import { Status } from './models/mastodon.interfaces'; @Injectable() export class NotificationService { public notifactionStream = new Subject(); + public newRespondPostedStream = new Subject(); constructor() { } @@ -22,6 +25,12 @@ export class NotificationService { } catch(err){} this.notify(message, true); } + + // public newStatusPosted(status: StatusWrapper){ + public newStatusPosted(uiStatusRepliedToId: string, response: StatusWrapper){ + const notification = new NewReplyData(uiStatusRepliedToId, response); + this.newRespondPostedStream.next(notification); + } } export class NotificatioData { @@ -34,3 +43,9 @@ export class NotificatioData { this.id = `${message}${new Date().getTime()}`; } } + +export class NewReplyData { + constructor(public uiStatusId: string, public response: StatusWrapper){ + + } +}