db: Pool<ConnectionManager<PgConnection>>,
}
-// TODO show online users for communities too
-// TODO GetPosts is the community / front page join.
-// What is sent: New posts, post edits, post removes, post likes, community edits, community mod adds. Notifs for new posts?
-// GetPost is the PostJoin, LeavePost is the leave
-// What is sent: New comments, comment edits, comment likes
-// UserJoin is the user join, a disconnect should remove you from all the scopes
impl ChatServer {
pub fn startup(db: Pool<ConnectionManager<PgConnection>>) -> ChatServer {
ChatServer {
let res_str = to_json_string(&user_operation, &res)?;
// Don't send my data with it
- let mut post_sent = res.clone();
+ let mut post_sent = res;
post_sent.post.my_vote = None;
post_sent.post.user_id = None;
let post_sent_str = to_json_string(&user_operation, &post_sent)?;
let res_str = to_json_string(&user_operation, &res)?;
// Don't send my data with it
- let mut post_sent = res.clone();
+ let mut post_sent = res;
post_sent.post.my_vote = None;
post_sent.post.user_id = None;
let post_sent_str = to_json_string(&user_operation, &post_sent)?;
this.sendUnreadCount();
this.setState(this.state);
} else if (res.op == UserOperation.CreateComment) {
- // let res: CommentResponse = msg;
- // TODO gotta remove this
- toast(i18n.t('reply_sent'));
- // this.state.replies.unshift(res.comment); // TODO do this right
- // this.setState(this.state);
+ let data = res.data as CommentResponse;
+
+ if (data.recipient_ids.includes(UserService.Instance.user.id)) {
+ this.state.replies.unshift(data.comment);
+ this.setState(this.state);
+ } else if (data.comment.creator_id == UserService.Instance.user.id) {
+ toast(i18n.t('reply_sent'));
+ }
+ this.setState(this.state);
+ } else if (res.op == UserOperation.CreatePrivateMessage) {
+ let data = res.data as PrivateMessageResponse;
+
+ if (data.message.recipient_id == UserService.Instance.user.id) {
+ this.state.messages.unshift(data.message);
+ this.setState(this.state);
+ } else if (data.message.creator_id == UserService.Instance.user.id) {
+ toast(i18n.t('message_sent'));
+ }
+ this.setState(this.state);
} else if (res.op == UserOperation.SaveComment) {
let data = res.data as CommentResponse;
let found = this.state.replies.find(c => c.id == data.comment.id);
Comment,
CommentResponse,
PrivateMessage,
+ PrivateMessageResponse,
WebSocketJsonResponse,
} from '../interfaces';
import {
replies: Array<Comment>;
mentions: Array<Comment>;
messages: Array<PrivateMessage>;
- fetchCount: number;
unreadCount: number;
siteName: string;
}
emptyState: NavbarState = {
isLoggedIn: UserService.Instance.user !== undefined,
unreadCount: 0,
- fetchCount: 0,
replies: [],
mentions: [],
messages: [],
super(props, context);
this.state = this.emptyState;
- this.fetchUnreads();
-
// Subscribe to user changes
this.userSub = UserService.Instance.sub.subscribe(user => {
this.state.isLoggedIn = user.user !== undefined;
if (this.state.isLoggedIn) {
this.requestNotificationPermission();
+ // TODO couldn't get re-logging in to re-fetch unreads
+ this.fetchUnreads();
}
WebSocketService.Instance.getSite();
let unreadReplies = data.replies.filter(r => !r.read);
this.state.replies = unreadReplies;
+ this.state.unreadCount = this.calculateUnreadCount();
this.setState(this.state);
this.sendUnreadCount();
} else if (res.op == UserOperation.GetUserMentions) {
let unreadMentions = data.mentions.filter(r => !r.read);
this.state.mentions = unreadMentions;
+ this.state.unreadCount = this.calculateUnreadCount();
this.setState(this.state);
this.sendUnreadCount();
} else if (res.op == UserOperation.GetPrivateMessages) {
let unreadMessages = data.messages.filter(r => !r.read);
this.state.messages = unreadMessages;
+ this.state.unreadCount = this.calculateUnreadCount();
this.setState(this.state);
this.sendUnreadCount();
} else if (res.op == UserOperation.CreateComment) {
- // TODO do private messages too
let data = res.data as CommentResponse;
- if (UserService.Instance.user) {
+ if (this.state.isLoggedIn) {
if (data.recipient_ids.includes(UserService.Instance.user.id)) {
+ this.state.replies.push(data.comment);
+ this.state.unreadCount++;
+ this.setState(this.state);
+ this.sendUnreadCount();
this.notify(data.comment);
}
}
+ } else if (res.op == UserOperation.CreatePrivateMessage) {
+ let data = res.data as PrivateMessageResponse;
+
+ if (this.state.isLoggedIn) {
+ if (data.message.recipient_id == UserService.Instance.user.id) {
+ this.state.messages.push(data.message);
+ this.state.unreadCount++;
+ this.setState(this.state);
+ this.sendUnreadCount();
+ this.notify(data.message);
+ }
+ }
} else if (res.op == UserOperation.GetSite) {
let data = res.data as GetSiteResponse;
WebSocketService.Instance.getReplies(repliesForm);
WebSocketService.Instance.getUserMentions(userMentionsForm);
WebSocketService.Instance.getPrivateMessages(privateMessagesForm);
- this.state.fetchCount++;
}
}
}
sendUnreadCount() {
UserService.Instance.sub.next({
user: UserService.Instance.user,
- unreadCount: this.unreadCount,
+ unreadCount: this.state.unreadCount,
});
}
- get unreadCount() {
+ calculateUnreadCount(): number {
return (
this.state.replies.filter(r => !r.read).length +
this.state.mentions.filter(r => !r.read).length +
notify(reply: Comment | PrivateMessage) {
if (Notification.permission !== 'granted') Notification.requestPermission();
else {
- var notification = new Notification(
- `${this.state.unreadCount} ${i18n.t('unread_messages')}`,
- {
- icon: reply.creator_avatar
- ? reply.creator_avatar
- : `${window.location.protocol}//${window.location.host}/static/assets/apple-touch-icon.png`,
- body: `${reply.creator_name}: ${reply.content}`,
- }
- );
+ var notification = new Notification(reply.creator_name, {
+ icon: reply.creator_avatar
+ ? reply.creator_avatar
+ : `${window.location.protocol}//${window.location.host}/static/assets/apple-touch-icon.png`,
+ body: `${reply.content}`,
+ });
notification.onclick = () => {
this.context.router.history.push(