forked from Ivasoft/mattermost-mobile
[Gekidou] Add is_unread to MyChannel model (#5878)
* Make message_count become has_unreads * Fix lint * Re-add the message count * rename has_unreads to is_unread and address feedback * fix query with is_unread Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
This commit is contained in:
committed by
GitHub
parent
7e6248dfb3
commit
b49b4050ac
@@ -151,7 +151,7 @@ export const markChannelAsViewed = async (serverUrl: string, channelId: string,
|
||||
}
|
||||
|
||||
member.prepareUpdate((m) => {
|
||||
m.messageCount = 0;
|
||||
m.isUnread = false;
|
||||
m.mentionsCount = 0;
|
||||
m.manuallyUnread = false;
|
||||
m.viewedAt = member.lastViewedAt;
|
||||
|
||||
@@ -18,34 +18,30 @@ import type SystemModel from '@typings/database/models/servers/system';
|
||||
|
||||
const {SERVER: {SYSTEM, MY_CHANNEL, CHANNEL}} = MM_TABLES;
|
||||
|
||||
const withSystem = withObservables([], ({database}: WithDatabaseArgs) => {
|
||||
const currentTeamId = database.get<SystemModel>(SYSTEM).findAndObserve(SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID).pipe(
|
||||
switchMap((t) => of$(t.value)),
|
||||
);
|
||||
return {
|
||||
currentTeamId,
|
||||
};
|
||||
});
|
||||
|
||||
type WithTeamsArgs = WithDatabaseArgs & {
|
||||
myTeam: MyTeamModel;
|
||||
}
|
||||
|
||||
const withTeams = withObservables(['myTeam'], ({myTeam, database}: WithTeamsArgs) => {
|
||||
const myChannels = database.get<MyChannelModel>(MY_CHANNEL).query(Q.on(CHANNEL, Q.and(Q.where('delete_at', Q.eq(0)), Q.where('team_id', Q.eq(myTeam.id))))).observeWithColumns(['mentions_count', 'message_count']);
|
||||
const enhance = withObservables(['myTeam'], ({myTeam, database}: WithTeamsArgs) => {
|
||||
const myChannels = database.get<MyChannelModel>(MY_CHANNEL).query(Q.on(CHANNEL, Q.and(Q.where('delete_at', Q.eq(0)), Q.where('team_id', Q.eq(myTeam.id))))).observeWithColumns(['mentions_count', 'is_unread']);
|
||||
const mentionCount = myChannels.pipe(
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
switchMap((val) => of$(val.reduce((acc, v) => acc + v.mentionsCount, 0))),
|
||||
);
|
||||
const hasUnreads = myChannels.pipe(
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
switchMap((val) => of$(val.reduce((acc, v) => acc + v.messageCount, 0) > 0)),
|
||||
switchMap((val) => of$(val.reduce((acc, v) => acc || v.isUnread, false))),
|
||||
);
|
||||
const currentTeamId = database.get<SystemModel>(SYSTEM).findAndObserve(SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID).pipe(
|
||||
switchMap((t) => of$(t.value)),
|
||||
);
|
||||
|
||||
return {
|
||||
currentTeamId,
|
||||
team: myTeam.team.observe(),
|
||||
mentionCount,
|
||||
hasUnreads,
|
||||
};
|
||||
});
|
||||
|
||||
export default withDatabase(withSystem(withTeams(TeamItem)));
|
||||
export default withDatabase(enhance(TeamItem));
|
||||
|
||||
@@ -31,11 +31,14 @@ export default class MyChannelModel extends Model {
|
||||
/** manually_unread : Determine if the user marked a post as unread */
|
||||
@field('manually_unread') manuallyUnread!: boolean;
|
||||
|
||||
/** message_count : The derived number of unread messages on this channel */
|
||||
@field('message_count') messageCount!: number;
|
||||
|
||||
/** mentions_count : The number of mentions on this channel */
|
||||
@field('mentions_count') mentionsCount!: number;
|
||||
|
||||
/** message_count : The derived number of unread messages on this channel */
|
||||
@field('message_count') messageCount!: number;
|
||||
/** is_unread : Whether the channel has unread messages */
|
||||
@field('is_unread') isUnread!: boolean;
|
||||
|
||||
/** roles : The user's privileges on this channel */
|
||||
@field('roles') roles!: string;
|
||||
|
||||
@@ -140,7 +140,9 @@ const ChannelHandler = (superclass: any) => class extends superclass {
|
||||
myChannels.forEach((my) => {
|
||||
const channel = channels.find((c) => c.id === my.channel_id);
|
||||
if (channel) {
|
||||
my.msg_count = Math.max(0, channel.total_msg_count - my.msg_count);
|
||||
const msgCount = Math.max(0, channel.total_msg_count - my.msg_count);
|
||||
my.msg_count = msgCount;
|
||||
my.is_unread = msgCount > 0;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -125,6 +125,7 @@ export const transformMyChannelRecord = ({action, database, value}: TransformerA
|
||||
myChannel._raw.id = isCreateAction ? (raw.channel_id || myChannel.id) : record.id;
|
||||
myChannel.roles = raw.roles;
|
||||
myChannel.messageCount = raw.msg_count;
|
||||
myChannel.isUnread = Boolean(raw.is_unread);
|
||||
myChannel.mentionsCount = raw.mention_count;
|
||||
myChannel.lastPostAt = raw.last_post_at || 0;
|
||||
myChannel.lastViewedAt = raw.last_viewed_at;
|
||||
|
||||
@@ -15,6 +15,7 @@ export default tableSchema({
|
||||
{name: 'manually_unread', type: 'boolean'},
|
||||
{name: 'mentions_count', type: 'number'},
|
||||
{name: 'message_count', type: 'number'},
|
||||
{name: 'is_unread', type: 'boolean'},
|
||||
{name: 'roles', type: 'string'},
|
||||
{name: 'viewed_at', type: 'number'},
|
||||
],
|
||||
|
||||
@@ -116,6 +116,7 @@ describe('*** Test schema for SERVER database ***', () => {
|
||||
manually_unread: {name: 'manually_unread', type: 'boolean'},
|
||||
mentions_count: {name: 'mentions_count', type: 'number'},
|
||||
message_count: {name: 'message_count', type: 'number'},
|
||||
is_unread: {name: 'is_unread', type: 'boolean'},
|
||||
roles: {name: 'roles', type: 'string'},
|
||||
viewed_at: {name: 'viewed_at', type: 'number'},
|
||||
},
|
||||
@@ -125,6 +126,7 @@ describe('*** Test schema for SERVER database ***', () => {
|
||||
{name: 'manually_unread', type: 'boolean'},
|
||||
{name: 'mentions_count', type: 'number'},
|
||||
{name: 'message_count', type: 'number'},
|
||||
{name: 'is_unread', type: 'boolean'},
|
||||
{name: 'roles', type: 'string'},
|
||||
{name: 'viewed_at', type: 'number'},
|
||||
],
|
||||
|
||||
@@ -23,7 +23,7 @@ type Props = {
|
||||
|
||||
type UnreadMessages = {
|
||||
mentions: number;
|
||||
messages: number;
|
||||
unread: boolean;
|
||||
};
|
||||
|
||||
type UnreadSubscription = UnreadMessages & {
|
||||
@@ -52,30 +52,30 @@ const style = StyleSheet.create({
|
||||
|
||||
const Home = ({isFocused, theme}: Props) => {
|
||||
const db = DatabaseManager.appDatabase?.database;
|
||||
const [total, setTotal] = useState<UnreadMessages>({mentions: 0, messages: 0});
|
||||
const [total, setTotal] = useState<UnreadMessages>({mentions: 0, unread: false});
|
||||
|
||||
const updateTotal = () => {
|
||||
let messages = 0;
|
||||
let unread = false;
|
||||
let mentions = 0;
|
||||
subscriptions.forEach((value) => {
|
||||
messages += value.messages;
|
||||
unread = unread || value.unread;
|
||||
mentions += value.mentions;
|
||||
});
|
||||
setTotal({mentions, messages});
|
||||
setTotal({mentions, unread});
|
||||
};
|
||||
|
||||
const unreadsSubscription = (serverUrl: string, myChannels: MyChannelModel[]) => {
|
||||
const unreads = subscriptions.get(serverUrl);
|
||||
if (unreads) {
|
||||
let mentions = 0;
|
||||
let messages = 0;
|
||||
let unread = false;
|
||||
myChannels.forEach((myChannel) => {
|
||||
mentions += myChannel.mentionsCount;
|
||||
messages += myChannel.messageCount;
|
||||
unread = unread || myChannel.isUnread;
|
||||
});
|
||||
|
||||
unreads.mentions = mentions;
|
||||
unreads.messages = messages;
|
||||
unreads.unread = unread;
|
||||
subscriptions.set(serverUrl, unreads);
|
||||
updateTotal();
|
||||
}
|
||||
@@ -90,13 +90,13 @@ const Home = ({isFocused, theme}: Props) => {
|
||||
if (!subscriptions.has(serverUrl)) {
|
||||
const unreads: UnreadSubscription = {
|
||||
mentions: 0,
|
||||
messages: 0,
|
||||
unread: false,
|
||||
};
|
||||
subscriptions.set(serverUrl, unreads);
|
||||
unreads.subscription = sdb.database.
|
||||
get(MY_CHANNEL).
|
||||
query(Q.on(CHANNEL, Q.where('delete_at', Q.eq(0)))).
|
||||
observeWithColumns(['mentions_count', 'message_count']).
|
||||
observeWithColumns(['mentions_count', 'has_unreads']).
|
||||
subscribe(unreadsSubscription.bind(undefined, serverUrl));
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,7 @@ const Home = ({isFocused, theme}: Props) => {
|
||||
} else if (total.mentions > 99) {
|
||||
unreadStyle = style.mentionsThreeDigits;
|
||||
}
|
||||
} else if (total.messages) {
|
||||
} else if (total.unread) {
|
||||
unreadStyle = style.unread;
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ const Home = ({isFocused, theme}: Props) => {
|
||||
style={unreadStyle}
|
||||
visible={!isFocused && Boolean(unreadStyle)}
|
||||
type='Small'
|
||||
value={total.mentions || (total.messages * -1)}
|
||||
value={total.mentions || (total.unread ? -1 : 0)}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
|
||||
1
types/api/channels.d.ts
vendored
1
types/api/channels.d.ts
vendored
@@ -60,6 +60,7 @@ type ChannelMembership = {
|
||||
scheme_user?: boolean;
|
||||
scheme_admin?: boolean;
|
||||
post_root_id?: string;
|
||||
is_unread?: boolean;
|
||||
};
|
||||
type ChannelUnread = {
|
||||
channel_id: string;
|
||||
|
||||
@@ -26,6 +26,9 @@ export default class MyChannelModel extends Model {
|
||||
/** message_count : The derived number of unread messages on this channel */
|
||||
messageCount: number;
|
||||
|
||||
/** is_unread : Whether the channel has unread posts */
|
||||
isUnread: boolean;
|
||||
|
||||
/** roles : The user's privileges on this channel */
|
||||
roles: string;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user