-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Development of pinned function
- Loading branch information
Showing
19 changed files
with
517 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
alter table `message_extra` add column is_pinned smallint not null default 0; -- 是否置顶 | ||
|
||
|
||
-- 消息扩展表 | ||
create table pinned_message | ||
( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
message_id UNSIGNED BIG INT not null default 0, -- 消息ID | ||
message_seq UNSIGNED BIG INT not null default 0, -- 消息seq | ||
channel_id VARCHAR(100) not null default '', -- 频道ID | ||
channel_type smallint not null default 0, -- 频道类型 | ||
version bigint not null default 0, -- 数据版本 | ||
is_deleted smallint not null default 0 -- 是否已删除 | ||
); | ||
CREATE UNIQUE INDEX IF NOT EXISTS idx_pinned_message_id ON pinned_message(message_id); | ||
CREATE INDEX IF NOT EXISTS idx_pinned_message_channel ON pinned_message (channel_id, channel_type); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ | |
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// WKPinnedMessageDB.h | ||
// WuKongIMSDK | ||
// | ||
// Created by tt on 2024/5/22. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "WKPinnedMessage.h" | ||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface WKPinnedMessageDB : NSObject | ||
|
||
+ (WKPinnedMessageDB *)shared; | ||
|
||
|
||
// 通过频道获取置顶的消息集合 | ||
-(NSArray<WKPinnedMessage*>*) getPinnedMessagesByChannel:(WKChannel*)channel; | ||
|
||
// 获取某个频道的最大version | ||
-(uint64_t) getMaxVersion:(WKChannel*)channel; | ||
|
||
// 删除某个频道的所有置顶 | ||
-(void) deletePinnedByChannel:(WKChannel*)channel; | ||
|
||
// 删除某条消息的置顶 | ||
-(void) deletePinnedByMessageId:(uint64_t)messageId; | ||
|
||
// 获取某条置顶消息 | ||
-(WKPinnedMessage*) getPinnedMessageByMessageId:(uint64_t)messageId; | ||
|
||
// 添加或更新置顶消息 | ||
-(void) addOrUpdatePinnedMessages:(NSArray<WKPinnedMessage*>*)messages; | ||
|
||
// 根据消息id查询是否置顶 | ||
-(BOOL) hasPinned:(uint64_t)messageId; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// | ||
// WKPinnedMessageDB.m | ||
// WuKongIMSDK | ||
// | ||
// Created by tt on 2024/5/22. | ||
// | ||
|
||
#import "WKPinnedMessageDB.h" | ||
#import "WKDB.h" | ||
|
||
// 根据频道查询 | ||
#define SQL_PINNED_MESSAGE_GET_WITH_CHANNEL [NSString stringWithFormat:@"select * from %@ where channel_id=? and channel_type=? and is_deleted=0",@"pinned_message"] | ||
|
||
// 获取频道最大版本号 | ||
#define SQL_PINNED_MESSAGE_MAX_VERSION_WITH_CHANNEL [NSString stringWithFormat:@"select max(version) version from %@ where channel_id=? and channel_type=?",@"pinned_message"] | ||
|
||
// 通过消息id获取置顶消息 | ||
#define SQL_PINNED_MESSAGE_GET_WITH_MESSAGEID [NSString stringWithFormat:@"select * from %@ where message_id=? and is_deleted=0",@"pinned_message"] | ||
|
||
// 通过消息id获取置顶消息 | ||
#define SQL_PINNED_MESSAGE_HAS_WITH_MESSAGEID [NSString stringWithFormat:@"select count(*) cn from %@ where message_id=? and is_deleted=0",@"pinned_message"] | ||
|
||
// 根据频道删除置顶消息 | ||
#define SQL_PINNED_MESSAGE_DELETE_WITH_CHANNEL [NSString stringWithFormat:@"update %@ set is_deleted=1 where channel_id=? and channel_type=?",@"pinned_message"] | ||
|
||
// 根据消息id删除置顶消息 | ||
#define SQL_PINNED_MESSAGE_DELETE_WITH_MESSAGE_ID [NSString stringWithFormat:@"update %@ set is_deleted=1 where message_id=?",@"pinned_message"] | ||
|
||
// 添加或更新置顶消息 | ||
#define SQL_PINNED_MESSAGE_ADD_OR_UPDATE [NSString stringWithFormat:@"insert into pinned_message( message_id,message_seq,channel_id,channel_type,version,is_deleted) values(?,?,?,?,?,?) ON CONFLICT(message_id) DO UPDATE SET version=excluded.version,is_deleted=excluded.is_deleted"] | ||
|
||
@implementation WKPinnedMessageDB | ||
|
||
|
||
static WKPinnedMessageDB *_instance; | ||
+ (id)allocWithZone:(NSZone *)zone | ||
{ | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
_instance = [super allocWithZone:zone]; | ||
}); | ||
return _instance; | ||
} | ||
+ (WKPinnedMessageDB *)shared | ||
{ | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
_instance = [[self alloc] init]; | ||
}); | ||
return _instance; | ||
} | ||
|
||
-(NSArray<WKPinnedMessage*>*) getPinnedMessagesByChannel:(WKChannel*)channel { | ||
|
||
__block NSMutableArray *pinnedMessages = [NSMutableArray array]; | ||
[WKDB.sharedDB.dbQueue inDatabase:^(FMDatabase * _Nonnull db) { | ||
FMResultSet *resultSet = [db executeQuery:SQL_PINNED_MESSAGE_GET_WITH_CHANNEL,channel.channelId?:@"",@(channel.channelType)]; | ||
while (resultSet.next) { | ||
[pinnedMessages addObject:[self toPinnedMessage:resultSet]]; | ||
} | ||
[resultSet close]; | ||
}]; | ||
|
||
return pinnedMessages; | ||
} | ||
|
||
-(uint64_t) getMaxVersion:(WKChannel*)channel { | ||
__block uint64_t version = 0; | ||
[WKDB.sharedDB.dbQueue inDatabase:^(FMDatabase * _Nonnull db) { | ||
FMResultSet *resultSet = [db executeQuery:SQL_PINNED_MESSAGE_MAX_VERSION_WITH_CHANNEL,channel.channelId?:@"",@(channel.channelType)]; | ||
if (resultSet.next) { | ||
version = [resultSet unsignedLongLongIntForColumn:@"version"]; | ||
} | ||
[resultSet close]; | ||
}]; | ||
return version; | ||
} | ||
|
||
-(void) deletePinnedByChannel:(WKChannel*)channel { | ||
[WKDB.sharedDB.dbQueue inDatabase:^(FMDatabase * _Nonnull db) { | ||
[db executeUpdate:SQL_PINNED_MESSAGE_DELETE_WITH_CHANNEL,channel.channelId?:@"",@(channel.channelType)]; | ||
}]; | ||
} | ||
|
||
-(void) deletePinnedByMessageId:(uint64_t)messageId { | ||
[WKDB.sharedDB.dbQueue inDatabase:^(FMDatabase * _Nonnull db) { | ||
[db executeUpdate:SQL_PINNED_MESSAGE_DELETE_WITH_MESSAGE_ID,@(messageId)]; | ||
}]; | ||
} | ||
|
||
-(void) addOrUpdatePinnedMessages:(NSArray<WKPinnedMessage*>*)messages { | ||
if(!messages || messages.count==0) { | ||
return; | ||
} | ||
[WKDB.sharedDB.dbQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { | ||
for (WKPinnedMessage *message in messages) { | ||
[db executeUpdate:SQL_PINNED_MESSAGE_ADD_OR_UPDATE,@(message.messageId),@(message.messageSeq),message.channel.channelId?:@"",@(message.channel.channelType),@(message.version),@(message.isDeleted)]; | ||
} | ||
}]; | ||
} | ||
|
||
-(WKPinnedMessage*) getPinnedMessageByMessageId:(uint64_t)messageId { | ||
__block WKPinnedMessage *message; | ||
[WKDB.sharedDB.dbQueue inDatabase:^(FMDatabase * _Nonnull db) { | ||
FMResultSet *resultSet = [db executeQuery:SQL_PINNED_MESSAGE_GET_WITH_MESSAGEID,@(messageId)]; | ||
if (resultSet.next) { | ||
message = [self toPinnedMessage:resultSet]; | ||
} | ||
[resultSet close]; | ||
}]; | ||
return message; | ||
} | ||
|
||
-(BOOL) hasPinned:(uint64_t)messageId { | ||
__block BOOL has = false; | ||
[WKDB.sharedDB.dbQueue inDatabase:^(FMDatabase * _Nonnull db) { | ||
FMResultSet *resultSet = [db executeQuery:SQL_PINNED_MESSAGE_HAS_WITH_MESSAGEID,@(messageId)]; | ||
if (resultSet.next) { | ||
has = [resultSet intForColumn:@"cn"]>0; | ||
} | ||
[resultSet close]; | ||
}]; | ||
return has; | ||
} | ||
|
||
-(WKPinnedMessage*) toPinnedMessage:(FMResultSet*)resultSet{ | ||
WKPinnedMessage *msg = [WKPinnedMessage new]; | ||
msg.messageId = [resultSet unsignedLongLongIntForColumn:@"message_id"]; | ||
msg.messageSeq = (uint32_t)[resultSet longLongIntForColumn:@"message_seq"]; | ||
|
||
NSString *channelId = [resultSet stringForColumn:@"channel_id"]; | ||
int channelType = [resultSet intForColumn:@"channel_type"]; | ||
|
||
WKChannel *channel = [WKChannel channelID:channelId channelType:channelType]; | ||
msg.channel = channel; | ||
msg.isDeleted = [resultSet boolForColumn:@"is_deleted"]; | ||
msg.version = [resultSet unsignedLongLongIntForColumn:@"version"]; | ||
|
||
return msg; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.