diff --git a/app/database/models/server/channel.ts b/app/database/models/server/channel.ts index f87de03b9..456b74a8a 100644 --- a/app/database/models/server/channel.ts +++ b/app/database/models/server/channel.ts @@ -52,7 +52,7 @@ export default class ChannelModel extends Model { [GROUPS_IN_CHANNEL]: {type: 'has_many', foreignKey: 'channel_id'}, /** A CHANNEL can be associated with multiple POSTS_IN_CHANNEL (relationship is 1:N) */ - [POSTS_IN_CHANNEL]: {type: 'has_many', foreignKey: 'id'}, + [POSTS_IN_CHANNEL]: {type: 'has_many', foreignKey: 'channel_id'}, /** A CHANNEL can contain multiple POST (relationship is 1:N) */ [POST]: {type: 'has_many', foreignKey: 'channel_id'}, diff --git a/app/database/models/server/post.ts b/app/database/models/server/post.ts index 426a1fe06..ae3d4430d 100644 --- a/app/database/models/server/post.ts +++ b/app/database/models/server/post.ts @@ -38,7 +38,7 @@ export default class PostModel extends Model { [FILE]: {type: 'has_many', foreignKey: 'post_id'}, /** A POST can have multiple POSTS_IN_THREAD. (relationship is 1:N)*/ - [POSTS_IN_THREAD]: {type: 'has_many', foreignKey: 'id'}, + [POSTS_IN_THREAD]: {type: 'has_many', foreignKey: 'root_id'}, /** A POST can have multiple REACTION. (relationship is 1:N)*/ [REACTION]: {type: 'has_many', foreignKey: 'post_id'}, diff --git a/app/database/models/server/posts_in_channel.ts b/app/database/models/server/posts_in_channel.ts index 1d1e80df2..8952485d4 100644 --- a/app/database/models/server/posts_in_channel.ts +++ b/app/database/models/server/posts_in_channel.ts @@ -26,6 +26,9 @@ export default class PostsInChannelModel extends Model { [CHANNEL]: {type: 'belongs_to', key: 'id'}, }; + /** channel_id: Associated channel identifier */ + @field('channel_id') channelId!: string; + /** earliest : The earliest timestamp of the post in that channel */ @field('earliest') earliest!: number; diff --git a/app/database/models/server/posts_in_thread.ts b/app/database/models/server/posts_in_thread.ts index 6b8be343e..1b72d94df 100644 --- a/app/database/models/server/posts_in_thread.ts +++ b/app/database/models/server/posts_in_thread.ts @@ -26,6 +26,9 @@ export default class PostsInThreadModel extends Model { [POST]: {type: 'belongs_to', key: 'id'}, }; + /** root_id: Associated root post identifier */ + @field('root_id') rootId!: string; + /** earliest : Lower bound of a timestamp range */ @field('earliest') earliest!: number; diff --git a/app/database/operator/server_data_operator/handlers/posts_in_channel.ts b/app/database/operator/server_data_operator/handlers/posts_in_channel.ts index 003da757c..74c1eb21b 100644 --- a/app/database/operator/server_data_operator/handlers/posts_in_channel.ts +++ b/app/database/operator/server_data_operator/handlers/posts_in_channel.ts @@ -75,7 +75,7 @@ const PostsInChannelHandler = (superclass: any) => class extends superclass { // Find the records in the PostsInChannel table that have a matching channel_id const chunks = (await this.database.get(POSTS_IN_CHANNEL).query( - Q.where('id', channelId), + Q.where('channel_id', channelId), Q.experimentalSortBy('latest', Q.desc), ).fetch()) as PostsInChannelModel[]; diff --git a/app/database/operator/server_data_operator/handlers/posts_in_thread.ts b/app/database/operator/server_data_operator/handlers/posts_in_thread.ts index f3b76e8e2..c5ba4ac9d 100644 --- a/app/database/operator/server_data_operator/handlers/posts_in_thread.ts +++ b/app/database/operator/server_data_operator/handlers/posts_in_thread.ts @@ -32,13 +32,13 @@ const PostsInThreadHandler = (superclass: any) => class extends superclass { const chunks = (await retrieveRecords({ database: this.database, tableName: POSTS_IN_THREAD, - condition: Q.where('id', rootId), + condition: Q.where('root_id', rootId), })) as PostsInThreadModel[]; if (chunks.length) { const chunk = chunks[0]; const newValue = { - id: rootId, + root_id: rootId, earliest: Math.min(chunk.earliest, firstPost.create_at), latest: Math.max(chunk.latest, lastPost.create_at), }; @@ -50,7 +50,7 @@ const PostsInThreadHandler = (superclass: any) => class extends superclass { } else { // create chunk create.push({ - id: rootId, + root_id: rootId, earliest: firstPost.create_at, latest: lastPost.create_at, }); diff --git a/app/database/operator/server_data_operator/transformers/post.test.ts b/app/database/operator/server_data_operator/transformers/post.test.ts index 62f51a0ce..b43c52cd8 100644 --- a/app/database/operator/server_data_operator/transformers/post.test.ts +++ b/app/database/operator/server_data_operator/transformers/post.test.ts @@ -66,7 +66,7 @@ describe('*** POST Prepare Records Test ***', () => { record: undefined, raw: { id: 'ps81iqbddesfby8jayz7owg4yypoo', - post_id: '8swgtrrdiff89jnsiwiip3y1eoe', + root_id: '8swgtrrdiff89jnsiwiip3y1eoe', earliest: 1596032651748, latest: 1597032651748, }, diff --git a/app/database/operator/server_data_operator/transformers/post.ts b/app/database/operator/server_data_operator/transformers/post.ts index e34da4964..1235b97f3 100644 --- a/app/database/operator/server_data_operator/transformers/post.ts +++ b/app/database/operator/server_data_operator/transformers/post.ts @@ -76,7 +76,8 @@ export const transformPostInThreadRecord = ({action, database, value}: Transform const isCreateAction = action === OperationType.CREATE; const fieldsMapper = (postsInThread: PostsInThreadModel) => { - postsInThread._raw.id = isCreateAction ? raw.id : record.id; + postsInThread._raw.id = isCreateAction ? (raw.id || postsInThread.id) : record.id; + postsInThread.rootId = raw.root_id; postsInThread.earliest = raw.earliest; postsInThread.latest = raw.latest!; }; @@ -193,7 +194,8 @@ export const transformPostsInChannelRecord = ({action, database, value}: Transfo const isCreateAction = action === OperationType.CREATE; const fieldsMapper = (postsInChannel: PostsInChannelModel) => { - postsInChannel._raw.id = isCreateAction ? (raw.channel_id || postsInChannel.id) : record.id; + postsInChannel._raw.id = isCreateAction ? (raw.id || postsInChannel.id) : record.id; + postsInChannel.channelId = raw.channel_id; postsInChannel.earliest = raw.earliest; postsInChannel.latest = raw.latest; }; diff --git a/app/database/schema/server/table_schemas/posts_in_channel.ts b/app/database/schema/server/table_schemas/posts_in_channel.ts index ae1f3029e..1e42d27f0 100644 --- a/app/database/schema/server/table_schemas/posts_in_channel.ts +++ b/app/database/schema/server/table_schemas/posts_in_channel.ts @@ -10,6 +10,7 @@ const {POSTS_IN_CHANNEL} = MM_TABLES.SERVER; export default tableSchema({ name: POSTS_IN_CHANNEL, columns: [ + {name: 'channel_id', type: 'string', isIndexed: true}, {name: 'earliest', type: 'number'}, {name: 'latest', type: 'number'}, ], diff --git a/app/database/schema/server/table_schemas/posts_in_thread.ts b/app/database/schema/server/table_schemas/posts_in_thread.ts index 6430e9aad..5e5fbddf6 100644 --- a/app/database/schema/server/table_schemas/posts_in_thread.ts +++ b/app/database/schema/server/table_schemas/posts_in_thread.ts @@ -10,6 +10,7 @@ const {POSTS_IN_THREAD} = MM_TABLES.SERVER; export default tableSchema({ name: POSTS_IN_THREAD, columns: [ + {name: 'root_id', type: 'string', isIndexed: true}, {name: 'earliest', type: 'number'}, {name: 'latest', type: 'number'}, ], diff --git a/app/database/schema/server/test.ts b/app/database/schema/server/test.ts index 9baddaacf..af4231699 100644 --- a/app/database/schema/server/test.ts +++ b/app/database/schema/server/test.ts @@ -138,10 +138,12 @@ describe('*** Test schema for SERVER database ***', () => { [POSTS_IN_CHANNEL]: { name: POSTS_IN_CHANNEL, columns: { + channel_id: {name: 'channel_id', type: 'string', isIndexed: true}, earliest: {name: 'earliest', type: 'number'}, latest: {name: 'latest', type: 'number'}, }, columnArray: [ + {name: 'channel_id', type: 'string', isIndexed: true}, {name: 'earliest', type: 'number'}, {name: 'latest', type: 'number'}, ], @@ -189,10 +191,12 @@ describe('*** Test schema for SERVER database ***', () => { [POSTS_IN_THREAD]: { name: POSTS_IN_THREAD, columns: { + root_id: {name: 'root_id', type: 'string', isIndexed: true}, earliest: {name: 'earliest', type: 'number'}, latest: {name: 'latest', type: 'number'}, }, columnArray: [ + {name: 'root_id', type: 'string', isIndexed: true}, {name: 'earliest', type: 'number'}, {name: 'latest', type: 'number'}, ], diff --git a/app/queries/servers/post.ts b/app/queries/servers/post.ts index e67ccdf91..34d0cb960 100644 --- a/app/queries/servers/post.ts +++ b/app/queries/servers/post.ts @@ -13,7 +13,7 @@ const {SERVER: {POST, POSTS_IN_CHANNEL}} = MM_TABLES; export const queryPostsInChannel = (database: Database, channelId: string): Promise => { try { return database.get(POSTS_IN_CHANNEL).query( - Q.where('id', channelId), + Q.where('channel_id', channelId), Q.experimentalSortBy('latest', Q.desc), ).fetch() as Promise; } catch { diff --git a/types/database/models/servers/posts_in_channel.d.ts b/types/database/models/servers/posts_in_channel.d.ts index baddf4176..6f71f566a 100644 --- a/types/database/models/servers/posts_in_channel.d.ts +++ b/types/database/models/servers/posts_in_channel.d.ts @@ -15,6 +15,9 @@ export default class PostsInChannelModel extends Model { /** associations : Describes every relationship to this table. */ static associations: Associations; + /** channel_id: Associated channel identifier */ + channelId: string; + /** earliest : The earliest timestamp of the post in that channel */ earliest: number; diff --git a/types/database/models/servers/posts_in_thread.d.ts b/types/database/models/servers/posts_in_thread.d.ts index 0d0653fcf..57fdfe7b7 100644 --- a/types/database/models/servers/posts_in_thread.d.ts +++ b/types/database/models/servers/posts_in_thread.d.ts @@ -15,6 +15,9 @@ export default class PostsInThreadModel extends Model { /** associations : Describes every relationship to this table. */ static associations: Associations; + /** root_id: Associated root post identifier */ + rootId: string; + /** earliest : Lower bound of a timestamp range */ earliest: number; diff --git a/types/database/raw_values.d.ts b/types/database/raw_values.d.ts index a87d25dfb..4b25428a2 100644 --- a/types/database/raw_values.d.ts +++ b/types/database/raw_values.d.ts @@ -37,15 +37,17 @@ type MyTeam = { }; type PostsInChannel = { + id?: string; channel_id: string; earliest: number; latest: number; }; type PostsInThread = { + id?: string; earliest: number; - latest?: number; - id: string; + latest: number; + root_id: string; }; type Metadata = {