2019-03-06 04:46:50 +01:00
|
|
|
import { Component, OnInit, Input, Output, EventEmitter, ElementRef, ViewChild } from '@angular/core';
|
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
|
|
|
|
import { MastodonService, VisibilityEnum } from '../../services/mastodon.service';
|
|
|
|
import { Status } from '../../services/models/mastodon.interfaces';
|
|
|
|
import { ToolsService } from '../../services/tools.service';
|
|
|
|
import { NotificationService } from '../../services/notification.service';
|
|
|
|
import { StatusWrapper } from '../../models/common.model';
|
2019-03-06 05:02:11 +01:00
|
|
|
import { AccountInfo } from '../../states/accounts.state';
|
2019-03-06 04:46:50 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-create-status',
|
|
|
|
templateUrl: './create-status.component.html',
|
|
|
|
styleUrls: ['./create-status.component.scss']
|
|
|
|
})
|
|
|
|
export class CreateStatusComponent implements OnInit {
|
|
|
|
title: string;
|
|
|
|
status: string = '';
|
|
|
|
|
2019-03-06 05:37:58 +01:00
|
|
|
isSending: boolean;
|
|
|
|
|
2019-03-06 04:46:50 +01:00
|
|
|
@Input() statusReplyingToWrapper: StatusWrapper;
|
|
|
|
@Output() onClose = new EventEmitter();
|
|
|
|
@ViewChild('reply') replyElement: ElementRef;
|
|
|
|
|
|
|
|
private statusReplyingTo: Status;
|
|
|
|
|
|
|
|
selectedPrivacy = 'Public';
|
|
|
|
privacyList: string[] = ['Public', 'Unlisted', 'Follows-only', 'DM'];
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
// private readonly store: Store,
|
|
|
|
private readonly notificationService: NotificationService,
|
|
|
|
private readonly toolsService: ToolsService,
|
|
|
|
private readonly mastodonService: MastodonService) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (this.statusReplyingToWrapper) {
|
|
|
|
if (this.statusReplyingToWrapper.status.reblog) {
|
|
|
|
this.statusReplyingTo = this.statusReplyingToWrapper.status.reblog;
|
|
|
|
} else {
|
|
|
|
this.statusReplyingTo = this.statusReplyingToWrapper.status;
|
|
|
|
}
|
2019-03-06 06:02:16 +01:00
|
|
|
|
2019-03-06 05:00:06 +01:00
|
|
|
const uniqueMentions = this.getMentions(this.statusReplyingTo, this.statusReplyingToWrapper.provider);
|
2019-03-06 04:46:50 +01:00
|
|
|
for (const mention of uniqueMentions) {
|
|
|
|
this.status += `@${mention} `;
|
|
|
|
}
|
2019-03-06 06:02:16 +01:00
|
|
|
|
|
|
|
this.title = this.statusReplyingTo.spoiler_text;
|
2019-03-06 04:46:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.replyElement.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2019-03-06 06:02:16 +01:00
|
|
|
private getMentions(status: Status, providerInfo: AccountInfo): string[] {
|
2019-03-06 04:46:50 +01:00
|
|
|
const mentions = [...status.mentions.map(x => x.acct), status.account.acct];
|
2019-03-06 05:00:06 +01:00
|
|
|
|
2019-03-06 04:46:50 +01:00
|
|
|
let uniqueMentions = [];
|
2019-03-06 06:02:16 +01:00
|
|
|
for (let mention of mentions) {
|
|
|
|
if (!uniqueMentions.includes(mention)) {
|
2019-03-06 04:46:50 +01:00
|
|
|
uniqueMentions.push(mention);
|
|
|
|
}
|
2019-03-06 05:00:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let globalUniqueMentions = [];
|
2019-03-06 06:02:16 +01:00
|
|
|
for (let mention of uniqueMentions) {
|
|
|
|
if (!mention.includes('@')) {
|
2019-03-06 05:00:06 +01:00
|
|
|
mention += `@${providerInfo.instance}`;
|
|
|
|
}
|
|
|
|
globalUniqueMentions.push(mention);
|
|
|
|
}
|
|
|
|
|
|
|
|
return globalUniqueMentions;
|
2019-03-06 04:46:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit(): boolean {
|
2019-03-06 06:02:16 +01:00
|
|
|
if (this.isSending) return false;
|
2019-03-06 05:37:58 +01:00
|
|
|
|
|
|
|
this.isSending = true;
|
|
|
|
|
2019-03-06 04:46:50 +01:00
|
|
|
let visibility: VisibilityEnum = VisibilityEnum.Unknown;
|
|
|
|
switch (this.selectedPrivacy) { //FIXME: in case of responding, set the visibility to original
|
|
|
|
case 'Public':
|
|
|
|
visibility = VisibilityEnum.Public;
|
|
|
|
break;
|
|
|
|
case 'Unlisted':
|
|
|
|
visibility = VisibilityEnum.Unlisted;
|
|
|
|
break;
|
|
|
|
case 'Follows-only':
|
|
|
|
visibility = VisibilityEnum.Private;
|
|
|
|
break;
|
|
|
|
case 'DM':
|
|
|
|
visibility = VisibilityEnum.Direct;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let spoiler = this.title;
|
2019-03-06 06:02:16 +01:00
|
|
|
|
2019-03-06 04:46:50 +01:00
|
|
|
|
|
|
|
const acc = this.toolsService.getSelectedAccounts()[0];
|
|
|
|
|
|
|
|
let usableStatus: Promise<Status>;
|
|
|
|
if (this.statusReplyingToWrapper) {
|
|
|
|
usableStatus = this.toolsService.getStatusUsableByAccount(acc, this.statusReplyingToWrapper);
|
|
|
|
} else {
|
|
|
|
usableStatus = Promise.resolve(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
usableStatus
|
|
|
|
.then((status: Status) => {
|
|
|
|
let inReplyToId = null;
|
|
|
|
if (status) {
|
|
|
|
inReplyToId = status.id;
|
|
|
|
}
|
|
|
|
return this.mastodonService.postNewStatus(acc, this.status, visibility, spoiler, inReplyToId);
|
|
|
|
})
|
|
|
|
.then((res: Status) => {
|
|
|
|
this.title = '';
|
|
|
|
this.status = '';
|
|
|
|
this.onClose.emit();
|
|
|
|
})
|
|
|
|
.catch((err: HttpErrorResponse) => {
|
|
|
|
this.notificationService.notifyHttpError(err);
|
2019-03-06 05:37:58 +01:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.isSending = false;
|
2019-03-06 04:46:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// private getRegisteredAccounts(): AccountInfo[] {
|
|
|
|
// var regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
|
|
|
|
// return regAccounts;
|
|
|
|
// }
|
|
|
|
|
|
|
|
onCtrlEnter(): boolean {
|
|
|
|
this.onSubmit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|