Page MenuHomePhabricator

[lib/web/native] Unify logic to check canTogglePins in web and native tooltips
ClosedPublic

Authored by rohan on Oct 30 2023, 12:18 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Nov 20, 7:30 PM
Unknown Object (File)
Wed, Nov 20, 4:38 PM
Unknown Object (File)
Wed, Nov 20, 4:37 PM
Unknown Object (File)
Wed, Nov 20, 4:37 PM
Unknown Object (File)
Wed, Nov 20, 4:37 PM
Unknown Object (File)
Wed, Nov 20, 4:37 PM
Unknown Object (File)
Wed, Nov 20, 4:36 PM
Unknown Object (File)
Wed, Nov 20, 4:18 PM
Subscribers

Details

Summary

This diff unifies the logic for checking whether a client can toggle a pin. Previously, there was a hacky check in two places (threadInfo.sourceMessageID === messageInfo.id), as well as duplicated checking logic across web and native.

This diff achieves a few things to clean this code up:

  • For native, it moves the call to check whether the client can toggle pins up to message.react.js so it can be passed in as a prop to both TextMessage and MultimediaMessage. This is better than duplicating the call in the individual components.
  • Updated isInvalidPinSource to take in an optional parameter threadInfo, and if defined we can do the aforementioned check here in one place.
    • In my opinion, this is better than doing this in all of the individual message specs since that’s going to result in a ton of copy and pasted hacky checks.
    • This optional parameter will not be provided from callsites to isInvalidPinSource from the server
  • Creates a helper function canToggleMessagePin that is used for the client tooltips. This will check both isInvalidSidebarSource and also whether the client thread has permission to manage pins.

Resolves ENG-5619

Depends on D9634

Test Plan

Verified that the tooltip option to pin messages still appears on both web and native when it should/shouldn't

Diff Detail

Repository
rCOMM Comm
Branch
ENG-5617
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

ashoat requested changes to this revision.Oct 30 2023, 1:11 PM

Looks good, just a question:

This optional parameter will not be provided from callsites to isInvalidPinSource from the server

Can you clarify why? Wouldn't we want to make this check everywhere?

This revision now requires changes to proceed.Oct 30 2023, 1:11 PM

Can you clarify why? Wouldn't we want to make this check everywhere?

There's no strong reason, I'm ultimately happy to include it on the server. My thinking was as follows:

There are two callsites of isInvalidPinSource on the server:

  • fetchDerivedMessages in message-fetchers
  • toggleMessagePinForThread in thread-updaters (this is the endpoint when pinning / unpinning a message)

For fetchDerivedMessages, I weighed the tradeoff of adding some additional complexity to the fetchDerivedMessages code of having to fetch all of the threadInfos associated with the message ids. I ultimately decided against adding another query on the database since it's impossible for a client to pin a message that begins a sidebar.

For toggleMessagePinForThread, we already have to fetch the thread info in the updater code. This change actually wouldn't be too bad, it would just require moving the call to fetchServerThreadInfos earlier and passing it in and fixing the types, but I thought to keep the call sites consistent (so we can definitively say threadInfo will be provided to isInvalidSidebarSource from the client, but not from the server). So if I change fetchDerivedMessages, I'll change this too.

I'm ok to include the threadInfo parameter from the server for a matter of consistency everywhere, I estimate it should be a short change (just will take some time testing fetchDerivedMessages) so it won't derail any time estimates further. I'll go ahead and work on that change and then follow through by updating the diff here

Interesting that toggleMessagePinForThread is missing the threadInfo.sourceMessageID !== item.messageInfo.id check. I guess that can't happen, since it only takes a messageID, and the parent channel gets fetched from that (rather than the corresponding sidebar).

Still, it might be a good idea to add this to toggleMessagePinForThread. Besides the benefits of consistently checking the same condition in as many places as possible, you also get a bonus for moving the fetchServerThreadInfos call earlier. I think that lets you replace the isInvalidPinSource check there with canToggleMessagePin, and drop the checkThreadPermission entirely. (You'd need to update the types on canToggleMessagePin I think.)

On the other hand, adding an extra query to fetchDerivedMessages probably isn't worth it. And like you said, that functions handles fetching rather than setting data, so the need to validate isn't as high.

That said, I don't love that the thread is currently an optional parameter to isInvalidPinSource. It's ambiguous to the caller whether it needs to be provided or not.

Could you instead introduce two variants of the function with two different names? That way you could use naming to communicate the differences between the situations in which each function should be called.

(toggleMessagePinForThread changes can probably be in a separate diff)

  1. Reverted isInvalidPinSource to how it originally was (open to renaming it to isInvalidMessagePinSource or something if it needs to be more descriptive)
  2. Introduced a new variant isInvalidPinSourceForThread that definitively accepts a threadInfo parameter and performs the threadInfo.sourceMessageID !== item.messageInfo.id chcek

Open to generally renaming these as well. The next diff in this stack will update toggleMessagePinForThread

Update threadInfo param types

ashoat added inline comments.
lib/shared/message-utils.js
668

Can you add a comment here saying something like:

// Prefer checking isInvalidPinSourceForThread below. This function doesn't
// check whether the user is attempting to pin a SIDEBAR_SOURCE in the context
// of its parent thread, so it's not suitable for permission checks. We only use
// it in the message-fetchers.js code where we don't have access to the
// RawThreadInfo and don't need to do permission checks.
678

Nit: could we use isInvalidPinSource here instead?

This revision is now accepted and ready to land.Nov 1 2023, 7:21 AM

Add comment and use isInvalidPinSource