Example #1
0
    def send(cls, request, params):
        uid = request.user.id
        target_id = params['to']
        body = params['body']
        message = Message(to=target_id, sender=uid, body=body)
        message.save()
        attachment_paths = []
        attachments = params.get('attachments')
        if attachments:
            for attachment in attachments:
                file_name = attachment['name']
                doc = MessageDocument(
                    message_id=message.id,
                    file_type='message',
                    name=file_name
                )

                image_data = attachment['binary']
                image_data = image_data.encode()
                image_data = ContentFile(base64.b64decode(image_data))
                file_name = attachment['name']
                doc.attachment.save(file_name, image_data, save=True)

                attachment_paths.append({
                    'name': file_name,
                    'path': doc.pdf_doc.url
                })
        message = message.__dict__
        message['attachments'] = attachment_paths
        del message['_state']
        events = [
            {'name': 'chat_message_received', 'data': message, 'audience': [target_id]}
        ]
        res = ws_methods.emit_event(events)
        return res
Example #2
0
    def add_members(cls, request, params):
        audience = []
        uid = request.user.id
        member_ids = []
        group_id = params['group_id']
        for obj in params.get('members'):
            member_ids.append(obj['id'])
            audience.append(obj['id'])

        chat_group = ChatGroup.objects.get(pk=group_id)
        existing_members_ids = []
        for mem in chat_group.members:
            existing_members_ids.append(mem.id)
        member_ids = member_ids + existing_members_ids
        chat_group.members.set(member_ids)
        chat_group.save()

        group_members = []
        for mem in chat_group.members.all():
            group_members.append({
                'id': mem.id,
                'name': mem.name,
                'photo': mem.image.url,
            })
        group = {
            'id': chat_group.id,
            'name': chat_group.name,
            'members': group_members,
            'created_by': {
                'id': chat_group.created_by.id,
                'name': chat_group.created_by.name,
                'photo': chat_group.created_by.image.url,
            },
            'is_group': True,
            'create_time': str(chat_group.create_time),
        }

        event1 = {
            'name': 'chat_group_created',
            'data': group,
            'audience': member_ids
        }
        event2 = {
            'name': 'members_added_to_group',
            'data': group,
            'audience': existing_members_ids
        }
        events = [event1, event2]
        res = ws_methods.emit_event(events)
        if res == 'done':
            return {'error': '', 'data': group}
        else:
            return res
Example #3
0
 def save_comment(cls, request, params):
     doc_id = params.get('parent_res_id')
     user_id = request.user.id
     point = params.get('point')
     if point:
         point['document_id'] = doc_id
         user_point = cls.save_point(point)
         point_id = user_point.get('point_id')
         new_point = user_point.get('new_point')
         comment = point.get('comment')
         comment_uuid = comment.get('uuid')
         comment_body = comment.get('content')
         comment_uid = comment.get('uid')
         comment_date_time = comment.get('date_time')
         if comment:
             comment = CommentAnnotation(body=comment_body,
                                         point_id_id=point_id,
                                         user_id=comment_uid,
                                         date_time=comment_date_time,
                                         uuid=comment_uuid)
             comment.save()
             res = {}
             res['point'] = point
             point['id'] = point_id
             res['new_point'] = new_point
             doc = File.objects.get(pk=doc_id)
             attendees = []
             if doc:
                 meeting_doc = doc.meetingdocument
                 if meeting_doc:
                     meeting = meeting_doc.meeting
                     if meeting:
                         meeting_attendees = meeting.attendees.all()
                         if meeting_attendees:
                             for attendee in meeting_attendees:
                                 if user_id != attendee.id:
                                     attendees.append(attendee.id)
             events = [{
                 'name': 'point_comment_received',
                 'data': res,
                 'audience': attendees
             }]
             res = ws_methods.emit_event(events)
             return res
     else:
         return 'Invalid Point'
Example #4
0
    def create(cls, request, params):
        uid = request.user.id
        me_added = False
        member_ids = []
        for obj in params.get('members'):
            member_ids.append(obj['id'])
        chat_group = ChatGroup(name=params['name'],
                               created_by_id=uid,
                               owner_id=uid)
        chat_group.save()
        if not me_added:
            member_ids.append(uid)
        chat_group.members.set(member_ids)
        chat_group.save()

        owner = chat_group.owner
        created_chat_group = {
            'name': params['name'],
            'id': chat_group.id,
            'members': params.get('members'),
            'owner': {
                'id': uid,
                'name': owner.fullname(),
                'image': owner.image.url
            }
        }
        events = [{
            'name': 'chat_group_created',
            'data': created_chat_group,
            'audience': member_ids
        }]
        res = ws_methods.emit_event(events)
        if res == 'done':
            return {'error': '', 'data': created_chat_group}
        else:
            return res
Example #5
0
    def add_notification(cls, params, event_data):

        res_model = params['res_model']
        res_app = params['res_app']
        res_id = params['res_id']
        res_id = int(res_id)

        model = apps.get_model(res_app, res_model)
        obj_res = model.objects.get(pk=res_id)
        audience = obj_res.get_audience()
        if not audience:
            return 'No Audience'

        res_type = params.get('res_type')
        if not res_type:
            res_type = 'comment'

        template = params.get('template')
        if not template:
            template = ' comment(s) '

        notification_type = NotificationType.objects.filter(
            res_app=res_app, res_model=res_model, res_type=res_type
        )
        if not notification_type:
            notification_type = NotificationType(
                res_app=res_app, res_model=res_model,
                res_type=res_type, template=template
            )
            notification_type.save()
        else:
            notification_type = notification_type[0]

        for uid in audience:
            notification = Notification.objects.filter(
                notification_type_id=notification_type.id,
                res_id=res_id,user_id=uid
            )
            if not notification:
                notification = Notification(
                    res_id=res_id,
                    user_id=uid,
                    notification_type_id=notification_type.id
                )
                notification.save()
            else:
                notification = notification[0]
                notification.counter += 1
                notification.save()

        if len(audience) > 0:
            note = notification_type.template
            note = {
                'res_id': res_id,
                'res_model': res_model,
                'res_app': res_app,
                'res_type': res_type,
                'body': note,
                'counter':1
            }
            events = [
                {'name': 'notification_received', 'data': note, 'audience': audience},
                {'name': event_data['name'], 'data': event_data['data'], 'audience': audience}
            ]
            res = ws_methods.emit_event(events)
        else:
            return 'No audience for the notification'
        return res
Example #6
0
    def send(cls, request, params):
        uid = request.user.id
        group_id = params.get('group_id')
        target_id = params.get('to')
        body = params['body']
        message_type = params.get('message_type')
        message = Message(sender_id=uid,
                          body=body,
                          create_date=datetime.now(),
                          message_type=message_type)
        if group_id:
            message.chat_group_id = group_id
        else:
            message.to = target_id
        message.save()
        attachment_urls = []
        attachments = params.get('attachments')
        if attachments:
            for attachment in attachments:
                file_name = attachment['name']
                doc = MessageDocument(
                    message_id=message.id,
                    file_type='message',
                    name=file_name,
                    file_name=file_name,
                )

                image_data = attachment['binary']
                image_data = ws_methods.base64_str_to_file(
                    image_data, file_name)

                doc.attachment.save(file_name, image_data, save=True)
                attachment_urls.append({
                    'name': file_name,
                    'id': doc.id,
                    'file_type': doc.extention,
                    'url': doc.attachment.url,
                })

        message_dict = message.__dict__
        message_dict['sender'] = {
            'id': message.sender.id,
            'name': message.sender.name,
            'photo': message.sender.image.url,
        }
        if message.chat_group:
            message_dict['chat_group'] = {
                'id': message.chat_group.id,
                'name': message.chat_group.name,
            }
        message_dict['attachments'] = attachment_urls
        message_dict['create_date'] = str(datetime.now())

        del message_dict['_state']
        ws_methods.stringify_fields(message_dict)
        message_dict['uuid'] = params['uuid']
        events = [{
            'name': 'chat_message_received',
            'data': message_dict,
            'audience': [target_id]
        }]
        if group_id:
            events = [{
                'name': 'group_chat_message_received',
                'data': message_dict,
                'room': {
                    'type': 'chat_room',
                    'id': group_id
                }
            }]
        res = ws_methods.emit_event(events)
        if res == 'done':
            return message_dict
        else:
            return res
Example #7
0
    def add_notification(cls, sender, params, event_data, mentioned_list=None):
        type_name = params['notification_type']
        file_type = params.get('file_type')
        res_model = params['res_model']
        res_app = params['res_app']
        res_id = params['res_id']

        post_address = cls.get_post_address(res_app, res_model, res_id)
        if mentioned_list:
            mention_notification_type = cls.get_notification_type('mention')
            mention_notification = cls.get_notification(
                mention_notification_type.id, post_address.id)

        notification_type = cls.get_notification_type(type_name)
        notification = cls.get_notification(notification_type.id,
                                            post_address.id)

        model = apps.get_model(res_app, res_model)
        obj_res = model.objects.get(pk=res_id)
        audience = None
        try:
            audience = obj_res.get_audience()
        except:
            return 'get audience not defined for ' + res_app + '.' + res_model
        if not audience:
            return 'No Audience'
        if sender.id in audience:
            audience.remove(sender.id)
        senders_for_mention = {}
        count = 0
        if mentioned_list:
            for uid in mentioned_list:
                user_notification = UserNotification(
                    notification_id=mention_notification.id,
                    sender_id=sender.id,
                    user_id=uid)
                user_notification.save()
                senders_for_mention[uid], count = UserNotification.get_senders(
                    cls, uid, mention_notification.id)
            audience = list(set(audience) - set(mentioned_list))
        senders_for_all = {}
        for uid in audience:
            user_notification = UserNotification(
                notification_id=notification.id,
                sender_id=sender.id,
                user_id=uid)
            user_notification.save()
            senders_for_all[uid], count = UserNotification.get_senders(
                cls, uid, notification.id)

        meta = notification.get_meta(obj_res)
        # text = ' ' + meta['template'] + ' ' + meta['name_place']
        if len(audience) > 0:
            client_object = {
                'id': notification.id,
                'body': meta['text'],
                'senders': senders_for_all,
                'notification_type': notification_type.name,
                'address': {
                    'res_id': post_address.res_id,
                    'res_model': post_address.res_model,
                    'res_app': post_address.res_app,
                    'info': meta['info']
                }
            }
            events = [
                {
                    'name': 'notification_received',
                    'data': client_object,
                    'audience': audience
                },
            ]
            if mentioned_list:
                mention_meta = mention_notification.get_meta(obj_res)
                clone = client_object.copy()
                clone['id'] = mention_notification.id
                clone['audience'] = mentioned_list
                clone['senders'] = senders_for_mention
                clone['notification_type'] = mention_notification_type.name
                clone['body'] = mention_meta['text']
                events.append({
                    'name': 'notification_received',
                    'data': clone,
                    'audience': mentioned_list
                })
                emit_data = {
                    'name': event_data['name'],
                    'data': event_data['data'],
                    'audience': audience + mentioned_list
                }
                events.append(emit_data)
            else:
                events.append({
                    'name': event_data['name'],
                    'data': event_data['data'],
                    'audience': audience
                })
            res = ws_methods.emit_event(events)
        else:
            return 'No audience for the notification'
        return res