discourse/plugins/chat
Alan Guo Xiang Tan 80d289eb8b
PERF: Add index for chat unread counts query (#19516)
This commit adds an index for the query which the chat plugin executes
multiple times when preloading user data in `Chat::ChatChannelFetcher.unread_counts`.

Sample query plan from a query I grabbed from one of our production
instance.

Before:

```
                                                                                        QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=10.77..696.67 rows=7 width=16) (actual time=7.735..7.736 rows=0 loops=1)
   Group Key: cc.id
   ->  Nested Loop  (cost=10.77..696.54 rows=12 width=8) (actual time=7.734..7.735 rows=0 loops=1)
         Join Filter: (cc.id = cm.chat_channel_id)
         ->  Nested Loop  (cost=0.56..76.44 rows=1 width=16) (actual time=0.011..0.037 rows=7 loops=1)
               ->  Index Only Scan using chat_channels_pkey on chat_channels cc  (cost=0.28..22.08 rows=7 width=8) (actual time=0.004..0.014 rows=7 loops=1)
                     Index Cond: (id = ANY ('{192,300,228,727,8,612,1633}'::bigint[]))
                     Heap Fetches: 0
               ->  Index Scan using user_chat_channel_unique_memberships on user_chat_channel_memberships uccm  (cost=0.28..7.73 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=7)
                     Index Cond: ((user_id = 1338) AND (chat_channel_id = cc.id))
         ->  Bitmap Heap Scan on chat_messages cm  (cost=10.21..618.98 rows=89 width=12) (actual time=1.096..1.097 rows=0 loops=7)
               Recheck Cond: (chat_channel_id = uccm.chat_channel_id)
               Filter: ((deleted_at IS NULL) AND (user_id <> 1338) AND (id > COALESCE(uccm.last_read_message_id, 0)))
               Rows Removed by Filter: 2085
               Heap Blocks: exact=7106
               ->  Bitmap Index Scan on index_chat_messages_on_chat_channel_id_and_created_at  (cost=0.00..10.19 rows=270 width=0) (actual time=0.114..0.114 rows=2085 loops=7)
                     Index Cond: (chat_channel_id = uccm.chat_channel_id)
 Planning Time: 0.408 ms
 Execution Time: 7.762 ms
(19 rows)
```

After:

```
                                                                                        QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=5.84..367.39 rows=7 width=16) (actual time=0.130..0.131 rows=0 loops=1)
   Group Key: cc.id
   ->  Nested Loop  (cost=5.84..367.26 rows=12 width=8) (actual time=0.129..0.130 rows=0 loops=1)
         Join Filter: (cc.id = cm.chat_channel_id)
         ->  Nested Loop  (cost=0.56..76.44 rows=1 width=16) (actual time=0.038..0.069 rows=7 loops=1)
               ->  Index Only Scan using chat_channels_pkey on chat_channels cc  (cost=0.28..22.08 rows=7 width=8) (actual time=0.011..0.022 rows=7 loops=1)
                     Index Cond: (id = ANY ('{192,300,228,727,8,612,1633}'::bigint[]))
                     Heap Fetches: 0
               ->  Index Scan using user_chat_channel_unique_memberships on user_chat_channel_memberships uccm  (cost=0.28..7.73 rows=1 width=8) (actual time=0.006..0.006 rows=1 loops=7)
                     Index Cond: ((user_id = 1338) AND (chat_channel_id = cc.id))
         ->  Bitmap Heap Scan on chat_messages cm  (cost=5.28..289.71 rows=89 width=12) (actual time=0.008..0.008 rows=0 loops=7)
               Recheck Cond: ((chat_channel_id = uccm.chat_channel_id) AND (id > COALESCE(uccm.last_read_message_id, 0)) AND (deleted_at IS NULL))
               Filter: (user_id <> 1338)
               ->  Bitmap Index Scan on index_chat_messages_on_chat_channel_id_and_id  (cost=0.00..5.26 rows=90 width=0) (actual time=0.008..0.008 rows=0 loops=7)
                     Index Cond: ((chat_channel_id = uccm.chat_channel_id) AND (id > COALESCE(uccm.last_read_message_id, 0)))
 Planning Time: 1.217 ms
 Execution Time: 0.188 ms
(17 rows)
```
2022-12-20 05:10:53 +08:00
..
app PERF: Add index for chat unread counts query (#19516) 2022-12-20 05:10:53 +08:00
assets FIX: box-sizing for avatars (#19517) 2022-12-19 13:43:18 +01:00
config FEATURE: Chat and Sidebar are now on by default (#19406) 2022-12-13 17:25:19 -07:00
db PERF: Add index for chat unread counts query (#19516) 2022-12-20 05:10:53 +08:00
lib DEV: Change HashtagAutocompleteService to use DiscoursePluginRegistry (#19491) 2022-12-19 13:46:17 +10:00
public
spec UX: Hashtag autocomplete styling (#19426) 2022-12-19 12:31:45 +01:00
test/javascripts DEV: Update @ember/test-helpers and fix chat test (#19495) 2022-12-19 11:23:31 +01:00
plugin.rb PERF: Remove unnecessary DB queries when preloading for chat (#19514) 2022-12-19 15:34:07 +08:00
README.md

⚠️ This plugin is still in active development and may change frequently

Documentation

The Discourse Chat plugin adds chat functionality to your Discourse so it can natively support both long-form and short-form communication needs of your online community.

For documentation, see Discourse Chat

Plugin API

registerChatComposerButton

Usage

api.registerChatComposerButton({ id: "foo", ... });

Options

Every option accepts a value or a function, when passing a function this will be the chat-composer component instance. Example of an option using a function:

api.registerChatComposerButton({
  id: "foo",
  displayed() {
    return this.site.mobileView && this.canAttachUploads;
  },
});
Required
  • id unique, used to identify your button, eg: "gifs"
  • action callback when the button is pressed, can be an action name or an anonymous function, eg: "onFooClicked" or () => { console.log("clicked") }

A button requires at least an icon or a label:

  • icon, eg: "times"
  • label, text displayed on the button, a translatable key, eg: "foo.bar"
  • translatedLabel, text displayed on the button, a string, eg: "Add gifs"
Optional
  • position, can be "inline" or "dropdown", defaults to "inline"
  • title, title attribute of the button, a translatable key, eg: "foo.bar"
  • translatedTitle, title attribute of the button, a string, eg: "Add gifs"
  • ariaLabel, aria-label attribute of the button, a translatable key, eg: "foo.bar"
  • translatedAriaLabel, aria-label attribute of the button, a string, eg: "Add gifs"
  • classNames, additional names to add to the buttons class attribute, eg: ["foo", "bar"]
  • displayed, hide/or show the button, expects a boolean
  • disabled, sets the disabled attribute on the button, expects a boolean
  • priority, an integer defining the order of the buttons, higher comes first, eg: 700
  • dependentKeys, list of property names which should trigger a refresh of the buttons when changed, eg: ["foo.bar", "bar.baz"]