Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pleroma
pleroma
Commits
43fb03be
Commit
43fb03be
authored
Mar 15, 2019
by
Eugenij
Browse files
Allow to mark a single notification as read
parent
4b3c86c1
Changes
5
Hide whitespace changes
Inline
Side-by-side
docs/Pleroma-API.md
View file @
43fb03be
...
...
@@ -108,3 +108,11 @@ See [Admin-API](Admin-API.md)
*
Response: JSON string. Returns the user flavour or the default one.
*
Example response: "glitch"
*
Note: This is intended to be used only by mastofe
## `/api/pleroma/notifications/read`
### Mark a single notification as read
*
Method
`POST`
*
Authentication: required
*
Params:
*
`id`
: notifications's id
*
Response: JSON. Returns
`{"status": "success"}`
if the reading was successful, otherwise returns
`{"error": "error_msg"}`
lib/pleroma/notification.ex
View file @
43fb03be
...
...
@@ -13,6 +13,7 @@ defmodule Pleroma.Notification do
alias
Pleroma
.
Web
.
CommonAPI
.
Utils
import
Ecto
.
Query
import
Ecto
.
Changeset
schema
"notifications"
do
field
(
:seen
,
:boolean
,
default:
false
)
...
...
@@ -22,6 +23,11 @@ defmodule Pleroma.Notification do
timestamps
()
end
def
changeset
(%
Notification
{}
=
notification
,
attrs
)
do
notification
|>
cast
(
attrs
,
[
:seen
])
end
# TODO: Make generic and unify (see activity_pub.ex)
defp
restrict_max
(
query
,
%{
"max_id"
=>
max_id
})
do
from
(
activity
in
query
,
where:
activity
.
id
<
^
max_id
)
...
...
@@ -68,6 +74,14 @@ def set_read_up_to(%{id: user_id} = _user, id) do
Repo
.
update_all
(
query
,
[])
end
def
read_one
(%
User
{}
=
user
,
notification_id
)
do
with
{
:ok
,
%
Notification
{}
=
notification
}
<-
get
(
user
,
notification_id
)
do
notification
|>
changeset
(%{
seen:
true
})
|>
Repo
.
update
()
end
end
def
get
(%{
id:
user_id
}
=
_user
,
id
)
do
query
=
from
(
...
...
lib/pleroma/web/router.ex
View file @
43fb03be
...
...
@@ -190,6 +190,12 @@ defmodule Pleroma.Web.Router do
post
(
"/blocks_import"
,
UtilController
,
:blocks_import
)
post
(
"/follow_import"
,
UtilController
,
:follow_import
)
end
scope
[]
do
pipe_through
(
:oauth_read
)
post
(
"/notifications/read"
,
UtilController
,
:notifications_read
)
end
end
scope
"/oauth"
,
Pleroma
.
Web
.
OAuth
do
...
...
lib/pleroma/web/twitter_api/controllers/util_controller.ex
View file @
43fb03be
...
...
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
alias
Comeonin
.
Pbkdf2
alias
Pleroma
.
Emoji
alias
Pleroma
.
Notification
alias
Pleroma
.
PasswordResetToken
alias
Pleroma
.
Repo
alias
Pleroma
.
User
...
...
@@ -142,6 +143,17 @@ def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}
end
end
def
notifications_read
(%{
assigns:
%{
user:
user
}}
=
conn
,
%{
"id"
=>
notification_id
})
do
with
{
:ok
,
_
}
<-
Notification
.
read_one
(
user
,
notification_id
)
do
json
(
conn
,
%{
status:
"success"
})
else
{
:error
,
message
}
->
conn
|>
put_resp_content_type
(
"application/json"
)
|>
send_resp
(
403
,
Jason
.
encode!
(%{
"error"
=>
message
}))
end
end
def
config
(
conn
,
_params
)
do
instance
=
Pleroma
.
Config
.
get
(
:instance
)
instance_fe
=
Pleroma
.
Config
.
get
(
:fe
)
...
...
test/web/twitter_api/util_controller_test.exs
View file @
43fb03be
defmodule
Pleroma
.
Web
.
TwitterAPI
.
UtilControllerTest
do
use
Pleroma
.
Web
.
ConnCase
alias
Pleroma
.
Notification
alias
Pleroma
.
Repo
alias
Pleroma
.
Web
.
CommonAPI
import
Pleroma
.
Factory
describe
"POST /api/pleroma/follow_import"
do
...
...
@@ -52,6 +55,25 @@ test "it returns HTTP 200", %{conn: conn} do
end
end
describe
"POST /api/pleroma/notifications/read"
do
test
"it marks a single notification as read"
,
%{
conn:
conn
}
do
user1
=
insert
(
:user
)
user2
=
insert
(
:user
)
{
:ok
,
activity1
}
=
CommonAPI
.
post
(
user2
,
%{
"status"
=>
"hi @
#{
user1
.
nickname
}
"
})
{
:ok
,
activity2
}
=
CommonAPI
.
post
(
user2
,
%{
"status"
=>
"hi @
#{
user1
.
nickname
}
"
})
{
:ok
,
[
notification1
]}
=
Notification
.
create_notifications
(
activity1
)
{
:ok
,
[
notification2
]}
=
Notification
.
create_notifications
(
activity2
)
conn
|>
assign
(
:user
,
user1
)
|>
post
(
"/api/pleroma/notifications/read"
,
%{
"id"
=>
"
#{
notification1
.
id
}
"
})
|>
json_response
(
:ok
)
assert
Repo
.
get
(
Notification
,
notification1
.
id
)
.
seen
refute
Repo
.
get
(
Notification
,
notification2
.
id
)
.
seen
end
end
describe
"GET /api/statusnet/config.json"
do
test
"it returns the managed config"
,
%{
conn:
conn
}
do
Pleroma
.
Config
.
put
([
:instance
,
:managed_config
],
false
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment