-
+
|
- {this.isAdminOrMod ? (
+ {this.amAdminOrMod ? (
) : (
{this.modOrAdminText(i.view.moderator)}
@@ -410,19 +408,11 @@ export class Modlog extends Component {
);
}
- get isAdminOrMod(): boolean {
- let isAdmin =
- UserService.Instance.myUserInfo &&
- this.isoData.site_res.admins
- .map(a => a.person.id)
- .includes(UserService.Instance.myUserInfo.local_user_view.person.id);
- let isMod =
- UserService.Instance.myUserInfo &&
- this.state.communityMods &&
- this.state.communityMods
- .map(m => m.moderator.id)
- .includes(UserService.Instance.myUserInfo.local_user_view.person.id);
- return isAdmin || isMod;
+ get amAdminOrMod(): boolean {
+ return (
+ amAdmin(Some(this.state.siteRes.admins)) ||
+ amMod(this.state.communityMods)
+ );
}
modOrAdminText(person: PersonSafe): Text {
@@ -436,7 +426,10 @@ export class Modlog extends Component {
}
get documentTitle(): string {
- return `Modlog - ${this.state.site_view.site.name}`;
+ return this.state.siteRes.site_view.match({
+ some: siteView => `Modlog - ${siteView.site.name}`,
+ none: "",
+ });
}
render() {
@@ -445,6 +438,8 @@ export class Modlog extends Component {
{this.state.loading ? (
@@ -452,17 +447,6 @@ export class Modlog extends Component {
) : (
-
- {this.state.communityName && (
-
- /c/{this.state.communityName}{" "}
-
- )}
- {i18n.t("modlog")}
-
@@ -491,46 +475,52 @@ export class Modlog extends Component {
}
refetch() {
- let modlogForm: GetModlog = {
+ let modlogForm = new GetModlog({
community_id: this.state.communityId,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(false),
- };
+ mod_person_id: None,
+ page: Some(this.state.page),
+ limit: Some(fetchLimit),
+ auth: auth(false).ok(),
+ });
WebSocketService.Instance.send(wsClient.getModlog(modlogForm));
- if (this.state.communityId) {
- let communityForm: GetCommunity = {
- id: this.state.communityId,
- name: this.state.communityName,
- };
- WebSocketService.Instance.send(wsClient.getCommunity(communityForm));
- }
+ this.state.communityId.match({
+ some: id => {
+ let communityForm = new GetCommunity({
+ id: Some(id),
+ name: None,
+ auth: auth(false).ok(),
+ });
+ WebSocketService.Instance.send(wsClient.getCommunity(communityForm));
+ },
+ none: void 0,
+ });
}
static fetchInitialData(req: InitialFetchRequest): Promise[] {
let pathSplit = req.path.split("/");
- let communityId = pathSplit[3];
+ let communityId = Some(pathSplit[3]).map(Number);
let promises: Promise[] = [];
- let modlogForm: GetModlog = {
- page: 1,
- limit: fetchLimit,
- };
-
- if (communityId) {
- modlogForm.community_id = Number(communityId);
- }
- setOptionalAuth(modlogForm, req.auth);
+ let modlogForm = new GetModlog({
+ page: Some(1),
+ limit: Some(fetchLimit),
+ community_id: communityId,
+ mod_person_id: None,
+ auth: req.auth,
+ });
promises.push(req.client.getModlog(modlogForm));
- if (communityId) {
- let communityForm: GetCommunity = {
- id: Number(communityId),
- };
- setOptionalAuth(communityForm, req.auth);
+ if (communityId.isSome()) {
+ let communityForm = new GetCommunity({
+ id: communityId,
+ name: None,
+ auth: req.auth,
+ });
promises.push(req.client.getCommunity(communityForm));
+ } else {
+ promises.push(Promise.resolve());
}
return promises;
}
@@ -542,14 +532,14 @@ export class Modlog extends Component {
toast(i18n.t(msg.error), "danger");
return;
} else if (op == UserOperation.GetModlog) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, GetModlogResponse);
this.state.loading = false;
window.scrollTo(0, 0);
- this.state.res = data;
+ this.state.res = Some(data);
this.setState(this.state);
} else if (op == UserOperation.GetCommunity) {
- let data = wsJsonToRes(msg).data;
- this.state.communityMods = data.moderators;
+ let data = wsJsonToRes(msg, GetCommunityResponse);
+ this.state.communityMods = Some(data.moderators);
}
}
}
diff --git a/src/shared/components/person/inbox.tsx b/src/shared/components/person/inbox.tsx
index 7995baf7..e2faffcc 100644
--- a/src/shared/components/person/inbox.tsx
+++ b/src/shared/components/person/inbox.tsx
@@ -1,3 +1,4 @@
+import { None, Some } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import {
BlockPersonResponse,
@@ -9,25 +10,28 @@ import {
GetPrivateMessages,
GetReplies,
GetRepliesResponse,
+ GetSiteResponse,
PersonMentionResponse,
PersonMentionView,
PostReportResponse,
PrivateMessageResponse,
PrivateMessagesResponse,
PrivateMessageView,
- SiteView,
SortType,
UserOperation,
+ wsJsonToRes,
+ wsUserOp,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { InitialFetchRequest } from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
import {
- authField,
+ auth,
commentsToFlatNodes,
createCommentLikeRes,
editCommentRes,
+ enableDownvotes,
fetchLimit,
isBrowser,
relTags,
@@ -37,9 +41,7 @@ import {
toast,
updatePersonBlock,
wsClient,
- wsJsonToRes,
wsSubscribe,
- wsUserOp,
} from "../../utils";
import { CommentNodes } from "../comment/comment-nodes";
import { HtmlTags } from "../common/html-tags";
@@ -81,12 +83,17 @@ interface InboxState {
combined: ReplyType[];
sort: SortType;
page: number;
- site_view: SiteView;
+ siteRes: GetSiteResponse;
loading: boolean;
}
export class Inbox extends Component {
- private isoData = setIsoData(this.context);
+ private isoData = setIsoData(
+ this.context,
+ GetRepliesResponse,
+ GetPersonMentionsResponse,
+ PrivateMessagesResponse
+ );
private subscription: Subscription;
private emptyState: InboxState = {
unreadOrAll: UnreadOrAll.Unread,
@@ -97,7 +104,7 @@ export class Inbox extends Component {
combined: [],
sort: SortType.New,
page: 1,
- site_view: this.isoData.site_res.site_view,
+ siteRes: this.isoData.site_res,
loading: true,
};
@@ -108,7 +115,7 @@ export class Inbox extends Component {
this.handleSortChange = this.handleSortChange.bind(this);
this.handlePageChange = this.handlePageChange.bind(this);
- if (!UserService.Instance.myUserInfo && isBrowser()) {
+ if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
@@ -118,9 +125,13 @@ export class Inbox extends Component {
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
- this.state.replies = this.isoData.routeData[0].replies || [];
- this.state.mentions = this.isoData.routeData[1].mentions || [];
- this.state.messages = this.isoData.routeData[2].messages || [];
+ this.state.replies =
+ (this.isoData.routeData[0] as GetRepliesResponse).replies || [];
+ this.state.mentions =
+ (this.isoData.routeData[1] as GetPersonMentionsResponse).mentions || [];
+ this.state.messages =
+ (this.isoData.routeData[2] as PrivateMessagesResponse)
+ .private_messages || [];
this.state.combined = this.buildCombined();
this.state.loading = false;
} else {
@@ -135,13 +146,23 @@ export class Inbox extends Component {
}
get documentTitle(): string {
- return `@${
- UserService.Instance.myUserInfo.local_user_view.person.name
- } ${i18n.t("inbox")} - ${this.state.site_view.site.name}`;
+ return this.state.siteRes.site_view.match({
+ some: siteView =>
+ UserService.Instance.myUserInfo.match({
+ some: mui =>
+ `@${mui.local_user_view.person.name} ${i18n.t("inbox")} - ${
+ siteView.site.name
+ }`,
+ none: "",
+ }),
+ none: "",
+ });
}
render() {
- let inboxRss = `/feeds/inbox/${UserService.Instance.auth}.xml`;
+ let inboxRss = auth()
+ .ok()
+ .map(a => `/feeds/inbox/${a}.xml`);
return (
{this.state.loading ? (
@@ -154,19 +175,26 @@ export class Inbox extends Component {
{i18n.t("inbox")}
-
-
-
-
-
-
+ {inboxRss.match({
+ some: rss => (
+
+
+
+
+
+
+ ),
+ none: <>>,
+ })}
{this.state.replies.length +
this.state.mentions.length +
@@ -355,11 +383,14 @@ export class Inbox extends Component {
);
case ReplyEnum.Mention:
@@ -367,11 +398,14 @@ export class Inbox extends Component {
);
case ReplyEnum.Message:
@@ -395,11 +429,14 @@ export class Inbox extends Component {
);
@@ -412,11 +449,14 @@ export class Inbox extends Component {
))}
@@ -459,62 +499,67 @@ export class Inbox extends Component {
let promises: Promise[] = [];
// It can be /u/me, or /username/1
- let repliesForm: GetReplies = {
- sort: SortType.New,
- unread_only: true,
- page: 1,
- limit: fetchLimit,
- auth: req.auth,
- };
+ let repliesForm = new GetReplies({
+ sort: Some(SortType.New),
+ unread_only: Some(true),
+ page: Some(1),
+ limit: Some(fetchLimit),
+ auth: req.auth.unwrap(),
+ });
promises.push(req.client.getReplies(repliesForm));
- let personMentionsForm: GetPersonMentions = {
- sort: SortType.New,
- unread_only: true,
- page: 1,
- limit: fetchLimit,
- auth: req.auth,
- };
+ let personMentionsForm = new GetPersonMentions({
+ sort: Some(SortType.New),
+ unread_only: Some(true),
+ page: Some(1),
+ limit: Some(fetchLimit),
+ auth: req.auth.unwrap(),
+ });
promises.push(req.client.getPersonMentions(personMentionsForm));
- let privateMessagesForm: GetPrivateMessages = {
- unread_only: true,
- page: 1,
- limit: fetchLimit,
- auth: req.auth,
- };
+ let privateMessagesForm = new GetPrivateMessages({
+ unread_only: Some(true),
+ page: Some(1),
+ limit: Some(fetchLimit),
+ auth: req.auth.unwrap(),
+ });
promises.push(req.client.getPrivateMessages(privateMessagesForm));
return promises;
}
refetch() {
- let repliesForm: GetReplies = {
- sort: this.state.sort,
- unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(),
- };
+ let sort = Some(this.state.sort);
+ let unread_only = Some(this.state.unreadOrAll == UnreadOrAll.Unread);
+ let page = Some(this.state.page);
+ let limit = Some(fetchLimit);
+
+ let repliesForm = new GetReplies({
+ sort,
+ unread_only,
+ page,
+ limit,
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(wsClient.getReplies(repliesForm));
- let personMentionsForm: GetPersonMentions = {
- sort: this.state.sort,
- unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(),
- };
+ let personMentionsForm = new GetPersonMentions({
+ sort,
+ unread_only,
+ page,
+ limit,
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(
wsClient.getPersonMentions(personMentionsForm)
);
- let privateMessagesForm: GetPrivateMessages = {
- unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(),
- };
+ let privateMessagesForm = new GetPrivateMessages({
+ unread_only,
+ page,
+ limit,
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(
wsClient.getPrivateMessages(privateMessagesForm)
);
@@ -530,7 +575,7 @@ export class Inbox extends Component {
markAllAsRead(i: Inbox) {
WebSocketService.Instance.send(
wsClient.markAllAsRead({
- auth: authField(),
+ auth: auth().unwrap(),
})
);
i.state.replies = [];
@@ -559,7 +604,7 @@ export class Inbox extends Component {
} else if (msg.reconnect) {
this.refetch();
} else if (op == UserOperation.GetReplies) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, GetRepliesResponse);
this.state.replies = data.replies;
this.state.combined = this.buildCombined();
this.state.loading = false;
@@ -567,21 +612,30 @@ export class Inbox extends Component {
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.GetPersonMentions) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(
+ msg,
+ GetPersonMentionsResponse
+ );
this.state.mentions = data.mentions;
this.state.combined = this.buildCombined();
window.scrollTo(0, 0);
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.GetPrivateMessages) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(
+ msg,
+ PrivateMessagesResponse
+ );
this.state.messages = data.private_messages;
this.state.combined = this.buildCombined();
window.scrollTo(0, 0);
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.EditPrivateMessage) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(
+ msg,
+ PrivateMessageResponse
+ );
let found: PrivateMessageView = this.state.messages.find(
m =>
m.private_message.id === data.private_message_view.private_message.id
@@ -597,7 +651,10 @@ export class Inbox extends Component {
}
this.setState(this.state);
} else if (op == UserOperation.DeletePrivateMessage) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(
+ msg,
+ PrivateMessageResponse
+ );
let found: PrivateMessageView = this.state.messages.find(
m =>
m.private_message.id === data.private_message_view.private_message.id
@@ -613,7 +670,10 @@ export class Inbox extends Component {
}
this.setState(this.state);
} else if (op == UserOperation.MarkPrivateMessageAsRead) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(
+ msg,
+ PrivateMessageResponse
+ );
let found: PrivateMessageView = this.state.messages.find(
m =>
m.private_message.id === data.private_message_view.private_message.id
@@ -653,11 +713,11 @@ export class Inbox extends Component {
op == UserOperation.DeleteComment ||
op == UserOperation.RemoveComment
) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentResponse);
editCommentRes(data.comment_view, this.state.replies);
this.setState(this.state);
} else if (op == UserOperation.MarkCommentAsRead) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentResponse);
// If youre in the unread view, just remove it from the list
if (
@@ -685,7 +745,7 @@ export class Inbox extends Component {
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.MarkPersonMentionAsRead) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, PersonMentionResponse);
// TODO this might not be correct, it might need to use the comment id
let found = this.state.mentions.find(
@@ -732,85 +792,109 @@ export class Inbox extends Component {
this.sendUnreadCount(data.person_mention_view.person_mention.read);
this.setState(this.state);
} else if (op == UserOperation.CreateComment) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentResponse);
- if (
- data.recipient_ids.includes(
- UserService.Instance.myUserInfo.local_user_view.local_user.id
- )
- ) {
- this.state.replies.unshift(data.comment_view);
- this.state.combined.unshift(this.replyToReplyType(data.comment_view));
- this.setState(this.state);
- } else if (
- data.comment_view.creator.id ==
- UserService.Instance.myUserInfo.local_user_view.person.id
- ) {
- // If youre in the unread view, just remove it from the list
- if (this.state.unreadOrAll == UnreadOrAll.Unread) {
- this.state.replies = this.state.replies.filter(
- r => r.comment.id !== data.comment_view.comment.parent_id
- );
- this.state.mentions = this.state.mentions.filter(
- m => m.comment.id !== data.comment_view.comment.parent_id
- );
- this.state.combined = this.state.combined.filter(r => {
- if (this.isMention(r.view))
- return r.view.comment.id !== data.comment_view.comment.parent_id;
- else return r.id !== data.comment_view.comment.parent_id;
- });
- } else {
- let mention_found = this.state.mentions.find(
- i => i.comment.id == data.comment_view.comment.parent_id
- );
- if (mention_found) {
- mention_found.person_mention.read = true;
+ UserService.Instance.myUserInfo.match({
+ some: mui => {
+ if (data.recipient_ids.includes(mui.local_user_view.local_user.id)) {
+ this.state.replies.unshift(data.comment_view);
+ this.state.combined.unshift(
+ this.replyToReplyType(data.comment_view)
+ );
+ this.setState(this.state);
+ } else if (
+ data.comment_view.creator.id == mui.local_user_view.person.id
+ ) {
+ // If youre in the unread view, just remove it from the list
+ if (this.state.unreadOrAll == UnreadOrAll.Unread) {
+ this.state.replies = this.state.replies.filter(
+ r =>
+ r.comment.id !==
+ data.comment_view.comment.parent_id.unwrapOr(0)
+ );
+ this.state.mentions = this.state.mentions.filter(
+ m =>
+ m.comment.id !==
+ data.comment_view.comment.parent_id.unwrapOr(0)
+ );
+ this.state.combined = this.state.combined.filter(r => {
+ if (this.isMention(r.view))
+ return (
+ r.view.comment.id !==
+ data.comment_view.comment.parent_id.unwrapOr(0)
+ );
+ else
+ return (
+ r.id !== data.comment_view.comment.parent_id.unwrapOr(0)
+ );
+ });
+ } else {
+ let mention_found = this.state.mentions.find(
+ i =>
+ i.comment.id ==
+ data.comment_view.comment.parent_id.unwrapOr(0)
+ );
+ if (mention_found) {
+ mention_found.person_mention.read = true;
+ }
+ let reply_found = this.state.replies.find(
+ i =>
+ i.comment.id ==
+ data.comment_view.comment.parent_id.unwrapOr(0)
+ );
+ if (reply_found) {
+ reply_found.comment.read = true;
+ }
+ this.state.combined = this.buildCombined();
+ }
+ this.sendUnreadCount(true);
+ this.setState(this.state);
+ setupTippy();
+ // TODO this seems wrong, you should be using form_id
+ toast(i18n.t("reply_sent"));
}
- let reply_found = this.state.replies.find(
- i => i.comment.id == data.comment_view.comment.parent_id
- );
- if (reply_found) {
- reply_found.comment.read = true;
- }
- this.state.combined = this.buildCombined();
- }
- this.sendUnreadCount(true);
- this.setState(this.state);
- setupTippy();
- // TODO this seems wrong, you should be using form_id
- toast(i18n.t("reply_sent"));
- }
+ },
+ none: void 0,
+ });
} else if (op == UserOperation.CreatePrivateMessage) {
- let data = wsJsonToRes(msg).data;
- if (
- data.private_message_view.recipient.id ==
- UserService.Instance.myUserInfo.local_user_view.person.id
- ) {
- this.state.messages.unshift(data.private_message_view);
- this.state.combined.unshift(
- this.messageToReplyType(data.private_message_view)
- );
- this.setState(this.state);
- }
+ let data = wsJsonToRes(
+ msg,
+ PrivateMessageResponse
+ );
+ UserService.Instance.myUserInfo.match({
+ some: mui => {
+ if (
+ data.private_message_view.recipient.id ==
+ mui.local_user_view.person.id
+ ) {
+ this.state.messages.unshift(data.private_message_view);
+ this.state.combined.unshift(
+ this.messageToReplyType(data.private_message_view)
+ );
+ this.setState(this.state);
+ }
+ },
+ none: void 0,
+ });
} else if (op == UserOperation.SaveComment) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentResponse);
saveCommentRes(data.comment_view, this.state.replies);
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.CreateCommentLike) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentResponse);
createCommentLikeRes(data.comment_view, this.state.replies);
this.setState(this.state);
} else if (op == UserOperation.BlockPerson) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, BlockPersonResponse);
updatePersonBlock(data);
} else if (op == UserOperation.CreatePostReport) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, PostReportResponse);
if (data) {
toast(i18n.t("report_created"));
}
} else if (op == UserOperation.CreateCommentReport) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentReportResponse);
if (data) {
toast(i18n.t("report_created"));
}
diff --git a/src/shared/components/person/password-change.tsx b/src/shared/components/person/password-change.tsx
index 2b23ddcb..8120fe97 100644
--- a/src/shared/components/person/password-change.tsx
+++ b/src/shared/components/person/password-change.tsx
@@ -1,9 +1,12 @@
+import { None } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import {
+ GetSiteResponse,
LoginResponse,
PasswordChange as PasswordChangeForm,
- SiteView,
UserOperation,
+ wsJsonToRes,
+ wsUserOp,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
@@ -14,9 +17,7 @@ import {
setIsoData,
toast,
wsClient,
- wsJsonToRes,
wsSubscribe,
- wsUserOp,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
@@ -24,7 +25,7 @@ import { Spinner } from "../common/icon";
interface State {
passwordChangeForm: PasswordChangeForm;
loading: boolean;
- site_view: SiteView;
+ siteRes: GetSiteResponse;
}
export class PasswordChange extends Component {
@@ -32,13 +33,13 @@ export class PasswordChange extends Component {
private subscription: Subscription;
emptyState: State = {
- passwordChangeForm: {
+ passwordChangeForm: new PasswordChangeForm({
token: this.props.match.params.token,
password: undefined,
password_verify: undefined,
- },
+ }),
loading: false,
- site_view: this.isoData.site_res.site_view,
+ siteRes: this.isoData.site_res,
};
constructor(props: any, context: any) {
@@ -57,7 +58,10 @@ export class PasswordChange extends Component {
}
get documentTitle(): string {
- return `${i18n.t("password_change")} - ${this.state.site_view.site.name}`;
+ return this.state.siteRes.site_view.match({
+ some: siteView => `${i18n.t("password_change")} - ${siteView.site.name}`,
+ none: "",
+ });
}
render() {
@@ -66,6 +70,8 @@ export class PasswordChange extends Component {
@@ -156,7 +162,7 @@ export class PasswordChange extends Component {
this.setState(this.state);
return;
} else if (op == UserOperation.PasswordChange) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, LoginResponse);
this.state = this.emptyState;
this.setState(this.state);
UserService.Instance.login(data);
diff --git a/src/shared/components/person/person-details.tsx b/src/shared/components/person/person-details.tsx
index 1e37a581..0dabf2af 100644
--- a/src/shared/components/person/person-details.tsx
+++ b/src/shared/components/person/person-details.tsx
@@ -1,3 +1,4 @@
+import { None, Some } from "@sniptt/monads/build";
import { Component } from "inferno";
import {
CommentView,
@@ -89,7 +90,9 @@ export class PersonDetails extends Component {
{
{
{
<>
diff --git a/src/shared/components/person/person-listing.tsx b/src/shared/components/person/person-listing.tsx
index 56db662b..88b88820 100644
--- a/src/shared/components/person/person-listing.tsx
+++ b/src/shared/components/person/person-listing.tsx
@@ -21,7 +21,7 @@ export class PersonListing extends Component {
render() {
let person = this.props.person;
- let local = person.local == null ? true : person.local;
+ let local = person.local;
let apubName: string, link: string;
if (local) {
@@ -37,11 +37,9 @@ export class PersonListing extends Component {
let displayName = this.props.useApubName
? apubName
- : person.display_name
- ? person.display_name
- : apubName;
+ : person.display_name.unwrapOr(apubName);
- if (this.props.showApubName && !local && person.display_name) {
+ if (this.props.showApubName && !local && person.display_name.isSome()) {
displayName = `${displayName} (${apubName})`;
}
@@ -72,12 +70,14 @@ export class PersonListing extends Component {
}
avatarAndName(displayName: string) {
- let person = this.props.person;
return (
<>
- {!this.props.hideAvatar && person.avatar && showAvatars() && (
-
- )}
+ {this.props.person.avatar.match({
+ some: avatar =>
+ !this.props.hideAvatar &&
+ showAvatars() && ,
+ none: <>>,
+ })}
{displayName}
>
);
diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx
index 372a899f..053e4b27 100644
--- a/src/shared/components/person/profile.tsx
+++ b/src/shared/components/person/profile.tsx
@@ -1,3 +1,4 @@
+import { None, Option, Some } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import { Link } from "inferno-router";
import {
@@ -12,7 +13,10 @@ import {
GetSiteResponse,
PostResponse,
SortType,
+ toUndefined,
UserOperation,
+ wsJsonToRes,
+ wsUserOp,
} from "lemmy-js-client";
import moment from "moment";
import { Subscription } from "rxjs";
@@ -20,18 +24,20 @@ import { i18n } from "../../i18next";
import { InitialFetchRequest, PersonDetailsView } from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
import {
- authField,
+ auth,
canMod,
capitalizeFirstLetter,
createCommentLikeRes,
createPostLikeFindRes,
editCommentRes,
editPostFindRes,
+ enableDownvotes,
+ enableNsfw,
fetchLimit,
futureDaysToUnixTime,
getUsernameFromProps,
+ isAdmin,
isBanned,
- isMod,
mdToHtml,
numToSI,
relTags,
@@ -40,14 +46,11 @@ import {
saveCommentRes,
saveScrollPosition,
setIsoData,
- setOptionalAuth,
setupTippy,
toast,
updatePersonBlock,
wsClient,
- wsJsonToRes,
wsSubscribe,
- wsUserOp,
} from "../../utils";
import { BannerIconHeader } from "../common/banner-icon-header";
import { HtmlTags } from "../common/html-tags";
@@ -59,18 +62,18 @@ import { PersonDetails } from "./person-details";
import { PersonListing } from "./person-listing";
interface ProfileState {
- personRes: GetPersonDetailsResponse;
+ personRes: Option;
userName: string;
view: PersonDetailsView;
sort: SortType;
page: number;
loading: boolean;
personBlocked: boolean;
- siteRes: GetSiteResponse;
+ banReason: Option;
+ banExpireDays: Option;
showBanDialog: boolean;
- banReason: string;
- banExpireDays: number;
removeData: boolean;
+ siteRes: GetSiteResponse;
}
interface ProfileProps {
@@ -88,10 +91,10 @@ interface UrlParams {
}
export class Profile extends Component {
- private isoData = setIsoData(this.context);
+ private isoData = setIsoData(this.context, GetPersonDetailsResponse);
private subscription: Subscription;
private emptyState: ProfileState = {
- personRes: undefined,
+ personRes: None,
userName: getUsernameFromProps(this.props),
loading: true,
view: Profile.getViewFromProps(this.props.match.view),
@@ -117,7 +120,9 @@ export class Profile extends Component {
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
- this.state.personRes = this.isoData.routeData[0];
+ this.state.personRes = Some(
+ this.isoData.routeData[0] as GetPersonDetailsResponse
+ );
this.state.loading = false;
} else {
this.fetchUserData();
@@ -127,28 +132,44 @@ export class Profile extends Component {
}
fetchUserData() {
- let form: GetPersonDetails = {
- username: this.state.userName,
- sort: this.state.sort,
- saved_only: this.state.view === PersonDetailsView.Saved,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(false),
- };
+ let form = new GetPersonDetails({
+ username: Some(this.state.userName),
+ person_id: None,
+ community_id: None,
+ sort: Some(this.state.sort),
+ saved_only: Some(this.state.view === PersonDetailsView.Saved),
+ page: Some(this.state.page),
+ limit: Some(fetchLimit),
+ auth: auth(false).ok(),
+ });
WebSocketService.Instance.send(wsClient.getPersonDetails(form));
}
- get isCurrentUser() {
- return (
- UserService.Instance.myUserInfo?.local_user_view.person.id ==
- this.state.personRes?.person_view.person.id
- );
+ get amCurrentUser() {
+ return UserService.Instance.myUserInfo.match({
+ some: mui =>
+ this.state.personRes.match({
+ some: res =>
+ mui.local_user_view.person.id == res.person_view.person.id,
+ none: false,
+ }),
+ none: false,
+ });
}
setPersonBlock() {
- this.state.personBlocked = UserService.Instance.myUserInfo?.person_blocks
- .map(a => a.target.id)
- .includes(this.state.personRes?.person_view.person.id);
+ UserService.Instance.myUserInfo.match({
+ some: mui =>
+ this.state.personRes.match({
+ some: res => {
+ this.state.personBlocked = mui.person_blocks
+ .map(a => a.target.id)
+ .includes(res.person_view.person.id);
+ },
+ none: void 0,
+ }),
+ none: void 0,
+ });
}
static getViewFromProps(view: string): PersonDetailsView {
@@ -165,23 +186,23 @@ export class Profile extends Component {
static fetchInitialData(req: InitialFetchRequest): Promise[] {
let pathSplit = req.path.split("/");
- let promises: Promise[] = [];
let username = pathSplit[2];
let view = this.getViewFromProps(pathSplit[4]);
- let sort = this.getSortTypeFromProps(pathSplit[6]);
- let page = this.getPageFromProps(Number(pathSplit[8]));
+ let sort = Some(this.getSortTypeFromProps(pathSplit[6]));
+ let page = Some(this.getPageFromProps(Number(pathSplit[8])));
- let form: GetPersonDetails = {
+ let form = new GetPersonDetails({
+ username: Some(username),
+ person_id: None,
+ community_id: None,
sort,
- saved_only: view === PersonDetailsView.Saved,
+ saved_only: Some(view === PersonDetailsView.Saved),
page,
- limit: fetchLimit,
- username: username,
- };
- setOptionalAuth(form, req.auth);
- promises.push(req.client.getPersonDetails(form));
- return promises;
+ limit: Some(fetchLimit),
+ auth: req.auth,
+ });
+ return [req.client.getPersonDetails(form)];
}
componentDidMount() {
@@ -215,13 +236,15 @@ export class Profile extends Component {
}
get documentTitle(): string {
- return `@${this.state.personRes.person_view.person.name} - ${this.state.siteRes.site_view.site.name}`;
- }
-
- get bioTag(): string {
- return this.state.personRes.person_view.person.bio
- ? this.state.personRes.person_view.person.bio
- : undefined;
+ return this.state.siteRes.site_view.match({
+ some: siteView =>
+ this.state.personRes.match({
+ some: res =>
+ `@${res.person_view.person.name} - ${siteView.site.name}`,
+ none: "",
+ }),
+ none: "",
+ });
}
render() {
@@ -232,41 +255,44 @@ export class Profile extends Component {
) : (
-
-
- <>
-
- {this.userInfo()}
-
- >
- {!this.state.loading && this.selects()}
-
-
+ this.state.personRes.match({
+ some: res => (
+
+
+ <>
+
+ {this.userInfo()}
+
+ >
+ {!this.state.loading && this.selects()}
+
+
- {!this.state.loading && (
-
- {this.moderates()}
- {this.isCurrentUser && this.follows()}
+ {!this.state.loading && (
+
+ {this.moderates()}
+ {this.amCurrentUser && this.follows()}
+
+ )}
- )}
-
+ ),
+ none: <>>,
+ })
)}
);
@@ -352,286 +378,330 @@ export class Profile extends Component {
}
handleBlockPerson(personId: number) {
if (personId != 0) {
- let blockUserForm: BlockPerson = {
+ let blockUserForm = new BlockPerson({
person_id: personId,
block: true,
- auth: authField(),
- };
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
}
}
handleUnblockPerson(recipientId: number) {
- let blockUserForm: BlockPerson = {
+ let blockUserForm = new BlockPerson({
person_id: recipientId,
block: false,
- auth: authField(),
- };
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm));
}
userInfo() {
- let pv = this.state.personRes?.person_view;
-
- return (
-
-
-
-
-
-
- {pv.person.display_name && (
- {pv.person.display_name}
- )}
-
- -
-
-
- {isBanned(pv.person) && (
- -
- {i18n.t("banned")}
-
- )}
- {pv.person.admin && (
- -
- {i18n.t("admin")}
-
- )}
- {pv.person.bot_account && (
- -
- {i18n.t("bot_account").toLowerCase()}
-
- )}
-
-
- {this.banDialog()}
-
- {!this.isCurrentUser && UserService.Instance.myUserInfo && (
- <>
-
- {i18n.t("send_secure_message")}
-
-
- {i18n.t("send_message")}
-
- {this.state.personBlocked ? (
-
+ ),
+ none: <>>,
+ });
}
+ banDialog() {
+ return this.state.personRes
+ .map(r => r.person_view)
+ .match({
+ some: pv => (
+ <>
+ {this.state.showBanDialog && (
+
+ )}
+ >
+ ),
+ none: <>>,
+ });
+ }
+
+ // TODO test this, make sure its good
moderates() {
- return (
-
- {this.state.personRes.moderates.length > 0 && (
-
-
- {i18n.t("moderates")}
-
- {this.state.personRes.moderates.map(cmv => (
- -
-
-
- ))}
-
-
-
- )}
-
- );
+ return this.state.personRes
+ .map(r => r.moderates)
+ .match({
+ some: moderates => {
+ if (moderates.length > 0) {
+
+
+ {i18n.t("moderates")}
+
+ {moderates.map(cmv => (
+ -
+
+
+ ))}
+
+
+ ;
+ }
+ },
+ none: void 0,
+ });
}
follows() {
- let follows = UserService.Instance.myUserInfo.follows;
- return (
-
- {follows.length > 0 && (
-
-
- {i18n.t("subscribed")}
-
- {follows.map(cfv => (
- -
-
-
- ))}
-
-
-
- )}
-
- );
+ return UserService.Instance.myUserInfo
+ .map(m => m.follows)
+ .match({
+ some: follows => {
+ if (follows.length > 0) {
+
+
+ {i18n.t("subscribed")}
+
+ {follows.map(cfv => (
+ -
+
+
+ ))}
+
+
+ ;
+ }
+ },
+ none: void 0,
+ });
}
updateUrl(paramUpdates: UrlParams) {
@@ -649,29 +719,8 @@ export class Profile extends Component {
this.fetchUserData();
}
- get canAdmin(): boolean {
- return (
- this.state.siteRes?.admins &&
- canMod(
- UserService.Instance.myUserInfo,
- this.state.siteRes.admins.map(a => a.person.id),
- this.state.personRes?.person_view.person.id
- )
- );
- }
-
- get personIsAdmin(): boolean {
- return (
- this.state.siteRes?.admins &&
- isMod(
- this.state.siteRes.admins.map(a => a.person.id),
- this.state.personRes?.person_view.person.id
- )
- );
- }
-
handlePageChange(page: number) {
- this.updateUrl({ page });
+ this.updateUrl({ page: page });
}
handleSortChange(val: SortType) {
@@ -714,24 +763,30 @@ export class Profile extends Component {
handleModBanSubmit(i: Profile, event?: any) {
if (event) event.preventDefault();
- let pv = i.state.personRes.person_view;
- // If its an unban, restore all their data
- let ban = !pv.person.banned;
- if (ban == false) {
- i.state.removeData = false;
- }
- let form: BanPerson = {
- person_id: pv.person.id,
- ban,
- remove_data: i.state.removeData,
- reason: i.state.banReason,
- expires: futureDaysToUnixTime(i.state.banExpireDays),
- auth: authField(),
- };
- WebSocketService.Instance.send(wsClient.banPerson(form));
+ i.state.personRes
+ .map(r => r.person_view.person)
+ .match({
+ some: person => {
+ // If its an unban, restore all their data
+ let ban = !person.banned;
+ if (ban == false) {
+ i.state.removeData = false;
+ }
+ let form = new BanPerson({
+ person_id: person.id,
+ ban,
+ remove_data: Some(i.state.removeData),
+ reason: i.state.banReason,
+ expires: i.state.banExpireDays.map(futureDaysToUnixTime),
+ auth: auth().unwrap(),
+ });
+ WebSocketService.Instance.send(wsClient.banPerson(form));
- i.state.showBanDialog = false;
- i.setState(i.state);
+ i.state.showBanDialog = false;
+ i.setState(i.state);
+ },
+ none: void 0,
+ });
}
parseMessage(msg: any) {
@@ -749,41 +804,53 @@ export class Profile extends Component {
// Since the PersonDetails contains posts/comments as well as some general user info we listen here as well
// and set the parent state if it is not set or differs
// TODO this might need to get abstracted
- let data = wsJsonToRes(msg).data;
- this.state.personRes = data;
- console.log(data);
+ let data = wsJsonToRes(
+ msg,
+ GetPersonDetailsResponse
+ );
+ this.state.personRes = Some(data);
this.state.loading = false;
this.setPersonBlock();
this.setState(this.state);
restoreScrollPosition(this.context);
} else if (op == UserOperation.AddAdmin) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, AddAdminResponse);
this.state.siteRes.admins = data.admins;
this.setState(this.state);
} else if (op == UserOperation.CreateCommentLike) {
- let data = wsJsonToRes(msg).data;
- createCommentLikeRes(data.comment_view, this.state.personRes.comments);
+ let data = wsJsonToRes(msg, CommentResponse);
+ createCommentLikeRes(
+ data.comment_view,
+ this.state.personRes.map(r => r.comments).unwrapOr([])
+ );
this.setState(this.state);
} else if (
op == UserOperation.EditComment ||
op == UserOperation.DeleteComment ||
op == UserOperation.RemoveComment
) {
- let data = wsJsonToRes(msg).data;
- editCommentRes(data.comment_view, this.state.personRes.comments);
+ let data = wsJsonToRes(msg, CommentResponse);
+ editCommentRes(
+ data.comment_view,
+ this.state.personRes.map(r => r.comments).unwrapOr([])
+ );
this.setState(this.state);
} else if (op == UserOperation.CreateComment) {
- let data = wsJsonToRes(msg).data;
- if (
- UserService.Instance.myUserInfo &&
- data.comment_view.creator.id ==
- UserService.Instance.myUserInfo?.local_user_view.person.id
- ) {
- toast(i18n.t("reply_sent"));
- }
+ let data = wsJsonToRes(msg, CommentResponse);
+ UserService.Instance.myUserInfo.match({
+ some: mui => {
+ if (data.comment_view.creator.id == mui.local_user_view.person.id) {
+ toast(i18n.t("reply_sent"));
+ }
+ },
+ none: void 0,
+ });
} else if (op == UserOperation.SaveComment) {
- let data = wsJsonToRes(msg).data;
- saveCommentRes(data.comment_view, this.state.personRes.comments);
+ let data = wsJsonToRes(msg, CommentResponse);
+ saveCommentRes(
+ data.comment_view,
+ this.state.personRes.map(r => r.comments).unwrapOr([])
+ );
this.setState(this.state);
} else if (
op == UserOperation.EditPost ||
@@ -793,29 +860,40 @@ export class Profile extends Component {
op == UserOperation.StickyPost ||
op == UserOperation.SavePost
) {
- let data = wsJsonToRes(msg).data;
- editPostFindRes(data.post_view, this.state.personRes.posts);
+ let data = wsJsonToRes(msg, PostResponse);
+ editPostFindRes(
+ data.post_view,
+ this.state.personRes.map(r => r.posts).unwrapOr([])
+ );
this.setState(this.state);
} else if (op == UserOperation.CreatePostLike) {
- let data = wsJsonToRes(msg).data;
- createPostLikeFindRes(data.post_view, this.state.personRes.posts);
+ let data = wsJsonToRes(msg, PostResponse);
+ createPostLikeFindRes(
+ data.post_view,
+ this.state.personRes.map(r => r.posts).unwrapOr([])
+ );
this.setState(this.state);
} else if (op == UserOperation.BanPerson) {
- let data = wsJsonToRes(msg).data;
- this.state.personRes.comments
- .filter(c => c.creator.id == data.person_view.person.id)
- .forEach(c => (c.creator.banned = data.banned));
- this.state.personRes.posts
- .filter(c => c.creator.id == data.person_view.person.id)
- .forEach(c => (c.creator.banned = data.banned));
- let pv = this.state.personRes.person_view;
+ let data = wsJsonToRes(msg, BanPersonResponse);
+ this.state.personRes.match({
+ some: res => {
+ res.comments
+ .filter(c => c.creator.id == data.person_view.person.id)
+ .forEach(c => (c.creator.banned = data.banned));
+ res.posts
+ .filter(c => c.creator.id == data.person_view.person.id)
+ .forEach(c => (c.creator.banned = data.banned));
+ let pv = res.person_view;
- if (pv.person.id == data.person_view.person.id) {
- pv.person.banned = data.banned;
- }
- this.setState(this.state);
+ if (pv.person.id == data.person_view.person.id) {
+ pv.person.banned = data.banned;
+ }
+ this.setState(this.state);
+ },
+ none: void 0,
+ });
} else if (op == UserOperation.BlockPerson) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, BlockPersonResponse);
updatePersonBlock(data);
this.setPersonBlock();
this.setState(this.state);
diff --git a/src/shared/components/person/registration-applications.tsx b/src/shared/components/person/registration-applications.tsx
index 9009f746..eec90319 100644
--- a/src/shared/components/person/registration-applications.tsx
+++ b/src/shared/components/person/registration-applications.tsx
@@ -1,18 +1,20 @@
+import { None, Option, Some } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import {
+ GetSiteResponse,
ListRegistrationApplications,
ListRegistrationApplicationsResponse,
RegistrationApplicationResponse,
- RegistrationApplicationView,
- SiteView,
UserOperation,
+ wsJsonToRes,
+ wsUserOp,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { InitialFetchRequest } from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
import {
- authField,
+ auth,
fetchLimit,
isBrowser,
setIsoData,
@@ -20,9 +22,7 @@ import {
toast,
updateRegistrationApplicationRes,
wsClient,
- wsJsonToRes,
wsSubscribe,
- wsUserOp,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
@@ -35,10 +35,10 @@ enum UnreadOrAll {
}
interface RegistrationApplicationsState {
- applications: RegistrationApplicationView[];
- page: number;
- site_view: SiteView;
+ listRegistrationApplicationsResponse: Option;
+ siteRes: GetSiteResponse;
unreadOrAll: UnreadOrAll;
+ page: number;
loading: boolean;
}
@@ -46,13 +46,16 @@ export class RegistrationApplications extends Component<
any,
RegistrationApplicationsState
> {
- private isoData = setIsoData(this.context);
+ private isoData = setIsoData(
+ this.context,
+ ListRegistrationApplicationsResponse
+ );
private subscription: Subscription;
private emptyState: RegistrationApplicationsState = {
+ listRegistrationApplicationsResponse: None,
+ siteRes: this.isoData.site_res,
unreadOrAll: UnreadOrAll.Unread,
- applications: [],
page: 1,
- site_view: this.isoData.site_res.site_view,
loading: true,
};
@@ -62,7 +65,7 @@ export class RegistrationApplications extends Component<
this.state = this.emptyState;
this.handlePageChange = this.handlePageChange.bind(this);
- if (!UserService.Instance.myUserInfo && isBrowser()) {
+ if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
@@ -72,8 +75,9 @@ export class RegistrationApplications extends Component<
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
- this.state.applications =
- this.isoData.routeData[0].registration_applications || []; // TODO test
+ this.state.listRegistrationApplicationsResponse = Some(
+ this.isoData.routeData[0] as ListRegistrationApplicationsResponse
+ );
this.state.loading = false;
} else {
this.refetch();
@@ -91,11 +95,17 @@ export class RegistrationApplications extends Component<
}
get documentTitle(): string {
- return `@${
- UserService.Instance.myUserInfo.local_user_view.person.name
- } ${i18n.t("registration_applications")} - ${
- this.state.site_view.site.name
- }`;
+ return this.state.siteRes.site_view.match({
+ some: siteView =>
+ UserService.Instance.myUserInfo.match({
+ some: mui =>
+ `@${mui.local_user_view.person.name} ${i18n.t(
+ "registration_applications"
+ )} - ${siteView.site.name}`,
+ none: "",
+ }),
+ none: "",
+ });
}
render() {
@@ -111,6 +121,8 @@ export class RegistrationApplications extends Component<
{i18n.t("registration_applications")}
{this.selects()}
@@ -168,19 +180,22 @@ export class RegistrationApplications extends Component<
}
applicationList() {
- return (
-
- {this.state.applications.map(ra => (
- <>
-
-
- >
- ))}
-
- );
+ return this.state.listRegistrationApplicationsResponse.match({
+ some: res => (
+
+ {res.registration_applications.map(ra => (
+ <>
+
+
+ >
+ ))}
+
+ ),
+ none: <>>,
+ });
}
handleUnreadOrAllChange(i: RegistrationApplications, event: any) {
@@ -198,12 +213,12 @@ export class RegistrationApplications extends Component<
static fetchInitialData(req: InitialFetchRequest): Promise[] {
let promises: Promise[] = [];
- let form: ListRegistrationApplications = {
- unread_only: true,
- page: 1,
- limit: fetchLimit,
- auth: req.auth,
- };
+ let form = new ListRegistrationApplications({
+ unread_only: Some(true),
+ page: Some(1),
+ limit: Some(fetchLimit),
+ auth: req.auth.unwrap(),
+ });
promises.push(req.client.listRegistrationApplications(form));
return promises;
@@ -211,12 +226,12 @@ export class RegistrationApplications extends Component<
refetch() {
let unread_only = this.state.unreadOrAll == UnreadOrAll.Unread;
- let form: ListRegistrationApplications = {
- unread_only: unread_only,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(),
- };
+ let form = new ListRegistrationApplications({
+ unread_only: Some(unread_only),
+ page: Some(this.state.page),
+ limit: Some(fetchLimit),
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(wsClient.listRegistrationApplications(form));
}
@@ -229,16 +244,24 @@ export class RegistrationApplications extends Component<
} else if (msg.reconnect) {
this.refetch();
} else if (op == UserOperation.ListRegistrationApplications) {
- let data = wsJsonToRes(msg).data;
- this.state.applications = data.registration_applications;
+ let data = wsJsonToRes(
+ msg,
+ ListRegistrationApplicationsResponse
+ );
+ this.state.listRegistrationApplicationsResponse = Some(data);
this.state.loading = false;
window.scrollTo(0, 0);
this.setState(this.state);
} else if (op == UserOperation.ApproveRegistrationApplication) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(
+ msg,
+ RegistrationApplicationResponse
+ );
updateRegistrationApplicationRes(
data.registration_application,
- this.state.applications
+ this.state.listRegistrationApplicationsResponse
+ .map(r => r.registration_applications)
+ .unwrapOr([])
);
let uacs = UserService.Instance.unreadApplicationCountSub;
// Minor bug, where if the application switches from deny to approve, the count will still go down
diff --git a/src/shared/components/person/reports.tsx b/src/shared/components/person/reports.tsx
index 99edf968..f8a641b5 100644
--- a/src/shared/components/person/reports.tsx
+++ b/src/shared/components/person/reports.tsx
@@ -1,22 +1,25 @@
+import { None, Option, Some } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import {
CommentReportResponse,
CommentReportView,
+ GetSiteResponse,
ListCommentReports,
ListCommentReportsResponse,
ListPostReports,
ListPostReportsResponse,
PostReportResponse,
PostReportView,
- SiteView,
UserOperation,
+ wsJsonToRes,
+ wsUserOp,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { InitialFetchRequest } from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
import {
- authField,
+ auth,
fetchLimit,
isBrowser,
setIsoData,
@@ -25,9 +28,7 @@ import {
updateCommentReportRes,
updatePostReportRes,
wsClient,
- wsJsonToRes,
wsSubscribe,
- wsUserOp,
} from "../../utils";
import { CommentReport } from "../comment/comment-report";
import { HtmlTags } from "../common/html-tags";
@@ -59,27 +60,31 @@ type ItemType = {
};
interface ReportsState {
+ listCommentReportsResponse: Option;
+ listPostReportsResponse: Option;
unreadOrAll: UnreadOrAll;
messageType: MessageType;
- commentReports: CommentReportView[];
- postReports: PostReportView[];
combined: ItemType[];
+ siteRes: GetSiteResponse;
page: number;
- site_view: SiteView;
loading: boolean;
}
export class Reports extends Component {
- private isoData = setIsoData(this.context);
+ private isoData = setIsoData(
+ this.context,
+ ListCommentReportsResponse,
+ ListPostReportsResponse
+ );
private subscription: Subscription;
private emptyState: ReportsState = {
+ listCommentReportsResponse: None,
+ listPostReportsResponse: None,
unreadOrAll: UnreadOrAll.Unread,
messageType: MessageType.All,
- commentReports: [],
- postReports: [],
combined: [],
page: 1,
- site_view: this.isoData.site_res.site_view,
+ siteRes: this.isoData.site_res,
loading: true,
};
@@ -89,7 +94,7 @@ export class Reports extends Component {
this.state = this.emptyState;
this.handlePageChange = this.handlePageChange.bind(this);
- if (!UserService.Instance.myUserInfo && isBrowser()) {
+ if (UserService.Instance.myUserInfo.isNone() && isBrowser()) {
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
@@ -99,9 +104,12 @@ export class Reports extends Component {
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
- this.state.commentReports =
- this.isoData.routeData[0].comment_reports || [];
- this.state.postReports = this.isoData.routeData[1].post_reports || [];
+ this.state.listCommentReportsResponse = Some(
+ this.isoData.routeData[0] as ListCommentReportsResponse
+ );
+ this.state.listPostReportsResponse = Some(
+ this.isoData.routeData[1] as ListPostReportsResponse
+ );
this.state.combined = this.buildCombined();
this.state.loading = false;
} else {
@@ -116,9 +124,17 @@ export class Reports extends Component {
}
get documentTitle(): string {
- return `@${
- UserService.Instance.myUserInfo.local_user_view.person.name
- } ${i18n.t("reports")} - ${this.state.site_view.site.name}`;
+ return this.state.siteRes.site_view.match({
+ some: siteView =>
+ UserService.Instance.myUserInfo.match({
+ some: mui =>
+ `@${mui.local_user_view.person.name} ${i18n.t("reports")} - ${
+ siteView.site.name
+ }`,
+ none: "",
+ }),
+ none: "",
+ });
}
render() {
@@ -134,6 +150,8 @@ export class Reports extends Component {
{i18n.t("reports")}
{this.selects()}
@@ -260,12 +278,14 @@ export class Reports extends Component {
}
buildCombined(): ItemType[] {
- let comments: ItemType[] = this.state.commentReports.map(r =>
- this.replyToReplyType(r)
- );
- let posts: ItemType[] = this.state.postReports.map(r =>
- this.mentionToReplyType(r)
- );
+ let comments: ItemType[] = this.state.listCommentReportsResponse
+ .map(r => r.comment_reports)
+ .unwrapOr([])
+ .map(r => this.replyToReplyType(r));
+ let posts: ItemType[] = this.state.listPostReportsResponse
+ .map(r => r.post_reports)
+ .unwrapOr([])
+ .map(r => this.mentionToReplyType(r));
return [...comments, ...posts].sort((a, b) =>
b.published.localeCompare(a.published)
@@ -299,29 +319,35 @@ export class Reports extends Component {
}
commentReports() {
- return (
-
- {this.state.commentReports.map(cr => (
- <>
-
-
- >
- ))}
-
- );
+ return this.state.listCommentReportsResponse.match({
+ some: res => (
+
+ {res.comment_reports.map(cr => (
+ <>
+
+
+ >
+ ))}
+
+ ),
+ none: <>>,
+ });
}
postReports() {
- return (
-
- {this.state.postReports.map(pr => (
- <>
-
-
- >
- ))}
-
- );
+ return this.state.listPostReportsResponse.match({
+ some: res => (
+
+ {res.post_reports.map(pr => (
+ <>
+
+
+ >
+ ))}
+
+ ),
+ none: <>>,
+ });
}
handlePageChange(page: number) {
@@ -346,47 +372,61 @@ export class Reports extends Component {
static fetchInitialData(req: InitialFetchRequest): Promise[] {
let promises: Promise[] = [];
- let commentReportsForm: ListCommentReports = {
+ let unresolved_only = Some(true);
+ let page = Some(1);
+ let limit = Some(fetchLimit);
+ let community_id = None;
+ let auth = req.auth.unwrap();
+
+ let commentReportsForm = new ListCommentReports({
// TODO community_id
- unresolved_only: true,
- page: 1,
- limit: fetchLimit,
- auth: req.auth,
- };
+ unresolved_only,
+ community_id,
+ page,
+ limit,
+ auth,
+ });
promises.push(req.client.listCommentReports(commentReportsForm));
- let postReportsForm: ListPostReports = {
+ let postReportsForm = new ListPostReports({
// TODO community_id
- unresolved_only: true,
- page: 1,
- limit: fetchLimit,
- auth: req.auth,
- };
+ unresolved_only,
+ community_id,
+ page,
+ limit,
+ auth,
+ });
promises.push(req.client.listPostReports(postReportsForm));
return promises;
}
refetch() {
- let unresolved_only = this.state.unreadOrAll == UnreadOrAll.Unread;
- let commentReportsForm: ListCommentReports = {
- // TODO community_id
+ let unresolved_only = Some(this.state.unreadOrAll == UnreadOrAll.Unread);
+ let community_id = None;
+ let page = Some(this.state.page);
+ let limit = Some(fetchLimit);
+
+ let commentReportsForm = new ListCommentReports({
unresolved_only,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(),
- };
+ // TODO community_id
+ community_id,
+ page,
+ limit,
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(
wsClient.listCommentReports(commentReportsForm)
);
- let postReportsForm: ListPostReports = {
- // TODO community_id
+ let postReportsForm = new ListPostReports({
unresolved_only,
- page: this.state.page,
- limit: fetchLimit,
- auth: authField(),
- };
+ // TODO community_id
+ community_id,
+ page,
+ limit,
+ auth: auth().unwrap(),
+ });
WebSocketService.Instance.send(wsClient.listPostReports(postReportsForm));
}
@@ -399,8 +439,11 @@ export class Reports extends Component {
} else if (msg.reconnect) {
this.refetch();
} else if (op == UserOperation.ListCommentReports) {
- let data = wsJsonToRes(msg).data;
- this.state.commentReports = data.comment_reports;
+ let data = wsJsonToRes(
+ msg,
+ ListCommentReportsResponse
+ );
+ this.state.listCommentReportsResponse = Some(data);
this.state.combined = this.buildCombined();
this.state.loading = false;
// this.sendUnreadCount();
@@ -408,8 +451,11 @@ export class Reports extends Component {
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.ListPostReports) {
- let data = wsJsonToRes(msg).data;
- this.state.postReports = data.post_reports;
+ let data = wsJsonToRes(
+ msg,
+ ListPostReportsResponse
+ );
+ this.state.listPostReportsResponse = Some(data);
this.state.combined = this.buildCombined();
this.state.loading = false;
// this.sendUnreadCount();
@@ -417,8 +463,11 @@ export class Reports extends Component {
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.ResolvePostReport) {
- let data = wsJsonToRes(msg).data;
- updatePostReportRes(data.post_report_view, this.state.postReports);
+ let data = wsJsonToRes(msg, PostReportResponse);
+ updatePostReportRes(
+ data.post_report_view,
+ this.state.listPostReportsResponse.map(r => r.post_reports).unwrapOr([])
+ );
let urcs = UserService.Instance.unreadReportCountSub;
if (data.post_report_view.post_report.resolved) {
urcs.next(urcs.getValue() - 1);
@@ -427,10 +476,12 @@ export class Reports extends Component {
}
this.setState(this.state);
} else if (op == UserOperation.ResolveCommentReport) {
- let data = wsJsonToRes(msg).data;
+ let data = wsJsonToRes(msg, CommentReportResponse);
updateCommentReportRes(
data.comment_report_view,
- this.state.commentReports
+ this.state.listCommentReportsResponse
+ .map(r => r.comment_reports)
+ .unwrapOr([])
);
let urcs = UserService.Instance.unreadReportCountSub;
if (data.comment_report_view.comment_report.resolved) {
diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx
index 0884085a..532d9220 100644
--- a/src/shared/components/person/settings.tsx
+++ b/src/shared/components/person/settings.tsx
@@ -1,3 +1,4 @@
+import { None, Option, Some } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import {
BlockCommunity,
@@ -15,19 +16,23 @@ import {
PersonViewSafe,
SaveUserSettings,
SortType,
+ toUndefined,
UserOperation,
+ wsJsonToRes,
+ wsUserOp,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import { i18n, languages } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import {
- authField,
+ auth,
capitalizeFirstLetter,
choicesConfig,
communitySelectName,
communityToChoice,
debounce,
elementUrl,
+ enableNsfw,
fetchCommunities,
fetchThemeList,
fetchUsers,
@@ -44,9 +49,7 @@ import {
updateCommunityBlock,
updatePersonBlock,
wsClient,
- wsJsonToRes,
wsSubscribe,
- wsUserOp,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Icon, Spinner } from "../common/icon";
@@ -65,20 +68,19 @@ if (isBrowser()) {
interface SettingsState {
saveUserSettingsForm: SaveUserSettings;
changePasswordForm: ChangePassword;
- saveUserSettingsLoading: boolean;
- changePasswordLoading: boolean;
- deleteAccountLoading: boolean;
- deleteAccountShowConfirm: boolean;
deleteAccountForm: DeleteAccount;
personBlocks: PersonBlockView[];
- blockPersonId: number;
- blockPerson?: PersonViewSafe;
+ blockPerson: Option;
communityBlocks: CommunityBlockView[];
blockCommunityId: number;
blockCommunity?: CommunityView;
currentTab: string;
- siteRes: GetSiteResponse;
themeList: string[];
+ saveUserSettingsLoading: boolean;
+ changePasswordLoading: boolean;
+ deleteAccountLoading: boolean;
+ deleteAccountShowConfirm: boolean;
+ siteRes: GetSiteResponse;
}
export class Settings extends Component {
@@ -87,25 +89,43 @@ export class Settings extends Component {
private blockCommunityChoices: any;
private subscription: Subscription;
private emptyState: SettingsState = {
- saveUserSettingsForm: {
- auth: authField(false),
- },
- changePasswordForm: {
- new_password: null,
- new_password_verify: null,
- old_password: null,
- auth: authField(false),
- },
- saveUserSettingsLoading: null,
+ saveUserSettingsForm: new SaveUserSettings({
+ show_nsfw: None,
+ show_scores: None,
+ show_avatars: None,
+ show_read_posts: None,
+ show_bot_accounts: None,
+ show_new_post_notifs: None,
+ default_sort_type: None,
+ default_listing_type: None,
+ theme: None,
+ lang: None,
+ avatar: None,
+ banner: None,
+ display_name: None,
+ email: None,
+ bio: None,
+ matrix_user_id: None,
+ send_notifications_to_email: None,
+ bot_account: None,
+ auth: undefined,
+ }),
+ changePasswordForm: new ChangePassword({
+ new_password: undefined,
+ new_password_verify: undefined,
+ old_password: undefined,
+ auth: undefined,
+ }),
+ saveUserSettingsLoading: false,
changePasswordLoading: false,
- deleteAccountLoading: null,
+ deleteAccountLoading: false,
deleteAccountShowConfirm: false,
- deleteAccountForm: {
- password: null,
- auth: authField(false),
- },
+ deleteAccountForm: new DeleteAccount({
+ password: undefined,
+ auth: undefined,
+ }),
personBlocks: [],
- blockPersonId: 0,
+ blockPerson: None,
communityBlocks: [],
blockCommunityId: 0,
currentTab: "settings",
@@ -154,7 +174,7 @@ export class Settings extends Component {
|