MM-45009: Delete ThreadMemberships from "left" channels (#22559)

* MM-50550: Filter out threads from "left" channels v2

Currently leaving a channel doesn't affect the thread memberships of
that user/channel combination.
This PR aims to filter out all threads from those channels for the user.

Adds a DeleteAt column in the ThreadMemberships table, and filter out
all thread memberships that are "deleted".
Each time a user leaves a channel all thread memberships are going to be
marked as deleted, and when a user joins a channel again all those
existing thread memberships will be re-instantiated.

Adds a migration to mark all existing thread memberships as deleted
depending on whether there exists a channel membership for that
channel/user.

* Added migration files into list

* Fixes tests

* Fixes case where DeleteAt would be null

* Guard thread API endpoints with appropriate perms

* Deletes ThreadMembership rows upon leaving channel

* Minor style changes

* Use NoTranslation error

* Refactors tests

* Adds API tests to assert permissions on Team

* Adds tests, and fixes migrations

* Fixes test description

* Fix test

* Removes check on DM/GMs

* Change the MySQL query in the migration

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Kyriakos Z
2023-04-19 15:20:34 +03:00
коммит произвёл GitHub
родитель c34a50a6c7
Коммит a24111f9bd
16 изменённых файлов: 426 добавлений и 38 удалений

Просмотреть файл

@@ -212,6 +212,8 @@ channels/db/migrations/mysql/000105_remove_tokens.down.sql
channels/db/migrations/mysql/000105_remove_tokens.up.sql
channels/db/migrations/mysql/000106_fileinfo_channelid.down.sql
channels/db/migrations/mysql/000106_fileinfo_channelid.up.sql
channels/db/migrations/mysql/000107_threadmemberships_cleanup.down.sql
channels/db/migrations/mysql/000107_threadmemberships_cleanup.up.sql
channels/db/migrations/postgres/000001_create_teams.down.sql
channels/db/migrations/postgres/000001_create_teams.up.sql
channels/db/migrations/postgres/000002_create_team_members.down.sql
@@ -424,3 +426,5 @@ channels/db/migrations/postgres/000105_remove_tokens.down.sql
channels/db/migrations/postgres/000105_remove_tokens.up.sql
channels/db/migrations/postgres/000106_fileinfo_channelid.down.sql
channels/db/migrations/postgres/000106_fileinfo_channelid.up.sql
channels/db/migrations/postgres/000107_threadmemberships_cleanup.down.sql
channels/db/migrations/postgres/000107_threadmemberships_cleanup.up.sql

Просмотреть файл

@@ -0,0 +1 @@
-- Skipping it because the forward migrations are destructive

Просмотреть файл

@@ -0,0 +1,5 @@
DELETE FROM
tm USING ThreadMemberships AS tm
JOIN Threads ON Threads.PostId = tm.PostId
WHERE
(tm.UserId, Threads.ChannelId) NOT IN (SELECT UserId, ChannelId FROM ChannelMembers);

Просмотреть файл

@@ -0,0 +1 @@
-- Skipping it because the forward migrations are destructive

Просмотреть файл

@@ -0,0 +1,12 @@
DELETE FROM threadmemberships WHERE (postid, userid) IN (
SELECT
threadmemberships.postid,
threadmemberships.userid
FROM
threadmemberships
JOIN threads ON threads.postid = threadmemberships.postid
LEFT JOIN channelmembers ON channelmembers.userid = threadmemberships.userid
AND threads.channelid = channelmembers.channelid
WHERE
channelmembers.channelid IS NULL
);