Redirect to login page for votes, comments, pages, etc. Fixes #849 (#926)

This commit is contained in:
Dessalines 2020-07-09 20:03:47 -04:00 committed by GitHub
parent 85c07e7154
commit 50e6d81d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 122 deletions

View file

@ -1,4 +1,5 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Link } from 'inferno-router';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators'; import { retryWhen, delay, take } from 'rxjs/operators';
import { Prompt } from 'inferno-router'; import { Prompt } from 'inferno-router';
@ -25,6 +26,7 @@ import autosize from 'autosize';
import Tribute from 'tributejs/src/Tribute.js'; import Tribute from 'tributejs/src/Tribute.js';
import emojiShortName from 'emoji-short-name'; import emojiShortName from 'emoji-short-name';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
import { T } from 'inferno-i18next';
interface CommentFormProps { interface CommentFormProps {
postId?: number; postId?: number;
@ -99,6 +101,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
componentDidMount() { componentDidMount() {
let textarea: any = document.getElementById(this.id); let textarea: any = document.getElementById(this.id);
if (textarea) {
autosize(textarea); autosize(textarea);
this.tribute.attach(textarea); this.tribute.attach(textarea);
textarea.addEventListener('tribute-replaced', () => { textarea.addEventListener('tribute-replaced', () => {
@ -123,6 +126,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
textarea.focus(); textarea.focus();
} }
}
componentDidUpdate() { componentDidUpdate() {
if (this.state.commentForm.content) { if (this.state.commentForm.content) {
@ -144,6 +148,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
when={this.state.commentForm.content} when={this.state.commentForm.content}
message={i18n.t('block_leaving')} message={i18n.t('block_leaving')}
/> />
{UserService.Instance.user ? (
<form <form
id={this.formId} id={this.formId}
onSubmit={linkEvent(this, this.handleCommentSubmit)} onSubmit={linkEvent(this, this.handleCommentSubmit)}
@ -152,7 +157,9 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
<div className={`col-sm-12`}> <div className={`col-sm-12`}>
<textarea <textarea
id={this.id} id={this.id}
className={`form-control ${this.state.previewMode && 'd-none'}`} className={`form-control ${
this.state.previewMode && 'd-none'
}`}
value={this.state.commentForm.content} value={this.state.commentForm.content}
onInput={linkEvent(this, this.handleCommentContentChange)} onInput={linkEvent(this, this.handleCommentContentChange)}
onPaste={linkEvent(this, this.handleImageUploadPaste)} onPaste={linkEvent(this, this.handleImageUploadPaste)}
@ -253,6 +260,16 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
</div> </div>
</div> </div>
</form> </form>
) : (
<div class="alert alert-warning" role="alert">
<svg class="icon icon-inline mr-2">
<use xlinkHref="#icon-alert-triangle"></use>
</svg>
<T i18nKey="must_login" class="d-inline">
#<Link to="/login">#</Link>
</T>
</div>
)}
</div> </div>
); );
} }

View file

@ -9,7 +9,7 @@ import {
GetSiteResponse, GetSiteResponse,
} from '../interfaces'; } from '../interfaces';
import { toast, wsJsonToRes } from '../utils'; import { toast, wsJsonToRes } from '../utils';
import { WebSocketService } from '../services'; import { WebSocketService, UserService } from '../services';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
interface CreateCommunityState { interface CreateCommunityState {
@ -26,6 +26,11 @@ export class CreateCommunity extends Component<any, CreateCommunityState> {
this.handleCommunityCreate = this.handleCommunityCreate.bind(this); this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
this.state = this.emptyState; this.state = this.emptyState;
if (!UserService.Instance.user) {
toast(i18n.t('not_logged_in'), 'danger');
this.context.router.history.push(`/login`);
}
this.subscription = WebSocketService.Instance.subject this.subscription = WebSocketService.Instance.subject
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
.subscribe( .subscribe(

View file

@ -3,7 +3,7 @@ import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators'; import { retryWhen, delay, take } from 'rxjs/operators';
import { PostForm } from './post-form'; import { PostForm } from './post-form';
import { toast, wsJsonToRes } from '../utils'; import { toast, wsJsonToRes } from '../utils';
import { WebSocketService } from '../services'; import { WebSocketService, UserService } from '../services';
import { import {
UserOperation, UserOperation,
PostFormParams, PostFormParams,
@ -41,6 +41,11 @@ export class CreatePost extends Component<any, CreatePostState> {
this.handlePostCreate = this.handlePostCreate.bind(this); this.handlePostCreate = this.handlePostCreate.bind(this);
this.state = this.emptyState; this.state = this.emptyState;
if (!UserService.Instance.user) {
toast(i18n.t('not_logged_in'), 'danger');
this.context.router.history.push(`/login`);
}
this.subscription = WebSocketService.Instance.subject this.subscription = WebSocketService.Instance.subject
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
.subscribe( .subscribe(

View file

@ -2,7 +2,7 @@ import { Component } from 'inferno';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators'; import { retryWhen, delay, take } from 'rxjs/operators';
import { PrivateMessageForm } from './private-message-form'; import { PrivateMessageForm } from './private-message-form';
import { WebSocketService } from '../services'; import { WebSocketService, UserService } from '../services';
import { import {
UserOperation, UserOperation,
WebSocketJsonResponse, WebSocketJsonResponse,
@ -20,6 +20,11 @@ export class CreatePrivateMessage extends Component<any, any> {
this this
); );
if (!UserService.Instance.user) {
toast(i18n.t('not_logged_in'), 'danger');
this.context.router.history.push(`/login`);
}
this.subscription = WebSocketService.Instance.subject this.subscription = WebSocketService.Instance.subject
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
.subscribe( .subscribe(

View file

@ -33,6 +33,7 @@ import {
setupTippy, setupTippy,
hostname, hostname,
previewLines, previewLines,
toast,
} from '../utils'; } from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
@ -1032,6 +1033,10 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
handlePostLike(i: PostListing) { handlePostLike(i: PostListing) {
if (!UserService.Instance.user) {
this.context.router.history.push(`/login`);
}
let new_vote = i.state.my_vote == 1 ? 0 : 1; let new_vote = i.state.my_vote == 1 ? 0 : 1;
if (i.state.my_vote == 1) { if (i.state.my_vote == 1) {
@ -1059,6 +1064,10 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
handlePostDisLike(i: PostListing) { handlePostDisLike(i: PostListing) {
if (!UserService.Instance.user) {
this.context.router.history.push(`/login`);
}
let new_vote = i.state.my_vote == -1 ? 0 : -1; let new_vote = i.state.my_vote == -1 ? 0 : -1;
if (i.state.my_vote == 1) { if (i.state.my_vote == 1) {

View file

@ -220,6 +220,7 @@
"Lemmy is a <1>link aggregator</1> / reddit alternative, intended to work in the <2>fediverse</2>.<3></3>It's self-hostable, has live-updating comment threads, and is tiny (<4>~80kB</4>). Federation into the ActivityPub network is on the roadmap. <5></5>This is a <6>very early beta version</6>, and a lot of features are currently broken or missing. <7></7>Suggest new features or report bugs <8>here.</8><9></9>Made with <10>Rust</10>, <11>Actix</11>, <12>Inferno</12>, <13>Typescript</13>. <14></14> <15>Thank you to our contributors: </15> dessalines, Nutomic, asonix, zacanger, and iav.", "Lemmy is a <1>link aggregator</1> / reddit alternative, intended to work in the <2>fediverse</2>.<3></3>It's self-hostable, has live-updating comment threads, and is tiny (<4>~80kB</4>). Federation into the ActivityPub network is on the roadmap. <5></5>This is a <6>very early beta version</6>, and a lot of features are currently broken or missing. <7></7>Suggest new features or report bugs <8>here.</8><9></9>Made with <10>Rust</10>, <11>Actix</11>, <12>Inferno</12>, <13>Typescript</13>. <14></14> <15>Thank you to our contributors: </15> dessalines, Nutomic, asonix, zacanger, and iav.",
"not_logged_in": "Not logged in.", "not_logged_in": "Not logged in.",
"logged_in": "Logged in.", "logged_in": "Logged in.",
"must_login": "You must <1>log in or register</1> to comment.",
"site_saved": "Site Saved.", "site_saved": "Site Saved.",
"community_ban": "You have been banned from this community.", "community_ban": "You have been banned from this community.",
"site_ban": "You have been banned from the site", "site_ban": "You have been banned from the site",