Skip to content

Instantly share code, notes, and snippets.

@mccraigmccraig
Created September 29, 2017 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mccraigmccraig/86c4e60eae1462f2c37a738268ca0c4c to your computer and use it in GitHub Desktop.
Save mccraigmccraig/86c4e60eae1462f2c37a738268ca0c4c to your computer and use it in GitHub Desktop.
(s/defmethod ^:always-validate h/api-method-handler ::org-timeline-posts-POST
[{cassandra :cassandra
kafka-producer :kafka-producer
:as app}
opts
[tag
{auth-user-id :auth-user-id
org-id :org-id
timeline-id-or-tag :timeline-id-or-tag
{con-org-id :org_id
con-id :id
con-creator-id :creator_id
con-type :type
:as conversation} :conversation}] :- [(s/one (s/eq ::org-timeline-posts-POST) :tag)
(s/one (m.s/merge-map-schemas
nf.s/UpsertTimelinePostBodySchema
nf.s/OrgTimelinePostsPathParams
nf.s/AuthOrgUserParam)
:value)]]
(when (and con-org-id (not= org-id con-org-id))
(throw (pr/error-ex ::api/bad-request "cannot upsert to another org")))
(ddo [:let [timeline-id (tlid/validate-org-default-news-timeline-id
org-id
timeline-id-or-tag)]
;; if a convo id is given, it must refer to an existing convo
{xcon-creator-id :creator_id
:as xcon} (m/when con-id
(c/ensure-one cassandra
con.e/Conversations
[:org_id :id]
[org-id con-id]))
:let [_ (when (and xcon (not= auth-user-id xcon-creator-id))
(throw (pr/error-ex ::api/unauthorized "can't modify someone else's post")))
new-created-at-timeuuid (uuid/v1)
conversation (merge
{:id new-created-at-timeuuid
:created_at_timeuuid new-created-at-timeuuid}
xcon
conversation
{:org_id org-id
:creator_id auth-user-id
:timeline_ids [timeline-id]
:participant_ids nil
:type "group"
:visibility "public"
:join_policy "open"})]
[{u-con-id :id
u-con-type :type
u-con-created-at-timeuuid :created_at_timeuuid
u-con-name :name
:as u-con} _] (c/upsert cassandra con.e/Conversations conversation)
cp (cpsx/conversation-participant app u-con "user" auth-user-id)
_ (notify-conversation-created
{:cassandra cassandra
:kafka-producer kafka-producer
:conversation-created-notification
{:org_id org-id
:conversation_id u-con-id
:conversation_type u-con-type
:author_id auth-user-id
:creation_time_uuid u-con-created-at-timeuuid
:name u-con-name}})]
(return
[:ok {:conversation u-con
:conversation-participant cp}])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment