4b25d69b148c18bee62c13dc4a1b513d16b16e93
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts         1) namespace ns_model
source/model.ts         2) {
source/model.ts         3) 	
source/model.ts         4) 	/**
source/model.ts         5) 	 * adds a listener for a certain incident
source/model.ts         6) 	 */
source/model.ts         7) 	export function listen
source/model.ts         8) 	(
source/model.ts         9) 		model: type_model,
source/model.ts        10) 		incident: string,
source/model.ts        11) 		handler: (details?: any)=>void
source/model.ts        12) 	): void
source/model.ts        13) 	{
source/model.ts        14) 		if (! model.listeners.hasOwnProperty(incident))
source/model.ts        15) 		{
source/model.ts        16) 			model.listeners[incident] = [];
source/model.ts        17) 		}
source/model.ts        18) 		else
source/model.ts        19) 		{
source/model.ts        20) 			// do nothing
source/model.ts        21) 		}
source/model.ts        22) 		model.listeners[incident].push(handler);
source/model.ts        23) 	}
source/model.ts        24) 
source/model.ts        25) 
source/model.ts        26) 	/**
source/model.ts        27) 	 * sends a notification to all listeners for a certain incident
source/model.ts        28) 	 */
source/model.ts        29) 	function notify
source/model.ts        30) 	(
source/model.ts        31) 		model: type_model,
source/model.ts        32) 		incident: string,
source/model.ts        33) 		details: any = null
source/model.ts        34) 	): void
source/model.ts        35) 	{
source/model.ts        36) 		if (model.listeners.hasOwnProperty(incident))
source/model.ts        37) 		{
source/model.ts        38) 			for (const handler of model.listeners[incident])
source/model.ts        39) 			{
source/model.ts        40) 				handler(details);
source/model.ts        41) 			}
source/model.ts        42) 		}
source/model.ts        43) 	}
source/model.ts        44) 
source/model.ts        45) 
source/model.ts        46) 	/**
source/model.ts        47) 	 * sets the state
source/model.ts        48) 	 */
source/model.ts        49) 	export function set_state
source/model.ts        50) 	(
source/model.ts        51) 		model: type_model,
source/model.ts        52) 		state: enum_state
source/model.ts        53) 	): void
source/model.ts        54) 	{
source/model.ts        55) 		model.state = state;
source/model.ts        56) 		notify(model, "state_changed");
source/model.ts        57) 	}
source/model.ts        58) 
source/model.ts        59) 
source/model.ts        60) 	/**
source/model.ts        61) 	 * sets the active spot (channel or query)
source/model.ts        62) 	 */
source/model.ts        63) 	export function set_active
source/model.ts        64) 	(
source/model.ts        65) 		model: type_model,
source/model.ts        66) 		spot: type_spot
source/model.ts        67) 	): void
source/model.ts        68) 	{
source/model.ts        69) 		model.active = spot;
source/model.ts        70) 		notify(model, "spots_changed");
source/model.ts        71) 		notify(model, "entries_changed");
source/model.ts        72) 		notify(model, "users_changed");
source/model.ts        73) 	}
source/model.ts        74) 
source/model.ts        75) 
source/model.ts        76) 	/**
source/model.ts        77) 	 * updates the model according to a list of events
source/model.ts        78) 	 */
source/model.ts        79) 	function process_events
source/model.ts        80) 	(
source/model.ts        81) 		model: type_model,
source/model.ts        82) 		events: Array<type_event>
source/model.ts        83) 	): void
source/model.ts        84) 	{
source/model.ts        85) 		let shall_update_spots: boolean = false;
source/model.ts        86) 		let shall_update_entries: boolean = false;
source/model.ts        87) 		let shall_update_users: boolean = false;
source/model.ts        88) 		
source/model.ts        89) 		for (const event of events)
source/model.ts        90) 		{
source/model.ts        91) 			switch (event.kind)
source/model.ts        92) 			{
source/model.ts        93) 				default:
source/model.ts        94) 				{
source/model.ts        95) 					console.warn("unhandled event kind: " + event.kind);
source/model.ts        96) 					break;
source/model.ts        97) 				}
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts  98) 				case "user_list":
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts        99) 				{
source/model.ts       100) 					model.channels[event.data["channel"]].users = event.data["users"];
source/model.ts       101) 					shall_update_users = true;
source/model.ts       102) 					break;
source/model.ts       103) 				}
Christian Fraß [add] handling for "user_re...

Christian Fraß authored 2 years ago

source/logic/model.ts 104) 				case "user_renamed":
source/logic/model.ts 105) 				{
source/logic/model.ts 106) 					for (let [channel_name, channel] of Object.entries(model.channels))
source/logic/model.ts 107) 					{
source/logic/model.ts 108) 						for (let user of channel.users)
source/logic/model.ts 109) 						{
source/logic/model.ts 110) 							if (user.name === event.data["user_name_old"])
source/logic/model.ts 111) 							{
source/logic/model.ts 112) 								user.name = event.data["user_name_new"];
source/logic/model.ts 113) 								channel.entries.push
source/logic/model.ts 114) 								({
source/logic/model.ts 115) 									"timestamp": event.timestamp,
source/logic/model.ts 116) 									"kind": enum_entrykind.info,
source/logic/model.ts 117) 									"sender": null,
source/logic/model.ts 118) 									"content": `${event.data["user_name_old"]} renamed to ${event.data["user_name_new"]}`,
source/logic/model.ts 119) 								});
source/logic/model.ts 120) 								shall_update_entries = true;
source/logic/model.ts 121) 								shall_update_users = true;
source/logic/model.ts 122) 								break;
source/logic/model.ts 123) 							}
source/logic/model.ts 124) 						}
source/logic/model.ts 125) 					}
source/logic/model.ts 126) 					break;
source/logic/model.ts 127) 				}
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts 128) 				case "user_joined":
source/logic/model.ts 129) 				{
Christian Fraß [fix] info entries

Christian Fraß authored 2 years ago

source/logic/model.ts 130) 					if (model.channels.hasOwnProperty(event.data["channel"]))
source/logic/model.ts 131) 					{
source/logic/model.ts 132) 						model.channels[event.data["channel"]].users.push({"name": event.data["user_name"], "role": ""});
source/logic/model.ts 133) 						model.channels[event.data["channel"]].entries.push
source/logic/model.ts 134) 						({
source/logic/model.ts 135) 							"timestamp": event.timestamp,
source/logic/model.ts 136) 							"kind": enum_entrykind.info,
source/logic/model.ts 137) 							"sender": null,
source/logic/model.ts 138) 							"content": `${event.data["user_name"]} joined`,
source/logic/model.ts 139) 						});
source/logic/model.ts 140) 						shall_update_users = true;
source/logic/model.ts 141) 						shall_update_entries = true;
source/logic/model.ts 142) 					}
source/logic/model.ts 143) 					else
source/logic/model.ts 144) 					{
source/logic/model.ts 145) 						// do nothing
source/logic/model.ts 146) 					}
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts 147) 					break;
source/logic/model.ts 148) 				}
source/logic/model.ts 149) 				case "user_parted":
source/logic/model.ts 150) 				{
Christian Fraß [fix] info entries

Christian Fraß authored 2 years ago

source/logic/model.ts 151) 					if (model.channels.hasOwnProperty(event.data["channel"]))
source/logic/model.ts 152) 					{
source/logic/model.ts 153) 						model.channels[event.data["channel"]].users = model.channels[event.data["channel"]].users.filter
source/logic/model.ts 154) 						(
source/logic/model.ts 155) 							(user) => (user.name != event.data["user_name"])
source/logic/model.ts 156) 						);
source/logic/model.ts 157) 						model.channels[event.data["channel"]].entries.push
source/logic/model.ts 158) 						({
source/logic/model.ts 159) 							"timestamp": event.timestamp,
source/logic/model.ts 160) 							"kind": enum_entrykind.info,
source/logic/model.ts 161) 							"sender": null,
source/logic/model.ts 162) 							"content": `${event.data["user_name"]} left`,
source/logic/model.ts 163) 						});
source/logic/model.ts 164) 						shall_update_users = true;
source/logic/model.ts 165) 						shall_update_entries = true;
source/logic/model.ts 166) 					}
source/logic/model.ts 167) 					else
source/logic/model.ts 168) 					{
source/logic/model.ts 169) 						// do nothing
source/logic/model.ts 170) 					}
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts 171) 					break;
source/logic/model.ts 172) 				}
source/logic/model.ts 173) 				case "user_kicked":
source/logic/model.ts 174) 				{
Christian Fraß [fix] info entries

Christian Fraß authored 2 years ago

source/logic/model.ts 175) 					if (model.channels.hasOwnProperty(event.data["channel"]))
source/logic/model.ts 176) 					{
source/logic/model.ts 177) 						model.channels[event.data["channel"]].users = model.channels[event.data["channel"]].users.filter
source/logic/model.ts 178) 						(
source/logic/model.ts 179) 							(user) => (user.name != event.data["user_name"])
source/logic/model.ts 180) 						);
source/logic/model.ts 181) 						model.channels[event.data["channel"]].entries.push
source/logic/model.ts 182) 						({
source/logic/model.ts 183) 							"timestamp": event.timestamp,
source/logic/model.ts 184) 							"kind": enum_entrykind.info,
source/logic/model.ts 185) 							"sender": null,
source/logic/model.ts 186) 							"content": `${event.data["user_name"]} was kicked by ${event.data["op_name"]}: ${event.data["reason"]}`,
source/logic/model.ts 187) 						});
source/logic/model.ts 188) 						shall_update_users = true;
source/logic/model.ts 189) 						shall_update_entries = true;
source/logic/model.ts 190) 					}
source/logic/model.ts 191) 					else
source/logic/model.ts 192) 					{
source/logic/model.ts 193) 						// do nothing
source/logic/model.ts 194) 					}
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts 195) 					break;
source/logic/model.ts 196) 				}
source/logic/model.ts 197) 				case "user_quit":
source/logic/model.ts 198) 				{
source/logic/model.ts 199) 					for (let channel_name of event.data["channels"])
source/logic/model.ts 200) 					{
source/logic/model.ts 201) 						if (model.channels.hasOwnProperty(channel_name))
source/logic/model.ts 202) 						{
Christian Fraß [fix] info entries

Christian Fraß authored 2 years ago

source/logic/model.ts 203) 							model.channels[channel_name].users = model.channels[channel_name].users.filter
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts 204) 							(
source/logic/model.ts 205) 								(user) => (user.name != event.data["user_name"])
source/logic/model.ts 206) 							);
Christian Fraß [fix] info entries

Christian Fraß authored 2 years ago

source/logic/model.ts 207) 							model.channels[channel_name].entries.push
source/logic/model.ts 208) 							({
source/logic/model.ts 209) 								"timestamp": event.timestamp,
source/logic/model.ts 210) 								"kind": enum_entrykind.info,
source/logic/model.ts 211) 								"sender": null,
source/logic/model.ts 212) 								"content": `${event.data["user_name"]} quit`,
source/logic/model.ts 213) 							});
source/logic/model.ts 214) 							shall_update_entries = true;
source/logic/model.ts 215) 							shall_update_users = true;
Christian Fraß [mod] support user join/par...

Christian Fraß authored 2 years ago

source/logic/model.ts 216) 						}
source/logic/model.ts 217) 					}
source/logic/model.ts 218) 					break;
source/logic/model.ts 219) 				}
Christian Fraß [add] handling of "topic" e...

Christian Fraß authored 2 years ago

source/logic/model.ts 220) 				case "topic":
source/logic/model.ts 221) 				{
source/logic/model.ts 222) 					if (model.channels.hasOwnProperty(event.data["channel"]))
source/logic/model.ts 223) 					{
source/logic/model.ts 224) 						// model.channels[event.data["channel"]].topic = event.data["content"];
source/logic/model.ts 225) 						model.channels[event.data["channel"]].entries.push
source/logic/model.ts 226) 						({
source/logic/model.ts 227) 							"timestamp": event.timestamp,
source/logic/model.ts 228) 							"kind": enum_entrykind.info,
source/logic/model.ts 229) 							"sender": null,
source/logic/model.ts 230) 							"content": `channel topic: ${event.data["content"]}`,
source/logic/model.ts 231) 						});
source/logic/model.ts 232) 						shall_update_entries = true;
source/logic/model.ts 233) 					}
source/logic/model.ts 234) 					else
source/logic/model.ts 235) 					{
source/logic/model.ts 236) 						// do nothing
source/logic/model.ts 237) 					}
source/logic/model.ts 238) 					break;
source/logic/model.ts 239) 				}
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       240) 				case "message_channel":
source/model.ts       241) 				{
source/model.ts       242) 					model.channels[event.data["channel"]].entries.push
source/model.ts       243) 					({
source/model.ts       244) 						"timestamp": event.timestamp,
Christian Fraß [mod] added view for info e...

Christian Fraß authored 2 years ago

source/logic/model.ts 245) 						"kind": enum_entrykind.message,
Christian Fraß [mod] wait for server side...

Christian Fraß authored 2 years ago

source/logic/model.ts 246) 						"sender": (event.data["sender"] ?? model.nickname),
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       247) 						"content": event.data["content"],
source/model.ts       248) 					});
source/model.ts       249) 					shall_update_entries = true;
source/model.ts       250) 					break;
source/model.ts       251) 				}
source/model.ts       252) 				case "message_query":
source/model.ts       253) 				{
source/model.ts       254) 					if (! model.queries.hasOwnProperty(event.data["user_name"]))
source/model.ts       255) 					{
source/model.ts       256) 						model.queries[event.data["user_name"]] = {"entries": []};
source/model.ts       257) 						shall_update_spots = true;
source/model.ts       258) 					}
source/model.ts       259) 					else
source/model.ts       260) 					{
source/model.ts       261) 						// do nothing
source/model.ts       262) 					}
source/model.ts       263) 					model.queries[event.data["user_name"]].entries.push
source/model.ts       264) 					({
source/model.ts       265) 						"timestamp": event.timestamp,
Christian Fraß [mod] added view for info e...

Christian Fraß authored 2 years ago

source/logic/model.ts 266) 						"kind": enum_entrykind.message,
Christian Fraß [mod] wait for server side...

Christian Fraß authored 2 years ago

source/logic/model.ts 267) 						"sender": (event.data["sender"] ?? model.nickname),
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       268) 						"content": event.data["content"],
source/model.ts       269) 					});
source/model.ts       270) 					shall_update_entries = true;
source/model.ts       271) 					break;
source/model.ts       272) 				}
source/model.ts       273) 			}
source/model.ts       274) 		}
source/model.ts       275) 		
source/model.ts       276) 		if (shall_update_spots) notify(model, "spots_changed");
source/model.ts       277) 		if (shall_update_entries) notify(model, "entries_changed");
source/model.ts       278) 		if (shall_update_users) notify(model, "users_changed");
source/model.ts       279) 	}
source/model.ts       280) 
source/model.ts       281) 
source/model.ts       282) 	/**
source/model.ts       283) 	 * establishes the connection
source/model.ts       284) 	 */
source/model.ts       285) 	export async function connect
source/model.ts       286) 	(
source/model.ts       287) 		conf: type_conf,
source/model.ts       288) 		model: type_model,
source/model.ts       289) 		nickname: string,
source/model.ts       290) 		channel_names: Array<string>
source/model.ts       291) 	): Promise<void>
source/model.ts       292) 	{
source/model.ts       293) 		set_state(model, enum_state.connecting);
source/model.ts       294) 		const connection_id: string = await backend_call
source/model.ts       295) 		(
source/model.ts       296) 			conf,
source/model.ts       297) 			model.connection_id,
source/model.ts       298) 			"connect",
source/model.ts       299) 			{
source/model.ts       300) 				"server": conf.irc.server,
source/model.ts       301) 				"channels": channel_names,
source/model.ts       302) 				"nickname": nickname,
source/model.ts       303) 			}
source/model.ts       304) 		);
source/model.ts       305) 		model.connection_id = connection_id;
source/model.ts       306) 		model.nickname = nickname;
source/model.ts       307) 		for (const channel_name of channel_names)
source/model.ts       308) 		{
source/model.ts       309) 			model.channels[channel_name] =
source/model.ts       310) 			{
source/model.ts       311) 				"users": [],
source/model.ts       312) 				"entries": [],
source/model.ts       313) 			};
source/model.ts       314) 		}
source/model.ts       315) 		if (channel_names.length > 0)
source/model.ts       316) 		{
source/model.ts       317) 			set_active(model, {"kind": "channel", "name": channel_names[0]});
source/model.ts       318) 		}
source/model.ts       319) 		return Promise.resolve<void>(undefined);
source/model.ts       320) 	}
source/model.ts       321) 
source/model.ts       322) 
source/model.ts       323) 	/**
source/model.ts       324) 	 * closes the connection
source/model.ts       325) 	 */
source/model.ts       326) 	export async function disconnect
source/model.ts       327) 	(
source/model.ts       328) 		conf: type_conf,
source/model.ts       329) 		model: type_model
source/model.ts       330) 	): Promise<void>
source/model.ts       331) 	{
source/model.ts       332) 		await backend_call
source/model.ts       333) 		(
source/model.ts       334) 			conf,
source/model.ts       335) 			model.connection_id,
source/model.ts       336) 			"disconnect",
source/model.ts       337) 			null
source/model.ts       338) 		);
source/model.ts       339) 		set_state(model, enum_state.offline);
source/model.ts       340) 		model.connection_id = null;
source/model.ts       341) 		return Promise.resolve<void>(undefined);
source/model.ts       342) 	}
source/model.ts       343) 
source/model.ts       344) 
source/model.ts       345) 	/**
source/model.ts       346) 	 * adds a client side message
source/model.ts       347) 	 */
source/model.ts       348) 	export function send
source/model.ts       349) 	(
source/model.ts       350) 		conf: type_conf,
source/model.ts       351) 		model: type_model,
source/model.ts       352) 		content: string
source/model.ts       353) 	): void
source/model.ts       354) 	{
source/model.ts       355) 		switch (model.active.kind)
source/model.ts       356) 		{
source/model.ts       357) 			case "channel":
source/model.ts       358) 			{
source/model.ts       359) 				backend_call
source/model.ts       360) 				(
source/model.ts       361) 					conf,
source/model.ts       362) 					model.connection_id,
source/model.ts       363) 					"send_channel",
source/model.ts       364) 					{
source/model.ts       365) 						"channel": model.active.name,
source/model.ts       366) 						"content": content,
source/model.ts       367) 					}
source/model.ts       368) 				);
Christian Fraß [mod] wait for server side...

Christian Fraß authored 2 years ago

source/logic/model.ts 369) 				/*
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       370) 				const event: type_event =
source/model.ts       371) 				{
source/model.ts       372) 					"timestamp": get_timestamp(),
source/model.ts       373) 					"kind": "message_channel",
source/model.ts       374) 					"data":
source/model.ts       375) 					{
source/model.ts       376) 						"channel": model.active.name,
source/model.ts       377) 						"sender": model.nickname,
source/model.ts       378) 						"content": content,
source/model.ts       379) 					}
source/model.ts       380) 				};
source/model.ts       381) 				process_events(model, [event]);
source/model.ts       382) 				notify(model, "entries_changed");
Christian Fraß [mod] wait for server side...

Christian Fraß authored 2 years ago

source/logic/model.ts 383) 				 */
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       384) 				notify(model, "message_sent");
source/model.ts       385) 				break;
source/model.ts       386) 			}
source/model.ts       387) 			case "query":
source/model.ts       388) 			{
source/model.ts       389) 				backend_call
source/model.ts       390) 				(
source/model.ts       391) 					conf,
source/model.ts       392) 					model.connection_id,
source/model.ts       393) 					"send_query",
source/model.ts       394) 					{
source/model.ts       395) 						"receiver": model.active.name,
source/model.ts       396) 						"content": content,
source/model.ts       397) 					}
source/model.ts       398) 				);
Christian Fraß [mod] wait for server side...

Christian Fraß authored 2 years ago

source/logic/model.ts 399) 				/*
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       400) 				const event: type_event =
source/model.ts       401) 				{
source/model.ts       402) 					"timestamp": get_timestamp(),
source/model.ts       403) 					"kind": "message_query",
source/model.ts       404) 					"data":
source/model.ts       405) 					{
source/model.ts       406) 						"user_name": model.active.name,
source/model.ts       407) 						"sender": model.nickname,
source/model.ts       408) 						"content": content,
source/model.ts       409) 					}
source/model.ts       410) 				};
source/model.ts       411) 				process_events(model, [event]);
source/model.ts       412) 				notify(model, "entries_changed");
Christian Fraß [mod] wait for server side...

Christian Fraß authored 2 years ago

source/logic/model.ts 413) 				 */
Christian Fraß [mod] clean model-view-control

Christian Fraß authored 2 years ago

source/model.ts       414) 				notify(model, "message_sent");
source/model.ts       415) 				break;
source/model.ts       416) 			}
source/model.ts       417) 		}
source/model.ts       418) 	}
source/model.ts       419) 	
source/model.ts       420) 	
Christian Fraß [add] possibility to open q...

Christian Fraß authored 2 years ago

source/logic/model.ts 421) 	/**
source/logic/model.ts 422) 	 * adds a query to a user as spot
source/logic/model.ts 423) 	 */
source/logic/model.ts 424) 	export function open_query
source/logic/model.ts 425) 	(
source/logic/model.ts 426) 		model: type_model,
source/logic/model.ts 427) 		user_name: string
source/logic/model.ts 428) 	): void
source/logic/model.ts 429) 	{
source/logic/model.ts 430) 		if ((user_name !== model.nickname) && (! model.queries.hasOwnProperty(user_name)))
source/logic/model.ts 431) 		{
source/logic/model.ts 432) 			model.queries[user_name] = {"entries": []};
Christian Fraß [mod] auto focus on message...

Christian Fraß authored 2 years ago

source/logic/model.ts 433) 			set_active(model, {"kind": "query", "name": user_name});
Christian Fraß [add] possibility to open q...

Christian Fraß authored 2 years ago

source/logic/model.ts 434) 		}
source/logic/model.ts 435) 		else
source/logic/model.ts 436) 		{
source/logic/model.ts 437) 			// do nothing
source/logic/model.ts 438) 		}
source/logic/model.ts 439) 	}
source/logic/model.ts 440) 	
source/logic/model.ts 441)