#!/usr/bin/env python3

# Copyright © 2014 Dropbox, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import argparse
import json
import os
import sys

usage = """Export all messages on a given stream to a JSON dump.

zulip-export --user=<your email address> --api-key=<your API key> --stream=<stream name>

(You can find your API key on your Settings page.)

Example: zulip-export --user=wdaher@zulip.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --stream=social

You can omit --user and --api-key arguments if you have a properly set up ~/.zuliprc
This script requires the Zulip API bindings to be installed."""
sys.path.append(os.path.join(os.path.dirname(__file__), "../../api"))
import zulip

parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage))
parser.add_argument("--stream", required=True)
options = parser.parse_args()

client = zulip.init_from_options(options)

client.add_subscriptions([{"name": options.stream}])
queue = client.register(event_types=["message"])
max_id = queue["max_message_id"]
messages = []
request = {
    "anchor": 0,
    "num_before": 0,
    "num_after": max_id,
    "narrow": [{"operator": "stream", "operand": options.stream}],
    "apply_markdown": False,
}

print("Fetching messages...")
result = client.call_endpoint(
    url="messages",
    method="GET",
    request=request,
)

if result["result"] != "success":
    print("Unfortunately, there was an error fetching some old messages.")
    print(result)
    sys.exit(1)
for msg in result["messages"]:
    if msg["type"] != "stream":
        continue
    # Remove extraneous metadata
    for k in [
        "flags",
        "edit_history",
        "topic_links",
        "avatar_url",
        "recipient_id",
        "content_type",
        "client",
        "sender_realm_str",
        "id",
        "type",
    ]:
        msg.pop(k, None)
    messages.append(msg)

filename = f"zulip-{options.stream}.json"
with open(filename, "w") as f:
    json.dump(messages, f, indent=0, sort_keys=False)
print(f"{len(messages)} messages exported to {filename}")
sys.exit(0)
