1

I have a Phoenix web application and am using Absinthe for subscriptions that are triggered whenever a new Comment is added to a Topic. I am trying to send a subscription only if the user is a member of a topic, and otherwise not send something at all.

So far I've tried playing with config as mentioned in the Subscription docs but it seems like that it is only executed when creating the subscription and not when it is triggered. I'm hoping it is something as simple as:

resolve fn comment, _, %{context: context} ->
  if User.member_of?(context.user, commment.topic) do
    {:ok, comment}
  else
    :noreply
  end
end

For now, I'm just returning {:ok, nil} but I'm hoping there is a better way to achieve this.

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • Huh, normally you can specify whether to publish a subscribtion in the `trigger` macro. If you return an empty array, no subscription will be triggered. However, the problem is that you need a user context, which AFAIK is not exposed in the trigger macro...it's just a comment there... – Jonas Dellinger Feb 26 '19 at 21:58
  • Just to be clear, what do you mean with "User"? The one who created the comment? Or any user who is a member of a topic? – Jonas Dellinger Feb 26 '19 at 21:59
  • When a user creates the comment in a topic, and I want all users (who are members of that topic) to get the subscription. – Sheharyar Feb 26 '19 at 22:08
  • The correct way to solve this is to make a subscription-topic per "Topic". Every User who is interested in a subscription for a "Topic" has to register interest in this specific "Topic" subscription beforehand. If you replace `repo_name` with `topic_id`, then the example is pretty much the solution: https://hexdocs.pm/absinthe/subscriptions.html#schema – Jonas Dellinger Feb 26 '19 at 22:14
  • Yes, I did see that but it seemed overkill. I thought it would've been much easier to just create one general "topic" subscription and move the authorization logic to the trigger/resolve in the backend. – Sheharyar Feb 26 '19 at 22:18
  • I guess it's not overkill, rather the only solution :P As an example, for every comment submitted, you would have to call resolve for every different user which is currently in this general topic. Would not scale very well and thus is probably not preferred. – Jonas Dellinger Feb 26 '19 at 22:29
  • Yeah but you can always just cache topic membership in ETS or something. – Sheharyar Feb 26 '19 at 22:37
  • But I guess this question is answered with using the `topic/2` macro? – Jonas Dellinger Feb 26 '19 at 22:49

0 Answers0