API call to have a user watch specific category(ies)?

AI-generated summary

The discussion revolves around making an API call to have a user watch specific categories in Discourse. SwisherSweet initially asks about the API call but cannot find it in the documentation. pfaffman suggests reverse-engineering the API, but SwisherSweet is concerned about the API documentation being incomplete.

Osama provides a solution using jQuery Ajax, and kgoetz offers an alternative method via Python requests, which sets the watch list directly against the user. Both methods require an API key and username.

jord8on asks if it’s possible to create a hyperlink with a URL parameter that would allow users to watch a category with a specific notification level when clicked. However, pacharanero points out that this would require a REST POST action, which cannot be achieved with a simple hyperlink, and suggests writing a plugin or theme component as a solution.

According to a reply by @Falco on my post, there’s a way to set users to watch categories using the API. I’ve spent hours searching the web and combing through the API documentation but I cannot find this call.

Does anyone know what API call could be used to set a user to watch a specific category?

Thank you.

You can search for "reverse engineer api " for tips on how to figure out the API call. Also, there is a plugin that will make people in a certain group follow a set of categories.

Thanks for the reply @pfaffman. Are you suggesting the API documentation is incomplete and I need to reverse engineer the API code to find the undocumented API call?

if you don’t see what you’re looking for, it’s almost as easy to reverse engineer as use the documentation.

Have you figured out how to achieve this?

Here is how to do it using jQuery Ajax for anyone interested:

const data = {
	notification_level: 3, 
	api_key: "all_users_api_key",
	api_username: "target_user_username"
};

const settings = {
  url: "/category/{{category_id}}/notifications",
  method: "POST",
  data: data
};

$.ajax(settings).done(response => {
  console.log(response);
});

I’m sure @Osama’s method works, but setting user watches via the category seemed a bit weird to me. The following sets the watch list directly against the user (This is via python requests, but shows what is required).

DISCOURSE_URL - domain, including http(s)
username - user to change
DISCOURSE_API_KEY - api key generated by, and associated with a user
DISCOURSE_API_USERNAME - user who the key is associated with
watch_category - a list of categories the user should be watching

requests.request(‘PUT’, ‘{0}/u/{1}.json’.format(DISCOURSE_URL, username), allow_redirects=False, params={“api_key”:DISCOURSE_API_KEY, “api_username”: DISCOURSE_API_USERNAME}, json={‘watched_category_ids’: [watch_category]}, headers={‘Accept’: ‘application/json; charset=utf-8’})

@SwisherSweet if you end up opening a bug against the docs for missing info on how to modify the watch list/user prefs do post back here with it.

이 코드가 제가 원하는 기능을 수행할 수 있는지 확실하지 않지만…

사용자가 링크를 클릭하면 특정 카테고리에 대해 감시(Watching)/추적(Tracking)/일반(Normal)/음소거(Muted) 상태로 전환되는 URL 파라미터가 포함된 하이퍼링크를 추가하고 싶습니다.

실제 사용 사례를 들어 설명하자면, "해당 카테고리를 방문하세요… 그리고 벨 아이콘을 클릭하여 _____를 선택하세요…"라고 안내하는 대신, 다음과 같이 안내할 수 있습니다.

여기를 클릭하세요 이 포럼에 새로운 게시물이 추가될 때마다 알림을 받으려면.

그런 다음 사용자가 해당 하이퍼링크를 클릭하면 URL 파라미터에서 정의한 수준으로 구독이 설정됩니다.

플러그인이나 테마 컴포넌트로 작성하는 것이 비교적 간단할 것 같습니다. 이런 동작을 대신 수행할 수 있는 내장 가능한 엔티티를 만드는 방식이죠. 위에 게시된 AJAX 코드의 문제는 전역 사용자 API 키를 클라이언트로 전송하고 있다는 것입니다. 이는 절대 하지 말아야 할 일입니다.

OP(원문)에서 언급한 대로 링크를 생성할 때 겪게 될 문제는 API 호출에 REST POST 동작이 필요하지만, 링크는 REST GET 동작만 수행할 수 있다는 점입니다.