slacklog

package
v0.0.0-...-72a4bb2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2021 License: CC-BY-4.0 Imports: 26 Imported by: 0

Documentation

Overview

Package slacklog はSlackからエクスポートされた各チャンネルのログの取得、HTMLへ の変換を行なうためのパッケージである。

LogStoreはログデータの取得方法を規定し、必要に応じて各種ログテーブルからデータ を取得する。

ChannelTable/MessageTable/UserTable/EmojiTableはSlackからエクスポートされたJSON 形式のログファイルを読み込み、LogStoreが処理しやすい形でデータを保持する。

TextConverterはログが保持しているテキストのエスケープやHTMLへの変換を行なう。

HTMLGeneratorはLogStoreから取得し、TextConverterで変換したデータを、 text/templateパッケージを用いてHTMLとして出力する。

Index

Constants

This section is empty.

Variables

View Source
var FiletypeToExtension = map[string]string{}/* 121 elements not displayed */

FiletypeToExtension is a map which maps filetype to filename extension.

Based on <https://api.slack.com/types/file>

Functions

func HostBySlack

func HostBySlack(f slack.File) bool

HostBySlack checks a file is hosted by slack or not.

func LevelOfDetailTime

func LevelOfDetailTime(target, base time.Time) string

LevelOfDetailTime returns a label string which represents time. The resolution of the string is determined by differece from base time in 4 levels.

func LocalName

func LocalName(f slack.File, url, suffix string) string

LocalName returns name of local downloaded file.

func LocalPath

func LocalPath(f slack.File) string

LocalPath returns path of local downloaded file.

func ReadFileAsJSON

func ReadFileAsJSON(filename string, strict bool, dst interface{}) error

ReadFileAsJSON reads a file and unmarshal its contents as JSON to `dst` destination object.

func RegulateFilename

func RegulateFilename(s string) string

RegulateFilename replaces unusable characters as filepath by '_'.

func SortChannel

func SortChannel(channels []Channel)

SortChannel sorts []Channel by name. It modify original slice.

func ThumbImageHeight

func ThumbImageHeight(f slack.File) int

ThumbImageHeight returns height of thumbnail image.

func ThumbImagePath

func ThumbImagePath(f slack.File) string

ThumbImagePath returns path of thumbnail image file.

func ThumbImageWidth

func ThumbImageWidth(f slack.File) int

ThumbImageWidth returns width of thumbnail image.

func ThumbVideoPath

func ThumbVideoPath(f slack.File) string

ThumbVideoPath returns local path of thumbnail for the video.

func TopLevelMimetype

func TopLevelMimetype(f slack.File) string

TopLevelMimetype extracts top level type from MIME Type.

func TsToDateTime

func TsToDateTime(ts string) time.Time

TsToDateTime converts Ts string to time.Time.

Types

type Channel

type Channel struct {
	slack.Channel

	Pins []ChannelPin `json:"pins"`
}

Channel represents channel object in Slack.

func FilterChannel

func FilterChannel(channels []Channel, whitelist []string) []Channel

FilterChannel : whitelistに指定したチャンネル名に該当するチャンネルのみを返 す。 whitelistに'*'が含まれる場合はchannelをそのまま返す。

type ChannelPin

type ChannelPin struct {
	ID      string `json:"id"`
	Typ     string `json:"type"`
	Created int64  `json:"created"`
	User    string `json:"user"`
	Owner   string `json:"owner"`
}

ChannelPin represents a pinned message for a channel.

type ChannelTable

type ChannelTable struct {
	Channels   []Channel
	ChannelMap map[string]*Channel
}

ChannelTable : チャンネルデータを保持する。 channelsもchannelMapも保持するチャンネルデータは同じで、channelMapはチャンネ ルIDをキーとするmapとなっている。 ユースケースに応じてchannelsとchannelMapは使い分ける。

func NewChannelTable

func NewChannelTable(path string, whitelist []string) (*ChannelTable, error)

NewChannelTable : pathに指定したJSON形式のチャンネルデータを読み込み、 ChannelTable を生成する。 whitelistに指定したチャンネル名のみを読み込む。

type Config

type Config struct {
	EditedSuffix  string   `json:"edited_suffix"`
	Channels      []string `json:"channels"`
	EmojiJSONPath string   `json:"emoji_json_path"`
}

Config : ログ出力時の設定を保持する。

func ReadConfig

func ReadConfig(path string) (*Config, error)

ReadConfig : pathに指定したファイルからコンフィグを読み込む。

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Downloader : ダウンロード処理をするワーカを管理するための構造体。 TODO: 今のところ、ダウンロード処理中にエラーが発生してもキューに積まれたタス クが全て完了するまでは分からない(Wait()の返り値として見るまでは)。

func NewDownloader

func NewDownloader(token string) *Downloader

NewDownloader creates a downloader for Slack with the token.

func (*Downloader) CloseQueue

func (d *Downloader) CloseQueue()

CloseQueue : ダウンロードキューへの追加が完了したことをDownloaderに通知する ために実行する。 TODO: 2回実行するとpanicしてしまうのを修正する。Downloaderに状態でも持たせる とよいだろうか。

func (*Downloader) QueueDownloadRequest

func (d *Downloader) QueueDownloadRequest(url, outputPath string, withToken bool)

QueueDownloadRequest : ダウンロード処理をqueueに積む

func (*Downloader) Wait

func (d *Downloader) Wait() error

Wait : ワーカが全て実行終了するまで待つ。 ダウンロード処理中にエラーが発生していた場合は最初に発生した1つを返す。 他のエラーはログに出力している。

type EmojiTable

type EmojiTable struct {
	// NameToExtは絵文字名をキーとし、画像の拡張子が値である。
	// 絵文字は事前に全てダウンロードしている、という前提であり、そのため拡張子のみ
	// を保持している。
	NameToExt map[string]string
}

EmojiTable : 絵文字データを保持する。

func NewEmojiTable

func NewEmojiTable(path string) (*EmojiTable, error)

NewEmojiTable : pathに指定したJSON形式の絵文字データを読み込み、EmojiTableを 生成する。

type HTMLGenerator

type HTMLGenerator struct {
	// contains filtered or unexported fields
}

HTMLGenerator : ログデータからHTMLを生成するための構造体。

func NewHTMLGenerator

func NewHTMLGenerator(templateDir string, filesDir string, s *LogStore) *HTMLGenerator

NewHTMLGenerator : HTMLGeneratorを生成する。

func (*HTMLGenerator) Generate

func (g *HTMLGenerator) Generate(outDir string) error

Generate はoutDirにログデータの変換結果を生成する。 目標とする構造は以下となる:

  • outDir/
  • index.html // generateIndex()
  • ${channel_id}/ // generateChannelDir()
  • index.html // generateChannelIndex()
  • ${YYYY}/
  • ${MM}/
  • index.html // generateMessageDir()

type Indexer

type Indexer struct {
	// contains filtered or unexported fields
}

func NewIndexer

func NewIndexer(s *LogStore) *Indexer

func (*Indexer) Build

func (idx *Indexer) Build() error

func (*Indexer) Output

func (idx *Indexer) Output(outDir string) error

type LogStore

type LogStore struct {
	// contains filtered or unexported fields
}

LogStore : ログデータを各種テーブルを介して取得するための構造体。 MessageTableはチャンネル毎に用意しているためmtsはチャンネルIDをキーとするmap となっている。

func NewLogStore

func NewLogStore(dirPath string, cfg *Config) (*LogStore, error)

NewLogStore : 各テーブルを生成して、LogStoreを生成する。

func (*LogStore) GetAllMessages

func (s *LogStore) GetAllMessages(channelID string) (Messages, error)

GetAllMessages returns all messages as array.

func (*LogStore) GetChannels

func (s *LogStore) GetChannels() []Channel

GetChannels gets all stored channgels.

func (*LogStore) GetDisplayNameByUserID

func (s *LogStore) GetDisplayNameByUserID(userID string) string

GetDisplayNameByUserID gets display name for the user.

func (*LogStore) GetDisplayNameMap

func (s *LogStore) GetDisplayNameMap() map[string]string

GetDisplayNameMap gets a map from user ID to user's display name.

func (*LogStore) GetEmojiMap

func (s *LogStore) GetEmojiMap() map[string]string

GetEmojiMap gets a map from emoji name to its file extension (image type).

func (*LogStore) GetMessagesPerMonth

func (s *LogStore) GetMessagesPerMonth(channelID string) (MessagesMap, error)

GetMessagesPerMonth gets a messages map for the channel. Messages map have all message split in per month.

func (*LogStore) GetThread

func (s *LogStore) GetThread(channelID, ts string) (*Thread, bool)

GetThread gets a thread (chain of messages) by channel ID and Ts (thread root's Ts).

func (*LogStore) GetUserByID

func (s *LogStore) GetUserByID(userID string) (*User, bool)

GetUserByID gets a user by (user) ID. Sometimes by bot ID.

func (*LogStore) HasNextMonth

func (s *LogStore) HasNextMonth(channelID string, key MessageMonthKey) bool

HasNextMonth returns a channel has next key or not.

func (*LogStore) HasPrevMonth

func (s *LogStore) HasPrevMonth(channelID string, key MessageMonthKey) bool

HasPrevMonth returns a channel has previous logs or not.

type Message

type Message struct {
	slack.Message

	// Trail shows the user of message is same as the previous one.
	// FIXME: 本来はココに書いてはいけない
	Trail bool `json:"-"`
}

Message : メッセージ エクスポートしたYYYY-MM-DD.jsonの中身を保持する。 https://slack.com/intl/ja-jp/help/articles/220556107-Slack-%E3%81%8B%E3%82%89%E3%82%A8%E3%82%AF%E3%82%B9%E3%83%9D%E3%83%BC%E3%83%88%E3%81%97%E3%81%9F%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E8%AA%AD%E3%81%BF%E6%96%B9

func (Message) IsRootOfThread

func (m Message) IsRootOfThread() bool

IsRootOfThread : メッセージがスレッドの最初のメッセージであるかを判定する。

func (*Message) RemoveTokenFromURLs

func (m *Message) RemoveTokenFromURLs()

RemoveTokenFromURLs removes the token from URLs in a message.

type MessageIcons

type MessageIcons struct {
	Image48 string `json:"image_48"`
}

MessageIcons represents icon for each message.

type MessageMonthKey

type MessageMonthKey struct {
	// contains filtered or unexported fields
}

MessageMonthKey is a key for messages.

func NewMessageMonthKey

func NewMessageMonthKey(year, month string) (MessageMonthKey, error)

NewMessageMonthKey creates MessageMonthKey key from two strings: which represents year and month.

func (MessageMonthKey) Month

func (k MessageMonthKey) Month() string

Month returns string represents month.

func (MessageMonthKey) Next

Next gets a key for next month.

func (MessageMonthKey) NextMonth

func (k MessageMonthKey) NextMonth() string

NextMonth returns a string for next month.

func (MessageMonthKey) NextYear

func (k MessageMonthKey) NextYear() string

NextYear returns a string for next year.

func (MessageMonthKey) Prev

Prev gets a key for previous month.

func (MessageMonthKey) PrevMonth

func (k MessageMonthKey) PrevMonth() string

PrevMonth returns a string for previous month.

func (MessageMonthKey) PrevYear

func (k MessageMonthKey) PrevYear() string

PrevYear returns a string for previous year.

func (MessageMonthKey) Year

func (k MessageMonthKey) Year() string

Year returns string represents year.

type MessageTable

type MessageTable struct {
	// key: timestamp
	AllMessageMap map[string]*Message
	// key: thread timestamp
	ThreadMap map[string]*Thread
	MsgsMap   MessagesMap
	// contains filtered or unexported fields
}

MessageTable : メッセージデータを保持する スレッドは投稿時刻からどのスレッドへの返信かが判断できるためThreadMapのキー はtsである。 MsgsMapは月毎にメッセージを保持する。そのためキーは投稿月である。 loadedFilesはすでに読み込んだファイルパスを保持する。 loadedFilesは同じファイルを二度読むことを防ぐために用いている。

func NewMessageTable

func NewMessageTable() *MessageTable

NewMessageTable : MessageTableを生成する。 他のテーブルと違い、メッセージファイルは量が多いため、NewMessageTable()実行 時には読み込まず、(*MessageTable).ReadLogDir()/(*MessageTable).ReadLogFile() 実行時に読み込ませる。

func (*MessageTable) ReadLogDir

func (m *MessageTable) ReadLogDir(path string, readAllMessages bool) error

ReadLogDir : pathに指定したディレクトリに存在するJSON形式のメッセージデータ を読み込む。 すでにそのディレクトリが読み込み済みの場合は処理をスキップする。 デフォルトでは特定のサブタイプを持つメッセージのみをmsgMapに登録するが、 readAllMessages が true である場合はすべてのメッセージを登録する。

func (*MessageTable) ReadLogFile

func (m *MessageTable) ReadLogFile(path string, readAllMessages bool) error

ReadLogFile : pathに指定したJSON形式のメッセージデータを読み込む。 すでにそのファイルが読み込み済みの場合は処理をスキップする。 readAllMessagesがfalseである場合は特定のサブタイプを持つメッセージのみをmsgMapに登録する。

type Messages

type Messages []*Message

Messages is an array of `*Message`.

func (Messages) Sort

func (msgs Messages) Sort()

Sort sorts messages by `Message.Ts` ascendant order.

type MessagesMap

type MessagesMap map[MessageMonthKey]Messages

MessagesMap is a map, maps MessageMonthKey to Messages.

func (MessagesMap) Keys

func (mm MessagesMap) Keys() []MessageMonthKey

Keys returns all keys in the map.

type ReactionInfo

type ReactionInfo struct {
	EmojiPath string
	Name      string
	Count     int
	Users     []string
	Default   bool
}

ReactionInfo is information for a reaction.

type TextConverter

type TextConverter struct {
	// contains filtered or unexported fields
}

TextConverter : markdown形式のテキストをHTMLに変換するための構造体。

func NewTextConverter

func NewTextConverter(users, emojis map[string]string) *TextConverter

NewTextConverter : TextConverter を生成する

func (*TextConverter) ToHTML

func (c *TextConverter) ToHTML(text string) string

ToHTML : markdown形式のtextをHTMLに変換する

type Thread

type Thread struct {
	// contains filtered or unexported fields
}

Thread : スレッド rootMsgはスレッドの先頭メッセージを表わす。 repliesにはそのスレッドへの返信メッセージが入る。先頭メッセージは含まない。

func (Thread) LastReplyTime

func (th Thread) LastReplyTime() time.Time

LastReplyTime returns last replied time for the thread.

func (*Thread) Put

func (th *Thread) Put(m *Message)

Put puts a message to the thread as "root" or "reply".

func (Thread) Replies

func (th Thread) Replies() Messages

Replies returns replied messages for the thread.

func (Thread) ReplyCount

func (th Thread) ReplyCount() int

ReplyCount return counts of replied messages.

func (Thread) RootText

func (th Thread) RootText() string

RootText returns text of root message of the thread.

type Ts

type Ts struct {
	IsNumber bool
	Value    string
}

Ts is a type represents "Ts" fields in message.go or so.

func (Ts) MarshalJSON

func (ts Ts) MarshalJSON() ([]byte, error)

MarshalJSON implements "encoding/json".Marshaller interface.

func (*Ts) UnmarshalJSON

func (ts *Ts) UnmarshalJSON(b []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaller interface.

type User

type User slack.User

User : ユーザ エクスポートしたuser.jsonの中身を保持する。 公式の情報は以下だがuser.jsonの解説までは書かれていない。 https://slack.com/intl/ja-jp/help/articles/220556107-Slack-%E3%81%8B%E3%82%89%E3%82%A8%E3%82%AF%E3%82%B9%E3%83%9D%E3%83%BC%E3%83%88%E3%81%97%E3%81%9F%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E8%AA%AD%E3%81%BF%E6%96%B9

type UserTable

type UserTable struct {
	Users []User
	// key: user ID
	UserMap map[string]*User
}

UserTable : ユーザデータを保持する UsersもUserMapも保持するユーザデータは同じで、UserMapはユーザIDをキーとする mapとなっている。 ユースケースに応じてUsersとUserMapは使い分ける。

func NewUserTable

func NewUserTable(path string) (*UserTable, error)

NewUserTable : pathに指定したJSON形式のユーザデータを読み込み、UserTableを生 成する。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL