Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

read_receipts: Add support for displaying read receipts.

Dinesh48d2783559dd
diff --git a/static/js/people.js b/static/js/people.js
index fd5634d1cd7..88acdbeddb7 100644
--- a/static/js/people.js
+++ b/static/js/people.js
@@ -1362,7 +1362,7 @@ export function is_my_user_id(user_id) {
return user_id === my_user_id;
}
-function compare_by_name(a, b) {
+export function compare_by_name(a, b) {
return util.strcmp(a.full_name, b.full_name);
}
diff --git a/static/js/popovers.js b/static/js/popovers.js
index e9295c0e005..699e5068731 100644
--- a/static/js/popovers.js
+++ b/static/js/popovers.js
@@ -38,6 +38,7 @@ import * as overlays from "./overlays";
import {page_params} from "./page_params";
import * as people from "./people";
import * as popover_menus from "./popover_menus";
+import * as read_receipts from "./read_receipts";
import * as realm_playground from "./realm_playground";
import * as reminder from "./reminder";
import * as resize from "./resize";
@@ -509,6 +510,9 @@ export function toggle_actions_popover(element, id) {
const should_display_delete_option =
message_edit.get_deletability(message) && not_spectator;
+ const should_display_read_receipts_option =
+ page_params.realm_enable_read_receipts && not_spectator;
+
const args = {
message_id: message.id,
historical: message.historical,
@@ -523,6 +527,7 @@ export function toggle_actions_popover(element, id) {
conversation_time_uri,
narrowed: narrow_state.active(),
should_display_delete_option,
+ should_display_read_receipts_option,
should_display_reminder_option: feature_flags.reminders_in_message_action_menu,
should_display_edit_and_view_source,
should_display_quote_and_reply,
@@ -1235,6 +1240,14 @@ export function register_click_handlers() {
e.preventDefault();
});
+ $("body").on("click", ".view_read_receipts", (e) => {
+ const message_id = $(e.currentTarget).data("message-id");
+ hide_actions_popover();
+ read_receipts.show_user_list(message_id);
+ e.stopPropagation();
+ e.preventDefault();
+ });
+
$("body").on("click", ".delete_message", (e) => {
const message_id = $(e.currentTarget).data("message-id");
hide_actions_popover();
diff --git a/static/js/read_receipts.js b/static/js/read_receipts.js
new file mode 100644
index 00000000000..c862ffdb4a3
--- /dev/null
+++ b/static/js/read_receipts.js
@@ -0,0 +1,63 @@
+import $ from "jquery";
+import SimpleBar from "simplebar";
+
+import render_read_receipts from "../templates/read_receipts.hbs";
+import render_read_receipts_modal from "../templates/read_receipts_modal.hbs";
+
+import * as channel from "./channel";
+import {$t} from "./i18n";
+import * as loading from "./loading";
+import * as overlays from "./overlays";
+import * as people from "./people";
+import * as popovers from "./popovers";
+import * as ui_report from "./ui_report";
+
+export function show_user_list(message_id) {
+ $("body").append(render_read_receipts_modal());
+ overlays.open_modal("read_receipts_modal", {
+ autoremove: true,
+ on_show: () => {
+ loading.make_indicator($("#read_receipts_modal .loading_indicator"));
+ channel.get({
+ url: `/json/messages/${message_id}/read_receipts`,
+ idempotent: true,
+ success(data) {
+ const users = data.user_ids.map((id) => people.get_by_user_id(id));
+ users.sort(people.compare_by_name);
+
+ loading.destroy_indicator($("#read_receipts_modal .loading_indicator"));
+ if (users.length === 0) {
+ $("#read_receipts_modal .read_receipts_info").text(
+ $t({defaultMessage: "No one has read this message yet."}),
+ );
+ } else {
+ $("#read_receipts_modal .read_receipts_info").text(
+ $t(
+ {
+ defaultMessage:
+ "This message has been read by {num_of_people} people:",
+ },
+ {num_of_people: users.length},
+ ),
+ );
+ $("#read_receipts_modal .modal__container").addClass(
+ "showing_read_receipts_list",
+ );
+ $("#read_receipts_modal .modal__content").append(
+ render_read_receipts({users}),
+ );
+ new SimpleBar($("#read_receipts_modal .modal__content")[0]);
+ }
+ },
+ error(xhr) {
+ ui_report.error("", xhr, $("#read_receipts_error"));
+ loading.destroy_indicator($("#read_receipts_modal .loading_indicator"));
+ },
+ });
+ },
+ on_hide: () => {
+ // Ensure any user info popovers are closed
+ popovers.hide_all();
+ },
+ });
+}
diff --git a/static/shared/icons/readreceipts.svg b/static/shared/icons/readreceipts.svg
new file mode 100644
index 00000000000..fb59f33c955
--- /dev/null
+++ b/static/shared/icons/readreceipts.svg
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+ width="512"
+ height="512"
+ viewBox="0 0 512 512"
+ version="1.1"
+ id="SVGRoot"
+ sodipodi:docname="final.svg"
+ inkscape:version="1.1 (c4e8f9ed74, 2021-05-24)"
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:svg="http://www.w3.org/2000/svg">
+ <sodipodi:namedview
+ id="namedview14703"
+ pagecolor="#505050"
+ bordercolor="#eeeeee"
+ borderopacity="1"
+ inkscape:pageshadow="0"
+ inkscape:pageopacity="0"
+ inkscape:pagecheckerboard="0"
+ inkscape:document-units="px"
+ showgrid="true"
+ inkscape:zoom="1.4823794"
+ inkscape:cx="330.88695"
+ inkscape:cy="235.7696"
+ inkscape:window-width="1920"
+ inkscape:window-height="1018"
+ inkscape:window-x="0"
+ inkscape:window-y="0"
+ inkscape:window-maximized="1"
+ inkscape:current-layer="layer1"
+ width="512px">
+ <inkscape:grid
+ type="xygrid"
+ id="grid14709" />
+ </sodipodi:namedview>
+ <defs
+ id="defs14698" />
+ <g
+ inkscape:label="Layer 1"
+ inkscape:groupmode="layer"
+ id="layer1">
+ <path
+ id="rect15221"
+ style="fill:#1a1a1a;stroke-width:20.2826"
+ d="M 2.5195312,120.86719 V 468.25977 H 452.51953 V 231.2793 a 97.92847,105.62694 0 0 1 -47.3457,34.11914 V 429.66016 H 49.904297 V 159.46484 H 259.04688 c 0.002,-0.2309 0.002,-0.46272 0.004,-0.69336 -0.0637,-0.67733 -0.15238,-2.23334 -0.0215,-3 -1.03483,-1.48148 0.19546,-5.46484 1.16406,-6.63476 0.40696,1.79552 0.0611,-0.20325 0.25195,-0.66211 0.18239,-0.37906 -0.51088,-2.2379 -0.29101,-2.83984 0.33164,-0.90792 1.02383,-1.63415 1.54687,-2.45118 -0.0388,-0.0625 -0.0813,-0.11986 -0.12109,-0.0996 -0.0379,0.0417 -0.0712,0.0765 -0.10547,0.11329 -0.15454,0.24928 -0.1346,0.68144 -0.12891,0.14062 0.0306,-0.0297 0.0867,-0.0953 0.12891,-0.14062 0.0283,-0.0457 0.0602,-0.087 0.10156,-0.11329 0.001,-7.8e-4 0.003,6.3e-4 0.004,0 0.0845,-0.093 0.15228,-0.16483 0.22656,-0.24414 -0.13444,-0.70848 -0.58603,-1.95703 -0.29102,-2.34961 0.38142,-0.72606 0.78025,-1.44411 1.1504,-2.17578 -0.0204,0.0339 -0.11761,-0.0273 -0.0996,-0.0625 0.0666,-0.13052 0.28345,-0.42064 0.35547,-0.48633 0.0142,-0.0543 0.0151,-0.0765 0.0156,-0.0977 0.008,0.0684 0.0546,0.23009 0.24219,0.46289 0.0575,0.0713 -0.0324,-0.17943 -0.0488,-0.26953 -0.3667,-1.93467 -0.12894,-2.59641 0.92773,-3.40821 0.22311,-0.75132 0.54367,-1.47292 0.85547,-2.19531 0.05,-0.47553 0.16907,-0.98009 0.33594,-1.48828 0.0969,-0.29502 0.24858,-0.56832 0.40234,-0.83984 8.4e-4,-0.003 8.6e-4,-0.006 0.006,-0.008 0.0834,-0.14733 0.16777,-0.29417 0.24219,-0.44532 0.44283,-0.89939 0.88465,-1.7993 1.31836,-2.70312 0.44971,-0.93715 0.88781,-1.88055 1.33203,-2.82032 0.59614,-1.26113 1.3334,-2.12793 2.10156,-2.6621 0.0355,-0.13918 0.0372,-0.28567 0.0762,-0.42383 z M 262.92188,137.76562 c -0.012,0.0459 -0.0306,0.0956 -0.0605,0.16797 0.0231,-0.048 0.0496,-0.0943 0.0723,-0.14257 0.0212,-0.0452 0.0128,-0.0478 -0.0117,-0.0254 z m -1.11524,5.07422 c 0.009,0.0491 0.0156,0.0914 0.0215,0.13477 0.0463,-0.0788 0.0927,-0.15719 0.13477,-0.23828 0.0612,-0.11816 -0.0281,-0.0333 -0.15625,0.10351 z m 0.0215,0.13477 c -0.0413,0.0703 -0.0829,0.14015 -0.12695,0.20898 0.048,0.0772 0.0916,0.16059 0.11914,0.10938 0.0321,-0.0598 0.0265,-0.17906 0.008,-0.31836 z m 4.10547,4.51953 c -0.0428,0.0549 -0.0775,0.10956 -0.11328,0.16406 0.0334,-0.0471 0.066,-0.0934 0.0996,-0.14062 0.003,-0.005 0.01,-0.0175 0.0137,-0.0234 z m -5.35547,9.5957 c -0.003,0.001 -0.008,0.005 -0.01,0.006 -0.004,0.002 0.0108,-0.003 0.0117,-0.004 -7.5e-4,-5.3e-4 -0.001,-0.001 -0.002,-0.002 z m 0.41993,0.58985 c -0.005,-1.3e-4 -0.0271,0.004 -0.043,0.008 0.008,0.005 0.0149,0.008 0.0234,0.0137 0.01,-0.009 0.0251,-0.0213 0.0195,-0.0215 z m 0.0508,0.0644 c 0.0519,0.0326 0.0929,0.0572 0.14258,0.0879 0.004,-0.009 0.009,-0.0168 0.0137,-0.0254 -0.0548,-0.0209 -0.10403,-0.0407 -0.15625,-0.0625 z m 10.07422,1.07422 c -0.004,0.003 -0.008,0.007 -0.0117,0.01 0.011,0.006 0.001,-0.0117 0.0117,-0.01 z" />
+ <path
+ id="path15093"
+ style="fill:#1a1a1a;stroke-width:45.8897"
+ d="M 371.7276,23.955146 A 137.56607,137.77894 0 0 0 234.17732,161.72936 137.56607,137.77894 0 0 0 371.7276,299.5036 137.56607,137.77894 0 0 0 509.27786,161.72936 137.56607,137.77894 0 0 0 371.7276,23.955146 Z M 370,60 A 97.92847,102.64636 0 0 1 467.93877,162.65037 97.92847,102.64636 0 0 1 370,265.30077 97.92847,102.64636 0 0 1 272.06128,162.65037 97.92847,102.64636 0 0 1 370,60 Z" />
+ <rect
+ style="fill:#1a1a1a"
+ id="rect1345"
+ width="170"
+ height="40"
+ x="90"
+ y="270" />
+ <rect
+ style="fill:#1a1a1a"
+ id="rect1347"
+ width="230"
+ height="40"
+ x="90"
+ y="350" />
+ <path
+ style="fill:#1a1a1a;stroke-width:0.360527"
+ d="m 320,190 -27.89004,-27.19033 7.26208,-7.79208 7.26206,-7.79208 20.43012,19.18012 c 11.23657,10.54908 20.5457,19.22478 20.68699,19.27935 0.14126,0.0546 19.87802,-18.46967 43.85942,-41.165 23.98143,-22.6953 43.71378,-41.2642 43.84968,-41.2642 0.13588,0 3.4548,3.44356 7.37536,7.65237 l 7.12831,7.65238 -1.03941,1.07287 c -0.57169,0.59009 -23.04921,22.28959 -49.95003,48.22111 -26.90085,25.93153 -49.39974,47.64045 -49.99757,48.24204 l -1.08693,1.09379 z"
+ id="path6649" />
+ </g>
+</svg>
diff --git a/static/styles/dark_theme.css b/static/styles/dark_theme.css
index dd6b9e37efe..b400bbabdc8 100644
--- a/static/styles/dark_theme.css
+++ b/static/styles/dark_theme.css
@@ -1205,6 +1205,17 @@ body.dark-theme {
color: hsl(0, 0%, 100%);
}
}
+
+ #read_receipts_modal #read_receipts_list li {
+ &:hover {
+ background-color: hsla(0, 0%, 100%, 0.05);
+ }
+
+ &:active,
+ &:focus {
+ background-color: hsla(0, 0%, 100%, 0.1);
+ }
+ }
}
@supports (-moz-appearance: none) {
diff --git a/static/styles/modal.css b/static/styles/modal.css
index 4d64b6af128..0c621fc58c6 100644
--- a/static/styles/modal.css
+++ b/static/styles/modal.css
@@ -120,6 +120,7 @@
color: hsl(0, 0%, 100%) !important;
}
+#read_receipts_error,
#dialog_error {
margin-bottom: 10px;
}
diff --git a/static/styles/zulip.css b/static/styles/zulip.css
index e1559bfa135..52be2103c30 100644
--- a/static/styles/zulip.css
+++ b/static/styles/zulip.css
@@ -650,6 +650,78 @@ strong {
display: none;
}
+#read_receipts_modal {
+ .modal__container {
+ width: 360px;
+
+ .modal__content {
+ /* When showing read receipts, we use simplebar
+ to make the list scrollable. It requires this to
+ be flex. */
+ display: flex;
+
+ /* Setting a minimum height prevents the loading indicator
+ appearing/disappearing from resizing the modal in the
+ common case that one is requesting read receipts for
+ PMs. */
+ min-height: 120px;
+ /* Setting a maximum height is just for aesthetics; the modal looks
+ weird if its aspect ratio gets too stretched. */
+ max-height: 480px;
+ }
+ }
+
+ .modal__header {
+ padding-bottom: 0;
+ }
+
+ hr {
+ margin: 10px 0;
+ }
+
+ .modal__content {
+ padding: 0 24px 8px;
+ }
+
+ .loading_indicator {
+ margin: auto;
+ }
+
+ #read_receipts_list {
+ margin-left: 0;
+
+ li {
+ .read_receipts_user_avatar {
+ display: inline-block;
+ height: 20px;
+ width: 20px;
+ position: relative;
+ right: 8px;
+ border-radius: 4px;
+ }
+
+ margin: 2px 0;
+ list-style-type: none;
+ overflow-x: hidden;
+ padding-left: 10px;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+ cursor: pointer;
+ line-height: 26px;
+
+ &:hover {
+ background-color: hsla(0, 0%, 0%, 0.05);
+ }
+
+ &:active,
+ &:focus {
+ background-color: hsla(0, 0%, 0%, 0.1);
+ outline: none;
+ }
+ }
+ }
+}
+
/* .dropdown-menu from v2.3.2
+ https://github.com/zulip/zulip/commit/7a3a3be7e547d3e8f0ed00820835104867f2433d
basic idea of this fix is to remove decorations from :hover and apply them only
@@ -931,6 +1003,15 @@ td.pointer {
}
}
+ > .view_read_receipts {
+ font-size: 14px;
+ height: 16px;
+
+ &:hover {
+ color: hsl(200, 100%, 40%);
+ }
+ }
+
.edit_content {
width: 12px;
diff --git a/static/templates/actions_popover_content.hbs b/static/templates/actions_popover_content.hbs
index 189640ad33c..0e46b1b04c0 100644
--- a/static/templates/actions_popover_content.hbs
+++ b/static/templates/actions_popover_content.hbs
@@ -79,6 +79,14 @@
</li>
{{/if}}
+ {{#if should_display_read_receipts_option}}
+ <li>
+ <a class="view_read_receipts" data-message-id="{{message_id}}" tabindex="0">
+ <i class="zulip-icon zulip-icon-readreceipts" aria-label="{{#tr}}View read receipts{{/tr}}"></i> {{t "View read receipts" }}
+ </a>
+ </li>
+ {{/if}}
+
<li>
<a class="copy_link" data-message-id="{{message_id}}" data-clipboard-text="{{ conversation_time_uri }}" tabindex="0">
<i class="fa fa-link" aria-hidden="true"></i> {{t "Copy link to message" }}
diff --git a/static/templates/read_receipts.hbs b/static/templates/read_receipts.hbs
new file mode 100644
index 00000000000..53b7f1837a0
--- /dev/null
+++ b/static/templates/read_receipts.hbs
@@ -0,0 +1,8 @@
+<ul id="read_receipts_list">
+ {{#each users}}
+ <li class="view_user_profile" data-user-id="{{user_id}}" tabindex="0" role="button">
+ <img class="read_receipts_user_avatar" src="{{avatar_url}}" />
+ <span>{{full_name}}</span>
+ </li>
+ {{/each}}
+</ul>
diff --git a/static/templates/read_receipts_modal.hbs b/static/templates/read_receipts_modal.hbs
new file mode 100644
index 00000000000..285e13737b4
--- /dev/null
+++ b/static/templates/read_receipts_modal.hbs
@@ -0,0 +1,19 @@
+<div class="micromodal" id="read_receipts_modal" aria-hidden="true">
+ <div class="modal__overlay" tabindex="-1" data-micromodal-close>
+ <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="read_receipts_modal_label">
+ <header class="modal__header">
+ <h1 class="modal__title" id="read_receipts_modal_label">
+ {{t "Read receipts" }}
+ </h1>
+ <button class="modal__close" aria-label="{{t 'Close modal' }}" data-micromodal-close></button>
+ </header>
+ <hr/>
+ <main class="modal__content">
+ <div class="alert" id="read_receipts_error"></div>
+ <div class="read_receipts_info">
+ </div>
+ <div class="loading_indicator"></div>
+ </main>
+ </div>
+ </div>
+</div>
diff --git a/static/templates/settings/account_settings.hbs b/static/templates/settings/account_settings.hbs
index 5b8c6118ed7..1cbf08886e8 100644
--- a/static/templates/settings/account_settings.hbs
+++ b/static/templates/settings/account_settings.hbs
@@ -64,6 +64,7 @@
is_checked=settings_object.send_stream_typing_notifications
label=settings_label.send_stream_typing_notifications
}}
+ {{/if}}
{{> settings_checkbox
setting_name="send_read_receipts"
is_checked=settings_object.send_read_receipts
@@ -71,7 +72,6 @@
tooltip_text=send_read_receipts_tooltip
hide_tooltip=page_params.realm_enable_read_receipts
}}
- {{/if}}
</div>
</div>
diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md
index 4e3ebba48a0..ce1e7b522f8 100644
--- a/templates/zerver/api/changelog.md
+++ b/templates/zerver/api/changelog.md
@@ -22,6 +22,8 @@ format used by the Zulip server that they are interacting with.
**Feature level 137**
+* [`GET /messages/{message_id}/read_receipts`](/api/get-read-receipts):
+ Added new endpoint to fetch read receipts for a message.
* [`POST /register`](/api/register-queue), [`GET
/events`](/api/get-events), `PATCH /realm`: Added new
`enable_read_receipts` realm setting.
diff --git a/templates/zerver/help/include/rest-endpoints.md b/templates/zerver/help/include/rest-endpoints.md
index b00553bd851..bf5c78282d1 100644
--- a/templates/zerver/help/include/rest-endpoints.md
+++ b/templates/zerver/help/include/rest-endpoints.md
@@ -14,6 +14,7 @@
* [Get a message's edit history](/api/get-message-history)
* [Update personal message flags](/api/update-message-flags)
* [Mark messages as read in bulk](/api/mark-all-as-read)
+* [Get a message's read receipts](/api/get-read-receipts)
#### Drafts
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index 0b678833d2b..c7edff0a126 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -128,6 +128,7 @@ EXEMPT_FILES = make_set(
"static/js/poll_widget.js",
"static/js/popover_menus.js",
"static/js/popovers.js",
+ "static/js/read_receipts.js",
"static/js/ready.ts",
"static/js/realm_icon.js",
"static/js/realm_logo.js",
diff --git a/version.py b/version.py
index 1bf81ae2389..bd2d484eb15 100644
--- a/version.py
+++ b/version.py
@@ -48,4 +48,4 @@ API_FEATURE_LEVEL = 137
# historical commits sharing the same major version, in which case a
# minor version bump suffices.
-PROVISION_VERSION = (197, 1)
+PROVISION_VERSION = (198, 0)
diff --git a/zerver/actions/create_realm.py b/zerver/actions/create_realm.py
index e7c5bb37f81..cdb6a48ba31 100644
--- a/zerver/actions/create_realm.py
+++ b/zerver/actions/create_realm.py
@@ -142,6 +142,7 @@ def do_create_realm(
org_type: Optional[int] = None,
date_created: Optional[datetime.datetime] = None,
is_demo_organization: bool = False,
+ enable_read_receipts: Optional[bool] = None,
enable_spectator_access: Optional[bool] = None,
) -> Realm:
if string_id == settings.SOCIAL_AUTH_SUBDOMAIN:
@@ -178,6 +179,19 @@ def do_create_realm(
assert not settings.PRODUCTION
kwargs["date_created"] = date_created
+ # Generally, closed organizations like companies want read
+ # receipts, whereas it's unclear what an open organization's
+ # preferences will be. We enable the setting by default only for
+ # closed organizations.
+ if enable_read_receipts is not None:
+ kwargs["enable_read_receipts"] = enable_read_receipts
+ else:
+ # Hacky: The default of invited_required is True, so we need
+ # to check for None too.
+ kwargs["enable_read_receipts"] = (
+ invite_required is None or invite_required is True or emails_restricted_to_domains
+ )
+
with transaction.atomic():
realm = Realm(string_id=string_id, name=name, **kwargs)
if is_demo_organization:
diff --git a/zerver/models.py b/zerver/models.py
index f29d2b7f509..317ee801275 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -3246,6 +3246,10 @@ class AbstractUserMessage(models.Model):
def where_unread() -> str:
return AbstractUserMessage.where_flag_is_absent(getattr(AbstractUserMessage.flags, "read"))
+ @staticmethod
+ def where_read() -> str:
+ return AbstractUserMessage.where_flag_is_present(getattr(AbstractUserMessage.flags, "read"))
+
@staticmethod
def where_starred() -> str:
return AbstractUserMessage.where_flag_is_present(
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py
index cfe22bd412d..617b6e10e01 100644
--- a/zerver/openapi/python_examples.py
+++ b/zerver/openapi/python_examples.py
@@ -1015,6 +1015,15 @@ def remove_reaction(client: Client, message_id: int) -> None:
validate_against_openapi_schema(result, "/messages/{message_id}/reactions", "delete", "200")
+@openapi_test_function("/messages/{message_id}/read_receipts:get")
+def get_read_receipts(client: Client, message_id: int) -> None:
+ # {code_example|start}
+ # Get read receipts for a message
+ result = client.call_endpoint(f"/messages/{message_id}/read_receipts", method="GET")
+ # {code_example|end}
+ validate_against_openapi_schema(result, "/messages/{message_id}/read_receipts", "get", "200")
+
+
def test_nonexistent_stream_error(client: Client) -> None:
request = {
"type": "stream",
@@ -1498,6 +1507,7 @@ def test_messages(client: Client, nonadmin_client: Client) -> None:
get_messages(client)
check_messages_match_narrow(client)
get_message_history(client, message_id)
+ get_read_receipts(client, message_id)
delete_message(client, message_id)
mark_all_as_read(client)
mark_stream_as_read(client)
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 54b21449e03..3c45f740bbe 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -5811,6 +5811,70 @@ paths:
}
description: |
An example JSON error response for when the emoji code is invalid:
+
+ /messages/{message_id}/read_receipts:
+ get:
+ operationId: get-read-receipts
+ summary: Get the list of IDs of users who have read a message.
+ tags: ["messages"]
+ description: |
+ Returns a list containing the IDs for all users who have
+ marked the message as read (and whose privacy settings allow
+ sharing that information).
+
+ The list of users IDs will include any bots who have marked
+ the message as read via the API (providing a way for bots to
+ indicate whether they have processed a message successfully in
+ a way that can be easily inspected in a Zulip client). Bots
+ for which this behavior is not desired may disable the
+ `send_read_receipts` setting via the API.
+
+ It will never contain the message's sender.
+
+ **Changes**: New in Zulip 6.0 (feature level 137).
+ parameters:
+ - $ref: "#/components/parameters/MessageId"
+ responses:
+ "200":
+ description: Success.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonSuccessBase"
+ - $ref: "#/components/schemas/SuccessDescription"
+ - additionalProperties: false
+ properties:
+ result: {}
+ msg: {}
+ user_ids:
+ type: array
+ description: |
+ An array of IDs of users who have marked the target message as
+ read and whose read status is available to the current user.
+
+ The IDs of users who have disabled sending read receipts
+ (`send_read_receipts=false`) will never appear in the response,
+ nor will the message's sender. The current user's ID will, if
+ they have marked the read target message as read.
+ items:
+ type: integer
+ example:
+ {"msg": "", "result": "success", "user_ids": [3, 7, 9]}
+ "400":
+ description: Bad request.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonError"
+ - example: {"msg": "Invalid message(s)", "result": "error"}
+ description: |
+ A typical JSON response when attempting to
+ access read receipts for a message ID that
+ either does not exist or is not accessible to
+ the current user.
+
/messages/matches_narrow:
get:
operationId: check-messages-match-narrow
diff --git a/zerver/tests/test_read_receipts.py b/zerver/tests/test_read_receipts.py
new file mode 100644
index 00000000000..9761e0274c1
--- /dev/null
+++ b/zerver/tests/test_read_receipts.py
@@ -0,0 +1,208 @@
+import orjson
+
+from zerver.actions.create_user import do_reactivate_user
+from zerver.actions.realm_settings import do_set_realm_property
+from zerver.actions.user_settings import do_change_user_setting
+from zerver.actions.users import do_deactivate_user
+from zerver.lib.test_classes import ZulipTestCase
+from zerver.models import UserMessage, UserProfile
+
+
+class TestReadReceipts(ZulipTestCase):
+ def mark_message_read(self, user: UserProfile, message_id: int) -> None:
+ result = self.api_post(
+ user,
+ "/json/messages/flags",
+ {"messages": orjson.dumps([message_id]).decode(), "op": "add", "flag": "read"},
+ )
+ self.assert_json_success(result)
+
+ def test_stream_message(self) -> None:
+ hamlet = self.example_user("hamlet")
+ sender = self.example_user("othello")
+
+ message_id = self.send_stream_message(sender, "Verona", "read receipts")
+ self.login("hamlet")
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id not in result.json()["user_ids"])
+
+ self.mark_message_read(hamlet, message_id)
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id in result.json()["user_ids"])
+ self.assertTrue(sender.id not in result.json()["user_ids"])
+
+ def test_personal_message(self) -> None:
+ hamlet = self.example_user("hamlet")
+ sender = self.example_user("othello")
+
+ message_id = self.send_personal_message(sender, hamlet)
+ self.login("hamlet")
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id not in result.json()["user_ids"])
+
+ self.mark_message_read(hamlet, message_id)
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id in result.json()["user_ids"])
+ self.assertTrue(sender.id not in result.json()["user_ids"])
+
+ def test_huddle_message(self) -> None:
+ hamlet = self.example_user("hamlet")
+ sender = self.example_user("othello")
+ cordelia = self.example_user("cordelia")
+
+ message_id = self.send_huddle_message(sender, [hamlet, cordelia])
+ self.login("hamlet")
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id not in result.json()["user_ids"])
+ self.assertTrue(sender.id not in result.json()["user_ids"])
+
+ self.mark_message_read(hamlet, message_id)
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id in result.json()["user_ids"])
+ self.assertTrue(sender.id not in result.json()["user_ids"])
+
+ def test_inaccessible_stream_message(self) -> None:
+ sender = self.example_user("othello")
+
+ private_stream = "private stream"
+ self.make_stream(stream_name=private_stream, invite_only=True)
+ self.subscribe(sender, private_stream)
+
+ message_id = self.send_stream_message(sender, private_stream, "read receipts")
+
+ self.login("hamlet")
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_error(result, "Invalid message(s)")
+
+ self.login_user(sender)
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+
+ def test_filter_deactivated_users(self) -> None:
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ sender = self.example_user("othello")
+
+ message_id = self.send_stream_message(sender, "Verona", "read receipts")
+
+ # Mark message as read for hamlet and cordelia.
+ self.mark_message_read(hamlet, message_id)
+ self.mark_message_read(cordelia, message_id)
+
+ # Login as cordelia and make sure hamlet is in read receipts before deactivation.
+ self.login("cordelia")
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id in result.json()["user_ids"])
+ self.assertTrue(cordelia.id in result.json()["user_ids"])
+
+ # Deactivate hamlet and verify hamlet is not in read receipts.
+ do_deactivate_user(hamlet, acting_user=None)
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id not in result.json()["user_ids"])
+ self.assertTrue(cordelia.id in result.json()["user_ids"])
+
+ # Reactivate hamlet and verify hamlet appears again in read recipts.
+ do_reactivate_user(hamlet, acting_user=None)
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertTrue(hamlet.id in result.json()["user_ids"])
+ self.assertTrue(cordelia.id in result.json()["user_ids"])
+
+ def test_send_read_receipts_privacy_setting(self) -> None:
+ hamlet = self.example_user("hamlet")
+ sender = self.example_user("othello")
+ cordelia = self.example_user("cordelia")
+
+ message_id = self.send_stream_message(sender, "Verona", "read receipts")
+
+ self.mark_message_read(hamlet, message_id)
+ self.mark_message_read(cordelia, message_id)
+
+ self.login("aaron")
+ do_set_realm_property(sender.realm, "enable_read_receipts", False, acting_user=None)
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_error(result, "Read receipts are disabled in this organization.")
+
+ do_set_realm_property(sender.realm, "enable_read_receipts", True, acting_user=None)
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertIn(hamlet.id, result.json()["user_ids"])
+ self.assertIn(cordelia.id, result.json()["user_ids"])
+
+ # Disable read receipts setting; confirm Cordelia no longer appears.
+ do_change_user_setting(cordelia, "send_read_receipts", False, acting_user=cordelia)
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertIn(hamlet.id, result.json()["user_ids"])
+ self.assertNotIn(cordelia.id, result.json()["user_ids"])
+
+ def test_send_read_receipts_privacy_setting_bot(self) -> None:
+ hamlet = self.example_user("hamlet")
+ sender = self.example_user("othello")
+ bot = self.example_user("default_bot")
+
+ message_id = self.send_stream_message(sender, "Verona", "read receipts")
+
+ self.mark_message_read(hamlet, message_id)
+ self.mark_message_read(bot, message_id)
+
+ self.login("aaron")
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertIn(hamlet.id, result.json()["user_ids"])
+ self.assertIn(bot.id, result.json()["user_ids"])
+
+ # Disable read receipts setting; confirm bot no longer appears.
+ do_change_user_setting(bot, "send_read_receipts", False, acting_user=bot)
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertIn(hamlet.id, result.json()["user_ids"])
+ self.assertNotIn(bot.id, result.json()["user_ids"])
+
+ def test_historical_usermessages_read_flag_not_considered(self) -> None:
+ """
+ Ensure UserMessage rows with historical flag are also
+ considered for read receipts.
+ """
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+
+ stream_name = "test stream"
+ self.subscribe(cordelia, stream_name)
+
+ message_id = self.send_stream_message(cordelia, stream_name, content="foo")
+
+ self.login("hamlet")
+
+ # Have hamlet react to the message to
+ # create a historical UserMessage row.
+ reaction_info = {
+ "emoji_name": "smile",
+ }
+ result = self.client_post(f"/json/messages/{message_id}/reactions", reaction_info)
+ self.assert_json_success(result)
+
+ # Ensure UserMessage row with historical and read flags exists
+ user_message = UserMessage.objects.get(user_profile=hamlet, message_id=message_id)
+ self.assertTrue(user_message.flags.historical)
+ self.assertTrue(user_message.flags.read)
+
+ result = self.client_get(f"/json/messages/{message_id}/read_receipts")
+ self.assert_json_success(result)
+ self.assertIn(hamlet.id, result.json()["user_ids"])
diff --git a/zerver/views/read_receipts.py b/zerver/views/read_receipts.py
new file mode 100644
index 00000000000..606cd432b12
--- /dev/null
+++ b/zerver/views/read_receipts.py
@@ -0,0 +1,75 @@
+from django.http.request import HttpRequest
+from django.http.response import HttpResponse
+from django.utils.translation import gettext as _
+
+from zerver.lib.exceptions import JsonableError
+from zerver.lib.message import access_message
+from zerver.lib.request import REQ, has_request_variables
+from zerver.lib.response import json_success
+from zerver.lib.validator import to_non_negative_int
+from zerver.models import UserMessage, UserProfile
+
+
+@has_request_variables
+def read_receipts(
+ request: HttpRequest,
+ user_profile: UserProfile,
+ message_id: int = REQ(converter=to_non_negative_int, path_only=True),
+) -> HttpResponse:
+ message = access_message(user_profile, message_id)[0]
+
+ if not user_profile.realm.enable_read_receipts:
+ raise JsonableError(_("Read receipts are disabled in this organization."))
+
+ # This query implements a few decisions:
+ # * Most importantly, this is where we enforce the
+ # send_read_receipts privacy setting.
+ #
+ # * The message sender is never included, since presumably they
+ # read the message before sending it, and showing the sender as
+ # having read their own message is likely to be confusing.
+ #
+ # * Deactivated users are excluded. While in theory someone
+ # could be interested in the information, not including them
+ # is a cleaner policy, and usually read receipts are only of
+ # interest shortly after a message was sent.
+ #
+ # * We do not filter on the historical flag. This means that a user
+ # who stars a public stream message that they did not originally
+ # receive will appear in read receipts for that message.
+ #
+ # * Marking a message as unread causes it to appear as not read
+ # via read receipts, as well. This is consistent with the fact
+ # that users can view a message without leaving it marked as
+ # read in other ways (desktop/email/push notifications).
+ #
+ # * Bots are included. Most bots never mark any messages as read,
+ # but one could imagine having them to do so via the API to
+ # communicate useful information. For example, the `read` flag
+ # could be used by a bot to track which messages have been
+ # bridged to another chat system or otherwise processed
+ # successfully by the bot, and users might find it useful to be
+ # able to inspect that in the UI. If this behavior is not
+ # desired for a bot, it can be disabled using the
+ # send_read_receipts privacy setting.
+ #
+ # Note that we do not attempt to present how many users received a
+ # message but have NOT marked the message as read. There are
+ # tricky corner cases involved in doing so, such as the
+ # `historical` flag for public stream messages; but the most
+ # important one is how to handle users who read a message and then
+ # later unsubscribed from a stream.
+ user_ids = (
+ UserMessage.objects.filter(
+ message_id=message.id,
+ user_profile__is_active=True,
+ user_profile__send_read_receipts=True,
+ )
+ .exclude(user_profile_id=message.sender_id)
+ .extra(
+ where=[UserMessage.where_read()],
+ )
+ .values_list("user_profile_id", flat=True)
+ )
+
+ return json_success(request, {"user_ids": list(user_ids)})
diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py
index 35f8414292f..2b0f3134c26 100644
--- a/zilencer/management/commands/populate_db.py
+++ b/zilencer/management/commands/populate_db.py
@@ -322,6 +322,7 @@ class Command(BaseCommand):
invite_required=False,
plan_type=Realm.PLAN_TYPE_SELF_HOSTED,
org_type=Realm.ORG_TYPES["business"]["id"],
+ enable_read_receipts=True,
enable_spectator_access=True,
)
RealmDomain.objects.create(realm=zulip_realm, domain="zulip.com")
diff --git a/zproject/urls.py b/zproject/urls.py
index 34cf3a5ef89..9959eca7cb8 100644
--- a/zproject/urls.py
+++ b/zproject/urls.py
@@ -98,6 +98,7 @@ from zerver.views.push_notifications import (
remove_apns_device_token,
)
from zerver.views.reactions import add_reaction, remove_reaction
+from zerver.views.read_receipts import read_receipts
from zerver.views.realm import (
check_subdomain_available,
deactivate_realm,
@@ -344,6 +345,8 @@ v1_api_and_json_patterns = [
# POST adds a reaction to a message
# DELETE removes a reaction from a message
rest_path("messages/<int:message_id>/reactions", POST=add_reaction, DELETE=remove_reaction),
+ # read_receipts -> zerver.views.read_receipts
+ rest_path("messages/<int:message_id>/read_receipts", GET=read_receipts),
# attachments -> zerver.views.attachments
rest_path("attachments", GET=list_by_user),
rest_path("attachments/<int:attachment_id>", DELETE=remove),