- Latest Versions
| Name | Version | Description |
|---|---|---|
| Ming Ke Ming (名可名) | Decentralized User Identity Authentication | |
| Dao Ke Dao (道可道) | Universal Message Module | |
| DIMP (去中心化通讯协议) | Decentralized Instant Messaging Protocol |
extends CustomizedContent
#import <DIMSDK/DIMSDK.h>
NS_ASSUME_NONNULL_BEGIN
/*
* Customized Content Processing Unit
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Handle content for application customized
*/
@interface DIMAppCustomizedProcessor : DIMCustomizedContentProcessor
- (void)setContentHandler:(id<DIMCustomizedContentHandler>)handler
forModule:(NSString *)mod
inApplication:(NSString *)app;
// protected
- (nullable id<DIMCustomizedContentHandler>)contentHandlerForModule:(NSString *)mod
inApplication:(NSString *)app;
@end
/* Command Transform:
+===============================+===============================+
| Customized Content | Group Query Command |
+-------------------------------+-------------------------------+
| "type" : i2s(0xCC) | "type" : i2s(0x88) |
| "sn" : 123 | "sn" : 123 |
| "time" : 123.456 | "time" : 123.456 |
| "app" : "chat.dim.group" | |
| "mod" : "history" | |
| "act" : "query" | |
| | "command" : "query" |
| "group" : "{GROUP_ID}" | "group" : "{GROUP_ID}" |
| "last_time" : 0 | "last_time" : 0 |
+===============================+===============================+
*/
@interface DIMGroupHistoryHandler : DIMCustomizedContentHandler
@end
NS_ASSUME_NONNULL_END#import "DIMGroupCommand.h"
#import "DIMAppCustomizedProcessor.h"
static inline NSString *build_key(NSString *app, NSString *mod) {
return [[NSString alloc] initWithFormat:@"%@:%@", app, mod];;
}
@interface DIMAppCustomizedProcessor () {
NSMutableDictionary<NSString *, id<DIMCustomizedContentHandler>> *_handlers;
}
@end
@implementation DIMAppCustomizedProcessor
- (instancetype)initWithFacebook:(DIMFacebook *)facebook
messenger:(DIMMessenger *)transceiver {
if (self = [super initWithFacebook:facebook messenger:transceiver]) {
_handlers = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setContentHandler:(id<DIMCustomizedContentHandler>)handler
forModule:(NSString *)mod
inApplication:(NSString *)app {
[_handlers setObject:handler forKey:build_key(app, mod)];
}
- (nullable id<DIMCustomizedContentHandler>)contentHandlerForModule:(NSString *)mod
inApplication:(NSString *)app {
return [_handlers objectForKey:build_key(app, mod)];
}
// Override
- (id<DIMCustomizedContentHandler>)filterApplication:(NSString *)app
withModule:(NSString *)mod
content:(id<DKDCustomizedContent>)body
messasge:(id<DKDReliableMessage>)rMsg {
id<DIMCustomizedContentHandler> handler;
handler = [self contentHandlerForModule:mod inApplication:app];
if (!handler) {
// default handler
handler = [super filterForModule:mod inApplication:app content:body messasge:rMsg];
}
return handler;
}
@end
@implementation DIMGroupHistoryHandler
// Override
- (NSArray<id<DKDContent>> *)handleAction:(NSString *)act
sender:(id<MKMID>)uid
content:(id<DKDCustomizedContent>)body
message:(id<DKDReliableMessage>)rMsg {
if ([body group] == nil) {
NSAssert(false, @"group command error: %@, sender: %@", body, uid);
NSString *text = @"Group command error.";
return [self respondReceipt:text envelope:rMsg.envelope content:body extra:nil];
} else if ([act isEqualToString:DIMGroupHistory_ActQuery]) {
return [self transformQueryCommand:body message:rMsg];
}
NSAssert(false, @"unknown action: %@, %@, sender: %@", act, body, uid);
return [super handleAction:act sender:uid content:body message:rMsg];
}
// private
- (NSArray<id<DKDContent>> *)transformQueryCommand:(id<DKDCustomizedContent>)body
message:(id<DKDReliableMessage>)rMsg {
DIMMessenger *transceiver = [self messenger];
if (!transceiver) {
NSAssert(false, @"messenger lost");
return nil;
}
NSMutableDictionary *info = [body copyDictionary:NO];
[info setObject:DKDContentType_Command forKey:@"type"];
[info setObject:DKDGroupCommand_Query forKey:@"command"];
id<DKDContent> query = DKDContentParse(info);
if ([query conformsToProtocol:@protocol(DKDQueryGroupCommand)]) {
return [transceiver processContent:query
withReliableMessageMessage:rMsg];
}
NSAssert(false, @"query command error: %@, %@, sender: %@", query, body, rMsg.sender);
NSString *text = @"Query command error.";
return [self respondReceipt:text envelope:rMsg.envelope content:body extra:nil];
}
@end#import <DIMSDK/DIMSDK.h>
#import "DIMAppCustomizedProcessor.h"
NS_ASSUME_NONNULL_BEGIN
@interface DIMClientContentProcessorCreator : DIMContentProcessorCreator
@end
@interface DIMClientContentProcessorCreator (Customized)
// protected
- (DIMAppCustomizedProcessor *)createCustomizedProcessor:(DIMFacebook *)facebook
messenger:(DIMMessenger *)transceiver;
@end#import "DIMHandshakeCommandProcessor.h"
#import "DIMCreator.h"
#define CREATE_CPU(clazz) \
[[clazz alloc] initWithFacebook:self.facebook \
messenger:self.messenger] \
/* EOF 'CREATE_CPU(clazz)' */
@implementation DIMClientContentProcessorCreator
- (DIMAppCustomizedProcessor *)createCustomizedProcessor:(DIMFacebook *)facebook
messenger:(DIMMessenger *)transceiver {
DIMAppCustomizedProcessor *cpu = CREATE_CPU(DIMAppCustomizedProcessor);
// 'chat.dim.group:history'
[cpu setContentHandler:CREATE_CPU(DIMGroupHistoryHandler)
forModule:DIMGroupHistory_Mod
inApplication:DIMGroupHistory_App];
return cpu;
}
// Override
- (id<DIMContentProcessor>)createContentProcessor:(NSString *)type {
// application customized
if ([type isEqualToString:DKDContentType_Application] ||
[type isEqualToString:DKDContentType_Customized]) {
return [self createCustomizedProcessor:self.facebook messenger:self.messenger];
}
// ...
// others
return [super createContentProcessor:type];
}
// Override
- (id<DIMContentProcessor>)createCommandProcessor:(NSString *)name withType:(NSString *)msgType {
// handshake
if ([name isEqualToString:DKDCommand_Handshake]) {
return CREATE_CPU(DIMHandshakeCommandProcessor);
}
// ...
// others
return [super createCommandProcessor:name withType:msgType];
}
@endTo let your DIMAppCustomizedProcessor start to work,
you must override DIMContentProcessorCreator for message types:
- DKDContentType_Application
- DKDContentType_Customized
and then set your creator for DIMContentProcessorFactory in the DIMMessageProcessor.