From 81ff215840ad47c747f02f019f7e0f4115d023d1 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Mon, 11 Mar 2019 00:31:56 -0400 Subject: [PATCH] fix #54 --- .../create-status/create-status.component.ts | 54 +++++++++++++------ src/app/services/instances-info.service.ts | 43 ++++++++++++--- .../services/models/mastodon.interfaces.ts | 8 +++ 3 files changed, 82 insertions(+), 23 deletions(-) diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index 538f6d68..b94f3d41 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -22,9 +22,9 @@ export class CreateStatusComponent implements OnInit, OnDestroy { title: string; private _status: string = ''; - set status(value: string){ + set status(value: string) { this.countStatusChar(value); - this._status = value; + this._status = value; } get status(): string { return this._status; @@ -54,9 +54,9 @@ export class CreateStatusComponent implements OnInit, OnDestroy { private readonly toolsService: ToolsService, private readonly mastodonService: MastodonService, private readonly instancesInfoService: InstancesInfoService, - private readonly mediaService: MediaService) { - this.accounts$ = this.store.select(state => state.registeredaccounts.accounts); - } + private readonly mediaService: MediaService) { + this.accounts$ = this.store.select(state => state.registeredaccounts.accounts); + } ngOnInit() { this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => { @@ -83,7 +83,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { }, 0); } - ngOnDestroy(){ + ngOnDestroy() { this.accountSub.unsubscribe(); } @@ -93,10 +93,34 @@ export class CreateStatusComponent implements OnInit, OnDestroy { .then((maxChars: number) => { this.maxCharLength = maxChars; this.countStatusChar(this.status); + }) + .catch((err: HttpErrorResponse) => { + this.notificationService.notifyHttpError(err); + }); + + this.instancesInfoService.getDefaultPrivacy(selectedAccount) + .then((defaultPrivacy: VisibilityEnum) => { + switch (defaultPrivacy) { + case VisibilityEnum.Public: + this.selectedPrivacy = 'Public'; + break; + case VisibilityEnum.Unlisted: + this.selectedPrivacy = 'Unlisted'; + break; + case VisibilityEnum.Private: + this.selectedPrivacy = 'Follows-only'; + break; + case VisibilityEnum.Direct: + this.selectedPrivacy = 'DM'; + break; + } + }) + .catch((err: HttpErrorResponse) => { + this.notificationService.notifyHttpError(err); }); } - private countStatusChar(status: string){ + private countStatusChar(status: string) { const parseStatus = this.parseStatus(status); const currentStatus = parseStatus[parseStatus.length - 1]; @@ -125,7 +149,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { return globalUniqueMentions; } - + onCtrlEnter(): boolean { this.onSubmit(); return false; @@ -166,7 +190,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { .then((status: Status) => { return this.sendStatus(acc, this.status, visibility, this.title, status, mediaAttachments); }) - .then((res: Status) => { + .then((res: Status) => { this.title = ''; this.status = ''; this.onClose.emit(); @@ -185,7 +209,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { let parsedStatus = this.parseStatus(status); let resultPromise = Promise.resolve(previousStatus); - for(let i = 0; i < parsedStatus.length; i++){ + for (let i = 0; i < parsedStatus.length; i++) { let s = parsedStatus[i]; resultPromise = resultPromise.then((pStatus: Status) => { let inReplyToId = null; @@ -193,14 +217,14 @@ export class CreateStatusComponent implements OnInit, OnDestroy { inReplyToId = pStatus.id; } - if(i === 0){ + if (i === 0) { return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, attachments.map(x => x.id)) - .then((status:Status) => { + .then((status: Status) => { this.mediaService.clearMedia(); return status; - }); + }); } else { - return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, []); + return this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, []); } }); } @@ -212,7 +236,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { let trucatedStatus = `${status}`; let results = []; const maxChars = this.maxCharLength - 6; - while(trucatedStatus.length > this.maxCharLength){ + while (trucatedStatus.length > this.maxCharLength) { const nextIndex = trucatedStatus.lastIndexOf(' ', maxChars); results.push(trucatedStatus.substr(0, nextIndex) + ' (...)'); trucatedStatus = trucatedStatus.substr(nextIndex + 1); diff --git a/src/app/services/instances-info.service.ts b/src/app/services/instances-info.service.ts index c59331d3..f024438d 100644 --- a/src/app/services/instances-info.service.ts +++ b/src/app/services/instances-info.service.ts @@ -1,22 +1,24 @@ import { Injectable } from '@angular/core'; -import { MastodonService } from './mastodon.service'; -import { Instance } from './models/mastodon.interfaces'; +import { MastodonService, VisibilityEnum } from './mastodon.service'; +import { Instance, Account } from './models/mastodon.interfaces'; +import { AccountInfo } from '../states/accounts.state'; @Injectable({ providedIn: 'root' }) export class InstancesInfoService { - private defaultMaxChars = 500; - private cachedMaxInstanceChar: { [id: string] : Promise; } = {}; + private defaultMaxChars = 500; + private cachedMaxInstanceChar: { [id: string]: Promise; } = {}; + private cachedDefaultPrivacy: { [id: string]: Promise; } = {}; constructor(private mastodonService: MastodonService) { } - getMaxStatusChars(instance:string): Promise { - if(!this.cachedMaxInstanceChar[instance]){ + getMaxStatusChars(instance: string): Promise { + if (!this.cachedMaxInstanceChar[instance]) { this.cachedMaxInstanceChar[instance] = this.mastodonService.getInstance(instance) - .then((instance: Instance)=>{ - if(instance.max_toot_chars){ + .then((instance: Instance) => { + if (instance.max_toot_chars) { return instance.max_toot_chars; } else { return this.defaultMaxChars; @@ -28,4 +30,29 @@ export class InstancesInfoService { } return this.cachedMaxInstanceChar[instance]; } + + getDefaultPrivacy(account: AccountInfo): Promise { + const instance = account.instance; + if (!this.cachedDefaultPrivacy[instance]) { + this.cachedDefaultPrivacy[instance] = this.mastodonService.retrieveAccountDetails(account) + .then((accountDetails: Account) => { + switch (accountDetails.source.privacy) { + case 'public': + return VisibilityEnum.Public; + case 'unlisted': + return VisibilityEnum.Unlisted; + case 'private': + return VisibilityEnum.Private; + case 'direct': + return VisibilityEnum.Direct; + default: + return VisibilityEnum.Public; + } + }) + .catch(() => { + return VisibilityEnum.Public; + }); + } + return this.cachedDefaultPrivacy[instance]; + } } diff --git a/src/app/services/models/mastodon.interfaces.ts b/src/app/services/models/mastodon.interfaces.ts index d439a7c9..fdcbcfec 100644 --- a/src/app/services/models/mastodon.interfaces.ts +++ b/src/app/services/models/mastodon.interfaces.ts @@ -34,6 +34,14 @@ export interface Account { moved: boolean; fields: Field[]; bot: boolean; + source: AccountInfo; +} + +export interface AccountInfo { + privacy: string; + sensitive: boolean; + note: string; + fields: Field[]; } export interface Emoji {