commit 17bc93dc1d5ecba400692593e0d85bdafa68c43b
parent 9ead269f0f71a9be535b3ca0151cc78236123d75
Author: Johan Lindskogen <johan.lindskogen@gmail.com>
Date: Thu, 5 Apr 2018 17:54:34 +0200
Fetch nicklist
Diffstat:
7 files changed, 108 insertions(+), 8 deletions(-)
diff --git a/src/lib/weechat/action_transformer.ts b/src/lib/weechat/action_transformer.ts
@@ -18,6 +18,43 @@ const reduceToObjectByKey = <T, U>(
export const transformToReduxAction = (data: WeechatResponse<any>) => {
switch (data.id) {
// Weechat internal events starts with "_"
+ case "_nicklist_diff": {
+ const object = data.objects[0] as WeechatObject<WeechatNicklist[]>;
+ const nicklistDiffs = object.content;
+
+ const nick = nicklistDiffs.filter(diff => diff.group === 0)[0];
+
+ if (nick) {
+ const bufferId = nick.pointers[0];
+ const payload = nick;
+
+ switch (String.fromCharCode(nick._diff)) {
+ case "+": {
+ return {
+ type: "NICK_ADDED",
+ bufferId,
+ payload
+ };
+ }
+ case "-": {
+ return {
+ type: "NICK_REMOVED",
+ bufferId,
+ payload
+ };
+ }
+ case "*": {
+ return {
+ type: "NICK_UPDATED",
+ bufferId,
+ payload
+ };
+ }
+ }
+ }
+
+ return null;
+ }
case "_buffer_line_added": {
const object = data.objects[0] as WeechatObject<WeechatLine[]>;
const line = object.content[0];
@@ -103,6 +140,18 @@ export const transformToReduxAction = (data: WeechatResponse<any>) => {
)
};
}
+ case "nicklist": {
+ const object = data.objects[0] as WeechatObject<WeechatNicklist[]>;
+ const nicklistDiffs = object.content;
+
+ const nicks = nicklistDiffs.filter(diff => diff.group === 0);
+
+ return {
+ type: "FETCH_NICKLIST",
+ bufferId: object.content[0].pointers[0],
+ payload: nicks
+ };
+ }
case "buffers": {
const object = data.objects[0] as WeechatObject<WeechatBuffer[]>;
diff --git a/src/lib/weechat/types.ts b/src/lib/weechat/types.ts
@@ -34,6 +34,18 @@ interface WeechatHotlist {
count: number[];
}
+interface WeechatNicklist {
+ color: string;
+ group: number;
+ level: number;
+ name: string;
+ pointers: string[];
+ prefix: string;
+ prefix_color: string;
+ visible: number;
+ _diff: number;
+}
+
interface Hotlist extends WeechatHotlist {
message: number;
privmsg: number;
diff --git a/src/store/index.ts b/src/store/index.ts
@@ -7,6 +7,7 @@ import buffers, { BufferState } from "./buffers";
import lines, { LineState } from "./lines";
import hotlists, { HotListState } from "./hotlists";
import connection, { ConnectionInfo } from "./connection-info";
+import nicklists, { NicklistState } from "./nicklists";
type AppState = {
connected: boolean;
@@ -19,6 +20,7 @@ export type StoreState = {
buffers: BufferState;
lines: LineState;
hotlists: HotListState;
+ nicklists: NicklistState;
};
const initialState: AppState = {
@@ -53,7 +55,8 @@ const reducer = combineReducers({
buffers,
lines,
connection,
- hotlists
+ hotlists,
+ nicklists
});
const composeEnhancers =
diff --git a/src/store/nicklists.js b/src/store/nicklists.js
diff --git a/src/store/nicklists.ts b/src/store/nicklists.ts
@@ -0,0 +1,35 @@
+import { omit } from "lodash";
+
+export type NicklistState = { [key: string]: WeechatNicklist[] };
+
+const initialState: NicklistState = {};
+
+export default (state: NicklistState = initialState, action): NicklistState => {
+ switch (action.type) {
+ case "FETCH_NICKLIST":
+ return {
+ ...state,
+ [action.bufferId]: action.payload
+ };
+ case "NICK_ADDED": {
+ return {
+ ...state,
+ [action.bufferId]: [...(state[action.bufferId] || []), action.payload]
+ };
+ }
+ case "NICK_REMOVED": {
+ return {
+ ...state,
+ [action.bufferId]: (state[action.bufferId] || []).filter(
+ nick => nick.name !== action.payload.name
+ )
+ };
+ }
+ case "BUFFER_CLOSED": {
+ const newState = omit(state, action.bufferId);
+ return newState;
+ }
+ default:
+ return state;
+ }
+};
diff --git a/src/usecase/App.tsx b/src/usecase/App.tsx
@@ -26,7 +26,7 @@ interface Props {
currentBuffer: WeechatBuffer | null;
hasHighlights: boolean;
disconnect: () => void;
- fetchLinesForBuffer: (bufferId: string) => void;
+ fetchBufferInfo: (bufferId: string) => void;
sendMessageToBuffer: (fullBufferName: string, message: string) => void;
clearHotlistForBuffer: (fullBufferName: string) => void;
dispatch: (any) => void;
@@ -44,7 +44,7 @@ class App extends React.Component<Props, State> {
};
changeCurrentBuffer = (buffer: WeechatBuffer) => {
- const { currentBufferId, fetchLinesForBuffer } = this.props;
+ const { currentBufferId, fetchBufferInfo } = this.props;
this.drawer.close();
if (currentBufferId !== buffer.id) {
@@ -53,7 +53,7 @@ class App extends React.Component<Props, State> {
bufferId: buffer.id
});
this.props.clearHotlistForBuffer(buffer.full_name);
- fetchLinesForBuffer(buffer.id);
+ fetchBufferInfo(buffer.id);
}
};
@@ -142,7 +142,6 @@ class App extends React.Component<Props, State> {
showTopic={showTopic}
buffer={currentBuffer}
sendMessage={this.sendMessage}
- fetchLinesForBuffer={fetchLinesForBuffer}
bufferId={currentBufferId}
/>
</SafeAreaView>
diff --git a/src/usecase/Root.tsx b/src/usecase/Root.tsx
@@ -55,11 +55,13 @@ export default class WeechatNative extends React.Component<{}, State> {
);
};
- fetchLines = (bufferId: string, numLines: number = 50) => {
- this.connection &&
+ fetchBufferInfo = (bufferId: string, numLines: number = 50) => {
+ if (this.connection) {
this.connection.send(
`(lines) hdata buffer:0x${bufferId}/own_lines/last_line(-${numLines})/data`
);
+ this.connection.send(`(nicklist) nicklist 0x${bufferId}`);
+ }
};
sendMessageToBuffer = (fullBufferName: string, message: string) => {
this.connection &&
@@ -84,7 +86,7 @@ export default class WeechatNative extends React.Component<{}, State> {
disconnect={this.disconnect}
clearHotlistForBuffer={this.clearHotlistForBuffer}
sendMessageToBuffer={this.sendMessageToBuffer}
- fetchLinesForBuffer={this.fetchLines}
+ fetchBufferInfo={this.fetchBufferInfo}
/>
</ConnectionGate>
</PersistGate>