dart-app-core/lib/utils/slack/slack_data.dart

209 lines
5 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:json_annotation/json_annotation.dart';
import 'package:http/http.dart';
// ignore: depend_on_referenced_packages, implementation_imports
import 'package:http_parser/src/media_type.dart';
part 'slack_data.g.dart';
enum SlackBlockType { header, section }
enum SlackTextType { mrkdwn, plain_text }
enum SlackActionType { button, select }
enum SlackActionStyle { danger, primary }
@JsonSerializable()
class DataSlack {
String? message;
List<SlackBlock> blocks = [];
List<SlackAttachment> attachments = [];
String getBlocks() {
var list = [];
for (var item in blocks) {
list.add(item.toJson());
}
return jsonEncode(list);
}
String getAttachments() {
var list = [];
for (var item in attachments) {
list.add(item.toJson());
}
return jsonEncode(list);
}
DataSlack();
factory DataSlack.fromJson(Map<String, dynamic> json) =>
_$DataSlackFromJson(json);
Map<String, dynamic> toJson() => _$DataSlackToJson(this);
}
@JsonSerializable()
class SlackBlock {
late SlackBlockType type;
SlackBlock();
factory SlackBlock.fromJson(Map<String, dynamic> json) =>
_$SlackBlockFromJson(json);
Map<String, dynamic> toJson() => _$SlackBlockToJson(this);
}
@JsonSerializable()
class SlackBlockHeader extends SlackBlock {
late SlackBlockText text;
SlackBlockHeader() {
super.type = SlackBlockType.header;
text = SlackBlockText();
text.type = SlackTextType.plain_text;
}
factory SlackBlockHeader.fromJson(Map<String, dynamic> json) =>
_$SlackBlockHeaderFromJson(json);
@override
Map<String, dynamic> toJson() => _$SlackBlockHeaderToJson(this);
}
@JsonSerializable()
class SlackBlockSection extends SlackBlock {
List<SlackBlockSectionField> fields = [];
SlackBlockSection() {
super.type = SlackBlockType.section;
}
factory SlackBlockSection.fromJson(Map<String, dynamic> json) =>
_$SlackBlockSectionFromJson(json);
@override
Map<String, dynamic> toJson() => _$SlackBlockSectionToJson(this);
}
@JsonSerializable()
class SlackBlockSectionField {
SlackTextType type = SlackTextType.mrkdwn;
String? text;
SlackBlockSectionField();
factory SlackBlockSectionField.fromJson(Map<String, dynamic> json) =>
_$SlackBlockSectionFieldFromJson(json);
Map<String, dynamic> toJson() => _$SlackBlockSectionFieldToJson(this);
}
@JsonSerializable()
class SlackBlockText {
SlackTextType type = SlackTextType.mrkdwn;
String? text;
SlackBlockText();
factory SlackBlockText.fromJson(Map<String, dynamic> json) =>
_$SlackBlockTextFromJson(json);
Map<String, dynamic> toJson() => _$SlackBlockTextToJson(this);
}
@JsonSerializable()
class SlackAttachment {
String? fallback;
String? color;
String? author_name;
String? author_link;
String? author_icon;
String? title;
String? title_link;
String? pretext;
String? text;
String? image_url;
String? thumb_url;
String? footer;
String? footer_icon;
String? ts;
List<SlackField>? fields;
List<SlackAction>? actions;
SlackAttachment();
factory SlackAttachment.fromJson(Map<String, dynamic> json) =>
_$SlackAttachmentFromJson(json);
Map<String, dynamic> toJson() => _$SlackAttachmentToJson(this);
}
@JsonSerializable()
class SlackField {
String? title;
String? value;
SlackField();
SlackField.withParam(this.title, this.value);
factory SlackField.fromJson(Map<String, dynamic> json) =>
_$SlackFieldFromJson(json);
Map<String, dynamic> toJson() => _$SlackFieldToJson(this);
}
@JsonSerializable()
class SlackAction {
SlackActionType? type;
String? name;
String? text;
String? url;
String? value;
SlackActionStyle? style;
/*SlackField options;
SlackConfirm confirm;*/
SlackAction();
SlackAction.withParam(
this.name, this.text, this.url, this.value, this.type, this.style);
factory SlackAction.fromJson(Map<String, dynamic> json) =>
_$SlackActionFromJson(json);
Map<String, dynamic> toJson() => _$SlackActionToJson(this);
}
@JsonSerializable()
class SlackConfirm {
String? title;
String? text;
// ignore: non_constant_identifier_names
String? ok_text;
// ignore: non_constant_identifier_names
String? dismiss_text;
}
class SlackFile {
String? title;
// ignore: non_constant_identifier_names
String? initial_comment;
String? fileName;
String? mimeType;
late File file;
Future<MultipartRequest> setFileData(MultipartRequest form) async {
// var bytes = await readFileByte(filePath!);
if (title != null) {
form.fields['title'] = title!;
}
if (initial_comment != null) {
form.fields['initial_comment'] = initial_comment!;
}
form.fields['filetype'] =
"auto"; //참조 - https://api.slack.com/types/file#file_types
if (fileName != null && mimeType != null) {
form.files.add(await MultipartFile.fromPath('file', file.path,
contentType: MediaType(HttpHeaders.contentTypeHeader, mimeType!)));
}
var c = Completer<MultipartRequest>();
c.complete(form);
return c.future;
}
}