2024-01-02 06:29:20 +01:00
|
|
|
import assert from "minimalistic-assert";
|
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
import * as buddy_data from "./buddy_data.ts";
|
|
|
|
import * as hash_util from "./hash_util.ts";
|
|
|
|
import * as narrow_state from "./narrow_state.ts";
|
|
|
|
import * as people from "./people.ts";
|
|
|
|
import * as pm_conversations from "./pm_conversations.ts";
|
|
|
|
import * as unread from "./unread.ts";
|
|
|
|
import * as user_status from "./user_status.ts";
|
|
|
|
import type {UserStatusEmojiInfo} from "./user_status.ts";
|
2022-03-15 17:45:35 +01:00
|
|
|
|
2022-03-27 16:21:49 +02:00
|
|
|
// Maximum number of conversation threads to show in default view.
|
2023-02-10 20:03:06 +01:00
|
|
|
const max_conversations_to_show = 8;
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
// Maximum number of conversation threads to show in default view with unreads.
|
2023-02-10 20:03:06 +01:00
|
|
|
const max_conversations_to_show_with_unreads = 15;
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2024-01-02 06:29:20 +01:00
|
|
|
export function get_active_user_ids_string(): string | undefined {
|
2022-03-15 17:45:35 +01:00
|
|
|
const filter = narrow_state.filter();
|
|
|
|
|
|
|
|
if (!filter) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-04-11 21:04:33 +02:00
|
|
|
const emails = filter.operands("dm")[0];
|
2022-03-15 17:45:35 +01:00
|
|
|
|
|
|
|
if (!emails) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:28:37 +02:00
|
|
|
const users_ids_array = people.emails_strings_to_user_ids_array(emails);
|
|
|
|
if (!users_ids_array || users_ids_array.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return people.sorted_other_user_ids(users_ids_array).join(",");
|
2022-03-15 17:45:35 +01:00
|
|
|
}
|
|
|
|
|
2024-01-02 06:29:20 +01:00
|
|
|
type DisplayObject = {
|
|
|
|
recipients: string;
|
|
|
|
user_ids_string: string;
|
|
|
|
unread: number;
|
|
|
|
is_zero: boolean;
|
|
|
|
is_active: boolean;
|
|
|
|
url: string;
|
2024-05-16 05:05:29 +02:00
|
|
|
status_emoji_info: UserStatusEmojiInfo | undefined;
|
|
|
|
user_circle_class: string | undefined;
|
2024-01-02 06:29:20 +01:00
|
|
|
is_group: boolean;
|
|
|
|
is_bot: boolean;
|
|
|
|
};
|
|
|
|
|
2024-06-06 10:35:51 +02:00
|
|
|
export function get_conversations(search_string = ""): DisplayObject[] {
|
2024-06-07 04:44:29 +02:00
|
|
|
const conversations = pm_conversations.recent.get();
|
2022-04-07 00:53:27 +02:00
|
|
|
const display_objects = [];
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
// The user_ids_string for the current view, if any.
|
2022-03-15 17:45:35 +01:00
|
|
|
const active_user_ids_string = get_active_user_ids_string();
|
|
|
|
|
2024-03-05 23:25:05 +01:00
|
|
|
if (
|
|
|
|
active_user_ids_string !== undefined &&
|
2024-06-07 04:44:29 +02:00
|
|
|
!conversations
|
|
|
|
.map((conversation) => conversation.user_ids_string)
|
|
|
|
.includes(active_user_ids_string)
|
2024-03-05 23:25:05 +01:00
|
|
|
) {
|
2024-06-07 04:44:29 +02:00
|
|
|
conversations.unshift({user_ids_string: active_user_ids_string, max_message_id: -1});
|
2024-03-05 23:25:05 +01:00
|
|
|
}
|
|
|
|
|
2024-06-07 04:44:29 +02:00
|
|
|
for (const conversation of conversations) {
|
2022-03-27 16:21:49 +02:00
|
|
|
const user_ids_string = conversation.user_ids_string;
|
2024-06-06 10:35:51 +02:00
|
|
|
|
|
|
|
const users = people.get_users_from_ids(
|
|
|
|
people.user_ids_string_to_ids_array(user_ids_string),
|
|
|
|
);
|
|
|
|
if (!people.dm_matches_search_string(users, search_string)) {
|
|
|
|
// Skip adding the conversation to the display_objects array if it does
|
|
|
|
// not match the search_term.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-15 17:45:35 +01:00
|
|
|
const reply_to = people.user_ids_string_to_emails_string(user_ids_string);
|
2024-01-02 06:29:20 +01:00
|
|
|
assert(reply_to !== undefined);
|
2022-03-15 17:45:35 +01:00
|
|
|
const recipients_string = people.get_recipients(user_ids_string);
|
|
|
|
|
2022-10-22 07:15:44 +02:00
|
|
|
const num_unread = unread.num_unread_for_user_ids_string(user_ids_string);
|
2022-03-15 17:45:35 +01:00
|
|
|
const is_group = user_ids_string.includes(",");
|
|
|
|
const is_active = user_ids_string === active_user_ids_string;
|
|
|
|
|
|
|
|
let user_circle_class;
|
|
|
|
let status_emoji_info;
|
2023-04-11 14:43:49 +02:00
|
|
|
let is_bot = false;
|
2022-03-15 17:45:35 +01:00
|
|
|
|
|
|
|
if (!is_group) {
|
|
|
|
const user_id = Number.parseInt(user_ids_string, 10);
|
|
|
|
user_circle_class = buddy_data.get_user_circle_class(user_id);
|
|
|
|
const recipient_user_obj = people.get_by_user_id(user_id);
|
|
|
|
|
|
|
|
if (recipient_user_obj.is_bot) {
|
2023-04-11 14:43:49 +02:00
|
|
|
// We display the bot icon rather than a user circle for bots.
|
|
|
|
is_bot = true;
|
2022-03-15 17:45:35 +01:00
|
|
|
} else {
|
|
|
|
status_emoji_info = user_status.get_status_emoji(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 00:53:27 +02:00
|
|
|
const display_object = {
|
2022-03-15 17:45:35 +01:00
|
|
|
recipients: recipients_string,
|
|
|
|
user_ids_string,
|
|
|
|
unread: num_unread,
|
|
|
|
is_zero: num_unread === 0,
|
|
|
|
is_active,
|
|
|
|
url: hash_util.pm_with_url(reply_to),
|
|
|
|
status_emoji_info,
|
|
|
|
user_circle_class,
|
|
|
|
is_group,
|
2023-04-11 14:43:49 +02:00
|
|
|
is_bot,
|
2022-03-15 17:45:35 +01:00
|
|
|
};
|
2022-04-07 00:53:27 +02:00
|
|
|
display_objects.push(display_object);
|
2022-03-15 17:45:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 00:53:27 +02:00
|
|
|
return display_objects;
|
2022-03-15 17:45:35 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 16:21:49 +02:00
|
|
|
// Designed to closely match topic_list_data.get_list_info().
|
2024-06-06 10:35:51 +02:00
|
|
|
export function get_list_info(
|
|
|
|
zoomed: boolean,
|
|
|
|
search_term = "",
|
|
|
|
): {
|
2024-01-02 06:29:20 +01:00
|
|
|
conversations_to_be_shown: DisplayObject[];
|
|
|
|
more_conversations_unread_count: number;
|
|
|
|
} {
|
2024-06-06 10:35:51 +02:00
|
|
|
const conversations = get_conversations(search_term);
|
2022-03-27 16:21:49 +02:00
|
|
|
|
|
|
|
if (zoomed || conversations.length <= max_conversations_to_show) {
|
|
|
|
return {
|
|
|
|
conversations_to_be_shown: conversations,
|
|
|
|
more_conversations_unread_count: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversations_to_be_shown = [];
|
|
|
|
let more_conversations_unread_count = 0;
|
2024-01-02 06:29:20 +01:00
|
|
|
function should_show_conversation(idx: number, conversation: DisplayObject): boolean {
|
2022-07-14 23:33:44 +02:00
|
|
|
// We always show the active conversation; see the similar
|
2024-01-11 08:09:17 +01:00
|
|
|
// comment in topic_list_data.ts.
|
2022-07-14 23:33:44 +02:00
|
|
|
if (conversation.is_active) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2022-07-14 23:33:44 +02:00
|
|
|
// We don't need to filter muted users here, because
|
|
|
|
// pm_conversations.js takes care of this for us.
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2022-07-14 23:33:44 +02:00
|
|
|
// We include the most recent max_conversations_to_show
|
|
|
|
// conversations, regardless of whether they have unread
|
|
|
|
// messages.
|
|
|
|
if (idx < max_conversations_to_show) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-27 16:21:49 +02:00
|
|
|
|
2022-07-14 23:33:44 +02:00
|
|
|
// We include older conversations with unread messages up
|
|
|
|
// until max_conversations_to_show_with_unreads total
|
|
|
|
// topics have been included.
|
|
|
|
if (
|
|
|
|
conversation.unread > 0 &&
|
|
|
|
conversations_to_be_shown.length < max_conversations_to_show_with_unreads
|
|
|
|
) {
|
|
|
|
return true;
|
2022-03-27 16:21:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 23:33:44 +02:00
|
|
|
// Otherwise, this conversation should only be visible in
|
|
|
|
// the unzoomed view.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const [idx, conversation] of conversations.entries()) {
|
|
|
|
if (should_show_conversation(idx, conversation)) {
|
2022-03-27 16:21:49 +02:00
|
|
|
conversations_to_be_shown.push(conversation);
|
|
|
|
} else {
|
|
|
|
more_conversations_unread_count += conversation.unread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
conversations_to_be_shown,
|
|
|
|
more_conversations_unread_count,
|
|
|
|
};
|
|
|
|
}
|