DEV: Minor refactor of chat models (#27467)

* remove default prop values where they're being set in constructor
* replace some `||` operators in constructors with `??` so the fallback boolean values are actually used
This commit is contained in:
Jarek Radosz 2024-06-13 17:07:31 +02:00 committed by GitHub
parent b289351a3f
commit fe00796027
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 62 additions and 66 deletions

View File

@ -59,17 +59,17 @@ export default class ChatChannel {
@tracked slug; @tracked slug;
@tracked description; @tracked description;
@tracked status; @tracked status;
@tracked activeThread = null; @tracked activeThread;
@tracked meta; @tracked meta;
@tracked chatableId; @tracked chatableId;
@tracked chatableType; @tracked chatableType;
@tracked chatableUrl; @tracked chatableUrl;
@tracked autoJoinUsers = false; @tracked autoJoinUsers;
@tracked allowChannelWideMentions = true; @tracked allowChannelWideMentions;
@tracked membershipsCount = 0; @tracked membershipsCount;
@tracked archive; @tracked archive;
@tracked tracking; @tracked tracking;
@tracked threadingEnabled = false; @tracked threadingEnabled;
@tracked draft; @tracked draft;
threadsManager = new ChatThreadsManager(getOwnerWithFallback(this)); threadsManager = new ChatThreadsManager(getOwnerWithFallback(this));
@ -91,16 +91,16 @@ export default class ChatChannel {
this.threadingEnabled = args.threading_enabled; this.threadingEnabled = args.threading_enabled;
this.autoJoinUsers = args.auto_join_users; this.autoJoinUsers = args.auto_join_users;
this.allowChannelWideMentions = args.allow_channel_wide_mentions; this.allowChannelWideMentions = args.allow_channel_wide_mentions;
this.chatable = this.#initChatable(args.chatable || []);
this.currentUserMembership = args.current_user_membership; this.currentUserMembership = args.current_user_membership;
this.lastMessage = args.last_message;
this.meta = args.meta;
this.chatable = this.#initChatable(args.chatable ?? []);
this.tracking = new ChatTrackingState(getOwnerWithFallback(this));
if (args.archive_completed || args.archive_failed) { if (args.archive_completed || args.archive_failed) {
this.archive = ChatChannelArchive.create(args); this.archive = ChatChannelArchive.create(args);
} }
this.tracking = new ChatTrackingState(getOwnerWithFallback(this));
this.lastMessage = args.last_message;
this.meta = args.meta;
} }
get unreadThreadsCountSinceLastViewed() { get unreadThreadsCountSinceLastViewed() {

View File

@ -7,8 +7,8 @@ export default class ChatDirectMessage {
return new ChatDirectMessage(args); return new ChatDirectMessage(args);
} }
@tracked users = null; @tracked users;
@tracked group = false; @tracked group;
type = CHATABLE_TYPES.directMessageChannel; type = CHATABLE_TYPES.directMessageChannel;

View File

@ -9,16 +9,16 @@ export default class ChatMessageReaction {
return new ChatMessageReaction(args); return new ChatMessageReaction(args);
} }
@tracked count = 0; @tracked count;
@tracked reacted = false; @tracked reacted;
@tracked users = []; @tracked users;
@tracked emoji; @tracked emoji;
constructor(args = {}) { constructor(args = {}) {
this.count = args.count; this.count = args.count;
this.emoji = args.emoji; this.emoji = args.emoji;
this.users = this.#initUsersModels(args.users);
this.reacted = args.reacted; this.reacted = args.reacted;
this.users = this.#initUsersModels(args.users);
} }
#initUsersModels(users = []) { #initUsersModels(users = []) {

View File

@ -1,5 +1,5 @@
import { cached, tracked } from "@glimmer/tracking"; import { cached, tracked } from "@glimmer/tracking";
import { TrackedArray, TrackedObject } from "@ember-compat/tracked-built-ins"; import { TrackedArray } from "@ember-compat/tracked-built-ins";
import { generateCookFunction, parseMentions } from "discourse/lib/text"; import { generateCookFunction, parseMentions } from "discourse/lib/text";
import Bookmark from "discourse/models/bookmark"; import Bookmark from "discourse/models/bookmark";
import User from "discourse/models/user"; import User from "discourse/models/user";
@ -25,7 +25,7 @@ export default class ChatMessage {
@tracked selected; @tracked selected;
@tracked channel; @tracked channel;
@tracked staged; @tracked staged;
@tracked processed = true; @tracked processed;
@tracked draftSaved; @tracked draftSaved;
@tracked draft; @tracked draft;
@tracked createdAt; @tracked createdAt;
@ -35,14 +35,14 @@ export default class ChatMessage {
@tracked reviewableId; @tracked reviewableId;
@tracked user; @tracked user;
@tracked inReplyTo; @tracked inReplyTo;
@tracked expanded = true; @tracked expanded;
@tracked bookmark; @tracked bookmark;
@tracked userFlagStatus; @tracked userFlagStatus;
@tracked hidden; @tracked hidden;
@tracked version = 0; @tracked version = 0;
@tracked edited; @tracked edited;
@tracked editing; @tracked editing;
@tracked chatWebhookEvent = new TrackedObject(); @tracked chatWebhookEvent;
@tracked mentionWarning; @tracked mentionWarning;
@tracked availableFlags; @tracked availableFlags;
@tracked newest; @tracked newest;
@ -51,7 +51,7 @@ export default class ChatMessage {
@tracked message; @tracked message;
@tracked manager; @tracked manager;
@tracked deletedById; @tracked deletedById;
@tracked streaming = false; @tracked streaming;
@tracked _deletedAt; @tracked _deletedAt;
@tracked _cooked; @tracked _cooked;
@ -62,33 +62,33 @@ export default class ChatMessage {
this.channel = channel; this.channel = channel;
this.streaming = args.streaming; this.streaming = args.streaming;
this.manager = args.manager; this.manager = args.manager;
this.newest = args.newest || false; this.newest = args.newest ?? false;
this.draftSaved = args.draftSaved || args.draft_saved || false; this.draftSaved = args.draftSaved ?? args.draft_saved ?? false;
this.firstOfResults = args.firstOfResults || args.first_of_results || false; this.firstOfResults = args.firstOfResults ?? args.first_of_results ?? false;
this.staged = args.staged || false; this.staged = args.staged ?? false;
this.processed = args.processed || true; this.processed = args.processed ?? true;
this.edited = args.edited || false; this.edited = args.edited ?? false;
this.editing = args.editing || false; this.editing = args.editing ?? false;
this.availableFlags = args.availableFlags || args.available_flags; this.availableFlags = args.availableFlags ?? args.available_flags;
this.hidden = args.hidden || false; this.hidden = args.hidden ?? false;
this.chatWebhookEvent = args.chatWebhookEvent || args.chat_webhook_event; this.chatWebhookEvent = args.chatWebhookEvent ?? args.chat_webhook_event;
this.createdAt = args.created_at this.createdAt = args.created_at
? new Date(args.created_at) ? new Date(args.created_at)
: new Date(args.createdAt); : new Date(args.createdAt);
this.deletedById = args.deletedById || args.deleted_by_id; this.deletedById = args.deletedById || args.deleted_by_id;
this._deletedAt = args.deletedAt || args.deleted_at; this._deletedAt = args.deletedAt || args.deleted_at;
this.expanded = this.expanded =
this.hidden || this._deletedAt ? false : args.expanded || true; this.hidden || this._deletedAt ? false : args.expanded ?? true;
this.excerpt = args.excerpt; this.excerpt = args.excerpt;
this.reviewableId = args.reviewableId || args.reviewable_id; this.reviewableId = args.reviewableId ?? args.reviewable_id;
this.userFlagStatus = args.userFlagStatus || args.user_flag_status; this.userFlagStatus = args.userFlagStatus ?? args.user_flag_status;
this.draft = args.draft; this.draft = args.draft;
this.message = args.message || ""; this.message = args.message ?? "";
this._cooked = args.cooked || ""; this._cooked = args.cooked ?? "";
this.inReplyTo = this.inReplyTo =
args.inReplyTo || args.inReplyTo ??
(args.in_reply_to || args.replyToMsg (args.in_reply_to ?? args.replyToMsg
? ChatMessage.create(channel, args.in_reply_to || args.replyToMsg) ? ChatMessage.create(channel, args.in_reply_to ?? args.replyToMsg)
: null); : null);
this.reactions = this.#initChatMessageReactionModel(args.reactions); this.reactions = this.#initChatMessageReactionModel(args.reactions);
this.uploads = new TrackedArray(args.uploads || []); this.uploads = new TrackedArray(args.uploads || []);

View File

@ -13,12 +13,8 @@ export default class ChatThreadPreview {
@tracked participantCount; @tracked participantCount;
@tracked participantUsers; @tracked participantUsers;
constructor(args = {}) { constructor(args) {
if (!args) { this.update(args || {});
args = {};
}
this.update(args);
} }
get otherParticipantCount() { get otherParticipantCount() {
@ -26,24 +22,24 @@ export default class ChatThreadPreview {
} }
update(args = {}) { update(args = {}) {
this.replyCount = args.reply_count || args.replyCount || 0; this.replyCount = args.reply_count ?? args.replyCount ?? 0;
this.lastReplyId = args.last_reply_id || args.lastReplyId; this.lastReplyId = args.last_reply_id ?? args.lastReplyId;
this.lastReplyCreatedAt = new Date( this.lastReplyCreatedAt = new Date(
args.last_reply_created_at || args.lastReplyCreatedAt args.last_reply_created_at ?? args.lastReplyCreatedAt
); );
this.lastReplyExcerpt = args.last_reply_excerpt || args.lastReplyExcerpt; this.lastReplyExcerpt = args.last_reply_excerpt ?? args.lastReplyExcerpt;
this.participantCount = this.participantCount =
args.participant_count || args.participantCount || 0; args.participant_count ?? args.participantCount ?? 0;
// cheap trick to avoid avatars flickering // cheap trick to avoid avatars flickering
const lastReplyUser = args.last_reply_user || args.lastReplyUser; const lastReplyUser = args.last_reply_user ?? args.lastReplyUser;
if (lastReplyUser?.id !== this.lastReplyUser?.id) { if (lastReplyUser?.id !== this.lastReplyUser?.id) {
this.lastReplyUser = lastReplyUser; this.lastReplyUser = lastReplyUser;
} }
// cheap trick to avoid avatars flickering // cheap trick to avoid avatars flickering
const participantUsers = const participantUsers =
args.participant_users || args.participantUsers || []; args.participant_users ?? args.participantUsers ?? [];
if ( if (
participantUsers?.map((u) => u.id).join(",") !== participantUsers?.map((u) => u.id).join(",") !==
this.participantUsers?.map((u) => u.id).join(",") this.participantUsers?.map((u) => u.id).join(",")

View File

@ -5,13 +5,13 @@ import { service } from "@ember/service";
export default class ChatTrackingState { export default class ChatTrackingState {
@service chatTrackingStateManager; @service chatTrackingStateManager;
@tracked _unreadCount = 0; @tracked _unreadCount;
@tracked _mentionCount = 0; @tracked _mentionCount;
constructor(owner, params = {}) { constructor(owner, params = {}) {
setOwner(this, owner); setOwner(this, owner);
this._unreadCount = params.unreadCount || 0; this._unreadCount = params.unreadCount ?? 0;
this._mentionCount = params.mentionCount || 0; this._mentionCount = params.mentionCount ?? 0;
} }
reset() { reset() {

View File

@ -6,13 +6,13 @@ export default class UserChatChannelMembership {
return new UserChatChannelMembership(args); return new UserChatChannelMembership(args);
} }
@tracked following = false; @tracked following;
@tracked muted = false; @tracked muted;
@tracked desktopNotificationLevel = null; @tracked desktopNotificationLevel;
@tracked mobileNotificationLevel = null; @tracked mobileNotificationLevel;
@tracked lastReadMessageId = null; @tracked lastReadMessageId;
@tracked lastViewedAt = null; @tracked lastViewedAt;
@tracked user = null; @tracked user;
constructor(args = {}) { constructor(args = {}) {
this.following = args.following; this.following = args.following;

View File

@ -6,9 +6,9 @@ export default class UserChatThreadMembership {
return new UserChatThreadMembership(args); return new UserChatThreadMembership(args);
} }
@tracked lastReadMessageId = null; @tracked lastReadMessageId;
@tracked notificationLevel = null; @tracked notificationLevel;
@tracked threadTitlePromptSeen = null; @tracked threadTitlePromptSeen;
constructor(args = {}) { constructor(args = {}) {
this.lastReadMessageId = args.last_read_message_id; this.lastReadMessageId = args.last_read_message_id;