dimsdk 1.2.2 copy "dimsdk: ^1.2.2" to clipboard
dimsdk: ^1.2.2 copied to clipboard

Decentralized Instant Messaging Software Development Kit.

Decentralized Instant Messaging (Dart SDK) #

License PRs Welcome Platform Issues Repo Size Tags Version

Watchers Forks Stars Followers

Dependencies #

  • Latest Versions
Name Version Description
Ming Ke Ming (名可名) Version Decentralized User Identity Authentication
Dao Ke Dao (道可道) Version Universal Message Module
DIMP (去中心化通讯协议) Version Decentralized Instant Messaging Protocol

Extensions #

1. extends Content #

extends CustomizedContent

2. extends ContentProcessor #

import 'package:dimsdk/dimsdk.dart';


///  Handler for Customized Content
///  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
abstract interface class CustomizedContentHandler {

  ///  Do your job
  ///
  /// @param act     - action
  /// @param sender  - user ID
  /// @param content - customized content
  /// @param rMsg    - network message
  /// @return responses
  Future<List<Content>> handleAction(String act, ID sender, CustomizedContent content,
      ReliableMessage rMsg);

}


///  Customized Content Processing Unit
///  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CustomizedContentProcessor extends BaseContentProcessor implements CustomizedContentHandler {
  CustomizedContentProcessor(super.facebook, super.messenger);

  @override
  Future<List<Content>> processContent(Content content, ReliableMessage rMsg) async {
    assert(content is CustomizedContent, 'customized content error: $content');
    CustomizedContent customized = content as CustomizedContent;
    // 1. check app id
    String app = customized.application;
    List<Content>? res = filter(app, content, rMsg);
    if (res != null) {
      // app id not found
      return res;
    }
    // 2. get handler with module name
    String mod = customized.module;
    CustomizedContentHandler? handler = fetch(mod, customized, rMsg);
    if (handler == null) {
      // module not support
      return [];
    }
    // 3. do the job
    String act = customized.action;
    ID sender = rMsg.sender;
    return await handler.handleAction(act, sender, customized, rMsg);
  }

  // protected
  List<Content>? filter(String app, CustomizedContent content, ReliableMessage rMsg) {
    /// override for your application
    String text = 'Content not support.';
    return respondReceipt(text, content: content, envelope: rMsg.envelope, extra: {
      'template': 'Customized content (app: \${app}) not support yet!',
      'replacements': {
        'app': app,
      }
    });
  }

  // protected
  CustomizedContentHandler? fetch(String mod, CustomizedContent content, ReliableMessage rMsg) {
    /// override for your module
    // if the application has too many modules, I suggest you to
    // use different handler to do the jobs for each module.
    return this;
  }

  @override
  Future<List<Content>> handleAction(String act, ID sender, CustomizedContent content, ReliableMessage rMsg) async {
    /// override for customized actions
    String app = content.application;
    String mod = content.module;
    String text = 'Content not support.';
    return respondReceipt(text, content: content, envelope: rMsg.envelope, extra: {
      'template': 'Customized content (app: \${app}, mod: \${mod}, act: \${act}) not support yet!',
      'replacements': {
        'app': app,
        'mod': mod,
        'act': act,
      }
    });
  }

}

3. extends ExtensionLoader #

import 'package:dimsdk/plugins.dart';


/// Extensions Loader
/// ~~~~~~~~~~~~~~~~~
class CommonLoader extends ExtensionLoader {

  // protected
  void registerCustomizedFactories() {
    
    // Application Customized
    Content.setFactory(ContentType.APPLICATION, ContentParser((dict) => AppCustomizedContent(dict)));
    Content.setFactory(ContentType.CUSTOMIZED, ContentParser((dict) => AppCustomizedContent(dict)));
    
  }

  @override
  void registerContentFactories() {
    super.registerContentFactories();
    
    registerCustomizedFactories();
  }

  @override
  void registerCommandFactories() {
    super.registerCommandFactories();

    // Handshake
    Command.setFactory(HandshakeCommand.HANDSHAKE, CommandParser((dict) => BaseHandshakeCommand(dict)));

  }

}

Usages #

You must load all extensions before your business run:

import 'common_loader.dart';

void main() {

  var loader = CommonLoader();
  loader.run();
  
  // do your jobs after all extensions loaded.
  
}

Also, to let your CustomizedContentProcessor start to work, you must override BaseContentProcessorCreator for message types:

  1. ContentType.APPLICATION
  2. ContentType.CUSTOMIZED

and then set your creator for GeneralContentProcessorFactory in the MessageProcessor.


Copyright © 2023 Albert Moky Followers

0
likes
130
points
75
downloads

Publisher

verified publisherdim.chat

Weekly Downloads

Decentralized Instant Messaging Software Development Kit.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

dimp

More

Packages that depend on dimsdk