diff --git a/codegen/codegen.php b/codegen/codegen.php index b33e1754f..f69b9785f 100755 --- a/codegen/codegen.php +++ b/codegen/codegen.php @@ -47,7 +47,18 @@ exec("find -L " . __DIR__ . "/" . $project . " -name index.php", $generators); $api_files = glob($project . "/docs/src/spec/*.json"); $apis = array_map(function($filename) { - return array_change_key_case_recursive(json_decode(file_get_contents($filename), true)); + $api = array_change_key_case_recursive(json_decode(file_get_contents($filename), true)); + if (empty($api['parameters'])) { + $api['parameters'] = array(); + } + foreach ($api['apis'] as $kk => $call) { + if (empty($call['parameters'])) { + $call['parameters'] = array(); + } + $call['parameters'] = array_merge($api['parameters'], $call['parameters']); + $api['apis'][$kk] = $call; + } + return $api; }, $api_files); usort($apis, function($a, $b) { diff --git a/crm/docs/src/spec.json b/crm/docs/src/spec.json index a1a0a06da..ef3984201 100644 --- a/crm/docs/src/spec.json +++ b/crm/docs/src/spec.json @@ -4,7 +4,7 @@ "description": "CRM input field definitions", "package": "crm", "entrypoint": "field", - "path": "field", + "path": "/field", "authentication": [], "struct": [ { @@ -40,7 +40,7 @@ "description": "CRM module definitions", "package": "crm", "entrypoint": "module", - "path": "module", + "path": "/module", "authentication": [], "struct": [ { diff --git a/crm/docs/src/spec/field.json b/crm/docs/src/spec/field.json index 6bff0cf27..632a2e6b5 100644 --- a/crm/docs/src/spec/field.json +++ b/crm/docs/src/spec/field.json @@ -20,6 +20,7 @@ "name": "Field" } ], + "Parameters": null, "Protocol": "", "Authentication": [], "Path": "/field", diff --git a/crm/docs/src/spec/module.json b/crm/docs/src/spec/module.json index 96889eb28..6ef191b7f 100644 --- a/crm/docs/src/spec/module.json +++ b/crm/docs/src/spec/module.json @@ -42,6 +42,7 @@ "name": "ModuleContentRow" } ], + "Parameters": null, "Protocol": "", "Authentication": [], "Path": "/module", diff --git a/sam/docs/README.md b/sam/docs/README.md index 28bb8ede7..b12ac2092 100644 --- a/sam/docs/README.md +++ b/sam/docs/README.md @@ -318,70 +318,69 @@ The following event types may be sent with a message event: | EDITED | A message has been edited by the sender | | REMOVED | A message has been removed by the sender | -## New message / edit message +## Post new message to the channel #### Method | URI | Protocol | Method | Authentication | | --- | -------- | ------ | -------------- | -| `/message/edit` | HTTP/S | POST | Client ID, Session ID | +| `/channels/{channelId}/messages/` | HTTP/S | POST | Client ID, Session ID | #### Request parameters | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | -| id | uint64 | POST | Message ID | N/A | NO | -| channel_id | uint64 | POST | Channel ID where to post message | N/A | NO | | contents | string | POST | Message contents (markdown) | N/A | YES | +## Edit existing message + +#### Method + +| URI | Protocol | Method | Authentication | +| --- | -------- | ------ | -------------- | +| `/channels/{channelId}/messages/{messageId}` | HTTP/S | PUT | Client ID, Session ID | + +#### Request parameters + +| Parameter | Type | Method | Description | Default | Required? | +| --------- | ---- | ------ | ----------- | ------- | --------- | +| messageId | uint64 | PATH | Message ID | N/A | YES | +| contents | string | POST | Message contents (markdown) | N/A | YES | + +## Delete existing message + +#### Method + +| URI | Protocol | Method | Authentication | +| --- | -------- | ------ | -------------- | +| `/channels/{channelId}/messages/{messageId}` | HTTP/S | DELETE | Client ID, Session ID | + +#### Request parameters + +| Parameter | Type | Method | Description | Default | Required? | +| --------- | ---- | ------ | ----------- | ------- | --------- | +| messageId | uint64 | PATH | Message ID | N/A | YES | + ## Attach file to message #### Method | URI | Protocol | Method | Authentication | | --- | -------- | ------ | -------------- | -| `/message/attach` | HTTP/S | PUT | Client ID, Session ID | +| `/channels/{channelId}/messages/{messageId}/attach` | HTTP/S | PUT | Client ID, Session ID | #### Request parameters | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | -## Remove message - -#### Method - -| URI | Protocol | Method | Authentication | -| --- | -------- | ------ | -------------- | -| `/message/remove` | HTTP/S | DELETE | Client ID, Session ID | - -#### Request parameters - -| Parameter | Type | Method | Description | Default | Required? | -| --------- | ---- | ------ | ----------- | ------- | --------- | -| id | uint64 | GET | Message ID | N/A | YES | - -## Read message details - -#### Method - -| URI | Protocol | Method | Authentication | -| --- | -------- | ------ | -------------- | -| `/message/read` | HTTP/S | GET | Client ID, Session ID | - -#### Request parameters - -| Parameter | Type | Method | Description | Default | Required? | -| --------- | ---- | ------ | ----------- | ------- | --------- | -| channel_id | uint64 | POST | Channel ID to read messages from | N/A | YES | - ## Search messages #### Method | URI | Protocol | Method | Authentication | | --- | -------- | ------ | -------------- | -| `/message/search` | HTTP/S | GET | Client ID, Session ID | +| `/channels/{channelId}/messages/search` | HTTP/S | GET | Client ID, Session ID | #### Request parameters @@ -396,27 +395,85 @@ The following event types may be sent with a message event: | URI | Protocol | Method | Authentication | | --- | -------- | ------ | -------------- | -| `/message/pin` | HTTP/S | POST | Client ID, Session ID | +| `/channels/{channelId}/messages/{messageId}/pin` | HTTP/S | POST | Client ID, Session ID | #### Request parameters | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | -| id | uint64 | POST | Message ID | N/A | YES | +| messageId | uint64 | PATH | Message ID | N/A | YES | -## Flag message for user (bookmark) +## Pin message to channel (public bookmark) #### Method | URI | Protocol | Method | Authentication | | --- | -------- | ------ | -------------- | -| `/message/flag` | HTTP/S | POST | Client ID, Session ID | +| `/channels/{channelId}/messages/{messageId}/pin` | HTTP/S | DELETE | Client ID, Session ID | #### Request parameters | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | -| id | uint64 | POST | Message ID | N/A | YES | +| messageId | uint64 | PATH | Message ID | N/A | YES | + +## Flag a message (private bookmark) + +#### Method + +| URI | Protocol | Method | Authentication | +| --- | -------- | ------ | -------------- | +| `/channels/{channelId}/messages/{messageId}/flag` | HTTP/S | POST | Client ID, Session ID | + +#### Request parameters + +| Parameter | Type | Method | Description | Default | Required? | +| --------- | ---- | ------ | ----------- | ------- | --------- | +| messageId | uint64 | PATH | Message ID | N/A | YES | + +## Remove flag from message (private bookmark) + +#### Method + +| URI | Protocol | Method | Authentication | +| --- | -------- | ------ | -------------- | +| `/channels/{channelId}/messages/{messageId}/flag` | HTTP/S | DELETE | Client ID, Session ID | + +#### Request parameters + +| Parameter | Type | Method | Description | Default | Required? | +| --------- | ---- | ------ | ----------- | ------- | --------- | +| messageId | uint64 | PATH | Message ID | N/A | YES | + +## React to a message + +#### Method + +| URI | Protocol | Method | Authentication | +| --- | -------- | ------ | -------------- | +| `/channels/{channelId}/messages/{messageId}/react` | HTTP/S | POST | Client ID, Session ID | + +#### Request parameters + +| Parameter | Type | Method | Description | Default | Required? | +| --------- | ---- | ------ | ----------- | ------- | --------- | +| messageId | uint64 | PATH | Message ID | N/A | YES | +| reaction | string | POST | Reaction | N/A | YES | + +## Delete reaction from a message + +#### Method + +| URI | Protocol | Method | Authentication | +| --- | -------- | ------ | -------------- | +| `/channels/{channelId}/messages/{messageId}/react/{reactionId}` | HTTP/S | DELETE | Client ID, Session ID | + +#### Request parameters + +| Parameter | Type | Method | Description | Default | Required? | +| --------- | ---- | ------ | ----------- | ------- | --------- | +| messageId | uint64 | PATH | Message ID | N/A | YES | +| reactionId | uint64 | PATH | Reaction ID | N/A | YES | diff --git a/sam/docs/src/spec.json b/sam/docs/src/spec.json index 20a6ee9dc..28818479b 100644 --- a/sam/docs/src/spec.json +++ b/sam/docs/src/spec.json @@ -298,6 +298,12 @@ { "title": "Messages", "package": "sam", + "path": "/channels/{channelId}/messages", + "parameters": { + "path": [ + { "name": "channelId", "type": "uint64", "required": true, "title": "Channel ID" } + ] + }, "entrypoint": "message", "authentication": ["Client ID", "Session ID"], "struct": [ @@ -310,7 +316,7 @@ { "name": "Message", "type": "string" }, { "name": "UserId", "type": "uint64", "dbname": "rel_user" }, { "name": "ChannelId", "type": "uint64", "dbname": "rel_channel"}, - { "name": "ReplyTo", "type": "uint64", "dbname": "rel_message" }, + { "name": "ReplyTo", "type": "uint64", "dbname": "reply_to" }, { "name": "UpdatedAt", "type": "*time.Time", "tag": "json:\",omitempty\"", "complex": true }, { "name": "DeletedAt", "type": "*time.Time", "tag": "json:\",omitempty\"", "complex": true } @@ -343,50 +349,58 @@ ], "apis": [ { - "name": "edit", + "name": "create", + "path": "/", "method": "POST", - "title": "New message / edit message", + "title": "Post new message to the channel", "parameters": { "post": [ - { "type": "uint64", "name": "id", "required": false, "title": "Message ID" }, - { "type": "uint64", "name": "channel_id", "required": false, "title": "Channel ID where to post message" }, { "type": "string", "name": "contents", "required": true, "title": "Message contents (markdown)" } ] } }, + { + "name": "edit", + "path": "/{messageId}", + "method": "PUT", + "title": "Edit existing message", + "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ], + "post": [ + { "type": "string", "name": "contents", "required": true, "title": "Message contents (markdown)" } + ] + } + }, + { + "name": "delete", + "path": "/{messageId}", + "method": "DELETE", + "title": "Delete existing message", + "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ] + } + }, { "name": "attach", + "path": "/{messageId}/attach", "method": "PUT", "title": "Attach file to message", "params": { - "get": [ - { "name": "message_id", "type": "uint64", "title": "Message ID" }, + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ], + "post": [ { "name": "name", "type": "string", "title": "File name to drop" } ] } }, - { - "name": "remove", - "method": "DELETE", - "title": "Remove message", - "parameters": { - "get": [ - { "type": "uint64", "name": "id", "required": true, "title": "Message ID" } - ] - } - }, - { - "name": "read", - "method": "GET", - "title": "Read message details", - "parameters": { - "post": [ - { "type": "uint64", "name": "channel_id", "required": true, "title": "Channel ID to read messages from" } - ] - } - }, { "name": "search", + "path": "/search", "method": "GET", "title": "Search messages", "parameters": { @@ -404,21 +418,71 @@ }, { "name": "pin", + "path": "/{messageId}/pin", "method": "POST", "title": "Pin message to channel (public bookmark)", "parameters": { - "post": [ - { "type": "uint64", "name": "id", "required": true, "title": "Message ID" } + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ] + } + }, + { + "name": "unpin", + "path": "/{messageId}/pin", + "method": "DELETE", + "title": "Pin message to channel (public bookmark)", + "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } ] } }, { "name": "flag", + "path": "/{messageId}/flag", "method": "POST", - "title": "Flag message for user (bookmark)", + "title": "Flag a message (private bookmark)", "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ] + } + }, + { + "name": "deflag", + "path": "/{messageId}/flag", + "method": "DELETE", + "title": "Remove flag from message (private bookmark)", + "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ] + } + }, + { + "name": "react", + "path": "/{messageId}/react", + "method": "POST", + "title": "React to a message", + "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" } + ], "post": [ - { "type": "uint64", "name": "id", "required": true, "title": "Message ID" } + { "type": "string", "name": "reaction", "required": true, "title": "Reaction" } + ] + } + }, + { + "name": "unreact", + "path": "/{messageId}/react/{reactionId}", + "method": "DELETE", + "title": "Delete reaction from a message", + "parameters": { + "path": [ + { "name": "messageId", "type": "uint64", "required": true, "title": "Message ID" }, + { "name": "reactionId", "type": "uint64", "required": true, "title": "Reaction ID" } ] } } diff --git a/sam/docs/src/spec/channel.json b/sam/docs/src/spec/channel.json index 589d2bb19..e4a56f53d 100644 --- a/sam/docs/src/spec/channel.json +++ b/sam/docs/src/spec/channel.json @@ -50,6 +50,7 @@ "name": "Channel" } ], + "Parameters": null, "Protocol": "", "Authentication": [ "Client ID", diff --git a/sam/docs/src/spec/message.json b/sam/docs/src/spec/message.json index ec14369f1..3460635e2 100644 --- a/sam/docs/src/spec/message.json +++ b/sam/docs/src/spec/message.json @@ -28,7 +28,7 @@ "type": "uint64" }, { - "dbname": "rel_message", + "dbname": "reply_to", "name": "ReplyTo", "type": "uint64" }, @@ -127,32 +127,30 @@ "name": "Attachment" } ], + "Parameters": { + "path": [ + { + "name": "channelId", + "required": true, + "title": "Channel ID", + "type": "uint64" + } + ] + }, "Protocol": "", "Authentication": [ "Client ID", "Session ID" ], - "Path": "/message", + "Path": "/channels/{channelId}/messages", "APIs": [ { - "Name": "edit", + "Name": "create", "Method": "POST", - "Title": "New message / edit message", - "Path": "/edit", + "Title": "Post new message to the channel", + "Path": "/", "Parameters": { "post": [ - { - "name": "id", - "required": false, - "title": "Message ID", - "type": "uint64" - }, - { - "name": "channel_id", - "required": false, - "title": "Channel ID where to post message", - "type": "uint64" - }, { "name": "contents", "required": true, @@ -163,21 +161,38 @@ } }, { - "Name": "attach", + "Name": "edit", "Method": "PUT", - "Title": "Attach file to message", - "Path": "/attach", - "Parameters": null + "Title": "Edit existing message", + "Path": "/{messageId}", + "Parameters": { + "path": [ + { + "name": "messageId", + "required": true, + "title": "Message ID", + "type": "uint64" + } + ], + "post": [ + { + "name": "contents", + "required": true, + "title": "Message contents (markdown)", + "type": "string" + } + ] + } }, { - "Name": "remove", + "Name": "delete", "Method": "DELETE", - "Title": "Remove message", - "Path": "/remove", + "Title": "Delete existing message", + "Path": "/{messageId}", "Parameters": { - "get": [ + "path": [ { - "name": "id", + "name": "messageId", "required": true, "title": "Message ID", "type": "uint64" @@ -186,20 +201,11 @@ } }, { - "Name": "read", - "Method": "GET", - "Title": "Read message details", - "Path": "/read", - "Parameters": { - "post": [ - { - "name": "channel_id", - "required": true, - "title": "Channel ID to read messages from", - "type": "uint64" - } - ] - } + "Name": "attach", + "Method": "PUT", + "Title": "Attach file to message", + "Path": "/{messageId}/attach", + "Parameters": null }, { "Name": "search", @@ -233,11 +239,27 @@ "Name": "pin", "Method": "POST", "Title": "Pin message to channel (public bookmark)", - "Path": "/pin", + "Path": "/{messageId}/pin", "Parameters": { - "post": [ + "path": [ { - "name": "id", + "name": "messageId", + "required": true, + "title": "Message ID", + "type": "uint64" + } + ] + } + }, + { + "Name": "unpin", + "Method": "DELETE", + "Title": "Pin message to channel (public bookmark)", + "Path": "/{messageId}/pin", + "Parameters": { + "path": [ + { + "name": "messageId", "required": true, "title": "Message ID", "type": "uint64" @@ -248,18 +270,80 @@ { "Name": "flag", "Method": "POST", - "Title": "Flag message for user (bookmark)", - "Path": "/flag", + "Title": "Flag a message (private bookmark)", + "Path": "/{messageId}/flag", "Parameters": { - "post": [ + "path": [ { - "name": "id", + "name": "messageId", "required": true, "title": "Message ID", "type": "uint64" } ] } + }, + { + "Name": "deflag", + "Method": "DELETE", + "Title": "Remove flag from message (private bookmark)", + "Path": "/{messageId}/flag", + "Parameters": { + "path": [ + { + "name": "messageId", + "required": true, + "title": "Message ID", + "type": "uint64" + } + ] + } + }, + { + "Name": "react", + "Method": "POST", + "Title": "React to a message", + "Path": "/{messageId}/react", + "Parameters": { + "path": [ + { + "name": "messageId", + "required": true, + "title": "Message ID", + "type": "uint64" + } + ], + "post": [ + { + "name": "reaction", + "required": true, + "title": "Reaction", + "type": "string" + } + ] + } + }, + { + "Name": "unreact", + "Method": "DELETE", + "Title": "Delete reaction from a message", + "Path": "/{messageId}/react/{reactionId}", + "Parameters": { + "path": [ + { + "name": "messageId", + "required": true, + "title": "Message ID", + "type": "uint64" + }, + { + "name": "reactionId", + "required": true, + "title": "Reaction ID", + "type": "uint64" + } + ] + } } ] } \ No newline at end of file diff --git a/sam/docs/src/spec/organisation.json b/sam/docs/src/spec/organisation.json index ff805b3dd..509b5ee50 100644 --- a/sam/docs/src/spec/organisation.json +++ b/sam/docs/src/spec/organisation.json @@ -33,6 +33,7 @@ "name": "Organisation" } ], + "Parameters": null, "Protocol": "", "Authentication": [ "Client ID", diff --git a/sam/docs/src/spec/team.json b/sam/docs/src/spec/team.json index e36251d33..f58e19533 100644 --- a/sam/docs/src/spec/team.json +++ b/sam/docs/src/spec/team.json @@ -45,6 +45,7 @@ "name": "Team" } ], + "Parameters": null, "Protocol": "", "Authentication": [ "Client ID", diff --git a/sam/docs/src/spec/user.json b/sam/docs/src/spec/user.json index ed62c4da0..063de3f01 100644 --- a/sam/docs/src/spec/user.json +++ b/sam/docs/src/spec/user.json @@ -48,6 +48,7 @@ "name": "User" } ], + "Parameters": null, "Protocol": "", "Authentication": [ "Client ID", diff --git a/sam/rest/message.go b/sam/rest/message.go index db95a99c4..de5d802e6 100644 --- a/sam/rest/message.go +++ b/sam/rest/message.go @@ -14,22 +14,22 @@ func (Message) New() *Message { return &Message{} } +func (ctrl *Message) Create(ctx context.Context, r *server.MessageCreateRequest) (interface{}, error) { + return nil, errors.New("Not implemented: Message.create") +} + func (ctrl *Message) Edit(ctx context.Context, r *server.MessageEditRequest) (interface{}, error) { return nil, errors.New("Not implemented: Message.edit") } +func (ctrl *Message) Delete(ctx context.Context, r *server.MessageDeleteRequest) (interface{}, error) { + return nil, errors.New("Not implemented: Message.delete") +} + func (ctrl *Message) Attach(ctx context.Context, r *server.MessageAttachRequest) (interface{}, error) { return nil, errors.New("Not implemented: Message.attach") } -func (ctrl *Message) Remove(ctx context.Context, r *server.MessageRemoveRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Message.remove") -} - -func (ctrl *Message) Read(ctx context.Context, r *server.MessageReadRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Message.read") -} - func (ctrl *Message) Search(ctx context.Context, r *server.MessageSearchRequest) (interface{}, error) { return nil, errors.New("Not implemented: Message.search") } @@ -38,6 +38,22 @@ func (ctrl *Message) Pin(ctx context.Context, r *server.MessagePinRequest) (inte return nil, errors.New("Not implemented: Message.pin") } +func (ctrl *Message) Unpin(ctx context.Context, r *server.MessageUnpinRequest) (interface{}, error) { + return nil, errors.New("Not implemented: Message.unpin") +} + func (ctrl *Message) Flag(ctx context.Context, r *server.MessageFlagRequest) (interface{}, error) { return nil, errors.New("Not implemented: Message.flag") } + +func (ctrl *Message) Deflag(ctx context.Context, r *server.MessageDeflagRequest) (interface{}, error) { + return nil, errors.New("Not implemented: Message.deflag") +} + +func (ctrl *Message) React(ctx context.Context, r *server.MessageReactRequest) (interface{}, error) { + return nil, errors.New("Not implemented: Message.react") +} + +func (ctrl *Message) Unreact(ctx context.Context, r *server.MessageUnreactRequest) (interface{}, error) { + return nil, errors.New("Not implemented: Message.unreact") +} diff --git a/sam/rest/router.go b/sam/rest/router.go index 31dbec4f0..199873a7a 100644 --- a/sam/rest/router.go +++ b/sam/rest/router.go @@ -39,14 +39,18 @@ func MountRoutes(r chi.Router) { }) r.Group(func(r chi.Router) { r.Use(message.Message.Authenticator()) - r.Route("/message", func(r chi.Router) { - r.Post("/edit", message.Edit) - r.Put("/attach", message.Attach) - r.Delete("/remove", message.Remove) - r.Get("/read", message.Read) + r.Route("/channels/{channelId}/messages", func(r chi.Router) { + r.Post("/", message.Create) + r.Put("/{messageId}", message.Edit) + r.Delete("/{messageId}", message.Delete) + r.Put("/{messageId}/attach", message.Attach) r.Get("/search", message.Search) - r.Post("/pin", message.Pin) - r.Post("/flag", message.Flag) + r.Post("/{messageId}/pin", message.Pin) + r.Delete("/{messageId}/pin", message.Unpin) + r.Post("/{messageId}/flag", message.Flag) + r.Delete("/{messageId}/flag", message.Deflag) + r.Post("/{messageId}/react", message.React) + r.Delete("/{messageId}/react/{reactionId}", message.Unreact) }) }) r.Group(func(r chi.Router) { diff --git a/sam/rest/server/message.go b/sam/rest/server/message.go index 771ab158b..afc1e1535 100644 --- a/sam/rest/server/message.go +++ b/sam/rest/server/message.go @@ -27,13 +27,17 @@ type MessageHandlers struct { // Internal API interface type MessageAPI interface { + Create(context.Context, *MessageCreateRequest) (interface{}, error) Edit(context.Context, *MessageEditRequest) (interface{}, error) + Delete(context.Context, *MessageDeleteRequest) (interface{}, error) Attach(context.Context, *MessageAttachRequest) (interface{}, error) - Remove(context.Context, *MessageRemoveRequest) (interface{}, error) - Read(context.Context, *MessageReadRequest) (interface{}, error) Search(context.Context, *MessageSearchRequest) (interface{}, error) Pin(context.Context, *MessagePinRequest) (interface{}, error) + Unpin(context.Context, *MessageUnpinRequest) (interface{}, error) Flag(context.Context, *MessageFlagRequest) (interface{}, error) + Deflag(context.Context, *MessageDeflagRequest) (interface{}, error) + React(context.Context, *MessageReactRequest) (interface{}, error) + Unreact(context.Context, *MessageUnreactRequest) (interface{}, error) // Authenticate API requests Authenticator() func(http.Handler) http.Handler @@ -41,11 +45,15 @@ type MessageAPI interface { // HTTP API interface type MessageHandlersAPI interface { + Create(http.ResponseWriter, *http.Request) Edit(http.ResponseWriter, *http.Request) + Delete(http.ResponseWriter, *http.Request) Attach(http.ResponseWriter, *http.Request) - Remove(http.ResponseWriter, *http.Request) - Read(http.ResponseWriter, *http.Request) Search(http.ResponseWriter, *http.Request) Pin(http.ResponseWriter, *http.Request) + Unpin(http.ResponseWriter, *http.Request) Flag(http.ResponseWriter, *http.Request) + Deflag(http.ResponseWriter, *http.Request) + React(http.ResponseWriter, *http.Request) + Unreact(http.ResponseWriter, *http.Request) } diff --git a/sam/rest/server/message_handlers.go b/sam/rest/server/message_handlers.go index 74baf63da..db4031b95 100644 --- a/sam/rest/server/message_handlers.go +++ b/sam/rest/server/message_handlers.go @@ -21,22 +21,22 @@ import ( "github.com/titpetric/factory/resputil" ) +func (mh *MessageHandlers) Create(w http.ResponseWriter, r *http.Request) { + params := MessageCreateRequest{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Create(r.Context(), params) }) +} func (mh *MessageHandlers) Edit(w http.ResponseWriter, r *http.Request) { params := MessageEditRequest{}.new() resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Edit(r.Context(), params) }) } +func (mh *MessageHandlers) Delete(w http.ResponseWriter, r *http.Request) { + params := MessageDeleteRequest{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Delete(r.Context(), params) }) +} func (mh *MessageHandlers) Attach(w http.ResponseWriter, r *http.Request) { params := MessageAttachRequest{}.new() resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Attach(r.Context(), params) }) } -func (mh *MessageHandlers) Remove(w http.ResponseWriter, r *http.Request) { - params := MessageRemoveRequest{}.new() - resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Remove(r.Context(), params) }) -} -func (mh *MessageHandlers) Read(w http.ResponseWriter, r *http.Request) { - params := MessageReadRequest{}.new() - resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Read(r.Context(), params) }) -} func (mh *MessageHandlers) Search(w http.ResponseWriter, r *http.Request) { params := MessageSearchRequest{}.new() resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Search(r.Context(), params) }) @@ -45,7 +45,23 @@ func (mh *MessageHandlers) Pin(w http.ResponseWriter, r *http.Request) { params := MessagePinRequest{}.new() resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Pin(r.Context(), params) }) } +func (mh *MessageHandlers) Unpin(w http.ResponseWriter, r *http.Request) { + params := MessageUnpinRequest{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Unpin(r.Context(), params) }) +} func (mh *MessageHandlers) Flag(w http.ResponseWriter, r *http.Request) { params := MessageFlagRequest{}.new() resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Flag(r.Context(), params) }) } +func (mh *MessageHandlers) Deflag(w http.ResponseWriter, r *http.Request) { + params := MessageDeflagRequest{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Deflag(r.Context(), params) }) +} +func (mh *MessageHandlers) React(w http.ResponseWriter, r *http.Request) { + params := MessageReactRequest{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.React(r.Context(), params) }) +} +func (mh *MessageHandlers) Unreact(w http.ResponseWriter, r *http.Request) { + params := MessageUnreactRequest{}.new() + resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return mh.Message.Unreact(r.Context(), params) }) +} diff --git a/sam/rest/server/message_requests.go b/sam/rest/server/message_requests.go index 0a8989cce..49c9746fc 100644 --- a/sam/rest/server/message_requests.go +++ b/sam/rest/server/message_requests.go @@ -22,11 +22,41 @@ import ( var _ = chi.URLParam +// Message create request parameters +type MessageCreateRequest struct { + ChannelId uint64 + Contents string +} + +func (MessageCreateRequest) new() *MessageCreateRequest { + return &MessageCreateRequest{} +} + +func (m *MessageCreateRequest) Fill(r *http.Request) error { + r.ParseForm() + get := map[string]string{} + post := map[string]string{} + urlQuery := r.URL.Query() + for name, param := range urlQuery { + get[name] = string(param[0]) + } + postVars := r.Form + for name, param := range postVars { + post[name] = string(param[0]) + } + + m.ChannelId = parseUInt64(chi.URLParam(r, "channelId")) + + m.Contents = post["contents"] + return nil +} + +var _ RequestFiller = MessageCreateRequest{}.new() + // Message edit request parameters type MessageEditRequest struct { - ID uint64 - Channel_id uint64 - Contents string + MessageId uint64 + Contents string } func (MessageEditRequest) new() *MessageEditRequest { @@ -46,9 +76,7 @@ func (m *MessageEditRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - m.ID = parseUInt64(post["id"]) - - m.Channel_id = parseUInt64(post["channel_id"]) + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) m.Contents = post["contents"] return nil @@ -56,8 +84,37 @@ func (m *MessageEditRequest) Fill(r *http.Request) error { var _ RequestFiller = MessageEditRequest{}.new() +// Message delete request parameters +type MessageDeleteRequest struct { + MessageId uint64 +} + +func (MessageDeleteRequest) new() *MessageDeleteRequest { + return &MessageDeleteRequest{} +} + +func (m *MessageDeleteRequest) Fill(r *http.Request) error { + r.ParseForm() + get := map[string]string{} + post := map[string]string{} + urlQuery := r.URL.Query() + for name, param := range urlQuery { + get[name] = string(param[0]) + } + postVars := r.Form + for name, param := range postVars { + post[name] = string(param[0]) + } + + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) + return nil +} + +var _ RequestFiller = MessageDeleteRequest{}.new() + // Message attach request parameters type MessageAttachRequest struct { + ChannelId uint64 } func (MessageAttachRequest) new() *MessageAttachRequest { @@ -76,69 +133,16 @@ func (m *MessageAttachRequest) Fill(r *http.Request) error { for name, param := range postVars { post[name] = string(param[0]) } + + m.ChannelId = parseUInt64(chi.URLParam(r, "channelId")) return nil } var _ RequestFiller = MessageAttachRequest{}.new() -// Message remove request parameters -type MessageRemoveRequest struct { - ID uint64 -} - -func (MessageRemoveRequest) new() *MessageRemoveRequest { - return &MessageRemoveRequest{} -} - -func (m *MessageRemoveRequest) Fill(r *http.Request) error { - r.ParseForm() - get := map[string]string{} - post := map[string]string{} - urlQuery := r.URL.Query() - for name, param := range urlQuery { - get[name] = string(param[0]) - } - postVars := r.Form - for name, param := range postVars { - post[name] = string(param[0]) - } - - m.ID = parseUInt64(get["id"]) - return nil -} - -var _ RequestFiller = MessageRemoveRequest{}.new() - -// Message read request parameters -type MessageReadRequest struct { - Channel_id uint64 -} - -func (MessageReadRequest) new() *MessageReadRequest { - return &MessageReadRequest{} -} - -func (m *MessageReadRequest) Fill(r *http.Request) error { - r.ParseForm() - get := map[string]string{} - post := map[string]string{} - urlQuery := r.URL.Query() - for name, param := range urlQuery { - get[name] = string(param[0]) - } - postVars := r.Form - for name, param := range postVars { - post[name] = string(param[0]) - } - - m.Channel_id = parseUInt64(post["channel_id"]) - return nil -} - -var _ RequestFiller = MessageReadRequest{}.new() - // Message search request parameters type MessageSearchRequest struct { + ChannelId uint64 Query string Message_type string } @@ -160,6 +164,8 @@ func (m *MessageSearchRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } + m.ChannelId = parseUInt64(chi.URLParam(r, "channelId")) + m.Query = get["query"] m.Message_type = get["message_type"] @@ -170,7 +176,7 @@ var _ RequestFiller = MessageSearchRequest{}.new() // Message pin request parameters type MessagePinRequest struct { - ID uint64 + MessageId uint64 } func (MessagePinRequest) new() *MessagePinRequest { @@ -190,15 +196,43 @@ func (m *MessagePinRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - m.ID = parseUInt64(post["id"]) + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) return nil } var _ RequestFiller = MessagePinRequest{}.new() +// Message unpin request parameters +type MessageUnpinRequest struct { + MessageId uint64 +} + +func (MessageUnpinRequest) new() *MessageUnpinRequest { + return &MessageUnpinRequest{} +} + +func (m *MessageUnpinRequest) Fill(r *http.Request) error { + r.ParseForm() + get := map[string]string{} + post := map[string]string{} + urlQuery := r.URL.Query() + for name, param := range urlQuery { + get[name] = string(param[0]) + } + postVars := r.Form + for name, param := range postVars { + post[name] = string(param[0]) + } + + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) + return nil +} + +var _ RequestFiller = MessageUnpinRequest{}.new() + // Message flag request parameters type MessageFlagRequest struct { - ID uint64 + MessageId uint64 } func (MessageFlagRequest) new() *MessageFlagRequest { @@ -218,8 +252,98 @@ func (m *MessageFlagRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - m.ID = parseUInt64(post["id"]) + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) return nil } var _ RequestFiller = MessageFlagRequest{}.new() + +// Message deflag request parameters +type MessageDeflagRequest struct { + MessageId uint64 +} + +func (MessageDeflagRequest) new() *MessageDeflagRequest { + return &MessageDeflagRequest{} +} + +func (m *MessageDeflagRequest) Fill(r *http.Request) error { + r.ParseForm() + get := map[string]string{} + post := map[string]string{} + urlQuery := r.URL.Query() + for name, param := range urlQuery { + get[name] = string(param[0]) + } + postVars := r.Form + for name, param := range postVars { + post[name] = string(param[0]) + } + + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) + return nil +} + +var _ RequestFiller = MessageDeflagRequest{}.new() + +// Message react request parameters +type MessageReactRequest struct { + MessageId uint64 + Reaction string +} + +func (MessageReactRequest) new() *MessageReactRequest { + return &MessageReactRequest{} +} + +func (m *MessageReactRequest) Fill(r *http.Request) error { + r.ParseForm() + get := map[string]string{} + post := map[string]string{} + urlQuery := r.URL.Query() + for name, param := range urlQuery { + get[name] = string(param[0]) + } + postVars := r.Form + for name, param := range postVars { + post[name] = string(param[0]) + } + + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) + + m.Reaction = post["reaction"] + return nil +} + +var _ RequestFiller = MessageReactRequest{}.new() + +// Message unreact request parameters +type MessageUnreactRequest struct { + MessageId uint64 + ReactionId uint64 +} + +func (MessageUnreactRequest) new() *MessageUnreactRequest { + return &MessageUnreactRequest{} +} + +func (m *MessageUnreactRequest) Fill(r *http.Request) error { + r.ParseForm() + get := map[string]string{} + post := map[string]string{} + urlQuery := r.URL.Query() + for name, param := range urlQuery { + get[name] = string(param[0]) + } + postVars := r.Form + for name, param := range postVars { + post[name] = string(param[0]) + } + + m.MessageId = parseUInt64(chi.URLParam(r, "messageId")) + + m.ReactionId = parseUInt64(chi.URLParam(r, "reactionId")) + return nil +} + +var _ RequestFiller = MessageUnreactRequest{}.new() diff --git a/sam/types/message.go b/sam/types/message.go index 8ad731228..2e8009eee 100644 --- a/sam/types/message.go +++ b/sam/types/message.go @@ -28,7 +28,7 @@ type ( Message string `db:"message"` UserId uint64 `db:"rel_user"` ChannelId uint64 `db:"rel_channel"` - ReplyTo uint64 `db:"rel_message"` + ReplyTo uint64 `db:"reply_to"` UpdatedAt *time.Time `json:",omitempty" db:"updated_at"` DeletedAt *time.Time `json:",omitempty" db:"deleted_at"`