4
1
Fork 0

Resolves #17 pinned field for conversation

pull/24/head
Daniel Lim 2019-08-11 11:06:19 +08:00
parent e81b91eebb
commit 1256066597
5 changed files with 72 additions and 14 deletions

View File

@ -357,7 +357,8 @@ List of conversations.
{ {
"id": "<id>", "id": "<id>",
"title": "<title>" "title": "<title>"
"picture": "<picture>" "picture": "<picture>",
"pinned: "<pinned:bool>"
}, },
... ...
] ]
@ -394,7 +395,8 @@ Conversation object.
{ {
"id": "<id>", "id": "<id>",
"title": "<title>", "title": "<title>",
"picture": "<picture>" "picture": "<picture>",
"pinned: "<pinned:bool>"
} }
``` ```
@ -408,6 +410,34 @@ Conversation object.
--- ---
### Pin Conversation
```
POST /user/conversation/:conversation/pin
```
Mark the specified conversation as pinned.
#### URL Params
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| conversation | String | Conversation's ID. | ✓ |
#### Success Response (200 OK)
Blank body.
#### Errors
| Code | Description |
| ---- | ----------- |
| 400 | Invalid `X-User-Claim` header. |
| 404 | Conversation with supplied ID could not be found in database. |
| 500 | Error occurred editing entry in the database. |
---
### Create Conversation Member* ### Create Conversation Member*
``` ```

View File

@ -248,14 +248,14 @@ func (h *Handler) GetConversations(w http.ResponseWriter, r *http.Request, p htt
// Select // Select
rows, err := h.db.Query(` rows, err := h.db.Query(`
SELECT id, CASE SELECT "conversation".id, CASE
WHEN dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id) WHEN "conversation".dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id)
ELSE title ELSE title
END AS title, END AS title,
picture "conversation".picture,
FROM "conversation" member.pinned
INNER JOIN member FROM "conversation", member
ON member.conversation = "conversation".id AND member.user = $1 WHERE member.conversation = "conversation".id AND member.user = $1
`, userID) `, userID)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -290,14 +290,14 @@ func (h *Handler) GetConversation(w http.ResponseWriter, r *http.Request, p http
// Select // Select
err := h.db.QueryRow(` err := h.db.QueryRow(`
SELECT id, CASE SELECT "conversation".id, CASE
WHEN dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id) WHEN "conversation".dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id)
ELSE title ELSE title
END AS title, END AS title,
picture "conversation" picture,
FROM "conversation" member.pinned
INNER JOIN member FROM "conversation", member
ON member.conversation = "conversation".id AND member.user = $1 AND member.conversation = $2 WHERE member.conversation = "conversation".id AND member.user = $1 AND member.conversation = $2
`, userID, conversationID).Scan(&conversation.ID, &conversation.Title, &conversation.Picture) `, userID, conversationID).Scan(&conversation.ID, &conversation.Title, &conversation.Picture)
switch { switch {
@ -643,6 +643,31 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
json.NewEncoder(w).Encode(contacts) json.NewEncoder(w).Encode(contacts)
} }
func (h *Handler) PinConversation(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
conversationID := p.ByName("conversation")
userID := r.Context().Value("user").(string)
// Check relation exists
var exists int
err := h.db.QueryRow(`SELECT 1 FROM member WHERE "user" = $1 AND "conversation" = $2`, userID, conversationID).Scan(&exists)
if err == sql.ErrNoRows {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Update relation
_, err = h.db.Exec(`UPDATE "member" SET "pinned" = TRUE WHERE "user" = $1 AND "conversation" = $2`, userID, conversationID)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(200)
}
func NewHandler(db *sql.DB) *Handler { func NewHandler(db *sql.DB) *Handler {
return &Handler{db} return &Handler{db}
} }

View File

@ -52,6 +52,7 @@ func main() {
router.GET("/user/conversation/:conversation", AuthMiddleware(h.GetConversation)) // USER MEMBER CONVERSATION router.GET("/user/conversation/:conversation", AuthMiddleware(h.GetConversation)) // USER MEMBER CONVERSATION
router.PATCH("/user/conversation/:conversation", AuthMiddleware(h.UpdateConversation)) // USER MEMBER CONVERSATION ADMIN=true -> update conversation title router.PATCH("/user/conversation/:conversation", AuthMiddleware(h.UpdateConversation)) // USER MEMBER CONVERSATION ADMIN=true -> update conversation title
//router.DELETE("/user/:user/conversation/:conversation", h.DeleteConversation) // USER MEMBER CONVERSATION -> delete membership //router.DELETE("/user/:user/conversation/:conversation", h.DeleteConversation) // USER MEMBER CONVERSATION -> delete membership
router.POST("/user/conversation/:conversation/pin", AuthMiddleware(h.PinConversation))
router.POST("/user/conversation/:conversation/member", AuthMiddleware(h.CreateConversationMember)) // USER MEMBER CONVERSATION ADMIN=true -> create new membership router.POST("/user/conversation/:conversation/member", AuthMiddleware(h.CreateConversationMember)) // USER MEMBER CONVERSATION ADMIN=true -> create new membership
router.GET("/user/conversation/:conversation/member", AuthMiddleware(h.GetConversationMembers)) // USER MEMBER CONVERSATION router.GET("/user/conversation/:conversation/member", AuthMiddleware(h.GetConversationMembers)) // USER MEMBER CONVERSATION
//router.DELETE("/user/:user/conversation/:conversation/member/:member", h.DeleteConversationMember) // USER MEMBER CONVERSATION ADMIN=true -> delete membership //router.DELETE("/user/:user/conversation/:conversation/member/:member", h.DeleteConversationMember) // USER MEMBER CONVERSATION ADMIN=true -> delete membership

View File

@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS "conversation" (
CREATE TABLE IF NOT EXISTS member ( CREATE TABLE IF NOT EXISTS member (
"user" BYTEA REFERENCES "user"(id), "user" BYTEA REFERENCES "user"(id),
"conversation" BYTEA REFERENCES "conversation"(id), "conversation" BYTEA REFERENCES "conversation"(id),
"pinned" BOOLEAN DEFAULT FALSE,
UNIQUE ("user", "conversation") UNIQUE ("user", "conversation")
); );

View File

@ -5,6 +5,7 @@ type Conversation struct {
Title string `json:"title"` // title Title string `json:"title"` // title
DM bool `json:"dm"` // dm DM bool `json:"dm"` // dm
Picture string `json:"picture"` // picture Picture string `json:"picture"` // picture
Pinned bool `json:"pinned"` // pinned
} }
type User struct { type User struct {