Hackathon: Post Reminders (#20555)
This PR adds the post reminder backend work. We add a new API endpoint via which a user can set a reminder for a post. An ephemeral message will be sent down the line to let the user know about the action. And then after the time is over, the system admin bot will send a DM message to the user about the reminder post.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e6459b97de
Коммит
20cb042362
32
api4/post.go
32
api4/post.go
@@ -34,6 +34,8 @@ func (api *API) InitPost() {
|
||||
api.BaseRoutes.Post.Handle("", api.APISessionRequired(updatePost)).Methods("PUT")
|
||||
api.BaseRoutes.Post.Handle("/patch", api.APISessionRequired(patchPost)).Methods("PUT")
|
||||
api.BaseRoutes.PostForUser.Handle("/set_unread", api.APISessionRequired(setPostUnread)).Methods("POST")
|
||||
api.BaseRoutes.PostForUser.Handle("/reminder", api.APISessionRequired(setPostReminder)).Methods("POST")
|
||||
|
||||
api.BaseRoutes.Post.Handle("/pin", api.APISessionRequired(pinPost)).Methods("POST")
|
||||
api.BaseRoutes.Post.Handle("/unpin", api.APISessionRequired(unpinPost)).Methods("POST")
|
||||
}
|
||||
@@ -842,6 +844,36 @@ func setPostUnread(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func setPostReminder(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequirePostId().RequireUserId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if c.AppContext.Session().UserId != c.Params.UserId && !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) {
|
||||
c.SetPermissionError(model.PermissionEditOtherUsers)
|
||||
return
|
||||
}
|
||||
if !c.App.SessionHasPermissionToChannelByPost(*c.AppContext.Session(), c.Params.PostId, model.PermissionReadChannel) {
|
||||
c.SetPermissionError(model.PermissionReadChannel)
|
||||
return
|
||||
}
|
||||
|
||||
var reminder model.PostReminder
|
||||
if jsonErr := json.NewDecoder(r.Body).Decode(&reminder); jsonErr != nil {
|
||||
c.SetInvalidParam("target_time")
|
||||
return
|
||||
}
|
||||
|
||||
appErr := c.App.SetPostReminder(c.Params.PostId, c.Params.UserId, reminder.TargetTime)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func saveIsPinnedPost(c *Context, w http.ResponseWriter, isPinned bool) {
|
||||
c.RequirePostId()
|
||||
if c.Err != nil {
|
||||
|
||||
@@ -3164,3 +3164,63 @@ func TestGetPostStripActionIntegrations(t *testing.T) {
|
||||
// integration must be omitted
|
||||
require.Nil(t, action["integration"])
|
||||
}
|
||||
|
||||
func TestPostReminder(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
client := th.Client
|
||||
userWSClient, err := th.CreateWebSocketClient()
|
||||
require.NoError(t, err)
|
||||
defer userWSClient.Close()
|
||||
userWSClient.Listen()
|
||||
|
||||
targetTime := time.Now().UTC().Unix()
|
||||
resp, err := client.SetPostReminder(&model.PostReminder{
|
||||
TargetTime: targetTime,
|
||||
PostId: th.BasicPost.Id,
|
||||
UserId: th.BasicUser.Id,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
CheckOKStatus(t, resp)
|
||||
|
||||
post, _, err := client.GetPost(th.BasicPost.Id, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
user, _, err := client.GetUser(post.UserId, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
var caught bool
|
||||
func() {
|
||||
for {
|
||||
select {
|
||||
case ev := <-userWSClient.EventChannel:
|
||||
if ev.EventType() == model.WebsocketEventEphemeralMessage {
|
||||
caught = true
|
||||
data := ev.GetData()
|
||||
|
||||
post, ok := data["post"].(string)
|
||||
require.True(t, ok)
|
||||
|
||||
var parsedPost model.Post
|
||||
err := json.Unmarshal([]byte(post), &parsedPost)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, model.PostTypeEphemeral, parsedPost.Type)
|
||||
assert.Equal(t, th.BasicUser.Id, parsedPost.UserId)
|
||||
assert.Equal(t, th.BasicPost.Id, parsedPost.RootId)
|
||||
|
||||
require.Equal(t, float64(targetTime), parsedPost.GetProp("target_time").(float64))
|
||||
require.Equal(t, th.BasicPost.Id, parsedPost.GetProp("post_id").(string))
|
||||
require.Equal(t, user.Username, parsedPost.GetProp("username").(string))
|
||||
require.Equal(t, th.BasicTeam.Name, parsedPost.GetProp("team_name").(string))
|
||||
return
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
require.Truef(t, caught, "User should have received %s event", model.WebsocketEventEphemeralMessage)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user