Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 39598
1
# 3rd Party Libraries
2
immutable = require('immutable')
3
4
# Internal Libraries
5
{Actions} = require('../smc-react')
6
{webapp_client} = require('../webapp_client')
7
8
# Sibling Libraries
9
10
class ChatActions extends Actions
11
_process_syncdb_obj: (x) =>
12
if x.event != 'chat'
13
# Event used to be used for video chat, etc...; but we have a better approach now, so
14
# all events we care about are chat.
15
return
16
if x.video_chat?.is_video_chat
17
# discard/ignore anything else related to the old old video chat approach
18
return
19
x.date = new Date(x.date)
20
if x.history?.length > 0
21
# nontrivial history -- nothing to do
22
else if x.payload?
23
# for old chats with payload: content (2014-2016)... plus the script @hsy wrote in the work project ;-(
24
x.history = []
25
x.history.push
26
content : x.payload.content
27
author_id : x.sender_id
28
date : new Date(x.date)
29
delete x.payload
30
else if x.mesg?
31
# for old chats with mesg: content (up to 2014)
32
x.history = []
33
x.history.push
34
content : x.mesg.content
35
author_id : x.sender_id
36
date : new Date(x.date)
37
delete x.mesg
38
x.history ?= []
39
if not x.editing
40
x.editing = {}
41
return x
42
43
# Initialize the state of the store from the contents of the syncdb.
44
init_from_syncdb: () =>
45
v = {}
46
for x in @syncdb.get().toJS()
47
x = @_process_syncdb_obj(x)
48
if x?
49
v[x.date - 0] = x
50
51
@setState
52
messages : immutable.fromJS(v)
53
54
_syncdb_change: (changes) =>
55
messages_before = messages = @store.get('messages')
56
if not messages?
57
# Messages need not be defined when changes appear in case of problems or race.
58
return
59
changes.map (obj) =>
60
obj.date = new Date(obj.date)
61
record = @syncdb.get_one(obj)
62
x = record?.toJS()
63
if not x?
64
# delete
65
messages = messages.delete(obj.date - 0)
66
else
67
# TODO/OPTIMIZATION: make into custom conversion to immutable (when rewrite)
68
x = @_process_syncdb_obj(x)
69
if x?
70
messages = messages.set("#{x.date - 0}", immutable.fromJS(x))
71
if not messages_before.equals(messages)
72
@setState(messages: messages)
73
74
send_chat: (mesg) =>
75
if not @syncdb?
76
# WARNING: give an error or try again later?
77
return
78
sender_id = @redux.getStore('account').get_account_id()
79
time_stamp = webapp_client.server_time().toISOString()
80
@syncdb.set
81
sender_id : sender_id
82
event : "chat"
83
history : [{author_id: sender_id, content:mesg, date:time_stamp}]
84
date : time_stamp
85
@syncdb.save()
86
@setState(last_sent: mesg)
87
88
set_editing: (message, is_editing) =>
89
if not @syncdb?
90
# WARNING: give an error or try again later?
91
return
92
author_id = @redux.getStore('account').get_account_id()
93
94
if is_editing
95
# FUTURE: Save edit changes
96
editing = message.get('editing').set(author_id, 'FUTURE')
97
else
98
editing = message.get('editing').set(author_id, null)
99
100
# console.log("Currently Editing:", editing.toJS())
101
@syncdb.set
102
history : message.get('history').toJS()
103
editing : editing.toJS()
104
date : message.get('date').toISOString()
105
106
# Used to edit sent messages.
107
# **Extremely** shockingly inefficient. Assumes number of edits is small.
108
send_edit: (message, mesg) =>
109
if not @syncdb?
110
# WARNING: give an error or try again later?
111
return
112
author_id = @redux.getStore('account').get_account_id()
113
# OPTIMIZATION: send less data over the network?
114
time_stamp = webapp_client.server_time().toISOString()
115
116
@syncdb.set
117
history : [{author_id: author_id, content:mesg, date:time_stamp}].concat(message.get('history').toJS())
118
editing : message.get('editing').set(author_id, null).toJS()
119
date : message.get('date').toISOString()
120
@syncdb.save()
121
122
set_to_last_input: =>
123
@setState(input:@store.get('last_sent'))
124
125
set_input: (input) =>
126
@setState(input:input)
127
128
saved_message: (saved_mesg) =>
129
@setState(saved_mesg:saved_mesg)
130
131
set_is_preview: (is_preview) =>
132
@setState(is_preview:is_preview)
133
134
set_use_saved_position: (use_saved_position) =>
135
@setState(use_saved_position:use_saved_position)
136
137
save_scroll_state: (position, height, offset) =>
138
# height == 0 means chat room is not rendered
139
if height != 0
140
@setState(saved_position:position, height:height, offset:offset)
141
142
exports.ChatActions = ChatActions
143