Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
c8f0e4b5
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
c8f0e4b5
编写于
5月 18, 2018
作者:
I
Imre
提交者:
Imre Farkas
6月 06, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Avatar API
上级
fe0ebf76
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
172 addition
and
0 deletion
+172
-0
changelogs/unreleased/45821-avatar_api.yml
changelogs/unreleased/45821-avatar_api.yml
+5
-0
doc/api/avatar.md
doc/api/avatar.md
+33
-0
lib/api/api.rb
lib/api/api.rb
+1
-0
lib/api/avatar.rb
lib/api/avatar.rb
+21
-0
lib/api/entities.rb
lib/api/entities.rb
+6
-0
spec/requests/api/avatar_spec.rb
spec/requests/api/avatar_spec.rb
+106
-0
未找到文件。
changelogs/unreleased/45821-avatar_api.yml
0 → 100644
浏览文件 @
c8f0e4b5
---
title
:
Add Avatar API
merge_request
:
19121
author
:
Imre Farkas
type
:
added
doc/api/avatar.md
0 → 100644
浏览文件 @
c8f0e4b5
# Avatar API
> [Introduced][ce-19121] in GitLab 11.0
## Get a single avatar URL
Get a single avatar URL for a given email addres. If user with matching public
email address is not found, results from external avatar services are returned.
This endpoint can be accessed without authentication. In case public visibility
is restricted, response will be
`403 Forbidden`
when unauthenticated.
```
GET /avatar?email=admin@example.com
```
| Attribute | Type | Required | Description |
| --------- | ------- | -------- | --------------------- |
|
`email`
| string | yes | Public email address of the user |
|
`size`
| integer | no | Single pixel dimension (since images are squares). Only used for avatar lookups at
`Gravatar`
or at the configured
`Libravatar`
server |
```
bash
curl https://gitlab.example.com/api/v4/avatar?email
=
admin@example.com
```
Example response:
```
json
{
"avatar_url"
:
"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80
\u
0026d=identicon"
}
```
[
ce-19121
]:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/19121
lib/api/api.rb
浏览文件 @
c8f0e4b5
...
...
@@ -83,6 +83,7 @@ module API
# Keep in alphabetical order
mount
::
API
::
AccessRequests
mount
::
API
::
Applications
mount
::
API
::
Avatar
mount
::
API
::
AwardEmoji
mount
::
API
::
Badges
mount
::
API
::
Boards
...
...
lib/api/avatar.rb
0 → 100644
浏览文件 @
c8f0e4b5
module
API
class
Avatar
<
Grape
::
API
resource
:avatar
do
desc
'Return avatar url for a user'
do
success
Entities
::
Avatar
end
params
do
requires
:email
,
type:
String
,
desc:
'Public email address of the user'
optional
:size
,
type:
Integer
,
desc:
'Single pixel dimension for Gravatar images'
end
get
do
forbidden!
(
'Unauthorized access'
)
unless
can?
(
current_user
,
:read_users_list
)
user
=
User
.
find_by_public_email
(
params
[
:email
])
user
||=
User
.
new
(
email:
params
[
:email
])
present
user
,
with:
Entities
::
Avatar
,
size:
params
[
:size
]
end
end
end
end
lib/api/entities.rb
浏览文件 @
c8f0e4b5
...
...
@@ -692,6 +692,12 @@ module API
expose
:notes
,
using:
Entities
::
Note
end
class
Avatar
<
Grape
::
Entity
expose
:avatar_url
do
|
avatarable
,
options
|
avatarable
.
avatar_url
(
only_path:
false
,
size:
options
[
:size
])
end
end
class
AwardEmoji
<
Grape
::
Entity
expose
:id
expose
:name
...
...
spec/requests/api/avatar_spec.rb
0 → 100644
浏览文件 @
c8f0e4b5
require
'spec_helper'
describe
API
::
Avatar
do
let
(
:gravatar_service
)
{
double
(
'GravatarService'
)
}
describe
'GET /avatar'
do
context
'avatar uploaded to GitLab'
do
context
'user with matching public email address'
do
let
(
:user
)
{
create
(
:user
,
:with_avatar
,
email:
'public@example.com'
,
public_email:
'public@example.com'
)
}
before
do
user
end
it
'returns the avatar url'
do
get
api
(
'/avatar'
),
{
email:
'public@example.com'
}
expect
(
response
.
status
).
to
eq
200
expect
(
json_response
[
'avatar_url'
]).
to
eql
(
"
#{
::
Settings
.
gitlab
.
base_url
}#{
user
.
avatar
.
local_url
}
"
)
end
end
context
'no user with matching public email address'
do
before
do
expect
(
GravatarService
).
to
receive
(
:new
).
and_return
(
gravatar_service
)
expect
(
gravatar_service
).
to
(
receive
(
:execute
)
.
with
(
'private@example.com'
,
nil
,
2
,
{
username:
nil
})
.
and_return
(
'https://gravatar'
))
end
it
'returns the avatar url from Gravatar'
do
get
api
(
'/avatar'
),
{
email:
'private@example.com'
}
expect
(
response
.
status
).
to
eq
200
expect
(
json_response
[
'avatar_url'
]).
to
eq
(
'https://gravatar'
)
end
end
end
context
'avatar uploaded to Gravatar'
do
context
'user with matching public email address'
do
let
(
:user
)
{
create
(
:user
,
email:
'public@example.com'
,
public_email:
'public@example.com'
)
}
before
do
user
expect
(
GravatarService
).
to
receive
(
:new
).
and_return
(
gravatar_service
)
expect
(
gravatar_service
).
to
(
receive
(
:execute
)
.
with
(
'public@example.com'
,
nil
,
2
,
{
username:
user
.
username
})
.
and_return
(
'https://gravatar'
))
end
it
'returns the avatar url from Gravatar'
do
get
api
(
'/avatar'
),
{
email:
'public@example.com'
}
expect
(
response
.
status
).
to
eq
200
expect
(
json_response
[
'avatar_url'
]).
to
eq
(
'https://gravatar'
)
end
end
context
'no user with matching public email address'
do
before
do
expect
(
GravatarService
).
to
receive
(
:new
).
and_return
(
gravatar_service
)
expect
(
gravatar_service
).
to
(
receive
(
:execute
)
.
with
(
'private@example.com'
,
nil
,
2
,
{
username:
nil
})
.
and_return
(
'https://gravatar'
))
end
it
'returns the avatar url from Gravatar'
do
get
api
(
'/avatar'
),
{
email:
'private@example.com'
}
expect
(
response
.
status
).
to
eq
200
expect
(
json_response
[
'avatar_url'
]).
to
eq
(
'https://gravatar'
)
end
end
context
'public visibility level restricted'
do
let
(
:user
)
{
create
(
:user
,
:with_avatar
,
email:
'public@example.com'
,
public_email:
'public@example.com'
)
}
before
do
user
stub_application_setting
(
restricted_visibility_levels:
[
Gitlab
::
VisibilityLevel
::
PUBLIC
])
end
context
'when authenticated'
do
it
'returns the avatar url'
do
get
api
(
'/avatar'
,
user
),
{
email:
'public@example.com'
}
expect
(
response
.
status
).
to
eq
200
expect
(
json_response
[
'avatar_url'
]).
to
eql
(
"
#{
::
Settings
.
gitlab
.
base_url
}#{
user
.
avatar
.
local_url
}
"
)
end
end
context
'when unauthenticated'
do
it_behaves_like
'403 response'
do
let
(
:request
)
{
get
api
(
'/avatar'
),
{
email:
'public@example.com'
}
}
end
end
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录