Skip to content

Commit dfa43ee

Browse files
committed
notifications
1 parent 2121986 commit dfa43ee

File tree

8 files changed

+181
-5
lines changed

8 files changed

+181
-5
lines changed

assets/static/js/app.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ $('#changePassword, #userForm, #projectForm, #systemSettings').on('hide.bs.modal
4444
$(this).find('.modal-dialog form').trigger("reset");
4545
});
4646

47+
$('#changeNotificationsSettings').on('show.bs.modal', function (event) {
48+
49+
$.ajax({
50+
method : 'GET',
51+
url : '/notification/method/',
52+
dataType : 'json',
53+
success : function(data, event) {
54+
55+
var select = [];
56+
57+
$('#notification_text_id').val(data.text_id);
58+
59+
['none', 'email', 'telegram'].forEach(function(item, i, arr) {
60+
var selected = '';
61+
if (typeof(data.method) !== undefined && data.method == item) {
62+
selected = 'selected';
63+
}
64+
select.push('<option value="' + item + '" ' + selected + '>' + item + '</option>');
65+
});
66+
67+
$('#notification_method').html(select.join(""));
68+
},
69+
error: function(request, status, error) {
70+
71+
var data = JSON.parse(request.responseText);
72+
73+
if (typeof(data.error) !== undefined) {
74+
75+
alert(data.error);
76+
77+
return;
78+
}
79+
80+
alert(error);
81+
}
82+
});
83+
});
84+
4785
$('#projectForm').on('show.bs.modal', function (event) {
4886

4987
var button = $(event.relatedTarget);

assets/templates_src/internal/layout/header.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h4 class="modal-title">Change password</h4>
5555

5656
<div class="modal fade" id="changeNotificationsSettings" tabindex="-1" role="dialog">
5757
<div class="modal-dialog" role="document">
58-
<form method="post" action="/notifications/update-method/">
58+
<form method="post" action="/notification/update-method/">
5959
<div class="modal-content">
6060
<div class="modal-header">
6161
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
@@ -64,13 +64,11 @@ <h4 class="modal-title">Change notifications settings</h4>
6464
<div class="modal-body">
6565
<div class="input-group">
6666
<div class="input-group-btn">
67-
<select class="btn btn-default dropdown-toggle" style="height: 34px" name="method">
67+
<select class="btn btn-default dropdown-toggle" style="height: 34px" name="method" id="notification_method">
6868
<option value="none">none</option>
69-
<option value="none">email</option>
70-
<option value="none">telegram</option>
7169
</select>
7270
</div>
73-
<input type="text" name="identifier" class="form-control">
71+
<input type="text" name="text_id" id="notification_text_id" class="form-control">
7472
</div>
7573
</div>
7674
<div class="modal-footer">

src/app/app_route.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"github.com/postgres-ci/app-server/src/app/controllers"
55
"github.com/postgres-ci/app-server/src/app/controllers/auth"
6+
"github.com/postgres-ci/app-server/src/app/controllers/notification"
67
"github.com/postgres-ci/app-server/src/app/controllers/password"
78
"github.com/postgres-ci/app-server/src/app/controllers/project"
89
"github.com/postgres-ci/app-server/src/app/controllers/users"
@@ -17,4 +18,5 @@ func (app *app) route() {
1718
project.Route(app.Server)
1819
webhooks.Route(app.Server)
1920
controllers.Index(app.Server)
21+
notification.Route(app.Server)
2022
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package notification
2+
3+
import (
4+
"github.com/postgres-ci/app-server/src/app/models/auth"
5+
"github.com/postgres-ci/app-server/src/app/models/notification"
6+
"github.com/postgres-ci/app-server/src/common/errors"
7+
"github.com/postgres-ci/app-server/src/tools/render"
8+
"github.com/postgres-ci/http200ok"
9+
10+
"net/http"
11+
)
12+
13+
func getMethodHandler(c *http200ok.Context) {
14+
15+
method, err := notification.GetMethod(c.Get("CurrentUser").(*auth.User).ID)
16+
17+
if err != nil {
18+
19+
code := http.StatusInternalServerError
20+
21+
if e, ok := err.(*errors.Error); ok {
22+
23+
code = e.Code
24+
}
25+
26+
render.JSONError(c, code, err.Error())
27+
28+
return
29+
}
30+
31+
render.JSON(c, method)
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package notification
2+
3+
import (
4+
"github.com/postgres-ci/http200ok"
5+
)
6+
7+
func Route(server *http200ok.Server) {
8+
9+
server.Get("/notification/method/", getMethodHandler)
10+
server.Post("/notification/update-method/", updateMethodHandler)
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package notification
2+
3+
import (
4+
"github.com/postgres-ci/app-server/src/app/models/auth"
5+
"github.com/postgres-ci/app-server/src/app/models/notification"
6+
"github.com/postgres-ci/app-server/src/common/errors"
7+
"github.com/postgres-ci/app-server/src/tools/render"
8+
"github.com/postgres-ci/http200ok"
9+
10+
"net/http"
11+
)
12+
13+
func updateMethodHandler(c *http200ok.Context) {
14+
15+
var (
16+
currentUser = c.Get("CurrentUser").(*auth.User)
17+
method = c.Request.PostFormValue("method")
18+
textID = c.Request.PostFormValue("text_id")
19+
)
20+
21+
if err := notification.UpdateMethod(currentUser.ID, method, textID); err != nil {
22+
23+
code := http.StatusInternalServerError
24+
25+
if e, ok := err.(*errors.Error); ok {
26+
27+
code = e.Code
28+
}
29+
30+
render.JSONError(c, code, err.Error())
31+
32+
return
33+
}
34+
35+
render.JSONok(c)
36+
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package notification
2+
3+
import (
4+
log "github.com/Sirupsen/logrus"
5+
"github.com/postgres-ci/app-server/src/common/errors"
6+
"github.com/postgres-ci/app-server/src/env"
7+
)
8+
9+
type method struct {
10+
Method string `db:"method" json:"method"`
11+
TextID string `db:"text_id" json:"text_id"`
12+
IntID int64 `db:"int_id" json:"int_id"`
13+
}
14+
15+
func GetMethod(userID int32) (*method, error) {
16+
17+
var result method
18+
19+
err := env.Connect().Get(&result, `SELECT method, text_id, int_id FROM notification.get_method($1)`, userID)
20+
21+
if err != nil {
22+
23+
err := errors.Wrap(err)
24+
25+
if err.(*errors.Error).IsFatal() {
26+
27+
log.Errorf("Could not fetch user notification method: %v", err)
28+
}
29+
30+
return nil, err
31+
}
32+
33+
return &result, nil
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package notification
2+
3+
import (
4+
log "github.com/Sirupsen/logrus"
5+
"github.com/postgres-ci/app-server/src/common/errors"
6+
"github.com/postgres-ci/app-server/src/env"
7+
)
8+
9+
func UpdateMethod(userID int32, method, textID string) error {
10+
11+
if _, err := env.Connect().Exec("SELECT notification.update_method($1, $2, $3)", userID, method, textID); err != nil {
12+
13+
err := errors.Wrap(err)
14+
15+
if err.(*errors.Error).IsFatal() {
16+
17+
log.Errorf("Could not update notification method: %v", err)
18+
}
19+
20+
return err
21+
}
22+
23+
return nil
24+
}

0 commit comments

Comments
 (0)