mirror of
				https://github.com/zulip/zulip.git
				synced 2025-10-31 12:03:46 +00:00 
			
		
		
		
	Currently, when there are some old starred messages in a topic, it is possible that some of them won't be unstarred on doing "Unstar all messages in topic". This happens when those messages haven't been fetched yet from the server, and we have no way to verify if they actually belong to the topic. This commit fixes that by changing this mechanism to first fetch all starred messages in the topic from the server, and then send their IDs back to the backend to unstar them. While doing this, we assume that the user does not have more than 1000 starred messages in that topic. Note that, we still depend on the local data to decide whether or not the "Unstar all messages in topic" option should be shown in the topic popover. A method similar to the above cannot be used here, because making server requests before opening the popover could visually slow down the popover opening. Using local data for the topic popover would probably not be a big problem, because users would want to unstar all messages in a topic probably after noticing that there are a lot of them, meaning there was at least one starred message from that topic which was already fetched, which is sufficient for us to conclude that we need to show the option in the topic popover. Fixes #17790
		
			
				
	
	
		
			86 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import * as message_store from "./message_store";
 | |
| import {page_params} from "./page_params";
 | |
| import * as stream_popover from "./stream_popover";
 | |
| import * as top_left_corner from "./top_left_corner";
 | |
| 
 | |
| export const starred_ids = new Set();
 | |
| 
 | |
| export function initialize() {
 | |
|     starred_ids.clear();
 | |
| 
 | |
|     for (const id of page_params.starred_messages) {
 | |
|         starred_ids.add(id);
 | |
|     }
 | |
| 
 | |
|     rerender_ui();
 | |
| }
 | |
| 
 | |
| export function add(ids) {
 | |
|     for (const id of ids) {
 | |
|         starred_ids.add(id);
 | |
|     }
 | |
| 
 | |
|     rerender_ui();
 | |
| }
 | |
| 
 | |
| export function remove(ids) {
 | |
|     for (const id of ids) {
 | |
|         starred_ids.delete(id);
 | |
|     }
 | |
| 
 | |
|     rerender_ui();
 | |
| }
 | |
| 
 | |
| export function get_count() {
 | |
|     return starred_ids.size;
 | |
| }
 | |
| 
 | |
| export function get_starred_msg_ids() {
 | |
|     return Array.from(starred_ids);
 | |
| }
 | |
| 
 | |
| export function get_count_in_topic(stream_id, topic) {
 | |
|     if (stream_id === undefined || topic === undefined) {
 | |
|         return 0;
 | |
|     }
 | |
| 
 | |
|     const messages = Array.from(starred_ids).filter((id) => {
 | |
|         const message = message_store.get(id);
 | |
| 
 | |
|         if (message === undefined) {
 | |
|             // We know the `id` from the initial data fetch from page_params,
 | |
|             // but the message itself hasn't been fetched from the server yet.
 | |
|             // We ignore this message, since we can't check if it belongs to
 | |
|             // the topic, but it could, meaning this implementation isn't
 | |
|             // completely correct.
 | |
|             // Since this function is used only when opening the topic popover,
 | |
|             // and not for actually unstarring messages, this discrepancy is
 | |
|             // probably acceptable. The worst it could manifest as is the topic
 | |
|             // popover not having the "Unstar all messages in topic" option, when
 | |
|             // it should have had.
 | |
|             return false;
 | |
|         }
 | |
| 
 | |
|         return (
 | |
|             message.type === "stream" &&
 | |
|             message.stream_id === stream_id &&
 | |
|             message.topic.toLowerCase() === topic.toLowerCase()
 | |
|         );
 | |
|     });
 | |
| 
 | |
|     return messages.length;
 | |
| }
 | |
| 
 | |
| export function rerender_ui() {
 | |
|     let count = get_count();
 | |
| 
 | |
|     if (!page_params.starred_message_counts) {
 | |
|         // This essentially hides the count
 | |
|         count = 0;
 | |
|     }
 | |
| 
 | |
|     stream_popover.hide_topic_popover();
 | |
|     top_left_corner.update_starred_count(count);
 | |
|     stream_popover.hide_starred_messages_popover();
 | |
| }
 |