# 외부 그룹용 제한된 범주 생성을 위한 API 사용

**URL:** https://meta.discourse.org/t/use-the-api-to-create-restricted-categories-for-external-groups/139107
**Category:** Developers
**Tags:** rest-api, how-to
**Created:** [1월 19, 2020, 6:31오후 UTC](https://meta.discourse.org/t/use-the-api-to-create-restricted-categories-for-external-groups/139107 "2020-01-19T18:31:12Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Judith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/judith/32/289903_2.png) [@Judith](https://meta.discourse.org/u/Judith)
#### Post date: [1월 19, 2020, 6:31오후 UTC](https://meta.discourse.org/t/use-the-api-to-create-restricted-categories-for-external-groups/139107/1 "2020-01-19T18:31:12Z")

</div>

Discourse를 **동시에** (단, 독점적으로만은) 사용하는 커뮤니티 사이트가 있다면, 각 그룹을 위한 제한된 서브포럼을 워크스페이스로 제공하고자 할 수 있습니다. Ruby로 이를 수행하는 방법은 다음과 같습니다:

1. API 호출이 관리자 권한을 가진 사용자로 인증되도록 하세요.

2. 그룹 리더와 일반 사용자를 포함한 포럼 그룹을 생성합니다.

```ruby
params = { 
  "group": { 
    "name": "<group_name_with_underscores>", 
    "full_name": "<group name>", 
    "title": "Member of <group name>", # 선택: 그룹 멤버를 위한 포럼 flair
    "mentionable_level": 99, # 선택: 이 그룹을 @로 멘션할 수 있는 사람?
    "messageable_level": 99, # 선택: 전체 그룹에게 메시지를 보낼 수 있는 사람?
    "owner_usernames": "<comma-separated forum usernames of your group leaders>",
    "usernames": "<comma-separated forum usernames of the other group members>"
   }
}
r = Discourse.make_request("/admin/groups.json", params, "POST", true)

```

성공적으로 완료되면 응답에서 ‘id’ 필드를 찾아 기억하세요. 이것이 그룹의 ID입니다.

1. 외부 그룹과 포럼 그룹을 동기화하세요.

```ruby
# 새로운 일반 멤버
r = Discourse.make_request("/groups/#{group_id}/members.json", {"usernames": "<new_members as comma-separated string>"}, "PUT", true)
# 새로운 리더
r = Discourse.make_request("/admin/groups/#{group_id}/owners.json", {"group": { "usernames": "<new leader forum usernames as comma-separated string>"}}, "PUT", true)
# 떠난 멤버 제거
r = Discourse.make_request("/groups/#{group_id}/members.json", {"usernames": "<leaving_members as comma-separated string>"}, "DELETE", true)

```

1. 이 그룹만 접근할 수 있는 서브카테고리(또는 서브포럼)를 생성하세요. 모든 이러한 서브포럼의 부모 카테고리가 될 수 있도록 “Workspaces” 또는 "Restricted"와 같은 서브포럼을 수동으로 생성하고, 카테고리 목록의 맨 아래에 배치하는 것을 권장합니다. 이 컨테이너 포럼에는 “모두가 볼 수 있음”(Everyone can see) 권한이 있어야 합니다(그렇지 않으면 Discourse가 오류를 발생시킵니다) 그리고 아무도 작성할 수 없도록 설정하는 것이 좋습니다. 그런 다음, 각 새로운 그룹을 위해 서브포럼을 프로그래매틱하게 생성할 수 있습니다.

```ruby
params = { 
  "name": "<subforum name, e.g. same as group full name>", 
  "color": "<whatever>", # 선택
  "text_color": "<whatever>", # 선택
  "permissions": { 
    "<your group name with underscores>": "1", 
    "admins": "1", # 이 항목이 없으면 API를 제외하고는 서브포럼을 삭제할 수 없을 수 있음
    "moderators": "1" 
   }, 
  "parent_category_id": "<see note above>", 
  "allow_badges": true, # 선택
  "suppress_from_latest": true, # 선택
}
r = Discourse.make_request("/categories.json", params, "POST", true)

```

성공적으로 완료되면 반환된 카테고리 ID를 기억하세요.

1. 이 서브포럼에 사전 내용을 채우고 싶을 수 있습니다. 저는 지식 공유와 계획 수립을 위해 그룹에게 고정된 위키 게시글을 제공할 것입니다.

```ruby
# 이 서브포럼에 일반 게시글 작성
post_msg = "Welcome! This post is editable by all members of the group and can be used as a knowledge repository as well as to track common projects.\n\n **The following is a template you can decide to use or not.** \n\n **Useful links for group members:** \n\n- Link 1\n\n- Link 2\n\n- Link 3\n\n **These are the projects we are currently working on:** \n\n- Project 1 - *started*. Involved: Member 1, 2 and 3\n\n- Project 2 - *planning phase*. To be discussed at our next meeting on Monday."
params = { "raw": post_msg, title: "Knowledge Wiki for #{group_name}", "category": category_id, "archetype": "regular"}
r = Discourse.make_request("/posts", params, "POST", true)
if r and r.status == 200
  my_post = JSON.parse(r.body)
  post_id = my_post["id"]
  topic_id = my_post["topic_id"]
  # 위키로 전환
  begin
    r = Discourse.make_request("/posts/#{post_id}/wiki", {"wiki": true}, "PUT", false)
    rescue JSON::ParserError # Discourse API는 잘못된 JSON 응답을 반환하지만, fortunately 이를 파싱할 필요가 없음
  end
  # 고정
  r = Discourse.make_request("/t/topic/#{topic_id}/status", { 
    "status": "pinned", 
    "enabled": "true", # 주의: 문자열이어야 하며, 그렇지 않으면 토픽이 고정 해제됨
    "until": "3020-01-01 08:00+1:00" # 영원히
  }, "PUT", false)
end

```

즐거운 시간 되세요!
