Module: DiscourseApi::API::Users

Included in:
Client
Defined in:
lib/discourse_api/api/users.rb

Instance Method Summary collapse

Instance Method Details

#activate(id) ⇒ Object



5
6
7
# File 'lib/discourse_api/api/users.rb', line 5

def activate(id)
  put("/admin/users/#{id}/activate")
end

#anonymize(user_id) ⇒ Object



113
114
115
# File 'lib/discourse_api/api/users.rb', line 113

def anonymize(user_id)
  put("/admin/users/#{user_id}/anonymize")
end

#by_external_id(external_id) ⇒ Object



100
101
102
103
# File 'lib/discourse_api/api/users.rb', line 100

def by_external_id(external_id)
  response = get("/users/by-external/#{external_id}")
  response[:body]["user"]
end

#check_username(username) ⇒ Object



121
122
123
124
# File 'lib/discourse_api/api/users.rb', line 121

def check_username(username)
  response = get("/users/check_username.json?username=#{CGI.escape(username)}")
  response[:body]
end

#create_user(args) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/discourse_api/api/users.rb', line 62

def create_user(args)
  args =
    API
      .params(args)
      .required(:name, :email, :password, :username)
      .optional(:active, :approved, :staged, :user_fields)
      .to_h
  post("/users", args)
end

#deactivate(user_id) ⇒ Object



126
127
128
# File 'lib/discourse_api/api/users.rb', line 126

def deactivate(user_id)
  put("/admin/users/#{user_id}/deactivate")
end

#delete_user(user_id, delete_posts = false) ⇒ Object



117
118
119
# File 'lib/discourse_api/api/users.rb', line 117

def delete_user(user_id, delete_posts = false)
  delete("/admin/users/#{user_id}.json?delete_posts=#{delete_posts}")
end

#grant_admin(user_id) ⇒ Object



81
82
83
84
# File 'lib/discourse_api/api/users.rb', line 81

def grant_admin(user_id)
  response = put("admin/users/#{user_id}/grant_admin")
  response[:body]
end

#grant_moderation(user_id) ⇒ Object



91
92
93
94
# File 'lib/discourse_api/api/users.rb', line 91

def grant_moderation(user_id)
  response = put("admin/users/#{user_id}/grant_moderation")
  response[:body]
end

#list_users(type, params = {}) ⇒ Object



76
77
78
79
# File 'lib/discourse_api/api/users.rb', line 76

def list_users(type, params = {})
  response = get("admin/users/list/#{type}.json", params)
  response[:body]
end

#log_out(id) ⇒ Object



72
73
74
# File 'lib/discourse_api/api/users.rb', line 72

def log_out(id)
  post("/admin/users/#{id}/log_out")
end

#revoke_admin(user_id) ⇒ Object



86
87
88
89
# File 'lib/discourse_api/api/users.rb', line 86

def revoke_admin(user_id)
  response = put("admin/users/#{user_id}/revoke_admin")
  response[:body]
end

#revoke_moderation(user_id) ⇒ Object



96
97
98
# File 'lib/discourse_api/api/users.rb', line 96

def revoke_moderation(user_id)
  put("admin/users/#{user_id}/revoke_moderation")
end

#suspend(user_id, suspend_until, reason) ⇒ Object



105
106
107
# File 'lib/discourse_api/api/users.rb', line 105

def suspend(user_id, suspend_until, reason)
  put("/admin/users/#{user_id}/suspend", suspend_until: suspend_until, reason: reason)
end

#unsuspend(user_id) ⇒ Object



109
110
111
# File 'lib/discourse_api/api/users.rb', line 109

def unsuspend(user_id)
  put("/admin/users/#{user_id}/unsuspend")
end

#update_avatar(username, args) ⇒ Object



19
20
21
22
23
24
# File 'lib/discourse_api/api/users.rb', line 19

def update_avatar(username, args)
  args =
    API.params(args).optional(:file, :url).default(type: "avatar", synchronous: true).to_h
  upload_response = post("/uploads", args)
  put("/u/#{username}/preferences/avatar/pick", upload_id: upload_response["id"])
end

#update_email(username, email) ⇒ Object



26
27
28
# File 'lib/discourse_api/api/users.rb', line 26

def update_email(username, email)
  put("/u/#{username}/preferences/email", email: email)
end

#update_trust_level(user_id, args) ⇒ Object



56
57
58
59
60
# File 'lib/discourse_api/api/users.rb', line 56

def update_trust_level(user_id, args)
  args = API.params(args).required(:level).to_h
  response = put("/admin/users/#{user_id}/trust_level", args)
  response[:body]
end

#update_user(username, args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/discourse_api/api/users.rb', line 30

def update_user(username, args)
  args =
    API
      .params(args)
      .optional(
        :name,
        :title,
        :bio_raw,
        :location,
        :website,
        :profile_background,
        :card_background,
        :email_messages_level,
        :mailing_list_mode,
        :homepage_id,
        :theme_ids,
        :user_fields,
      )
      .to_h
  put("/u/#{username}", args)
end

#update_username(username, new_username) ⇒ Object



52
53
54
# File 'lib/discourse_api/api/users.rb', line 52

def update_username(username, new_username)
  put("/u/#{username}/preferences/username", new_username: new_username)
end

#user(username, params = {}) ⇒ Object



9
10
11
12
# File 'lib/discourse_api/api/users.rb', line 9

def user(username, params = {})
  response = get("/users/#{username}.json", params)
  response[:body]["user"]
end

#user_sso(user_id) ⇒ Object



14
15
16
17
# File 'lib/discourse_api/api/users.rb', line 14

def user_sso(user_id)
  response = get("/admin/users/#{user_id}.json")
  response[:body]["single_sign_on_record"]
end